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 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,
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,
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();
98 // All subclasses should implement this method to provide localized values.
99 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) = 0;
101 // Subclasses can override these methods to pass additional parameters
102 // to loadTimeData. Generally, it is a bad approach, and it should be replaced
103 // with Context at some point.
104 virtual void GetAdditionalParameters(base::DictionaryValue* parameters);
106 // Shortcut for calling JS methods on WebUI side.
107 void CallJS(const std::string& method);
109 template<typename A1>
110 void CallJS(const std::string& method, const A1& arg1) {
111 web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1));
114 template<typename A1, typename A2>
115 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) {
116 web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1),
120 template<typename A1, typename A2, typename A3>
121 void CallJS(const std::string& method,
125 web_ui()->CallJavascriptFunction(FullMethodPath(method),
131 template<typename A1, typename A2, typename A3, typename A4>
132 void CallJS(const std::string& method,
137 web_ui()->CallJavascriptFunction(FullMethodPath(method),
144 // Shortcut methods for adding WebUI callbacks.
146 void AddRawCallback(const std::string& name,
147 void (T::*method)(const base::ListValue* args)) {
148 web_ui()->RegisterMessageCallback(
150 base::Bind(method, base::Unretained(static_cast<T*>(this))));
154 void AddCallback(const std::string& name, void (T::*method)()) {
155 base::Callback<void()> callback =
156 base::Bind(method, base::Unretained(static_cast<T*>(this)));
157 web_ui()->RegisterMessageCallback(
158 name, base::Bind(&CallbackWrapper0, callback));
161 template<typename T, typename A1>
162 void AddCallback(const std::string& name, void (T::*method)(A1 arg1)) {
163 base::Callback<void(A1)> callback =
164 base::Bind(method, base::Unretained(static_cast<T*>(this)));
165 web_ui()->RegisterMessageCallback(
166 name, base::Bind(&CallbackWrapper1<A1>, callback));
169 template<typename T, typename A1, typename A2>
170 void AddCallback(const std::string& name,
171 void (T::*method)(A1 arg1, A2 arg2)) {
172 base::Callback<void(A1, A2)> callback =
173 base::Bind(method, base::Unretained(static_cast<T*>(this)));
174 web_ui()->RegisterMessageCallback(
175 name, base::Bind(&CallbackWrapper2<A1, A2>, callback));
178 template<typename T, typename A1, typename A2, typename A3>
179 void AddCallback(const std::string& name,
180 void (T::*method)(A1 arg1, A2 arg2, A3 arg3)) {
181 base::Callback<void(A1, A2, A3)> callback =
182 base::Bind(method, base::Unretained(static_cast<T*>(this)));
183 web_ui()->RegisterMessageCallback(
184 name, base::Bind(&CallbackWrapper3<A1, A2, A3>, callback));
187 template<typename T, typename A1, typename A2, typename A3, typename A4>
188 void AddCallback(const std::string& name,
189 void (T::*method)(A1 arg1, A2 arg2, A3 arg3, A4 arg4)) {
190 base::Callback<void(A1, A2, A3, A4)> callback =
191 base::Bind(method, base::Unretained(static_cast<T*>(this)));
192 web_ui()->RegisterMessageCallback(
193 name, base::Bind(&CallbackWrapper4<A1, A2, A3, A4>, callback));
196 // Called when the page is ready and handler can do initialization.
197 virtual void Initialize() = 0;
199 // Show selected WebUI |screen|. Optionally it can pass screen initialization
200 // data via |data| parameter.
201 void ShowScreen(const char* screen, const base::DictionaryValue* data);
203 // Whether page is ready.
204 bool page_is_ready() const { return page_is_ready_; }
206 // Returns the window which shows us.
207 virtual gfx::NativeWindow GetNativeWindow();
210 // Returns full name of JS method based on screen and method
212 std::string FullMethodPath(const std::string& method) const;
214 // Keeps whether page is ready.
217 base::DictionaryValue* localized_values_;
219 // Full name of the corresponding JS screen object. Can be empty, if
220 // there are no corresponding screen object or several different
222 std::string js_screen_path_prefix_;
224 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler);
227 } // namespace chromeos
229 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_