| Index: chrome/browser/prefs/pref_hash_store_impl.cc
|
| diff --git a/chrome/browser/prefs/pref_hash_store_impl.cc b/chrome/browser/prefs/pref_hash_store_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2f9a0d20f159ce45e1d918493cc195a2b96c7501
|
| --- /dev/null
|
| +++ b/chrome/browser/prefs/pref_hash_store_impl.cc
|
| @@ -0,0 +1,85 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/prefs/pref_hash_store_impl.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/prefs/pref_registry_simple.h"
|
| +#include "base/prefs/pref_service.h"
|
| +#include "base/prefs/scoped_user_pref_update.h"
|
| +#include "base/values.h"
|
| +#include "chrome/common/pref_names.h"
|
| +
|
| +PrefHashStoreImpl::PrefHashStoreImpl(const PrefHashCalculator& hash_calculator,
|
| + PrefService* local_state,
|
| + const std::string& hash_store_id)
|
| + : local_state_(local_state),
|
| + hash_store_id_(hash_store_id),
|
| + pref_hash_calculator_(hash_calculator) {}
|
| +
|
| +// static
|
| +void PrefHashStoreImpl::RegisterPrefs(PrefRegistrySimple* registry) {
|
| + // Register the top level dictionary to map profile names to dictionaries of
|
| + // tracked preferences.
|
| + registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes);
|
| +}
|
| +
|
| +PrefHashStore::InitializationResult PrefHashStoreImpl::InitializeTrackedValue(
|
| + const std::string& path, const base::Value* initial_value) {
|
| + tracked_paths_.insert(path);
|
| +
|
| + const base::DictionaryValue* pref_hash_dicts =
|
| + local_state_->GetDictionary(prefs::kProfilePreferenceHashes);
|
| + const base::DictionaryValue* hashed_prefs = NULL;
|
| + pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_,
|
| + &hashed_prefs);
|
| +
|
| + std::string last_hash;
|
| + // First try to get the stored expected hash...
|
| + if (hashed_prefs && hashed_prefs->GetString(path, &last_hash)) {
|
| + PrefHashCalculator::ValidationResult validation_result =
|
| + pref_hash_calculator_.Validate(path, initial_value, last_hash);
|
| + if (validation_result == PrefHashCalculator::VALID)
|
| + return PrefHashStore::UNCHANGED;
|
| +
|
| + // No matter what the reason for the mismatch, we will correct it now.
|
| + OnPrefValueChanged(path, initial_value);
|
| +
|
| + switch (validation_result) {
|
| + case PrefHashCalculator::VALID_LEGACY:
|
| + return PrefHashStore::MIGRATED;
|
| + case PrefHashCalculator::INVALID:
|
| + return initial_value ? PrefHashStore::CHANGED : PrefHashStore::CLEARED;
|
| + default:
|
| + NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
|
| + << validation_result;
|
| + return PrefHashStore::INITIALIZED;
|
| + }
|
| + }
|
| +
|
| + // We haven't tracked this preference yet, or the hash in local state was
|
| + // removed.
|
| + OnPrefValueChanged(path, initial_value);
|
| + return PrefHashStore::INITIALIZED;
|
| +}
|
| +
|
| +void PrefHashStoreImpl::OnPrefValueChanged(const std::string& path,
|
| + const base::Value* new_value) {
|
| + if (tracked_paths_.find(path) == tracked_paths_.end())
|
| + return;
|
| +
|
| + DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes);
|
| + DictionaryValue* child_dictionary = NULL;
|
| +
|
| + // Get the dictionary corresponding to the profile name,
|
| + // which may have a '.'
|
| + if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_,
|
| + &child_dictionary)) {
|
| + child_dictionary = new DictionaryValue;
|
| + update->SetWithoutPathExpansion(hash_store_id_, child_dictionary);
|
| + }
|
| +
|
| + child_dictionary->SetString(
|
| + path, pref_hash_calculator_.Calculate(path, new_value));
|
| +}
|
|
|