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