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

Side by Side Diff: content/public/browser/web_ui_message_handler.h

Issue 2890523003: WebUI: Add helper FireWebUIListener method on WebUIMessageHandler. (Closed)
Patch Set: Address comment. Created 3 years, 7 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
« no previous file with comments | « chrome/browser/ui/webui/settings/search_engines_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_ 5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_ 6 #define CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/gtest_prod_util.h" 10 #include "base/gtest_prod_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "base/values.h"
13 #include "content/common/content_export.h" 14 #include "content/common/content_export.h"
14 #include "content/public/browser/web_ui.h" 15 #include "content/public/browser/web_ui.h"
15 16
16 class WebUIBrowserTest; 17 class WebUIBrowserTest;
17 18
18 namespace base { 19 namespace base {
19 class ListValue; 20 class ListValue;
20 class Value;
21 } 21 }
22 22
23 namespace content { 23 namespace content {
24 24
25 class WebUI; 25 class WebUI;
26 class WebUIImpl; 26 class WebUIImpl;
27 27
28 // Messages sent from the DOM are forwarded via the WebUI to handler 28 // Messages sent from the DOM are forwarded via the WebUI to handler
29 // classes. These objects are owned by WebUI and destroyed when the 29 // classes. These objects are owned by WebUI and destroyed when the
30 // host is destroyed. 30 // host is destroyed.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // promise should be resolved (request succeeded). 84 // promise should be resolved (request succeeded).
85 void ResolveJavascriptCallback(const base::Value& callback_id, 85 void ResolveJavascriptCallback(const base::Value& callback_id,
86 const base::Value& response); 86 const base::Value& response);
87 87
88 // Helper method for responding to Javascript requests initiated with 88 // Helper method for responding to Javascript requests initiated with
89 // cr.sendWithPromise() (defined in cr.js), for the case where the returned 89 // cr.sendWithPromise() (defined in cr.js), for the case where the returned
90 // promise should be rejected (request failed). 90 // promise should be rejected (request failed).
91 void RejectJavascriptCallback(const base::Value& callback_id, 91 void RejectJavascriptCallback(const base::Value& callback_id,
92 const base::Value& response); 92 const base::Value& response);
93 93
94 // Helper method for notifying Javascript listeners added with
95 // cr.addWebUIListener() (defined in cr.js).
96 template <typename... Values>
97 void FireWebUIListener(const std::string& event_name,
98 const Values&... values) {
99 // cr.webUIListenerCallback is a global JS function exposed from cr.js.
100 CallJavascriptFunction("cr.webUIListenerCallback", base::Value(event_name),
101 values...);
102 }
103
94 // Call a Javascript function by sending its name and arguments down to 104 // Call a Javascript function by sending its name and arguments down to
95 // the renderer. This is asynchronous; there's no way to get the result 105 // the renderer. This is asynchronous; there's no way to get the result
96 // of the call, and should be thought of more like sending a message to 106 // of the call, and should be thought of more like sending a message to
97 // the page. 107 // the page.
98 // All function names in WebUI must consist of only ASCII characters. 108 // All function names in WebUI must consist of only ASCII characters.
99 // These functions will crash if JavaScript is not currently allowed. 109 // These functions will crash if JavaScript is not currently allowed.
100 template <typename... Values> 110 template <typename... Values>
101 void CallJavascriptFunction(const std::string& function_name, 111 void CallJavascriptFunction(const std::string& function_name,
102 const Values&... values) { 112 const Values&... values) {
103 CHECK(IsJavascriptAllowed()) << "Cannot CallJavascriptFunction before " 113 CHECK(IsJavascriptAllowed()) << "Cannot CallJavascriptFunction before "
(...skipping 20 matching lines...) Expand all
124 134
125 // True if the page is for JavaScript calls from this handler. 135 // True if the page is for JavaScript calls from this handler.
126 bool javascript_allowed_; 136 bool javascript_allowed_;
127 137
128 WebUI* web_ui_; 138 WebUI* web_ui_;
129 }; 139 };
130 140
131 } // namespace content 141 } // namespace content
132 142
133 #endif // CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_ 143 #endif // CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/search_engines_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698