1 // Copyright (c) 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/dictionary_hash_store_contents.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/prefs/persistent_pref_store.h"
10 #include "base/values.h"
11 #include "components/pref_registry/pref_registry_syncable.h"
15 const char kPreferenceMACs
[] = "protection.macs";
16 const char kSuperMACPref
[] = "protection.super_mac";
18 class MutablePreferenceMacDictionary
19 : public HashStoreContents::MutableDictionary
{
21 explicit MutablePreferenceMacDictionary(base::DictionaryValue
* storage
);
23 // MutableDictionary implementation
24 virtual base::DictionaryValue
* operator->() OVERRIDE
;
27 base::DictionaryValue
* storage_
;
29 DISALLOW_COPY_AND_ASSIGN(MutablePreferenceMacDictionary
);
32 MutablePreferenceMacDictionary::MutablePreferenceMacDictionary(
33 base::DictionaryValue
* storage
)
37 base::DictionaryValue
* MutablePreferenceMacDictionary::operator->() {
38 base::DictionaryValue
* mac_dictionary
= NULL
;
40 if (!storage_
->GetDictionary(kPreferenceMACs
, &mac_dictionary
)) {
41 mac_dictionary
= new base::DictionaryValue
;
42 storage_
->Set(kPreferenceMACs
, mac_dictionary
);
45 return mac_dictionary
;
50 DictionaryHashStoreContents::DictionaryHashStoreContents(
51 base::DictionaryValue
* storage
)
56 void DictionaryHashStoreContents::RegisterProfilePrefs(
57 user_prefs::PrefRegistrySyncable
* registry
) {
58 registry
->RegisterDictionaryPref(
60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
61 registry
->RegisterStringPref(
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
67 std::string
DictionaryHashStoreContents::hash_store_id() const {
71 void DictionaryHashStoreContents::Reset() {
72 storage_
->Remove(kPreferenceMACs
, NULL
);
75 bool DictionaryHashStoreContents::IsInitialized() const {
76 return storage_
->GetDictionary(kPreferenceMACs
, NULL
);
79 const base::DictionaryValue
* DictionaryHashStoreContents::GetContents() const {
80 const base::DictionaryValue
* mac_dictionary
= NULL
;
81 storage_
->GetDictionary(kPreferenceMACs
, &mac_dictionary
);
82 return mac_dictionary
;
85 scoped_ptr
<HashStoreContents::MutableDictionary
>
86 DictionaryHashStoreContents::GetMutableContents() {
87 return scoped_ptr
<MutableDictionary
>(
88 new MutablePreferenceMacDictionary(storage_
));
91 std::string
DictionaryHashStoreContents::GetSuperMac() const {
92 std::string super_mac_string
;
93 storage_
->GetString(kSuperMACPref
, &super_mac_string
);
94 return super_mac_string
;
97 void DictionaryHashStoreContents::SetSuperMac(const std::string
& super_mac
) {
98 storage_
->SetString(kSuperMACPref
, super_mac
);