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 var webRequestNatives = requireNative('web_request'); | 7 var webRequestNatives = requireNative('web_request'); |
| 8 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; | 8 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; |
| 9 | 9 |
| 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 11 var sendRequest = require('sendRequest').sendRequest; | 11 var sendRequest = require('sendRequest').sendRequest; |
| 12 var validate = require('schemaUtils').validate; | 12 var validate = require('schemaUtils').validate; |
| 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) { | 25 opt_eventOptions, opt_webViewInstanceId, opt_routeId) { |
|
Matt Perry
2013/02/06 22:40:24
remove opt_routeId
Fady Samuel
2013/05/15 21:58:53
Done.
| |
| 26 if (typeof eventName != 'string') | 26 if (typeof eventName != 'string') |
| 27 throw new Error('chrome.WebRequestEvent requires an event name.'); | 27 throw new Error('chrome.WebRequestEvent requires an event name.'); |
| 28 | 28 |
| 29 this.eventName_ = eventName; | 29 this.eventName_ = eventName; |
| 30 this.argSchemas_ = opt_argSchemas; | 30 this.argSchemas_ = opt_argSchemas; |
| 31 this.extraArgSchemas_ = opt_extraArgSchemas; | 31 this.extraArgSchemas_ = opt_extraArgSchemas; |
| 32 this.webViewInstanceId_ = opt_webViewInstanceId ? opt_webViewInstanceId : 0; | |
| 32 this.subEvents_ = []; | 33 this.subEvents_ = []; |
| 33 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); | 34 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); |
| 34 if (this.eventOptions_.supportsRules) { | 35 if (this.eventOptions_.supportsRules) { |
| 35 this.eventForRules_ = | 36 this.eventForRules_ = |
| 36 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); | 37 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); |
| 37 } | 38 } |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 // Test if the given callback is registered for this event. | 41 // Test if the given callback is registered for this event. |
| 41 WebRequestEvent.prototype.hasListener = function(cb) { | 42 WebRequestEvent.prototype.hasListener = function(cb) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 57 // info is sent to the callback. | 58 // info is sent to the callback. |
| 58 WebRequestEvent.prototype.addListener = | 59 WebRequestEvent.prototype.addListener = |
| 59 function(cb, opt_filter, opt_extraInfo) { | 60 function(cb, opt_filter, opt_extraInfo) { |
| 60 if (!this.eventOptions_.supportsListeners) | 61 if (!this.eventOptions_.supportsListeners) |
| 61 throw new Error('This event does not support listeners.'); | 62 throw new Error('This event does not support listeners.'); |
| 62 var subEventName = GetUniqueSubEventName(this.eventName_); | 63 var subEventName = GetUniqueSubEventName(this.eventName_); |
| 63 // Note: this could fail to validate, in which case we would not add the | 64 // Note: this could fail to validate, in which case we would not add the |
| 64 // subEvent listener. | 65 // subEvent listener. |
| 65 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); | 66 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); |
| 66 chromeHidden.internalAPIs.webRequestInternal.addEventListener( | 67 chromeHidden.internalAPIs.webRequestInternal.addEventListener( |
| 67 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); | 68 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName, |
| 69 this.webViewInstanceId_); | |
| 68 | 70 |
| 69 var subEvent = new chrome.Event(subEventName, this.argSchemas_); | 71 var subEvent = new chrome.Event(subEventName, this.argSchemas_); |
| 70 var subEventCallback = cb; | 72 var subEventCallback = cb; |
| 71 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { | 73 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { |
| 72 var eventName = this.eventName_; | 74 var eventName = this.eventName_; |
| 73 subEventCallback = function() { | 75 subEventCallback = function() { |
| 74 var requestId = arguments[0].requestId; | 76 var requestId = arguments[0].requestId; |
| 75 try { | 77 try { |
| 76 var result = cb.apply(null, arguments); | 78 var result = cb.apply(null, arguments); |
| 77 chromeHidden.internalAPIs.webRequestInternal.eventHandled( | 79 chromeHidden.internalAPIs.webRequestInternal.eventHandled( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 | 152 |
| 151 chromeHidden.registerCustomHook('webRequest', function(api) { | 153 chromeHidden.registerCustomHook('webRequest', function(api) { |
| 152 var apiFunctions = api.apiFunctions; | 154 var apiFunctions = api.apiFunctions; |
| 153 | 155 |
| 154 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { | 156 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { |
| 155 var args = Array.prototype.slice.call(arguments); | 157 var args = Array.prototype.slice.call(arguments); |
| 156 sendRequest(this.name, args, this.definition.parameters, | 158 sendRequest(this.name, args, this.definition.parameters, |
| 157 {forIOThread: true}); | 159 {forIOThread: true}); |
| 158 }); | 160 }); |
| 159 }); | 161 }); |
| 162 | |
| 163 exports.webRequestEvent = WebRequestEvent; | |
| OLD | NEW |