xref: /aosp_15_r20/external/cronet/net/extras/sqlite/sqlite_persistent_shared_dictionary_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_SHARED_DICTIONARY_STORE_H_
6 #define NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_SHARED_DICTIONARY_STORE_H_
7 
8 #include <map>
9 #include <set>
10 #include <vector>
11 
12 #include "base/component_export.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/scoped_refptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/sequence_checker.h"
17 #include "base/time/time.h"
18 #include "base/types/expected.h"
19 #include "base/unguessable_token.h"
20 #include "net/extras/shared_dictionary/shared_dictionary_info.h"
21 #include "net/extras/shared_dictionary/shared_dictionary_usage_info.h"
22 #include "url/origin.h"
23 
24 namespace base {
25 class FilePath;
26 class SequencedTaskRunner;
27 }  // namespace base
28 
29 namespace net {
30 
31 class SharedDictionaryIsolationKey;
32 
33 // This class is used for storing SharedDictionary information to the persistent
34 // storage.
COMPONENT_EXPORT(NET_EXTRAS)35 class COMPONENT_EXPORT(NET_EXTRAS) SQLitePersistentSharedDictionaryStore {
36  public:
37   // These values are persisted to logs. Entries should not be renumbered and
38   // numeric values should never be reused.
39   enum class Error {
40     kOk = 0,
41     kFailedToInitializeDatabase = 1,
42     kInvalidSql = 2,
43     kFailedToExecuteSql = 3,
44     kFailedToBeginTransaction = 4,
45     kFailedToCommitTransaction = 5,
46     kInvalidTotalDictSize = 6,
47     kFailedToGetTotalDictSize = 7,
48     kFailedToSetTotalDictSize = 8,
49     kTooBigDictionary = 9,
50     kMaxValue = kTooBigDictionary
51   };
52   class COMPONENT_EXPORT(NET_EXTRAS) RegisterDictionaryResult {
53    public:
54     RegisterDictionaryResult(
55         int64_t primary_key_in_database,
56         std::optional<base::UnguessableToken> replaced_disk_cache_key_token,
57         std::set<base::UnguessableToken> evicted_disk_cache_key_tokens,
58         uint64_t total_dictionary_size,
59         uint64_t total_dictionary_count);
60     ~RegisterDictionaryResult();
61 
62     RegisterDictionaryResult(const RegisterDictionaryResult& other);
63     RegisterDictionaryResult(RegisterDictionaryResult&& other);
64     RegisterDictionaryResult& operator=(const RegisterDictionaryResult& other);
65     RegisterDictionaryResult& operator=(RegisterDictionaryResult&& other);
66 
67     int64_t primary_key_in_database() const { return primary_key_in_database_; }
68     const std::optional<base::UnguessableToken>& replaced_disk_cache_key_token()
69         const {
70       return replaced_disk_cache_key_token_;
71     }
72     const std::set<base::UnguessableToken>& evicted_disk_cache_key_tokens()
73         const {
74       return evicted_disk_cache_key_tokens_;
75     }
76     uint64_t total_dictionary_size() const { return total_dictionary_size_; }
77     uint64_t total_dictionary_count() const { return total_dictionary_count_; }
78 
79    private:
80     int64_t primary_key_in_database_;
81     std::optional<base::UnguessableToken> replaced_disk_cache_key_token_;
82     std::set<base::UnguessableToken> evicted_disk_cache_key_tokens_;
83     uint64_t total_dictionary_size_;
84     uint64_t total_dictionary_count_;
85   };
86 
87   using SizeOrError = base::expected<uint64_t, Error>;
88   using RegisterDictionaryResultOrError =
89       base::expected<RegisterDictionaryResult, Error>;
90   using DictionaryListOrError =
91       base::expected<std::vector<SharedDictionaryInfo>, Error>;
92   using DictionaryMapOrError = base::expected<
93       std::map<SharedDictionaryIsolationKey, std::vector<SharedDictionaryInfo>>,
94       Error>;
95   using UnguessableTokenSetOrError =
96       base::expected<std::set<base::UnguessableToken>, Error>;
97   using UsageInfoOrError =
98       base::expected<std::vector<SharedDictionaryUsageInfo>, Error>;
99   using OriginListOrError = base::expected<std::vector<url::Origin>, Error>;
100 
101   SQLitePersistentSharedDictionaryStore(
102       const base::FilePath& path,
103       const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
104       const scoped_refptr<base::SequencedTaskRunner>& background_task_runner);
105 
106   SQLitePersistentSharedDictionaryStore(
107       const SQLitePersistentSharedDictionaryStore&) = delete;
108   SQLitePersistentSharedDictionaryStore& operator=(
109       const SQLitePersistentSharedDictionaryStore&) = delete;
110 
111   ~SQLitePersistentSharedDictionaryStore();
112 
113   void GetTotalDictionarySize(base::OnceCallback<void(SizeOrError)> callback);
114   void RegisterDictionary(
115       const SharedDictionaryIsolationKey& isolation_key,
116       SharedDictionaryInfo dictionary_info,
117       const uint64_t max_size_per_site,
118       const uint64_t max_count_per_site,
119       base::OnceCallback<void(RegisterDictionaryResultOrError)> callback);
120   void GetDictionaries(
121       const SharedDictionaryIsolationKey& isolation_key,
122       base::OnceCallback<void(DictionaryListOrError)> callback);
123   void GetAllDictionaries(
124       base::OnceCallback<void(DictionaryMapOrError)> callback);
125   void GetUsageInfo(base::OnceCallback<void(UsageInfoOrError)> callback);
126   void GetOriginsBetween(const base::Time start_time,
127                          const base::Time end_time,
128                          base::OnceCallback<void(OriginListOrError)> callback);
129   void ClearAllDictionaries(
130       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
131   void ClearDictionaries(
132       const base::Time start_time,
133       const base::Time end_time,
134       base::RepeatingCallback<bool(const GURL&)> url_matcher,
135       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
136   void ClearDictionariesForIsolationKey(
137       const SharedDictionaryIsolationKey& isolation_key,
138       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
139   void DeleteExpiredDictionaries(
140       const base::Time now,
141       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
142   // Deletes dictionaries in order of `last_used_time` if the total size of all
143   // dictionaries exceeds `cache_max_size` or the total dictionary count exceeds
144   // `cache_max_count` until the total size reaches `size_low_watermark` and the
145   // total count reaches `count_low_watermark`. If `cache_max_size` is zero, the
146   // size limitation is ignored.
147   void ProcessEviction(
148       const uint64_t cache_max_size,
149       const uint64_t size_low_watermark,
150       const uint64_t cache_max_count,
151       const uint64_t count_low_watermark,
152       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
153   void GetAllDiskCacheKeyTokens(
154       base::OnceCallback<void(UnguessableTokenSetOrError)> callback);
155   void DeleteDictionariesByDiskCacheKeyTokens(
156       std::set<base::UnguessableToken> disk_cache_key_tokens,
157       base::OnceCallback<void(Error)> callback);
158   void UpdateDictionaryLastUsedTime(int64_t primary_key_in_database,
159                                     base::Time last_used_time);
160 
161   base::WeakPtr<SQLitePersistentSharedDictionaryStore> GetWeakPtr();
162 
163  private:
164   class Backend;
165 
166   const scoped_refptr<Backend> backend_;
167 
168   SEQUENCE_CHECKER(sequence_checker_);
169 
170   base::WeakPtrFactory<SQLitePersistentSharedDictionaryStore> weak_factory_
171       GUARDED_BY_CONTEXT(sequence_checker_){this};
172 };
173 
174 }  // namespace net
175 
176 #endif  // NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_REPORTING_AND_NEL_STORE_H_
177