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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 } | 254 } |
168 | 255 |
169 void ProfilePrefStoreManager::ResetPrefHashStore() { | 256 void ProfilePrefStoreManager::ResetPrefHashStore() { |
170 if (kPlatformSupportsPreferenceTracking) | 257 if (kPlatformSupportsPreferenceTracking) |
171 GetPrefHashStoreImpl()->Reset(); | 258 GetPrefHashStoreImpl()->Reset(); |
172 } | 259 } |
173 | 260 |
174 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 261 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
175 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 262 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
176 scoped_ptr<PrefFilter> pref_filter; | 263 scoped_ptr<PrefFilter> pref_filter; |
177 if (kPlatformSupportsPreferenceTracking) { | 264 if (!kPlatformSupportsPreferenceTracking) { |
178 pref_filter.reset( | 265 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
179 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 266 io_task_runner, |
180 tracking_configuration_, | 267 scoped_ptr<PrefFilter>()); |
181 reporting_ids_count_)); | |
182 } | 268 } |
183 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 269 |
184 io_task_runner, | 270 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
185 pref_filter.Pass()); | 271 unprotected_configuration; |
| 272 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 273 protected_configuration; |
| 274 std::set<std::string> protected_pref_names; |
| 275 for (size_t i = 0; i < tracking_configuration_.size(); ++i) { |
| 276 if (tracking_configuration_[i].enforcement_level > |
| 277 PrefHashFilter::NO_ENFORCEMENT) { |
| 278 protected_configuration.push_back(tracking_configuration_[i]); |
| 279 protected_pref_names.insert(tracking_configuration_[i].name); |
| 280 } else { |
| 281 unprotected_configuration.push_back(tracking_configuration_[i]); |
| 282 } |
| 283 } |
| 284 |
| 285 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( |
| 286 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 287 unprotected_configuration, |
| 288 reporting_ids_count_)); |
| 289 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( |
| 290 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 291 protected_configuration, |
| 292 reporting_ids_count_)); |
| 293 |
| 294 scoped_refptr<PersistentPrefStore> unprotected_pref_store( |
| 295 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 296 io_task_runner, |
| 297 unprotected_pref_hash_filter.PassAs<PrefFilter>())); |
| 298 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( |
| 299 profile_path_.Append(chrome::kProtectedPreferencesFilename), |
| 300 io_task_runner, |
| 301 protected_pref_hash_filter.PassAs<PrefFilter>())); |
| 302 |
| 303 return new SegregatedPrefStore( |
| 304 unprotected_pref_store, |
| 305 protected_pref_store, |
| 306 protected_pref_names, |
| 307 base::Bind(&PrefHashFilter::MigrateValues, |
| 308 base::Owned(new PrefHashFilter( |
| 309 CopyPrefHashStore(), |
| 310 protected_configuration, |
| 311 reporting_ids_count_)), |
| 312 unprotected_pref_store, |
| 313 protected_pref_store)); |
186 } | 314 } |
187 | 315 |
188 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 316 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
189 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 317 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
190 if (!kPlatformSupportsPreferenceTracking) | 318 if (!kPlatformSupportsPreferenceTracking) |
191 return; | 319 return; |
192 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 320 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
193 const PrefHashStoreImpl::StoreVersion current_version = | 321 const PrefHashStoreImpl::StoreVersion current_version = |
194 pref_hash_store_impl->GetCurrentVersion(); | 322 pref_hash_store_impl->GetCurrentVersion(); |
195 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 323 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
(...skipping 15 matching lines...) Expand all Loading... |
211 } | 339 } |
212 } | 340 } |
213 | 341 |
214 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 342 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
215 const base::DictionaryValue& master_prefs) { | 343 const base::DictionaryValue& master_prefs) { |
216 // Create the profile directory if it doesn't exist yet (very possible on | 344 // Create the profile directory if it doesn't exist yet (very possible on |
217 // first run). | 345 // first run). |
218 if (!base::CreateDirectory(profile_path_)) | 346 if (!base::CreateDirectory(profile_path_)) |
219 return false; | 347 return false; |
220 | 348 |
| 349 // This will write out to a single combined file which will be immediately |
| 350 // migrated to two files on load. |
221 JSONFileValueSerializer serializer( | 351 JSONFileValueSerializer serializer( |
222 GetPrefFilePathFromProfilePath(profile_path_)); | 352 GetPrefFilePathFromProfilePath(profile_path_)); |
223 | 353 |
224 // Call Serialize (which does IO) on the main thread, which would _normally_ | 354 // 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 | 355 // be verboten. In this case however, we require this IO to synchronously |
226 // complete before Chrome can start (as master preferences seed the Local | 356 // complete before Chrome can start (as master preferences seed the Local |
227 // State and Preferences files). This won't trip ThreadIORestrictions as they | 357 // State and Preferences files). This won't trip ThreadIORestrictions as they |
228 // won't have kicked in yet on the main thread. | 358 // won't have kicked in yet on the main thread. |
229 bool success = serializer.Serialize(master_prefs); | 359 bool success = serializer.Serialize(master_prefs); |
230 | 360 |
231 if (success && kPlatformSupportsPreferenceTracking) { | 361 if (success && kPlatformSupportsPreferenceTracking) { |
232 scoped_refptr<const PrefStore> pref_store( | 362 scoped_refptr<const PrefStore> pref_store( |
233 new DictionaryPrefStore(&master_prefs)); | 363 new DictionaryPrefStore(&master_prefs)); |
234 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 364 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
235 tracking_configuration_, | 365 tracking_configuration_, |
236 reporting_ids_count_).Initialize(*pref_store); | 366 reporting_ids_count_).Initialize(*pref_store); |
237 } | 367 } |
238 | 368 |
239 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 369 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
240 return success; | 370 return success; |
241 } | 371 } |
242 | 372 |
| 373 PersistentPrefStore* |
| 374 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( |
| 375 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 376 scoped_ptr<PrefFilter> pref_filter; |
| 377 if (kPlatformSupportsPreferenceTracking) { |
| 378 pref_filter.reset( |
| 379 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
| 380 tracking_configuration_, |
| 381 reporting_ids_count_)); |
| 382 } |
| 383 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
| 384 io_task_runner, |
| 385 pref_filter.Pass()); |
| 386 } |
| 387 |
243 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 388 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
244 DCHECK(kPlatformSupportsPreferenceTracking); | 389 DCHECK(kPlatformSupportsPreferenceTracking); |
245 | 390 |
246 return make_scoped_ptr(new PrefHashStoreImpl( | 391 return make_scoped_ptr(new PrefHashStoreImpl( |
247 seed_, | 392 seed_, |
248 device_id_, | 393 device_id_, |
249 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 394 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
250 profile_path_.AsUTF8Unsafe(), local_state_)))); | 395 profile_path_.AsUTF8Unsafe(), local_state_)))); |
251 } | 396 } |
| 397 |
| 398 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { |
| 399 DCHECK(kPlatformSupportsPreferenceTracking); |
| 400 |
| 401 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), |
| 402 local_state_); |
| 403 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( |
| 404 seed_, |
| 405 device_id_, |
| 406 scoped_ptr<HashStoreContents>( |
| 407 new DictionaryHashStoreContents(real_contents)))); |
| 408 } |
OLD | NEW |