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