Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(865)

Side by Side Diff: chrome/browser/prefs/pref_hash_store_impl.cc

Issue 90563003: Fix a race condition in preference metric reporting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nit. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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::StoreValue(
57 const std::string& path, const base::Value* new_value) {
58 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes);
59 DictionaryValue* child_dictionary = NULL;
60
61 // Get the dictionary corresponding to the profile name, which may have a '.'
62 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_,
63 &child_dictionary)) {
64 child_dictionary = new DictionaryValue;
65 update->SetWithoutPathExpansion(hash_store_id_, child_dictionary);
66 }
67
68 child_dictionary->SetString(
69 path, pref_hash_calculator_.Calculate(path, new_value));
70 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698