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