Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / login / screens / screen_context.cc
blobfa253fc00e458b9ab206d5aa954ff9ee23be7735
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"
10 namespace login {
12 namespace {
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();
18 it != list.end();
19 ++it) {
20 result->AppendString(*it);
22 return result;
25 } // namespace
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 {
71 return Get<int>(key);
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(
101 const KeyType& key,
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(
120 const KeyType& key,
121 const String16List& default_value) const {
122 return Get(key, default_value);
125 void ScreenContext::CopyFrom(ScreenContext& context) {
126 scoped_ptr<base::DictionaryValue> storage(context.storage_.DeepCopy());
127 scoped_ptr<base::DictionaryValue> changes(context.changes_.DeepCopy());
128 storage_.Swap(storage.get());
129 changes_.Swap(changes.get());
132 bool ScreenContext::HasKey(const KeyType& key) const {
133 DCHECK(CalledOnValidThread());
134 return storage_.HasKey(key);
137 bool ScreenContext::HasChanges() const {
138 DCHECK(CalledOnValidThread());
139 return !changes_.empty();
142 void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) {
143 DCHECK(CalledOnValidThread());
144 DCHECK(diff);
145 changes_.Swap(diff);
146 changes_.Clear();
149 void ScreenContext::ApplyChanges(const base::DictionaryValue& diff,
150 std::vector<std::string>* keys) {
151 DCHECK(CalledOnValidThread());
152 DCHECK(!HasChanges());
153 if (keys) {
154 keys->clear();
155 keys->reserve(diff.size());
158 for (base::DictionaryValue::Iterator it(diff); !it.IsAtEnd(); it.Advance()) {
159 Set(it.key(), it.value().DeepCopy());
160 if (keys)
161 keys->push_back(it.key());
163 changes_.Clear();
166 bool ScreenContext::Set(const KeyType& key, base::Value* value) {
167 DCHECK(CalledOnValidThread());
168 DCHECK(value);
169 scoped_ptr<base::Value> new_value(value);
171 base::Value* current_value;
172 bool in_storage = storage_.Get(key, &current_value);
174 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
175 if (in_storage && new_value->Equals(current_value))
176 return false;
178 changes_.Set(key, new_value->DeepCopy());
179 storage_.Set(key, new_value.release());
180 return true;
183 } // namespace login