| 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 binding for the webRequest API. | 5 // Custom bindings for the webRequest API. |
| 6 | |
| 7 var binding = require('binding').Binding.create('webRequest'); | |
| 8 | 6 |
| 9 var webRequestNatives = requireNative('web_request'); | 7 var webRequestNatives = requireNative('web_request'); |
| 10 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; | 8 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; |
| 11 | 9 |
| 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 13 var sendRequest = require('sendRequest').sendRequest; | 11 var sendRequest = require('sendRequest').sendRequest; |
| 14 var validate = require('schemaUtils').validate; | 12 var validate = require('schemaUtils').validate; |
| 15 var webRequestInternal = require('webRequestInternal').binding; | |
| 16 | 13 |
| 17 // WebRequestEvent object. This is used for special webRequest events with | 14 // WebRequestEvent object. This is used for special webRequest events with |
| 18 // extra parameters. Each invocation of addListener creates a new named | 15 // extra parameters. Each invocation of addListener creates a new named |
| 19 // 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 |
| 20 // 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 |
| 21 // matching the extra parameters. | 18 // matching the extra parameters. |
| 22 // | 19 // |
| 23 // Example: | 20 // Example: |
| 24 // chrome.webRequest.onBeforeRequest.addListener( | 21 // chrome.webRequest.onBeforeRequest.addListener( |
| 25 // callback, {urls: 'http://*.google.com/*'}); | 22 // callback, {urls: 'http://*.google.com/*'}); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // match the given filters. If opt_extraInfo is specified, the given optional | 56 // match the given filters. If opt_extraInfo is specified, the given optional |
| 60 // info is sent to the callback. | 57 // info is sent to the callback. |
| 61 WebRequestEvent.prototype.addListener = | 58 WebRequestEvent.prototype.addListener = |
| 62 function(cb, opt_filter, opt_extraInfo) { | 59 function(cb, opt_filter, opt_extraInfo) { |
| 63 if (!this.eventOptions_.supportsListeners) | 60 if (!this.eventOptions_.supportsListeners) |
| 64 throw new Error('This event does not support listeners.'); | 61 throw new Error('This event does not support listeners.'); |
| 65 var subEventName = GetUniqueSubEventName(this.eventName_); | 62 var subEventName = GetUniqueSubEventName(this.eventName_); |
| 66 // Note: this could fail to validate, in which case we would not add the | 63 // Note: this could fail to validate, in which case we would not add the |
| 67 // subEvent listener. | 64 // subEvent listener. |
| 68 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); | 65 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); |
| 69 webRequestInternal.addEventListener( | 66 chromeHidden.internalAPIs.webRequestInternal.addEventListener( |
| 70 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); | 67 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); |
| 71 | 68 |
| 72 var subEvent = new chrome.Event(subEventName, this.argSchemas_); | 69 var subEvent = new chrome.Event(subEventName, this.argSchemas_); |
| 73 var subEventCallback = cb; | 70 var subEventCallback = cb; |
| 74 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { | 71 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { |
| 75 var eventName = this.eventName_; | 72 var eventName = this.eventName_; |
| 76 subEventCallback = function() { | 73 subEventCallback = function() { |
| 77 var requestId = arguments[0].requestId; | 74 var requestId = arguments[0].requestId; |
| 78 try { | 75 try { |
| 79 var result = cb.apply(null, arguments); | 76 var result = cb.apply(null, arguments); |
| 80 webRequestInternal.eventHandled( | 77 chromeHidden.internalAPIs.webRequestInternal.eventHandled( |
| 81 eventName, subEventName, requestId, result); | 78 eventName, subEventName, requestId, result); |
| 82 } catch (e) { | 79 } catch (e) { |
| 83 webRequestInternal.eventHandled( | 80 chromeHidden.internalAPIs.webRequestInternal.eventHandled( |
| 84 eventName, subEventName, requestId); | 81 eventName, subEventName, requestId); |
| 85 throw e; | 82 throw e; |
| 86 } | 83 } |
| 87 }; | 84 }; |
| 88 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { | 85 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { |
| 89 var eventName = this.eventName_; | 86 var eventName = this.eventName_; |
| 90 subEventCallback = function() { | 87 subEventCallback = function() { |
| 91 var details = arguments[0]; | 88 var details = arguments[0]; |
| 92 var requestId = details.requestId; | 89 var requestId = details.requestId; |
| 93 var handledCallback = function(response) { | 90 var handledCallback = function(response) { |
| 94 webRequestInternal.eventHandled( | 91 chromeHidden.internalAPIs.webRequestInternal.eventHandled( |
| 95 eventName, subEventName, requestId, response); | 92 eventName, subEventName, requestId, response); |
| 96 }; | 93 }; |
| 97 cb.apply(null, [details, handledCallback]); | 94 cb.apply(null, [details, handledCallback]); |
| 98 }; | 95 }; |
| 99 } | 96 } |
| 100 this.subEvents_.push( | 97 this.subEvents_.push( |
| 101 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); | 98 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); |
| 102 subEvent.addListener(subEventCallback); | 99 subEvent.addListener(subEventCallback); |
| 103 }; | 100 }; |
| 104 | 101 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 throw new Error('This event does not support rules.'); | 139 throw new Error('This event does not support rules.'); |
| 143 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); | 140 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); |
| 144 } | 141 } |
| 145 | 142 |
| 146 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { | 143 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { |
| 147 if (!this.eventOptions_.supportsRules) | 144 if (!this.eventOptions_.supportsRules) |
| 148 throw new Error('This event does not support rules.'); | 145 throw new Error('This event does not support rules.'); |
| 149 this.eventForRules_.getRules(ruleIdentifiers, cb); | 146 this.eventForRules_.getRules(ruleIdentifiers, cb); |
| 150 } | 147 } |
| 151 | 148 |
| 152 binding.registerCustomEvent(WebRequestEvent); | 149 chromeHidden.registerCustomEvent('webRequest', WebRequestEvent); |
| 153 | 150 |
| 154 binding.registerCustomHook(function(api) { | 151 chromeHidden.registerCustomHook('webRequest', function(api) { |
| 155 var apiFunctions = api.apiFunctions; | 152 var apiFunctions = api.apiFunctions; |
| 156 | 153 |
| 157 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { | 154 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { |
| 158 var args = Array.prototype.slice.call(arguments); | 155 var args = Array.prototype.slice.call(arguments); |
| 159 sendRequest(this.name, args, this.definition.parameters, | 156 sendRequest(this.name, args, this.definition.parameters, |
| 160 {forIOThread: true}); | 157 {forIOThread: true}); |
| 161 }); | 158 }); |
| 162 }); | 159 }); |
| 163 | |
| 164 exports.binding = binding.generate(); | |
| OLD | NEW |