diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-15 00:25:34 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-15 00:25:34 +0000 |
commit | 00023ad3c7d803535165e3a23c914342a7137dc4 (patch) | |
tree | b1147352c60adf0f9c5f8c0735100737fdddfde2 /chrome/renderer/resources | |
parent | 5832ff87058de4a6eec25fd2abb183134e834124 (diff) | |
download | chromium_src-00023ad3c7d803535165e3a23c914342a7137dc4.zip chromium_src-00023ad3c7d803535165e3a23c914342a7137dc4.tar.gz chromium_src-00023ad3c7d803535165e3a23c914342a7137dc4.tar.bz2 |
Add plumbing to webRequest API to support event filtering.
This introduces a new kind of event (WebRequestEvent) to handle the extra parameters to addListener. Behind the scenes, adding a listener creates a unique sub-event associated with that set of filter+extraInfo parameters. When we dispatch the event, we dispatch only those sub-events that match the required filters.
BUG=60101
TEST=no
Review URL: http://codereview.chromium.org/6250152
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74887 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/resources')
-rw-r--r-- | chrome/renderer/resources/extension_process_bindings.js | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index f09b40a..d6e08fe 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -18,6 +18,7 @@ var chrome = chrome || {}; native function SetIconCommon(); native function IsExtensionProcess(); native function IsIncognitoProcess(); + native function GetUniqueSubEventName(eventName); var chromeHidden = GetChromeHidden(); @@ -234,6 +235,63 @@ var chrome = chrome || {}; // --- Setup additional api's not currently handled in common/extensions/api + // WebRequestEvent object. This is used for special webRequest events with + // extra parameters. Each invocation of addListener creates a new named + // sub-event. That sub-event is associated with the extra parameters in the + // browser process, so that only it is dispatched when the main event occurs + // matching the extra parameters. + // + // Example: + // chrome.webRequest.onBeforeRequest.addListener( + // callback, {urls: "http://*.google.com/*"}); + // ^ callback will only be called for onBeforeRequests matching the filter. + chrome.WebRequestEvent = function(eventName, opt_argSchemas) { + if (typeof eventName != "string") + throw new Error("chrome.WebRequestEvent requires an event name."); + + this.eventName_ = eventName; + this.argSchemas_ = opt_argSchemas; + this.subEvents_ = []; + }; + + // Registers a callback to be called when this event is dispatched. If + // opt_filter is specified, then the callback is only called for events that + // match the given filters. If opt_extraInfo is specified, the given optional + // info is sent to the callback. + chrome.WebRequestEvent.prototype.addListener = + function(cb, opt_filter, opt_extraInfo) { + var subEventName = GetUniqueSubEventName(this.eventName_); + // Note: this could fail to validate, in which case we would not add the + // subEvent listener. + chrome.experimental.webRequest.addEventListener( + cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); + + var subEvent = new chrome.Event(subEventName, this.argSchemas_); + this.subEvents_.push(subEvent); + subEvent.addListener(cb); + }; + + // Unregisters a callback. + chrome.WebRequestEvent.prototype.removeListener = function(cb) { + var idx = this.findListener_(cb); + if (idx >= -1) { + return; + } + + this.subEvents_[idx].removeListener(cb); + if (!this.subEvents_[idx].hasListeners()) + this.subEvents_.splice(idx, 1); + }; + + chrome.WebRequestEvent.prototype.findListener_ = function(cb) { + for (var i = 0; i < this.subEvents_.length; i++) { + if (this.subEvents_[i].findListener_(cb) > -1) + return i; + } + + return -1; + }; + // Page action events send (pageActionId, {tabId, tabUrl}). function setupPageActionEvents(extensionId) { var pageActions = GetCurrentPageActions(extensionId); @@ -442,8 +500,14 @@ var chrome = chrome || {}; return; var eventName = apiDef.namespace + "." + eventDef.name; - module[eventDef.name] = new chrome.Event(eventName, - eventDef.parameters); + if (apiDef.namespace == "experimental.webRequest") { + // WebRequest events have a special structure. + module[eventDef.name] = new chrome.WebRequestEvent(eventName, + eventDef.parameters); + } else { + module[eventDef.name] = new chrome.Event(eventName, + eventDef.parameters); + } }); } |