Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 bindings for the webRequest API. | 5 // Custom bindings for the webRequest API. |
| 6 | 6 |
| 7 (function() { | 7 (function() { |
| 8 | 8 |
| 9 native function GetChromeHidden(); | 9 native function GetChromeHidden(); |
| 10 native function GetUniqueSubEventName(eventName); | 10 native function GetUniqueSubEventName(eventName); |
| 11 | 11 |
| 12 var chromeHidden = GetChromeHidden(); | 12 var chromeHidden = GetChromeHidden(); |
| 13 | 13 |
| 14 // WebRequestEvent object. This is used for special webRequest events with | 14 // WebRequestEvent object. This is used for special webRequest events with |
| 15 // extra parameters. Each invocation of addListener creates a new named | 15 // extra parameters. Each invocation of addListener creates a new named |
| 16 // sub-event. That sub-event is associated with the extra parameters in the | 16 // sub-event. That sub-event is associated with the extra parameters in the |
| 17 // browser process, so that only it is dispatched when the main event occurs | 17 // browser process, so that only it is dispatched when the main event occurs |
| 18 // matching the extra parameters. | 18 // matching the extra parameters. |
| 19 // | 19 // |
| 20 // Example: | 20 // Example: |
| 21 // chrome.webRequest.onBeforeRequest.addListener( | 21 // chrome.webRequest.onBeforeRequest.addListener( |
| 22 // callback, {urls: "http://*.google.com/*"}); | 22 // callback, {urls: "http://*.google.com/*"}); |
| 23 // ^ callback will only be called for onBeforeRequests matching the filter. | 23 // ^ callback will only be called for onBeforeRequests matching the filter. |
| 24 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas) { | 24 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas, |
| 25 opt_eventOptions, opt_typesAPI) { | |
| 25 if (typeof eventName != "string") | 26 if (typeof eventName != "string") |
| 26 throw new Error("chrome.WebRequestEvent requires an event name."); | 27 throw new Error("chrome.WebRequestEvent requires an event name."); |
| 27 | 28 |
| 28 this.eventName_ = eventName; | 29 this.eventName_ = eventName; |
| 29 this.argSchemas_ = opt_argSchemas; | 30 this.argSchemas_ = opt_argSchemas; |
| 30 this.extraArgSchemas_ = opt_extraArgSchemas; | 31 this.extraArgSchemas_ = opt_extraArgSchemas; |
| 31 this.subEvents_ = []; | 32 this.subEvents_ = []; |
| 33 this.eventOptions_ = opt_eventOptions || | |
| 34 {"supportsListeners": true, "supportsRules": false}; | |
|
Matt Perry
2012/01/24 22:39:10
indent 2 more
battre
2012/01/25 19:25:08
Done.
| |
| 35 | |
| 36 if (this.eventOptions_.supportsRules) | |
| 37 this.eventForRules_ = | |
| 38 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions, | |
| 39 opt_typesAPI); | |
| 32 }; | 40 }; |
| 33 | 41 |
| 34 // Test if the given callback is registered for this event. | 42 // Test if the given callback is registered for this event. |
| 35 WebRequestEvent.prototype.hasListener = function(cb) { | 43 WebRequestEvent.prototype.hasListener = function(cb) { |
| 44 if (!this.eventOptions_.supportsListeners) | |
| 45 throw new Error("This event does not support listeners."); | |
| 36 return this.findListener_(cb) > -1; | 46 return this.findListener_(cb) > -1; |
| 37 }; | 47 }; |
| 38 | 48 |
| 39 // Test if any callbacks are registered fur thus event. | 49 // Test if any callbacks are registered fur thus event. |
| 40 WebRequestEvent.prototype.hasListeners = function() { | 50 WebRequestEvent.prototype.hasListeners = function() { |
| 51 if (!this.eventOptions_.supportsListeners) | |
| 52 throw new Error("This event does not support listeners."); | |
| 41 return this.subEvents_.length > 0; | 53 return this.subEvents_.length > 0; |
| 42 }; | 54 }; |
| 43 | 55 |
| 44 // 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 |
| 45 // 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 |
| 46 // 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 |
| 47 // info is sent to the callback. | 59 // info is sent to the callback. |
| 48 WebRequestEvent.prototype.addListener = | 60 WebRequestEvent.prototype.addListener = |
| 49 function(cb, opt_filter, opt_extraInfo) { | 61 function(cb, opt_filter, opt_extraInfo) { |
| 62 if (!this.eventOptions_.supportsListeners) | |
| 63 throw new Error("This event does not support listeners."); | |
| 50 var subEventName = GetUniqueSubEventName(this.eventName_); | 64 var subEventName = GetUniqueSubEventName(this.eventName_); |
| 51 // Note: this could fail to validate, in which case we would not add the | 65 // Note: this could fail to validate, in which case we would not add the |
| 52 // subEvent listener. | 66 // subEvent listener. |
| 53 chromeHidden.validate(Array.prototype.slice.call(arguments, 1), | 67 chromeHidden.validate(Array.prototype.slice.call(arguments, 1), |
| 54 this.extraArgSchemas_); | 68 this.extraArgSchemas_); |
| 55 chrome.webRequest.addEventListener( | 69 chrome.webRequest.addEventListener( |
| 56 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); | 70 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); |
| 57 | 71 |
| 58 var subEvent = new chrome.Event(subEventName, this.argSchemas_); | 72 var subEvent = new chrome.Event(subEventName, this.argSchemas_); |
| 59 var subEventCallback = cb; | 73 var subEventCallback = cb; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 83 cb.apply(null, [details, handledCallback]); | 97 cb.apply(null, [details, handledCallback]); |
| 84 }; | 98 }; |
| 85 } | 99 } |
| 86 this.subEvents_.push( | 100 this.subEvents_.push( |
| 87 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); | 101 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); |
| 88 subEvent.addListener(subEventCallback); | 102 subEvent.addListener(subEventCallback); |
| 89 }; | 103 }; |
| 90 | 104 |
| 91 // Unregisters a callback. | 105 // Unregisters a callback. |
| 92 WebRequestEvent.prototype.removeListener = function(cb) { | 106 WebRequestEvent.prototype.removeListener = function(cb) { |
| 107 if (!this.eventOptions_.supportsListeners) | |
| 108 throw new Error("This event does not support listeners."); | |
| 93 var idx; | 109 var idx; |
| 94 while ((idx = this.findListener_(cb)) >= 0) { | 110 while ((idx = this.findListener_(cb)) >= 0) { |
| 95 var e = this.subEvents_[idx]; | 111 var e = this.subEvents_[idx]; |
| 96 e.subEvent.removeListener(e.subEventCallback); | 112 e.subEvent.removeListener(e.subEventCallback); |
| 97 if (e.subEvent.hasListeners()) { | 113 if (e.subEvent.hasListeners()) { |
| 98 console.error( | 114 console.error( |
| 99 "Internal error: webRequest subEvent has orphaned listeners."); | 115 "Internal error: webRequest subEvent has orphaned listeners."); |
| 100 } | 116 } |
| 101 this.subEvents_.splice(idx, 1); | 117 this.subEvents_.splice(idx, 1); |
| 102 } | 118 } |
| 103 }; | 119 }; |
| 104 | 120 |
| 105 WebRequestEvent.prototype.findListener_ = function(cb) { | 121 WebRequestEvent.prototype.findListener_ = function(cb) { |
| 106 for (var i in this.subEvents_) { | 122 for (var i in this.subEvents_) { |
| 107 var e = this.subEvents_[i]; | 123 var e = this.subEvents_[i]; |
| 108 if (e.callback === cb) { | 124 if (e.callback === cb) { |
| 109 if (e.subEvent.findListener_(e.subEventCallback) > -1) | 125 if (e.subEvent.findListener_(e.subEventCallback) > -1) |
| 110 return i; | 126 return i; |
| 111 console.error("Internal error: webRequest subEvent has no callback."); | 127 console.error("Internal error: webRequest subEvent has no callback."); |
| 112 } | 128 } |
| 113 } | 129 } |
| 114 | 130 |
| 115 return -1; | 131 return -1; |
| 116 }; | 132 }; |
| 117 | 133 |
| 118 chromeHidden.registerCustomEvent('webRequest', WebRequestEvent); | 134 WebRequestEvent.prototype.addRules = function(rules, opt_cb) { |
| 135 if (!this.eventOptions_.supportsRules) | |
| 136 throw new Error("This event does not support listeners."); | |
|
Matt Perry
2012/01/24 22:39:10
s/listeners/rules
battre
2012/01/25 19:25:08
Done.
| |
| 137 this.eventForRules_.addRules(rules, opt_cb); | |
| 138 } | |
| 119 | 139 |
| 120 chromeHidden.registerCustomHook('webRequest', function(api) { | 140 WebRequestEvent.prototype.removeRules = function(rule_identifiers, opt_cb) { |
| 141 if (!this.eventOptions_.supportsRules) | |
| 142 throw new Error("This event does not support listeners."); | |
| 143 this.eventForRules_.removeRules(rule_identifiers, opt_cb); | |
| 144 } | |
| 145 | |
| 146 WebRequestEvent.prototype.getRules = function(rule_identifiers, cb) { | |
| 147 if (!this.eventOptions_.supportsRules) | |
| 148 throw new Error("This event does not support listeners."); | |
| 149 this.eventForRules_.getRules(rule_identifiers, cb); | |
| 150 } | |
| 151 | |
| 152 chromeHidden.registerCustomEvent("webRequest", WebRequestEvent); | |
| 153 | |
| 154 chromeHidden.registerCustomHook("webRequest", function(api) { | |
| 121 var apiFunctions = api.apiFunctions; | 155 var apiFunctions = api.apiFunctions; |
| 122 var sendRequest = api.sendRequest; | 156 var sendRequest = api.sendRequest; |
| 123 | 157 |
| 124 apiFunctions.setHandleRequest("webRequest.addEventListener", | 158 apiFunctions.setHandleRequest("webRequest.addEventListener", |
| 125 function() { | 159 function() { |
| 126 var args = Array.prototype.slice.call(arguments); | 160 var args = Array.prototype.slice.call(arguments); |
| 127 sendRequest(this.name, args, this.definition.parameters, | 161 sendRequest(this.name, args, this.definition.parameters, |
| 128 {forIOThread: true}); | 162 {forIOThread: true}); |
| 129 }); | 163 }); |
| 130 | 164 |
| 131 apiFunctions.setHandleRequest("webRequest.eventHandled", | 165 apiFunctions.setHandleRequest("webRequest.eventHandled", |
| 132 function() { | 166 function() { |
| 133 var args = Array.prototype.slice.call(arguments); | 167 var args = Array.prototype.slice.call(arguments); |
| 134 sendRequest(this.name, args, this.definition.parameters, | 168 sendRequest(this.name, args, this.definition.parameters, |
| 135 {forIOThread: true}); | 169 {forIOThread: true}); |
| 136 }); | 170 }); |
| 137 | 171 |
| 138 apiFunctions.setHandleRequest("webRequest.handlerBehaviorChanged", | 172 apiFunctions.setHandleRequest("webRequest.handlerBehaviorChanged", |
| 139 function() { | 173 function() { |
| 140 var args = Array.prototype.slice.call(arguments); | 174 var args = Array.prototype.slice.call(arguments); |
| 141 sendRequest(this.name, args, this.definition.parameters, | 175 sendRequest(this.name, args, this.definition.parameters, |
| 142 {forIOThread: true}); | 176 {forIOThread: true}); |
| 143 }); | 177 }); |
| 144 }); | 178 }); |
| 145 | 179 |
| 146 })(); | 180 })(); |
| OLD | NEW |