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 "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 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); | |
| 91 it.Advance()) { | |
| 92 ValueState value_state = CheckValue(path + "." + it.key(), &it.value()); | |
|
robertshield
2014/01/16 03:59:30
the +'s yield a fair amount of object construction
gab
2014/01/16 18:58:06
How about this? Out-of-scope string with "|path|."
| |
| 93 switch (value_state) { | |
| 94 case CLEARED: | |
| 95 // CLEARED doesn't make sense as a NULL value would never be sampled | |
| 96 // by the DictionaryValue::Iterator; in fact it is a known weakness of | |
| 97 // this current algorithm to not detect the case where a single key is | |
| 98 // cleared entirely from the dictionary pref. | |
| 99 NOTREACHED(); | |
| 100 break; | |
| 101 case MIGRATED: | |
| 102 // Split tracked preferences were introduced after the migration started | |
| 103 // so no migration is expected, but declare it invalid in Release builds | |
| 104 // anyways. | |
| 105 NOTREACHED(); | |
| 106 invalid_keys->push_back(it.key()); | |
| 107 break; | |
| 108 case UNCHANGED: | |
| 109 break; | |
| 110 case CHANGED: // Falls through. | |
| 111 case UNTRUSTED_UNKNOWN_VALUE: // Falls through. | |
| 112 case TRUSTED_UNKNOWN_VALUE: | |
| 113 // Declare this value invalid, whether it was changed or never seen | |
| 114 // before (note that the initial hashes being trusted is irrelevant for | |
| 115 // individual entries in this scenario). | |
| 116 invalid_keys->push_back(it.key()); | |
| 117 break; | |
| 118 } | |
| 119 } | |
| 120 return invalid_keys->empty() ? UNCHANGED : CHANGED; | |
| 121 } | |
| 122 | |
| 123 void PrefHashStoreImpl::StoreSplitHash( | |
| 124 const std::string& path, | |
| 125 const base::DictionaryValue* split_value) { | |
| 126 // TODO(gab): This method should be a transaction on the underlying dictionary | |
| 127 // to avoid the case where a scheduled write kicks in on the blocking pool | |
| 128 // right after ClearPath() and chrome subsequently crashes; resulting in no | |
| 129 // hashes being stored on disk in the next startup... | |
|
robertshield
2014/01/16 03:59:30
Can you file / reference a bug for the transaction
gab
2014/01/16 18:58:06
Done.
| |
| 130 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); | |
| 131 ClearPath(path, &update); | |
| 132 | |
| 133 if (split_value) { | |
| 134 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | |
| 135 it.Advance()) { | |
| 136 StoreHashInternal(path + "." + it.key(), &it.value(), &update); | |
|
robertshield
2014/01/16 03:59:30
Same comment on object construction here.
gab
2014/01/16 18:58:06
Done.
| |
| 137 } | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 void PrefHashStoreImpl::ClearPath(const std::string& path, | |
| 142 DictionaryPrefUpdate* update) { | |
| 143 base::DictionaryValue* hashes_dict = NULL; | |
| 144 if (update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
| 145 &hashes_dict)) { | |
| 146 hashes_dict->Remove(path, NULL); | |
| 147 } | |
| 148 UpdateHashOfHashes(hashes_dict, update); | |
| 149 } | |
| 150 | |
| 151 bool PrefHashStoreImpl::HasSplitHashesAtPath(const std::string& path) const { | |
| 152 const base::DictionaryValue* pref_hash_dicts = | |
| 153 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | |
| 154 const base::DictionaryValue* hashed_prefs = NULL; | |
| 155 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
| 156 &hashed_prefs); | |
| 157 return hashed_prefs && hashed_prefs->GetDictionary(path, NULL); | |
| 158 } | |
| 159 | |
| 160 void PrefHashStoreImpl::StoreHashInternal(const std::string& path, | |
| 161 const base::Value* new_value, | |
| 162 DictionaryPrefUpdate* update) { | |
| 163 base::DictionaryValue* hashes_dict = NULL; | |
| 164 | |
| 165 // Get the dictionary corresponding to the profile name, which may have a '.' | |
| 166 if (!update->Get()->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
| 167 &hashes_dict)) { | |
| 168 hashes_dict = new base::DictionaryValue; | |
| 169 update->Get()->SetWithoutPathExpansion(hash_store_id_, hashes_dict); | |
| 170 } | |
| 78 hashes_dict->SetString( | 171 hashes_dict->SetString( |
| 79 path, pref_hash_calculator_.Calculate(path, new_value)); | 172 path, pref_hash_calculator_.Calculate(path, new_value)); |
| 80 | 173 |
| 174 UpdateHashOfHashes(hashes_dict, update); | |
| 175 } | |
| 176 | |
| 177 void PrefHashStoreImpl::UpdateHashOfHashes( | |
| 178 const base::DictionaryValue* hashes_dict, | |
| 179 DictionaryPrefUpdate* update) { | |
| 180 | |
| 81 // Get the dictionary where the hash of hashes are stored. | 181 // Get the dictionary where the hash of hashes are stored. |
| 82 base::DictionaryValue* hash_of_hashes_dict = NULL; | 182 base::DictionaryValue* hash_of_hashes_dict = NULL; |
| 83 if (!update->GetDictionaryWithoutPathExpansion(internals::kHashOfHashesPref, | 183 if (!update->Get()->GetDictionaryWithoutPathExpansion( |
| 84 &hash_of_hashes_dict)) { | 184 internals::kHashOfHashesPref, &hash_of_hashes_dict)) { |
| 85 hash_of_hashes_dict = new base::DictionaryValue; | 185 hash_of_hashes_dict = new base::DictionaryValue; |
| 86 update->SetWithoutPathExpansion(internals::kHashOfHashesPref, | 186 update->Get()->SetWithoutPathExpansion(internals::kHashOfHashesPref, |
| 87 hash_of_hashes_dict); | 187 hash_of_hashes_dict); |
| 88 } | 188 } |
| 89 // Use the |hash_store_id_| as the hashed path to avoid having the hash | 189 // Use the |hash_store_id_| as the hashed path to avoid having the hash |
| 90 // depend on kProfilePreferenceHashes. | 190 // depend on kProfilePreferenceHashes. |
| 91 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, | 191 std::string hash_of_hashes(pref_hash_calculator_.Calculate(hash_store_id_, |
| 92 hashes_dict)); | 192 hashes_dict)); |
| 93 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, | 193 hash_of_hashes_dict->SetStringWithoutPathExpansion(hash_store_id_, |
| 94 hash_of_hashes); | 194 hash_of_hashes); |
| 95 } | 195 } |
| 96 | 196 |
| 97 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { | 197 bool PrefHashStoreImpl::IsHashDictionaryTrusted() const { |
| 98 const base::DictionaryValue* pref_hash_dicts = | 198 const base::DictionaryValue* pref_hash_dicts = |
| 99 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | 199 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); |
| 100 const base::DictionaryValue* hashes_dict = NULL; | 200 const base::DictionaryValue* hashes_dict = NULL; |
| 101 const base::DictionaryValue* hash_of_hashes_dict = NULL; | 201 const base::DictionaryValue* hash_of_hashes_dict = NULL; |
| 102 std::string hash_of_hashes; | 202 std::string hash_of_hashes; |
| 103 // The absence of the hashes dictionary isn't trusted. Nor is the absence of | 203 // The absence of the hashes dictionary isn't trusted. Nor is the absence of |
| 104 // the hash of hashes for this |hash_store_id_|. | 204 // the hash of hashes for this |hash_store_id_|. |
| 105 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 205 if (!pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
| 106 hash_store_id_, &hashes_dict) || | 206 hash_store_id_, &hashes_dict) || |
| 107 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( | 207 !pref_hash_dicts->GetDictionaryWithoutPathExpansion( |
| 108 internals::kHashOfHashesPref, &hash_of_hashes_dict) || | 208 internals::kHashOfHashesPref, &hash_of_hashes_dict) || |
| 109 !hash_of_hashes_dict->GetStringWithoutPathExpansion( | 209 !hash_of_hashes_dict->GetStringWithoutPathExpansion( |
| 110 hash_store_id_, &hash_of_hashes)) { | 210 hash_store_id_, &hash_of_hashes)) { |
| 111 return false; | 211 return false; |
| 112 } | 212 } |
| 113 | 213 |
| 114 return pref_hash_calculator_.Validate( | 214 return pref_hash_calculator_.Validate( |
| 115 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; | 215 hash_store_id_, hashes_dict, hash_of_hashes) == PrefHashCalculator::VALID; |
| 116 } | 216 } |
| OLD | NEW |