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 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 Loading... |
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 Loading... |
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_ |
OLD | NEW |