Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / base / prefs / pref_value_map.cc
blob93eadb72db46920b6bc4602b9a02c76e3df4998b
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.
5 #include "base/prefs/pref_value_map.h"
7 #include <map>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "base/values.h"
14 PrefValueMap::PrefValueMap() {}
16 PrefValueMap::~PrefValueMap() {}
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;
24 return !!got_value;
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;
32 return !!got_value;
35 bool PrefValueMap::SetValue(const std::string& key,
36 scoped_ptr<base::Value> value) {
37 DCHECK(value);
39 base::Value* old_value = prefs_.get(key);
40 if (old_value && value->Equals(old_value))
41 return false;
43 prefs_.set(key, value.Pass());
44 return true;
47 bool PrefValueMap::RemoveValue(const std::string& key) {
48 return prefs_.erase(key) != 0;
51 void PrefValueMap::Clear() {
52 prefs_.clear();
55 void PrefValueMap::Swap(PrefValueMap* other) {
56 prefs_.swap(other->prefs_);
59 PrefValueMap::iterator PrefValueMap::begin() {
60 return prefs_.begin();
63 PrefValueMap::iterator PrefValueMap::end() {
64 return prefs_.end();
67 PrefValueMap::const_iterator PrefValueMap::begin() const {
68 return prefs_.begin();
71 PrefValueMap::const_iterator PrefValueMap::end() const {
72 return prefs_.end();
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);
81 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
82 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value)));
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);
91 void PrefValueMap::SetString(const std::string& key,
92 const std::string& value) {
93 SetValue(key, make_scoped_ptr(new base::StringValue(value)));
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);
101 void PrefValueMap::SetInteger(const std::string& key, const int value) {
102 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value)));
105 void PrefValueMap::SetDouble(const std::string& key, const double value) {
106 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value)));
109 void PrefValueMap::GetDifferingKeys(
110 const PrefValueMap* other,
111 std::vector<std::string>* differing_keys) const {
112 differing_keys->clear();
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());
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;
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);