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 binding for the webRequest API. |
6 | 6 |
7 var binding = require('binding').Binding.create('webRequest'); | 7 var binding = require('binding').Binding.create('webRequest'); |
8 | 8 |
9 var webRequestNatives = requireNative('web_request'); | 9 var webRequestNatives = requireNative('web_request'); |
10 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; | 10 var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName; |
11 | 11 |
12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
13 var sendRequest = require('sendRequest').sendRequest; | 13 var sendRequest = require('sendRequest').sendRequest; |
14 var validate = require('schemaUtils').validate; | 14 var validate = require('schemaUtils').validate; |
15 var webRequestInternal = require('webRequestInternal').binding; | 15 var webRequestInternal = require('webRequestInternal').binding; |
16 | 16 |
17 // WebRequestEvent object. This is used for special webRequest events with | 17 // WebRequestEvent object. This is used for special webRequest events with |
18 // extra parameters. Each invocation of addListener creates a new named | 18 // extra parameters. Each invocation of addListener creates a new named |
19 // 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 |
20 // 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 |
21 // matching the extra parameters. | 21 // matching the extra parameters. |
22 // | 22 // |
23 // Example: | 23 // Example: |
24 // chrome.webRequest.onBeforeRequest.addListener( | 24 // chrome.webRequest.onBeforeRequest.addListener( |
25 // callback, {urls: 'http://*.google.com/*'}); | 25 // callback, {urls: 'http://*.google.com/*'}); |
26 // ^ callback will only be called for onBeforeRequests matching the filter. | 26 // ^ callback will only be called for onBeforeRequests matching the filter. |
27 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas, | 27 function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas, |
28 opt_eventOptions) { | 28 opt_eventOptions, opt_webViewInstanceId) { |
29 if (typeof eventName != 'string') | 29 if (typeof eventName != 'string') |
30 throw new Error('chrome.WebRequestEvent requires an event name.'); | 30 throw new Error('chrome.WebRequestEvent requires an event name.'); |
31 | 31 |
32 this.eventName_ = eventName; | 32 this.eventName_ = eventName; |
33 this.argSchemas_ = opt_argSchemas; | 33 this.argSchemas_ = opt_argSchemas; |
34 this.extraArgSchemas_ = opt_extraArgSchemas; | 34 this.extraArgSchemas_ = opt_extraArgSchemas; |
| 35 this.webViewInstanceId_ = opt_webViewInstanceId ? opt_webViewInstanceId : 0; |
35 this.subEvents_ = []; | 36 this.subEvents_ = []; |
36 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); | 37 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); |
37 if (this.eventOptions_.supportsRules) { | 38 if (this.eventOptions_.supportsRules) { |
38 this.eventForRules_ = | 39 this.eventForRules_ = |
39 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); | 40 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); |
40 } | 41 } |
41 }; | 42 } |
42 | 43 |
43 // Test if the given callback is registered for this event. | 44 // Test if the given callback is registered for this event. |
44 WebRequestEvent.prototype.hasListener = function(cb) { | 45 WebRequestEvent.prototype.hasListener = function(cb) { |
45 if (!this.eventOptions_.supportsListeners) | 46 if (!this.eventOptions_.supportsListeners) |
46 throw new Error('This event does not support listeners.'); | 47 throw new Error('This event does not support listeners.'); |
47 return this.findListener_(cb) > -1; | 48 return this.findListener_(cb) > -1; |
48 }; | 49 }; |
49 | 50 |
50 // Test if any callbacks are registered fur thus event. | 51 // Test if any callbacks are registered fur thus event. |
51 WebRequestEvent.prototype.hasListeners = function() { | 52 WebRequestEvent.prototype.hasListeners = function() { |
(...skipping 11 matching lines...) Expand all Loading... |
63 if (!this.eventOptions_.supportsListeners) | 64 if (!this.eventOptions_.supportsListeners) |
64 throw new Error('This event does not support listeners.'); | 65 throw new Error('This event does not support listeners.'); |
65 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does | 66 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does |
66 // not play well with event pages. See downloads.onDeterminingFilename and | 67 // not play well with event pages. See downloads.onDeterminingFilename and |
67 // ExtensionDownloadsEventRouter for an alternative approach. | 68 // ExtensionDownloadsEventRouter for an alternative approach. |
68 var subEventName = GetUniqueSubEventName(this.eventName_); | 69 var subEventName = GetUniqueSubEventName(this.eventName_); |
69 // Note: this could fail to validate, in which case we would not add the | 70 // Note: this could fail to validate, in which case we would not add the |
70 // subEvent listener. | 71 // subEvent listener. |
71 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); | 72 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); |
72 webRequestInternal.addEventListener( | 73 webRequestInternal.addEventListener( |
73 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName); | 74 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName, |
| 75 this.webViewInstanceId_); |
74 | 76 |
75 var subEvent = new chrome.Event(subEventName, this.argSchemas_); | 77 var subEvent = new chrome.Event(subEventName, this.argSchemas_); |
76 var subEventCallback = cb; | 78 var subEventCallback = cb; |
77 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { | 79 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { |
78 var eventName = this.eventName_; | 80 var eventName = this.eventName_; |
79 subEventCallback = function() { | 81 subEventCallback = function() { |
80 var requestId = arguments[0].requestId; | 82 var requestId = arguments[0].requestId; |
81 try { | 83 try { |
82 var result = cb.apply(null, arguments); | 84 var result = cb.apply(null, arguments); |
83 webRequestInternal.eventHandled( | 85 webRequestInternal.eventHandled( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 var apiFunctions = api.apiFunctions; | 160 var apiFunctions = api.apiFunctions; |
159 | 161 |
160 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { | 162 apiFunctions.setHandleRequest('handlerBehaviorChanged', function() { |
161 var args = Array.prototype.slice.call(arguments); | 163 var args = Array.prototype.slice.call(arguments); |
162 sendRequest(this.name, args, this.definition.parameters, | 164 sendRequest(this.name, args, this.definition.parameters, |
163 {forIOThread: true}); | 165 {forIOThread: true}); |
164 }); | 166 }); |
165 }); | 167 }); |
166 | 168 |
167 exports.binding = binding.generate(); | 169 exports.binding = binding.generate(); |
| 170 exports.WebRequestEvent = WebRequestEvent; |
OLD | NEW |