OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/pref_hash_store_impl.h" | 5 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | |
12 #include "base/values.h" | 11 #include "base/values.h" |
13 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
14 | 13 |
15 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& hash_store_id, | 14 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& hash_store_id, |
16 const std::string& seed, | 15 const std::string& seed, |
17 const std::string& device_id, | 16 const std::string& device_id, |
18 PrefService* local_state) | 17 PrefService* local_state) |
19 : hash_store_id_(hash_store_id), | 18 : hash_store_id_(hash_store_id), |
20 pref_hash_calculator_(seed, device_id), | 19 pref_hash_calculator_(seed, device_id), |
21 local_state_(local_state), | 20 local_state_(local_state), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return initial_value ? CHANGED : CLEARED; | 57 return initial_value ? CHANGED : CLEARED; |
59 } | 58 } |
60 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | 59 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
61 << validation_result; | 60 << validation_result; |
62 return UNTRUSTED_UNKNOWN_VALUE; | 61 return UNTRUSTED_UNKNOWN_VALUE; |
63 } | 62 } |
64 | 63 |
65 void PrefHashStoreImpl::StoreHash( | 64 void PrefHashStoreImpl::StoreHash( |
66 const std::string& path, const base::Value* new_value) { | 65 const std::string& path, const base::Value* new_value) { |
67 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); | 66 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
| 67 StoreHashInternal(path, new_value, &update); |
| 68 } |
68 | 69 |
69 // Get the dictionary corresponding to the profile name, which may have a | 70 PrefHashStore::ValueState PrefHashStoreImpl::CheckSplitValue( |
70 // '.' | 71 const std::string& path, |
71 base::DictionaryValue* hashes_dict = NULL; | 72 const base::DictionaryValue* initial_split_value, |
72 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_, | 73 std::vector<std::string>* invalid_keys) const { |
73 &hashes_dict)) { | 74 DCHECK(invalid_keys && invalid_keys->empty()); |
74 hashes_dict = new base::DictionaryValue; | 75 |
75 update->SetWithoutPathExpansion(hash_store_id_, hashes_dict); | 76 const bool has_hashes = HasSplitHashesAtPath(path); |
| 77 |
| 78 // Treat NULL and empty the same; otherwise we would need to store a hash |
| 79 // for the entire dictionary (or some other special beacon) to |
| 80 // differentiate these two cases which are really the same for |
| 81 // dictionaries. |
| 82 if (!initial_split_value || initial_split_value->empty()) |
| 83 return has_hashes ? CLEARED : UNCHANGED; |
| 84 |
| 85 if (!has_hashes) { |
| 86 return initial_hashes_dictionary_trusted_ ? |
| 87 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; |
76 } | 88 } |
77 | 89 |
| 90 std::string keyed_path(path); |
| 91 keyed_path.push_back('.'); |
| 92 const size_t common_part_length = keyed_path.length(); |
| 93 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); |
| 94 it.Advance()) { |
| 95 // Keep the common part from the old |keyed_path| and replace the key to |
| 96 // get the new |keyed_path|. |
| 97 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
| 98 ValueState value_state = CheckValue(keyed_path, &it.value()); |
| 99 switch (value_state) { |
| 100 case CLEARED: |
| 101 // CLEARED doesn't make sense as a NULL value would never be sampled |
| 102 // by the DictionaryValue::Iterator; in fact it is a known weakness of |
| 103 // this current algorithm to not detect the case where a single key is |
| 104 // cleared entirely from the dictionary pref. |
| 105 NOTREACHED(); |
| 106 break; |
| 107 case MIGRATED: |
| 108 // Split tracked preferences were introduced after the migration started |
| 109 // so no migration is expected, but declare it invalid in Release builds |
| 110 // anyways. |
| 111 NOTREACHED(); |
| 112 invalid_keys->push_back(it.key()); |
| 113 break; |
| 114 case UNCHANGED: |
| 115 break; |
| 116 case CHANGED: // Falls through. |
| 117 case UNTRUSTED_UNKNOWN_VALUE: // Falls through. |
| 118 case TRUSTED_UNKNOWN_VALUE: |
| 119 // Declare this value invalid, whether it was changed or never seen |
| 120 // before (note that the initial hashes being trusted is irrelevant for |
| 121 // individual entries in this scenario). |
| 122 invalid_keys->push_back(it.key()); |
| 123 break; |
| 124 } |
| 125 } |
| 126 return invalid_keys->empty() ? UNCHANGED : CHANGED; |
| 127 } |
| 128 |
| 129 void PrefHashStoreImpl::StoreSplitHash( |
| 130 const std::string& path, |
| 131 const base::DictionaryValue* split_value) { |
| 132 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); |
| 133 ClearPath(path, &update); |
| 134 |
| 135 if (split_value) { |
| 136 std::string keyed_path(path); |
| 137 keyed_path.push_back('.'); |
| 138 const size_t common_part_length = keyed_path.length(); |
| 139 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); |
| 140 it.Advance()) { |
| 141 // Keep the common part from the old |keyed_path| and replace the key to |
| 142 // get the new |keyed_path|. |
| 143 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
| 144 StoreHashInternal(keyed_path, &it.value(), &update); |
| 145 } |
| 146 } |
| 147 } |
| 148 |
| 149 void PrefHashStoreImpl::ClearPath(const std::string& path, |
| 150 DictionaryPrefUpdate* update) { |
| 151 base::DictionaryValue* hashes_dict = NULL; |
| 152 if (update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 153 &hashes_dict)) { |
| 154 hashes_dict->Remove(path, NULL); |
| 155 } |
| 156 UpdateHashOfHashes(hashes_dict, update); |
| 157 } |
| 158 |
| 159 bool PrefHashStoreImpl::HasSplitHashesAtPath(const std::string& path) const { |
| 160 const base::DictionaryValue* pref_hash_dicts = |
| 161 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
| 162 const base::DictionaryValue* hashed_prefs = NULL; |
| 163 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 164 &hashed_prefs); |
| 165 return hashed_prefs && hashed_prefs->GetDictionary(path, NULL); |
| 166 } |
| 167 |
| 168 void PrefHashStoreImpl::StoreHashInternal(const std::string& path, |
| 169 const base::Value* new_value, |
| 170 DictionaryPrefUpdate* update) { |
| 171 base::DictionaryValue* hashes_dict = NULL; |
| 172 |
| 173 // Get the dictionary corresponding to the profile name, which may have a '.' |
| 174 if (!update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 175 &hashes_dict)) { |
| 176 hashes_dict = new base::DictionaryValue; |
| 177 update->Get()->SetWithoutPathExpansion(hash_store_id_, hashes_dict); |
| 178 } |
78 hashes_dict->SetString( | 179 hashes_dict->SetString( |
79 path, pref_hash_calculator_.Calculate(path, new_value)); | 180 path, pref_hash_calculator_.Calculate(path, new_value)); |
80 | 181 |
| 182 UpdateHashOfHashes(hashes_dict, update); |
| 183 } |
| 184 |
| 185 void PrefHashStoreImpl::UpdateHashOfHashes( |
| 186 const base::DictionaryValue* hashes_dict, |
| 187 DictionaryPrefUpdate* update) { |
| 188 |
81 // Get the dictionary where the hash of hashes are stored. | 189 // Get the dictionary where the hash of hashes are stored. |
82 base::DictionaryValue* hash_of_hashes_dict = NULL; | 190 base::DictionaryValue* hash_of_hashes_dict = NULL; |
83 if (!update->GetDictionaryWithoutPathExpansion(internals::kHashOfHashesPref, | 191 if (!update->Get()->GetDictionaryWithoutPathExpansion( |
84 &hash_of_hashes_dict)) { | 192 internals::kHashOfHashesPref, &hash_of_hashes_dict)) { |
85 hash_of_hashes_dict = new base::DictionaryValue; | 193 hash_of_hashes_dict = new base::DictionaryValue; |
86 update->SetWithoutPathExpansion(internals::kHashOfHashesPref, | 194 update->Get()->SetWithoutPathExpansion(internals::kHashOfHashesPref, |
87 hash_of_hashes_dict); | 195 hash_of_hashes_dict); |
88 } | 196 } |
89 // Use the |hash_store_id_| as the hashed path to avoid having the hash | 197 // Use the |hash_store_id_| as the hashed path to avoid having the hash |
90 // depend on kProfilePreferenceHashes. | 198 // depend on kProfilePreferenceHashes. |
91 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, | 199 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, |
92 hashes_dict)); | 200 hashes_dict)); |
93 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, | 201 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, |
94 hash_of_hashes); | 202 hash_of_hashes); |
95 } | 203 } |
96 | 204 |
97 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { | 205 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { |
98 const base::DictionaryValue* pref_hash_dicts = | 206 const base::DictionaryValue* pref_hash_dicts = |
99 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | 207 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
100 const base::DictionaryValue* hashes_dict = NULL; | 208 const base::DictionaryValue* hashes_dict = NULL; |
101 const base::DictionaryValue* hash_of_hashes_dict = NULL; | 209 const base::DictionaryValue* hash_of_hashes_dict = NULL; |
102 std::string hash_of_hashes; | 210 std::string hash_of_hashes; |
103 // The absence of the hashes dictionary isn't trusted. Nor is the absence of | 211 // The absence of the hashes dictionary isn't trusted. Nor is the absence of |
104 // the hash of hashes for this |hash_store_id_|. | 212 // the hash of hashes for this |hash_store_id_|. |
105 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 213 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
106 hash_store_id_, &hashes_dict) || | 214 hash_store_id_, &hashes_dict) || |
107 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 215 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
108 internals::kHashOfHashesPref, &hash_of_hashes_dict) || | 216 internals::kHashOfHashesPref, &hash_of_hashes_dict) || |
109 !hash_of_hashes_dict->GetStringWithoutPathExpansion( | 217 !hash_of_hashes_dict->GetStringWithoutPathExpansion( |
110 hash_store_id_, &hash_of_hashes)) { | 218 hash_store_id_, &hash_of_hashes)) { |
111 return false; | 219 return false; |
112 } | 220 } |
113 | 221 |
114 return pref_hash_calculator_.Validate( | 222 return pref_hash_calculator_.Validate( |
115 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; | 223 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; |
116 } | 224 } |
OLD | NEW |