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