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.
5 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h"
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"
15 // Implements get-or-create of a dictionary value and holds a
16 // DictionaryPrefUpdate.
17 class PrefServiceMutableDictionary
18 : public HashStoreContents::MutableDictionary
{
20 // Creates an instance that provides mutable access to a dictionary value
21 // named |key| that is a child of |prefs::kProfilePreferenceHashes| in
23 PrefServiceMutableDictionary(const std::string
& key
,
24 PrefService
* pref_service
);
26 // HashStoreContents::MutableDictionary implementation
27 virtual base::DictionaryValue
* operator->() OVERRIDE
;
30 const std::string key_
;
31 DictionaryPrefUpdate update_
;
34 PrefServiceMutableDictionary::PrefServiceMutableDictionary(
35 const std::string
& key
,
36 PrefService
* pref_service
)
37 : key_(key
), update_(pref_service
, prefs::kProfilePreferenceHashes
) {
38 DCHECK(!key_
.empty());
41 base::DictionaryValue
* PrefServiceMutableDictionary::operator->() {
42 base::DictionaryValue
* dictionary
= NULL
;
43 if (!update_
->GetDictionaryWithoutPathExpansion(key_
, &dictionary
)) {
44 dictionary
= new base::DictionaryValue
;
45 update_
->SetWithoutPathExpansion(key_
, dictionary
);
53 const char PrefServiceHashStoreContents::kHashOfHashesDict
[] = "hash_of_hashes";
56 const char PrefServiceHashStoreContents::kStoreVersionsDict
[] =
59 PrefServiceHashStoreContents::PrefServiceHashStoreContents(
60 const std::string
& hash_store_id
,
61 PrefService
* pref_service
)
62 : hash_store_id_(hash_store_id
), pref_service_(pref_service
) {}
65 void 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
);
72 void PrefServiceHashStoreContents::ResetAllPrefHashStores(
73 PrefService
* pref_service
) {
74 pref_service
->ClearPref(prefs::kProfilePreferenceHashes
);
77 std::string
PrefServiceHashStoreContents::hash_store_id() const {
78 return hash_store_id_
;
81 void PrefServiceHashStoreContents::Reset() {
82 DictionaryPrefUpdate
update(pref_service_
, prefs::kProfilePreferenceHashes
);
84 update
->RemoveWithoutPathExpansion(hash_store_id_
, NULL
);
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
);
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
);
99 bool 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_
,
106 bool PrefServiceHashStoreContents::GetVersion(int* version
) const {
108 const base::DictionaryValue
* pref_hash_data
=
109 pref_service_
->GetDictionary(prefs::kProfilePreferenceHashes
);
111 const base::DictionaryValue
* version_dict
;
112 return pref_hash_data
->GetDictionary(kStoreVersionsDict
, &version_dict
) &&
113 version_dict
->GetIntegerWithoutPathExpansion(hash_store_id_
, version
);
116 void PrefServiceHashStoreContents::SetVersion(int version
) {
117 PrefServiceMutableDictionary(kStoreVersionsDict
, pref_service_
)
118 ->SetIntegerWithoutPathExpansion(hash_store_id_
, version
);
121 const 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_
,
130 scoped_ptr
<HashStoreContents::MutableDictionary
>
131 PrefServiceHashStoreContents::GetMutableContents() {
132 return scoped_ptr
<HashStoreContents::MutableDictionary
>(
133 new PrefServiceMutableDictionary(hash_store_id_
, pref_service_
));
136 std::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_
,
146 return hash_of_hashes
;
149 void PrefServiceHashStoreContents::SetSuperMac(const std::string
& super_mac
) {
150 PrefServiceMutableDictionary(kHashOfHashesDict
, pref_service_
)
151 ->SetStringWithoutPathExpansion(hash_store_id_
, super_mac
);
154 void PrefServiceHashStoreContents::CommitPendingWrite() {
155 pref_service_
->CommitPendingWrite();