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

Unified Diff: content/public/browser/web_ui_message_handler.h

Issue 1896463003: WebUI: Add JavaScript lifecycle-control to WebUIMessageHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/public/browser/web_ui.h ('k') | content/public/test/test_web_ui.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/public/browser/web_ui_message_handler.h
diff --git a/content/public/browser/web_ui_message_handler.h b/content/public/browser/web_ui_message_handler.h
index 3a33ab1db4993ed9b80f9c6fd7c09b5cfedaa32e..fe91c6493610c4d43be31dad75d70dd9fe33ef95 100644
--- a/content/public/browser/web_ui_message_handler.h
+++ b/content/public/browser/web_ui_message_handler.h
@@ -5,9 +5,13 @@
#ifndef CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
#define CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
+#include <vector>
+
#include "base/gtest_prod_util.h"
+#include "base/logging.h"
#include "base/strings/string16.h"
#include "content/common/content_export.h"
+#include "content/public/browser/web_ui.h"
class GURL;
class WebUIBrowserTest;
@@ -15,6 +19,7 @@ class WebUIBrowserTest;
namespace base {
class DictionaryValue;
class ListValue;
+class Value;
}
namespace content {
@@ -27,7 +32,7 @@ class WebUIImpl;
// host is destroyed.
class CONTENT_EXPORT WebUIMessageHandler {
public:
- WebUIMessageHandler() : web_ui_(nullptr) {}
+ WebUIMessageHandler() : javascript_allowed_(false), web_ui_(nullptr) {}
virtual ~WebUIMessageHandler() {}
protected:
@@ -35,6 +40,12 @@ class CONTENT_EXPORT WebUIMessageHandler {
FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractDoubleValue);
FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractStringValue);
+ // Subclasses must call this once the page is ready for JavaScript calls
+ // from this handler.
+ void AllowJavascript();
+
+ bool IsJavascriptAllowed() const;
+
// Helper methods:
// Extract an integer value from a list Value.
@@ -47,14 +58,36 @@ class CONTENT_EXPORT WebUIMessageHandler {
// Extract a string value from a list Value.
static base::string16 ExtractStringValue(const base::ListValue* value);
- // Called when a RenderView is reused to display a page (i.e. reload).
- virtual void RenderViewReused() {}
-
// This is where subclasses specify which messages they'd like to handle and
// perform any additional initialization.. At this point web_ui() will return
// the associated WebUI object.
virtual void RegisterMessages() = 0;
+ // Will be called whenever JavaScript from this handler becomes allowed from
+ // the disallowed state. Subclasses should override this method to register
+ // observers that push JavaScript calls to the page.
+ virtual void OnJavascriptAllowed() {}
+
+ // Will be called whenever JavaScript from this handler becomes disallowed
+ // from the allowed state. This will never be called before
+ // OnJavascriptAllowed has been called. Subclasses should override this method
+ // to deregister or disabled observers that push JavaScript calls to the page.
+ virtual void OnJavascriptDisallowed() {}
+
+ // Call a Javascript function by sending its name and arguments down to
+ // the renderer. This is asynchronous; there's no way to get the result
+ // of the call, and should be thought of more like sending a message to
+ // the page.
+ // All function names in WebUI must consist of only ASCII characters.
+ // These functions will crash if JavaScript is not currently allowed.
+ template <typename... Values>
+ void CallJavascriptFunction(const std::string& function_name,
Dan Beam 2017/05/18 01:13:09 hey, does this HAVE to be in the .h?
+ const Values&... values) {
+ CHECK(IsJavascriptAllowed()) << "Cannot CallJavascriptFunction before "
+ "explicitly allowing JavaScript.";
+ web_ui()->CallJavascriptFunction(function_name, values...);
+ }
+
// Returns the attached WebUI for this handler.
WebUI* web_ui() const { return web_ui_; }
@@ -62,10 +95,17 @@ class CONTENT_EXPORT WebUIMessageHandler {
void set_web_ui(WebUI* web_ui) { web_ui_ = web_ui; }
private:
- // Provide external classes access to web_ui() and set_web_ui().
+ // Provide external classes access to web_ui(), set_web_ui(), and
+ // RenderViewReused.
friend class WebUIImpl;
friend class ::WebUIBrowserTest;
+ // Called when a RenderView is reused to display a page (i.e. reload).
+ void RenderViewReused();
+
+ // True if the page is for JavaScript calls from this handler.
+ bool javascript_allowed_;
+
WebUI* web_ui_;
};
« no previous file with comments | « content/public/browser/web_ui.h ('k') | content/public/test/test_web_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698