1 // Copyright (c) 2013 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 "components/login/screens/screen_context.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
14 template <typename StringListType>
15 base::ListValue* StringListToListValue(const StringListType& list) {
16 base::ListValue* result = new base::ListValue();
17 for (typename StringListType::const_iterator it = list.begin();
20 result->AppendString(*it);
27 ScreenContext::ScreenContext() {
30 ScreenContext::~ScreenContext() {
33 bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
34 return Set(key, new base::FundamentalValue(value));
37 bool ScreenContext::SetInteger(const KeyType& key, int value) {
38 return Set(key, new base::FundamentalValue(value));
41 bool ScreenContext::SetDouble(const KeyType& key, double value) {
42 return Set(key, new base::FundamentalValue(value));
45 bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
46 return Set(key, new base::StringValue(value));
49 bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
50 return Set(key, new base::StringValue(value));
53 bool ScreenContext::SetStringList(const KeyType& key, const StringList& value) {
54 return Set(key, StringListToListValue(value));
57 bool ScreenContext::SetString16List(const KeyType& key,
58 const String16List& value) {
59 return Set(key, StringListToListValue(value));
62 bool ScreenContext::GetBoolean(const KeyType& key) const {
63 return Get<bool>(key);
66 bool ScreenContext::GetBoolean(const KeyType& key, bool default_value) const {
67 return Get(key, default_value);
70 int ScreenContext::GetInteger(const KeyType& key) const {
74 int ScreenContext::GetInteger(const KeyType& key, int default_value) const {
75 return Get(key, default_value);
78 double ScreenContext::GetDouble(const KeyType& key) const {
79 return Get<double>(key);
82 double ScreenContext::GetDouble(const KeyType& key,
83 double default_value) const {
84 return Get(key, default_value);
87 std::string ScreenContext::GetString(const KeyType& key) const {
88 return Get<std::string>(key);
91 std::string ScreenContext::GetString(const KeyType& key,
92 const std::string& default_value) const {
93 return Get(key, default_value);
96 base::string16 ScreenContext::GetString16(const KeyType& key) const {
97 return Get<base::string16>(key);
100 base::string16 ScreenContext::GetString16(
102 const base::string16& default_value) const {
103 return Get(key, default_value);
106 StringList ScreenContext::GetStringList(const KeyType& key) const {
107 return Get<StringList>(key);
110 StringList ScreenContext::GetStringList(const KeyType& key,
111 const StringList& default_value) const {
112 return Get(key, default_value);
115 String16List ScreenContext::GetString16List(const KeyType& key) const {
116 return Get<String16List>(key);
119 String16List ScreenContext::GetString16List(
121 const String16List& default_value) const {
122 return Get(key, default_value);
125 bool ScreenContext::HasKey(const KeyType& key) const {
126 DCHECK(CalledOnValidThread());
127 return storage_.HasKey(key);
130 bool ScreenContext::HasChanges() const {
131 DCHECK(CalledOnValidThread());
132 return !changes_.empty();
135 void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) {
136 DCHECK(CalledOnValidThread());
142 void ScreenContext::ApplyChanges(const base::DictionaryValue& diff,
143 std::vector<std::string>* keys) {
144 DCHECK(CalledOnValidThread());
145 DCHECK(!HasChanges());
148 keys->reserve(diff.size());
150 base::DictionaryValue::Iterator it(diff);
151 while (!it.IsAtEnd()) {
152 Set(it.key(), it.value().DeepCopy());
154 keys->push_back(it.key());
160 bool ScreenContext::Set(const KeyType& key, base::Value* value) {
161 DCHECK(CalledOnValidThread());
163 scoped_ptr<base::Value> new_value(value);
165 base::Value* current_value;
166 bool in_storage = storage_.Get(key, ¤t_value);
168 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
169 if (in_storage && new_value->Equals(current_value))
172 changes_.Set(key, new_value->DeepCopy());
173 storage_.Set(key, new_value.release());