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 std::string& hash_store_id, |
| 15 const std::string& seed, |
| 16 const std::string& device_id, |
| 17 PrefService* local_state) |
| 18 : hash_store_id_(hash_store_id), |
| 19 pref_hash_calculator_(seed, device_id), |
| 20 local_state_(local_state) {} |
| 21 |
| 22 // static |
| 23 void PrefHashStoreImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
| 24 // Register the top level dictionary to map profile names to dictionaries of |
| 25 // tracked preferences. |
| 26 registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes); |
| 27 } |
| 28 |
| 29 PrefHashStore::ValueState PrefHashStoreImpl::CheckValue( |
| 30 const std::string& path, const base::Value* initial_value) const { |
| 31 const base::DictionaryValue* pref_hash_dicts = |
| 32 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
| 33 const base::DictionaryValue* hashed_prefs = NULL; |
| 34 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 35 &hashed_prefs); |
| 36 |
| 37 std::string last_hash; |
| 38 if (!hashed_prefs || !hashed_prefs->GetString(path, &last_hash)) |
| 39 return PrefHashStore::UNKNOWN_VALUE; |
| 40 |
| 41 PrefHashCalculator::ValidationResult validation_result = |
| 42 pref_hash_calculator_.Validate(path, initial_value, last_hash); |
| 43 switch (validation_result) { |
| 44 case PrefHashCalculator::VALID: |
| 45 return PrefHashStore::UNCHANGED; |
| 46 case PrefHashCalculator::VALID_LEGACY: |
| 47 return PrefHashStore::MIGRATED; |
| 48 case PrefHashCalculator::INVALID: |
| 49 return initial_value ? PrefHashStore::CHANGED : PrefHashStore::CLEARED; |
| 50 } |
| 51 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
| 52 << validation_result; |
| 53 return PrefHashStore::UNKNOWN_VALUE; |
| 54 } |
| 55 |
| 56 void PrefHashStoreImpl::StoreHash( |
| 57 const std::string& path, const base::Value* new_value) { |
| 58 { |
| 59 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
| 60 DictionaryValue* child_dictionary = NULL; |
| 61 |
| 62 // Get the dictionary corresponding to the profile name, which may have a |
| 63 // '.' |
| 64 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 65 &child_dictionary)) { |
| 66 child_dictionary = new DictionaryValue; |
| 67 update->SetWithoutPathExpansion(hash_store_id_, child_dictionary); |
| 68 } |
| 69 |
| 70 child_dictionary->SetString( |
| 71 path, pref_hash_calculator_.Calculate(path, new_value)); |
| 72 } |
| 73 // TODO(erikwright): During tests, pending writes were still waiting when the |
| 74 // IO thread is already gone. Consider other solutions. |
| 75 local_state_->CommitPendingWrite(); |
| 76 } |
OLD | NEW |