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 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());
149 void ScreenContext::ApplyChanges(const base::DictionaryValue
& diff
,
150 std::vector
<std::string
>* keys
) {
151 DCHECK(CalledOnValidThread());
152 DCHECK(!HasChanges());
155 keys
->reserve(diff
.size());
158 for (base::DictionaryValue::Iterator
it(diff
); !it
.IsAtEnd(); it
.Advance()) {
159 Set(it
.key(), it
.value().DeepCopy());
161 keys
->push_back(it
.key());
166 bool ScreenContext::Set(const KeyType
& key
, base::Value
* value
) {
167 DCHECK(CalledOnValidThread());
169 scoped_ptr
<base::Value
> new_value(value
);
171 base::Value
* current_value
;
172 bool in_storage
= storage_
.Get(key
, ¤t_value
);
174 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
175 if (in_storage
&& new_value
->Equals(current_value
))
178 changes_
.Set(key
, new_value
->DeepCopy());
179 storage_
.Set(key
, new_value
.release());