1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/login/screens/screen_context.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/logging.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace chromeos {
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtemplate <typename StringListType>
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::ListValue* StringListToListValue(const StringListType& list) {
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::ListValue* result = new base::ListValue();
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (typename StringListType::const_iterator it = list.begin();
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       it != list.end();
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       ++it) {
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    result->AppendString(*it);
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return result;
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ScreenContext::ScreenContext() {
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ScreenContext::~ScreenContext() {
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Set(key, new base::FundamentalValue(value));
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::SetInteger(const KeyType& key, int value) {
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Set(key, new base::FundamentalValue(value));
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::SetDouble(const KeyType& key, double value) {
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Set(key, new base::FundamentalValue(value));
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Set(key, new base::StringValue(value));
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Set(key, new base::StringValue(value));
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ScreenContext::SetStringList(const KeyType& key, const StringList& value) {
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Set(key, StringListToListValue(value));
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ScreenContext::SetString16List(const KeyType& key,
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                    const String16List& value) {
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Set(key, StringListToListValue(value));
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ScreenContext::GetBoolean(const KeyType& key) const {
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get<bool>(key);
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ScreenContext::GetBoolean(const KeyType& key, bool default_value) const {
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get(key, default_value);
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint ScreenContext::GetInteger(const KeyType& key) const {
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get<int>(key);
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint ScreenContext::GetInteger(const KeyType& key, int default_value) const {
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get(key, default_value);
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdouble ScreenContext::GetDouble(const KeyType& key) const {
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get<double>(key);
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdouble ScreenContext::GetDouble(const KeyType& key,
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                double default_value) const {
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get(key, default_value);
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string ScreenContext::GetString(const KeyType& key) const {
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get<std::string>(key);
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::string ScreenContext::GetString(const KeyType& key,
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                     const std::string& default_value) const {
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get(key, default_value);
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::string16 ScreenContext::GetString16(const KeyType& key) const {
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get<base::string16>(key);
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::string16 ScreenContext::GetString16(
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const KeyType& key,
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const base::string16& default_value) const {
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Get(key, default_value);
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben MurdochStringList ScreenContext::GetStringList(const KeyType& key) const {
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Get<StringList>(key);
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
110116680a4aac90f2aa7413d9095a592090648e557Ben MurdochStringList ScreenContext::GetStringList(const KeyType& key,
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                        const StringList& default_value) const {
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Get(key, default_value);
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
115116680a4aac90f2aa7413d9095a592090648e557Ben MurdochString16List ScreenContext::GetString16List(const KeyType& key) const {
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return Get<String16List>(key);
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
119116680a4aac90f2aa7413d9095a592090648e557Ben MurdochString16List ScreenContext::GetString16List(
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const KeyType& key,
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const String16List& default_value) const {
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return Get(key, default_value);
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::HasKey(const KeyType& key) const {
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(CalledOnValidThread());
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return storage_.HasKey(key);
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ScreenContext::HasChanges() const {
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(CalledOnValidThread());
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !changes_.empty();
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) {
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(CalledOnValidThread());
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(diff);
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  changes_.Swap(diff);
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  changes_.Clear();
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ScreenContext::ApplyChanges(const base::DictionaryValue& diff,
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                 std::vector<std::string>* keys) {
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(CalledOnValidThread());
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!HasChanges());
1466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (keys) {
1476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    keys->clear();
1486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    keys->reserve(diff.size());
1496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::DictionaryValue::Iterator it(diff);
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  while (!it.IsAtEnd()) {
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Set(it.key(), it.value().DeepCopy());
1536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    if (keys)
1546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      keys->push_back(it.key());
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    it.Advance();
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  changes_.Clear();
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ScreenContext::Set(const KeyType& key, base::Value* value) {
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(CalledOnValidThread());
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(value);
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::Value> new_value(value);
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Value* current_value;
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool in_storage = storage_.Get(key, &current_value);
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (in_storage && new_value->Equals(current_value))
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  changes_.Set(key, new_value->DeepCopy());
173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  storage_.Set(key, new_value.release());
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace chromeos
178