1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/user_prefs/tracked/dictionary_hash_store_contents.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/values.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/persistent_pref_store.h"
namespace {
const char kPreferenceMACs[] = "protection.macs";
const char kSuperMACPref[] = "protection.super_mac";
}
DictionaryHashStoreContents::DictionaryHashStoreContents(
base::DictionaryValue* storage)
: storage_(storage) {
}
// static
void DictionaryHashStoreContents::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(kPreferenceMACs);
registry->RegisterStringPref(kSuperMACPref, std::string());
}
bool DictionaryHashStoreContents::IsCopyable() const {
return false;
}
std::unique_ptr<HashStoreContents> DictionaryHashStoreContents::MakeCopy()
const {
NOTREACHED() << "DictionaryHashStoreContents does not support MakeCopy";
return nullptr;
}
base::StringPiece DictionaryHashStoreContents::GetUMASuffix() const {
// To stay consistent with existing reported data, do not append a suffix
// when reporting UMA stats for this content.
return base::StringPiece();
}
void DictionaryHashStoreContents::Reset() {
storage_->Remove(kPreferenceMACs, NULL);
}
bool DictionaryHashStoreContents::GetMac(const std::string& path,
std::string* out_value) {
const base::DictionaryValue* macs_dict = GetContents();
if (macs_dict)
return macs_dict->GetString(path, out_value);
return false;
}
bool DictionaryHashStoreContents::GetSplitMacs(
const std::string& path,
std::map<std::string, std::string>* split_macs) {
DCHECK(split_macs);
DCHECK(split_macs->empty());
const base::DictionaryValue* macs_dict = GetContents();
const base::DictionaryValue* split_macs_dict = NULL;
if (!macs_dict || !macs_dict->GetDictionary(path, &split_macs_dict))
return false;
for (base::DictionaryValue::Iterator it(*split_macs_dict); !it.IsAtEnd();
it.Advance()) {
std::string mac_string;
if (!it.value().GetAsString(&mac_string)) {
NOTREACHED();
continue;
}
split_macs->insert(make_pair(it.key(), mac_string));
}
return true;
}
void DictionaryHashStoreContents::SetMac(const std::string& path,
const std::string& value) {
base::DictionaryValue* macs_dict = GetMutableContents(true);
macs_dict->SetString(path, value);
}
void DictionaryHashStoreContents::SetSplitMac(const std::string& path,
const std::string& split_path,
const std::string& value) {
// DictionaryValue handles a '.' delimiter.
SetMac(path + '.' + split_path, value);
}
void DictionaryHashStoreContents::ImportEntry(const std::string& path,
const base::Value* in_value) {
base::DictionaryValue* macs_dict = GetMutableContents(true);
macs_dict->Set(path, in_value->DeepCopy());
}
bool DictionaryHashStoreContents::RemoveEntry(const std::string& path) {
base::DictionaryValue* macs_dict = GetMutableContents(false);
if (macs_dict)
return macs_dict->RemovePath(path, NULL);
return false;
}
std::string DictionaryHashStoreContents::GetSuperMac() const {
std::string super_mac_string;
storage_->GetString(kSuperMACPref, &super_mac_string);
return super_mac_string;
}
void DictionaryHashStoreContents::SetSuperMac(const std::string& super_mac) {
storage_->SetString(kSuperMACPref, super_mac);
}
const base::DictionaryValue* DictionaryHashStoreContents::GetContents() const {
const base::DictionaryValue* macs_dict = NULL;
storage_->GetDictionary(kPreferenceMACs, &macs_dict);
return macs_dict;
}
base::DictionaryValue* DictionaryHashStoreContents::GetMutableContents(
bool create_if_null) {
base::DictionaryValue* macs_dict = NULL;
storage_->GetDictionary(kPreferenceMACs, &macs_dict);
if (!macs_dict && create_if_null) {
macs_dict = new base::DictionaryValue;
storage_->Set(kPreferenceMACs, macs_dict);
}
return macs_dict;
}
|