Chromium Code Reviews| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/prefs/json_pref_store.h" | 12 #include "base/prefs/json_pref_store.h" |
| 13 #include "base/prefs/persistent_pref_store.h" | 13 #include "base/prefs/persistent_pref_store.h" |
| 14 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
| 15 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 15 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
| 16 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" | 16 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" |
| 17 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" | 17 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" |
| 18 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" | 18 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" |
| 19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // An adaptor that allows a PrefHashStoreImpl to access a preference store | 25 // An adaptor that allows a PrefHashStoreImpl to access a preference store |
| 26 // directly as a dictionary. Uses an equivalent layout to | 26 // directly as a dictionary. Uses an equivalent layout to |
| 27 // PrefStoreHashStoreContents. | 27 // PrefStoreHashStoreContents. |
| 28 class DictionaryHashStoreContents : public HashStoreContents { | 28 class DictionaryHashStoreContents : public HashStoreContents { |
|
gab
2014/06/12 22:09:00
This can be removed altogether as well; not sure w
| |
| 29 public: | 29 public: |
| 30 // Instantiates a HashStoreContents that is a copy of |to_copy|. The copy is | 30 // Instantiates a HashStoreContents that is a copy of |to_copy|. The copy is |
| 31 // mutable but does not affect the original, nor is it persisted to disk in | 31 // mutable but does not affect the original, nor is it persisted to disk in |
| 32 // any other way. | 32 // any other way. |
| 33 explicit DictionaryHashStoreContents(const HashStoreContents& to_copy) | 33 explicit DictionaryHashStoreContents(const HashStoreContents& to_copy) |
| 34 : hash_store_id_(to_copy.hash_store_id()), | 34 : hash_store_id_(to_copy.hash_store_id()), |
| 35 super_mac_(to_copy.GetSuperMac()) { | 35 super_mac_(to_copy.GetSuperMac()) { |
| 36 if (to_copy.IsInitialized()) | 36 if (to_copy.IsInitialized()) |
| 37 dictionary_.reset(to_copy.GetContents()->DeepCopy()); | 37 dictionary_.reset(to_copy.GetContents()->DeepCopy()); |
| 38 int version = 0; | |
| 39 if (to_copy.GetVersion(&version)) | |
| 40 version_.reset(new int(version)); | |
| 41 } | 38 } |
| 42 | 39 |
| 43 // HashStoreContents implementation | 40 // HashStoreContents implementation |
| 44 virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; } | 41 virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; } |
| 45 | 42 |
| 46 virtual void Reset() OVERRIDE { | 43 virtual void Reset() OVERRIDE { |
| 47 dictionary_.reset(); | 44 dictionary_.reset(); |
| 48 super_mac_.clear(); | 45 super_mac_.clear(); |
| 49 version_.reset(); | |
| 50 } | 46 } |
| 51 | 47 |
| 52 virtual bool IsInitialized() const OVERRIDE { | 48 virtual bool IsInitialized() const OVERRIDE { |
| 53 return dictionary_; | 49 return dictionary_; |
| 54 } | 50 } |
| 55 | 51 |
| 56 virtual const base::DictionaryValue* GetContents() const OVERRIDE{ | 52 virtual const base::DictionaryValue* GetContents() const OVERRIDE{ |
| 57 return dictionary_.get(); | 53 return dictionary_.get(); |
| 58 } | 54 } |
| 59 | 55 |
| 60 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | 56 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { |
| 61 return scoped_ptr<MutableDictionary>( | 57 return scoped_ptr<MutableDictionary>( |
| 62 new SimpleMutableDictionary(this)); | 58 new SimpleMutableDictionary(this)); |
| 63 } | 59 } |
| 64 | 60 |
| 65 virtual std::string GetSuperMac() const OVERRIDE { return super_mac_; } | 61 virtual std::string GetSuperMac() const OVERRIDE { return super_mac_; } |
| 66 | 62 |
| 67 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | 63 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { |
| 68 super_mac_ = super_mac; | 64 super_mac_ = super_mac; |
| 69 } | 65 } |
| 70 | 66 |
| 71 virtual bool GetVersion(int* version) const OVERRIDE { | |
| 72 if (!version_) | |
| 73 return false; | |
| 74 *version = *version_; | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 virtual void SetVersion(int version) OVERRIDE { | |
| 79 version_.reset(new int(version)); | |
| 80 } | |
| 81 | |
| 82 virtual void CommitPendingWrite() OVERRIDE {} | 67 virtual void CommitPendingWrite() OVERRIDE {} |
| 83 | 68 |
| 84 private: | 69 private: |
| 85 class SimpleMutableDictionary | 70 class SimpleMutableDictionary |
| 86 : public HashStoreContents::MutableDictionary { | 71 : public HashStoreContents::MutableDictionary { |
| 87 public: | 72 public: |
| 88 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) | 73 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) |
| 89 : outer_(outer) {} | 74 : outer_(outer) {} |
| 90 | 75 |
| 91 virtual ~SimpleMutableDictionary() {} | 76 virtual ~SimpleMutableDictionary() {} |
| 92 | 77 |
| 93 // MutableDictionary implementation | 78 // MutableDictionary implementation |
| 94 virtual base::DictionaryValue* operator->() OVERRIDE { | 79 virtual base::DictionaryValue* operator->() OVERRIDE { |
| 95 if (!outer_->dictionary_) | 80 if (!outer_->dictionary_) |
| 96 outer_->dictionary_.reset(new base::DictionaryValue); | 81 outer_->dictionary_.reset(new base::DictionaryValue); |
| 97 return outer_->dictionary_.get(); | 82 return outer_->dictionary_.get(); |
| 98 } | 83 } |
| 99 | 84 |
| 100 private: | 85 private: |
| 101 DictionaryHashStoreContents* outer_; | 86 DictionaryHashStoreContents* outer_; |
| 102 | 87 |
| 103 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); | 88 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); |
| 104 }; | 89 }; |
| 105 | 90 |
| 106 const std::string hash_store_id_; | 91 const std::string hash_store_id_; |
| 107 std::string super_mac_; | 92 std::string super_mac_; |
| 108 scoped_ptr<int> version_; | |
| 109 scoped_ptr<base::DictionaryValue> dictionary_; | 93 scoped_ptr<base::DictionaryValue> dictionary_; |
| 110 | 94 |
| 111 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); | 95 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); |
| 112 }; | 96 }; |
| 113 | 97 |
| 114 // An in-memory PrefStore backed by an immutable DictionaryValue. | 98 // An in-memory PrefStore backed by an immutable DictionaryValue. |
| 115 class DictionaryPrefStore : public PrefStore { | 99 class DictionaryPrefStore : public PrefStore { |
| 116 public: | 100 public: |
| 117 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | 101 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
| 118 : dictionary_(dictionary) {} | 102 : dictionary_(dictionary) {} |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 129 } | 113 } |
| 130 | 114 |
| 131 private: | 115 private: |
| 132 virtual ~DictionaryPrefStore() {} | 116 virtual ~DictionaryPrefStore() {} |
| 133 | 117 |
| 134 const base::DictionaryValue* dictionary_; | 118 const base::DictionaryValue* dictionary_; |
| 135 | 119 |
| 136 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); | 120 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); |
| 137 }; | 121 }; |
| 138 | 122 |
| 139 // Waits for a PrefStore to be initialized and then initializes the | |
| 140 // corresponding PrefHashStore. | |
| 141 // The observer deletes itself when its work is completed. | |
| 142 class InitializeHashStoreObserver : public PrefStore::Observer { | |
| 143 public: | |
| 144 // Creates an observer that will initialize |pref_hash_store| with the | |
| 145 // contents of |pref_store| when the latter is fully loaded. | |
| 146 InitializeHashStoreObserver( | |
| 147 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& | |
| 148 tracking_configuration, | |
| 149 size_t reporting_ids_count, | |
| 150 const scoped_refptr<PrefStore>& pref_store, | |
| 151 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl) | |
| 152 : tracking_configuration_(tracking_configuration), | |
| 153 reporting_ids_count_(reporting_ids_count), | |
| 154 pref_store_(pref_store), | |
| 155 pref_hash_store_impl_(pref_hash_store_impl.Pass()) {} | |
| 156 | |
| 157 virtual ~InitializeHashStoreObserver(); | |
| 158 | |
| 159 // PrefStore::Observer implementation. | |
| 160 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
| 161 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
| 162 | |
| 163 private: | |
| 164 const std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 165 tracking_configuration_; | |
| 166 const size_t reporting_ids_count_; | |
| 167 scoped_refptr<PrefStore> pref_store_; | |
| 168 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl_; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); | |
| 171 }; | |
| 172 | |
| 173 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} | |
| 174 | |
| 175 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} | |
| 176 | |
| 177 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { | |
| 178 // If we successfully loaded the preferences _and_ the PrefHashStoreImpl | |
| 179 // hasn't been initialized by someone else in the meantime, initialize it now. | |
| 180 const PrefHashStoreImpl::StoreVersion pre_update_version = | |
| 181 pref_hash_store_impl_->GetCurrentVersion(); | |
| 182 if (succeeded && pre_update_version < PrefHashStoreImpl::VERSION_LATEST) { | |
| 183 PrefHashFilter(pref_hash_store_impl_.PassAs<PrefHashStore>(), | |
| 184 tracking_configuration_, | |
| 185 NULL, | |
| 186 reporting_ids_count_).Initialize(*pref_store_); | |
| 187 UMA_HISTOGRAM_ENUMERATION( | |
| 188 "Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom", | |
| 189 pre_update_version, | |
| 190 PrefHashStoreImpl::VERSION_LATEST + 1); | |
| 191 } | |
| 192 pref_store_->RemoveObserver(this); | |
| 193 delete this; | |
| 194 } | |
| 195 | |
| 196 } // namespace | 123 } // namespace |
| 197 | 124 |
| 198 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved | 125 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved |
| 199 // out of Local State. This will resolve a race condition on Android and a | 126 // out of Local State. This will resolve a race condition on Android and a |
| 200 // privacy issue on ChromeOS. http://crbug.com/349158 | 127 // privacy issue on ChromeOS. http://crbug.com/349158 |
| 201 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 128 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
| 202 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 129 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 203 false; | 130 false; |
| 204 #else | 131 #else |
| 205 true; | 132 true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 // static | 174 // static |
| 248 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 175 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
| 249 return PrefHashFilter::GetResetTime(pref_service); | 176 return PrefHashFilter::GetResetTime(pref_service); |
| 250 } | 177 } |
| 251 | 178 |
| 252 // static | 179 // static |
| 253 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 180 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
| 254 PrefHashFilter::ClearResetTime(pref_service); | 181 PrefHashFilter::ClearResetTime(pref_service); |
| 255 } | 182 } |
| 256 | 183 |
| 257 void ProfilePrefStoreManager::ResetPrefHashStore() { | |
| 258 if (kPlatformSupportsPreferenceTracking) | |
| 259 GetPrefHashStoreImpl()->Reset(); | |
| 260 } | |
| 261 | |
| 262 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 184 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 263 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 185 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 264 TrackedPreferenceValidationDelegate* validation_delegate) { | 186 TrackedPreferenceValidationDelegate* validation_delegate) { |
| 265 scoped_ptr<PrefFilter> pref_filter; | 187 scoped_ptr<PrefFilter> pref_filter; |
| 266 if (!kPlatformSupportsPreferenceTracking) { | 188 if (!kPlatformSupportsPreferenceTracking) { |
| 267 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 189 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 268 io_task_runner, | 190 io_task_runner, |
| 269 scoped_ptr<PrefFilter>()); | 191 scoped_ptr<PrefFilter>()); |
| 270 } | 192 } |
| 271 | 193 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 unprotected_pref_store->AsWeakPtr()), | 246 unprotected_pref_store->AsWeakPtr()), |
| 325 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 247 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, |
| 326 protected_pref_store->AsWeakPtr()), | 248 protected_pref_store->AsWeakPtr()), |
| 327 raw_unprotected_pref_hash_filter, | 249 raw_unprotected_pref_hash_filter, |
| 328 raw_protected_pref_hash_filter); | 250 raw_protected_pref_hash_filter); |
| 329 | 251 |
| 330 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | 252 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, |
| 331 protected_pref_names); | 253 protected_pref_names); |
| 332 } | 254 } |
| 333 | 255 |
| 334 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | |
| 335 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | |
| 336 if (!kPlatformSupportsPreferenceTracking) | |
| 337 return; | |
| 338 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | |
| 339 const PrefHashStoreImpl::StoreVersion current_version = | |
| 340 pref_hash_store_impl->GetCurrentVersion(); | |
| 341 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | |
| 342 current_version, | |
| 343 PrefHashStoreImpl::VERSION_LATEST + 1); | |
| 344 | |
| 345 // Update the pref hash store if it's not at the latest version. | |
| 346 if (current_version != PrefHashStoreImpl::VERSION_LATEST) { | |
| 347 scoped_refptr<JsonPrefStore> pref_store = | |
| 348 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 349 io_task_runner, | |
| 350 scoped_ptr<PrefFilter>()); | |
| 351 pref_store->AddObserver( | |
| 352 new InitializeHashStoreObserver(tracking_configuration_, | |
| 353 reporting_ids_count_, | |
| 354 pref_store, | |
| 355 pref_hash_store_impl.Pass())); | |
| 356 pref_store->ReadPrefsAsync(NULL); | |
| 357 } | |
| 358 } | |
| 359 | |
| 360 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 256 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 361 const base::DictionaryValue& master_prefs) { | 257 const base::DictionaryValue& master_prefs) { |
| 362 // Create the profile directory if it doesn't exist yet (very possible on | 258 // Create the profile directory if it doesn't exist yet (very possible on |
| 363 // first run). | 259 // first run). |
| 364 if (!base::CreateDirectory(profile_path_)) | 260 if (!base::CreateDirectory(profile_path_)) |
| 365 return false; | 261 return false; |
| 366 | 262 |
| 367 // This will write out to a single combined file which will be immediately | 263 // This will write out to a single combined file which will be immediately |
| 368 // migrated to two files on load. | 264 // migrated to two files on load. |
| 369 JSONFileValueSerializer serializer( | 265 JSONFileValueSerializer serializer( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 | 303 |
| 408 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 304 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
| 409 DCHECK(kPlatformSupportsPreferenceTracking); | 305 DCHECK(kPlatformSupportsPreferenceTracking); |
| 410 | 306 |
| 411 return make_scoped_ptr(new PrefHashStoreImpl( | 307 return make_scoped_ptr(new PrefHashStoreImpl( |
| 412 seed_, | 308 seed_, |
| 413 device_id_, | 309 device_id_, |
| 414 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 310 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
| 415 profile_path_.AsUTF8Unsafe(), local_state_)))); | 311 profile_path_.AsUTF8Unsafe(), local_state_)))); |
| 416 } | 312 } |
| OLD | NEW |