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/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 // Get the dictionary corresponding to the profile name, which may have a '.' | 61 // Get the dictionary corresponding to the profile name, which may have a '.' |
| 62 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_, | 62 if (!update->GetDictionaryWithoutPathExpansion(hash_store_id_, |
| 63 &child_dictionary)) { | 63 &child_dictionary)) { |
| 64 child_dictionary = new DictionaryValue; | 64 child_dictionary = new DictionaryValue; |
| 65 update->SetWithoutPathExpansion(hash_store_id_, child_dictionary); | 65 update->SetWithoutPathExpansion(hash_store_id_, child_dictionary); |
| 66 } | 66 } |
| 67 | 67 |
| 68 child_dictionary->SetString( | 68 child_dictionary->SetString( |
| 69 path, pref_hash_calculator_.Calculate(path, new_value)); | 69 path, pref_hash_calculator_.Calculate(path, new_value)); |
| 70 } | 70 } |
| 71 | |
| 72 PrefHashStore::ValueState PrefHashStoreImpl::CheckSplitValue( | |
| 73 const std::string& path, | |
| 74 const base::DictionaryValue* initial_split_value, | |
| 75 std::vector<std::string>* invalid_keys) const { | |
| 76 DCHECK(invalid_keys && invalid_keys->empty()); | |
| 77 | |
| 78 bool has_hashes = HasPath(path); | |
| 79 | |
| 80 // Treat NULL and empty the same; otherwise we would need to store a hash | |
| 81 // for the entire dictionary (or some other special beacon) to | |
| 82 // differentiate these two cases which are really the same for | |
| 83 // dictionaries. | |
| 84 if (!initial_split_value || initial_split_value->empty()) | |
| 85 return has_hashes ? CLEARED : UNCHANGED; | |
| 86 | |
| 87 if (!has_hashes) | |
| 88 return UNKNOWN_VALUE; | |
| 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()); | |
| 93 switch (value_state) { | |
| 94 case CLEARED: // Falls through. | |
|
erikwright (departed)
2013/12/17 02:22:50
Don't fall through, have a dedicated NOTREACHED fo
gab
2013/12/17 18:08:05
Done.
| |
| 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 case MIGRATED: | |
| 100 // Split tracked preferences were introduced after the migration started | |
| 101 // so no migration is expected. | |
| 102 NOTREACHED(); | |
| 103 break; | |
| 104 case UNCHANGED: | |
| 105 break; | |
| 106 case CHANGED: // Falls through. | |
| 107 case UNKNOWN_VALUE: | |
| 108 // Declare this value invalid, whether it was changed or never seen | |
| 109 // before. | |
| 110 invalid_keys->push_back(it.key()); | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 return invalid_keys->empty() ? UNCHANGED : CHANGED; | |
| 115 } | |
| 116 | |
| 117 void PrefHashStoreImpl::StoreSplitHash( | |
| 118 const std::string& path, | |
| 119 const base::DictionaryValue* split_value) { | |
| 120 ClearPath(path); | |
| 121 | |
| 122 if (split_value) { | |
| 123 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | |
| 124 it.Advance()) { | |
| 125 StoreHash(path + "." + it.key(), &it.value()); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void PrefHashStoreImpl::ClearPath(const std::string& path) { | |
| 131 DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes); | |
| 132 DictionaryValue* child_dictionary = NULL; | |
| 133 | |
| 134 if (update->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
| 135 &child_dictionary)) { | |
| 136 child_dictionary->Remove(path, NULL); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 bool PrefHashStoreImpl::HasPath(const std::string& path) const { | |
| 141 const base::DictionaryValue* pref_hash_dicts = | |
| 142 local_state_->GetDictionary(prefs::kProfilePreferenceHashes); | |
| 143 const base::DictionaryValue* hashed_prefs = NULL; | |
| 144 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, | |
| 145 &hashed_prefs); | |
| 146 return hashed_prefs && hashed_prefs->Get(path, NULL); | |
| 147 } | |
| OLD | NEW |