pref_service_hash_store_contents.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" 6 7#include "base/prefs/pref_registry_simple.h" 8#include "base/prefs/pref_service.h" 9#include "base/prefs/scoped_user_pref_update.h" 10#include "base/values.h" 11#include "chrome/common/pref_names.h" 12 13namespace { 14 15// Implements get-or-create of a dictionary value and holds a 16// DictionaryPrefUpdate. 17class PrefServiceMutableDictionary 18 : public HashStoreContents::MutableDictionary { 19 public: 20 // Creates an instance that provides mutable access to a dictionary value 21 // named |key| that is a child of |prefs::kProfilePreferenceHashes| in 22 // |prefs|. 23 PrefServiceMutableDictionary(const std::string& key, 24 PrefService* pref_service); 25 26 // HashStoreContents::MutableDictionary implementation 27 virtual base::DictionaryValue* operator->() OVERRIDE; 28 29 private: 30 const std::string key_; 31 DictionaryPrefUpdate update_; 32}; 33 34PrefServiceMutableDictionary::PrefServiceMutableDictionary( 35 const std::string& key, 36 PrefService* pref_service) 37 : key_(key), update_(pref_service, prefs::kProfilePreferenceHashes) { 38 DCHECK(!key_.empty()); 39} 40 41base::DictionaryValue* PrefServiceMutableDictionary::operator->() { 42 base::DictionaryValue* dictionary = NULL; 43 if (!update_->GetDictionaryWithoutPathExpansion(key_, &dictionary)) { 44 dictionary = new base::DictionaryValue; 45 update_->SetWithoutPathExpansion(key_, dictionary); 46 } 47 return dictionary; 48} 49 50} // namespace 51 52// static 53const char PrefServiceHashStoreContents::kHashOfHashesDict[] = "hash_of_hashes"; 54 55// static 56const char PrefServiceHashStoreContents::kStoreVersionsDict[] = 57 "store_versions"; 58 59PrefServiceHashStoreContents::PrefServiceHashStoreContents( 60 const std::string& hash_store_id, 61 PrefService* pref_service) 62 : hash_store_id_(hash_store_id), pref_service_(pref_service) {} 63 64// static 65void PrefServiceHashStoreContents::RegisterPrefs(PrefRegistrySimple* registry) { 66 // Register the top level dictionary to map profile names to dictionaries of 67 // tracked preferences. 68 registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes); 69} 70 71// static 72void PrefServiceHashStoreContents::ResetAllPrefHashStores( 73 PrefService* pref_service) { 74 pref_service->ClearPref(prefs::kProfilePreferenceHashes); 75} 76 77std::string PrefServiceHashStoreContents::hash_store_id() const { 78 return hash_store_id_; 79} 80 81void PrefServiceHashStoreContents::Reset() { 82 DictionaryPrefUpdate update(pref_service_, prefs::kProfilePreferenceHashes); 83 84 update->RemoveWithoutPathExpansion(hash_store_id_, NULL); 85 86 // Remove this store's entry in the kStoreVersionsDict. 87 base::DictionaryValue* version_dict; 88 if (update->GetDictionary(kStoreVersionsDict, &version_dict)) 89 version_dict->RemoveWithoutPathExpansion(hash_store_id_, NULL); 90 91 // Remove this store's entry in the kHashOfHashesDict. 92 base::DictionaryValue* hash_of_hashes_dict; 93 if (update->GetDictionaryWithoutPathExpansion(kHashOfHashesDict, 94 &hash_of_hashes_dict)) { 95 hash_of_hashes_dict->RemoveWithoutPathExpansion(hash_store_id_, NULL); 96 } 97} 98 99bool PrefServiceHashStoreContents::IsInitialized() const { 100 const base::DictionaryValue* pref_hash_dicts = 101 pref_service_->GetDictionary(prefs::kProfilePreferenceHashes); 102 return pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, 103 NULL); 104} 105 106bool PrefServiceHashStoreContents::GetVersion(int* version) const { 107 DCHECK(version); 108 const base::DictionaryValue* pref_hash_data = 109 pref_service_->GetDictionary(prefs::kProfilePreferenceHashes); 110 111 const base::DictionaryValue* version_dict; 112 return pref_hash_data->GetDictionary(kStoreVersionsDict, &version_dict) && 113 version_dict->GetIntegerWithoutPathExpansion(hash_store_id_, version); 114} 115 116void PrefServiceHashStoreContents::SetVersion(int version) { 117 PrefServiceMutableDictionary(kStoreVersionsDict, pref_service_) 118 ->SetIntegerWithoutPathExpansion(hash_store_id_, version); 119} 120 121const base::DictionaryValue* PrefServiceHashStoreContents::GetContents() const { 122 const base::DictionaryValue* pref_hash_dicts = 123 pref_service_->GetDictionary(prefs::kProfilePreferenceHashes); 124 const base::DictionaryValue* hashes_dict = NULL; 125 pref_hash_dicts->GetDictionaryWithoutPathExpansion(hash_store_id_, 126 &hashes_dict); 127 return hashes_dict; 128} 129 130scoped_ptr<HashStoreContents::MutableDictionary> 131PrefServiceHashStoreContents::GetMutableContents() { 132 return scoped_ptr<HashStoreContents::MutableDictionary>( 133 new PrefServiceMutableDictionary(hash_store_id_, pref_service_)); 134} 135 136std::string PrefServiceHashStoreContents::GetSuperMac() const { 137 const base::DictionaryValue* pref_hash_dicts = 138 pref_service_->GetDictionary(prefs::kProfilePreferenceHashes); 139 const base::DictionaryValue* hash_of_hashes_dict = NULL; 140 std::string hash_of_hashes; 141 if (pref_hash_dicts->GetDictionaryWithoutPathExpansion( 142 kHashOfHashesDict, &hash_of_hashes_dict)) { 143 hash_of_hashes_dict->GetStringWithoutPathExpansion(hash_store_id_, 144 &hash_of_hashes); 145 } 146 return hash_of_hashes; 147} 148 149void PrefServiceHashStoreContents::SetSuperMac(const std::string& super_mac) { 150 PrefServiceMutableDictionary(kHashOfHashesDict, pref_service_) 151 ->SetStringWithoutPathExpansion(hash_store_id_, super_mac); 152} 153