screen_context.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
12ScreenContext::ScreenContext() {
13}
14
15ScreenContext::~ScreenContext() {
16}
17
18bool ScreenContext::SetBoolean(const KeyType& key, bool value) {
19  return Set(key, new base::FundamentalValue(value));
20}
21
22bool ScreenContext::SetInteger(const KeyType& key, int value) {
23  return Set(key, new base::FundamentalValue(value));
24}
25
26bool ScreenContext::SetDouble(const KeyType& key, double value) {
27  return Set(key, new base::FundamentalValue(value));
28}
29
30bool ScreenContext::SetString(const KeyType& key, const std::string& value) {
31  return Set(key, new base::StringValue(value));
32}
33
34bool ScreenContext::SetString(const KeyType& key, const base::string16& value) {
35  return Set(key, new base::StringValue(value));
36}
37
38bool ScreenContext::GetBoolean(const KeyType& key) {
39  return Get<bool>(key);
40}
41
42bool ScreenContext::GetBoolean(const KeyType& key, bool default_value) {
43  return Get(key, default_value);
44}
45
46int ScreenContext::GetInteger(const KeyType& key) {
47  return Get<int>(key);
48}
49
50int ScreenContext::GetInteger(const KeyType& key, int default_value) {
51  return Get(key, default_value);
52}
53
54double ScreenContext::GetDouble(const KeyType& key) {
55  return Get<double>(key);
56}
57
58double ScreenContext::GetDouble(const KeyType& key, double default_value) {
59  return Get(key, default_value);
60}
61
62std::string ScreenContext::GetString(const KeyType& key) {
63  return Get<std::string>(key);
64}
65
66std::string ScreenContext::GetString(const KeyType& key,
67                                     const std::string& default_value) {
68  return Get(key, default_value);
69}
70
71base::string16 ScreenContext::GetString16(const KeyType& key) {
72  return Get<base::string16>(key);
73}
74
75base::string16 ScreenContext::GetString16(const KeyType& key,
76                                          const base::string16& default_value) {
77  return Get(key, default_value);
78}
79
80bool ScreenContext::HasKey(const KeyType& key) const {
81  DCHECK(CalledOnValidThread());
82  return storage_.HasKey(key);
83}
84
85bool ScreenContext::HasChanges() const {
86  DCHECK(CalledOnValidThread());
87  return !changes_.empty();
88}
89
90void ScreenContext::GetChangesAndReset(DictionaryValue* diff) {
91  DCHECK(CalledOnValidThread());
92  DCHECK(diff);
93  changes_.Swap(diff);
94  changes_.Clear();
95}
96
97void 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
113bool 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, &current_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