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

Side by Side Diff: chrome/renderer/resources/extensions/web_request_custom_bindings.js

Issue 15841013: Make miscellaneous_bindings and event_bindings Required as needed. Previously (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
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 // Custom binding for the webRequest API. 5 // Custom binding for the webRequest API.
6 6
7 var binding = require('binding').Binding.create('webRequest'); 7 var binding = require('binding').Binding.create('webRequest');
8 8
9 var webRequestNatives = requireNative('web_request'); 9 var eventBindings = require('event_bindings');
10 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName;
11
12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
13 var sendRequest = require('sendRequest').sendRequest; 10 var sendRequest = require('sendRequest').sendRequest;
14 var validate = require('schemaUtils').validate; 11 var validate = require('schemaUtils').validate;
15 var webRequestInternal = require('webRequestInternal').binding; 12 var webRequestInternal = require('webRequestInternal').binding;
13 var webRequestNatives = requireNative('web_request');
16 14
17 // WebRequestEvent object. This is used for special webRequest events with 15 // WebRequestEvent object. This is used for special webRequest events with
18 // extra parameters. Each invocation of addListener creates a new named 16 // extra parameters. Each invocation of addListener creates a new named
19 // sub-event. That sub-event is associated with the extra parameters in the 17 // sub-event. That sub-event is associated with the extra parameters in the
20 // browser process, so that only it is dispatched when the main event occurs 18 // browser process, so that only it is dispatched when the main event occurs
21 // matching the extra parameters. 19 // matching the extra parameters.
22 // 20 //
23 // Example: 21 // Example:
24 // chrome.webRequest.onBeforeRequest.addListener( 22 // chrome.webRequest.onBeforeRequest.addListener(
25 // callback, {urls: 'http://*.google.com/*'}); 23 // callback, {urls: 'http://*.google.com/*'});
26 // ^ callback will only be called for onBeforeRequests matching the filter. 24 // ^ callback will only be called for onBeforeRequests matching the filter.
27 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas, 25 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas,
28 opt_eventOptions, opt_webViewInstanceId) { 26 opt_eventOptions, opt_webViewInstanceId) {
29 if (typeof eventName != 'string') 27 if (typeof eventName != 'string')
30 throw new Error('chrome.WebRequestEvent requires an event name.'); 28 throw new Error('chrome.WebRequestEvent requires an event name.');
31 29
32 this.eventName_ = eventName; 30 this.eventName_ = eventName;
33 this.argSchemas_ = opt_argSchemas; 31 this.argSchemas_ = opt_argSchemas;
34 this.extraArgSchemas_ = opt_extraArgSchemas; 32 this.extraArgSchemas_ = opt_extraArgSchemas;
35 this.webViewInstanceId_ = opt_webViewInstanceId ? opt_webViewInstanceId : 0; 33 this.webViewInstanceId_ = opt_webViewInstanceId ? opt_webViewInstanceId : 0;
36 this.subEvents_ = []; 34 this.subEvents_ = [];
37 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); 35 this.eventOptions_ = eventBindings.parseEventOptions(opt_eventOptions);
38 if (this.eventOptions_.supportsRules) { 36 if (this.eventOptions_.supportsRules) {
39 this.eventForRules_ = 37 this.eventForRules_ =
40 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); 38 new eventBindings.Event(eventName, opt_argSchemas, opt_eventOptions);
41 } 39 }
42 } 40 }
43 41
44 // Test if the given callback is registered for this event. 42 // Test if the given callback is registered for this event.
45 WebRequestEvent.prototype.hasListener = function(cb) { 43 WebRequestEvent.prototype.hasListener = function(cb) {
46 if (!this.eventOptions_.supportsListeners) 44 if (!this.eventOptions_.supportsListeners)
47 throw new Error('This event does not support listeners.'); 45 throw new Error('This event does not support listeners.');
48 return this.findListener_(cb) > -1; 46 return this.findListener_(cb) > -1;
49 }; 47 };
50 48
51 // Test if any callbacks are registered fur thus event. 49 // Test if any callbacks are registered fur thus event.
52 WebRequestEvent.prototype.hasListeners = function() { 50 WebRequestEvent.prototype.hasListeners = function() {
53 if (!this.eventOptions_.supportsListeners) 51 if (!this.eventOptions_.supportsListeners)
54 throw new Error('This event does not support listeners.'); 52 throw new Error('This event does not support listeners.');
55 return this.subEvents_.length > 0; 53 return this.subEvents_.length > 0;
56 }; 54 };
57 55
58 // Registers a callback to be called when this event is dispatched. If 56 // Registers a callback to be called when this event is dispatched. If
59 // opt_filter is specified, then the callback is only called for events that 57 // opt_filter is specified, then the callback is only called for events that
60 // match the given filters. If opt_extraInfo is specified, the given optional 58 // match the given filters. If opt_extraInfo is specified, the given optional
61 // info is sent to the callback. 59 // info is sent to the callback.
62 WebRequestEvent.prototype.addListener = 60 WebRequestEvent.prototype.addListener =
63 function(cb, opt_filter, opt_extraInfo) { 61 function(cb, opt_filter, opt_extraInfo) {
64 if (!this.eventOptions_.supportsListeners) 62 if (!this.eventOptions_.supportsListeners)
65 throw new Error('This event does not support listeners.'); 63 throw new Error('This event does not support listeners.');
66 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does 64 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does
67 // not play well with event pages. See downloads.onDeterminingFilename and 65 // not play well with event pages. See downloads.onDeterminingFilename and
68 // ExtensionDownloadsEventRouter for an alternative approach. 66 // ExtensionDownloadsEventRouter for an alternative approach.
69 var subEventName = GetUniqueSubEventName(this.eventName_); 67 var subEventName = webRequestNatives.GetUniqueSubEventName(this.eventName_);
70 // Note: this could fail to validate, in which case we would not add the 68 // Note: this could fail to validate, in which case we would not add the
71 // subEvent listener. 69 // subEvent listener.
72 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); 70 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_);
73 webRequestInternal.addEventListener( 71 webRequestInternal.addEventListener(
74 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName, 72 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName,
75 this.webViewInstanceId_); 73 this.webViewInstanceId_);
76 74
77 var subEvent = new chrome.Event(subEventName, this.argSchemas_); 75 var subEvent = new eventBindings.Event(subEventName, this.argSchemas_);
78 var subEventCallback = cb; 76 var subEventCallback = cb;
79 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { 77 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) {
80 var eventName = this.eventName_; 78 var eventName = this.eventName_;
81 subEventCallback = function() { 79 subEventCallback = function() {
82 var requestId = arguments[0].requestId; 80 var requestId = arguments[0].requestId;
83 try { 81 try {
84 var result = cb.apply(null, arguments); 82 var result = cb.apply(null, arguments);
85 webRequestInternal.eventHandled( 83 webRequestInternal.eventHandled(
86 eventName, subEventName, requestId, result); 84 eventName, subEventName, requestId, result);
87 } catch (e) { 85 } catch (e) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 159
162 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { 160 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() {
163 var args = Array.prototype.slice.call(arguments); 161 var args = Array.prototype.slice.call(arguments);
164 sendRequest(this.name, args, this.definition.parameters, 162 sendRequest(this.name, args, this.definition.parameters,
165 {forIOThread: true}); 163 {forIOThread: true});
166 }); 164 });
167 }); 165 });
168 166
169 exports.binding = binding.generate(); 167 exports.binding = binding.generate();
170 exports.WebRequestEvent = WebRequestEvent; 168 exports.WebRequestEvent = WebRequestEvent;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698