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
13namespace {
14
15const char kPreferenceMACs[] = "protection.macs";
16const char kSuperMACPref[] = "protection.super_mac";
17
18class 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
32MutablePreferenceMacDictionary::MutablePreferenceMacDictionary(
33    base::DictionaryValue* storage)
34    : storage_(storage) {
35}
36
37base::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
50DictionaryHashStoreContents::DictionaryHashStoreContents(
51    base::DictionaryValue* storage)
52    : storage_(storage) {
53}
54
55// static
56void 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
67std::string DictionaryHashStoreContents::hash_store_id() const {
68  return "";
69}
70
71void DictionaryHashStoreContents::Reset() {
72  storage_->Remove(kPreferenceMACs, NULL);
73}
74
75bool DictionaryHashStoreContents::IsInitialized() const {
76  return storage_->GetDictionary(kPreferenceMACs, NULL);
77}
78
79const base::DictionaryValue* DictionaryHashStoreContents::GetContents() const {
80  const base::DictionaryValue* mac_dictionary = NULL;
81  storage_->GetDictionary(kPreferenceMACs, &mac_dictionary);
82  return mac_dictionary;
83}
84
85scoped_ptr<HashStoreContents::MutableDictionary>
86DictionaryHashStoreContents::GetMutableContents() {
87  return scoped_ptr<MutableDictionary>(
88      new MutablePreferenceMacDictionary(storage_));
89}
90
91std::string DictionaryHashStoreContents::GetSuperMac() const {
92  std::string super_mac_string;
93  storage_->GetString(kSuperMACPref, &super_mac_string);
94  return super_mac_string;
95}
96
97void DictionaryHashStoreContents::SetSuperMac(const std::string& super_mac) {
98  storage_->SetString(kSuperMACPref, super_mac);
99}
100