1 // Copyright (c) 2012 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_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler_utils.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_message_handler.h"
17 #include "ui/gfx/native_widget_types.h"
20 class DictionaryValue
;
27 // Class that collects Localized Values for translation.
28 class LocalizedValuesBuilder
{
30 explicit LocalizedValuesBuilder(base::DictionaryValue
* dict
);
31 // Method to declare localized value. |key| is the i18n key used in html.
32 // |message| is text of the message.
33 void Add(const std::string
& key
, const std::string
& message
);
35 // Method to declare localized value. |key| is the i18n key used in html.
36 // |message| is text of the message.
37 void Add(const std::string
& key
, const base::string16
& message
);
39 // Method to declare localized value. |key| is the i18n key used in html.
40 // |message_id| is a resource id of message.
41 void Add(const std::string
& key
, int message_id
);
43 // Method to declare localized value. |key| is the i18n key used in html.
44 // |message_id| is a resource id of message. Message is expected to have
45 // one format parameter subsituted by |a|.
46 void AddF(const std::string
& key
,
48 const base::string16
& a
);
50 // Method to declare localized value. |key| is the i18n key used in html.
51 // |message_id| is a resource id of message. Message is expected to have
52 // two format parameters subsituted by |a| and |b| respectively.
53 void AddF(const std::string
& key
,
55 const base::string16
& a
,
56 const base::string16
& b
);
58 // Method to declare localized value. |key| is the i18n key used in html.
59 // |message_id| is a resource id of message. Message is expected to have
60 // one format parameter subsituted by resource identified by |message_id_a|.
61 void AddF(const std::string
& key
,
65 // Method to declare localized value. |key| is the i18n key used in html.
66 // |message_id| is a resource id of message. Message is expected to have
67 // two format parameters subsituted by resource identified by |message_id_a|
68 // and |message_id_b| respectively.
69 void AddF(const std::string
& key
,
75 base::DictionaryValue
* dict_
;
78 // Base class for the OOBE/Login WebUI handlers.
79 class BaseScreenHandler
: public content::WebUIMessageHandler
{
81 // C-tor used when JS screen prefix is not needed.
84 // C-tor used when JS screen prefix is needed.
85 explicit BaseScreenHandler(const std::string
& js_screen_path
);
87 virtual ~BaseScreenHandler();
89 // Gets localized strings to be used on the page.
90 void GetLocalizedStrings(
91 base::DictionaryValue
* localized_strings
);
93 // This method is called when page is ready. It propagates to inherited class
94 // via virtual Initialize() method (see below).
95 void InitializeBase();
97 void set_async_assets_load_id(const std::string
& async_assets_load_id
) {
98 async_assets_load_id_
= async_assets_load_id
;
100 const std::string
& async_assets_load_id() const {
101 return async_assets_load_id_
;
105 // All subclasses should implement this method to provide localized values.
106 virtual void DeclareLocalizedValues(LocalizedValuesBuilder
* builder
) = 0;
108 // Subclasses can override these methods to pass additional parameters
109 // to loadTimeData. Generally, it is a bad approach, and it should be replaced
110 // with Context at some point.
111 virtual void GetAdditionalParameters(base::DictionaryValue
* parameters
);
113 // Shortcut for calling JS methods on WebUI side.
114 void CallJS(const std::string
& method
);
116 template<typename A1
>
117 void CallJS(const std::string
& method
, const A1
& arg1
) {
118 web_ui()->CallJavascriptFunction(FullMethodPath(method
), MakeValue(arg1
));
121 template<typename A1
, typename A2
>
122 void CallJS(const std::string
& method
, const A1
& arg1
, const A2
& arg2
) {
123 web_ui()->CallJavascriptFunction(FullMethodPath(method
), MakeValue(arg1
),
127 template<typename A1
, typename A2
, typename A3
>
128 void CallJS(const std::string
& method
,
132 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
138 template<typename A1
, typename A2
, typename A3
, typename A4
>
139 void CallJS(const std::string
& method
,
144 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
151 // Shortcut methods for adding WebUI callbacks.
153 void AddRawCallback(const std::string
& name
,
154 void (T::*method
)(const base::ListValue
* args
)) {
155 web_ui()->RegisterMessageCallback(
157 base::Bind(method
, base::Unretained(static_cast<T
*>(this))));
161 void AddCallback(const std::string
& name
, void (T::*method
)()) {
162 base::Callback
<void()> callback
=
163 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
164 web_ui()->RegisterMessageCallback(
165 name
, base::Bind(&CallbackWrapper0
, callback
));
168 template<typename T
, typename A1
>
169 void AddCallback(const std::string
& name
, void (T::*method
)(A1 arg1
)) {
170 base::Callback
<void(A1
)> callback
=
171 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
172 web_ui()->RegisterMessageCallback(
173 name
, base::Bind(&CallbackWrapper1
<A1
>, callback
));
176 template<typename T
, typename A1
, typename A2
>
177 void AddCallback(const std::string
& name
,
178 void (T::*method
)(A1 arg1
, A2 arg2
)) {
179 base::Callback
<void(A1
, A2
)> callback
=
180 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
181 web_ui()->RegisterMessageCallback(
182 name
, base::Bind(&CallbackWrapper2
<A1
, A2
>, callback
));
185 template<typename T
, typename A1
, typename A2
, typename A3
>
186 void AddCallback(const std::string
& name
,
187 void (T::*method
)(A1 arg1
, A2 arg2
, A3 arg3
)) {
188 base::Callback
<void(A1
, A2
, A3
)> callback
=
189 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
190 web_ui()->RegisterMessageCallback(
191 name
, base::Bind(&CallbackWrapper3
<A1
, A2
, A3
>, callback
));
194 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
>
195 void AddCallback(const std::string
& name
,
196 void (T::*method
)(A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
)) {
197 base::Callback
<void(A1
, A2
, A3
, A4
)> callback
=
198 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
199 web_ui()->RegisterMessageCallback(
200 name
, base::Bind(&CallbackWrapper4
<A1
, A2
, A3
, A4
>, callback
));
203 // Called when the page is ready and handler can do initialization.
204 virtual void Initialize() = 0;
206 // Show selected WebUI |screen|. Optionally it can pass screen initialization
207 // data via |data| parameter.
208 void ShowScreen(const char* screen
, const base::DictionaryValue
* data
);
210 // Whether page is ready.
211 bool page_is_ready() const { return page_is_ready_
; }
213 // Returns the window which shows us.
214 virtual gfx::NativeWindow
GetNativeWindow();
217 // Returns full name of JS method based on screen and method
219 std::string
FullMethodPath(const std::string
& method
) const;
221 // Keeps whether page is ready.
224 base::DictionaryValue
* localized_values_
;
226 // Full name of the corresponding JS screen object. Can be empty, if
227 // there are no corresponding screen object or several different
229 std::string js_screen_path_prefix_
;
231 // The string id used in the async asset load in JS. If it is set to a
232 // non empty value, the Initialize will be deferred until the underlying load
234 std::string async_assets_load_id_
;
236 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler
);
239 } // namespace chromeos
241 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_