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

Unified Diff: extensions/renderer/resources/web_view_events.js

Issue 596003002: Allow declarative webrequest and webrequest in extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up RuleRegistry in ExtensionsApiClient. Created 6 years, 3 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
Index: extensions/renderer/resources/web_view_events.js
diff --git a/extensions/renderer/resources/web_view_events.js b/extensions/renderer/resources/web_view_events.js
index 137baa2ee45e469dcaf1df7a4d9bcda1ecc83cfd..32b500ce23e83ca761c7223055845fa9db72216a 100644
--- a/extensions/renderer/resources/web_view_events.js
+++ b/extensions/renderer/resources/web_view_events.js
@@ -4,8 +4,14 @@
// Event management for WebViewInternal.
+var DeclarativeWebRequestSchema =
+ requireNative('schema_registry').GetSchema('declarativeWebRequest');
var EventBindings = require('event_bindings');
+var IdGenerator = requireNative('id_generator');
var MessagingNatives = requireNative('messaging_natives');
+var WebRequestEvent = require('webRequestInternal').WebRequestEvent;
+var WebRequestSchema =
+ requireNative('schema_registry').GetSchema('webRequest');
var WebView = require('webViewInternal').WebView;
var CreateEvent = function(name) {
@@ -15,6 +21,7 @@ var CreateEvent = function(name) {
var FrameNameChangedEvent = CreateEvent('webViewInternal.onFrameNameChanged');
var PluginDestroyedEvent = CreateEvent('webViewInternal.onPluginDestroyed');
+var WebRequestMessageEvent = CreateEvent('webViewInternal.onMessage');
// WEB_VIEW_EVENTS is a map of stable <webview> DOM event names to their
// associated extension event descriptor objects.
@@ -148,6 +155,25 @@ var WEB_VIEW_EVENTS = {
}
};
+function DeclarativeWebRequestEvent(opt_eventName,
+ opt_argSchemas,
+ opt_eventOptions,
+ opt_webViewInstanceId) {
+ var subEventName = opt_eventName + '/' + IdGenerator.GetNextId();
+ EventBindings.Event.call(this, subEventName, opt_argSchemas, opt_eventOptions,
+ opt_webViewInstanceId);
+
+ // TODO(lazyboy): When do we dispose this listener?
+ WebRequestMessageEvent.addListener(function() {
+ // Re-dispatch to subEvent's listeners.
+ $Function.apply(this.dispatch, this, $Array.slice(arguments));
+ }.bind(this), {instanceId: opt_webViewInstanceId || 0});
+}
+
+DeclarativeWebRequestEvent.prototype = {
+ __proto__: EventBindings.Event.prototype
+};
+
// Constructor.
function WebViewEvents(webViewInternal, viewInstanceId) {
this.webViewInternal = webViewInternal;
@@ -159,7 +185,7 @@ function WebViewEvents(webViewInternal, viewInstanceId) {
WebViewEvents.prototype.setup = function() {
this.setupFrameNameChangedEvent();
this.setupPluginDestroyedEvent();
- this.webViewInternal.maybeSetupChromeWebViewEvents();
+ this.setupWebRequestEvents();
this.webViewInternal.setupExperimentalContextMenus();
var events = this.getEvents();
@@ -180,6 +206,70 @@ WebViewEvents.prototype.setupPluginDestroyedEvent = function() {
}.bind(this), {instanceId: this.viewInstanceId});
};
+WebViewEvents.prototype.setupWebRequestEvents = function() {
+ var request = {};
+ var createWebRequestEvent = function(webRequestEvent) {
+ return function() {
+ if (!this[webRequestEvent.name]) {
+ this[webRequestEvent.name] =
+ new WebRequestEvent(
+ 'webViewInternal.' + webRequestEvent.name,
+ webRequestEvent.parameters,
+ webRequestEvent.extraParameters, webRequestEvent.options,
+ this.viewInstanceId);
+ }
+ return this[webRequestEvent.name];
+ }.bind(this);
+ }.bind(this);
+
+ var createDeclarativeWebRequestEvent = function(webRequestEvent) {
+ return function() {
+ if (!this[webRequestEvent.name]) {
+ // The onMessage event gets a special event type because we want
+ // the listener to fire only for messages targeted for this particular
+ // <webview>.
+ var EventClass = webRequestEvent.name === 'onMessage' ?
+ DeclarativeWebRequestEvent : EventBindings.Event;
+ this[webRequestEvent.name] =
+ new EventClass(
+ 'webViewInternal.' + webRequestEvent.name,
+ webRequestEvent.parameters,
+ webRequestEvent.options,
+ this.viewInstanceId);
+ }
+ return this[webRequestEvent.name];
+ }.bind(this);
+ }.bind(this);
+
+ for (var i = 0; i < DeclarativeWebRequestSchema.events.length; ++i) {
+ var eventSchema = DeclarativeWebRequestSchema.events[i];
+ var webRequestEvent = createDeclarativeWebRequestEvent(eventSchema);
+ Object.defineProperty(
+ request,
+ eventSchema.name,
+ {
+ get: webRequestEvent,
+ enumerable: true
+ }
+ );
+ }
+
+ // Populate the WebRequest events from the API definition.
+ for (var i = 0; i < WebRequestSchema.events.length; ++i) {
+ var webRequestEvent = createWebRequestEvent(WebRequestSchema.events[i]);
+ Object.defineProperty(
+ request,
+ WebRequestSchema.events[i].name,
+ {
+ get: webRequestEvent,
+ enumerable: true
+ }
+ );
+ }
+
+ this.webViewInternal.setRequestPropertyOnWebViewNode(request);
+};
+
WebViewEvents.prototype.getEvents = function() {
var experimentalEvents = this.webViewInternal.maybeGetExperimentalEvents();
for (var eventName in experimentalEvents) {

Powered by Google App Engine
This is Rietveld 408576698