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 webRequestInternal API. | 5 // Custom binding for the webRequestInternal API. |
6 | 6 |
7 var binding = require('binding').Binding.create('webRequestInternal'); | 7 var binding = require('binding').Binding.create('webRequestInternal'); |
8 var eventBindings = require('event_bindings'); | 8 var eventBindings = require('event_bindings'); |
9 var sendRequest = require('sendRequest').sendRequest; | 9 var sendRequest = require('sendRequest').sendRequest; |
10 var validate = require('schemaUtils').validate; | 10 var validate = require('schemaUtils').validate; |
11 var utils = require('utils'); | 11 var utils = require('utils'); |
12 var idGeneratorNatives = requireNative('id_generator'); | 12 var idGeneratorNatives = requireNative('id_generator'); |
13 | 13 |
14 var webRequestInternal; | 14 var webRequestInternal; |
15 | 15 |
16 function GetUniqueSubEventName(eventName) { | 16 function GetUniqueSubEventName(eventName) { |
17 return eventName + "/" + idGeneratorNatives.GetNextId(); | 17 return eventName + '/' + idGeneratorNatives.GetNextId(); |
18 } | 18 } |
19 | 19 |
20 // WebRequestEventImpl object. This is used for special webRequest events | 20 // WebRequestEventImpl object. This is used for special webRequest events |
21 // with extra parameters. Each invocation of addListener creates a new named | 21 // with extra parameters. Each invocation of addListener creates a new named |
22 // sub-event. That sub-event is associated with the extra parameters in the | 22 // sub-event. That sub-event is associated with the extra parameters in the |
23 // browser process, so that only it is dispatched when the main event occurs | 23 // browser process, so that only it is dispatched when the main event occurs |
24 // matching the extra parameters. | 24 // matching the extra parameters. |
25 // | 25 // |
26 // Example: | 26 // Example: |
27 // chrome.webRequest.onBeforeRequest.addListener( | 27 // chrome.webRequest.onBeforeRequest.addListener( |
28 // callback, {urls: 'http://*.google.com/*'}); | 28 // callback, {urls: 'http://*.google.com/*'}); |
29 // ^ callback will only be called for onBeforeRequests matching the filter. | 29 // ^ callback will only be called for onBeforeRequests matching the filter. |
30 function WebRequestEventImpl(eventName, opt_argSchemas, opt_extraArgSchemas, | 30 function WebRequestEventImpl(eventName, opt_argSchemas, opt_extraArgSchemas, |
31 opt_eventOptions, opt_webViewInstanceId) { | 31 opt_eventOptions, opt_webViewInstanceId) { |
32 if (typeof eventName != 'string') | 32 if (typeof eventName != 'string') |
33 throw new Error('chrome.WebRequestEvent requires an event name.'); | 33 throw new Error('chrome.WebRequestEvent requires an event name.'); |
34 | 34 |
35 this.eventName = eventName; | 35 this.eventName = eventName; |
36 this.argSchemas = opt_argSchemas; | 36 this.argSchemas = opt_argSchemas; |
37 this.extraArgSchemas = opt_extraArgSchemas; | 37 this.extraArgSchemas = opt_extraArgSchemas; |
38 this.webViewInstanceId = opt_webViewInstanceId || 0; | 38 this.webViewInstanceId = opt_webViewInstanceId || 0; |
39 this.subEvents = []; | 39 this.subEvents = []; |
40 this.eventOptions = eventBindings.parseEventOptions(opt_eventOptions); | 40 this.eventOptions = eventBindings.parseEventOptions(opt_eventOptions); |
41 if (this.eventOptions.supportsRules) { | 41 if (this.eventOptions.supportsRules) { |
42 this.eventForRules = | 42 this.eventForRules = |
43 new eventBindings.Event(eventName, opt_argSchemas, opt_eventOptions, | 43 new eventBindings.Event(eventName, opt_argSchemas, opt_eventOptions, |
44 opt_webViewInstanceId); | 44 opt_webViewInstanceId); |
45 } | 45 } |
46 } | 46 } |
| 47 $Object.setPrototypeOf(WebRequestEventImpl.prototype, null); |
47 | 48 |
48 // Test if the given callback is registered for this event. | 49 // Test if the given callback is registered for this event. |
49 WebRequestEventImpl.prototype.hasListener = function(cb) { | 50 WebRequestEventImpl.prototype.hasListener = function(cb) { |
50 if (!this.eventOptions.supportsListeners) | 51 if (!this.eventOptions.supportsListeners) |
51 throw new Error('This event does not support listeners.'); | 52 throw new Error('This event does not support listeners.'); |
52 return this.findListener_(cb) > -1; | 53 return this.findListener_(cb) > -1; |
53 }; | 54 }; |
54 | 55 |
55 // Test if any callbacks are registered fur thus event. | 56 // Test if any callbacks are registered fur thus event. |
56 WebRequestEventImpl.prototype.hasListeners = function() { | 57 WebRequestEventImpl.prototype.hasListeners = function() { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 'removeListener', | 187 'removeListener', |
187 'addRules', | 188 'addRules', |
188 'removeRules', | 189 'removeRules', |
189 'getRules', | 190 'getRules', |
190 ], | 191 ], |
191 }); | 192 }); |
192 | 193 |
193 webRequestInternal = binding.generate(); | 194 webRequestInternal = binding.generate(); |
194 exports.$set('binding', webRequestInternal); | 195 exports.$set('binding', webRequestInternal); |
195 exports.$set('WebRequestEvent', WebRequestEvent); | 196 exports.$set('WebRequestEvent', WebRequestEvent); |
OLD | NEW |