dictionary_hash_store_contents.cc 4.5 KB

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