Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(806)

Side by Side Diff: components/user_prefs/tracked/pref_hash_store_impl.cc

Issue 2304573002: Add ComputeMac and ComputeSplitMacs methods to pref_hash_store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove stray line break Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
gab 2016/09/01 21:21:56 Keep this now that it's only fwd-decl'ed in header
proberge 2016/09/06 18:43:39 Done.
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" 14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
16 15
17 class PrefHashStoreImpl::PrefHashStoreTransactionImpl 16 class PrefHashStoreImpl::PrefHashStoreTransactionImpl
18 : public PrefHashStoreTransaction { 17 : public PrefHashStoreTransaction {
19 public: 18 public:
20 // Constructs a PrefHashStoreTransactionImpl which can use the private 19 // Constructs a PrefHashStoreTransactionImpl which can use the private
21 // members of its |outer| PrefHashStoreImpl. 20 // members of its |outer| PrefHashStoreImpl.
22 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, 21 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer,
23 std::unique_ptr<HashStoreContents> storage); 22 std::unique_ptr<HashStoreContents> storage);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 56
58 PrefHashStoreImpl::~PrefHashStoreImpl() { 57 PrefHashStoreImpl::~PrefHashStoreImpl() {
59 } 58 }
60 59
61 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( 60 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction(
62 std::unique_ptr<HashStoreContents> storage) { 61 std::unique_ptr<HashStoreContents> storage) {
63 return std::unique_ptr<PrefHashStoreTransaction>( 62 return std::unique_ptr<PrefHashStoreTransaction>(
64 new PrefHashStoreTransactionImpl(this, std::move(storage))); 63 new PrefHashStoreTransactionImpl(this, std::move(storage)));
65 } 64 }
66 65
66 std::string PrefHashStoreImpl::ComputeMac(const std::string& path,
67 const base::Value* value) {
68 return pref_hash_calculator_.Calculate(path, value);
69 }
70
71 std::unique_ptr<base::DictionaryValue> PrefHashStoreImpl::ComputeSplitMacs(
72 const std::string& path,
73 const base::DictionaryValue* split_values) {
74 DCHECK(split_values);
75
76 std::string keyed_path(path);
77 keyed_path.push_back('.');
78 const size_t common_part_length = keyed_path.length();
79
80 std::unique_ptr<base::DictionaryValue> split_macs(new base::DictionaryValue);
81
82 for (base::DictionaryValue::Iterator it(*split_values); !it.IsAtEnd();
83 it.Advance()) {
84 // Keep the common part from the old |keyed_path| and replace the key to
85 // get the new |keyed_path|.
86 keyed_path.replace(common_part_length, std::string::npos, it.key());
87
88 split_macs->SetStringWithoutPathExpansion(
89 it.key(), ComputeMac(keyed_path, &it.value()));
90 }
91
92 return split_macs;
93 }
94
67 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( 95 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
68 PrefHashStoreImpl* outer, 96 PrefHashStoreImpl* outer,
69 std::unique_ptr<HashStoreContents> storage) 97 std::unique_ptr<HashStoreContents> storage)
70 : outer_(outer), 98 : outer_(outer),
71 contents_(std::move(storage)), 99 contents_(std::move(storage)),
72 super_mac_valid_(false), 100 super_mac_valid_(false),
73 super_mac_dirty_(false) { 101 super_mac_dirty_(false) {
74 if (!outer_->use_super_mac_) 102 if (!outer_->use_super_mac_)
75 return; 103 return;
76 104
77 // The store must have a valid super MAC to be trusted. 105 // The store must have a valid super MAC to be trusted.
78 std::string super_mac = contents_->GetSuperMac(); 106 std::string super_mac = contents_->GetSuperMac();
79 if (super_mac.empty()) 107 if (super_mac.empty())
80 return; 108 return;
81 109
82 super_mac_valid_ = 110 super_mac_valid_ =
83 outer_->pref_hash_calculator_.Validate( 111 outer_->pref_hash_calculator_.Validate(
84 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; 112 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID;
85 } 113 }
86 114
87 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: 115 PrefHashStoreImpl::PrefHashStoreTransactionImpl::
88 ~PrefHashStoreTransactionImpl() { 116 ~PrefHashStoreTransactionImpl() {
89 if (super_mac_dirty_ && outer_->use_super_mac_) { 117 if (super_mac_dirty_ && outer_->use_super_mac_) {
90 // Get the dictionary of hashes (or NULL if it doesn't exist). 118 // Get the dictionary of hashes (or NULL if it doesn't exist).
91 const base::DictionaryValue* hashes_dict = contents_->GetContents(); 119 const base::DictionaryValue* hashes_dict = contents_->GetContents();
92 contents_->SetSuperMac( 120 contents_->SetSuperMac(outer_->ComputeMac("", hashes_dict));
93 outer_->pref_hash_calculator_.Calculate("", hashes_dict));
94 } 121 }
95 } 122 }
96 123
97 PrefHashStoreTransaction::ValueState 124 PrefHashStoreTransaction::ValueState
98 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( 125 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
99 const std::string& path, 126 const std::string& path,
100 const base::Value* initial_value) const { 127 const base::Value* initial_value) const {
101 std::string last_hash; 128 std::string last_hash;
102 contents_->GetMac(path, &last_hash); 129 contents_->GetMac(path, &last_hash);
103 130
(...skipping 19 matching lines...) Expand all
123 return initial_value ? CHANGED : CLEARED; 150 return initial_value ? CHANGED : CLEARED;
124 } 151 }
125 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " 152 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
126 << validation_result; 153 << validation_result;
127 return UNTRUSTED_UNKNOWN_VALUE; 154 return UNTRUSTED_UNKNOWN_VALUE;
128 } 155 }
129 156
130 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( 157 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
131 const std::string& path, 158 const std::string& path,
132 const base::Value* new_value) { 159 const base::Value* new_value) {
133 const std::string mac = 160 const std::string mac = outer_->ComputeMac(path, new_value);
134 outer_->pref_hash_calculator_.Calculate(path, new_value);
135 contents_->SetMac(path, mac); 161 contents_->SetMac(path, mac);
136 super_mac_dirty_ = true; 162 super_mac_dirty_ = true;
137 } 163 }
138 164
139 PrefHashStoreTransaction::ValueState 165 PrefHashStoreTransaction::ValueState
140 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( 166 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
141 const std::string& path, 167 const std::string& path,
142 const base::DictionaryValue* initial_split_value, 168 const base::DictionaryValue* initial_split_value,
143 std::vector<std::string>* invalid_keys) const { 169 std::vector<std::string>* invalid_keys) const {
144 DCHECK(invalid_keys && invalid_keys->empty()); 170 DCHECK(invalid_keys && invalid_keys->empty());
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) 226 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED)
201 : CHANGED; 227 : CHANGED;
202 } 228 }
203 229
204 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( 230 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
205 const std::string& path, 231 const std::string& path,
206 const base::DictionaryValue* split_value) { 232 const base::DictionaryValue* split_value) {
207 contents_->RemoveEntry(path); 233 contents_->RemoveEntry(path);
208 234
209 if (split_value) { 235 if (split_value) {
210 std::string keyed_path(path); 236 std::unique_ptr<base::DictionaryValue> split_macs =
211 keyed_path.push_back('.'); 237 outer_->ComputeSplitMacs(path, split_value);
212 const size_t common_part_length = keyed_path.length(); 238
213 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); 239 for (base::DictionaryValue::Iterator it(*split_macs); !it.IsAtEnd();
214 it.Advance()) { 240 it.Advance()) {
215 // Keep the common part from the old |keyed_path| and replace the key to 241 const base::StringValue* value_as_string;
216 // get the new |keyed_path|. 242 bool is_string = it.value().GetAsString(&value_as_string);
217 keyed_path.replace(common_part_length, std::string::npos, it.key()); 243 DCHECK(is_string);
218 contents_->SetSplitMac( 244
219 path, it.key(), 245 contents_->SetSplitMac(path, it.key(), value_as_string->GetString());
220 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value()));
221 } 246 }
222 } 247 }
223 super_mac_dirty_ = true; 248 super_mac_dirty_ = true;
224 } 249 }
225 250
226 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( 251 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash(
227 const std::string& path) const { 252 const std::string& path) const {
228 std::string out_value; 253 std::string out_value;
229 std::map<std::string, std::string> out_values; 254 std::map<std::string, std::string> out_values;
230 return contents_->GetMac(path, &out_value) || 255 return contents_->GetMac(path, &out_value) ||
(...skipping 21 matching lines...) Expand all
252 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { 277 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const {
253 return super_mac_valid_; 278 return super_mac_valid_;
254 } 279 }
255 280
256 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { 281 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
257 if (!outer_->use_super_mac_ || super_mac_valid_) 282 if (!outer_->use_super_mac_ || super_mac_valid_)
258 return false; 283 return false;
259 super_mac_dirty_ = true; 284 super_mac_dirty_ = true;
260 return true; 285 return true;
261 } 286 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698