OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ |
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 namespace login { | 26 namespace login { |
27 class LocalizedValuesBuilder; | 27 class LocalizedValuesBuilder; |
28 } | 28 } |
29 | 29 |
30 namespace chromeos { | 30 namespace chromeos { |
31 | 31 |
32 class BaseScreen; | 32 class BaseScreen; |
33 class OobeUI; | 33 class OobeUI; |
34 | 34 |
| 35 // A helper class to store deferred Javascript calls, shared by subclasses of |
| 36 // BaseScreenHandler. |
| 37 class JSCallsContainer { |
| 38 public: |
| 39 JSCallsContainer(); |
| 40 ~JSCallsContainer(); |
| 41 |
| 42 // Used to decide whether the JS call should be deferred. |
| 43 bool is_initialized() { return is_initialized_; } |
| 44 |
| 45 // Used to mark the instance as intialized. |
| 46 void mark_initialized() { is_initialized_ = true; } |
| 47 |
| 48 // Used to add deferred calls to. |
| 49 std::vector<base::Closure>& deferred_js_calls() { return deferred_js_calls_; } |
| 50 |
| 51 private: |
| 52 // Whether the instance is initialized. |
| 53 // |
| 54 // The instance becomes initialized after the corresponding message is |
| 55 // received from Javascript side. |
| 56 bool is_initialized_ = false; |
| 57 |
| 58 // Javascript calls that have been deferred while the instance was not |
| 59 // initialized yet. |
| 60 std::vector<base::Closure> deferred_js_calls_; |
| 61 }; |
| 62 |
35 // Base class for the OOBE/Login WebUI handlers. | 63 // Base class for the OOBE/Login WebUI handlers. |
36 class BaseScreenHandler : public content::WebUIMessageHandler, | 64 class BaseScreenHandler : public content::WebUIMessageHandler, |
37 public ModelViewChannel { | 65 public ModelViewChannel { |
38 public: | 66 public: |
39 BaseScreenHandler(); | 67 BaseScreenHandler(); |
| 68 explicit BaseScreenHandler(JSCallsContainer* js_calls_container); |
40 ~BaseScreenHandler() override; | 69 ~BaseScreenHandler() override; |
41 | 70 |
42 // Gets localized strings to be used on the page. | 71 // Gets localized strings to be used on the page. |
43 void GetLocalizedStrings( | 72 void GetLocalizedStrings( |
44 base::DictionaryValue* localized_strings); | 73 base::DictionaryValue* localized_strings); |
45 | 74 |
46 // WebUIMessageHandler implementation: | 75 // WebUIMessageHandler implementation: |
47 void RegisterMessages() override; | 76 void RegisterMessages() override; |
48 | 77 |
49 // ModelViewChannel implementation: | 78 // ModelViewChannel implementation: |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 const A1& arg1, | 144 const A1& arg1, |
116 const A2& arg2, | 145 const A2& arg2, |
117 const A3& arg3, | 146 const A3& arg3, |
118 const A4& arg4) { | 147 const A4& arg4) { |
119 web_ui()->CallJavascriptFunctionUnsafe( | 148 web_ui()->CallJavascriptFunctionUnsafe( |
120 FullMethodPath(method), ::login::MakeValue(arg1), | 149 FullMethodPath(method), ::login::MakeValue(arg1), |
121 ::login::MakeValue(arg2), ::login::MakeValue(arg3), | 150 ::login::MakeValue(arg2), ::login::MakeValue(arg3), |
122 ::login::MakeValue(arg4)); | 151 ::login::MakeValue(arg4)); |
123 } | 152 } |
124 | 153 |
| 154 template <typename... Args> |
| 155 void CallJSOrDefer(const std::string& function_name, const Args&... args) { |
| 156 DCHECK(js_calls_container_); |
| 157 if (js_calls_container_->is_initialized()) { |
| 158 CallJS(function_name, args...); |
| 159 } else { |
| 160 // Note that std::conditional is used here in order to obtain a sequence |
| 161 // of base::Value types with the length equal to sizeof...(Args); the C++ |
| 162 // template parameter pack expansion rules require that the name of the |
| 163 // parameter pack appears in the pattern, even though the elements of the |
| 164 // Args pack are not actually in this code. |
| 165 js_calls_container_->deferred_js_calls().push_back(base::Bind( |
| 166 &BaseScreenHandler::ExecuteDeferredJSCall< |
| 167 typename std::conditional<true, base::Value, Args>::type...>, |
| 168 base::Unretained(this), function_name, |
| 169 base::Passed(::login::MakeValue(args).CreateDeepCopy())...)); |
| 170 } |
| 171 } |
| 172 |
| 173 // Executes Javascript calls that were deferred while the instance was not |
| 174 // initialized yet. |
| 175 void ExecuteDeferredJSCalls(); |
| 176 |
125 // Shortcut methods for adding WebUI callbacks. | 177 // Shortcut methods for adding WebUI callbacks. |
126 template<typename T> | 178 template<typename T> |
127 void AddRawCallback(const std::string& name, | 179 void AddRawCallback(const std::string& name, |
128 void (T::*method)(const base::ListValue* args)) { | 180 void (T::*method)(const base::ListValue* args)) { |
129 web_ui()->RegisterMessageCallback( | 181 web_ui()->RegisterMessageCallback( |
130 name, | 182 name, |
131 base::Bind(method, base::Unretained(static_cast<T*>(this)))); | 183 base::Bind(method, base::Unretained(static_cast<T*>(this)))); |
132 } | 184 } |
133 | 185 |
134 template<typename T, typename... Args> | 186 template<typename T, typename... Args> |
(...skipping 27 matching lines...) Expand all Loading... |
162 | 214 |
163 // Whether page is ready. | 215 // Whether page is ready. |
164 bool page_is_ready() const { return page_is_ready_; } | 216 bool page_is_ready() const { return page_is_ready_; } |
165 | 217 |
166 // Returns the window which shows us. | 218 // Returns the window which shows us. |
167 virtual gfx::NativeWindow GetNativeWindow(); | 219 virtual gfx::NativeWindow GetNativeWindow(); |
168 | 220 |
169 void SetBaseScreen(BaseScreen* base_screen); | 221 void SetBaseScreen(BaseScreen* base_screen); |
170 | 222 |
171 private: | 223 private: |
| 224 // Calls Javascript method. |
| 225 // |
| 226 // Note that the Args template parameter pack should consist of types |
| 227 // convertible to base::Value. |
| 228 template <typename... Args> |
| 229 void ExecuteDeferredJSCall(const std::string& function_name, |
| 230 std::unique_ptr<Args>... args) { |
| 231 CallJS(function_name, *args...); |
| 232 } |
| 233 |
172 // Returns full name of JS method based on screen and method | 234 // Returns full name of JS method based on screen and method |
173 // names. | 235 // names. |
174 std::string FullMethodPath(const std::string& method) const; | 236 std::string FullMethodPath(const std::string& method) const; |
175 | 237 |
176 // Handles user action. | 238 // Handles user action. |
177 void HandleUserAction(const std::string& action_id); | 239 void HandleUserAction(const std::string& action_id); |
178 | 240 |
179 // Handles situation when screen context is changed. | 241 // Handles situation when screen context is changed. |
180 void HandleContextChanged(const base::DictionaryValue* diff); | 242 void HandleContextChanged(const base::DictionaryValue* diff); |
181 | 243 |
182 // Keeps whether page is ready. | 244 // Keeps whether page is ready. |
183 bool page_is_ready_ = false; | 245 bool page_is_ready_ = false; |
184 | 246 |
185 BaseScreen* base_screen_ = nullptr; | 247 BaseScreen* base_screen_ = nullptr; |
186 | 248 |
187 // Full name of the corresponding JS screen object. Can be empty, if | 249 // Full name of the corresponding JS screen object. Can be empty, if |
188 // there are no corresponding screen object or several different | 250 // there are no corresponding screen object or several different |
189 // objects. | 251 // objects. |
190 std::string js_screen_path_prefix_; | 252 std::string js_screen_path_prefix_; |
191 | 253 |
192 // The string id used in the async asset load in JS. If it is set to a | 254 // The string id used in the async asset load in JS. If it is set to a |
193 // non empty value, the Initialize will be deferred until the underlying load | 255 // non empty value, the Initialize will be deferred until the underlying load |
194 // is finished. | 256 // is finished. |
195 std::string async_assets_load_id_; | 257 std::string async_assets_load_id_; |
196 | 258 |
197 // Pending changes to context which will be sent when the page will be ready. | 259 // Pending changes to context which will be sent when the page will be ready. |
198 base::DictionaryValue pending_context_changes_; | 260 base::DictionaryValue pending_context_changes_; |
199 | 261 |
| 262 JSCallsContainer* js_calls_container_ = nullptr; // non-owning pointers. |
| 263 |
200 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); | 264 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); |
201 }; | 265 }; |
202 | 266 |
203 } // namespace chromeos | 267 } // namespace chromeos |
204 | 268 |
205 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 269 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ |
OLD | NEW |