| 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> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | 8 #include "base/macros.h" |
| 14 #include "chrome/browser/chromeos/login/oobe_screen.h" | 9 #include "chrome/browser/chromeos/login/oobe_screen.h" |
| 15 #include "chrome/browser/chromeos/login/screens/model_view_channel.h" | 10 #include "chrome/browser/ui/webui/chromeos/login/base_webui_handler.h" |
| 16 #include "components/login/base_screen_handler_utils.h" | |
| 17 #include "content/public/browser/web_ui.h" | |
| 18 #include "content/public/browser/web_ui_message_handler.h" | |
| 19 #include "ui/gfx/native_widget_types.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class DictionaryValue; | |
| 23 class ListValue; | |
| 24 } | |
| 25 | |
| 26 namespace login { | |
| 27 class LocalizedValuesBuilder; | |
| 28 } | |
| 29 | 11 |
| 30 namespace chromeos { | 12 namespace chromeos { |
| 31 | 13 |
| 32 class BaseScreen; | 14 // Base class for the OOBE/Login WebUI handlers which provide methods specific |
| 33 class OobeUI; | 15 // to a particular OobeScreen. |
| 16 class BaseScreenHandler : public BaseWebUIHandler { |
| 17 public: |
| 18 explicit BaseScreenHandler(OobeScreen oobe_screen); |
| 19 ~BaseScreenHandler() override; |
| 34 | 20 |
| 35 // A helper class to store deferred Javascript calls, shared by subclasses of | 21 OobeScreen oobe_screen() const { return oobe_screen_; } |
| 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 | 22 |
| 51 private: | 23 private: |
| 52 // Whether the instance is initialized. | 24 // OobeScreen that this handler corresponds to. |
| 53 // | 25 OobeScreen oobe_screen_ = OobeScreen::SCREEN_UNKNOWN; |
| 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 | |
| 63 // Base class for the OOBE/Login WebUI handlers. | |
| 64 class BaseScreenHandler : public content::WebUIMessageHandler, | |
| 65 public ModelViewChannel { | |
| 66 public: | |
| 67 BaseScreenHandler(); | |
| 68 explicit BaseScreenHandler(JSCallsContainer* js_calls_container); | |
| 69 ~BaseScreenHandler() override; | |
| 70 | |
| 71 // Gets localized strings to be used on the page. | |
| 72 void GetLocalizedStrings( | |
| 73 base::DictionaryValue* localized_strings); | |
| 74 | |
| 75 // WebUIMessageHandler implementation: | |
| 76 void RegisterMessages() override; | |
| 77 | |
| 78 // ModelViewChannel implementation: | |
| 79 void CommitContextChanges(const base::DictionaryValue& diff) override; | |
| 80 | |
| 81 // This method is called when page is ready. It propagates to inherited class | |
| 82 // via virtual Initialize() method (see below). | |
| 83 void InitializeBase(); | |
| 84 | |
| 85 void set_async_assets_load_id(const std::string& async_assets_load_id) { | |
| 86 async_assets_load_id_ = async_assets_load_id; | |
| 87 } | |
| 88 const std::string& async_assets_load_id() const { | |
| 89 return async_assets_load_id_; | |
| 90 } | |
| 91 | |
| 92 // Set the prefix used when running CallJs with a method. For example, | |
| 93 // set_call_js_prefix("Oobe") | |
| 94 // CallJs("lock") -> Invokes JS global named "Oobe.lock" | |
| 95 void set_call_js_prefix(const std::string& prefix) { | |
| 96 js_screen_path_prefix_ = prefix + "."; | |
| 97 } | |
| 98 | |
| 99 protected: | |
| 100 // All subclasses should implement this method to provide localized values. | |
| 101 virtual void DeclareLocalizedValues( | |
| 102 ::login::LocalizedValuesBuilder* builder) = 0; | |
| 103 | |
| 104 // All subclasses should implement this method to register callbacks for JS | |
| 105 // messages. | |
| 106 // | |
| 107 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when | |
| 108 // all screens will be switched to use ScreenContext. | |
| 109 virtual void DeclareJSCallbacks() {} | |
| 110 | |
| 111 // Subclasses can override these methods to pass additional parameters | |
| 112 // to loadTimeData. Generally, it is a bad approach, and it should be replaced | |
| 113 // with Context at some point. | |
| 114 virtual void GetAdditionalParameters(base::DictionaryValue* parameters); | |
| 115 | |
| 116 // Shortcut for calling JS methods on WebUI side. | |
| 117 void CallJS(const std::string& method); | |
| 118 | |
| 119 template<typename A1> | |
| 120 void CallJS(const std::string& method, const A1& arg1) { | |
| 121 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), | |
| 122 ::login::MakeValue(arg1)); | |
| 123 } | |
| 124 | |
| 125 template<typename A1, typename A2> | |
| 126 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) { | |
| 127 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), | |
| 128 ::login::MakeValue(arg1), | |
| 129 ::login::MakeValue(arg2)); | |
| 130 } | |
| 131 | |
| 132 template<typename A1, typename A2, typename A3> | |
| 133 void CallJS(const std::string& method, | |
| 134 const A1& arg1, | |
| 135 const A2& arg2, | |
| 136 const A3& arg3) { | |
| 137 web_ui()->CallJavascriptFunctionUnsafe( | |
| 138 FullMethodPath(method), ::login::MakeValue(arg1), | |
| 139 ::login::MakeValue(arg2), ::login::MakeValue(arg3)); | |
| 140 } | |
| 141 | |
| 142 template<typename A1, typename A2, typename A3, typename A4> | |
| 143 void CallJS(const std::string& method, | |
| 144 const A1& arg1, | |
| 145 const A2& arg2, | |
| 146 const A3& arg3, | |
| 147 const A4& arg4) { | |
| 148 web_ui()->CallJavascriptFunctionUnsafe( | |
| 149 FullMethodPath(method), ::login::MakeValue(arg1), | |
| 150 ::login::MakeValue(arg2), ::login::MakeValue(arg3), | |
| 151 ::login::MakeValue(arg4)); | |
| 152 } | |
| 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 | |
| 177 // Shortcut methods for adding WebUI callbacks. | |
| 178 template<typename T> | |
| 179 void AddRawCallback(const std::string& name, | |
| 180 void (T::*method)(const base::ListValue* args)) { | |
| 181 web_ui()->RegisterMessageCallback( | |
| 182 name, | |
| 183 base::Bind(method, base::Unretained(static_cast<T*>(this)))); | |
| 184 } | |
| 185 | |
| 186 template<typename T, typename... Args> | |
| 187 void AddCallback(const std::string& name, void (T::*method)(Args...)) { | |
| 188 base::Callback<void(Args...)> callback = | |
| 189 base::Bind(method, base::Unretained(static_cast<T*>(this))); | |
| 190 web_ui()->RegisterMessageCallback( | |
| 191 name, base::Bind(&::login::CallbackWrapper<Args...>, callback)); | |
| 192 } | |
| 193 | |
| 194 template <typename Method> | |
| 195 void AddPrefixedCallback(const std::string& unprefixed_name, | |
| 196 const Method& method) { | |
| 197 AddCallback(FullMethodPath(unprefixed_name), method); | |
| 198 } | |
| 199 | |
| 200 // Called when the page is ready and handler can do initialization. | |
| 201 virtual void Initialize() = 0; | |
| 202 | |
| 203 // Show selected WebUI |screen|. | |
| 204 void ShowScreen(OobeScreen screen); | |
| 205 // Show selected WebUI |screen|. Pass screen initialization using the |data| | |
| 206 // parameter. | |
| 207 void ShowScreenWithData(OobeScreen screen, const base::DictionaryValue* data); | |
| 208 | |
| 209 // Returns the OobeUI instance. | |
| 210 OobeUI* GetOobeUI() const; | |
| 211 | |
| 212 // Returns current visible OOBE screen. | |
| 213 OobeScreen GetCurrentScreen() const; | |
| 214 | |
| 215 // Whether page is ready. | |
| 216 bool page_is_ready() const { return page_is_ready_; } | |
| 217 | |
| 218 // Returns the window which shows us. | |
| 219 virtual gfx::NativeWindow GetNativeWindow(); | |
| 220 | |
| 221 void SetBaseScreen(BaseScreen* base_screen); | |
| 222 | |
| 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 | |
| 234 // Returns full name of JS method based on screen and method | |
| 235 // names. | |
| 236 std::string FullMethodPath(const std::string& method) const; | |
| 237 | |
| 238 // Handles user action. | |
| 239 void HandleUserAction(const std::string& action_id); | |
| 240 | |
| 241 // Handles situation when screen context is changed. | |
| 242 void HandleContextChanged(const base::DictionaryValue* diff); | |
| 243 | |
| 244 // Keeps whether page is ready. | |
| 245 bool page_is_ready_ = false; | |
| 246 | |
| 247 BaseScreen* base_screen_ = nullptr; | |
| 248 | |
| 249 // Full name of the corresponding JS screen object. Can be empty, if | |
| 250 // there are no corresponding screen object or several different | |
| 251 // objects. | |
| 252 std::string js_screen_path_prefix_; | |
| 253 | |
| 254 // The string id used in the async asset load in JS. If it is set to a | |
| 255 // non empty value, the Initialize will be deferred until the underlying load | |
| 256 // is finished. | |
| 257 std::string async_assets_load_id_; | |
| 258 | |
| 259 // Pending changes to context which will be sent when the page will be ready. | |
| 260 base::DictionaryValue pending_context_changes_; | |
| 261 | |
| 262 JSCallsContainer* js_calls_container_ = nullptr; // non-owning pointers. | |
| 263 | 26 |
| 264 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); | 27 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); |
| 265 }; | 28 }; |
| 266 | 29 |
| 267 } // namespace chromeos | 30 } // namespace chromeos |
| 268 | 31 |
| 269 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 32 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ |
| OLD | NEW |