[go: nahoru, domu]

blob: 3011e2925123d26c056d70f9e8297a6cf4c6a508 [file] [log] [blame]
Guido Urdaneta9f831c22023-06-22 13:44:341// 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 Urdanetafcee7ad2023-07-18 00:04:207#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 Urdaneta9f831c22023-06-22 13:44:3419#include "components/media_device_salt/media_device_id_salt.h"
Guido Urdanetafcee7ad2023-07-18 00:04:2020#include "components/media_device_salt/media_device_salt_database.h"
Guido Urdaneta9f831c22023-06-22 13:44:3421
22namespace media_device_salt {
23
Guido Urdanetafcee7ad2023-07-18 00:04:2024BASE_FEATURE(kMediaDeviceIdPartitioning,
25 "MediaDeviceIdPartitioning",
26 base::FEATURE_DISABLED_BY_DEFAULT);
27BASE_FEATURE(kMediaDeviceIdRandomSaltsPerStorageKey,
28 "MediaDeviceIdRandomSaltsPerStorageKey",
29 base::FEATURE_DISABLED_BY_DEFAULT);
30
31namespace {
32
33scoped_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
45MediaDeviceSaltService::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 Urdaneta9f831c22023-06-22 13:44:3450 base::MakeRefCounted<MediaDeviceIDSalt>(pref_service)),
Guido Urdanetafcee7ad2023-07-18 00:04:2051 pref_service_(pref_service),
52 db_(base::FeatureList::IsEnabled(kMediaDeviceIdPartitioning)
53 ? base::SequenceBound<MediaDeviceSaltDatabase>(
54 CreateDatabaseTaskRunner(),
55 path)
56 : base::SequenceBound<MediaDeviceSaltDatabase>()) {}
Guido Urdaneta9f831c22023-06-22 13:44:3457
58MediaDeviceSaltService::~MediaDeviceSaltService() = default;
59
60void MediaDeviceSaltService::GetSalt(
Guido Urdanetafcee7ad2023-07-18 00:04:2061 const blink::StorageKey& storage_key,
Guido Urdaneta9f831c22023-06-22 13:44:3462 base::OnceCallback<void(const std::string&)> callback) {
Guido Urdanetafcee7ad2023-07-18 00:04:2063 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 Urdaneta9f831c22023-06-22 13:44:3483}
84
Guido Urdanetafcee7ad2023-07-18 00:04:2085void 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
92void MediaDeviceSaltService::GetSalt(
93 base::OnceCallback<void(const std::string&)> callback) {
94 std::move(callback).Run(GetGlobalSalt());
95}
96
97void 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 Urdaneta62ddc2512023-07-18 12:53:18114 FinalizeDeleteSalts(std::move(done_closure));
Guido Urdanetafcee7ad2023-07-18 00:04:20115 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
133void 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
145std::string MediaDeviceSaltService::GetGlobalSalt() {
146 return media_device_id_salt_->GetSalt();
147}
148
149void MediaDeviceSaltService::ResetGlobalSalt() {
Guido Urdaneta9f831c22023-06-22 13:44:34150 MediaDeviceIDSalt::Reset(pref_service_);
151}
152
153} // namespace media_device_salt