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   bool SetStringList(const KeyType& key, const StringList& value);
     49   bool SetString16List(const KeyType& key, const String16List& value);
     50 
     51   bool GetBoolean(const KeyType& key) const;
     52   bool GetBoolean(const KeyType& key, bool default_value) const;
     53   int GetInteger(const KeyType& key) const;
     54   int GetInteger(const KeyType& key, int default_value) const;
     55   double GetDouble(const KeyType& key) const;
     56   double GetDouble(const KeyType& key, double default_value) const;
     57   std::string GetString(const KeyType& key) const;
     58   std::string GetString(const KeyType& key,
     59                         const std::string& default_value) const;
     60   base::string16 GetString16(const KeyType& key) const;
     61   base::string16 GetString16(const KeyType& key,
     62                              const base::string16& default_value) const;
     63   StringList GetStringList(const KeyType& key) const;
     64   StringList GetStringList(const KeyType& key,
     65                            const StringList& default_value) const;
     66   String16List GetString16List(const KeyType& key) const;
     67   String16List GetString16List(const KeyType& key,
     68                                const String16List& default_value) const;
     69 
     70   // Returns true if context has |key|.
     71   bool HasKey(const KeyType& key) const;
     72 
     73   // Returns true if there was changes since last call to
     74   // GetChangesAndReset().
     75   bool HasChanges() const;
     76 
     77   // Stores all changes since the last call to the
     78   // GetChangesAndReset() in |diff|.  All previous contents of |diff|
     79   // will be thrown away.
     80   void GetChangesAndReset(base::DictionaryValue* diff);
     81 
     82   // Applies changes from |diff| to the context. All keys from |diff|
     83   // are stored in |keys|. |keys| argument is optional and can be NULL.
     84   void ApplyChanges(const base::DictionaryValue& diff,
     85                     std::vector<std::string>* keys);
     86 
     87   // Returns underlying dictionary containing all the stored data.
     88   const base::DictionaryValue& storage() const { return storage_; }
     89 
     90  private:
     91   bool Set(const KeyType& key, base::Value* value);
     92 
     93   template <typename T>
     94   T Get(const KeyType& key) const {
     95     DCHECK(CalledOnValidThread());
     96     const base::Value* value;
     97     bool has_key = storage_.Get(key, &value);
     98     DCHECK(has_key);
     99     T result;
    100     if (!ParseValue(value, &result)) {
    101       NOTREACHED();
    102       return T();
    103     }
    104     return result;
    105   }
    106 
    107   template <typename T>
    108   T Get(const KeyType& key, const T& default_value) const {
    109     DCHECK(CalledOnValidThread());
    110     if (!HasKey(key))
    111       return default_value;
    112     return Get<T>(key);
    113   }
    114 
    115   // Contains current state of <key, value> map.
    116   base::DictionaryValue storage_;
    117 
    118   // Contains all pending changes.
    119   base::DictionaryValue changes_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(ScreenContext);
    122 };
    123 
    124 }  // namespace chromeos
    125 
    126 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
    127