Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/media_device_salt/media_device_salt_service.h" |
| 6 | |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
| 9 | #include "base/containers/cxx20_erase_vector.h" |
| 10 | #include "base/feature_list.h" |
| 11 | #include "base/functional/bind.h" |
| 12 | #include "base/system/system_monitor.h" |
| 13 | #include "base/task/sequenced_task_runner.h" |
| 14 | #include "base/task/task_traits.h" |
| 15 | #include "base/task/thread_pool.h" |
| 16 | #include "base/threading/sequence_bound.h" |
| 17 | #include "base/time/time.h" |
| 18 | #include "base/unguessable_token.h" |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 19 | #include "components/media_device_salt/media_device_id_salt.h" |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 20 | #include "components/media_device_salt/media_device_salt_database.h" |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 21 | |
| 22 | namespace media_device_salt { |
| 23 | |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 24 | BASE_FEATURE(kMediaDeviceIdPartitioning, |
| 25 | "MediaDeviceIdPartitioning", |
| 26 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 27 | BASE_FEATURE(kMediaDeviceIdRandomSaltsPerStorageKey, |
| 28 | "MediaDeviceIdRandomSaltsPerStorageKey", |
| 29 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | scoped_refptr<base::SequencedTaskRunner> CreateDatabaseTaskRunner() { |
| 34 | // We use a SequencedTaskRunner so that there is a global ordering to a |
| 35 | // storage key's directory operations. |
| 36 | return base::ThreadPool::CreateSequencedTaskRunner({ |
| 37 | base::MayBlock(), // For File I/O |
| 38 | base::TaskPriority::USER_VISIBLE, |
| 39 | base::TaskShutdownBehavior::BLOCK_SHUTDOWN, // To allow clean shutdown |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | MediaDeviceSaltService::MediaDeviceSaltService(PrefService* pref_service, |
| 46 | const base::FilePath& path) |
| 47 | : fallback_salt_(CreateRandomSalt()), |
| 48 | fallback_salt_creation_time_(base::Time::Now()), |
| 49 | media_device_id_salt_( |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 50 | base::MakeRefCounted<MediaDeviceIDSalt>(pref_service)), |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 51 | pref_service_(pref_service), |
| 52 | db_(base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning) |
| 53 | ? base::SequenceBound<MediaDeviceSaltDatabase>( |
| 54 | CreateDatabaseTaskRunner(), |
| 55 | path) |
| 56 | : base::SequenceBound<MediaDeviceSaltDatabase>()) {} |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 57 | |
| 58 | MediaDeviceSaltService::~MediaDeviceSaltService() = default; |
| 59 | |
| 60 | void MediaDeviceSaltService::GetSalt( |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 61 | const blink::StorageKey& storage_key, |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 62 | base::OnceCallback<void(const std::string&)> callback) { |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 63 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 64 | if (!base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning)) { |
| 65 | GetSalt(std::move(callback)); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (storage_key.origin().opaque()) { |
| 70 | std::move(callback).Run(fallback_salt_); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | absl::optional<std::string> candidate_salt; |
| 75 | if (!base::FeatureList::IsEnabled(kMediaDeviceIdRandomSaltsPerStorageKey)) { |
| 76 | candidate_salt = GetGlobalSalt(); |
| 77 | } |
| 78 | |
| 79 | db_.AsyncCall(&MediaDeviceSaltDatabase::GetOrInsertSalt) |
| 80 | .WithArgs(storage_key, candidate_salt) |
| 81 | .Then(base::BindOnce(&MediaDeviceSaltService::FinalizeGetSalt, |
| 82 | weak_factory_.GetWeakPtr(), std::move(callback))); |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 83 | } |
| 84 | |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 85 | void MediaDeviceSaltService::FinalizeGetSalt( |
| 86 | base::OnceCallback<void(const std::string&)> callback, |
| 87 | absl::optional<std::string> salt) { |
| 88 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 89 | std::move(callback).Run(salt.has_value() ? *salt : fallback_salt_); |
| 90 | } |
| 91 | |
| 92 | void MediaDeviceSaltService::GetSalt( |
| 93 | base::OnceCallback<void(const std::string&)> callback) { |
| 94 | std::move(callback).Run(GetGlobalSalt()); |
| 95 | } |
| 96 | |
| 97 | void MediaDeviceSaltService::DeleteSalts( |
| 98 | base::Time delete_begin, |
| 99 | base::Time delete_end, |
| 100 | content::StoragePartition::StorageKeyMatcherFunction matcher, |
| 101 | base::OnceClosure done_closure) { |
| 102 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 103 | if (matcher) { |
| 104 | if (!base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning)) { |
| 105 | std::move(done_closure).Run(); |
| 106 | return; |
| 107 | } |
| 108 | } else { |
| 109 | if (!base::FeatureList::IsEnabled(kMediaDeviceIdRandomSaltsPerStorageKey) || |
| 110 | !base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning)) { |
| 111 | ResetGlobalSalt(); |
| 112 | } |
| 113 | if (!base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning)) { |
Guido Urdaneta | 62ddc251 | 2023-07-18 12:53:18 | [diff] [blame^] | 114 | FinalizeDeleteSalts(std::move(done_closure)); |
Guido Urdaneta | fcee7ad | 2023-07-18 00:04:20 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Reset the fallback key if the deletion period includes its creation time. |
| 119 | if (delete_begin <= fallback_salt_creation_time_ && |
| 120 | fallback_salt_creation_time_ <= delete_end) { |
| 121 | fallback_salt_ = CreateRandomSalt(); |
| 122 | fallback_salt_creation_time_ = base::Time::Now(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | db_.AsyncCall(&MediaDeviceSaltDatabase::DeleteEntries) |
| 127 | .WithArgs(delete_begin, delete_end, std::move(matcher)) |
| 128 | .Then(base::BindOnce(&MediaDeviceSaltService::FinalizeDeleteSalts, |
| 129 | weak_factory_.GetWeakPtr(), |
| 130 | std::move(done_closure))); |
| 131 | } |
| 132 | |
| 133 | void MediaDeviceSaltService::FinalizeDeleteSalts( |
| 134 | base::OnceClosure done_closure) { |
| 135 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 136 | // Propagate device change notifications, for anything currently using devices |
| 137 | // which will now have new IDs. |
| 138 | if (base::SystemMonitor* monitor = base::SystemMonitor::Get()) { |
| 139 | monitor->ProcessDevicesChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); |
| 140 | monitor->ProcessDevicesChanged(base::SystemMonitor::DEVTYPE_AUDIO); |
| 141 | } |
| 142 | std::move(done_closure).Run(); |
| 143 | } |
| 144 | |
| 145 | std::string MediaDeviceSaltService::GetGlobalSalt() { |
| 146 | return media_device_id_salt_->GetSalt(); |
| 147 | } |
| 148 | |
| 149 | void MediaDeviceSaltService::ResetGlobalSalt() { |
Guido Urdaneta | 9f831c2 | 2023-06-22 13:44:34 | [diff] [blame] | 150 | MediaDeviceIDSalt::Reset(pref_service_); |
| 151 | } |
| 152 | |
| 153 | } // namespace media_device_salt |