Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: services/preferences/public/cpp/user_prefs_impl.cc

Issue 2601873002: Add a mojo bridge for PersistentPrefStore. (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
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 "services/preferences/public/cpp/user_prefs_impl.h"
6
7 #include <memory>
8 #include <set>
9 #include <utility>
10
11 #include "base/macros.h"
12 #include "base/values.h"
13 #include "components/prefs/json_pref_store.h"
14 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
15 #include "components/user_prefs/tracked/segregated_pref_store.h"
16 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
17 #include "mojo/public/cpp/bindings/strong_binding.h"
18
19 #if defined(OS_WIN)
20 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h"
21 #endif
22
23 namespace prefs {
24 namespace {
25
26 class ValidationDelegatePtrHolder
tibell 2017/03/08 03:39:54 Document what each class does. I'm guess this is:
27 : public base::RefCounted<ValidationDelegatePtrHolder> {
28 public:
29 ValidationDelegatePtrHolder(mojom::TrackedPreferenceValidationDelegatePtr ptr)
30 : ptr_(std::move(ptr)) {}
31
32 mojom::TrackedPreferenceValidationDelegate* get() { return ptr_.get(); }
33
34 private:
35 friend class base::RefCounted<ValidationDelegatePtrHolder>;
36 ~ValidationDelegatePtrHolder() = default;
37
38 mojom::TrackedPreferenceValidationDelegatePtr ptr_;
39 };
40
41 class PersistentPrefStoreImpl : public mojom::PersistentPrefStore {
42 public:
43 explicit PersistentPrefStoreImpl(
44 scoped_refptr<::PersistentPrefStore> pref_store);
45
46 ~PersistentPrefStoreImpl() override;
47
48 // mojom::PersistentPrefStore:
49 void SetValue(const std::string& key,
50 std::unique_ptr<base::Value> value,
51 uint32_t flags) override;
52
53 void CommitPendingWrite() override;
54 void SchedulePendingLossyWrites() override;
55 void ClearMutableValues() override;
56
57 private:
58 scoped_refptr<ValidationDelegatePtrHolder> validation_delegate_ref_;
59 scoped_refptr<::PersistentPrefStore> backing_pref_store_;
60
61 DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreImpl);
62 };
63
64 PersistentPrefStoreImpl::PersistentPrefStoreImpl(
65 scoped_refptr<::PersistentPrefStore> pref_store)
66 : backing_pref_store_(std::move(pref_store)) {}
67
68 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() = default;
69
70 // mojomJsonPrefStore:
71 void PersistentPrefStoreImpl::SetValue(const std::string& key,
72 std::unique_ptr<base::Value> value,
73 uint32_t flags) {
74 if (value)
75 backing_pref_store_->SetValue(key, std::move(value), flags);
76 else
77 backing_pref_store_->RemoveValue(key, flags);
78 }
79
80 void PersistentPrefStoreImpl::CommitPendingWrite() {
81 backing_pref_store_->CommitPendingWrite();
82 }
83
84 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() {
85 backing_pref_store_->SchedulePendingLossyWrites();
86 }
87
88 void PersistentPrefStoreImpl::ClearMutableValues() {
89 backing_pref_store_->ClearMutableValues();
90 }
91
92 void CallConnectCallback(
93 PersistentPrefStore* pref_store,
tibell 2017/03/08 03:39:54 Should be a scoped_refptr I think.
94 const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) {
95 mojom::PersistentPrefStorePtr pref_store_ptr;
96 mojo::MakeStrongBinding(
97 base::MakeUnique<PersistentPrefStoreImpl>(std::move(pref_store)),
98 mojo::MakeRequest(&pref_store_ptr));
99 callback.Run(pref_store->GetReadError(), pref_store->ReadOnly(),
100 pref_store->IsInitializationComplete() ? pref_store->GetValues()
101 : nullptr,
102 std::move(pref_store_ptr));
103 }
104
105 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store,
106 const std::string& key) {
107 if (pref_store) {
108 pref_store->RemoveValueSilently(
109 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
110 }
111 }
112
113 std::unique_ptr<PrefHashStore> CreatePrefHashStore(
114 const std::string& seed,
115 const std::string& legacy_device_id,
116 bool use_super_mac) {
117 return std::unique_ptr<PrefHashStore>(
tibell 2017/03/08 03:39:54 Would MakeUnique work since the function has a ret
118 new PrefHashStoreImpl(seed, legacy_device_id, use_super_mac));
119 }
120
121 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>>
122 GetExternalVerificationPrefHashStorePair(const std::string& seed,
123 const std::string& legacy_device_id,
124 const base::string16& registry_path,
125 const base::FilePath& prefs_path) {
126 #if defined(OS_WIN)
127 return std::make_pair(
128 base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id,
129 false /* use_super_mac */),
130 base::MakeUnique<RegistryHashStoreContentsWin>(
131 registry_path, prefs_path.DirName().BaseName().LossyDisplayName()));
132 #else
133 return std::make_pair(nullptr, nullptr);
134 #endif
135 }
136
137 class PersistentPrefStoreConnectorImpl
138 : public mojom::PersistentPrefStoreConnector,
139 public PrefStore::Observer {
140 public:
141 PersistentPrefStoreConnectorImpl(
142 scoped_refptr<PersistentPrefStore> backing_pref_store,
143 scoped_refptr<ValidationDelegatePtrHolder> validation_delegate)
144 : backing_pref_store_(backing_pref_store),
145 validation_delegate_holder_(std::move(validation_delegate)) {}
146
147 ~PersistentPrefStoreConnectorImpl() override = default;
148
149 // mojom::PersistentPrefStoreConnector override:
150 void Connect(const ConnectCallback& callback) override {
151 if (backing_pref_store_->IsInitializationComplete()) {
152 CallConnectCallback(backing_pref_store_.get(), callback);
153 return;
154 }
155 connect_callbacks_.push_back(callback);
156 if (loading_)
157 return;
158
159 backing_pref_store_->AddObserver(this);
160 loading_ = true;
161 backing_pref_store_->ReadPrefsAsync(nullptr);
162 }
163
164 static void CreateUserPrefs(
165 const base::FilePath& pref_filename,
166 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
167 mojom::PersistentPrefStoreConnectorRequest request) {
168 mojo::MakeStrongBinding(
169 base::MakeUnique<PersistentPrefStoreConnectorImpl>(
170 new JsonPrefStore(pref_filename, io_task_runner, nullptr), nullptr),
171 std::move(request));
172 }
173
174 static void CreateSegregatedUserPrefs(
175 const base::FilePath& unprotected_pref_filename,
176 const base::FilePath& protected_pref_filename,
177 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
178 tracking_configuration,
179 size_t reporting_ids_count,
180 const std::string& seed,
181 const std::string& legacy_device_id,
182 const base::string16& registry_path,
183 mojom::TrackedPreferenceValidationDelegatePtrInfo
184 validation_delegate_info,
185 const base::Closure& on_reset_on_load,
186 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
187 mojom::PersistentPrefStoreConnectorRequest request) {
188 scoped_refptr<ValidationDelegatePtrHolder> validation_delegate_holder(
tibell 2017/03/08 03:39:54 There's lots of setup going on here. Is this code
189 new ValidationDelegatePtrHolder(
190 mojo::MakeProxy(std::move(validation_delegate_info))));
191 std::vector<PrefHashFilter::TrackedPreferenceMetadata>
192 unprotected_configuration;
193 std::vector<PrefHashFilter::TrackedPreferenceMetadata>
194 protected_configuration;
195 std::set<std::string> protected_pref_names;
196 std::set<std::string> unprotected_pref_names;
197 for (const auto& metadata : tracking_configuration) {
198 if (metadata.enforcement_level > PrefHashFilter::NO_ENFORCEMENT) {
199 protected_configuration.push_back(metadata);
200 protected_pref_names.insert(metadata.name);
201 } else {
202 unprotected_configuration.push_back(metadata);
203 unprotected_pref_names.insert(metadata.name);
204 }
205 }
206
207 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter(
208 new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, false),
209 GetExternalVerificationPrefHashStorePair(
210 seed, legacy_device_id, registry_path,
211 unprotected_pref_filename),
212 unprotected_configuration, base::Closure(),
213 validation_delegate_holder->get(),
214 reporting_ids_count, false));
215 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(
216 new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, true),
217 GetExternalVerificationPrefHashStorePair(
218 seed, legacy_device_id, registry_path,
219 unprotected_pref_filename),
220 protected_configuration, on_reset_on_load,
221 validation_delegate_holder->get(),
222 reporting_ids_count, true));
223
224 PrefHashFilter* raw_unprotected_pref_hash_filter =
225 unprotected_pref_hash_filter.get();
226 PrefHashFilter* raw_protected_pref_hash_filter =
227 protected_pref_hash_filter.get();
228
229 scoped_refptr<JsonPrefStore> unprotected_pref_store(
230 new JsonPrefStore(unprotected_pref_filename, io_task_runner.get(),
231 std::move(unprotected_pref_hash_filter)));
232 scoped_refptr<JsonPrefStore> protected_pref_store(
233 new JsonPrefStore(protected_pref_filename, io_task_runner.get(),
234 std::move(protected_pref_hash_filter)));
235
236 SetupTrackedPreferencesMigration(
237 unprotected_pref_names, protected_pref_names,
238 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()),
239 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()),
240 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
241 unprotected_pref_store->AsWeakPtr()),
242 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
243 protected_pref_store->AsWeakPtr()),
244 CreatePrefHashStore(seed, legacy_device_id, false),
245 CreatePrefHashStore(seed, legacy_device_id, true),
246 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter);
247
248 mojo::MakeStrongBinding(
249 base::MakeUnique<PersistentPrefStoreConnectorImpl>(
250 new SegregatedPrefStore(unprotected_pref_store,
251 protected_pref_store, protected_pref_names),
252 std::move(validation_delegate_holder)),
253 std::move(request));
254 }
255
256 private:
257 void OnPrefValueChanged(const std::string& key) override {}
tibell 2017/03/08 03:39:54 Add a comment why it's OK to ignore this.
258
259 void OnInitializationCompleted(bool succeeded) override {
260 loading_ = false;
261 backing_pref_store_->RemoveObserver(this);
262 for (const auto& callback : connect_callbacks_) {
263 CallConnectCallback(backing_pref_store_.get(), callback);
264 }
265 connect_callbacks_.clear();
266 }
267
268 scoped_refptr<PersistentPrefStore> backing_pref_store_;
269 scoped_refptr<ValidationDelegatePtrHolder> validation_delegate_holder_;
270
271 bool loading_ = false;
272 std::vector<ConnectCallback> connect_callbacks_;
273
274 DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreConnectorImpl);
275 };
276
277 } // namespace
278
279 void CreateUserPrefs(
280 const base::FilePath& pref_filename,
281 const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner,
282 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
283 mojom::PersistentPrefStoreConnectorRequest request) {
284 connection_task_runner->PostTask(
285 FROM_HERE,
286 base::Bind(&PersistentPrefStoreConnectorImpl::CreateUserPrefs,
287 pref_filename, io_task_runner, base::Passed(&request)));
288 }
289
290 void CreateSegregatedUserPrefs(
291 const base::FilePath& unprotected_pref_filename,
292 const base::FilePath& protected_pref_filename,
293 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
294 tracking_configuration,
295 size_t reporting_ids_count,
296 const std::string& seed,
297 const std::string& legacy_device_id,
298 const base::string16& registry_path,
299 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate,
300 const base::Closure& on_reset_on_load,
301 const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner,
302 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
303 mojom::PersistentPrefStoreConnectorRequest request) {
304 connection_task_runner->PostTask(
305 FROM_HERE,
306 base::Bind(&PersistentPrefStoreConnectorImpl::CreateSegregatedUserPrefs,
307 unprotected_pref_filename, protected_pref_filename,
308 tracking_configuration, reporting_ids_count, seed,
309 legacy_device_id, registry_path,
310 base::Passed(validation_delegate.PassInterface()),
311 on_reset_on_load, io_task_runner, base::Passed(&request)));
312 }
313
314 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698