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
);
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
);
61 // Returns true if context has |key|.
62 bool HasKey(const KeyType
& key
) const;
64 // Returns true if there was changes since last call to
65 // GetChangesAndReset().
66 bool HasChanges() const;
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(base::DictionaryValue
* diff
);
73 // Applies changes from |diff| to the context. All keys from |diff|
74 // are stored in |keys|.
75 void ApplyChanges(const base::DictionaryValue
& diff
,
76 std::vector
<std::string
>* keys
);
79 bool Set(const KeyType
& key
, base::Value
* value
);
82 T
Get(const KeyType
& key
) {
83 DCHECK(CalledOnValidThread());
84 const base::Value
* value
;
85 bool has_key
= storage_
.Get(key
, &value
);
88 if (!ParseValue
<T
>(value
, &result
)) {
96 T
Get(const KeyType
& key
, const T
& default_value
) {
97 DCHECK(CalledOnValidThread());
103 // Contains current state of <key, value> map.
104 base::DictionaryValue storage_
;
106 // Contains all pending changes.
107 base::DictionaryValue changes_
;
109 DISALLOW_COPY_AND_ASSIGN(ScreenContext
);
112 } // namespace chromeos
114 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_SCREEN_CONTEXT_H_