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 "chrome/browser/chromeos/login/screens/screen_context.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
12 ScreenContext::ScreenContext() {
15 ScreenContext::~ScreenContext() {
18 bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
19 return Set(key, new base::FundamentalValue(value));
22 bool ScreenContext::SetInteger(const KeyType& key, int value) {
23 return Set(key, new base::FundamentalValue(value));
26 bool ScreenContext::SetDouble(const KeyType& key, double value) {
27 return Set(key, new base::FundamentalValue(value));
30 bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
31 return Set(key, new base::StringValue(value));
34 bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
35 return Set(key, new base::StringValue(value));
38 bool ScreenContext::GetBoolean(const KeyType& key) {
39 return Get<bool>(key);
42 bool ScreenContext::GetBoolean(const KeyType& key, bool default_value) {
43 return Get(key, default_value);
46 int ScreenContext::GetInteger(const KeyType& key) {
50 int ScreenContext::GetInteger(const KeyType& key, int default_value) {
51 return Get(key, default_value);
54 double ScreenContext::GetDouble(const KeyType& key) {
55 return Get<double>(key);
58 double ScreenContext::GetDouble(const KeyType& key, double default_value) {
59 return Get(key, default_value);
62 std::string ScreenContext::GetString(const KeyType& key) {
63 return Get<std::string>(key);
66 std::string ScreenContext::GetString(const KeyType& key,
67 const std::string& default_value) {
68 return Get(key, default_value);
71 base::string16 ScreenContext::GetString16(const KeyType& key) {
72 return Get<base::string16>(key);
75 base::string16 ScreenContext::GetString16(const KeyType& key,
76 const base::string16& default_value) {
77 return Get(key, default_value);
80 bool ScreenContext::HasKey(const KeyType& key) const {
81 DCHECK(CalledOnValidThread());
82 return storage_.HasKey(key);
85 bool ScreenContext::HasChanges() const {
86 DCHECK(CalledOnValidThread());
87 return !changes_.empty();
90 void ScreenContext::GetChangesAndReset(DictionaryValue* diff) {
91 DCHECK(CalledOnValidThread());
97 void ScreenContext::ApplyChanges(const DictionaryValue& diff,
98 std::vector<std::string>* keys) {
99 DCHECK(CalledOnValidThread());
100 DCHECK(!HasChanges());
103 keys->reserve(diff.size());
104 base::DictionaryValue::Iterator it(diff);
105 while (!it.IsAtEnd()) {
106 Set(it.key(), it.value().DeepCopy());
107 keys->push_back(it.key());
113 bool ScreenContext::Set(const KeyType& key, Value* value) {
114 DCHECK(CalledOnValidThread());
116 scoped_ptr<Value> new_value(value);
118 Value* current_value;
119 bool in_storage = storage_.Get(key, ¤t_value);
121 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
122 if (in_storage && new_value->Equals(current_value))
125 changes_.Set(key, new_value->DeepCopy());
126 storage_.Set(key, new_value.release());
130 } // namespace chromeos