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