screen_context.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#include "chrome/browser/chromeos/login/screens/screen_context.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9
10namespace chromeos {
11
12namespace {
13
14template <typename StringListType>
15base::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);
21  }
22  return result;
23}
24
25}  // namespace
26
27ScreenContext::ScreenContext() {
28}
29
30ScreenContext::~ScreenContext() {
31}
32
33bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
34  return Set(key, new base::FundamentalValue(value));
35}
36
37bool ScreenContext::SetInteger(const KeyType& key, int value) {
38  return Set(key, new base::FundamentalValue(value));
39}
40
41bool ScreenContext::SetDouble(const KeyType& key, double value) {
42  return Set(key, new base::FundamentalValue(value));
43}
44
45bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
46  return Set(key, new base::StringValue(value));
47}
48
49bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
50  return Set(key, new base::StringValue(value));
51}
52
53bool ScreenContext::SetStringList(const KeyType& key, const StringList& value) {
54  return Set(key, StringListToListValue(value));
55}
56
57bool ScreenContext::SetString16List(const KeyType& key,
58                                    const String16List& value) {
59  return Set(key, StringListToListValue(value));
60}
61
62bool ScreenContext::GetBoolean(const KeyType& key) const {
63  return Get<bool>(key);
64}
65
66bool ScreenContext::GetBoolean(const KeyType& key, bool default_value) const {
67  return Get(key, default_value);
68}
69
70int ScreenContext::GetInteger(const KeyType& key) const {
71  return Get<int>(key);
72}
73
74int ScreenContext::GetInteger(const KeyType& key, int default_value) const {
75  return Get(key, default_value);
76}
77
78double ScreenContext::GetDouble(const KeyType& key) const {
79  return Get<double>(key);
80}
81
82double ScreenContext::GetDouble(const KeyType& key,
83                                double default_value) const {
84  return Get(key, default_value);
85}
86
87std::string ScreenContext::GetString(const KeyType& key) const {
88  return Get<std::string>(key);
89}
90
91std::string ScreenContext::GetString(const KeyType& key,
92                                     const std::string& default_value) const {
93  return Get(key, default_value);
94}
95
96base::string16 ScreenContext::GetString16(const KeyType& key) const {
97  return Get<base::string16>(key);
98}
99
100base::string16 ScreenContext::GetString16(
101    const KeyType& key,
102    const base::string16& default_value) const {
103  return Get(key, default_value);
104}
105
106StringList ScreenContext::GetStringList(const KeyType& key) const {
107  return Get<StringList>(key);
108}
109
110StringList ScreenContext::GetStringList(const KeyType& key,
111                                        const StringList& default_value) const {
112  return Get(key, default_value);
113}
114
115String16List ScreenContext::GetString16List(const KeyType& key) const {
116  return Get<String16List>(key);
117}
118
119String16List ScreenContext::GetString16List(
120    const KeyType& key,
121    const String16List& default_value) const {
122  return Get(key, default_value);
123}
124
125bool ScreenContext::HasKey(const KeyType& key) const {
126  DCHECK(CalledOnValidThread());
127  return storage_.HasKey(key);
128}
129
130bool ScreenContext::HasChanges() const {
131  DCHECK(CalledOnValidThread());
132  return !changes_.empty();
133}
134
135void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) {
136  DCHECK(CalledOnValidThread());
137  DCHECK(diff);
138  changes_.Swap(diff);
139  changes_.Clear();
140}
141
142void ScreenContext::ApplyChanges(const base::DictionaryValue& diff,
143                                 std::vector<std::string>* keys) {
144  DCHECK(CalledOnValidThread());
145  DCHECK(!HasChanges());
146  if (keys) {
147    keys->clear();
148    keys->reserve(diff.size());
149  }
150  base::DictionaryValue::Iterator it(diff);
151  while (!it.IsAtEnd()) {
152    Set(it.key(), it.value().DeepCopy());
153    if (keys)
154      keys->push_back(it.key());
155    it.Advance();
156  }
157  changes_.Clear();
158}
159
160bool ScreenContext::Set(const KeyType& key, base::Value* value) {
161  DCHECK(CalledOnValidThread());
162  DCHECK(value);
163  scoped_ptr<base::Value> new_value(value);
164
165  base::Value* current_value;
166  bool in_storage = storage_.Get(key, &current_value);
167
168  // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
169  if (in_storage && new_value->Equals(current_value))
170    return false;
171
172  changes_.Set(key, new_value->DeepCopy());
173  storage_.Set(key, new_value.release());
174  return true;
175}
176
177}  // namespace chromeos
178