OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "base/prefs/pref_value_map.h" |
| 6 |
| 7 #include <map> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "base/values.h" |
| 14 |
| 15 PrefValueMap::PrefValueMap() {} |
| 16 |
| 17 PrefValueMap::~PrefValueMap() {} |
| 18 |
| 19 bool PrefValueMap::GetValue(const std::string& key, |
| 20 const base::Value** value) const { |
| 21 const base::Value* got_value = prefs_.get(key); |
| 22 if (value && got_value) |
| 23 *value = got_value; |
| 24 |
| 25 return !!got_value; |
| 26 } |
| 27 |
| 28 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { |
| 29 base::Value* got_value = prefs_.get(key); |
| 30 if (value && got_value) |
| 31 *value = got_value; |
| 32 |
| 33 return !!got_value; |
| 34 } |
| 35 |
| 36 bool PrefValueMap::SetValue(const std::string& key, |
| 37 scoped_ptr<base::Value> value) { |
| 38 DCHECK(value); |
| 39 |
| 40 base::Value* old_value = prefs_.get(key); |
| 41 if (old_value && value->Equals(old_value)) |
| 42 return false; |
| 43 |
| 44 prefs_.set(key, std::move(value)); |
| 45 return true; |
| 46 } |
| 47 |
| 48 bool PrefValueMap::RemoveValue(const std::string& key) { |
| 49 return prefs_.erase(key) != 0; |
| 50 } |
| 51 |
| 52 void PrefValueMap::Clear() { |
| 53 prefs_.clear(); |
| 54 } |
| 55 |
| 56 void PrefValueMap::Swap(PrefValueMap* other) { |
| 57 prefs_.swap(other->prefs_); |
| 58 } |
| 59 |
| 60 PrefValueMap::iterator PrefValueMap::begin() { |
| 61 return prefs_.begin(); |
| 62 } |
| 63 |
| 64 PrefValueMap::iterator PrefValueMap::end() { |
| 65 return prefs_.end(); |
| 66 } |
| 67 |
| 68 PrefValueMap::const_iterator PrefValueMap::begin() const { |
| 69 return prefs_.begin(); |
| 70 } |
| 71 |
| 72 PrefValueMap::const_iterator PrefValueMap::end() const { |
| 73 return prefs_.end(); |
| 74 } |
| 75 |
| 76 bool PrefValueMap::GetBoolean(const std::string& key, |
| 77 bool* value) const { |
| 78 const base::Value* stored_value = nullptr; |
| 79 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); |
| 80 } |
| 81 |
| 82 void PrefValueMap::SetBoolean(const std::string& key, bool value) { |
| 83 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
| 84 } |
| 85 |
| 86 bool PrefValueMap::GetString(const std::string& key, |
| 87 std::string* value) const { |
| 88 const base::Value* stored_value = nullptr; |
| 89 return GetValue(key, &stored_value) && stored_value->GetAsString(value); |
| 90 } |
| 91 |
| 92 void PrefValueMap::SetString(const std::string& key, |
| 93 const std::string& value) { |
| 94 SetValue(key, make_scoped_ptr(new base::StringValue(value))); |
| 95 } |
| 96 |
| 97 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { |
| 98 const base::Value* stored_value = nullptr; |
| 99 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); |
| 100 } |
| 101 |
| 102 void PrefValueMap::SetInteger(const std::string& key, const int value) { |
| 103 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
| 104 } |
| 105 |
| 106 void PrefValueMap::SetDouble(const std::string& key, const double value) { |
| 107 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
| 108 } |
| 109 |
| 110 void PrefValueMap::GetDifferingKeys( |
| 111 const PrefValueMap* other, |
| 112 std::vector<std::string>* differing_keys) const { |
| 113 differing_keys->clear(); |
| 114 |
| 115 // Put everything into ordered maps. |
| 116 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); |
| 117 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), |
| 118 other->prefs_.end()); |
| 119 |
| 120 // Walk over the maps in lockstep, adding everything that is different. |
| 121 auto this_pref(this_prefs.begin()); |
| 122 auto other_pref(other_prefs.begin()); |
| 123 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) { |
| 124 const int diff = this_pref->first.compare(other_pref->first); |
| 125 if (diff == 0) { |
| 126 if (!this_pref->second->Equals(other_pref->second)) |
| 127 differing_keys->push_back(this_pref->first); |
| 128 ++this_pref; |
| 129 ++other_pref; |
| 130 } else if (diff < 0) { |
| 131 differing_keys->push_back(this_pref->first); |
| 132 ++this_pref; |
| 133 } else if (diff > 0) { |
| 134 differing_keys->push_back(other_pref->first); |
| 135 ++other_pref; |
| 136 } |
| 137 } |
| 138 |
| 139 // Add the remaining entries. |
| 140 for ( ; this_pref != this_prefs.end(); ++this_pref) |
| 141 differing_keys->push_back(this_pref->first); |
| 142 for ( ; other_pref != other_prefs.end(); ++other_pref) |
| 143 differing_keys->push_back(other_pref->first); |
| 144 } |
OLD | NEW |