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

Side by Side 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, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 // Event management for WebViewInternal. 5 // Event management for WebViewInternal.
6 6
7 var DeclarativeWebRequestSchema =
8 requireNative('schema_registry').GetSchema('declarativeWebRequest');
7 var EventBindings = require('event_bindings'); 9 var EventBindings = require('event_bindings');
10 var IdGenerator = requireNative('id_generator');
8 var MessagingNatives = requireNative('messaging_natives'); 11 var MessagingNatives = requireNative('messaging_natives');
12 var WebRequestEvent = require('webRequestInternal').WebRequestEvent;
13 var WebRequestSchema =
14 requireNative('schema_registry').GetSchema('webRequest');
9 var WebView = require('webViewInternal').WebView; 15 var WebView = require('webViewInternal').WebView;
10 16
11 var CreateEvent = function(name) { 17 var CreateEvent = function(name) {
12 var eventOpts = {supportsListeners: true, supportsFilters: true}; 18 var eventOpts = {supportsListeners: true, supportsFilters: true};
13 return new EventBindings.Event(name, undefined, eventOpts); 19 return new EventBindings.Event(name, undefined, eventOpts);
14 }; 20 };
15 21
16 var FrameNameChangedEvent = CreateEvent('webViewInternal.onFrameNameChanged'); 22 var FrameNameChangedEvent = CreateEvent('webViewInternal.onFrameNameChanged');
17 var PluginDestroyedEvent = CreateEvent('webViewInternal.onPluginDestroyed'); 23 var PluginDestroyedEvent = CreateEvent('webViewInternal.onPluginDestroyed');
24 var WebRequestMessageEvent = CreateEvent('webViewInternal.onMessage');
18 25
19 // WEB_VIEW_EVENTS is a map of stable <webview> DOM event names to their 26 // WEB_VIEW_EVENTS is a map of stable <webview> DOM event names to their
20 // associated extension event descriptor objects. 27 // associated extension event descriptor objects.
21 // An event listener will be attached to the extension event |evt| specified in 28 // An event listener will be attached to the extension event |evt| specified in
22 // the descriptor. 29 // the descriptor.
23 // |fields| specifies the public-facing fields in the DOM event that are 30 // |fields| specifies the public-facing fields in the DOM event that are
24 // accessible to <webview> developers. 31 // accessible to <webview> developers.
25 // |customHandler| allows a handler function to be called each time an extension 32 // |customHandler| allows a handler function to be called each time an extension
26 // event is caught by its event listener. The DOM event should be dispatched 33 // event is caught by its event listener. The DOM event should be dispatched
27 // within this handler function. With no handler function, the DOM event 34 // within this handler function. With no handler function, the DOM event
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 'unresponsive': { 148 'unresponsive': {
142 evt: CreateEvent('webViewInternal.onUnresponsive'), 149 evt: CreateEvent('webViewInternal.onUnresponsive'),
143 fields: ['processId'] 150 fields: ['processId']
144 }, 151 },
145 'zoomchange': { 152 'zoomchange': {
146 evt: CreateEvent('webViewInternal.onZoomChange'), 153 evt: CreateEvent('webViewInternal.onZoomChange'),
147 fields: ['oldZoomFactor', 'newZoomFactor'] 154 fields: ['oldZoomFactor', 'newZoomFactor']
148 } 155 }
149 }; 156 };
150 157
158 function DeclarativeWebRequestEvent(opt_eventName,
159 opt_argSchemas,
160 opt_eventOptions,
161 opt_webViewInstanceId) {
162 var subEventName = opt_eventName + '/' + IdGenerator.GetNextId();
163 EventBindings.Event.call(this, subEventName, opt_argSchemas, opt_eventOptions,
164 opt_webViewInstanceId);
165
166 // TODO(lazyboy): When do we dispose this listener?
167 WebRequestMessageEvent.addListener(function() {
168 // Re-dispatch to subEvent's listeners.
169 $Function.apply(this.dispatch, this, $Array.slice(arguments));
170 }.bind(this), {instanceId: opt_webViewInstanceId || 0});
171 }
172
173 DeclarativeWebRequestEvent.prototype = {
174 __proto__: EventBindings.Event.prototype
175 };
176
151 // Constructor. 177 // Constructor.
152 function WebViewEvents(webViewInternal, viewInstanceId) { 178 function WebViewEvents(webViewInternal, viewInstanceId) {
153 this.webViewInternal = webViewInternal; 179 this.webViewInternal = webViewInternal;
154 this.viewInstanceId = viewInstanceId; 180 this.viewInstanceId = viewInstanceId;
155 this.setup(); 181 this.setup();
156 } 182 }
157 183
158 // Sets up events. 184 // Sets up events.
159 WebViewEvents.prototype.setup = function() { 185 WebViewEvents.prototype.setup = function() {
160 this.setupFrameNameChangedEvent(); 186 this.setupFrameNameChangedEvent();
161 this.setupPluginDestroyedEvent(); 187 this.setupPluginDestroyedEvent();
162 this.webViewInternal.maybeSetupChromeWebViewEvents(); 188 this.setupWebRequestEvents();
163 this.webViewInternal.setupExperimentalContextMenus(); 189 this.webViewInternal.setupExperimentalContextMenus();
164 190
165 var events = this.getEvents(); 191 var events = this.getEvents();
166 for (var eventName in events) { 192 for (var eventName in events) {
167 this.setupEvent(eventName, events[eventName]); 193 this.setupEvent(eventName, events[eventName]);
168 } 194 }
169 }; 195 };
170 196
171 WebViewEvents.prototype.setupFrameNameChangedEvent = function() { 197 WebViewEvents.prototype.setupFrameNameChangedEvent = function() {
172 FrameNameChangedEvent.addListener(function(e) { 198 FrameNameChangedEvent.addListener(function(e) {
173 this.webViewInternal.onFrameNameChanged(e.name); 199 this.webViewInternal.onFrameNameChanged(e.name);
174 }.bind(this), {instanceId: this.viewInstanceId}); 200 }.bind(this), {instanceId: this.viewInstanceId});
175 }; 201 };
176 202
177 WebViewEvents.prototype.setupPluginDestroyedEvent = function() { 203 WebViewEvents.prototype.setupPluginDestroyedEvent = function() {
178 PluginDestroyedEvent.addListener(function(e) { 204 PluginDestroyedEvent.addListener(function(e) {
179 this.webViewInternal.onPluginDestroyed(); 205 this.webViewInternal.onPluginDestroyed();
180 }.bind(this), {instanceId: this.viewInstanceId}); 206 }.bind(this), {instanceId: this.viewInstanceId});
181 }; 207 };
182 208
209 WebViewEvents.prototype.setupWebRequestEvents = function() {
210 var request = {};
211 var createWebRequestEvent = function(webRequestEvent) {
212 return function() {
213 if (!this[webRequestEvent.name]) {
214 this[webRequestEvent.name] =
215 new WebRequestEvent(
216 'webViewInternal.' + webRequestEvent.name,
217 webRequestEvent.parameters,
218 webRequestEvent.extraParameters, webRequestEvent.options,
219 this.viewInstanceId);
220 }
221 return this[webRequestEvent.name];
222 }.bind(this);
223 }.bind(this);
224
225 var createDeclarativeWebRequestEvent = function(webRequestEvent) {
226 return function() {
227 if (!this[webRequestEvent.name]) {
228 // The onMessage event gets a special event type because we want
229 // the listener to fire only for messages targeted for this particular
230 // <webview>.
231 var EventClass = webRequestEvent.name === 'onMessage' ?
232 DeclarativeWebRequestEvent : EventBindings.Event;
233 this[webRequestEvent.name] =
234 new EventClass(
235 'webViewInternal.' + webRequestEvent.name,
236 webRequestEvent.parameters,
237 webRequestEvent.options,
238 this.viewInstanceId);
239 }
240 return this[webRequestEvent.name];
241 }.bind(this);
242 }.bind(this);
243
244 for (var i = 0; i < DeclarativeWebRequestSchema.events.length; ++i) {
245 var eventSchema = DeclarativeWebRequestSchema.events[i];
246 var webRequestEvent = createDeclarativeWebRequestEvent(eventSchema);
247 Object.defineProperty(
248 request,
249 eventSchema.name,
250 {
251 get: webRequestEvent,
252 enumerable: true
253 }
254 );
255 }
256
257 // Populate the WebRequest events from the API definition.
258 for (var i = 0; i < WebRequestSchema.events.length; ++i) {
259 var webRequestEvent = createWebRequestEvent(WebRequestSchema.events[i]);
260 Object.defineProperty(
261 request,
262 WebRequestSchema.events[i].name,
263 {
264 get: webRequestEvent,
265 enumerable: true
266 }
267 );
268 }
269
270 this.webViewInternal.setRequestPropertyOnWebViewNode(request);
271 };
272
183 WebViewEvents.prototype.getEvents = function() { 273 WebViewEvents.prototype.getEvents = function() {
184 var experimentalEvents = this.webViewInternal.maybeGetExperimentalEvents(); 274 var experimentalEvents = this.webViewInternal.maybeGetExperimentalEvents();
185 for (var eventName in experimentalEvents) { 275 for (var eventName in experimentalEvents) {
186 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName]; 276 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName];
187 } 277 }
188 var chromeEvents = this.webViewInternal.maybeGetChromeWebViewEvents(); 278 var chromeEvents = this.webViewInternal.maybeGetChromeWebViewEvents();
189 for (var eventName in chromeEvents) { 279 for (var eventName in chromeEvents) {
190 WEB_VIEW_EVENTS[eventName] = chromeEvents[eventName]; 280 WEB_VIEW_EVENTS[eventName] = chromeEvents[eventName];
191 } 281 }
192 return WEB_VIEW_EVENTS; 282 return WEB_VIEW_EVENTS;
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 } 612 }
523 }; 613 };
524 614
525 WebViewEvents.prototype.handleSizeChangedEvent = function( 615 WebViewEvents.prototype.handleSizeChangedEvent = function(
526 event, webViewEvent) { 616 event, webViewEvent) {
527 this.webViewInternal.onSizeChanged(webViewEvent); 617 this.webViewInternal.onSizeChanged(webViewEvent);
528 }; 618 };
529 619
530 exports.WebViewEvents = WebViewEvents; 620 exports.WebViewEvents = WebViewEvents;
531 exports.CreateEvent = CreateEvent; 621 exports.CreateEvent = CreateEvent;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698