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