OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/prefs/profile_pref_store_manager.h" | 5 #include "chrome/browser/prefs/profile_pref_store_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/prefs/json_pref_store.h" | 11 #include "base/prefs/json_pref_store.h" |
12 #include "base/prefs/persistent_pref_store.h" | 12 #include "base/prefs/persistent_pref_store.h" |
13 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
14 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 14 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
15 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" | 15 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" |
16 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" | |
16 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "components/user_prefs/pref_registry_syncable.h" | 19 #include "components/user_prefs/pref_registry_syncable.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
23 // An adaptor that allows a PrefHashStoreImpl to access a preference store | |
24 // directly as a dictionary. Uses an equivalent layout to | |
25 // PrefStoreHashStoreContents. | |
26 class DictionaryHashStoreContents : public HashStoreContents { | |
27 public: | |
28 // Instantiates a HashStoreContents that is a copy of |to_copy|. The copy is | |
29 // mutable but does not affect the original, nor is it persisted to disk in | |
30 // any other way. | |
31 explicit DictionaryHashStoreContents(const HashStoreContents& to_copy) | |
32 : hash_store_id_(to_copy.hash_store_id()), | |
33 super_mac_(to_copy.GetSuperMac()) { | |
34 if (to_copy.IsInitialized()) | |
35 dictionary_.reset(to_copy.GetContents()->DeepCopy()); | |
36 int version = 0; | |
37 if (to_copy.GetVersion(&version)) | |
38 version_.reset(new int(version)); | |
39 } | |
40 | |
41 // HashStoreContents implementation | |
42 virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; } | |
43 | |
44 virtual void Reset() OVERRIDE { | |
45 dictionary_.reset(); | |
46 super_mac_.clear(); | |
47 version_.reset(); | |
48 } | |
49 | |
50 virtual bool IsInitialized() const OVERRIDE { | |
51 return dictionary_; | |
52 } | |
53 | |
54 virtual const base::DictionaryValue* GetContents() const OVERRIDE{ | |
55 return dictionary_.get(); | |
56 } | |
57 | |
58 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | |
59 return scoped_ptr<MutableDictionary>( | |
60 new SimpleMutableDictionary(this)); | |
61 } | |
62 | |
63 virtual std::string GetSuperMac() const OVERRIDE { return super_mac_; } | |
64 | |
65 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | |
66 super_mac_ = super_mac; | |
67 } | |
68 | |
69 virtual bool GetVersion(int* version) const OVERRIDE { | |
70 if (!version_) | |
71 return false; | |
72 *version = *version_; | |
73 return true; | |
74 } | |
75 | |
76 virtual void SetVersion(int version) OVERRIDE { | |
77 version_.reset(new int(version)); | |
78 } | |
79 | |
80 private: | |
81 class SimpleMutableDictionary | |
82 : public HashStoreContents::MutableDictionary { | |
83 public: | |
84 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) | |
85 : outer_(outer) {} | |
86 | |
87 virtual ~SimpleMutableDictionary() {} | |
88 | |
89 // MutableDictionary implementation | |
90 virtual base::DictionaryValue* operator->() OVERRIDE { | |
91 if (!outer_->dictionary_) | |
92 outer_->dictionary_.reset(new base::DictionaryValue); | |
93 return outer_->dictionary_.get(); | |
94 } | |
95 | |
96 private: | |
97 DictionaryHashStoreContents* outer_; | |
98 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); | |
gab
2014/04/01 20:19:52
nit: +empty line above
erikwright (departed)
2014/04/02 15:03:17
Done.
| |
99 }; | |
100 | |
101 const std::string hash_store_id_; | |
102 std::string super_mac_; | |
103 scoped_ptr<int> version_; | |
104 scoped_ptr<base::DictionaryValue> dictionary_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); | |
107 }; | |
108 | |
22 // An in-memory PrefStore backed by an immutable DictionaryValue. | 109 // An in-memory PrefStore backed by an immutable DictionaryValue. |
23 class DictionaryPrefStore : public PrefStore { | 110 class DictionaryPrefStore : public PrefStore { |
24 public: | 111 public: |
25 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | 112 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
26 : dictionary_(dictionary) {} | 113 : dictionary_(dictionary) {} |
27 | 114 |
28 virtual bool GetValue(const std::string& key, | 115 virtual bool GetValue(const std::string& key, |
29 const base::Value** result) const OVERRIDE { | 116 const base::Value** result) const OVERRIDE { |
30 const base::Value* tmp = NULL; | 117 const base::Value* tmp = NULL; |
31 if (!dictionary_->Get(key, &tmp)) | 118 if (!dictionary_->Get(key, &tmp)) |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 } | 267 } |
181 | 268 |
182 void ProfilePrefStoreManager::ResetPrefHashStore() { | 269 void ProfilePrefStoreManager::ResetPrefHashStore() { |
183 if (kPlatformSupportsPreferenceTracking) | 270 if (kPlatformSupportsPreferenceTracking) |
184 GetPrefHashStoreImpl()->Reset(); | 271 GetPrefHashStoreImpl()->Reset(); |
185 } | 272 } |
186 | 273 |
187 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 274 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
188 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 275 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
189 scoped_ptr<PrefFilter> pref_filter; | 276 scoped_ptr<PrefFilter> pref_filter; |
190 if (kPlatformSupportsPreferenceTracking) { | 277 if (!kPlatformSupportsPreferenceTracking) { |
191 pref_filter.reset( | 278 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
192 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 279 io_task_runner, |
193 tracking_configuration_, | 280 scoped_ptr<PrefFilter>()); |
194 reporting_ids_count_)); | |
195 } | 281 } |
196 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 282 |
197 io_task_runner, | 283 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
198 pref_filter.Pass()); | 284 unprotected_configuration; |
285 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
286 protected_configuration; | |
287 std::set<std::string> protected_pref_names; | |
288 for (size_t i = 0; i < tracking_configuration_.size(); ++i) { | |
gab
2014/04/01 20:19:52
Use a std::vector<std::string>::const_iterator ins
erikwright (departed)
2014/04/02 15:03:17
Done.
| |
289 if (tracking_configuration_[i].enforcement_level > | |
290 PrefHashFilter::NO_ENFORCEMENT) { | |
291 protected_configuration.push_back(tracking_configuration_[i]); | |
292 protected_pref_names.insert(tracking_configuration_[i].name); | |
293 } else { | |
294 unprotected_configuration.push_back(tracking_configuration_[i]); | |
295 } | |
296 } | |
297 | |
298 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
gab
2014/04/01 20:19:52
Store these directly in scoped_ptr<PrefFilter>'s s
erikwright (departed)
2014/04/02 15:03:17
Done.
| |
299 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
300 unprotected_configuration, | |
301 reporting_ids_count_)); | |
302 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( | |
303 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
gab
2014/04/01 20:19:52
Why not start putting hashes in the Protected Pref
erikwright (departed)
2014/04/02 15:03:17
You have the burden of migrating hashes from one s
| |
304 protected_configuration, | |
305 reporting_ids_count_)); | |
306 | |
307 scoped_refptr<PersistentPrefStore> unprotected_pref_store( | |
308 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
309 io_task_runner, | |
310 unprotected_pref_hash_filter.PassAs<PrefFilter>())); | |
311 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( | |
312 profile_path_.Append(chrome::kProtectedPreferencesFilename), | |
313 io_task_runner, | |
314 protected_pref_hash_filter.PassAs<PrefFilter>())); | |
gab
2014/04/01 20:19:52
So migration is icky the way I see it, since both
erikwright (departed)
2014/04/02 15:03:17
Yes, but only for tracking-only values, and this i
gab
2014/04/02 18:24:38
Here is another idea, taking advantage of two thin
| |
315 | |
316 return new SegregatedPrefStore( | |
317 unprotected_pref_store, | |
318 protected_pref_store, | |
319 protected_pref_names, | |
320 base::Bind(&PrefHashFilter::MigrateValues, | |
321 base::Owned(new PrefHashFilter( | |
322 CopyPrefHashStore(), | |
323 protected_configuration, | |
324 reporting_ids_count_)), | |
gab
2014/04/01 20:19:52
Meta-comment either here or on MigrateValues() abo
erikwright (departed)
2014/04/02 15:03:17
Done.
| |
325 unprotected_pref_store, | |
326 protected_pref_store)); | |
199 } | 327 } |
200 | 328 |
201 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 329 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
202 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 330 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
203 if (!kPlatformSupportsPreferenceTracking) | 331 if (!kPlatformSupportsPreferenceTracking) |
204 return; | 332 return; |
205 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 333 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
206 const PrefHashStoreImpl::StoreVersion current_version = | 334 const PrefHashStoreImpl::StoreVersion current_version = |
207 pref_hash_store_impl->GetCurrentVersion(); | 335 pref_hash_store_impl->GetCurrentVersion(); |
208 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 336 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
(...skipping 15 matching lines...) Expand all Loading... | |
224 } | 352 } |
225 } | 353 } |
226 | 354 |
227 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 355 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
228 const base::DictionaryValue& master_prefs) { | 356 const base::DictionaryValue& master_prefs) { |
229 // Create the profile directory if it doesn't exist yet (very possible on | 357 // Create the profile directory if it doesn't exist yet (very possible on |
230 // first run). | 358 // first run). |
231 if (!base::CreateDirectory(profile_path_)) | 359 if (!base::CreateDirectory(profile_path_)) |
232 return false; | 360 return false; |
233 | 361 |
362 // This will write out to a single combined file which will be immediately | |
363 // migrated to two files on load. | |
234 JSONFileValueSerializer serializer( | 364 JSONFileValueSerializer serializer( |
235 GetPrefFilePathFromProfilePath(profile_path_)); | 365 GetPrefFilePathFromProfilePath(profile_path_)); |
236 | 366 |
237 // Call Serialize (which does IO) on the main thread, which would _normally_ | 367 // Call Serialize (which does IO) on the main thread, which would _normally_ |
238 // be verboten. In this case however, we require this IO to synchronously | 368 // be verboten. In this case however, we require this IO to synchronously |
239 // complete before Chrome can start (as master preferences seed the Local | 369 // complete before Chrome can start (as master preferences seed the Local |
240 // State and Preferences files). This won't trip ThreadIORestrictions as they | 370 // State and Preferences files). This won't trip ThreadIORestrictions as they |
241 // won't have kicked in yet on the main thread. | 371 // won't have kicked in yet on the main thread. |
242 bool success = serializer.Serialize(master_prefs); | 372 bool success = serializer.Serialize(master_prefs); |
243 | 373 |
244 if (success && kPlatformSupportsPreferenceTracking) { | 374 if (success && kPlatformSupportsPreferenceTracking) { |
245 scoped_refptr<const PrefStore> pref_store( | 375 scoped_refptr<const PrefStore> pref_store( |
246 new DictionaryPrefStore(&master_prefs)); | 376 new DictionaryPrefStore(&master_prefs)); |
247 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 377 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
248 tracking_configuration_, | 378 tracking_configuration_, |
249 reporting_ids_count_).Initialize(*pref_store); | 379 reporting_ids_count_).Initialize(*pref_store); |
250 } | 380 } |
251 | 381 |
252 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 382 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
253 return success; | 383 return success; |
254 } | 384 } |
255 | 385 |
386 PersistentPrefStore* | |
387 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( | |
388 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | |
389 scoped_ptr<PrefFilter> pref_filter; | |
390 if (kPlatformSupportsPreferenceTracking) { | |
391 pref_filter.reset( | |
392 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
393 tracking_configuration_, | |
394 reporting_ids_count_)); | |
395 } | |
396 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
397 io_task_runner, | |
398 pref_filter.Pass()); | |
399 } | |
400 | |
256 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 401 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
257 DCHECK(kPlatformSupportsPreferenceTracking); | 402 DCHECK(kPlatformSupportsPreferenceTracking); |
258 | 403 |
259 return make_scoped_ptr(new PrefHashStoreImpl( | 404 return make_scoped_ptr(new PrefHashStoreImpl( |
260 seed_, | 405 seed_, |
261 device_id_, | 406 device_id_, |
262 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 407 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
263 profile_path_.AsUTF8Unsafe(), local_state_)))); | 408 profile_path_.AsUTF8Unsafe(), local_state_)))); |
264 } | 409 } |
410 | |
411 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { | |
412 DCHECK(kPlatformSupportsPreferenceTracking); | |
413 | |
414 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), | |
415 local_state_); | |
416 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( | |
417 seed_, | |
418 device_id_, | |
419 scoped_ptr<HashStoreContents>( | |
420 new DictionaryHashStoreContents(real_contents)))); | |
421 } | |
OLD | NEW |