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
10 namespace chromeos {
11
ScreenContext()12 ScreenContext::ScreenContext() {
13 }
14
~ScreenContext()15 ScreenContext::~ScreenContext() {
16 }
17
SetBoolean(const KeyType & key,bool value)18 bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
19 return Set(key, new base::FundamentalValue(value));
20 }
21
SetInteger(const KeyType & key,int value)22 bool ScreenContext::SetInteger(const KeyType& key, int value) {
23 return Set(key, new base::FundamentalValue(value));
24 }
25
SetDouble(const KeyType & key,double value)26 bool ScreenContext::SetDouble(const KeyType& key, double value) {
27 return Set(key, new base::FundamentalValue(value));
28 }
29
SetString(const KeyType & key,const std::string & value)30 bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
31 return Set(key, new base::StringValue(value));
32 }
33
SetString(const KeyType & key,const base::string16 & value)34 bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
35 return Set(key, new base::StringValue(value));
36 }
37
GetBoolean(const KeyType & key)38 bool ScreenContext::GetBoolean(const KeyType& key) {
39 return Get<bool>(key);
40 }
41
GetBoolean(const KeyType & key,bool default_value)42 bool ScreenContext::GetBoolean(const KeyType& key, bool default_value) {
43 return Get(key, default_value);
44 }
45
GetInteger(const KeyType & key)46 int ScreenContext::GetInteger(const KeyType& key) {
47 return Get<int>(key);
48 }
49
GetInteger(const KeyType & key,int default_value)50 int ScreenContext::GetInteger(const KeyType& key, int default_value) {
51 return Get(key, default_value);
52 }
53
GetDouble(const KeyType & key)54 double ScreenContext::GetDouble(const KeyType& key) {
55 return Get<double>(key);
56 }
57
GetDouble(const KeyType & key,double default_value)58 double ScreenContext::GetDouble(const KeyType& key, double default_value) {
59 return Get(key, default_value);
60 }
61
GetString(const KeyType & key)62 std::string ScreenContext::GetString(const KeyType& key) {
63 return Get<std::string>(key);
64 }
65
GetString(const KeyType & key,const std::string & default_value)66 std::string ScreenContext::GetString(const KeyType& key,
67 const std::string& default_value) {
68 return Get(key, default_value);
69 }
70
GetString16(const KeyType & key)71 base::string16 ScreenContext::GetString16(const KeyType& key) {
72 return Get<base::string16>(key);
73 }
74
GetString16(const KeyType & key,const base::string16 & default_value)75 base::string16 ScreenContext::GetString16(const KeyType& key,
76 const base::string16& default_value) {
77 return Get(key, default_value);
78 }
79
HasKey(const KeyType & key) const80 bool ScreenContext::HasKey(const KeyType& key) const {
81 DCHECK(CalledOnValidThread());
82 return storage_.HasKey(key);
83 }
84
HasChanges() const85 bool ScreenContext::HasChanges() const {
86 DCHECK(CalledOnValidThread());
87 return !changes_.empty();
88 }
89
GetChangesAndReset(DictionaryValue * diff)90 void ScreenContext::GetChangesAndReset(DictionaryValue* diff) {
91 DCHECK(CalledOnValidThread());
92 DCHECK(diff);
93 changes_.Swap(diff);
94 changes_.Clear();
95 }
96
ApplyChanges(const DictionaryValue & diff,std::vector<std::string> * keys)97 void ScreenContext::ApplyChanges(const DictionaryValue& diff,
98 std::vector<std::string>* keys) {
99 DCHECK(CalledOnValidThread());
100 DCHECK(!HasChanges());
101 DCHECK(keys);
102 keys->clear();
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());
108 it.Advance();
109 }
110 changes_.Clear();
111 }
112
Set(const KeyType & key,Value * value)113 bool ScreenContext::Set(const KeyType& key, Value* value) {
114 DCHECK(CalledOnValidThread());
115 DCHECK(value);
116 scoped_ptr<Value> new_value(value);
117
118 Value* current_value;
119 bool in_storage = storage_.Get(key, ¤t_value);
120
121 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair.
122 if (in_storage && new_value->Equals(current_value))
123 return false;
124
125 changes_.Set(key, new_value->DeepCopy());
126 storage_.Set(key, new_value.release());
127 return true;
128 }
129
130 } // namespace chromeos
131