Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/protected_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 dictionary_(to_copy.IsInitialized() | |
| 35 ? to_copy.GetContents()->DeepCopy() | |
| 36 : static_cast<base::DictionaryValue*>(NULL)) { | |
| 37 int version = 0; | |
| 38 if (to_copy.GetVersion(&version)) | |
| 39 version_.reset(new int(version)); | |
| 40 } | |
| 41 | |
| 42 // HashStoreContents implementation | |
| 43 virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; } | |
| 44 | |
| 45 virtual void Reset() OVERRIDE { | |
| 46 dictionary_.reset(); | |
| 47 super_mac_ = ""; | |
| 48 version_.reset(); | |
| 49 } | |
| 50 | |
| 51 virtual bool IsInitialized() const OVERRIDE { | |
| 52 return dictionary_; | |
| 53 } | |
| 54 | |
| 55 virtual const base::DictionaryValue* GetContents() const OVERRIDE{ | |
| 56 return dictionary_.get(); | |
| 57 } | |
| 58 | |
| 59 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | |
| 60 return scoped_ptr<MutableDictionary>( | |
| 61 new SimpleMutableDictionary(this)); | |
| 62 } | |
| 63 | |
| 64 virtual std::string GetSuperMac() const OVERRIDE { | |
|
robertshield
2014/03/25 03:05:12
nit: double space
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
| 65 return super_mac_; | |
| 66 } | |
| 67 | |
| 68 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | |
|
robertshield
2014/03/25 03:05:12
please update hash_store_contents.h with a descrip
erikwright (departed)
2014/03/25 20:28:26
Done here:
https://codereview.chromium.org/2055630
| |
| 69 super_mac_ = super_mac; | |
| 70 } | |
| 71 | |
| 72 virtual bool GetVersion(int* version) const OVERRIDE { | |
|
robertshield
2014/03/25 03:05:12
nit: double space
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
| 73 if (!version_) | |
| 74 return false; | |
| 75 *version = *version_; | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 virtual void SetVersion(int version) OVERRIDE { | |
| 80 version_.reset(new int(version)); | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 class SimpleMutableDictionary | |
| 85 : public HashStoreContents::MutableDictionary { | |
| 86 public: | |
| 87 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) | |
| 88 : outer_(outer) {} | |
| 89 | |
| 90 virtual ~SimpleMutableDictionary() {} | |
| 91 | |
| 92 // MutableDictionary implementation | |
| 93 virtual base::DictionaryValue* operator->() OVERRIDE{ | |
| 94 if (!outer_->dictionary_) | |
| 95 outer_->dictionary_.reset(new base::DictionaryValue); | |
| 96 return outer_->dictionary_.get(); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 DictionaryHashStoreContents* outer_; | |
| 101 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); | |
| 102 }; | |
| 103 | |
| 104 const std::string hash_store_id_; | |
| 105 std::string super_mac_; | |
| 106 scoped_ptr<int> version_; | |
|
robertshield
2014/03/25 03:05:12
I wish HashStoreContents had
const int kInvalidVe
erikwright (departed)
2014/03/25 20:28:26
The version number will go away in a little while.
Bernhard Bauer
2014/03/26 15:05:50
FWIW, I would prefer a negative value for "no vers
| |
| 107 scoped_ptr<base::DictionaryValue> dictionary_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); | |
| 110 }; | |
| 111 | |
| 22 // An in-memory PrefStore backed by an immutable DictionaryValue. | 112 // An in-memory PrefStore backed by an immutable DictionaryValue. |
| 23 class DictionaryPrefStore : public PrefStore { | 113 class DictionaryPrefStore : public PrefStore { |
| 24 public: | 114 public: |
| 25 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | 115 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
| 26 : dictionary_(dictionary) {} | 116 : dictionary_(dictionary) {} |
| 27 | 117 |
| 28 virtual bool GetValue(const std::string& key, | 118 virtual bool GetValue(const std::string& key, |
| 29 const base::Value** result) const OVERRIDE { | 119 const base::Value** result) const OVERRIDE { |
| 30 const base::Value* tmp = NULL; | 120 const base::Value* tmp = NULL; |
| 31 if (!dictionary_->Get(key, &tmp)) | 121 if (!dictionary_->Get(key, &tmp)) |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 } | 257 } |
| 168 | 258 |
| 169 void ProfilePrefStoreManager::ResetPrefHashStore() { | 259 void ProfilePrefStoreManager::ResetPrefHashStore() { |
| 170 if (kPlatformSupportsPreferenceTracking) | 260 if (kPlatformSupportsPreferenceTracking) |
| 171 GetPrefHashStoreImpl()->Reset(); | 261 GetPrefHashStoreImpl()->Reset(); |
| 172 } | 262 } |
| 173 | 263 |
| 174 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 264 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 175 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 265 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 176 scoped_ptr<PrefFilter> pref_filter; | 266 scoped_ptr<PrefFilter> pref_filter; |
| 177 if (kPlatformSupportsPreferenceTracking) { | 267 if (!kPlatformSupportsPreferenceTracking) { |
| 178 pref_filter.reset( | 268 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 179 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 269 io_task_runner, |
| 180 tracking_configuration_, | 270 scoped_ptr<PrefFilter>()); |
| 181 reporting_ids_count_)); | |
| 182 } | 271 } |
| 183 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 272 |
| 184 io_task_runner, | 273 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 185 pref_filter.Pass()); | 274 unprotected_configuration; |
| 275 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 276 protected_configuration; | |
| 277 std::set<std::string> protected_pref_names; | |
| 278 for (size_t i = 0; i < tracking_configuration_.size(); ++i) { | |
| 279 bool is_protected = tracking_configuration_[i].enforcement_level > | |
| 280 PrefHashFilter::NO_ENFORCEMENT; | |
| 281 (is_protected ? protected_configuration : unprotected_configuration) | |
| 282 .push_back(tracking_configuration_[i]); | |
| 283 if (is_protected) | |
| 284 protected_pref_names.insert(tracking_configuration_[i].name); | |
|
robertshield
2014/03/25 03:05:12
I find the following easier to read:
if (is_prote
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
| 285 } | |
| 286 | |
| 287 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
| 288 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 289 unprotected_configuration, | |
| 290 reporting_ids_count_)); | |
| 291 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( | |
| 292 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 293 protected_configuration, | |
| 294 reporting_ids_count_)); | |
| 295 | |
| 296 scoped_refptr<PersistentPrefStore> unprotected_pref_store( | |
| 297 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 298 io_task_runner, | |
| 299 unprotected_pref_hash_filter.PassAs<PrefFilter>())); | |
| 300 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( | |
| 301 profile_path_.Append(chrome::kProtectedPreferencesFilename), | |
| 302 io_task_runner, | |
| 303 protected_pref_hash_filter.PassAs<PrefFilter>())); | |
| 304 | |
| 305 return new ProtectedPrefStore( | |
| 306 unprotected_pref_store, | |
| 307 protected_pref_store, | |
| 308 protected_pref_names, | |
| 309 base::Bind(&PrefHashFilter::MigrateValues, | |
| 310 base::Owned(new PrefHashFilter( | |
| 311 CopyPrefHashStore(), | |
| 312 protected_configuration, | |
| 313 reporting_ids_count_)), | |
| 314 unprotected_pref_store, | |
| 315 protected_pref_store)); | |
| 186 } | 316 } |
| 187 | 317 |
| 188 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 318 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
| 189 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 319 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 190 if (!kPlatformSupportsPreferenceTracking) | 320 if (!kPlatformSupportsPreferenceTracking) |
| 191 return; | 321 return; |
| 192 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 322 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
| 193 const PrefHashStoreImpl::StoreVersion current_version = | 323 const PrefHashStoreImpl::StoreVersion current_version = |
| 194 pref_hash_store_impl->GetCurrentVersion(); | 324 pref_hash_store_impl->GetCurrentVersion(); |
| 195 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 325 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 211 } | 341 } |
| 212 } | 342 } |
| 213 | 343 |
| 214 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 344 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 215 const base::DictionaryValue& master_prefs) { | 345 const base::DictionaryValue& master_prefs) { |
| 216 // Create the profile directory if it doesn't exist yet (very possible on | 346 // Create the profile directory if it doesn't exist yet (very possible on |
| 217 // first run). | 347 // first run). |
| 218 if (!base::CreateDirectory(profile_path_)) | 348 if (!base::CreateDirectory(profile_path_)) |
| 219 return false; | 349 return false; |
| 220 | 350 |
| 351 // This will actually write out to a single combined file but it will be | |
| 352 // immediately migrated to two files on load. | |
|
robertshield
2014/03/25 03:05:12
Suggestion: "This will write out to a single combi
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
| 221 JSONFileValueSerializer serializer( | 353 JSONFileValueSerializer serializer( |
| 222 GetPrefFilePathFromProfilePath(profile_path_)); | 354 GetPrefFilePathFromProfilePath(profile_path_)); |
| 223 | 355 |
| 224 // Call Serialize (which does IO) on the main thread, which would _normally_ | 356 // Call Serialize (which does IO) on the main thread, which would _normally_ |
| 225 // be verboten. In this case however, we require this IO to synchronously | 357 // be verboten. In this case however, we require this IO to synchronously |
| 226 // complete before Chrome can start (as master preferences seed the Local | 358 // complete before Chrome can start (as master preferences seed the Local |
| 227 // State and Preferences files). This won't trip ThreadIORestrictions as they | 359 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 228 // won't have kicked in yet on the main thread. | 360 // won't have kicked in yet on the main thread. |
| 229 bool success = serializer.Serialize(master_prefs); | 361 bool success = serializer.Serialize(master_prefs); |
| 230 | 362 |
| 231 if (success && kPlatformSupportsPreferenceTracking) { | 363 if (success && kPlatformSupportsPreferenceTracking) { |
| 232 scoped_refptr<const PrefStore> pref_store( | 364 scoped_refptr<const PrefStore> pref_store( |
| 233 new DictionaryPrefStore(&master_prefs)); | 365 new DictionaryPrefStore(&master_prefs)); |
| 234 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 366 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 235 tracking_configuration_, | 367 tracking_configuration_, |
| 236 reporting_ids_count_).Initialize(*pref_store); | 368 reporting_ids_count_).Initialize(*pref_store); |
| 237 } | 369 } |
| 238 | 370 |
| 239 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 371 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 240 return success; | 372 return success; |
| 241 } | 373 } |
| 242 | 374 |
| 375 PersistentPrefStore* | |
| 376 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( | |
| 377 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | |
| 378 if (!kPlatformSupportsPreferenceTracking) { | |
| 379 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 380 io_task_runner, | |
| 381 scoped_ptr<PrefFilter>()); | |
| 382 } | |
| 383 | |
| 384 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 385 io_task_runner, | |
| 386 scoped_ptr<PrefFilter>(new PrefHashFilter( | |
| 387 GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 388 tracking_configuration_, | |
| 389 reporting_ids_count_))); | |
|
robertshield
2014/03/25 03:05:12
Can the above be replaced by:
scoped_ptr<PrefFilt
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
| 390 } | |
| 391 | |
| 243 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 392 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
| 244 DCHECK(kPlatformSupportsPreferenceTracking); | 393 DCHECK(kPlatformSupportsPreferenceTracking); |
| 245 | 394 |
| 246 return make_scoped_ptr(new PrefHashStoreImpl( | 395 return make_scoped_ptr(new PrefHashStoreImpl( |
| 247 seed_, | 396 seed_, |
| 248 device_id_, | 397 device_id_, |
| 249 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 398 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
| 250 profile_path_.AsUTF8Unsafe(), local_state_)))); | 399 profile_path_.AsUTF8Unsafe(), local_state_)))); |
| 251 } | 400 } |
| 401 | |
| 402 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { | |
| 403 DCHECK(kPlatformSupportsPreferenceTracking); | |
| 404 | |
| 405 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), | |
| 406 local_state_); | |
| 407 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( | |
| 408 seed_, | |
| 409 device_id_, | |
| 410 scoped_ptr<HashStoreContents>( | |
| 411 new DictionaryHashStoreContents(real_contents)))); | |
| 412 } | |
| OLD | NEW |