| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_WEBUI_HANDLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_WEBUI_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "chrome/browser/chromeos/login/oobe_screen.h" | 14 #include "chrome/browser/chromeos/login/oobe_screen.h" |
| 15 #include "chrome/browser/chromeos/login/screens/model_view_channel.h" | 15 #include "chrome/browser/chromeos/login/screens/model_view_channel.h" |
| 16 #include "components/login/base_screen_handler_utils.h" | 16 #include "components/login/base_screen_handler_utils.h" |
| 17 #include "content/public/browser/web_ui.h" | 17 #include "content/public/browser/web_ui.h" |
| 18 #include "content/public/browser/web_ui_message_handler.h" | 18 #include "content/public/browser/web_ui_message_handler.h" |
| 19 #include "ui/gfx/native_widget_types.h" | 19 #include "ui/gfx/native_widget_types.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class DictionaryValue; | 22 class DictionaryValue; |
| 23 class ListValue; | 23 class ListValue; |
| 24 } | 24 } |
| 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 // Base class for the OOBE/Login WebUI handlers. | 35 // Base class for the OOBE/Login WebUI handlers. |
| 36 class BaseScreenHandler : public content::WebUIMessageHandler, | 36 class BaseWebUIHandler : public content::WebUIMessageHandler, |
| 37 public ModelViewChannel { | 37 public ModelViewChannel { |
| 38 public: | 38 public: |
| 39 BaseScreenHandler(); | 39 BaseWebUIHandler(); |
| 40 ~BaseScreenHandler() override; | 40 ~BaseWebUIHandler() override; |
| 41 | 41 |
| 42 // Gets localized strings to be used on the page. | 42 // Gets localized strings to be used on the page. |
| 43 void GetLocalizedStrings( | 43 void GetLocalizedStrings(base::DictionaryValue* localized_strings); |
| 44 base::DictionaryValue* localized_strings); | |
| 45 | 44 |
| 46 // WebUIMessageHandler implementation: | 45 // WebUIMessageHandler implementation: |
| 47 void RegisterMessages() override; | 46 void RegisterMessages() override; |
| 48 | 47 |
| 49 // ModelViewChannel implementation: | 48 // ModelViewChannel implementation: |
| 50 void CommitContextChanges(const base::DictionaryValue& diff) override; | 49 void CommitContextChanges(const base::DictionaryValue& diff) override; |
| 51 | 50 |
| 52 // This method is called when page is ready. It propagates to inherited class | 51 // This method is called when page is ready. It propagates to inherited class |
| 53 // via virtual Initialize() method (see below). | 52 // via virtual Initialize() method (see below). |
| 54 void InitializeBase(); | 53 void InitializeBase(); |
| 55 | 54 |
| 56 void set_async_assets_load_id(const std::string& async_assets_load_id) { | |
| 57 async_assets_load_id_ = async_assets_load_id; | |
| 58 } | |
| 59 const std::string& async_assets_load_id() const { | |
| 60 return async_assets_load_id_; | |
| 61 } | |
| 62 | |
| 63 // Set the prefix used when running CallJs with a method. For example, | 55 // Set the prefix used when running CallJs with a method. For example, |
| 64 // set_call_js_prefix("Oobe") | 56 // set_call_js_prefix("Oobe") |
| 65 // CallJs("lock") -> Invokes JS global named "Oobe.lock" | 57 // CallJs("lock") -> Invokes JS global named "Oobe.lock" |
| 66 void set_call_js_prefix(const std::string& prefix) { | 58 void set_call_js_prefix(const std::string& prefix) { |
| 67 js_screen_path_prefix_ = prefix + "."; | 59 js_screen_path_prefix_ = prefix + "."; |
| 68 } | 60 } |
| 69 | 61 |
| 62 void set_async_assets_load_id(const std::string& async_assets_load_id) { |
| 63 async_assets_load_id_ = async_assets_load_id; |
| 64 } |
| 65 const std::string& async_assets_load_id() const { |
| 66 return async_assets_load_id_; |
| 67 } |
| 68 |
| 70 protected: | 69 protected: |
| 71 // All subclasses should implement this method to provide localized values. | 70 // All subclasses should implement this method to provide localized values. |
| 72 virtual void DeclareLocalizedValues( | 71 virtual void DeclareLocalizedValues( |
| 73 ::login::LocalizedValuesBuilder* builder) = 0; | 72 ::login::LocalizedValuesBuilder* builder) = 0; |
| 74 | 73 |
| 75 // All subclasses should implement this method to register callbacks for JS | 74 // All subclasses should implement this method to register callbacks for JS |
| 76 // messages. | 75 // messages. |
| 77 // | 76 // |
| 78 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when | 77 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when |
| 79 // all screens will be switched to use ScreenContext. | 78 // all screens will be switched to use ScreenContext. |
| 80 virtual void DeclareJSCallbacks() {} | 79 virtual void DeclareJSCallbacks() {} |
| 81 | 80 |
| 82 // Subclasses can override these methods to pass additional parameters | 81 // Subclasses can override these methods to pass additional parameters |
| 83 // to loadTimeData. Generally, it is a bad approach, and it should be replaced | 82 // to loadTimeData. Generally, it is a bad approach, and it should be replaced |
| 84 // with Context at some point. | 83 // with Context at some point. |
| 85 virtual void GetAdditionalParameters(base::DictionaryValue* parameters); | 84 virtual void GetAdditionalParameters(base::DictionaryValue* parameters); |
| 86 | 85 |
| 87 // Shortcut for calling JS methods on WebUI side. | 86 // Shortcut for calling JS methods on WebUI side. |
| 88 void CallJS(const std::string& method); | 87 void CallJS(const std::string& method); |
| 89 | 88 |
| 90 template<typename A1> | 89 template <typename A1> |
| 91 void CallJS(const std::string& method, const A1& arg1) { | 90 void CallJS(const std::string& method, const A1& arg1) { |
| 92 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), | 91 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), |
| 93 ::login::MakeValue(arg1)); | 92 ::login::MakeValue(arg1)); |
| 94 } | 93 } |
| 95 | 94 |
| 96 template<typename A1, typename A2> | 95 template <typename A1, typename A2> |
| 97 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) { | 96 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) { |
| 98 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), | 97 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), |
| 99 ::login::MakeValue(arg1), | 98 ::login::MakeValue(arg1), |
| 100 ::login::MakeValue(arg2)); | 99 ::login::MakeValue(arg2)); |
| 101 } | 100 } |
| 102 | 101 |
| 103 template<typename A1, typename A2, typename A3> | 102 template <typename A1, typename A2, typename A3> |
| 104 void CallJS(const std::string& method, | 103 void CallJS(const std::string& method, |
| 105 const A1& arg1, | 104 const A1& arg1, |
| 106 const A2& arg2, | 105 const A2& arg2, |
| 107 const A3& arg3) { | 106 const A3& arg3) { |
| 108 web_ui()->CallJavascriptFunctionUnsafe( | 107 web_ui()->CallJavascriptFunctionUnsafe( |
| 109 FullMethodPath(method), ::login::MakeValue(arg1), | 108 FullMethodPath(method), ::login::MakeValue(arg1), |
| 110 ::login::MakeValue(arg2), ::login::MakeValue(arg3)); | 109 ::login::MakeValue(arg2), ::login::MakeValue(arg3)); |
| 111 } | 110 } |
| 112 | 111 |
| 113 template<typename A1, typename A2, typename A3, typename A4> | 112 template <typename A1, typename A2, typename A3, typename A4> |
| 114 void CallJS(const std::string& method, | 113 void CallJS(const std::string& method, |
| 115 const A1& arg1, | 114 const A1& arg1, |
| 116 const A2& arg2, | 115 const A2& arg2, |
| 117 const A3& arg3, | 116 const A3& arg3, |
| 118 const A4& arg4) { | 117 const A4& arg4) { |
| 119 web_ui()->CallJavascriptFunctionUnsafe( | 118 web_ui()->CallJavascriptFunctionUnsafe( |
| 120 FullMethodPath(method), ::login::MakeValue(arg1), | 119 FullMethodPath(method), ::login::MakeValue(arg1), |
| 121 ::login::MakeValue(arg2), ::login::MakeValue(arg3), | 120 ::login::MakeValue(arg2), ::login::MakeValue(arg3), |
| 122 ::login::MakeValue(arg4)); | 121 ::login::MakeValue(arg4)); |
| 123 } | 122 } |
| 124 | 123 |
| 125 // Shortcut methods for adding WebUI callbacks. | 124 // Shortcut methods for adding WebUI callbacks. |
| 126 template<typename T> | 125 template <typename T> |
| 127 void AddRawCallback(const std::string& name, | 126 void AddRawCallback(const std::string& name, |
| 128 void (T::*method)(const base::ListValue* args)) { | 127 void (T::*method)(const base::ListValue* args)) { |
| 129 web_ui()->RegisterMessageCallback( | 128 web_ui()->RegisterMessageCallback( |
| 130 name, | 129 name, base::Bind(method, base::Unretained(static_cast<T*>(this)))); |
| 131 base::Bind(method, base::Unretained(static_cast<T*>(this)))); | |
| 132 } | 130 } |
| 133 | 131 |
| 134 template<typename T, typename... Args> | 132 template <typename T, typename... Args> |
| 135 void AddCallback(const std::string& name, void (T::*method)(Args...)) { | 133 void AddCallback(const std::string& name, void (T::*method)(Args...)) { |
| 136 base::Callback<void(Args...)> callback = | 134 base::Callback<void(Args...)> callback = |
| 137 base::Bind(method, base::Unretained(static_cast<T*>(this))); | 135 base::Bind(method, base::Unretained(static_cast<T*>(this))); |
| 138 web_ui()->RegisterMessageCallback( | 136 web_ui()->RegisterMessageCallback( |
| 139 name, base::Bind(&::login::CallbackWrapper<Args...>, callback)); | 137 name, base::Bind(&::login::CallbackWrapper<Args...>, callback)); |
| 140 } | 138 } |
| 141 | 139 |
| 142 template <typename Method> | 140 template <typename Method> |
| 143 void AddPrefixedCallback(const std::string& unprefixed_name, | 141 void AddPrefixedCallback(const std::string& unprefixed_name, |
| 144 const Method& method) { | 142 const Method& method) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 std::string js_screen_path_prefix_; | 188 std::string js_screen_path_prefix_; |
| 191 | 189 |
| 192 // The string id used in the async asset load in JS. If it is set to a | 190 // 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 | 191 // non empty value, the Initialize will be deferred until the underlying load |
| 194 // is finished. | 192 // is finished. |
| 195 std::string async_assets_load_id_; | 193 std::string async_assets_load_id_; |
| 196 | 194 |
| 197 // Pending changes to context which will be sent when the page will be ready. | 195 // Pending changes to context which will be sent when the page will be ready. |
| 198 base::DictionaryValue pending_context_changes_; | 196 base::DictionaryValue pending_context_changes_; |
| 199 | 197 |
| 200 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); | 198 DISALLOW_COPY_AND_ASSIGN(BaseWebUIHandler); |
| 201 }; | 199 }; |
| 202 | 200 |
| 203 } // namespace chromeos | 201 } // namespace chromeos |
| 204 | 202 |
| 205 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ | 203 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_WEBUI_HANDLER_H_ |
| OLD | NEW |