Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: chrome/browser/ui/webui/chromeos/login/base_webui_handler.h

Issue 2713513009: cros: Break BaseScreenHandler into two classes. (Closed)
Patch Set: Add comments, fix compile Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // A helper class to store deferred Javascript calls, shared by subclasses of 35 // A helper class to store deferred Javascript calls, shared by subclasses of
36 // BaseScreenHandler. 36 // BaseWebUIHandler.
37 class JSCallsContainer { 37 class JSCallsContainer {
38 public: 38 public:
39 JSCallsContainer(); 39 JSCallsContainer();
40 ~JSCallsContainer(); 40 ~JSCallsContainer();
41 41
42 // Used to decide whether the JS call should be deferred. 42 // Used to decide whether the JS call should be deferred.
43 bool is_initialized() { return is_initialized_; } 43 bool is_initialized() { return is_initialized_; }
44 44
45 // Used to mark the instance as intialized. 45 // Used to mark the instance as intialized.
46 void mark_initialized() { is_initialized_ = true; } 46 void mark_initialized() { is_initialized_ = true; }
47 47
48 // Used to add deferred calls to. 48 // Used to add deferred calls to.
49 std::vector<base::Closure>& deferred_js_calls() { return deferred_js_calls_; } 49 std::vector<base::Closure>& deferred_js_calls() { return deferred_js_calls_; }
50 50
51 private: 51 private:
52 // Whether the instance is initialized. 52 // Whether the instance is initialized.
53 // 53 //
54 // The instance becomes initialized after the corresponding message is 54 // The instance becomes initialized after the corresponding message is
55 // received from Javascript side. 55 // received from Javascript side.
56 bool is_initialized_ = false; 56 bool is_initialized_ = false;
57 57
58 // Javascript calls that have been deferred while the instance was not 58 // Javascript calls that have been deferred while the instance was not
59 // initialized yet. 59 // initialized yet.
60 std::vector<base::Closure> deferred_js_calls_; 60 std::vector<base::Closure> deferred_js_calls_;
61 }; 61 };
62 62
63 // Base class for the OOBE/Login WebUI handlers. 63 // Base class for all oobe/login WebUI handlers. These handlers are the binding
64 class BaseScreenHandler : public content::WebUIMessageHandler, 64 // layer that allow the C++ and JavaScript code to communicate.
65 public ModelViewChannel { 65 //
66 // If the deriving type is associated with a specific OobeScreen, it should
67 // derive from BaseScreenHandler instead of BaseWebUIHandler.
68 //
69 // TODO(jdufault): Move all OobeScreen related concepts out of BaseWebUIHandler
70 // and into BaseScreenHandler.
71 class BaseWebUIHandler : public content::WebUIMessageHandler,
72 public ModelViewChannel {
66 public: 73 public:
67 BaseScreenHandler(); 74 BaseWebUIHandler();
68 explicit BaseScreenHandler(JSCallsContainer* js_calls_container); 75 explicit BaseWebUIHandler(JSCallsContainer* js_calls_container);
69 ~BaseScreenHandler() override; 76 ~BaseWebUIHandler() override;
70 77
71 // Gets localized strings to be used on the page. 78 // Gets localized strings to be used on the page.
72 void GetLocalizedStrings( 79 void GetLocalizedStrings(base::DictionaryValue* localized_strings);
73 base::DictionaryValue* localized_strings);
74 80
75 // WebUIMessageHandler implementation: 81 // WebUIMessageHandler implementation:
76 void RegisterMessages() override; 82 void RegisterMessages() override;
77 83
78 // ModelViewChannel implementation: 84 // ModelViewChannel implementation:
79 void CommitContextChanges(const base::DictionaryValue& diff) override; 85 void CommitContextChanges(const base::DictionaryValue& diff) override;
80 86
81 // This method is called when page is ready. It propagates to inherited class 87 // This method is called when page is ready. It propagates to inherited class
82 // via virtual Initialize() method (see below). 88 // via virtual Initialize() method (see below).
83 void InitializeBase(); 89 void InitializeBase();
84 90
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, 91 // Set the prefix used when running CallJs with a method. For example,
93 // set_call_js_prefix("Oobe") 92 // set_call_js_prefix("Oobe")
94 // CallJs("lock") -> Invokes JS global named "Oobe.lock" 93 // CallJs("lock") -> Invokes JS global named "Oobe.lock"
95 void set_call_js_prefix(const std::string& prefix) { 94 void set_call_js_prefix(const std::string& prefix) {
96 js_screen_path_prefix_ = prefix + "."; 95 js_screen_path_prefix_ = prefix + ".";
97 } 96 }
98 97
98 void set_async_assets_load_id(const std::string& async_assets_load_id) {
99 async_assets_load_id_ = async_assets_load_id;
100 }
101 const std::string& async_assets_load_id() const {
102 return async_assets_load_id_;
103 }
104
99 protected: 105 protected:
100 // All subclasses should implement this method to provide localized values. 106 // All subclasses should implement this method to provide localized values.
101 virtual void DeclareLocalizedValues( 107 virtual void DeclareLocalizedValues(
102 ::login::LocalizedValuesBuilder* builder) = 0; 108 ::login::LocalizedValuesBuilder* builder) = 0;
103 109
104 // All subclasses should implement this method to register callbacks for JS 110 // All subclasses should implement this method to register callbacks for JS
105 // messages. 111 // messages.
106 // 112 //
107 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when 113 // TODO (ygorshenin, crbug.com/433797): make this method purely vrtual when
108 // all screens will be switched to use ScreenContext. 114 // all screens will be switched to use ScreenContext.
109 virtual void DeclareJSCallbacks() {} 115 virtual void DeclareJSCallbacks() {}
110 116
111 // Subclasses can override these methods to pass additional parameters 117 // Subclasses can override these methods to pass additional parameters
112 // to loadTimeData. Generally, it is a bad approach, and it should be replaced 118 // to loadTimeData. Generally, it is a bad approach, and it should be replaced
113 // with Context at some point. 119 // with Context at some point.
114 virtual void GetAdditionalParameters(base::DictionaryValue* parameters); 120 virtual void GetAdditionalParameters(base::DictionaryValue* parameters);
115 121
116 // Shortcut for calling JS methods on WebUI side. 122 // Shortcut for calling JS methods on WebUI side.
117 void CallJS(const std::string& method); 123 void CallJS(const std::string& method);
118 124
119 template<typename A1> 125 template <typename A1>
120 void CallJS(const std::string& method, const A1& arg1) { 126 void CallJS(const std::string& method, const A1& arg1) {
121 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), 127 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method),
122 ::login::MakeValue(arg1)); 128 ::login::MakeValue(arg1));
123 } 129 }
124 130
125 template<typename A1, typename A2> 131 template <typename A1, typename A2>
126 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) { 132 void CallJS(const std::string& method, const A1& arg1, const A2& arg2) {
127 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method), 133 web_ui()->CallJavascriptFunctionUnsafe(FullMethodPath(method),
128 ::login::MakeValue(arg1), 134 ::login::MakeValue(arg1),
129 ::login::MakeValue(arg2)); 135 ::login::MakeValue(arg2));
130 } 136 }
131 137
132 template<typename A1, typename A2, typename A3> 138 template <typename A1, typename A2, typename A3>
133 void CallJS(const std::string& method, 139 void CallJS(const std::string& method,
134 const A1& arg1, 140 const A1& arg1,
135 const A2& arg2, 141 const A2& arg2,
136 const A3& arg3) { 142 const A3& arg3) {
137 web_ui()->CallJavascriptFunctionUnsafe( 143 web_ui()->CallJavascriptFunctionUnsafe(
138 FullMethodPath(method), ::login::MakeValue(arg1), 144 FullMethodPath(method), ::login::MakeValue(arg1),
139 ::login::MakeValue(arg2), ::login::MakeValue(arg3)); 145 ::login::MakeValue(arg2), ::login::MakeValue(arg3));
140 } 146 }
141 147
142 template<typename A1, typename A2, typename A3, typename A4> 148 template <typename A1, typename A2, typename A3, typename A4>
143 void CallJS(const std::string& method, 149 void CallJS(const std::string& method,
144 const A1& arg1, 150 const A1& arg1,
145 const A2& arg2, 151 const A2& arg2,
146 const A3& arg3, 152 const A3& arg3,
147 const A4& arg4) { 153 const A4& arg4) {
148 web_ui()->CallJavascriptFunctionUnsafe( 154 web_ui()->CallJavascriptFunctionUnsafe(
149 FullMethodPath(method), ::login::MakeValue(arg1), 155 FullMethodPath(method), ::login::MakeValue(arg1),
150 ::login::MakeValue(arg2), ::login::MakeValue(arg3), 156 ::login::MakeValue(arg2), ::login::MakeValue(arg3),
151 ::login::MakeValue(arg4)); 157 ::login::MakeValue(arg4));
152 } 158 }
153 159
154 template <typename... Args> 160 template <typename... Args>
155 void CallJSOrDefer(const std::string& function_name, const Args&... args) { 161 void CallJSOrDefer(const std::string& function_name, const Args&... args) {
156 DCHECK(js_calls_container_); 162 DCHECK(js_calls_container_);
157 if (js_calls_container_->is_initialized()) { 163 if (js_calls_container_->is_initialized()) {
158 CallJS(function_name, args...); 164 CallJS(function_name, args...);
159 } else { 165 } else {
160 // Note that std::conditional is used here in order to obtain a sequence 166 // 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++ 167 // 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 168 // template parameter pack expansion rules require that the name of the
163 // parameter pack appears in the pattern, even though the elements of the 169 // parameter pack appears in the pattern, even though the elements of the
164 // Args pack are not actually in this code. 170 // Args pack are not actually in this code.
165 js_calls_container_->deferred_js_calls().push_back(base::Bind( 171 js_calls_container_->deferred_js_calls().push_back(base::Bind(
166 &BaseScreenHandler::ExecuteDeferredJSCall< 172 &BaseWebUIHandler::ExecuteDeferredJSCall<
167 typename std::conditional<true, base::Value, Args>::type...>, 173 typename std::conditional<true, base::Value, Args>::type...>,
168 base::Unretained(this), function_name, 174 base::Unretained(this), function_name,
169 base::Passed(::login::MakeValue(args).CreateDeepCopy())...)); 175 base::Passed(::login::MakeValue(args).CreateDeepCopy())...));
170 } 176 }
171 } 177 }
172 178
173 // Executes Javascript calls that were deferred while the instance was not 179 // Executes Javascript calls that were deferred while the instance was not
174 // initialized yet. 180 // initialized yet.
175 void ExecuteDeferredJSCalls(); 181 void ExecuteDeferredJSCalls();
176 182
177 // Shortcut methods for adding WebUI callbacks. 183 // Shortcut methods for adding WebUI callbacks.
178 template<typename T> 184 template <typename T>
179 void AddRawCallback(const std::string& name, 185 void AddRawCallback(const std::string& name,
180 void (T::*method)(const base::ListValue* args)) { 186 void (T::*method)(const base::ListValue* args)) {
181 web_ui()->RegisterMessageCallback( 187 web_ui()->RegisterMessageCallback(
182 name, 188 name, base::Bind(method, base::Unretained(static_cast<T*>(this))));
183 base::Bind(method, base::Unretained(static_cast<T*>(this))));
184 } 189 }
185 190
186 template<typename T, typename... Args> 191 template <typename T, typename... Args>
187 void AddCallback(const std::string& name, void (T::*method)(Args...)) { 192 void AddCallback(const std::string& name, void (T::*method)(Args...)) {
188 base::Callback<void(Args...)> callback = 193 base::Callback<void(Args...)> callback =
189 base::Bind(method, base::Unretained(static_cast<T*>(this))); 194 base::Bind(method, base::Unretained(static_cast<T*>(this)));
190 web_ui()->RegisterMessageCallback( 195 web_ui()->RegisterMessageCallback(
191 name, base::Bind(&::login::CallbackWrapper<Args...>, callback)); 196 name, base::Bind(&::login::CallbackWrapper<Args...>, callback));
192 } 197 }
193 198
194 template <typename Method> 199 template <typename Method>
195 void AddPrefixedCallback(const std::string& unprefixed_name, 200 void AddPrefixedCallback(const std::string& unprefixed_name,
196 const Method& method) { 201 const Method& method) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // The string id used in the async asset load in JS. If it is set to a 259 // 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 260 // non empty value, the Initialize will be deferred until the underlying load
256 // is finished. 261 // is finished.
257 std::string async_assets_load_id_; 262 std::string async_assets_load_id_;
258 263
259 // Pending changes to context which will be sent when the page will be ready. 264 // Pending changes to context which will be sent when the page will be ready.
260 base::DictionaryValue pending_context_changes_; 265 base::DictionaryValue pending_context_changes_;
261 266
262 JSCallsContainer* js_calls_container_ = nullptr; // non-owning pointers. 267 JSCallsContainer* js_calls_container_ = nullptr; // non-owning pointers.
263 268
264 DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler); 269 DISALLOW_COPY_AND_ASSIGN(BaseWebUIHandler);
265 }; 270 };
266 271
267 } // namespace chromeos 272 } // namespace chromeos
268 273
269 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_ 274 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_WEBUI_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698