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.
4
5 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h"
6
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"
12
13 namespace {
14
15 const char kPreferenceMACs[] = "protection.macs";
16 const char kSuperMACPref[] = "protection.super_mac";
17
18 class MutablePreferenceMacDictionary
19 : public HashStoreContents::MutableDictionary {
20 public:
21 explicit MutablePreferenceMacDictionary(base::DictionaryValue* storage);
22
23 // MutableDictionary implementation
24 virtual base::DictionaryValue* operator->() OVERRIDE;
25
26 private:
27 base::DictionaryValue* storage_;
28
29 DISALLOW_COPY_AND_ASSIGN(MutablePreferenceMacDictionary);
30 };
31
MutablePreferenceMacDictionary(base::DictionaryValue * storage)32 MutablePreferenceMacDictionary::MutablePreferenceMacDictionary(
33 base::DictionaryValue* storage)
34 : storage_(storage) {
35 }
36
operator ->()37 base::DictionaryValue* MutablePreferenceMacDictionary::operator->() {
38 base::DictionaryValue* mac_dictionary = NULL;
39
40 if (!storage_->GetDictionary(kPreferenceMACs, &mac_dictionary)) {
41 mac_dictionary = new base::DictionaryValue;
42 storage_->Set(kPreferenceMACs, mac_dictionary);
43 }
44
45 return mac_dictionary;
46 }
47
48 } // namespace
49
DictionaryHashStoreContents(base::DictionaryValue * storage)50 DictionaryHashStoreContents::DictionaryHashStoreContents(
51 base::DictionaryValue* storage)
52 : storage_(storage) {
53 }
54
55 // static
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)56 void DictionaryHashStoreContents::RegisterProfilePrefs(
57 user_prefs::PrefRegistrySyncable* registry) {
58 registry->RegisterDictionaryPref(
59 kPreferenceMACs,
60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
61 registry->RegisterStringPref(
62 kSuperMACPref,
63 std::string(),
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
65 }
66
hash_store_id() const67 std::string DictionaryHashStoreContents::hash_store_id() const {
68 return "";
69 }
70
Reset()71 void DictionaryHashStoreContents::Reset() {
72 storage_->Remove(kPreferenceMACs, NULL);
73 }
74
IsInitialized() const75 bool DictionaryHashStoreContents::IsInitialized() const {
76 return storage_->GetDictionary(kPreferenceMACs, NULL);
77 }
78
GetContents() const79 const base::DictionaryValue* DictionaryHashStoreContents::GetContents() const {
80 const base::DictionaryValue* mac_dictionary = NULL;
81 storage_->GetDictionary(kPreferenceMACs, &mac_dictionary);
82 return mac_dictionary;
83 }
84
85 scoped_ptr<HashStoreContents::MutableDictionary>
GetMutableContents()86 DictionaryHashStoreContents::GetMutableContents() {
87 return scoped_ptr<MutableDictionary>(
88 new MutablePreferenceMacDictionary(storage_));
89 }
90
GetSuperMac() const91 std::string DictionaryHashStoreContents::GetSuperMac() const {
92 std::string super_mac_string;
93 storage_->GetString(kSuperMACPref, &super_mac_string);
94 return super_mac_string;
95 }
96
SetSuperMac(const std::string & super_mac)97 void DictionaryHashStoreContents::SetSuperMac(const std::string& super_mac) {
98 storage_->SetString(kSuperMACPref, super_mac);
99 }
100