15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/prefs/pref_value_map.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::PrefValueMap() {}
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::~PrefValueMap() {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Clear();
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool PrefValueMap::GetValue(const std::string& key,
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            const base::Value** value) const {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Map::const_iterator entry = prefs_.find(key);
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (entry != prefs_.end()) {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *value = entry->second;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Map::const_iterator entry = prefs_.find(key);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (entry != prefs_.end()) {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *value = entry->second;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool PrefValueMap::SetValue(const std::string& key, base::Value* value) {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(value);
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_ptr<base::Value> value_ptr(value);
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Map::iterator entry = prefs_.find(key);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (entry != prefs_.end()) {
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (base::Value::Equals(entry->second, value))
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete entry->second;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    entry->second = value_ptr.release();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs_[key] = value_ptr.release();
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PrefValueMap::RemoveValue(const std::string& key) {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Map::iterator entry = prefs_.find(key);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (entry != prefs_.end()) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete entry->second;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs_.erase(entry);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::Clear() {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  STLDeleteValues(&prefs_);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs_.clear();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::Swap(PrefValueMap* other) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs_.swap(other->prefs_);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::iterator PrefValueMap::begin() {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs_.begin();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::iterator PrefValueMap::end() {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs_.end();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::const_iterator PrefValueMap::begin() const {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs_.begin();
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefValueMap::const_iterator PrefValueMap::end() const {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs_.end();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PrefValueMap::GetBoolean(const std::string& key,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool* value) const {
957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const base::Value* stored_value = NULL;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::SetBoolean(const std::string& key, bool value) {
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetValue(key, new base::FundamentalValue(value));
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PrefValueMap::GetString(const std::string& key,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             std::string* value) const {
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const base::Value* stored_value = NULL;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetValue(key, &stored_value) && stored_value->GetAsString(value);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::SetString(const std::string& key,
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const std::string& value) {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetValue(key, new base::StringValue(value));
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const base::Value* stored_value = NULL;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::SetInteger(const std::string& key, const int value) {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetValue(key, new base::FundamentalValue(value));
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PrefValueMap::SetDouble(const std::string& key, const double value) {
1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SetValue(key, new base::FundamentalValue(value));
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrefValueMap::GetDifferingKeys(
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PrefValueMap* other,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<std::string>* differing_keys) const {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  differing_keys->clear();
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Walk over the maps in lockstep, adding everything that is different.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Map::const_iterator this_pref(prefs_.begin());
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Map::const_iterator other_pref(other->prefs_.begin());
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (this_pref != prefs_.end() && other_pref != other->prefs_.end()) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int diff = this_pref->first.compare(other_pref->first);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (diff == 0) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!this_pref->second->Equals(other_pref->second))
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        differing_keys->push_back(this_pref->first);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++this_pref;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++other_pref;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (diff < 0) {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      differing_keys->push_back(this_pref->first);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++this_pref;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (diff > 0) {
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      differing_keys->push_back(other_pref->first);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++other_pref;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add the remaining entries.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for ( ; this_pref != prefs_.end(); ++this_pref)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      differing_keys->push_back(this_pref->first);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for ( ; other_pref != other->prefs_.end(); ++other_pref)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      differing_keys->push_back(other_pref->first);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
157