Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / base_screen_handler.h
blobaadad3a7b97dc77fb271242498ae5c78ee5a6102
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_
8 #include <string>
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"
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
22 class Value;
25 namespace login {
26 class LocalizedValuesBuilder;
29 namespace chromeos {
31 class BaseScreen;
33 // Base class for the OOBE/Login WebUI handlers.
34 class BaseScreenHandler : public content::WebUIMessageHandler,
35 public ModelViewChannel {
36 public:
37 // C-tor used when JS screen prefix is not needed.
38 BaseScreenHandler();
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_;
66 protected:
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
72 // messages.
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);
86 template<typename A1>
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,
101 const A1& arg1,
102 const A2& arg2,
103 const A3& arg3) {
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,
112 const A1& arg1,
113 const A2& arg2,
114 const A3& arg3,
115 const A4& arg4) {
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.
124 template<typename T>
125 void AddRawCallback(const std::string& name,
126 void (T::*method)(const base::ListValue* args)) {
127 web_ui()->RegisterMessageCallback(
128 name,
129 base::Bind(method, base::Unretained(static_cast<T*>(this))));
132 template<typename T, typename... Args>
133 void AddCallback(const std::string& name, void (T::*method)(Args...)) {
134 base::Callback<void(Args...)> callback =
135 base::Bind(method, base::Unretained(static_cast<T*>(this)));
136 web_ui()->RegisterMessageCallback(
137 name, base::Bind(&::login::CallbackWrapper<Args...>, callback));
140 template <typename Method>
141 void AddPrefixedCallback(const std::string& unprefixed_name,
142 const Method& method) {
143 AddCallback(FullMethodPath(unprefixed_name), method);
146 // Called when the page is ready and handler can do initialization.
147 virtual void Initialize() = 0;
149 // Show selected WebUI |screen|. Optionally it can pass screen initialization
150 // data via |data| parameter.
151 void ShowScreen(const char* screen, const base::DictionaryValue* data);
153 // Whether page is ready.
154 bool page_is_ready() const { return page_is_ready_; }
156 // Returns the window which shows us.
157 virtual gfx::NativeWindow GetNativeWindow();
159 void SetBaseScreen(BaseScreen* base_screen);
161 private:
162 // Returns full name of JS method based on screen and method
163 // names.
164 std::string FullMethodPath(const std::string& method) const;
166 // Handles user action.
167 void HandleUserAction(const std::string& action_id);
169 // Handles situation when screen context is changed.
170 void HandleContextChanged(const base::DictionaryValue* diff);
172 // Keeps whether page is ready.
173 bool page_is_ready_;
175 BaseScreen* base_screen_;
177 // Full name of the corresponding JS screen object. Can be empty, if
178 // there are no corresponding screen object or several different
179 // objects.
180 std::string js_screen_path_prefix_;
182 // The string id used in the async asset load in JS. If it is set to a
183 // non empty value, the Initialize will be deferred until the underlying load
184 // is finished.
185 std::string async_assets_load_id_;
187 // Pending changes to context which will be sent when the page will be ready.
188 base::DictionaryValue pending_context_changes_;
190 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler);
193 } // namespace chromeos
195 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_