Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / user_prefs / tracked / dictionary_hash_store_contents.cc
blobba791278c0785a90e5f2d6b43aacf21c9e541ba3
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 "components/user_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"
13 namespace {
15 const char kPreferenceMACs[] = "protection.macs";
16 const char kSuperMACPref[] = "protection.super_mac";
18 class MutablePreferenceMacDictionary
19 : public HashStoreContents::MutableDictionary {
20 public:
21 explicit MutablePreferenceMacDictionary(base::DictionaryValue* storage);
23 // MutableDictionary implementation
24 base::DictionaryValue* operator->() override;
26 private:
27 base::DictionaryValue* storage_;
29 DISALLOW_COPY_AND_ASSIGN(MutablePreferenceMacDictionary);
32 MutablePreferenceMacDictionary::MutablePreferenceMacDictionary(
33 base::DictionaryValue* storage)
34 : storage_(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;
48 } // namespace
50 DictionaryHashStoreContents::DictionaryHashStoreContents(
51 base::DictionaryValue* storage)
52 : storage_(storage) {
55 // static
56 void DictionaryHashStoreContents::RegisterProfilePrefs(
57 user_prefs::PrefRegistrySyncable* registry) {
58 registry->RegisterDictionaryPref(kPreferenceMACs);
59 registry->RegisterStringPref(kSuperMACPref, std::string());
62 std::string DictionaryHashStoreContents::hash_store_id() const {
63 return "";
66 void DictionaryHashStoreContents::Reset() {
67 storage_->Remove(kPreferenceMACs, NULL);
70 bool DictionaryHashStoreContents::IsInitialized() const {
71 return storage_->GetDictionary(kPreferenceMACs, NULL);
74 const base::DictionaryValue* DictionaryHashStoreContents::GetContents() const {
75 const base::DictionaryValue* mac_dictionary = NULL;
76 storage_->GetDictionary(kPreferenceMACs, &mac_dictionary);
77 return mac_dictionary;
80 scoped_ptr<HashStoreContents::MutableDictionary>
81 DictionaryHashStoreContents::GetMutableContents() {
82 return scoped_ptr<MutableDictionary>(
83 new MutablePreferenceMacDictionary(storage_));
86 std::string DictionaryHashStoreContents::GetSuperMac() const {
87 std::string super_mac_string;
88 storage_->GetString(kSuperMACPref, &super_mac_string);
89 return super_mac_string;
92 void DictionaryHashStoreContents::SetSuperMac(const std::string& super_mac) {
93 storage_->SetString(kSuperMACPref, super_mac);