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 COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
6 #define COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/values.h"
18 #include "components/login/base_screen_handler_utils.h"
19 #include "components/login/login_export.h"
23 // ScreenContext is a key-value storage for values that are shared
24 // between C++ and JS sides. Objects of this class should be used in
27 // context.SetString("user", "john");
28 // context.SetInteger("image-index", 0);
29 // context.SetDouble("zoom", 1.25);
30 // context.GetChangesAndReset(&dictionary);
31 // CallJS("onContextChanged", dictionary);
33 // ScreenContext memorizes changed key-value pairs and returns them
34 // via GetChangesAndReset() method. After call to this method an
35 // internal buffer of changes will be cleared.
36 class LOGIN_EXPORT ScreenContext
: public base::NonThreadSafe
{
38 typedef std::string KeyType
;
39 typedef base::Value ValueType
;
44 bool SetBoolean(const KeyType
& key
, bool value
);
45 bool SetInteger(const KeyType
& key
, int value
);
46 bool SetDouble(const KeyType
& key
, double value
);
47 bool SetString(const KeyType
& key
, const std::string
& value
);
48 bool SetString(const KeyType
& key
, const base::string16
& value
);
49 bool SetStringList(const KeyType
& key
, const StringList
& value
);
50 bool SetString16List(const KeyType
& key
, const String16List
& value
);
52 bool GetBoolean(const KeyType
& key
) const;
53 bool GetBoolean(const KeyType
& key
, bool default_value
) const;
54 int GetInteger(const KeyType
& key
) const;
55 int GetInteger(const KeyType
& key
, int default_value
) const;
56 double GetDouble(const KeyType
& key
) const;
57 double GetDouble(const KeyType
& key
, double default_value
) const;
58 std::string
GetString(const KeyType
& key
) const;
59 std::string
GetString(const KeyType
& key
,
60 const std::string
& default_value
) const;
61 base::string16
GetString16(const KeyType
& key
) const;
62 base::string16
GetString16(const KeyType
& key
,
63 const base::string16
& default_value
) const;
64 StringList
GetStringList(const KeyType
& key
) const;
65 StringList
GetStringList(const KeyType
& key
,
66 const StringList
& default_value
) const;
67 String16List
GetString16List(const KeyType
& key
) const;
68 String16List
GetString16List(const KeyType
& key
,
69 const String16List
& default_value
) const;
71 // Copies internal state of |context|.
72 void CopyFrom(ScreenContext
& context
);
74 // Returns true if context has |key|.
75 bool HasKey(const KeyType
& key
) const;
77 // Returns true if there was changes since last call to
78 // GetChangesAndReset().
79 bool HasChanges() const;
81 // Stores all changes since the last call to the
82 // GetChangesAndReset() in |diff|. All previous contents of |diff|
83 // will be thrown away.
84 void GetChangesAndReset(base::DictionaryValue
* diff
);
86 // Applies changes from |diff| to the context. All keys from |diff|
87 // are stored in |keys|. |keys| argument is optional and can be NULL.
88 void ApplyChanges(const base::DictionaryValue
& diff
,
89 std::vector
<std::string
>* keys
);
91 // Returns underlying dictionary containing all the stored data.
92 const base::DictionaryValue
& storage() const { return storage_
; }
95 bool Set(const KeyType
& key
, base::Value
* value
);
98 T
Get(const KeyType
& key
) const {
99 DCHECK(CalledOnValidThread());
100 const base::Value
* value
;
101 bool has_key
= storage_
.Get(key
, &value
);
104 if (!ParseValue(value
, &result
)) {
111 template <typename T
>
112 T
Get(const KeyType
& key
, const T
& default_value
) const {
113 DCHECK(CalledOnValidThread());
115 return default_value
;
119 // Contains current state of <key, value> map.
120 base::DictionaryValue storage_
;
122 // Contains all pending changes.
123 base::DictionaryValue changes_
;
125 DISALLOW_COPY_AND_ASSIGN(ScreenContext
);
130 #endif // COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_