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/tracked/pref_hash_store_impl.h" | 5 #include "components/user_prefs/tracked/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/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/prefs/tracked/hash_store_contents.h" | 10 #include "components/user_prefs/tracked/hash_store_contents.h" |
11 #include "chrome/browser/prefs/tracked/pref_hash_store_transaction.h" | 11 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
12 | 12 |
13 class PrefHashStoreImpl::PrefHashStoreTransactionImpl | 13 class PrefHashStoreImpl::PrefHashStoreTransactionImpl |
14 : public PrefHashStoreTransaction { | 14 : public PrefHashStoreTransaction { |
15 public: | 15 public: |
16 // Constructs a PrefHashStoreTransactionImpl which can use the private | 16 // Constructs a PrefHashStoreTransactionImpl which can use the private |
17 // members of its |outer| PrefHashStoreImpl. | 17 // members of its |outer| PrefHashStoreImpl. |
18 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, | 18 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, |
19 scoped_ptr<HashStoreContents> storage); | 19 scoped_ptr<HashStoreContents> storage); |
20 ~PrefHashStoreTransactionImpl() override; | 20 ~PrefHashStoreTransactionImpl() override; |
21 | 21 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 bool super_mac_valid_; | 57 bool super_mac_valid_; |
58 bool super_mac_dirty_; | 58 bool super_mac_dirty_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); | 60 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); |
61 }; | 61 }; |
62 | 62 |
63 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, | 63 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, |
64 const std::string& device_id, | 64 const std::string& device_id, |
65 bool use_super_mac) | 65 bool use_super_mac) |
66 : pref_hash_calculator_(seed, device_id), | 66 : pref_hash_calculator_(seed, device_id), use_super_mac_(use_super_mac) { |
67 use_super_mac_(use_super_mac) { | |
68 } | 67 } |
69 | 68 |
70 PrefHashStoreImpl::~PrefHashStoreImpl() { | 69 PrefHashStoreImpl::~PrefHashStoreImpl() { |
71 } | 70 } |
72 | 71 |
73 void PrefHashStoreImpl::set_legacy_hash_store_contents( | 72 void PrefHashStoreImpl::set_legacy_hash_store_contents( |
74 scoped_ptr<HashStoreContents> legacy_hash_store_contents) { | 73 scoped_ptr<HashStoreContents> legacy_hash_store_contents) { |
75 legacy_hash_store_contents_ = legacy_hash_store_contents.Pass(); | 74 legacy_hash_store_contents_ = legacy_hash_store_contents.Pass(); |
76 } | 75 } |
77 | 76 |
(...skipping 16 matching lines...) Expand all Loading... |
94 // The store must be initialized and have a valid super MAC to be trusted. | 93 // The store must be initialized and have a valid super MAC to be trusted. |
95 | 94 |
96 const base::DictionaryValue* store_contents = contents()->GetContents(); | 95 const base::DictionaryValue* store_contents = contents()->GetContents(); |
97 if (!store_contents) | 96 if (!store_contents) |
98 return; | 97 return; |
99 | 98 |
100 std::string super_mac = contents()->GetSuperMac(); | 99 std::string super_mac = contents()->GetSuperMac(); |
101 if (super_mac.empty()) | 100 if (super_mac.empty()) |
102 return; | 101 return; |
103 | 102 |
104 super_mac_valid_ = | 103 super_mac_valid_ = outer_->pref_hash_calculator_.Validate( |
105 outer_->pref_hash_calculator_.Validate( | 104 contents()->hash_store_id(), store_contents, |
106 contents()->hash_store_id(), store_contents, super_mac) == | 105 super_mac) == PrefHashCalculator::VALID; |
107 PrefHashCalculator::VALID; | |
108 } | 106 } |
109 | 107 |
110 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 108 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
111 ~PrefHashStoreTransactionImpl() { | 109 ~PrefHashStoreTransactionImpl() { |
112 if (super_mac_dirty_ && outer_->use_super_mac_) { | 110 if (super_mac_dirty_ && outer_->use_super_mac_) { |
113 // Get the dictionary of hashes (or NULL if it doesn't exist). | 111 // Get the dictionary of hashes (or NULL if it doesn't exist). |
114 const base::DictionaryValue* hashes_dict = contents()->GetContents(); | 112 const base::DictionaryValue* hashes_dict = contents()->GetContents(); |
115 contents()->SetSuperMac(outer_->pref_hash_calculator_.Calculate( | 113 contents()->SetSuperMac(outer_->pref_hash_calculator_.Calculate( |
116 contents()->hash_store_id(), hashes_dict)); | 114 contents()->hash_store_id(), hashes_dict)); |
117 } | 115 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); | 186 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); |
189 it.Advance()) { | 187 it.Advance()) { |
190 std::map<std::string, std::string>::iterator entry = | 188 std::map<std::string, std::string>::iterator entry = |
191 split_macs.find(it.key()); | 189 split_macs.find(it.key()); |
192 if (entry == split_macs.end()) { | 190 if (entry == split_macs.end()) { |
193 invalid_keys->push_back(it.key()); | 191 invalid_keys->push_back(it.key()); |
194 } else { | 192 } else { |
195 // Keep the common part from the old |keyed_path| and replace the key to | 193 // Keep the common part from the old |keyed_path| and replace the key to |
196 // get the new |keyed_path|. | 194 // get the new |keyed_path|. |
197 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 195 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
198 switch (outer_->pref_hash_calculator_.Validate( | 196 switch (outer_->pref_hash_calculator_.Validate(keyed_path, &it.value(), |
199 keyed_path, &it.value(), entry->second)) { | 197 entry->second)) { |
200 case PrefHashCalculator::VALID: | 198 case PrefHashCalculator::VALID: |
201 break; | 199 break; |
202 case SECURE_LEGACY: | 200 case SECURE_LEGACY: |
203 // Secure legacy device IDs based hashes are still accepted, but we | 201 // Secure legacy device IDs based hashes are still accepted, but we |
204 // should make sure to notify the caller for him to update the legacy | 202 // should make sure to notify the caller for him to update the legacy |
205 // hashes. | 203 // hashes. |
206 has_secure_legacy_id_hashes = true; | 204 has_secure_legacy_id_hashes = true; |
207 break; | 205 break; |
208 case PrefHashCalculator::INVALID: | 206 case PrefHashCalculator::INVALID: |
209 invalid_keys->push_back(it.key()); | 207 invalid_keys->push_back(it.key()); |
210 break; | 208 break; |
211 } | 209 } |
212 // Remove processed MACs, remaining MACs at the end will also be | 210 // Remove processed MACs, remaining MACs at the end will also be |
213 // considered invalid. | 211 // considered invalid. |
214 split_macs.erase(entry); | 212 split_macs.erase(entry); |
215 } | 213 } |
216 } | 214 } |
217 | 215 |
218 // Anything left in the map is missing from the data. | 216 // Anything left in the map is missing from the data. |
219 for (std::map<std::string, std::string>::const_iterator it = | 217 for (std::map<std::string, std::string>::const_iterator it = |
220 split_macs.begin(); | 218 split_macs.begin(); |
221 it != split_macs.end(); | 219 it != split_macs.end(); ++it) { |
222 ++it) { | |
223 invalid_keys->push_back(it->first); | 220 invalid_keys->push_back(it->first); |
224 } | 221 } |
225 | 222 |
226 return invalid_keys->empty() | 223 return invalid_keys->empty() |
227 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) | 224 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) |
228 : CHANGED; | 225 : CHANGED; |
229 } | 226 } |
230 | 227 |
231 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | 228 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( |
232 const std::string& path, | 229 const std::string& path, |
233 const base::DictionaryValue* split_value) { | 230 const base::DictionaryValue* split_value) { |
234 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = | 231 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = |
235 contents()->GetMutableContents(); | 232 contents()->GetMutableContents(); |
236 (*mutable_dictionary)->Remove(path, NULL); | 233 (*mutable_dictionary)->Remove(path, NULL); |
237 | 234 |
238 if (split_value) { | 235 if (split_value) { |
239 std::string keyed_path(path); | 236 std::string keyed_path(path); |
240 keyed_path.push_back('.'); | 237 keyed_path.push_back('.'); |
241 const size_t common_part_length = keyed_path.length(); | 238 const size_t common_part_length = keyed_path.length(); |
242 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | 239 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); |
243 it.Advance()) { | 240 it.Advance()) { |
244 // Keep the common part from the old |keyed_path| and replace the key to | 241 // Keep the common part from the old |keyed_path| and replace the key to |
245 // get the new |keyed_path|. | 242 // get the new |keyed_path|. |
246 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 243 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
247 (*mutable_dictionary)->SetString( | 244 (*mutable_dictionary) |
248 keyed_path, | 245 ->SetString(keyed_path, outer_->pref_hash_calculator_.Calculate( |
249 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | 246 keyed_path, &it.value())); |
250 } | 247 } |
251 } | 248 } |
252 super_mac_dirty_ = true; | 249 super_mac_dirty_ = true; |
253 } | 250 } |
254 | 251 |
255 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( | 252 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( |
256 const std::string& key, | 253 const std::string& key, |
257 std::map<std::string, std::string>* split_macs) const { | 254 std::map<std::string, std::string>* split_macs) const { |
258 DCHECK(split_macs); | 255 DCHECK(split_macs); |
259 DCHECK(split_macs->empty()); | 256 DCHECK(split_macs->empty()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | 299 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { |
303 return super_mac_valid_; | 300 return super_mac_valid_; |
304 } | 301 } |
305 | 302 |
306 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | 303 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { |
307 if (!outer_->use_super_mac_ || super_mac_valid_) | 304 if (!outer_->use_super_mac_ || super_mac_valid_) |
308 return false; | 305 return false; |
309 super_mac_dirty_ = true; | 306 super_mac_dirty_ = true; |
310 return true; | 307 return true; |
311 } | 308 } |
OLD | NEW |