pref_value_map.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) 2011 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 "components/prefs/pref_value_map.h"
  5. #include <limits.h>
  6. #include <map>
  7. #include <memory>
  8. #include <utility>
  9. #include "base/values.h"
  10. PrefValueMap::PrefValueMap() {}
  11. PrefValueMap::~PrefValueMap() {}
  12. bool PrefValueMap::GetValue(const std::string& key,
  13. const base::Value** value) const {
  14. auto it = prefs_.find(key);
  15. if (it == prefs_.end())
  16. return false;
  17. if (value)
  18. *value = &it->second;
  19. return true;
  20. }
  21. bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
  22. auto it = prefs_.find(key);
  23. if (it == prefs_.end())
  24. return false;
  25. if (value)
  26. *value = &it->second;
  27. return true;
  28. }
  29. bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
  30. base::Value& existing_value = prefs_[key];
  31. if (value == existing_value)
  32. return false;
  33. existing_value = std::move(value);
  34. return true;
  35. }
  36. bool PrefValueMap::RemoveValue(const std::string& key) {
  37. return prefs_.erase(key) != 0;
  38. }
  39. void PrefValueMap::Clear() {
  40. prefs_.clear();
  41. }
  42. void PrefValueMap::ClearWithPrefix(const std::string& prefix) {
  43. Map::iterator low = prefs_.lower_bound(prefix);
  44. // Appending maximum possible character so that there will be no string with
  45. // prefix |prefix| that we may miss.
  46. Map::iterator high = prefs_.upper_bound(prefix + char(CHAR_MAX));
  47. prefs_.erase(low, high);
  48. }
  49. void PrefValueMap::Swap(PrefValueMap* other) {
  50. prefs_.swap(other->prefs_);
  51. }
  52. PrefValueMap::iterator PrefValueMap::begin() {
  53. return prefs_.begin();
  54. }
  55. PrefValueMap::iterator PrefValueMap::end() {
  56. return prefs_.end();
  57. }
  58. PrefValueMap::const_iterator PrefValueMap::begin() const {
  59. return prefs_.begin();
  60. }
  61. PrefValueMap::const_iterator PrefValueMap::end() const {
  62. return prefs_.end();
  63. }
  64. bool PrefValueMap::empty() const {
  65. return prefs_.empty();
  66. }
  67. bool PrefValueMap::GetBoolean(const std::string& key, bool* value) const {
  68. const base::Value* stored_value = nullptr;
  69. if (GetValue(key, &stored_value) && stored_value->is_bool()) {
  70. *value = stored_value->GetBool();
  71. return true;
  72. }
  73. return false;
  74. }
  75. void PrefValueMap::SetBoolean(const std::string& key, bool value) {
  76. SetValue(key, base::Value(value));
  77. }
  78. bool PrefValueMap::GetString(const std::string& key, std::string* value) const {
  79. const base::Value* stored_value = nullptr;
  80. if (GetValue(key, &stored_value) && stored_value->is_string()) {
  81. *value = stored_value->GetString();
  82. return true;
  83. }
  84. return false;
  85. }
  86. void PrefValueMap::SetString(const std::string& key, const std::string& value) {
  87. SetValue(key, base::Value(value));
  88. }
  89. bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
  90. const base::Value* stored_value = nullptr;
  91. if (GetValue(key, &stored_value) && stored_value->is_int()) {
  92. *value = stored_value->GetInt();
  93. return true;
  94. }
  95. return false;
  96. }
  97. void PrefValueMap::SetInteger(const std::string& key, const int value) {
  98. SetValue(key, base::Value(value));
  99. }
  100. void PrefValueMap::SetDouble(const std::string& key, const double value) {
  101. SetValue(key, base::Value(value));
  102. }
  103. void PrefValueMap::GetDifferingKeys(
  104. const PrefValueMap* other,
  105. std::vector<std::string>* differing_keys) const {
  106. differing_keys->clear();
  107. // Put everything into ordered maps.
  108. std::map<std::string, const base::Value*> this_prefs;
  109. std::map<std::string, const base::Value*> other_prefs;
  110. for (const auto& pair : prefs_)
  111. this_prefs.emplace(pair.first, &pair.second);
  112. for (const auto& pair : other->prefs_)
  113. other_prefs.emplace(pair.first, &pair.second);
  114. // Walk over the maps in lockstep, adding everything that is different.
  115. auto this_pref = this_prefs.begin();
  116. auto other_pref = other_prefs.begin();
  117. while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
  118. const int diff = this_pref->first.compare(other_pref->first);
  119. if (diff == 0) {
  120. if (*this_pref->second != *other_pref->second)
  121. differing_keys->push_back(this_pref->first);
  122. ++this_pref;
  123. ++other_pref;
  124. } else if (diff < 0) {
  125. differing_keys->push_back(this_pref->first);
  126. ++this_pref;
  127. } else if (diff > 0) {
  128. differing_keys->push_back(other_pref->first);
  129. ++other_pref;
  130. }
  131. }
  132. // Add the remaining entries.
  133. for (; this_pref != this_prefs.end(); ++this_pref)
  134. differing_keys->push_back(this_pref->first);
  135. for (; other_pref != other_prefs.end(); ++other_pref)
  136. differing_keys->push_back(other_pref->first);
  137. }
  138. base::Value::Dict PrefValueMap::AsDict() const {
  139. base::Value::Dict dictionary;
  140. for (const auto& value : prefs_)
  141. dictionary.SetByDottedPath(value.first, value.second.Clone());
  142. return dictionary;
  143. }