Chromium Code Reviews| 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> | |
| 9 | |
| 8 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/logging.h" | |
| 9 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 10 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/public/browser/web_ui.h" | |
| 11 | 15 |
| 12 class GURL; | 16 class GURL; |
| 13 class WebUIBrowserTest; | 17 class WebUIBrowserTest; |
| 14 | 18 |
| 15 namespace base { | 19 namespace base { |
| 16 class DictionaryValue; | 20 class DictionaryValue; |
| 17 class ListValue; | 21 class ListValue; |
| 22 class Value; | |
| 18 } | 23 } |
| 19 | 24 |
| 20 namespace content { | 25 namespace content { |
| 21 | 26 |
| 22 class WebUI; | 27 class WebUI; |
| 23 class WebUIImpl; | 28 class WebUIImpl; |
| 24 | 29 |
| 25 // Messages sent from the DOM are forwarded via the WebUI to handler | 30 // Messages sent from the DOM are forwarded via the WebUI to handler |
| 26 // classes. These objects are owned by WebUI and destroyed when the | 31 // classes. These objects are owned by WebUI and destroyed when the |
| 27 // host is destroyed. | 32 // host is destroyed. |
| 28 class CONTENT_EXPORT WebUIMessageHandler { | 33 class CONTENT_EXPORT WebUIMessageHandler { |
| 29 public: | 34 public: |
| 30 WebUIMessageHandler() : web_ui_(nullptr) {} | 35 WebUIMessageHandler() : javascript_allowed_(false), web_ui_(nullptr) {} |
| 31 virtual ~WebUIMessageHandler() {} | 36 virtual ~WebUIMessageHandler() {} |
| 32 | 37 |
| 33 protected: | 38 protected: |
| 34 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractIntegerValue); | 39 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractIntegerValue); |
| 35 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractDoubleValue); | 40 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractDoubleValue); |
| 36 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractStringValue); | 41 FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractStringValue); |
| 37 | 42 |
| 43 // Subclasses must call this once the page is ready for JavaScript calls | |
| 44 // from this handler. | |
| 45 void AllowJavascript(); | |
| 46 | |
| 47 bool IsJavascriptAllowed() const { return javascript_allowed_; } | |
|
Dan Beam
2016/04/18 23:13:20
this should probably also check some other things,
Dan Beam
2016/04/18 23:13:20
if this isn't virtual, javascript_allowed() instea
tommycli
2016/04/19 17:45:38
Done.
tommycli
2016/04/19 17:45:38
I opted not to use lowercase because the new versi
| |
| 48 | |
| 38 // Helper methods: | 49 // Helper methods: |
| 39 | 50 |
| 40 // Extract an integer value from a list Value. | 51 // Extract an integer value from a list Value. |
| 41 static bool ExtractIntegerValue(const base::ListValue* value, int* out_int); | 52 static bool ExtractIntegerValue(const base::ListValue* value, int* out_int); |
| 42 | 53 |
| 43 // Extract a floating point (double) value from a list Value. | 54 // Extract a floating point (double) value from a list Value. |
| 44 static bool ExtractDoubleValue(const base::ListValue* value, | 55 static bool ExtractDoubleValue(const base::ListValue* value, |
| 45 double* out_value); | 56 double* out_value); |
| 46 | 57 |
| 47 // Extract a string value from a list Value. | 58 // Extract a string value from a list Value. |
| 48 static base::string16 ExtractStringValue(const base::ListValue* value); | 59 static base::string16 ExtractStringValue(const base::ListValue* value); |
| 49 | 60 |
| 50 // Called when a RenderView is reused to display a page (i.e. reload). | |
| 51 virtual void RenderViewReused() {} | |
| 52 | |
| 53 // This is where subclasses specify which messages they'd like to handle and | 61 // This is where subclasses specify which messages they'd like to handle and |
| 54 // perform any additional initialization.. At this point web_ui() will return | 62 // perform any additional initialization.. At this point web_ui() will return |
| 55 // the associated WebUI object. | 63 // the associated WebUI object. |
| 56 virtual void RegisterMessages() = 0; | 64 virtual void RegisterMessages() = 0; |
| 57 | 65 |
| 66 // Will be called whenever JavaScript from this handler becomes allowed from | |
| 67 // the disallowed state. Subclasses should override this method to register | |
| 68 // observers that push JavaScript calls to the page. | |
| 69 virtual void OnJavascriptAllowed() {} | |
| 70 | |
| 71 // Will be called whenever JavaScript from this handler becomes disallowed | |
| 72 // from the allowed state. This will never be called before | |
| 73 // OnJavascriptAllowed has been called. Subclasses should override this method | |
| 74 // to deregister or disabled observers that push JavaScript calls to the page. | |
| 75 virtual void OnJavascriptDisallowed() {} | |
| 76 | |
| 77 // Call a Javascript function by sending its name and arguments down to | |
| 78 // the renderer. This is asynchronous; there's no way to get the result | |
| 79 // of the call, and should be thought of more like sending a message to | |
| 80 // the page. | |
| 81 // All function names in WebUI must consist of only ASCII characters. | |
| 82 // These functions will crash if JavaScript is not currently allowed. | |
| 83 template <typename... Values> | |
|
Dan Beam
2016/04/18 23:13:21
til: "Variadic Templates" are a thing
tommycli
2016/04/19 17:45:38
lol, I wrote this with the C++11 reference manual
| |
| 84 void CallJavascriptFunction(const std::string& function_name, | |
| 85 const Values&... values) { | |
| 86 CHECK(javascript_allowed_) << | |
| 87 "Cannot CallJavascriptFunction before explicitly allowing JavaScript."; | |
| 88 web_ui()->CallJavascriptFunction(function_name, values...); | |
| 89 } | |
| 90 | |
| 58 // Returns the attached WebUI for this handler. | 91 // Returns the attached WebUI for this handler. |
| 59 WebUI* web_ui() const { return web_ui_; } | 92 WebUI* web_ui() const { return web_ui_; } |
| 60 | 93 |
| 61 // Sets the attached WebUI - exposed to subclasses for testing purposes. | 94 // Sets the attached WebUI - exposed to subclasses for testing purposes. |
| 62 void set_web_ui(WebUI* web_ui) { web_ui_ = web_ui; } | 95 void set_web_ui(WebUI* web_ui) { web_ui_ = web_ui; } |
| 63 | 96 |
| 64 private: | 97 private: |
| 65 // Provide external classes access to web_ui() and set_web_ui(). | 98 // Provide external classes access to web_ui(), set_web_ui(), and |
| 99 // RenderViewReused. | |
| 66 friend class WebUIImpl; | 100 friend class WebUIImpl; |
| 67 friend class ::WebUIBrowserTest; | 101 friend class ::WebUIBrowserTest; |
| 68 | 102 |
| 103 // Called when a RenderView is reused to display a page (i.e. reload). | |
| 104 void RenderViewReused(); | |
| 105 | |
| 106 // True if the page is for JavaScript calls from this handler. | |
| 107 bool javascript_allowed_; | |
| 108 | |
| 69 WebUI* web_ui_; | 109 WebUI* web_ui_; |
| 70 }; | 110 }; |
| 71 | 111 |
| 72 } // namespace content | 112 } // namespace content |
| 73 | 113 |
| 74 #endif // CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_ | 114 #endif // CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_ |
| OLD | NEW |