Chromium Code Reviews| 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('logging').DCHECK; | 5 var DCHECK = requireNative('logging').DCHECK; |
| 6 // TODO(cduvall/kalman): json_schema shouldn't put things on chromeHidden. | 6 // TODO(cduvall/kalman): json_schema shouldn't put things on chromeHidden. |
| 7 require('json_schema'); | 7 require('json_schema'); |
| 8 var eventBindingsNatives = requireNative('event_bindings'); | 8 var eventBindingsNatives = requireNative('event_bindings'); |
| 9 var AttachEvent = eventBindingsNatives.AttachEvent; | 9 var AttachEvent = eventBindingsNatives.AttachEvent; |
| 10 var DetachEvent = eventBindingsNatives.DetachEvent; | 10 var DetachEvent = eventBindingsNatives.DetachEvent; |
| 11 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; | 11 var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; |
| 12 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; | 12 var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; |
| 13 var MatchAgainstEventFilter = eventBindingsNatives.MatchAgainstEventFilter; | 13 var MatchAgainstEventFilter = eventBindingsNatives.MatchAgainstEventFilter; |
| 14 var forEach = require('utils').forEach; | 14 var forEach = require('utils').forEach; |
| 15 var sendRequest = require('sendRequest').sendRequest; | 15 var sendRequest = require('sendRequest').sendRequest; |
| 16 var utils = require('utils'); | 16 var utils = require('utils'); |
| 17 var validate = require('schemaUtils').validate; | 17 var validate = require('schemaUtils').validate; |
| 18 var unloadEvent = require('unload_event'); | |
| 18 | 19 |
| 19 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 20 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 20 var chrome = requireNative('chrome').GetChrome(); | 21 var chrome = requireNative('chrome').GetChrome(); |
| 21 var schemaRegistry = requireNative('schema_registry'); | 22 var schemaRegistry = requireNative('schema_registry'); |
| 22 | 23 |
| 23 // Schemas for the rule-style functions on the events API that | 24 // Schemas for the rule-style functions on the events API that |
| 24 // only need to be generated occasionally, so populate them lazily. | 25 // only need to be generated occasionally, so populate them lazily. |
| 25 var ruleFunctionSchemas = { | 26 var ruleFunctionSchemas = { |
| 26 // These values are set lazily: | 27 // These values are set lazily: |
| 27 // addRules: {}, | 28 // addRules: {}, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 // dispatchEvent(), and dispatch is a function(args) that dispatches args to | 194 // dispatchEvent(), and dispatch is a function(args) that dispatches args to |
| 194 // its listeners. | 195 // its listeners. |
| 195 chromeHidden.Event.registerArgumentMassager = function(name, callback) { | 196 chromeHidden.Event.registerArgumentMassager = function(name, callback) { |
| 196 if (eventArgumentMassagers[name]) | 197 if (eventArgumentMassagers[name]) |
| 197 throw new Error("Massager already registered for event: " + name); | 198 throw new Error("Massager already registered for event: " + name); |
| 198 eventArgumentMassagers[name] = callback; | 199 eventArgumentMassagers[name] = callback; |
| 199 }; | 200 }; |
| 200 | 201 |
| 201 // Dispatches a named event with the given argument array. The args array is | 202 // Dispatches a named event with the given argument array. The args array is |
| 202 // the list of arguments that will be sent to the event callback. | 203 // the list of arguments that will be sent to the event callback. |
| 203 chromeHidden.Event.dispatchEvent = function(name, args, filteringInfo) { | 204 function dispatchEvent(name, args, filteringInfo) { |
| 204 var listenerIDs = null; | 205 var listenerIDs = null; |
| 205 | 206 |
| 206 if (filteringInfo) | 207 if (filteringInfo) |
| 207 listenerIDs = MatchAgainstEventFilter(name, filteringInfo); | 208 listenerIDs = MatchAgainstEventFilter(name, filteringInfo); |
| 208 | 209 |
| 209 var event = attachedNamedEvents[name]; | 210 var event = attachedNamedEvents[name]; |
| 210 if (!event) | 211 if (!event) |
| 211 return; | 212 return; |
| 212 | 213 |
| 213 var dispatchArgs = function(args) { | 214 var dispatchArgs = function(args) { |
| 214 var result = event.dispatch_(args, listenerIDs); | 215 var result = event.dispatch_(args, listenerIDs); |
| 215 if (result) | 216 if (result) |
| 216 DCHECK(!result.validationErrors, result.validationErrors); | 217 DCHECK(!result.validationErrors, result.validationErrors); |
| 217 return result; | 218 return result; |
| 218 }; | 219 }; |
| 219 | 220 |
| 220 if (eventArgumentMassagers[name]) | 221 if (eventArgumentMassagers[name]) |
| 221 eventArgumentMassagers[name](args, dispatchArgs); | 222 eventArgumentMassagers[name](args, dispatchArgs); |
| 222 else | 223 else |
| 223 dispatchArgs(args); | 224 dispatchArgs(args); |
| 224 }; | 225 } |
| 225 | |
| 226 // Test if a named event has any listeners. | |
| 227 chromeHidden.Event.hasListener = function(name) { | |
| 228 return (attachedNamedEvents[name] && | |
| 229 attachedNamedEvents[name].listeners_.length > 0); | |
| 230 }; | |
| 231 | 226 |
| 232 // Registers a callback to be called when this event is dispatched. | 227 // Registers a callback to be called when this event is dispatched. |
| 233 Event.prototype.addListener = function(cb, filters) { | 228 Event.prototype.addListener = function(cb, filters) { |
| 234 if (!this.eventOptions_.supportsListeners) | 229 if (!this.eventOptions_.supportsListeners) |
| 235 throw new Error("This event does not support listeners."); | 230 throw new Error("This event does not support listeners."); |
| 236 if (this.eventOptions_.maxListeners && | 231 if (this.eventOptions_.maxListeners && |
| 237 this.getListenerCount() >= this.eventOptions_.maxListeners) | 232 this.getListenerCount() >= this.eventOptions_.maxListeners) |
| 238 throw new Error("Too many listeners for " + this.eventName_); | 233 throw new Error("Too many listeners for " + this.eventName_); |
| 239 if (filters) { | 234 if (filters) { |
| 240 if (!this.eventOptions_.supportsFilters) | 235 if (!this.eventOptions_.supportsFilters) |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 // We remove the first parameter from the validation to give the user more | 438 // We remove the first parameter from the validation to give the user more |
| 444 // meaningful error messages. | 439 // meaningful error messages. |
| 445 validate([ruleIdentifiers, cb], | 440 validate([ruleIdentifiers, cb], |
| 446 ruleFunctionSchemas.getRules.parameters.slice().splice(1)); | 441 ruleFunctionSchemas.getRules.parameters.slice().splice(1)); |
| 447 | 442 |
| 448 sendRequest("events.getRules", | 443 sendRequest("events.getRules", |
| 449 [this.eventName_, ruleIdentifiers, cb], | 444 [this.eventName_, ruleIdentifiers, cb], |
| 450 ruleFunctionSchemas.getRules.parameters); | 445 ruleFunctionSchemas.getRules.parameters); |
| 451 } | 446 } |
| 452 | 447 |
| 453 // Special load events: we don't use the DOM unload because that slows | 448 unloadEvent.addListener(function() { |
|
koz (OOO until 15th September)
2013/05/31 04:28:44
Very nice :-)
not at google - send to devlin
2013/05/31 22:47:07
:)
| |
| 454 // down tab shutdown. On the other hand, onUnload might not always fire, | |
| 455 // since Chrome will terminate renderers on shutdown (SuddenTermination). | |
| 456 chromeHidden.onUnload = new Event(); | |
| 457 | |
| 458 chromeHidden.dispatchOnUnload = function() { | |
| 459 chromeHidden.onUnload.dispatch(); | |
| 460 chromeHidden.wasUnloaded = true; | |
| 461 | |
| 462 for (var i = 0; i < allAttachedEvents.length; ++i) { | 449 for (var i = 0; i < allAttachedEvents.length; ++i) { |
| 463 var event = allAttachedEvents[i]; | 450 var event = allAttachedEvents[i]; |
| 464 if (event) | 451 if (event) |
| 465 event.detach_(); | 452 event.detach_(); |
| 466 } | 453 } |
| 467 }; | 454 }); |
| 468 | 455 |
| 469 chrome.Event = Event; | 456 chrome.Event = Event; |
| 457 exports.dispatchEvent = dispatchEvent; | |
| OLD | NEW |