| 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; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 WebRequestEvent.prototype.addListener = | 60 WebRequestEvent.prototype.addListener = |
| 61 function(cb, opt_filter, opt_extraInfo) { | 61 function(cb, opt_filter, opt_extraInfo) { |
| 62 if (!this.eventOptions_.supportsListeners) | 62 if (!this.eventOptions_.supportsListeners) |
| 63 throw new Error('This event does not support listeners.'); | 63 throw new Error('This event does not support listeners.'); |
| 64 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does | 64 // NOTE(benjhayden) New APIs should not use this subEventName trick! It does |
| 65 // not play well with event pages. See downloads.onDeterminingFilename and | 65 // not play well with event pages. See downloads.onDeterminingFilename and |
| 66 // ExtensionDownloadsEventRouter for an alternative approach. | 66 // ExtensionDownloadsEventRouter for an alternative approach. |
| 67 var subEventName = webRequestNatives.GetUniqueSubEventName(this.eventName_); | 67 var subEventName = webRequestNatives.GetUniqueSubEventName(this.eventName_); |
| 68 // Note: this could fail to validate, in which case we would not add the | 68 // Note: this could fail to validate, in which case we would not add the |
| 69 // subEvent listener. | 69 // subEvent listener. |
| 70 validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_); | 70 validate($Array.slice(arguments, 1), this.extraArgSchemas_); |
| 71 webRequestInternal.addEventListener( | 71 webRequestInternal.addEventListener( |
| 72 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName, | 72 cb, opt_filter, opt_extraInfo, this.eventName_, subEventName, |
| 73 this.webViewInstanceId_); | 73 this.webViewInstanceId_); |
| 74 | 74 |
| 75 var subEvent = new eventBindings.Event(subEventName, this.argSchemas_); | 75 var subEvent = new eventBindings.Event(subEventName, this.argSchemas_); |
| 76 var subEventCallback = cb; | 76 var subEventCallback = cb; |
| 77 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { | 77 if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) { |
| 78 var eventName = this.eventName_; | 78 var eventName = this.eventName_; |
| 79 subEventCallback = function() { | 79 subEventCallback = function() { |
| 80 var requestId = arguments[0].requestId; | 80 var requestId = arguments[0].requestId; |
| 81 try { | 81 try { |
| 82 var result = cb.apply(null, arguments); | 82 var result = $Function.apply(cb, null, arguments); |
| 83 webRequestInternal.eventHandled( | 83 webRequestInternal.eventHandled( |
| 84 eventName, subEventName, requestId, result); | 84 eventName, subEventName, requestId, result); |
| 85 } catch (e) { | 85 } catch (e) { |
| 86 webRequestInternal.eventHandled( | 86 webRequestInternal.eventHandled( |
| 87 eventName, subEventName, requestId); | 87 eventName, subEventName, requestId); |
| 88 throw e; | 88 throw e; |
| 89 } | 89 } |
| 90 }; | 90 }; |
| 91 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { | 91 } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) { |
| 92 var eventName = this.eventName_; | 92 var eventName = this.eventName_; |
| 93 subEventCallback = function() { | 93 subEventCallback = function() { |
| 94 var details = arguments[0]; | 94 var details = arguments[0]; |
| 95 var requestId = details.requestId; | 95 var requestId = details.requestId; |
| 96 var handledCallback = function(response) { | 96 var handledCallback = function(response) { |
| 97 webRequestInternal.eventHandled( | 97 webRequestInternal.eventHandled( |
| 98 eventName, subEventName, requestId, response); | 98 eventName, subEventName, requestId, response); |
| 99 }; | 99 }; |
| 100 cb.apply(null, [details, handledCallback]); | 100 $Function.apply(cb, null, [details, handledCallback]); |
| 101 }; | 101 }; |
| 102 } | 102 } |
| 103 this.subEvents_.push( | 103 $Array.push(this.subEvents_, |
| 104 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); | 104 {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback}); |
| 105 subEvent.addListener(subEventCallback); | 105 subEvent.addListener(subEventCallback); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 // Unregisters a callback. | 108 // Unregisters a callback. |
| 109 WebRequestEvent.prototype.removeListener = function(cb) { | 109 WebRequestEvent.prototype.removeListener = function(cb) { |
| 110 if (!this.eventOptions_.supportsListeners) | 110 if (!this.eventOptions_.supportsListeners) |
| 111 throw new Error('This event does not support listeners.'); | 111 throw new Error('This event does not support listeners.'); |
| 112 var idx; | 112 var idx; |
| 113 while ((idx = this.findListener_(cb)) >= 0) { | 113 while ((idx = this.findListener_(cb)) >= 0) { |
| 114 var e = this.subEvents_[idx]; | 114 var e = this.subEvents_[idx]; |
| 115 e.subEvent.removeListener(e.subEventCallback); | 115 e.subEvent.removeListener(e.subEventCallback); |
| 116 if (e.subEvent.hasListeners()) { | 116 if (e.subEvent.hasListeners()) { |
| 117 console.error( | 117 console.error( |
| 118 'Internal error: webRequest subEvent has orphaned listeners.'); | 118 'Internal error: webRequest subEvent has orphaned listeners.'); |
| 119 } | 119 } |
| 120 this.subEvents_.splice(idx, 1); | 120 $Array.splice(this.subEvents_, idx, 1); |
| 121 } | 121 } |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 WebRequestEvent.prototype.findListener_ = function(cb) { | 124 WebRequestEvent.prototype.findListener_ = function(cb) { |
| 125 for (var i in this.subEvents_) { | 125 for (var i in this.subEvents_) { |
| 126 var e = this.subEvents_[i]; | 126 var e = this.subEvents_[i]; |
| 127 if (e.callback === cb) { | 127 if (e.callback === cb) { |
| 128 if (e.subEvent.findListener_(e.subEventCallback) > -1) | 128 if (e.subEvent.findListener_(e.subEventCallback) > -1) |
| 129 return i; | 129 return i; |
| 130 console.error('Internal error: webRequest subEvent has no callback.'); | 130 console.error('Internal error: webRequest subEvent has no callback.'); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 149 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { | 149 WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) { |
| 150 if (!this.eventOptions_.supportsRules) | 150 if (!this.eventOptions_.supportsRules) |
| 151 throw new Error('This event does not support rules.'); | 151 throw new Error('This event does not support rules.'); |
| 152 this.eventForRules_.getRules(ruleIdentifiers, cb); | 152 this.eventForRules_.getRules(ruleIdentifiers, cb); |
| 153 }; | 153 }; |
| 154 | 154 |
| 155 binding.registerCustomHook(function(api) { | 155 binding.registerCustomHook(function(api) { |
| 156 var apiFunctions = api.apiFunctions; | 156 var apiFunctions = api.apiFunctions; |
| 157 | 157 |
| 158 apiFunctions.setHandleRequest('addEventListener', function() { | 158 apiFunctions.setHandleRequest('addEventListener', function() { |
| 159 var args = Array.prototype.slice.call(arguments); | 159 var args = $Array.slice(arguments); |
| 160 sendRequest(this.name, args, this.definition.parameters, | 160 sendRequest(this.name, args, this.definition.parameters, |
| 161 {forIOThread: true}); | 161 {forIOThread: true}); |
| 162 }); | 162 }); |
| 163 | 163 |
| 164 apiFunctions.setHandleRequest('eventHandled', function() { | 164 apiFunctions.setHandleRequest('eventHandled', function() { |
| 165 var args = Array.prototype.slice.call(arguments); | 165 var args = $Array.slice(arguments); |
| 166 sendRequest(this.name, args, this.definition.parameters, | 166 sendRequest(this.name, args, this.definition.parameters, |
| 167 {forIOThread: true}); | 167 {forIOThread: true}); |
| 168 }); | 168 }); |
| 169 }); | 169 }); |
| 170 | 170 |
| 171 webRequestInternal = binding.generate(); | 171 webRequestInternal = binding.generate(); |
| 172 exports.binding = webRequestInternal; | 172 exports.binding = webRequestInternal; |
| 173 exports.WebRequestEvent = WebRequestEvent; | 173 exports.WebRequestEvent = WebRequestEvent; |
| OLD | NEW |