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 |
| 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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 268 } |
181 | 269 |
182 void ProfilePrefStoreManager::ResetPrefHashStore() { | 270 void ProfilePrefStoreManager::ResetPrefHashStore() { |
183 if (kPlatformSupportsPreferenceTracking) | 271 if (kPlatformSupportsPreferenceTracking) |
184 GetPrefHashStoreImpl()->Reset(); | 272 GetPrefHashStoreImpl()->Reset(); |
185 } | 273 } |
186 | 274 |
187 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 275 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
188 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 276 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
189 scoped_ptr<PrefFilter> pref_filter; | 277 scoped_ptr<PrefFilter> pref_filter; |
190 if (kPlatformSupportsPreferenceTracking) { | 278 if (!kPlatformSupportsPreferenceTracking) { |
191 pref_filter.reset( | 279 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
192 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 280 io_task_runner, |
193 tracking_configuration_, | 281 scoped_ptr<PrefFilter>()); |
194 reporting_ids_count_)); | |
195 } | 282 } |
196 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 283 |
197 io_task_runner, | 284 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
198 pref_filter.Pass()); | 285 unprotected_configuration; |
| 286 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 287 protected_configuration; |
| 288 std::set<std::string> protected_pref_names; |
| 289 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator |
| 290 it = tracking_configuration_.begin(); |
| 291 it != tracking_configuration_.end(); |
| 292 ++it) { |
| 293 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { |
| 294 protected_configuration.push_back(*it); |
| 295 protected_pref_names.insert(it->name); |
| 296 } else { |
| 297 unprotected_configuration.push_back(*it); |
| 298 } |
| 299 } |
| 300 |
| 301 scoped_ptr<PrefFilter> unprotected_pref_hash_filter( |
| 302 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 303 unprotected_configuration, |
| 304 reporting_ids_count_)); |
| 305 scoped_ptr<PrefFilter> protected_pref_hash_filter( |
| 306 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 307 protected_configuration, |
| 308 reporting_ids_count_)); |
| 309 |
| 310 scoped_refptr<PersistentPrefStore> unprotected_pref_store( |
| 311 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 312 io_task_runner, |
| 313 unprotected_pref_hash_filter.Pass())); |
| 314 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( |
| 315 profile_path_.Append(chrome::kProtectedPreferencesFilename), |
| 316 io_task_runner, |
| 317 protected_pref_hash_filter.Pass())); |
| 318 |
| 319 // The on_initialized callback is used to migrate newly protected values from |
| 320 // the main Preferences store to the Protected Preferences store. It is also |
| 321 // responsible for the initial migration to a two-store model. |
| 322 return new SegregatedPrefStore( |
| 323 unprotected_pref_store, |
| 324 protected_pref_store, |
| 325 protected_pref_names, |
| 326 base::Bind(&PrefHashFilter::MigrateValues, |
| 327 base::Owned(new PrefHashFilter( |
| 328 CopyPrefHashStore(), |
| 329 protected_configuration, |
| 330 reporting_ids_count_)), |
| 331 unprotected_pref_store, |
| 332 protected_pref_store)); |
199 } | 333 } |
200 | 334 |
201 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 335 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
202 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 336 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
203 if (!kPlatformSupportsPreferenceTracking) | 337 if (!kPlatformSupportsPreferenceTracking) |
204 return; | 338 return; |
205 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 339 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
206 const PrefHashStoreImpl::StoreVersion current_version = | 340 const PrefHashStoreImpl::StoreVersion current_version = |
207 pref_hash_store_impl->GetCurrentVersion(); | 341 pref_hash_store_impl->GetCurrentVersion(); |
208 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 342 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
(...skipping 15 matching lines...) Expand all Loading... |
224 } | 358 } |
225 } | 359 } |
226 | 360 |
227 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 361 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
228 const base::DictionaryValue& master_prefs) { | 362 const base::DictionaryValue& master_prefs) { |
229 // Create the profile directory if it doesn't exist yet (very possible on | 363 // Create the profile directory if it doesn't exist yet (very possible on |
230 // first run). | 364 // first run). |
231 if (!base::CreateDirectory(profile_path_)) | 365 if (!base::CreateDirectory(profile_path_)) |
232 return false; | 366 return false; |
233 | 367 |
| 368 // This will write out to a single combined file which will be immediately |
| 369 // migrated to two files on load. |
234 JSONFileValueSerializer serializer( | 370 JSONFileValueSerializer serializer( |
235 GetPrefFilePathFromProfilePath(profile_path_)); | 371 GetPrefFilePathFromProfilePath(profile_path_)); |
236 | 372 |
237 // Call Serialize (which does IO) on the main thread, which would _normally_ | 373 // Call Serialize (which does IO) on the main thread, which would _normally_ |
238 // be verboten. In this case however, we require this IO to synchronously | 374 // be verboten. In this case however, we require this IO to synchronously |
239 // complete before Chrome can start (as master preferences seed the Local | 375 // complete before Chrome can start (as master preferences seed the Local |
240 // State and Preferences files). This won't trip ThreadIORestrictions as they | 376 // State and Preferences files). This won't trip ThreadIORestrictions as they |
241 // won't have kicked in yet on the main thread. | 377 // won't have kicked in yet on the main thread. |
242 bool success = serializer.Serialize(master_prefs); | 378 bool success = serializer.Serialize(master_prefs); |
243 | 379 |
244 if (success && kPlatformSupportsPreferenceTracking) { | 380 if (success && kPlatformSupportsPreferenceTracking) { |
245 scoped_refptr<const PrefStore> pref_store( | 381 scoped_refptr<const PrefStore> pref_store( |
246 new DictionaryPrefStore(&master_prefs)); | 382 new DictionaryPrefStore(&master_prefs)); |
247 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 383 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
248 tracking_configuration_, | 384 tracking_configuration_, |
249 reporting_ids_count_).Initialize(*pref_store); | 385 reporting_ids_count_).Initialize(*pref_store); |
250 } | 386 } |
251 | 387 |
252 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 388 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
253 return success; | 389 return success; |
254 } | 390 } |
255 | 391 |
| 392 PersistentPrefStore* |
| 393 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( |
| 394 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 395 scoped_ptr<PrefFilter> pref_filter; |
| 396 if (kPlatformSupportsPreferenceTracking) { |
| 397 pref_filter.reset( |
| 398 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 399 tracking_configuration_, |
| 400 reporting_ids_count_)); |
| 401 } |
| 402 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 403 io_task_runner, |
| 404 pref_filter.Pass()); |
| 405 } |
| 406 |
256 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 407 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
257 DCHECK(kPlatformSupportsPreferenceTracking); | 408 DCHECK(kPlatformSupportsPreferenceTracking); |
258 | 409 |
259 return make_scoped_ptr(new PrefHashStoreImpl( | 410 return make_scoped_ptr(new PrefHashStoreImpl( |
260 seed_, | 411 seed_, |
261 device_id_, | 412 device_id_, |
262 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 413 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
263 profile_path_.AsUTF8Unsafe(), local_state_)))); | 414 profile_path_.AsUTF8Unsafe(), local_state_)))); |
264 } | 415 } |
| 416 |
| 417 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { |
| 418 DCHECK(kPlatformSupportsPreferenceTracking); |
| 419 |
| 420 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), |
| 421 local_state_); |
| 422 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( |
| 423 seed_, |
| 424 device_id_, |
| 425 scoped_ptr<HashStoreContents>( |
| 426 new DictionaryHashStoreContents(real_contents)))); |
| 427 } |
OLD | NEW |