OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/scoped_user_pref_update.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 |
| 14 PrefHashStoreImpl::PrefHashStoreImpl(const PrefHashCalculator& hash_calculator, |
| 15 PrefService* local_state, |
| 16 const std::string& hash_store_id) |
| 17 : local_state_(local_state), |
| 18 hash_store_id_(hash_store_id), |
| 19 pref_hash_calculator_(hash_calculator) {} |
| 20 |
| 21 // static |
| 22 void PrefHashStoreImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
| 23 // Register the top level dictionary to map profile names to dictionaries of |
| 24 // tracked preferences. |
| 25 registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes); |
| 26 } |
| 27 |
| 28 PrefHashStore::InitializationResult PrefHashStoreImpl::InitializeTrackedValue( |
| 29 const std::string& path, const base::Value* initial_value) { |
| 30 tracked_paths_.insert(path); |
| 31 |
| 32 const base::DictionaryValue* pref_hash_dicts = |
| 33 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
| 34 const base::DictionaryValue* hashed_prefs = NULL; |
| 35 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 36 &hashed_prefs); |
| 37 |
| 38 std::string last_hash; |
| 39 // First try to get the stored expected hash... |
| 40 if (hashed_prefs && hashed_prefs->GetString(path, &last_hash)) { |
| 41 PrefHashCalculator::ValidationResult validation_result = |
| 42 pref_hash_calculator_.Validate(path, initial_value, last_hash); |
| 43 if (validation_result == PrefHashCalculator::VALID) |
| 44 return PrefHashStore::UNCHANGED; |
| 45 |
| 46 // No matter what the reason for the mismatch, we will correct it now. |
| 47 OnPrefValueChanged(path, initial_value); |
| 48 |
| 49 switch (validation_result) { |
| 50 case PrefHashCalculator::VALID_LEGACY: |
| 51 return PrefHashStore::MIGRATED; |
| 52 case PrefHashCalculator::INVALID: |
| 53 return initial_value ? PrefHashStore::CHANGED : PrefHashStore::CLEARED; |
| 54 default: |
| 55 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
| 56 << validation_result; |
| 57 return PrefHashStore::INITIALIZED; |
| 58 } |
| 59 } |
| 60 |
| 61 // We haven't tracked this preference yet, or the hash in local state was |
| 62 // removed. |
| 63 OnPrefValueChanged(path, initial_value); |
| 64 return PrefHashStore::INITIALIZED; |
| 65 } |
| 66 |
| 67 void PrefHashStoreImpl::OnPrefValueChanged(const std::string& path, |
| 68 const base::Value* new_value) { |
| 69 if (tracked_paths_.find(path) == tracked_paths_.end()) |
| 70 return; |
| 71 |
| 72 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
| 73 DictionaryValue* child_dictionary = NULL; |
| 74 |
| 75 // Get the dictionary corresponding to the profile name, |
| 76 // which may have a '.' |
| 77 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 78 &child_dictionary)) { |
| 79 child_dictionary = new DictionaryValue; |
| 80 update->SetWithoutPathExpansion(hash_store_id_, child_dictionary); |
| 81 } |
| 82 |
| 83 child_dictionary->SetString( |
| 84 path, pref_hash_calculator_.Calculate(path, new_value)); |
| 85 } |
OLD | NEW |