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