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)) { | |
|
Bernhard Bauer
2014/03/26 15:05:50
Is the static_cast because of the ?: operator?
I
erikwright (departed)
2014/03/26 21:08:12
Done.
| |
| 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_ = ""; | |
|
Bernhard Bauer
2014/03/26 15:05:50
In general, using std::string() instead of "" save
erikwright (departed)
2014/03/26 21:08:12
Done.
| |
| 48 version_.reset(); | |
| 49 } | |
| 50 | |
| 51 virtual bool IsInitialized() const OVERRIDE { | |
| 52 return dictionary_; | |
|
Bernhard Bauer
2014/03/26 15:05:50
I would be a bit happier if we had an explicit coe
erikwright (departed)
2014/03/26 18:37:15
This is explicitly supported by scoped_ptr, and co
Bernhard Bauer
2014/03/27 15:10:45
Oh, right, it's a scoped_ptr. Yeah, those are safe
erikwright (departed)
2014/03/27 19:26:21
Ah, right. Yeah, that would be an improvement due
| |
| 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 { return super_mac_; } | |
| 65 | |
| 66 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | |
| 67 super_mac_ = super_mac; | |
| 68 } | |
| 69 | |
| 70 virtual bool GetVersion(int* version) const OVERRIDE { | |
| 71 if (!version_) | |
| 72 return false; | |
| 73 *version = *version_; | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 virtual void SetVersion(int version) OVERRIDE { | |
| 78 version_.reset(new int(version)); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 class SimpleMutableDictionary | |
| 83 : public HashStoreContents::MutableDictionary { | |
| 84 public: | |
| 85 explicit SimpleMutableDictionary(DictionaryHashStoreContents* outer) | |
| 86 : outer_(outer) {} | |
| 87 | |
| 88 virtual ~SimpleMutableDictionary() {} | |
| 89 | |
| 90 // MutableDictionary implementation | |
| 91 virtual base::DictionaryValue* operator->() OVERRIDE { | |
| 92 if (!outer_->dictionary_) | |
| 93 outer_->dictionary_.reset(new base::DictionaryValue); | |
| 94 return outer_->dictionary_.get(); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 DictionaryHashStoreContents* outer_; | |
| 99 DISALLOW_COPY_AND_ASSIGN(SimpleMutableDictionary); | |
| 100 }; | |
| 101 | |
| 102 const std::string hash_store_id_; | |
| 103 std::string super_mac_; | |
| 104 scoped_ptr<int> version_; | |
| 105 scoped_ptr<base::DictionaryValue> dictionary_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(DictionaryHashStoreContents); | |
| 108 }; | |
| 109 | |
| 22 // An in-memory PrefStore backed by an immutable DictionaryValue. | 110 // An in-memory PrefStore backed by an immutable DictionaryValue. |
| 23 class DictionaryPrefStore : public PrefStore { | 111 class DictionaryPrefStore : public PrefStore { |
| 24 public: | 112 public: |
| 25 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | 113 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
| 26 : dictionary_(dictionary) {} | 114 : dictionary_(dictionary) {} |
| 27 | 115 |
| 28 virtual bool GetValue(const std::string& key, | 116 virtual bool GetValue(const std::string& key, |
| 29 const base::Value** result) const OVERRIDE { | 117 const base::Value** result) const OVERRIDE { |
| 30 const base::Value* tmp = NULL; | 118 const base::Value* tmp = NULL; |
| 31 if (!dictionary_->Get(key, &tmp)) | 119 if (!dictionary_->Get(key, &tmp)) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 42 const base::DictionaryValue* dictionary_; | 130 const base::DictionaryValue* dictionary_; |
| 43 | 131 |
| 44 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); | 132 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); |
| 45 }; | 133 }; |
| 46 | 134 |
| 47 } // namespace | 135 } // namespace |
| 48 | 136 |
| 49 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved | 137 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved |
| 50 // out of Local State. This will resolve a race condition on Android and a | 138 // out of Local State. This will resolve a race condition on Android and a |
| 51 // privacy issue on ChromeOS. http://crbug.com/349158 | 139 // privacy issue on ChromeOS. http://crbug.com/349158 |
| 52 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 140 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
|
Bernhard Bauer
2014/03/26 15:05:50
Hm, couldn't you make this a #define and then #ifd
erikwright (departed)
2014/03/26 21:08:12
It's also referenced in chrome_pref_service_factor
Bernhard Bauer
2014/03/27 15:10:45
Oh, ok. If it will be removed soon anyway, it's fi
| |
| 53 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 141 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 54 false; | 142 false; |
| 55 #else | 143 #else |
| 56 true; | 144 true; |
| 57 #endif | 145 #endif |
| 58 | 146 |
| 59 // Waits for a PrefStore to be initialized and then initializes the | 147 // Waits for a PrefStore to be initialized and then initializes the |
| 60 // corresponding PrefHashStore. | 148 // corresponding PrefHashStore. |
| 61 // The observer deletes itself when its work is completed. | 149 // The observer deletes itself when its work is completed. |
| 62 class ProfilePrefStoreManager::InitializeHashStoreObserver | 150 class ProfilePrefStoreManager::InitializeHashStoreObserver |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 } | 255 } |
| 168 | 256 |
| 169 void ProfilePrefStoreManager::ResetPrefHashStore() { | 257 void ProfilePrefStoreManager::ResetPrefHashStore() { |
| 170 if (kPlatformSupportsPreferenceTracking) | 258 if (kPlatformSupportsPreferenceTracking) |
| 171 GetPrefHashStoreImpl()->Reset(); | 259 GetPrefHashStoreImpl()->Reset(); |
| 172 } | 260 } |
| 173 | 261 |
| 174 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 262 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 175 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 263 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 176 scoped_ptr<PrefFilter> pref_filter; | 264 scoped_ptr<PrefFilter> pref_filter; |
| 177 if (kPlatformSupportsPreferenceTracking) { | 265 if (!kPlatformSupportsPreferenceTracking) { |
| 178 pref_filter.reset( | 266 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 179 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 267 io_task_runner, |
| 180 tracking_configuration_, | 268 scoped_ptr<PrefFilter>()); |
| 181 reporting_ids_count_)); | |
| 182 } | 269 } |
| 183 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 270 |
| 184 io_task_runner, | 271 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 185 pref_filter.Pass()); | 272 unprotected_configuration; |
| 273 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 274 protected_configuration; | |
| 275 std::set<std::string> protected_pref_names; | |
| 276 for (size_t i = 0; i < tracking_configuration_.size(); ++i) { | |
| 277 if (tracking_configuration_[i].enforcement_level > | |
| 278 PrefHashFilter::NO_ENFORCEMENT) { | |
| 279 protected_configuration.push_back(tracking_configuration_[i]); | |
| 280 protected_pref_names.insert(tracking_configuration_[i].name); | |
| 281 } else { | |
| 282 unprotected_configuration.push_back(tracking_configuration_[i]); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
| 287 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 288 unprotected_configuration, | |
| 289 reporting_ids_count_)); | |
| 290 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( | |
| 291 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 292 protected_configuration, | |
| 293 reporting_ids_count_)); | |
| 294 | |
| 295 scoped_refptr<PersistentPrefStore> unprotected_pref_store( | |
| 296 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 297 io_task_runner, | |
| 298 unprotected_pref_hash_filter.PassAs<PrefFilter>())); | |
| 299 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( | |
| 300 profile_path_.Append(chrome::kProtectedPreferencesFilename), | |
| 301 io_task_runner, | |
| 302 protected_pref_hash_filter.PassAs<PrefFilter>())); | |
| 303 | |
| 304 return new ProtectedPrefStore( | |
| 305 unprotected_pref_store, | |
| 306 protected_pref_store, | |
| 307 protected_pref_names, | |
| 308 base::Bind(&PrefHashFilter::MigrateValues, | |
| 309 base::Owned(new PrefHashFilter( | |
| 310 CopyPrefHashStore(), | |
| 311 protected_configuration, | |
| 312 reporting_ids_count_)), | |
| 313 unprotected_pref_store, | |
| 314 protected_pref_store)); | |
| 186 } | 315 } |
| 187 | 316 |
| 188 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 317 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
| 189 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 318 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 190 if (!kPlatformSupportsPreferenceTracking) | 319 if (!kPlatformSupportsPreferenceTracking) |
| 191 return; | 320 return; |
| 192 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 321 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
| 193 const PrefHashStoreImpl::StoreVersion current_version = | 322 const PrefHashStoreImpl::StoreVersion current_version = |
| 194 pref_hash_store_impl->GetCurrentVersion(); | 323 pref_hash_store_impl->GetCurrentVersion(); |
| 195 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 324 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 211 } | 340 } |
| 212 } | 341 } |
| 213 | 342 |
| 214 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 343 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 215 const base::DictionaryValue& master_prefs) { | 344 const base::DictionaryValue& master_prefs) { |
| 216 // Create the profile directory if it doesn't exist yet (very possible on | 345 // Create the profile directory if it doesn't exist yet (very possible on |
| 217 // first run). | 346 // first run). |
| 218 if (!base::CreateDirectory(profile_path_)) | 347 if (!base::CreateDirectory(profile_path_)) |
| 219 return false; | 348 return false; |
| 220 | 349 |
| 350 // This will write out to a single combined file which will be immediately | |
| 351 // migrated to two files on load. | |
| 221 JSONFileValueSerializer serializer( | 352 JSONFileValueSerializer serializer( |
| 222 GetPrefFilePathFromProfilePath(profile_path_)); | 353 GetPrefFilePathFromProfilePath(profile_path_)); |
| 223 | 354 |
| 224 // Call Serialize (which does IO) on the main thread, which would _normally_ | 355 // 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 | 356 // be verboten. In this case however, we require this IO to synchronously |
| 226 // complete before Chrome can start (as master preferences seed the Local | 357 // complete before Chrome can start (as master preferences seed the Local |
| 227 // State and Preferences files). This won't trip ThreadIORestrictions as they | 358 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 228 // won't have kicked in yet on the main thread. | 359 // won't have kicked in yet on the main thread. |
| 229 bool success = serializer.Serialize(master_prefs); | 360 bool success = serializer.Serialize(master_prefs); |
| 230 | 361 |
| 231 if (success && kPlatformSupportsPreferenceTracking) { | 362 if (success && kPlatformSupportsPreferenceTracking) { |
| 232 scoped_refptr<const PrefStore> pref_store( | 363 scoped_refptr<const PrefStore> pref_store( |
| 233 new DictionaryPrefStore(&master_prefs)); | 364 new DictionaryPrefStore(&master_prefs)); |
| 234 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 365 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 235 tracking_configuration_, | 366 tracking_configuration_, |
| 236 reporting_ids_count_).Initialize(*pref_store); | 367 reporting_ids_count_).Initialize(*pref_store); |
| 237 } | 368 } |
| 238 | 369 |
| 239 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 370 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 240 return success; | 371 return success; |
| 241 } | 372 } |
| 242 | 373 |
| 374 PersistentPrefStore* | |
| 375 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( | |
| 376 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | |
| 377 scoped_ptr<PrefFilter> pref_filter; | |
| 378 if (kPlatformSupportsPreferenceTracking) { | |
| 379 pref_filter.reset( | |
| 380 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
| 381 tracking_configuration_, | |
| 382 reporting_ids_count_)); | |
| 383 } | |
| 384 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | |
| 385 io_task_runner, | |
| 386 pref_filter.Pass()); | |
| 387 } | |
| 388 | |
| 243 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 389 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
| 244 DCHECK(kPlatformSupportsPreferenceTracking); | 390 DCHECK(kPlatformSupportsPreferenceTracking); |
| 245 | 391 |
| 246 return make_scoped_ptr(new PrefHashStoreImpl( | 392 return make_scoped_ptr(new PrefHashStoreImpl( |
| 247 seed_, | 393 seed_, |
| 248 device_id_, | 394 device_id_, |
| 249 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 395 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
| 250 profile_path_.AsUTF8Unsafe(), local_state_)))); | 396 profile_path_.AsUTF8Unsafe(), local_state_)))); |
| 251 } | 397 } |
| 398 | |
| 399 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { | |
| 400 DCHECK(kPlatformSupportsPreferenceTracking); | |
| 401 | |
| 402 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), | |
| 403 local_state_); | |
| 404 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( | |
| 405 seed_, | |
| 406 device_id_, | |
| 407 scoped_ptr<HashStoreContents>( | |
| 408 new DictionaryHashStoreContents(real_contents)))); | |
| 409 } | |
| OLD | NEW |