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 "chrome/browser/chromeos/login/screens/model_view_channel.h"
14 #include "components/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
;
26 class LocalizedValuesBuilder
;
33 // Base class for the OOBE/Login WebUI handlers.
34 class BaseScreenHandler
: public content::WebUIMessageHandler
,
35 public ModelViewChannel
{
37 // C-tor used when JS screen prefix is not needed.
40 // C-tor used when JS screen prefix is needed.
41 explicit BaseScreenHandler(const std::string
& js_screen_path
);
43 ~BaseScreenHandler() override
;
45 // Gets localized strings to be used on the page.
46 void GetLocalizedStrings(
47 base::DictionaryValue
* localized_strings
);
49 // WebUIMessageHandler implementation:
50 void RegisterMessages() override
;
52 // ModelViewChannel implementation:
53 void CommitContextChanges(const base::DictionaryValue
& diff
) override
;
55 // This method is called when page is ready. It propagates to inherited class
56 // via virtual Initialize() method (see below).
57 void InitializeBase();
59 void set_async_assets_load_id(const std::string
& async_assets_load_id
) {
60 async_assets_load_id_
= async_assets_load_id
;
62 const std::string
& async_assets_load_id() const {
63 return async_assets_load_id_
;
67 // All subclasses should implement this method to provide localized values.
68 virtual void DeclareLocalizedValues(
69 ::login::LocalizedValuesBuilder
* builder
) = 0;
71 // All subclasses should implement this method to register callbacks for JS
74 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when
75 // all screens will be switched to use ScreenContext.
76 virtual void DeclareJSCallbacks() {}
78 // Subclasses can override these methods to pass additional parameters
79 // to loadTimeData. Generally, it is a bad approach, and it should be replaced
80 // with Context at some point.
81 virtual void GetAdditionalParameters(base::DictionaryValue
* parameters
);
83 // Shortcut for calling JS methods on WebUI side.
84 void CallJS(const std::string
& method
);
87 void CallJS(const std::string
& method
, const A1
& arg1
) {
88 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
89 ::login::MakeValue(arg1
));
92 template<typename A1
, typename A2
>
93 void CallJS(const std::string
& method
, const A1
& arg1
, const A2
& arg2
) {
94 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
95 ::login::MakeValue(arg1
),
96 ::login::MakeValue(arg2
));
99 template<typename A1
, typename A2
, typename A3
>
100 void CallJS(const std::string
& method
,
104 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
105 ::login::MakeValue(arg1
),
106 ::login::MakeValue(arg2
),
107 ::login::MakeValue(arg3
));
110 template<typename A1
, typename A2
, typename A3
, typename A4
>
111 void CallJS(const std::string
& method
,
116 web_ui()->CallJavascriptFunction(FullMethodPath(method
),
117 ::login::MakeValue(arg1
),
118 ::login::MakeValue(arg2
),
119 ::login::MakeValue(arg3
),
120 ::login::MakeValue(arg4
));
123 // Shortcut methods for adding WebUI callbacks.
125 void AddRawCallback(const std::string
& name
,
126 void (T::*method
)(const base::ListValue
* args
)) {
127 web_ui()->RegisterMessageCallback(
129 base::Bind(method
, base::Unretained(static_cast<T
*>(this))));
133 void AddCallback(const std::string
& name
, void (T::*method
)()) {
134 base::Callback
<void()> callback
=
135 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
136 web_ui()->RegisterMessageCallback(
137 name
, base::Bind(&::login::CallbackWrapper0
, callback
));
140 template<typename T
, typename A1
>
141 void AddCallback(const std::string
& name
, void (T::*method
)(A1 arg1
)) {
142 base::Callback
<void(A1
)> callback
=
143 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
144 web_ui()->RegisterMessageCallback(
145 name
, base::Bind(&::login::CallbackWrapper1
<A1
>, callback
));
148 template<typename T
, typename A1
, typename A2
>
149 void AddCallback(const std::string
& name
,
150 void (T::*method
)(A1 arg1
, A2 arg2
)) {
151 base::Callback
<void(A1
, A2
)> callback
=
152 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
153 web_ui()->RegisterMessageCallback(
154 name
, base::Bind(&::login::CallbackWrapper2
<A1
, A2
>, callback
));
157 template<typename T
, typename A1
, typename A2
, typename A3
>
158 void AddCallback(const std::string
& name
,
159 void (T::*method
)(A1 arg1
, A2 arg2
, A3 arg3
)) {
160 base::Callback
<void(A1
, A2
, A3
)> callback
=
161 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
162 web_ui()->RegisterMessageCallback(
163 name
, base::Bind(&::login::CallbackWrapper3
<A1
, A2
, A3
>, callback
));
166 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
>
167 void AddCallback(const std::string
& name
,
168 void (T::*method
)(A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
)) {
169 base::Callback
<void(A1
, A2
, A3
, A4
)> callback
=
170 base::Bind(method
, base::Unretained(static_cast<T
*>(this)));
171 web_ui()->RegisterMessageCallback(
172 name
, base::Bind(&::login::CallbackWrapper4
<A1
, A2
, A3
, A4
>, callback
));
175 template <typename Method
>
176 void AddPrefixedCallback(const std::string
& unprefixed_name
,
177 const Method
& method
) {
178 AddCallback(FullMethodPath(unprefixed_name
), method
);
181 // Called when the page is ready and handler can do initialization.
182 virtual void Initialize() = 0;
184 // Show selected WebUI |screen|. Optionally it can pass screen initialization
185 // data via |data| parameter.
186 void ShowScreen(const char* screen
, const base::DictionaryValue
* data
);
188 // Whether page is ready.
189 bool page_is_ready() const { return page_is_ready_
; }
191 // Returns the window which shows us.
192 virtual gfx::NativeWindow
GetNativeWindow();
194 void SetBaseScreen(BaseScreen
* base_screen
);
197 // Returns full name of JS method based on screen and method
199 std::string
FullMethodPath(const std::string
& method
) const;
201 // Handles user action.
202 void HandleUserAction(const std::string
& action_id
);
204 // Handles situation when screen context is changed.
205 void HandleContextChanged(const base::DictionaryValue
* diff
);
207 // Keeps whether page is ready.
210 BaseScreen
* base_screen_
;
212 base::DictionaryValue
* localized_values_
;
214 // Full name of the corresponding JS screen object. Can be empty, if
215 // there are no corresponding screen object or several different
217 std::string js_screen_path_prefix_
;
219 // The string id used in the async asset load in JS. If it is set to a
220 // non empty value, the Initialize will be deferred until the underlying load
222 std::string async_assets_load_id_
;
224 // Pending changes to context which will be sent when the page will be ready.
225 base::DictionaryValue pending_context_changes_
;
227 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler
);
230 } // namespace chromeos
232 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_