| 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); |
| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return profile_path.Append(chrome::kPreferencesFilename); | 238 return profile_path.Append(chrome::kPreferencesFilename); |
| 152 } | 239 } |
| 153 | 240 |
| 154 // static | 241 // static |
| 155 void ProfilePrefStoreManager::ResetAllPrefHashStores(PrefService* local_state) { | 242 void ProfilePrefStoreManager::ResetAllPrefHashStores(PrefService* local_state) { |
| 156 PrefServiceHashStoreContents::ResetAllPrefHashStores(local_state); | 243 PrefServiceHashStoreContents::ResetAllPrefHashStores(local_state); |
| 157 } | 244 } |
| 158 | 245 |
| 159 // static | 246 // static |
| 160 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 247 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
| 248 // It's a bit of a coincidence that this (and ClearResetTime) work(s). The |
| 249 // PrefHashFilter attached to the protected pref store will store the reset |
| 250 // time directly in the protected pref store without going through the |
| 251 // SegregatedPrefStore. |
| 252 |
| 253 // PrefHashFilter::GetResetTime will read the value through the pref service, |
| 254 // and thus through the SegregatedPrefStore. Even though it's not listed as |
| 255 // "protected" it will be read from the protected store prefentially to the |
| 256 // (NULL) value in the unprotected pref store. |
| 161 return PrefHashFilter::GetResetTime(pref_service); | 257 return PrefHashFilter::GetResetTime(pref_service); |
| 162 } | 258 } |
| 163 | 259 |
| 164 // static | 260 // static |
| 165 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 261 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
| 262 // PrefHashFilter::ClearResetTime will clear the value through the pref |
| 263 // service, and thus through the SegregatedPrefStore. Since it's not listed as |
| 264 // "protected" it will be migrated from the protected store to the unprotected |
| 265 // pref store before being deleted from the latter. |
| 166 PrefHashFilter::ClearResetTime(pref_service); | 266 PrefHashFilter::ClearResetTime(pref_service); |
| 167 } | 267 } |
| 168 | 268 |
| 169 void ProfilePrefStoreManager::ResetPrefHashStore() { | 269 void ProfilePrefStoreManager::ResetPrefHashStore() { |
| 170 if (kPlatformSupportsPreferenceTracking) | 270 if (kPlatformSupportsPreferenceTracking) |
| 171 GetPrefHashStoreImpl()->Reset(); | 271 GetPrefHashStoreImpl()->Reset(); |
| 172 } | 272 } |
| 173 | 273 |
| 174 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 274 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 175 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 275 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 176 scoped_ptr<PrefFilter> pref_filter; | 276 scoped_ptr<PrefFilter> pref_filter; |
| 177 if (kPlatformSupportsPreferenceTracking) { | 277 if (!kPlatformSupportsPreferenceTracking) { |
| 178 pref_filter.reset( | 278 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 179 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 279 io_task_runner, |
| 180 tracking_configuration_, | 280 scoped_ptr<PrefFilter>()); |
| 181 reporting_ids_count_)); | |
| 182 } | 281 } |
| 183 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 282 |
| 184 io_task_runner, | 283 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 185 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) { |
| 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( |
| 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>(), |
| 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>())); |
| 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_)), |
| 325 unprotected_pref_store, |
| 326 protected_pref_store)); |
| 186 } | 327 } |
| 187 | 328 |
| 188 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 329 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
| 189 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 330 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 190 if (!kPlatformSupportsPreferenceTracking) | 331 if (!kPlatformSupportsPreferenceTracking) |
| 191 return; | 332 return; |
| 192 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 333 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
| 193 const PrefHashStoreImpl::StoreVersion current_version = | 334 const PrefHashStoreImpl::StoreVersion current_version = |
| 194 pref_hash_store_impl->GetCurrentVersion(); | 335 pref_hash_store_impl->GetCurrentVersion(); |
| 195 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 336 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
| (...skipping 15 matching lines...) Expand all Loading... |
| 211 } | 352 } |
| 212 } | 353 } |
| 213 | 354 |
| 214 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 355 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 215 const base::DictionaryValue& master_prefs) { | 356 const base::DictionaryValue& master_prefs) { |
| 216 // 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 |
| 217 // first run). | 358 // first run). |
| 218 if (!base::CreateDirectory(profile_path_)) | 359 if (!base::CreateDirectory(profile_path_)) |
| 219 return false; | 360 return false; |
| 220 | 361 |
| 362 // This will write out to a single combined file which will be immediately |
| 363 // migrated to two files on load. |
| 221 JSONFileValueSerializer serializer( | 364 JSONFileValueSerializer serializer( |
| 222 GetPrefFilePathFromProfilePath(profile_path_)); | 365 GetPrefFilePathFromProfilePath(profile_path_)); |
| 223 | 366 |
| 224 // 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_ |
| 225 // 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 |
| 226 // complete before Chrome can start (as master preferences seed the Local | 369 // complete before Chrome can start (as master preferences seed the Local |
| 227 // State and Preferences files). This won't trip ThreadIORestrictions as they | 370 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 228 // won't have kicked in yet on the main thread. | 371 // won't have kicked in yet on the main thread. |
| 229 bool success = serializer.Serialize(master_prefs); | 372 bool success = serializer.Serialize(master_prefs); |
| 230 | 373 |
| 231 if (success && kPlatformSupportsPreferenceTracking) { | 374 if (success && kPlatformSupportsPreferenceTracking) { |
| 232 scoped_refptr<const PrefStore> pref_store( | 375 scoped_refptr<const PrefStore> pref_store( |
| 233 new DictionaryPrefStore(&master_prefs)); | 376 new DictionaryPrefStore(&master_prefs)); |
| 234 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 377 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 235 tracking_configuration_, | 378 tracking_configuration_, |
| 236 reporting_ids_count_).Initialize(*pref_store); | 379 reporting_ids_count_).Initialize(*pref_store); |
| 237 } | 380 } |
| 238 | 381 |
| 239 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 382 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 240 return success; | 383 return success; |
| 241 } | 384 } |
| 242 | 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 |
| 243 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 401 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
| 244 DCHECK(kPlatformSupportsPreferenceTracking); | 402 DCHECK(kPlatformSupportsPreferenceTracking); |
| 245 | 403 |
| 246 return make_scoped_ptr(new PrefHashStoreImpl( | 404 return make_scoped_ptr(new PrefHashStoreImpl( |
| 247 seed_, | 405 seed_, |
| 248 device_id_, | 406 device_id_, |
| 249 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 407 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
| 250 profile_path_.AsUTF8Unsafe(), local_state_)))); | 408 profile_path_.AsUTF8Unsafe(), local_state_)))); |
| 251 } | 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 |