pref_value_map.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/scoped_ptr.h"
9#include "base/stl_util-inl.h"
10#include "base/values.h"
11
12PrefValueMap::PrefValueMap() {}
13
14PrefValueMap::~PrefValueMap() {
15  Clear();
16}
17
18bool PrefValueMap::GetValue(const std::string& key, 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
29bool PrefValueMap::SetValue(const std::string& key, Value* value) {
30  DCHECK(value);
31  scoped_ptr<Value> value_ptr(value);
32  const Map::iterator entry = prefs_.find(key);
33  if (entry != prefs_.end()) {
34    if (Value::Equals(entry->second, value))
35      return false;
36    delete entry->second;
37    entry->second = value_ptr.release();
38  } else {
39    prefs_[key] = value_ptr.release();
40  }
41
42  return true;
43}
44
45bool PrefValueMap::RemoveValue(const std::string& key) {
46  const Map::iterator entry = prefs_.find(key);
47  if (entry != prefs_.end()) {
48    delete entry->second;
49    prefs_.erase(entry);
50    return true;
51  }
52
53  return false;
54}
55
56void PrefValueMap::Clear() {
57  STLDeleteValues(&prefs_);
58  prefs_.clear();
59}
60
61bool PrefValueMap::GetBoolean(const std::string& key,
62                              bool* value) const {
63  Value* stored_value = NULL;
64  return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
65}
66
67bool PrefValueMap::GetString(const std::string& key,
68                             std::string* value) const {
69  Value* stored_value = NULL;
70  return GetValue(key, &stored_value) && stored_value->GetAsString(value);
71}
72
73void PrefValueMap::SetString(const std::string& key,
74                             const std::string& value) {
75  SetValue(key, Value::CreateStringValue(value));
76}
77
78void PrefValueMap::GetDifferingKeys(
79    const PrefValueMap* other,
80    std::vector<std::string>* differing_keys) const {
81  differing_keys->clear();
82
83  // Walk over the maps in lockstep, adding everything that is different.
84  Map::const_iterator this_pref(prefs_.begin());
85  Map::const_iterator other_pref(other->prefs_.begin());
86  while (this_pref != prefs_.end() && other_pref != other->prefs_.end()) {
87    const int diff = this_pref->first.compare(other_pref->first);
88    if (diff == 0) {
89      if (!this_pref->second->Equals(other_pref->second))
90        differing_keys->push_back(this_pref->first);
91      ++this_pref;
92      ++other_pref;
93    } else if (diff < 0) {
94      differing_keys->push_back(this_pref->first);
95      ++this_pref;
96    } else if (diff > 0) {
97      differing_keys->push_back(other_pref->first);
98      ++other_pref;
99    }
100  }
101
102  // Add the remaining entries.
103  for ( ; this_pref != prefs_.end(); ++this_pref)
104      differing_keys->push_back(this_pref->first);
105  for ( ; other_pref != other->prefs_.end(); ++other_pref)
106      differing_keys->push_back(other_pref->first);
107}
108