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.
5 #ifndef COMPONENTS_MEDIA_DEVICE_SALT_MEDIA_DEVICE_SALT_SERVICE_H_
6 #define COMPONENTS_MEDIA_DEVICE_SALT_MEDIA_DEVICE_SALT_SERVICE_H_
11 #include "base/feature_list.h"
12 #include "base/functional/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/sequence_bound.h"
15 #include "base/time/time.h"
16 #include "base/types/expected.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "components/media_device_salt/media_device_salt_database.h"
19 #include "content/public/browser/storage_partition.h"
20 #include "third_party/blink/public/common/storage_key/storage_key.h"
24 namespace media_device_salt {
26 BASE_DECLARE_FEATURE(kMediaDeviceIdPartitioning);
27 BASE_DECLARE_FEATURE(kMediaDeviceIdRandomSaltsPerStorageKey);
29 class MediaDeviceIDSalt;
31 // Service that manages salts used to generate media device IDs.
32 class MediaDeviceSaltService : public KeyedService {
34 explicit MediaDeviceSaltService(PrefService* pref_service,
35 const base::FilePath& path);
36 ~MediaDeviceSaltService() override;
38 MediaDeviceSaltService(const MediaDeviceSaltService&) = delete;
39 MediaDeviceSaltService& operator=(const MediaDeviceSaltService&) = delete;
41 // Returns the salt for the given `storage_ket` via `callback`.
42 void GetSalt(const blink::StorageKey& storage_key,
43 base::OnceCallback<void(const std::string&)> callback);
45 // Deletes salts in the given time range whose storage keys match the given
46 // `matcher`. If `matcher` is null, all entries in the given time range are
47 // deleted. `done_closure` is invoked after the operation is complete.
48 void DeleteSalts(base::Time delete_begin,
49 base::Time delete_end,
50 content::StoragePartition::StorageKeyMatcherFunction matcher,
51 base::OnceClosure done_closure);
53 // Deletes the salt for the given `storage_key`. `done_closure` is invoked
54 // after the operation is complete.
55 void DeleteSalt(const blink::StorageKey& storage_key,
56 base::OnceClosure done_closure);
58 // Returns all the storage keys that have an associated salt (via `callback`).
59 void GetAllStorageKeys(
60 base::OnceCallback<void(std::vector<blink::StorageKey>)> callback);
63 void FinalizeGetSalt(base::OnceCallback<void(const std::string&)> callback,
64 absl::optional<std::string> new_salt);
65 void FinalizeDeleteSalts(base::OnceClosure done_closure);
66 void FinalizeGetAllStorageKeys(
67 base::OnceCallback<void(std::vector<blink::StorageKey>)> callback,
68 std::vector<blink::StorageKey> storage_keys);
70 // TODO(crbug.com/1462956): Remove these operations.
71 std::string GetGlobalSalt();
72 void ResetGlobalSalt();
74 // Ephemeral salt for opaque origins or if the database is broken.
75 std::string fallback_salt_;
76 base::Time fallback_salt_creation_time_;
78 // TODO(crbug.com/1462956): Remove these two fields.
79 const scoped_refptr<MediaDeviceIDSalt> media_device_id_salt_;
80 const raw_ptr<PrefService> pref_service_;
82 SEQUENCE_CHECKER(sequence_checker_);
83 base::SequenceBound<MediaDeviceSaltDatabase> db_
84 GUARDED_BY_CONTEXT(sequence_checker_);
85 base::WeakPtrFactory<MediaDeviceSaltService> weak_factory_
86 GUARDED_BY_CONTEXT(sequence_checker_){this};
89 } // namespace media_device_salt
91 #endif // COMPONENTS_MEDIA_DEVICE_SALT_MEDIA_DEVICE_SALT_SERVICE_H_