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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
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"
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
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);
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 {
37 typedef std::string KeyType;
38 typedef base::Value ValueType;
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);
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;
70 // Returns true if context has |key|.
71 bool HasKey(const KeyType& key) const;
73 // Returns true if there was changes since last call to
74 // GetChangesAndReset().
75 bool HasChanges() const;
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);
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);
87 // Returns underlying dictionary containing all the stored data.
88 const base::DictionaryValue& storage() const { return storage_; }
91 bool Set(const KeyType& key, base::Value* value);
94 T Get(const KeyType& key) const {
95 DCHECK(CalledOnValidThread());
96 const base::Value* value;
97 bool has_key = storage_.Get(key, &value);
100 if (!ParseValue(value, &result)) {
107 template <typename T>
108 T Get(const KeyType& key, const T& default_value) const {
109 DCHECK(CalledOnValidThread());
111 return default_value;
115 // Contains current state of <key, value> map.
116 base::DictionaryValue storage_;
118 // Contains all pending changes.
119 base::DictionaryValue changes_;
121 DISALLOW_COPY_AND_ASSIGN(ScreenContext);
124 } // namespace chromeos
126 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_