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 var DCHECK = requireNative('debug').DCHECK; | |
5 var eventBindingsNatives = requireNative('event_bindings'); | 6 var eventBindingsNatives = requireNative('event_bindings'); |
6 var AttachEvent = eventBindingsNatives.AttachEvent; | 7 var AttachEvent = eventBindingsNatives.AttachEvent; |
7 var DetachEvent = eventBindingsNatives.DetachEvent; | 8 var DetachEvent = eventBindingsNatives.DetachEvent; |
8 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; | 9 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; |
9 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; | 10 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; |
10 var MatchAgainstEventFilter = eventBindingsNatives.MatchAgainstEventFilter; | 11 var MatchAgainstEventFilter = eventBindingsNatives.MatchAgainstEventFilter; |
11 var sendRequest = require('sendRequest').sendRequest; | 12 var sendRequest = require('sendRequest').sendRequest; |
12 var utils = require('utils'); | 13 var utils = require('utils'); |
13 var validate = require('schemaUtils').validate; | 14 var validate = require('schemaUtils').validate; |
14 | 15 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 exception; | 202 exception; |
202 } | 203 } |
203 }; | 204 }; |
204 } else { | 205 } else { |
205 this.validateEventArgs_ = function() {} | 206 this.validateEventArgs_ = function() {} |
206 } | 207 } |
207 }; | 208 }; |
208 | 209 |
209 chromeHidden.Event = {}; | 210 chromeHidden.Event = {}; |
210 | 211 |
211 chromeHidden.Event.registerArgumentMassager = function(name, fn) { | 212 // callback is a function(args, dispatch). args are the args we recieve from |
213 // dispatchJSON(), and dispatch is a function(args) that dispatches args to | |
214 // its listeners. | |
215 chromeHidden.Event.registerArgumentMassager = function(name, callback) { | |
212 if (eventArgumentMassagers[name]) | 216 if (eventArgumentMassagers[name]) |
213 throw new Error("Massager already registered for event: " + name); | 217 throw new Error("Massager already registered for event: " + name); |
214 eventArgumentMassagers[name] = fn; | 218 eventArgumentMassagers[name] = callback; |
215 }; | 219 }; |
216 | 220 |
217 // Dispatches a named event with the given JSON array, which is deserialized | 221 // Dispatches a named event with the given JSON array, which is deserialized |
218 // before dispatch. The JSON array is the list of arguments that will be | 222 // before dispatch. The JSON array is the list of arguments that will be |
219 // sent with the event callback. | 223 // sent with the event callback. |
220 chromeHidden.Event.dispatchJSON = function(name, args, filteringInfo) { | 224 chromeHidden.Event.dispatchJSON = function(name, args, filteringInfo) { |
221 var listenerIDs = null; | 225 var listenerIDs = null; |
222 | 226 |
223 if (filteringInfo) { | 227 if (filteringInfo) { |
224 listenerIDs = MatchAgainstEventFilter(name, filteringInfo); | 228 listenerIDs = MatchAgainstEventFilter(name, filteringInfo); |
225 } | 229 } |
226 if (attachedNamedEvents[name]) { | 230 var event = attachedNamedEvents[name]; |
227 if (args) { | 231 if (!event) |
228 // TODO(asargent): This is an antiquity. Until all callers of | 232 return; |
229 // dispatchJSON use actual values, this must remain here to catch the | |
230 // cases where a caller has hard-coded a JSON string to pass in. | |
231 if (typeof(args) == "string") { | |
232 args = chromeHidden.JSON.parse(args); | |
233 } | |
234 if (eventArgumentMassagers[name]) | |
235 eventArgumentMassagers[name](args); | |
236 } | |
237 | 233 |
238 var event = attachedNamedEvents[name]; | 234 // TODO(asargent): This is an antiquity. Until all callers of |
239 var result; | 235 // dispatchJSON use actual values, this must remain here to catch the |
240 // TODO(koz): We have to do this differently for unfiltered events (which | 236 // cases where a caller has hard-coded a JSON string to pass in. |
241 // have listenerIDs = null) because some bindings write over | 237 if (typeof(args) == "string") |
242 // event.dispatch (eg: experimental.app.custom_bindings.js) and so expect | 238 args = chromeHidden.JSON.parse(args); |
243 // events to go through it. These places need to be fixed so that they | 239 |
244 // expect a listenerIDs parameter. | 240 var dispatchArgs = function(args) { |
245 if (listenerIDs) | 241 result = event.dispatch_(args, listenerIDs); |
246 result = event.dispatch_(args, listenerIDs); | |
247 else | |
248 result = event.dispatch.apply(event, args); | |
249 if (result && result.validationErrors) | 242 if (result && result.validationErrors) |
250 return result.validationErrors; | 243 DCHECK(false, result.validationErrors); |
not at google - send to devlin
2012/07/03 07:47:04
Either incorporate the condition into the DCHECK,
koz (OOO until 15th September)
2012/07/05 04:38:32
Done.
| |
251 } | 244 }; |
245 | |
246 if (eventArgumentMassagers[name]) | |
247 eventArgumentMassagers[name](args, dispatchArgs); | |
248 else | |
249 dispatchArgs(args); | |
252 }; | 250 }; |
253 | 251 |
254 // Dispatches a named event with the given arguments, supplied as an array. | 252 // Dispatches a named event with the given arguments, supplied as an array. |
255 chromeHidden.Event.dispatch = function(name, args) { | 253 chromeHidden.Event.dispatch = function(name, args) { |
benwells
2012/07/03 07:38:18
Why do we have this function as well as dispatchJS
koz (OOO until 15th September)
2012/07/05 04:38:32
Removed.
| |
256 if (attachedNamedEvents[name]) { | 254 if (attachedNamedEvents[name]) |
257 attachedNamedEvents[name].dispatch.apply( | 255 attachedNamedEvents[name].dispatch_(args, undefined); |
258 attachedNamedEvents[name], args); | |
259 } | |
260 }; | 256 }; |
261 | 257 |
262 // Test if a named event has any listeners. | 258 // Test if a named event has any listeners. |
263 chromeHidden.Event.hasListener = function(name) { | 259 chromeHidden.Event.hasListener = function(name) { |
264 return (attachedNamedEvents[name] && | 260 return (attachedNamedEvents[name] && |
265 attachedNamedEvents[name].listeners_.length > 0); | 261 attachedNamedEvents[name].listeners_.length > 0); |
266 }; | 262 }; |
267 | 263 |
268 // Registers a callback to be called when this event is dispatched. | 264 // Registers a callback to be called when this event is dispatched. |
269 chrome.Event.prototype.addListener = function(cb, filters) { | 265 chrome.Event.prototype.addListener = function(cb, filters) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
473 if (event) | 469 if (event) |
474 event.detach_(); | 470 event.detach_(); |
475 } | 471 } |
476 }; | 472 }; |
477 | 473 |
478 chromeHidden.dispatchError = function(msg) { | 474 chromeHidden.dispatchError = function(msg) { |
479 console.error(msg); | 475 console.error(msg); |
480 }; | 476 }; |
481 | 477 |
482 exports.Event = chrome.Event; | 478 exports.Event = chrome.Event; |
OLD | NEW |