Chromium Code Reviews| 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/memory/ptr_util.h" | |
| 12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "components/user_prefs/tracked/hash_store_contents.h" | 15 #include "components/user_prefs/tracked/hash_store_contents.h" |
| 15 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 16 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
| 16 | 17 |
| 17 class PrefHashStoreImpl::PrefHashStoreTransactionImpl | 18 class PrefHashStoreImpl::PrefHashStoreTransactionImpl |
| 18 : public PrefHashStoreTransaction { | 19 : public PrefHashStoreTransaction { |
| 19 public: | 20 public: |
| 20 // Constructs a PrefHashStoreTransactionImpl which can use the private | 21 // Constructs a PrefHashStoreTransactionImpl which can use the private |
| 21 // members of its |outer| PrefHashStoreImpl. | 22 // members of its |outer| PrefHashStoreImpl. |
| 22 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, | 23 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, |
| 23 std::unique_ptr<HashStoreContents> storage); | 24 std::unique_ptr<HashStoreContents> storage); |
| 24 ~PrefHashStoreTransactionImpl() override; | 25 ~PrefHashStoreTransactionImpl() override; |
| 25 | 26 |
| 26 // PrefHashStoreTransaction implementation. | 27 // PrefHashStoreTransaction implementation. |
| 28 HashStoreContentsType GetStoreType() const override; | |
| 27 ValueState CheckValue(const std::string& path, | 29 ValueState CheckValue(const std::string& path, |
| 28 const base::Value* value) const override; | 30 const base::Value* value) const override; |
| 29 void StoreHash(const std::string& path, const base::Value* value) override; | 31 void StoreHash(const std::string& path, const base::Value* value) override; |
| 30 ValueState CheckSplitValue( | 32 ValueState CheckSplitValue( |
| 31 const std::string& path, | 33 const std::string& path, |
| 32 const base::DictionaryValue* initial_split_value, | 34 const base::DictionaryValue* initial_split_value, |
| 33 std::vector<std::string>* invalid_keys) const override; | 35 std::vector<std::string>* invalid_keys) const override; |
| 34 void StoreSplitHash(const std::string& path, | 36 void StoreSplitHash(const std::string& path, |
| 35 const base::DictionaryValue* split_value) override; | 37 const base::DictionaryValue* split_value) override; |
| 36 bool HasHash(const std::string& path) const override; | 38 bool HasHash(const std::string& path) const override; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 57 | 59 |
| 58 PrefHashStoreImpl::~PrefHashStoreImpl() { | 60 PrefHashStoreImpl::~PrefHashStoreImpl() { |
| 59 } | 61 } |
| 60 | 62 |
| 61 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( | 63 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( |
| 62 std::unique_ptr<HashStoreContents> storage) { | 64 std::unique_ptr<HashStoreContents> storage) { |
| 63 return std::unique_ptr<PrefHashStoreTransaction>( | 65 return std::unique_ptr<PrefHashStoreTransaction>( |
| 64 new PrefHashStoreTransactionImpl(this, std::move(storage))); | 66 new PrefHashStoreTransactionImpl(this, std::move(storage))); |
| 65 } | 67 } |
| 66 | 68 |
| 69 std::unique_ptr<base::DictionaryValue> PrefHashStoreImpl::ComputeSplitMacs( | |
| 70 const std::string& path, | |
| 71 const base::DictionaryValue* split_values) { | |
| 72 if (!split_values) | |
| 73 return nullptr; | |
| 74 | |
| 75 std::string keyed_path(path); | |
| 76 keyed_path.push_back('.'); | |
| 77 const size_t common_part_length = keyed_path.length(); | |
| 78 | |
| 79 base::DictionaryValue* split_macs = new base::DictionaryValue; | |
|
gab
2016/08/03 18:19:36
std::unique_ptr here instead of only wrapping it a
proberge
2016/08/04 00:13:46
Done.
| |
| 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->SetString(it.key(), ComputeMac(keyed_path, &it.value())); | |
|
gab
2016/08/03 18:19:35
SetStringWithoutPathExpansion
proberge
2016/08/04 00:13:46
Done.
| |
| 88 } | |
| 89 | |
| 90 return base::WrapUnique(split_macs); | |
| 91 } | |
| 92 | |
| 93 std::string PrefHashStoreImpl::ComputeMac(const std::string& path, | |
|
gab
2016/08/03 18:19:35
Same order as in header (this method before Comput
proberge
2016/08/04 00:13:46
Done.
| |
| 94 const base::Value* new_value) { | |
| 95 return pref_hash_calculator_.Calculate(path, new_value); | |
| 96 } | |
| 97 | |
| 67 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | 98 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( |
| 68 PrefHashStoreImpl* outer, | 99 PrefHashStoreImpl* outer, |
| 69 std::unique_ptr<HashStoreContents> storage) | 100 std::unique_ptr<HashStoreContents> storage) |
| 70 : outer_(outer), | 101 : outer_(outer), |
| 71 contents_(std::move(storage)), | 102 contents_(std::move(storage)), |
| 72 super_mac_valid_(false), | 103 super_mac_valid_(false), |
| 73 super_mac_dirty_(false) { | 104 super_mac_dirty_(false) { |
| 74 if (!outer_->use_super_mac_) | 105 if (!outer_->use_super_mac_) |
| 75 return; | 106 return; |
| 76 | 107 |
| 77 // The store must have a valid super MAC to be trusted. | 108 // The store must have a valid super MAC to be trusted. |
| 78 std::string super_mac = contents_->GetSuperMac(); | 109 std::string super_mac = contents_->GetSuperMac(); |
| 79 if (super_mac.empty()) | 110 if (super_mac.empty()) |
| 80 return; | 111 return; |
| 81 | 112 |
| 82 super_mac_valid_ = | 113 super_mac_valid_ = |
| 83 outer_->pref_hash_calculator_.Validate( | 114 outer_->pref_hash_calculator_.Validate( |
| 84 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; | 115 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; |
| 85 } | 116 } |
| 86 | 117 |
| 87 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 118 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
| 88 ~PrefHashStoreTransactionImpl() { | 119 ~PrefHashStoreTransactionImpl() { |
| 89 if (super_mac_dirty_ && outer_->use_super_mac_) { | 120 if (super_mac_dirty_ && outer_->use_super_mac_) { |
| 90 // Get the dictionary of hashes (or NULL if it doesn't exist). | 121 // Get the dictionary of hashes (or NULL if it doesn't exist). |
| 91 const base::DictionaryValue* hashes_dict = contents_->GetContents(); | 122 const base::DictionaryValue* hashes_dict = contents_->GetContents(); |
| 92 contents_->SetSuperMac( | 123 contents_->SetSuperMac(outer_->ComputeMac("", hashes_dict)); |
| 93 outer_->pref_hash_calculator_.Calculate("", hashes_dict)); | |
| 94 } | 124 } |
| 95 } | 125 } |
| 96 | 126 |
| 127 HashStoreContentsType | |
| 128 PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetStoreType() const { | |
| 129 return contents_->GetType(); | |
| 130 } | |
| 131 | |
| 97 PrefHashStoreTransaction::ValueState | 132 PrefHashStoreTransaction::ValueState |
| 98 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | 133 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( |
| 99 const std::string& path, | 134 const std::string& path, |
| 100 const base::Value* initial_value) const { | 135 const base::Value* initial_value) const { |
| 101 std::string last_hash; | 136 std::string last_hash; |
| 102 contents_->GetMac(path, &last_hash); | 137 contents_->GetMac(path, &last_hash); |
| 103 | 138 |
| 104 if (last_hash.empty()) { | 139 if (last_hash.empty()) { |
| 105 // In the absence of a hash for this pref, always trust a NULL value, but | 140 // In the absence of a hash for this pref, always trust a NULL value, but |
| 106 // only trust an existing value if the initial hashes dictionary is trusted. | 141 // only trust an existing value if the initial hashes dictionary is trusted. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 123 return initial_value ? CHANGED : CLEARED; | 158 return initial_value ? CHANGED : CLEARED; |
| 124 } | 159 } |
| 125 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | 160 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
| 126 << validation_result; | 161 << validation_result; |
| 127 return UNTRUSTED_UNKNOWN_VALUE; | 162 return UNTRUSTED_UNKNOWN_VALUE; |
| 128 } | 163 } |
| 129 | 164 |
| 130 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( | 165 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( |
| 131 const std::string& path, | 166 const std::string& path, |
| 132 const base::Value* new_value) { | 167 const base::Value* new_value) { |
| 133 const std::string mac = | 168 const std::string mac = outer_->ComputeMac(path, new_value); |
| 134 outer_->pref_hash_calculator_.Calculate(path, new_value); | |
| 135 contents_->SetMac(path, mac); | 169 contents_->SetMac(path, mac); |
| 136 super_mac_dirty_ = true; | 170 super_mac_dirty_ = true; |
| 137 } | 171 } |
| 138 | 172 |
| 139 PrefHashStoreTransaction::ValueState | 173 PrefHashStoreTransaction::ValueState |
| 140 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( | 174 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( |
| 141 const std::string& path, | 175 const std::string& path, |
| 142 const base::DictionaryValue* initial_split_value, | 176 const base::DictionaryValue* initial_split_value, |
| 143 std::vector<std::string>* invalid_keys) const { | 177 std::vector<std::string>* invalid_keys) const { |
| 144 DCHECK(invalid_keys && invalid_keys->empty()); | 178 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) | 234 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) |
| 201 : CHANGED; | 235 : CHANGED; |
| 202 } | 236 } |
| 203 | 237 |
| 204 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | 238 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( |
| 205 const std::string& path, | 239 const std::string& path, |
| 206 const base::DictionaryValue* split_value) { | 240 const base::DictionaryValue* split_value) { |
| 207 contents_->RemoveEntry(path); | 241 contents_->RemoveEntry(path); |
| 208 | 242 |
| 209 if (split_value) { | 243 if (split_value) { |
| 210 std::string keyed_path(path); | 244 std::unique_ptr<base::DictionaryValue> split_macs = |
| 211 keyed_path.push_back('.'); | 245 outer_->ComputeSplitMacs(path, split_value); |
| 212 const size_t common_part_length = keyed_path.length(); | 246 |
| 213 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | 247 for (base::DictionaryValue::Iterator it(*split_macs); !it.IsAtEnd(); |
| 214 it.Advance()) { | 248 it.Advance()) { |
| 215 // Keep the common part from the old |keyed_path| and replace the key to | 249 std::string mac; |
| 216 // get the new |keyed_path|. | 250 bool is_string = it.value().GetAsString(&mac); |
|
gab
2016/08/03 18:19:35
Too bad that we now have an extra string copy, I g
proberge
2016/08/04 00:13:46
Tried some StringValue shenanigans. From my readin
gab
2016/08/08 04:37:44
Yes, your method of getting the underlying StringV
| |
| 217 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 251 DCHECK(is_string); |
| 218 contents_->SetSplitMac( | 252 |
| 219 path, it.key(), | 253 contents_->SetSplitMac(path, it.key(), mac); |
| 220 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | |
| 221 } | 254 } |
| 222 } | 255 } |
| 223 super_mac_dirty_ = true; | 256 super_mac_dirty_ = true; |
| 224 } | 257 } |
| 225 | 258 |
| 226 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( | 259 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( |
| 227 const std::string& path) const { | 260 const std::string& path) const { |
| 228 std::string out_value; | 261 std::string out_value; |
| 229 std::map<std::string, std::string> out_values; | 262 std::map<std::string, std::string> out_values; |
| 230 return contents_->GetMac(path, &out_value) || | 263 return contents_->GetMac(path, &out_value) || |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 252 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | 285 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { |
| 253 return super_mac_valid_; | 286 return super_mac_valid_; |
| 254 } | 287 } |
| 255 | 288 |
| 256 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | 289 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { |
| 257 if (!outer_->use_super_mac_ || super_mac_valid_) | 290 if (!outer_->use_super_mac_ || super_mac_valid_) |
| 258 return false; | 291 return false; |
| 259 super_mac_dirty_ = true; | 292 super_mac_dirty_ = true; |
| 260 return true; | 293 return true; |
| 261 } | 294 } |
| OLD | NEW |