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