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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // match the given filters. If opt_extraInfo is specified, the given optional | 59 // match the given filters. If opt_extraInfo is specified, the given optional |
57 // info is sent to the callback. | 60 // info is sent to the callback. |
58 WebRequestEvent.prototype.addListener = | 61 WebRequestEvent.prototype.addListener = |
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 var subEventName = GetUniqueSubEventName(this.eventName_); | 65 var subEventName = GetUniqueSubEventName(this.eventName_); |
63 // Note: this could fail to validate, in which case we would not add the | 66 // Note: this could fail to validate, in which case we would not add the |
64 // subEvent listener. | 67 // subEvent listener. |
65 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); | 68 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); |
66 chromeHidden.internalAPIs.webRequestInternal.addEventListener( | 69 webRequestInternal.addEventListener( |
67 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); | 70 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); |
68 | 71 |
69 var subEvent = new chrome.Event(subEventName, this.argSchemas_); | 72 var subEvent = new chrome.Event(subEventName, this.argSchemas_); |
70 var subEventCallback = cb; | 73 var subEventCallback = cb; |
71 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { | 74 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { |
72 var eventName = this.eventName_; | 75 var eventName = this.eventName_; |
73 subEventCallback = function() { | 76 subEventCallback = function() { |
74 var requestId = arguments[0].requestId; | 77 var requestId = arguments[0].requestId; |
75 try { | 78 try { |
76 var result = cb.apply(null, arguments); | 79 var result = cb.apply(null, arguments); |
77 chromeHidden.internalAPIs.webRequestInternal.eventHandled( | 80 webRequestInternal.eventHandled( |
78 eventName, subEventName, requestId, result); | 81 eventName, subEventName, requestId, result); |
79 } catch (e) { | 82 } catch (e) { |
80 chromeHidden.internalAPIs.webRequestInternal.eventHandled( | 83 webRequestInternal.eventHandled( |
81 eventName, subEventName, requestId); | 84 eventName, subEventName, requestId); |
82 throw e; | 85 throw e; |
83 } | 86 } |
84 }; | 87 }; |
85 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { | 88 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { |
86 var eventName = this.eventName_; | 89 var eventName = this.eventName_; |
87 subEventCallback = function() { | 90 subEventCallback = function() { |
88 var details = arguments[0]; | 91 var details = arguments[0]; |
89 var requestId = details.requestId; | 92 var requestId = details.requestId; |
90 var handledCallback = function(response) { | 93 var handledCallback = function(response) { |
91 chromeHidden.internalAPIs.webRequestInternal.eventHandled( | 94 webRequestInternal.eventHandled( |
92 eventName, subEventName, requestId, response); | 95 eventName, subEventName, requestId, response); |
93 }; | 96 }; |
94 cb.apply(null, [details, handledCallback]); | 97 cb.apply(null, [details, handledCallback]); |
95 }; | 98 }; |
96 } | 99 } |
97 this.subEvents_.push( | 100 this.subEvents_.push( |
98 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); | 101 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); |
99 subEvent.addListener(subEventCallback); | 102 subEvent.addListener(subEventCallback); |
100 }; | 103 }; |
101 | 104 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 throw new Error('This event does not support rules.'); | 142 throw new Error('This event does not support rules.'); |
140 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); | 143 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); |
141 } | 144 } |
142 | 145 |
143 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { | 146 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { |
144 if (!this.eventOptions_.supportsRules) | 147 if (!this.eventOptions_.supportsRules) |
145 throw new Error('This event does not support rules.'); | 148 throw new Error('This event does not support rules.'); |
146 this.eventForRules_.getRules(ruleIdentifiers, cb); | 149 this.eventForRules_.getRules(ruleIdentifiers, cb); |
147 } | 150 } |
148 | 151 |
149 chromeHidden.registerCustomEvent('webRequest', WebRequestEvent); | 152 binding.registerCustomEvent(WebRequestEvent); |
150 | 153 |
151 chromeHidden.registerCustomHook('webRequest', function(api) { | 154 binding.registerCustomHook(function(api) { |
152 var apiFunctions = api.apiFunctions; | 155 var apiFunctions = api.apiFunctions; |
153 | 156 |
154 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { | 157 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { |
155 var args = Array.prototype.slice.call(arguments); | 158 var args = Array.prototype.slice.call(arguments); |
156 sendRequest(this.name, args, this.definition.parameters, | 159 sendRequest(this.name, args, this.definition.parameters, |
157 {forIOThread: true}); | 160 {forIOThread: true}); |
158 }); | 161 }); |
159 }); | 162 }); |
| 163 |
| 164 exports.binding = binding.generate(); |
OLD | NEW |