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" | |
8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
9 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
12 #include "base/prefs/json_pref_store.h" | 11 #include "base/prefs/json_pref_store.h" |
13 #include "base/prefs/persistent_pref_store.h" | 12 #include "base/prefs/persistent_pref_store.h" |
14 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
15 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 14 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
16 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" | 15 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" |
17 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" | 16 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" |
18 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" | |
19 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
21 #include "components/user_prefs/pref_registry_syncable.h" | 19 #include "components/user_prefs/pref_registry_syncable.h" |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 // An adaptor that allows a PrefHashStoreImpl to access a preference store | 23 // An adaptor that allows a PrefHashStoreImpl to access a preference store |
26 // directly as a dictionary. Uses an equivalent layout to | 24 // directly as a dictionary. Uses an equivalent layout to |
27 // PrefStoreHashStoreContents. | 25 // PrefStoreHashStoreContents. |
28 class DictionaryHashStoreContents : public HashStoreContents { | 26 class DictionaryHashStoreContents : public HashStoreContents { |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 return profile_path.Append(chrome::kPreferencesFilename); | 236 return profile_path.Append(chrome::kPreferencesFilename); |
239 } | 237 } |
240 | 238 |
241 // static | 239 // static |
242 void ProfilePrefStoreManager::ResetAllPrefHashStores(PrefService* local_state) { | 240 void ProfilePrefStoreManager::ResetAllPrefHashStores(PrefService* local_state) { |
243 PrefServiceHashStoreContents::ResetAllPrefHashStores(local_state); | 241 PrefServiceHashStoreContents::ResetAllPrefHashStores(local_state); |
244 } | 242 } |
245 | 243 |
246 // static | 244 // static |
247 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 245 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
| 246 // It's a bit of a coincidence that this (and ClearResetTime) work(s). The |
| 247 // PrefHashFilter attached to the protected pref store will store the reset |
| 248 // time directly in the protected pref store without going through the |
| 249 // SegregatedPrefStore. |
| 250 |
| 251 // PrefHashFilter::GetResetTime will read the value through the pref service, |
| 252 // and thus through the SegregatedPrefStore. Even though it's not listed as |
| 253 // "protected" it will be read from the protected store preferentially to the |
| 254 // (NULL) value in the unprotected pref store. |
248 return PrefHashFilter::GetResetTime(pref_service); | 255 return PrefHashFilter::GetResetTime(pref_service); |
249 } | 256 } |
250 | 257 |
251 // static | 258 // static |
252 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 259 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
| 260 // PrefHashFilter::ClearResetTime will clear the value through the pref |
| 261 // service, and thus through the SegregatedPrefStore. Since it's not listed as |
| 262 // "protected" it will be migrated from the protected store to the unprotected |
| 263 // pref store before being deleted from the latter. |
253 PrefHashFilter::ClearResetTime(pref_service); | 264 PrefHashFilter::ClearResetTime(pref_service); |
254 } | 265 } |
255 | 266 |
256 void ProfilePrefStoreManager::ResetPrefHashStore() { | 267 void ProfilePrefStoreManager::ResetPrefHashStore() { |
257 if (kPlatformSupportsPreferenceTracking) | 268 if (kPlatformSupportsPreferenceTracking) |
258 GetPrefHashStoreImpl()->Reset(); | 269 GetPrefHashStoreImpl()->Reset(); |
259 } | 270 } |
260 | 271 |
261 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 272 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
262 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 273 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
263 scoped_ptr<PrefFilter> pref_filter; | 274 scoped_ptr<PrefFilter> pref_filter; |
264 if (!kPlatformSupportsPreferenceTracking) { | 275 if (!kPlatformSupportsPreferenceTracking) { |
265 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 276 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
266 io_task_runner, | 277 io_task_runner, |
267 scoped_ptr<PrefFilter>()); | 278 scoped_ptr<PrefFilter>()); |
268 } | 279 } |
269 | 280 |
270 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 281 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
271 unprotected_configuration; | 282 unprotected_configuration; |
272 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 283 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
273 protected_configuration; | 284 protected_configuration; |
274 std::set<std::string> protected_pref_names; | 285 std::set<std::string> protected_pref_names; |
275 std::set<std::string> unprotected_pref_names; | |
276 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator | 286 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator |
277 it = tracking_configuration_.begin(); | 287 it = tracking_configuration_.begin(); |
278 it != tracking_configuration_.end(); | 288 it != tracking_configuration_.end(); |
279 ++it) { | 289 ++it) { |
280 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { | 290 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { |
281 protected_configuration.push_back(*it); | 291 protected_configuration.push_back(*it); |
282 protected_pref_names.insert(it->name); | 292 protected_pref_names.insert(it->name); |
283 } else { | 293 } else { |
284 unprotected_configuration.push_back(*it); | 294 unprotected_configuration.push_back(*it); |
285 unprotected_pref_names.insert(it->name); | |
286 } | 295 } |
287 } | 296 } |
288 | 297 |
289 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( | 298 scoped_ptr<PrefFilter> unprotected_pref_hash_filter( |
290 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 299 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
291 unprotected_configuration, | 300 unprotected_configuration, |
292 reporting_ids_count_)); | 301 reporting_ids_count_)); |
293 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( | 302 scoped_ptr<PrefFilter> protected_pref_hash_filter( |
294 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 303 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
295 protected_configuration, | 304 protected_configuration, |
296 reporting_ids_count_)); | 305 reporting_ids_count_)); |
297 | 306 |
298 PrefHashFilter* raw_unprotected_pref_hash_filter = | 307 scoped_refptr<PersistentPrefStore> unprotected_pref_store( |
299 unprotected_pref_hash_filter.get(); | |
300 PrefHashFilter* raw_protected_pref_hash_filter = | |
301 protected_pref_hash_filter.get(); | |
302 | |
303 scoped_refptr<JsonPrefStore> unprotected_pref_store( | |
304 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 308 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
305 io_task_runner, | 309 io_task_runner, |
306 unprotected_pref_hash_filter.PassAs<PrefFilter>())); | 310 unprotected_pref_hash_filter.Pass())); |
307 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore( | 311 scoped_refptr<PersistentPrefStore> protected_pref_store(new JsonPrefStore( |
308 profile_path_.Append(chrome::kProtectedPreferencesFilename), | 312 profile_path_.Append(chrome::kProtectedPreferencesFilename), |
309 io_task_runner, | 313 io_task_runner, |
310 protected_pref_hash_filter.PassAs<PrefFilter>())); | 314 protected_pref_hash_filter.Pass())); |
311 | 315 |
312 SetupTrackedPreferencesMigration( | 316 // The on_initialized callback is used to migrate newly protected values from |
313 unprotected_pref_names, | 317 // the main Preferences store to the Protected Preferences store. It is also |
| 318 // responsible for the initial migration to a two-store model. |
| 319 return new SegregatedPrefStore( |
| 320 unprotected_pref_store, |
| 321 protected_pref_store, |
314 protected_pref_names, | 322 protected_pref_names, |
315 base::Bind(&JsonPrefStore::RemoveValueSilently, | 323 base::Bind(&PrefHashFilter::MigrateValues, |
316 unprotected_pref_store->AsWeakPtr()), | 324 base::Owned(new PrefHashFilter( |
317 base::Bind(&JsonPrefStore::RemoveValueSilently, | 325 CopyPrefHashStore(), |
318 protected_pref_store->AsWeakPtr()), | 326 protected_configuration, |
319 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 327 reporting_ids_count_)), |
320 unprotected_pref_store->AsWeakPtr()), | 328 unprotected_pref_store, |
321 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 329 protected_pref_store)); |
322 protected_pref_store->AsWeakPtr()), | |
323 raw_unprotected_pref_hash_filter, | |
324 raw_protected_pref_hash_filter); | |
325 | |
326 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | |
327 protected_pref_names); | |
328 } | 330 } |
329 | 331 |
330 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( | 332 void ProfilePrefStoreManager::UpdateProfileHashStoreIfRequired( |
331 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 333 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
332 if (!kPlatformSupportsPreferenceTracking) | 334 if (!kPlatformSupportsPreferenceTracking) |
333 return; | 335 return; |
334 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); | 336 scoped_ptr<PrefHashStoreImpl> pref_hash_store_impl(GetPrefHashStoreImpl()); |
335 const PrefHashStoreImpl::StoreVersion current_version = | 337 const PrefHashStoreImpl::StoreVersion current_version = |
336 pref_hash_store_impl->GetCurrentVersion(); | 338 pref_hash_store_impl->GetCurrentVersion(); |
337 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", | 339 UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferencesAlternateStoreVersion", |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 403 |
402 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 404 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
403 DCHECK(kPlatformSupportsPreferenceTracking); | 405 DCHECK(kPlatformSupportsPreferenceTracking); |
404 | 406 |
405 return make_scoped_ptr(new PrefHashStoreImpl( | 407 return make_scoped_ptr(new PrefHashStoreImpl( |
406 seed_, | 408 seed_, |
407 device_id_, | 409 device_id_, |
408 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 410 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
409 profile_path_.AsUTF8Unsafe(), local_state_)))); | 411 profile_path_.AsUTF8Unsafe(), local_state_)))); |
410 } | 412 } |
| 413 |
| 414 scoped_ptr<PrefHashStore> ProfilePrefStoreManager::CopyPrefHashStore() { |
| 415 DCHECK(kPlatformSupportsPreferenceTracking); |
| 416 |
| 417 PrefServiceHashStoreContents real_contents(profile_path_.AsUTF8Unsafe(), |
| 418 local_state_); |
| 419 return scoped_ptr<PrefHashStore>(new PrefHashStoreImpl( |
| 420 seed_, |
| 421 device_id_, |
| 422 scoped_ptr<HashStoreContents>( |
| 423 new DictionaryHashStoreContents(real_contents)))); |
| 424 } |
OLD | NEW |