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 // TODO(gab): This method should be a transaction on the underlying dictionary | |
133 // to avoid the case where a scheduled write kicks in on the blocking pool | |
134 // right after ClearPath() and chrome subsequently crashes; resulting in no | |
135 // hashes being stored on disk in the next startup. http://crbug.com/335149 | |
Bernhard Bauer
2014/01/17 17:00:05
This bug is WontFix :)
gab
2014/01/17 19:56:49
WooT :)!
| |
136 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); | |
137 ClearPath(path, &update); | |
138 | |
139 if (split_value) { | |
140 std::string keyed_path(path); | |
141 keyed_path.push_back('.'); | |
142 const size_t common_part_length = keyed_path.length(); | |
143 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | |
144 it.Advance()) { | |
145 // Keep the common part from the old |keyed_path| and replace the key to | |
146 // get the new |keyed_path|. | |
147 keyed_path.replace(common_part_length, std::string::npos, it.key()); | |
148 StoreHashInternal(keyed_path, &it.value(), &update); | |
149 } | |
150 } | |
151 } | |
152 | |
153 void PrefHashStoreImpl::ClearPath(const std::string& path, | |
154 DictionaryPrefUpdate* update) { | |
155 base::DictionaryValue* hashes_dict = NULL; | |
156 if (update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
157 &hashes_dict)) { | |
158 hashes_dict->Remove(path, NULL); | |
159 } | |
160 UpdateHashOfHashes(hashes_dict, update); | |
161 } | |
162 | |
163 bool PrefHashStoreImpl::HasSplitHashesAtPath(const std::string& path) const { | |
164 const base::DictionaryValue* pref_hash_dicts = | |
165 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | |
166 const base::DictionaryValue* hashed_prefs = NULL; | |
167 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
168 &hashed_prefs); | |
169 return hashed_prefs && hashed_prefs->GetDictionary(path, NULL); | |
170 } | |
171 | |
172 void PrefHashStoreImpl::StoreHashInternal(const std::string& path, | |
173 const base::Value* new_value, | |
174 DictionaryPrefUpdate* update) { | |
175 base::DictionaryValue* hashes_dict = NULL; | |
176 | |
177 // Get the dictionary corresponding to the profile name, which may have a '.' | |
178 if (!update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
179 &hashes_dict)) { | |
180 hashes_dict = new base::DictionaryValue; | |
181 update->Get()->SetWithoutPathExpansion(hash_store_id_, hashes_dict); | |
182 } | |
78 hashes_dict->SetString( | 183 hashes_dict->SetString( |
79 path, pref_hash_calculator_.Calculate(path, new_value)); | 184 path, pref_hash_calculator_.Calculate(path, new_value)); |
80 | 185 |
186 UpdateHashOfHashes(hashes_dict, update); | |
187 } | |
188 | |
189 void PrefHashStoreImpl::UpdateHashOfHashes( | |
190 const base::DictionaryValue* hashes_dict, | |
191 DictionaryPrefUpdate* update) { | |
192 | |
81 // Get the dictionary where the hash of hashes are stored. | 193 // Get the dictionary where the hash of hashes are stored. |
82 base::DictionaryValue* hash_of_hashes_dict = NULL; | 194 base::DictionaryValue* hash_of_hashes_dict = NULL; |
83 if (!update->GetDictionaryWithoutPathExpansion(internals::kHashOfHashesPref, | 195 if (!update->Get()->GetDictionaryWithoutPathExpansion( |
84 &hash_of_hashes_dict)) { | 196 internals::kHashOfHashesPref, &hash_of_hashes_dict)) { |
85 hash_of_hashes_dict = new base::DictionaryValue; | 197 hash_of_hashes_dict = new base::DictionaryValue; |
86 update->SetWithoutPathExpansion(internals::kHashOfHashesPref, | 198 update->Get()->SetWithoutPathExpansion(internals::kHashOfHashesPref, |
87 hash_of_hashes_dict); | 199 hash_of_hashes_dict); |
88 } | 200 } |
89 // Use the |hash_store_id_| as the hashed path to avoid having the hash | 201 // Use the |hash_store_id_| as the hashed path to avoid having the hash |
90 // depend on kProfilePreferenceHashes. | 202 // depend on kProfilePreferenceHashes. |
91 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, | 203 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, |
92 hashes_dict)); | 204 hashes_dict)); |
93 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, | 205 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, |
94 hash_of_hashes); | 206 hash_of_hashes); |
95 } | 207 } |
96 | 208 |
97 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { | 209 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { |
98 const base::DictionaryValue* pref_hash_dicts = | 210 const base::DictionaryValue* pref_hash_dicts = |
99 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | 211 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
100 const base::DictionaryValue* hashes_dict = NULL; | 212 const base::DictionaryValue* hashes_dict = NULL; |
101 const base::DictionaryValue* hash_of_hashes_dict = NULL; | 213 const base::DictionaryValue* hash_of_hashes_dict = NULL; |
102 std::string hash_of_hashes; | 214 std::string hash_of_hashes; |
103 // The absence of the hashes dictionary isn't trusted. Nor is the absence of | 215 // The absence of the hashes dictionary isn't trusted. Nor is the absence of |
104 // the hash of hashes for this |hash_store_id_|. | 216 // the hash of hashes for this |hash_store_id_|. |
105 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 217 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
106 hash_store_id_, &hashes_dict) || | 218 hash_store_id_, &hashes_dict) || |
107 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 219 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
108 internals::kHashOfHashesPref, &hash_of_hashes_dict) || | 220 internals::kHashOfHashesPref, &hash_of_hashes_dict) || |
109 !hash_of_hashes_dict->GetStringWithoutPathExpansion( | 221 !hash_of_hashes_dict->GetStringWithoutPathExpansion( |
110 hash_store_id_, &hash_of_hashes)) { | 222 hash_store_id_, &hash_of_hashes)) { |
111 return false; | 223 return false; |
112 } | 224 } |
113 | 225 |
114 return pref_hash_calculator_.Validate( | 226 return pref_hash_calculator_.Validate( |
115 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; | 227 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; |
116 } | 228 } |
OLD | NEW |