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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/containers/hash_tables.h"
     13 #include "base/logging.h"
     14 #include "base/memory/linked_ptr.h"
     15 #include "base/strings/string16.h"
     16 #include "base/threading/non_thread_safe.h"
     17 #include "base/values.h"
     18 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler_utils.h"
     19 
     20 namespace chromeos {
     21 
     22 // ScreenContext is a key-value storage for values that are shared
     23 // between C++ and JS sides. Objects of this class should be used in
     24 // the following way:
     25 //
     26 // context.SetString("user", "john");
     27 // context.SetInteger("image-index", 0);
     28 // context.SetDouble("zoom", 1.25);
     29 // context.GetChangesAndReset(&dictionary);
     30 // CallJS("onContextChanged", dictionary);
     31 //
     32 // ScreenContext memorizes changed key-value pairs and returns them
     33 // via GetChangesAndReset() method. After call to this method an
     34 // internal buffer of changes will be cleared.
     35 class ScreenContext : public base::NonThreadSafe {
     36  public:
     37   typedef std::string KeyType;
     38   typedef base::Value ValueType;
     39 
     40   ScreenContext();
     41   ~ScreenContext();
     42 
     43   bool SetBoolean(const KeyType& key, bool value);
     44   bool SetInteger(const KeyType& key, int value);
     45   bool SetDouble(const KeyType& key, double value);
     46   bool SetString(const KeyType& key, const std::string& value);
     47   bool SetString(const KeyType& key, const base::string16& value);
     48 
     49   bool GetBoolean(const KeyType& key);
     50   bool GetBoolean(const KeyType& key, bool default_value);
     51   int GetInteger(const KeyType& key);
     52   int GetInteger(const KeyType& key, int default_value);
     53   double GetDouble(const KeyType& key);
     54   double GetDouble(const KeyType& key, double default_value);
     55   std::string GetString(const KeyType& key);
     56   std::string GetString(const KeyType& key, const std::string& default_value);
     57   base::string16 GetString16(const KeyType& key);
     58   base::string16 GetString16(const KeyType& key,
     59                              const base::string16& default_value);
     60 
     61   // Returns true if context has |key|.
     62   bool HasKey(const KeyType& key) const;
     63 
     64   // Returns true if there was changes since last call to
     65   // GetChangesAndReset().
     66   bool HasChanges() const;
     67 
     68   // Stores all changes since the last call to the
     69   // GetChangesAndReset() in |diff|.  All previous contents of |diff|
     70   // will be thrown away.
     71   void GetChangesAndReset(DictionaryValue* diff);
     72 
     73   // Applies changes from |diff| to the context. All keys from |diff|
     74   // are stored in |keys|.
     75   void ApplyChanges(const DictionaryValue& diff,
     76                     std::vector<std::string>* keys);
     77 
     78  private:
     79   bool Set(const KeyType& key, Value* value);
     80 
     81   template<typename T>
     82   T Get(const KeyType& key) {
     83     DCHECK(CalledOnValidThread());
     84     const Value* value;
     85     bool has_key = storage_.Get(key, &value);
     86     DCHECK(has_key);
     87     T result;
     88     if (!ParseValue<T>(value, &result)) {
     89       NOTREACHED();
     90       return T();
     91     }
     92     return result;
     93   }
     94 
     95   template<typename T>
     96   T Get(const KeyType& key, const T& default_value) {
     97     DCHECK(CalledOnValidThread());
     98     if (!HasKey(key))
     99       return default_value;
    100     return Get<T>(key);
    101   }
    102 
    103   // Contains current state of <key, value> map.
    104   DictionaryValue storage_;
    105 
    106   // Contains all pending changes.
    107   DictionaryValue changes_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(ScreenContext);
    110 };
    111 
    112 }  // namespace chromeos
    113 
    114 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
    115