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 "components/user_prefs/tracked/pref_hash_store_impl.h" | 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/values.h" | |
14 #include "components/user_prefs/tracked/hash_store_contents.h" | 13 #include "components/user_prefs/tracked/hash_store_contents.h" |
15 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | |
16 | 14 |
17 class PrefHashStoreImpl::PrefHashStoreTransactionImpl | 15 class PrefHashStoreImpl::PrefHashStoreTransactionImpl |
18 : public PrefHashStoreTransaction { | 16 : public PrefHashStoreTransaction { |
19 public: | 17 public: |
20 // Constructs a PrefHashStoreTransactionImpl which can use the private | 18 // Constructs a PrefHashStoreTransactionImpl which can use the private |
21 // members of its |outer| PrefHashStoreImpl. | 19 // members of its |outer| PrefHashStoreImpl. |
22 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, | 20 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, |
23 HashStoreContents* storage); | 21 HashStoreContents* storage); |
24 ~PrefHashStoreTransactionImpl() override; | 22 ~PrefHashStoreTransactionImpl() override; |
25 | 23 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 55 |
58 PrefHashStoreImpl::~PrefHashStoreImpl() { | 56 PrefHashStoreImpl::~PrefHashStoreImpl() { |
59 } | 57 } |
60 | 58 |
61 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( | 59 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( |
62 HashStoreContents* storage) { | 60 HashStoreContents* storage) { |
63 return std::unique_ptr<PrefHashStoreTransaction>( | 61 return std::unique_ptr<PrefHashStoreTransaction>( |
64 new PrefHashStoreTransactionImpl(this, std::move(storage))); | 62 new PrefHashStoreTransactionImpl(this, std::move(storage))); |
65 } | 63 } |
66 | 64 |
| 65 std::string PrefHashStoreImpl::ComputeMac(const std::string& path, |
| 66 const base::Value* value) { |
| 67 return pref_hash_calculator_.Calculate(path, value); |
| 68 } |
| 69 |
| 70 std::unique_ptr<base::DictionaryValue> PrefHashStoreImpl::ComputeSplitMacs( |
| 71 const std::string& path, |
| 72 const base::DictionaryValue* split_values) { |
| 73 DCHECK(split_values); |
| 74 |
| 75 std::string keyed_path(path); |
| 76 keyed_path.push_back('.'); |
| 77 const size_t common_part_length = keyed_path.length(); |
| 78 |
| 79 std::unique_ptr<base::DictionaryValue> split_macs(new base::DictionaryValue); |
| 80 |
| 81 for (base::DictionaryValue::Iterator it(*split_values); !it.IsAtEnd(); |
| 82 it.Advance()) { |
| 83 // Keep the common part from the old |keyed_path| and replace the key to |
| 84 // get the new |keyed_path|. |
| 85 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
| 86 |
| 87 split_macs->SetStringWithoutPathExpansion( |
| 88 it.key(), ComputeMac(keyed_path, &it.value())); |
| 89 } |
| 90 |
| 91 return split_macs; |
| 92 } |
| 93 |
67 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | 94 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( |
68 PrefHashStoreImpl* outer, | 95 PrefHashStoreImpl* outer, |
69 HashStoreContents* storage) | 96 HashStoreContents* storage) |
70 : outer_(outer), | 97 : outer_(outer), |
71 contents_(std::move(storage)), | 98 contents_(std::move(storage)), |
72 super_mac_valid_(false), | 99 super_mac_valid_(false), |
73 super_mac_dirty_(false) { | 100 super_mac_dirty_(false) { |
74 if (!outer_->use_super_mac_) | 101 if (!outer_->use_super_mac_) |
75 return; | 102 return; |
76 | 103 |
77 // The store must have a valid super MAC to be trusted. | 104 // The store must have a valid super MAC to be trusted. |
78 std::string super_mac = contents_->GetSuperMac(); | 105 std::string super_mac = contents_->GetSuperMac(); |
79 if (super_mac.empty()) | 106 if (super_mac.empty()) |
80 return; | 107 return; |
81 | 108 |
82 super_mac_valid_ = | 109 super_mac_valid_ = |
83 outer_->pref_hash_calculator_.Validate( | 110 outer_->pref_hash_calculator_.Validate( |
84 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; | 111 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; |
85 } | 112 } |
86 | 113 |
87 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 114 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
88 ~PrefHashStoreTransactionImpl() { | 115 ~PrefHashStoreTransactionImpl() { |
89 if (super_mac_dirty_ && outer_->use_super_mac_) { | 116 if (super_mac_dirty_ && outer_->use_super_mac_) { |
90 // Get the dictionary of hashes (or NULL if it doesn't exist). | 117 // Get the dictionary of hashes (or NULL if it doesn't exist). |
91 const base::DictionaryValue* hashes_dict = contents_->GetContents(); | 118 const base::DictionaryValue* hashes_dict = contents_->GetContents(); |
92 contents_->SetSuperMac( | 119 contents_->SetSuperMac(outer_->ComputeMac("", hashes_dict)); |
93 outer_->pref_hash_calculator_.Calculate("", hashes_dict)); | |
94 } | 120 } |
95 } | 121 } |
96 | 122 |
97 PrefHashStoreTransaction::ValueState | 123 PrefHashStoreTransaction::ValueState |
98 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | 124 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( |
99 const std::string& path, | 125 const std::string& path, |
100 const base::Value* initial_value) const { | 126 const base::Value* initial_value) const { |
101 std::string last_hash; | 127 std::string last_hash; |
102 contents_->GetMac(path, &last_hash); | 128 contents_->GetMac(path, &last_hash); |
103 | 129 |
(...skipping 19 matching lines...) Expand all Loading... |
123 return initial_value ? CHANGED : CLEARED; | 149 return initial_value ? CHANGED : CLEARED; |
124 } | 150 } |
125 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | 151 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
126 << validation_result; | 152 << validation_result; |
127 return UNTRUSTED_UNKNOWN_VALUE; | 153 return UNTRUSTED_UNKNOWN_VALUE; |
128 } | 154 } |
129 | 155 |
130 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( | 156 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( |
131 const std::string& path, | 157 const std::string& path, |
132 const base::Value* new_value) { | 158 const base::Value* new_value) { |
133 const std::string mac = | 159 const std::string mac = outer_->ComputeMac(path, new_value); |
134 outer_->pref_hash_calculator_.Calculate(path, new_value); | |
135 contents_->SetMac(path, mac); | 160 contents_->SetMac(path, mac); |
136 super_mac_dirty_ = true; | 161 super_mac_dirty_ = true; |
137 } | 162 } |
138 | 163 |
139 PrefHashStoreTransaction::ValueState | 164 PrefHashStoreTransaction::ValueState |
140 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( | 165 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( |
141 const std::string& path, | 166 const std::string& path, |
142 const base::DictionaryValue* initial_split_value, | 167 const base::DictionaryValue* initial_split_value, |
143 std::vector<std::string>* invalid_keys) const { | 168 std::vector<std::string>* invalid_keys) const { |
144 DCHECK(invalid_keys && invalid_keys->empty()); | 169 DCHECK(invalid_keys && invalid_keys->empty()); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) | 225 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) |
201 : CHANGED; | 226 : CHANGED; |
202 } | 227 } |
203 | 228 |
204 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | 229 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( |
205 const std::string& path, | 230 const std::string& path, |
206 const base::DictionaryValue* split_value) { | 231 const base::DictionaryValue* split_value) { |
207 contents_->RemoveEntry(path); | 232 contents_->RemoveEntry(path); |
208 | 233 |
209 if (split_value) { | 234 if (split_value) { |
210 std::string keyed_path(path); | 235 std::unique_ptr<base::DictionaryValue> split_macs = |
211 keyed_path.push_back('.'); | 236 outer_->ComputeSplitMacs(path, split_value); |
212 const size_t common_part_length = keyed_path.length(); | 237 |
213 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | 238 for (base::DictionaryValue::Iterator it(*split_macs); !it.IsAtEnd(); |
214 it.Advance()) { | 239 it.Advance()) { |
215 // Keep the common part from the old |keyed_path| and replace the key to | 240 const base::StringValue* value_as_string; |
216 // get the new |keyed_path|. | 241 bool is_string = it.value().GetAsString(&value_as_string); |
217 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 242 DCHECK(is_string); |
218 contents_->SetSplitMac( | 243 |
219 path, it.key(), | 244 contents_->SetSplitMac(path, it.key(), value_as_string->GetString()); |
220 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | |
221 } | 245 } |
222 } | 246 } |
223 super_mac_dirty_ = true; | 247 super_mac_dirty_ = true; |
224 } | 248 } |
225 | 249 |
226 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( | 250 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( |
227 const std::string& path) const { | 251 const std::string& path) const { |
228 std::string out_value; | 252 std::string out_value; |
229 std::map<std::string, std::string> out_values; | 253 std::map<std::string, std::string> out_values; |
230 return contents_->GetMac(path, &out_value) || | 254 return contents_->GetMac(path, &out_value) || |
(...skipping 21 matching lines...) Expand all Loading... |
252 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | 276 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { |
253 return super_mac_valid_; | 277 return super_mac_valid_; |
254 } | 278 } |
255 | 279 |
256 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | 280 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { |
257 if (!outer_->use_super_mac_ || super_mac_valid_) | 281 if (!outer_->use_super_mac_ || super_mac_valid_) |
258 return false; | 282 return false; |
259 super_mac_dirty_ = true; | 283 super_mac_dirty_ = true; |
260 return true; | 284 return true; |
261 } | 285 } |
OLD | NEW |