| 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 | |
| 26 // directly as a dictionary. Uses an equivalent layout to | |
| 27 // PrefStoreHashStoreContents. | |
| 28 class DictionaryHashStoreContents : public HashStoreContents { | |
| 29 public: | |
| 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 | |
| 32 // any other way. | |
| 33 explicit DictionaryHashStoreContents(const HashStoreContents& to_copy) | |
| 34 : hash_store_id_(to_copy.hash_store_id()), | |
| 35 super_mac_(to_copy.GetSuperMac()) { | |
| 36 if (to_copy.IsInitialized()) | |
| 37 dictionary_.reset(to_copy.GetContents()->DeepCopy()); | |
| 38 int version = 0; | |
| 39 if (to_copy.GetVersion(&version)) | |
| 40 version_.reset(new int(version)); | |
| 41 } | |
| 42 | |
| 43 // HashStoreContents implementation | |
| 44 virtual std::string hash_store_id() const OVERRIDE { return hash_store_id_; } | |
| 45 | |
| 46 virtual void Reset() OVERRIDE { | |
| 47 dictionary_.reset(); | |
| 48 super_mac_.clear(); | |
| 49 version_.reset(); | |
| 50 } | |
| 51 | |
| 52 virtual bool IsInitialized() const OVERRIDE { | |
| 53 return dictionary_; | |
| 54 } | |
| 55 | |
| 56 virtual const base::DictionaryValue* GetContents() const OVERRIDE{ | |
| 57 return dictionary_.get(); | |
| 58 } | |
| 59 | |
| 60 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | |
| 61 return scoped_ptr<MutableDictionary>( | |
| 62 new SimpleMutableDictionary(this)); | |
| 63 } | |
| 64 | |
| 65 virtual std::string GetSuperMac() const OVERRIDE { return super_mac_; } | |
| 66 | |
| 67 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | |
| 68 super_mac_ = super_mac; | |
| 69 } | |
| 70 | |
| 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 {} | |
| 83 | |
| 84 private: | |
| 85 class SimpleMutableDictionary | |
| 86 : public HashStoreContents::MutableDictionary { | |
| 87 public: | |
| 88 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) | |
| 89 : outer_(outer) {} | |
| 90 | |
| 91 virtual ~SimpleMutableDictionary() {} | |
| 92 | |
| 93 // MutableDictionary implementation | |
| 94 virtual base::DictionaryValue* operator->() OVERRIDE { | |
| 95 if (!outer_->dictionary_) | |
| 96 outer_->dictionary_.reset(new base::DictionaryValue); | |
| 97 return outer_->dictionary_.get(); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 DictionaryHashStoreContents* outer_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); | |
| 104 }; | |
| 105 | |
| 106 const std::string hash_store_id_; | |
| 107 std::string super_mac_; | |
| 108 scoped_ptr<int> version_; | |
| 109 scoped_ptr<base::DictionaryValue> dictionary_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); | |
| 112 }; | |
| 113 | |
| 114 // An in-memory PrefStore backed by an immutable DictionaryValue. | 25 // An in-memory PrefStore backed by an immutable DictionaryValue. |
| 115 class DictionaryPrefStore : public PrefStore { | 26 class DictionaryPrefStore : public PrefStore { |
| 116 public: | 27 public: |
| 117 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | 28 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
| 118 : dictionary_(dictionary) {} | 29 : dictionary_(dictionary) {} |
| 119 | 30 |
| 120 virtual bool GetValue(const std::string& key, | 31 virtual bool GetValue(const std::string& key, |
| 121 const base::Value** result) const OVERRIDE { | 32 const base::Value** result) const OVERRIDE { |
| 122 const base::Value* tmp = NULL; | 33 const base::Value* tmp = NULL; |
| 123 if (!dictionary_->Get(key, &tmp)) | 34 if (!dictionary_->Get(key, &tmp)) |
| 124 return false; | 35 return false; |
| 125 | 36 |
| 126 if (result) | 37 if (result) |
| 127 *result = tmp; | 38 *result = tmp; |
| 128 return true; | 39 return true; |
| 129 } | 40 } |
| 130 | 41 |
| 131 private: | 42 private: |
| 132 virtual ~DictionaryPrefStore() {} | 43 virtual ~DictionaryPrefStore() {} |
| 133 | 44 |
| 134 const base::DictionaryValue* dictionary_; | 45 const base::DictionaryValue* dictionary_; |
| 135 | 46 |
| 136 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); | 47 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); |
| 137 }; | 48 }; |
| 138 | 49 |
| 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 | 50 } // namespace |
| 197 | 51 |
| 198 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved | 52 // 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 | 53 // out of Local State. This will resolve a race condition on Android and a |
| 200 // privacy issue on ChromeOS. http://crbug.com/349158 | 54 // privacy issue on ChromeOS. http://crbug.com/349158 |
| 201 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 55 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
| 202 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 56 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 203 false; | 57 false; |
| 204 #else | 58 #else |
| 205 true; | 59 true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // static | 101 // static |
| 248 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 102 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
| 249 return PrefHashFilter::GetResetTime(pref_service); | 103 return PrefHashFilter::GetResetTime(pref_service); |
| 250 } | 104 } |
| 251 | 105 |
| 252 // static | 106 // static |
| 253 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 107 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
| 254 PrefHashFilter::ClearResetTime(pref_service); | 108 PrefHashFilter::ClearResetTime(pref_service); |
| 255 } | 109 } |
| 256 | 110 |
| 257 void ProfilePrefStoreManager::ResetPrefHashStore() { | |
| 258 if (kPlatformSupportsPreferenceTracking) | |
| 259 GetPrefHashStoreImpl()->Reset(); | |
| 260 } | |
| 261 | |
| 262 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 111 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 263 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 112 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 264 TrackedPreferenceValidationDelegate* validation_delegate) { | 113 TrackedPreferenceValidationDelegate* validation_delegate) { |
| 265 scoped_ptr<PrefFilter> pref_filter; | 114 scoped_ptr<PrefFilter> pref_filter; |
| 266 if (!kPlatformSupportsPreferenceTracking) { | 115 if (!kPlatformSupportsPreferenceTracking) { |
| 267 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 116 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 268 io_task_runner, | 117 io_task_runner, |
| 269 scoped_ptr<PrefFilter>()); | 118 scoped_ptr<PrefFilter>()); |
| 270 } | 119 } |
| 271 | 120 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 unprotected_pref_store->AsWeakPtr()), | 173 unprotected_pref_store->AsWeakPtr()), |
| 325 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 174 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, |
| 326 protected_pref_store->AsWeakPtr()), | 175 protected_pref_store->AsWeakPtr()), |
| 327 raw_unprotected_pref_hash_filter, | 176 raw_unprotected_pref_hash_filter, |
| 328 raw_protected_pref_hash_filter); | 177 raw_protected_pref_hash_filter); |
| 329 | 178 |
| 330 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | 179 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, |
| 331 protected_pref_names); | 180 protected_pref_names); |
| 332 } | 181 } |
| 333 | 182 |
| 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( | 183 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 361 const base::DictionaryValue& master_prefs) { | 184 const base::DictionaryValue& master_prefs) { |
| 362 // Create the profile directory if it doesn't exist yet (very possible on | 185 // Create the profile directory if it doesn't exist yet (very possible on |
| 363 // first run). | 186 // first run). |
| 364 if (!base::CreateDirectory(profile_path_)) | 187 if (!base::CreateDirectory(profile_path_)) |
| 365 return false; | 188 return false; |
| 366 | 189 |
| 367 // This will write out to a single combined file which will be immediately | 190 // This will write out to a single combined file which will be immediately |
| 368 // migrated to two files on load. | 191 // migrated to two files on load. |
| 369 JSONFileValueSerializer serializer( | 192 JSONFileValueSerializer serializer( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 pref_filter.Pass()); | 228 pref_filter.Pass()); |
| 406 } | 229 } |
| 407 | 230 |
| 408 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 231 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
| 409 DCHECK(kPlatformSupportsPreferenceTracking); | 232 DCHECK(kPlatformSupportsPreferenceTracking); |
| 410 | 233 |
| 411 return make_scoped_ptr(new PrefHashStoreImpl( | 234 return make_scoped_ptr(new PrefHashStoreImpl( |
| 412 seed_, | 235 seed_, |
| 413 device_id_, | 236 device_id_, |
| 414 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 237 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
| 415 profile_path_.AsUTF8Unsafe(), local_state_)))); | 238 profile_path_.AsUTF8Unsafe(), local_state_)), |
| 239 true)); |
| 416 } | 240 } |
| OLD | NEW |