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