Index: chrome/renderer/resources/extensions/event.js |
diff --git a/chrome/renderer/resources/extensions/event.js b/chrome/renderer/resources/extensions/event.js |
index 8961ce56fc112758d971b496ccaa842b070af33b..034550d40f1dacf52ab2bc706bda4e90707cde0f 100644 |
--- a/chrome/renderer/resources/extensions/event.js |
+++ b/chrome/renderer/resources/extensions/event.js |
@@ -5,6 +5,8 @@ |
var eventBindingsNatives = requireNative('event_bindings'); |
var AttachEvent = eventBindingsNatives.AttachEvent; |
var DetachEvent = eventBindingsNatives.DetachEvent; |
+ var AttachFilteredEvent = eventBindingsNatives.AttachFilteredEvent; |
+ var DetachFilteredEvent = eventBindingsNatives.DetachFilteredEvent; |
var sendRequest = require('sendRequest').sendRequest; |
var utils = require('utils'); |
@@ -74,6 +76,82 @@ |
}; |
})(); |
+ // Handles adding/removing/dispatching listeners for unfiltered events. |
+ var UnfilteredAttachmentStrategy = function(event) { |
+ this.event_ = event; |
+ }; |
+ |
+ UnfilteredAttachmentStrategy.prototype.onAddedListener = |
+ function(listener) { |
+ // Only attach / detach on the first / last listener removed. |
+ if (this.event_.listeners_.length > 1) |
+ return; |
+ AttachEvent(this.event_.eventName_); |
+ allAttachedEvents[allAttachedEvents.length] = this.event_; |
+ if (!this.event_.eventName_) |
+ return; |
+ |
+ if (attachedNamedEvents[this.event_.eventName_]) { |
+ throw new Error("chrome.Event '" + this.event_.eventName_ + |
+ "' is already attached."); |
+ } |
+ |
+ attachedNamedEvents[this.event_.eventName_] = this.event_; |
+ }; |
+ |
+ UnfilteredAttachmentStrategy.prototype.onRemovedListener = |
+ function(listener) { |
+ if (this.event_.listeners_.length == 0) |
+ this.detach(true); |
+ }; |
+ |
+ UnfilteredAttachmentStrategy.prototype.detach = |
+ function(manual) { |
+ var i = allAttachedEvents.indexOf(this.event_); |
+ if (i >= 0) |
+ delete allAttachedEvents[i]; |
+ DetachEvent(this.event_.eventName_, manual); |
+ if (!this.event_.eventName_) |
+ return; |
+ |
+ if (!attachedNamedEvents[this.event_.eventName_]) { |
+ throw new Error("chrome.Event '" + this.event_.eventName_ + |
+ "' is not attached."); |
+ } |
+ |
+ delete attachedNamedEvents[this.event_.eventName_]; |
+ }; |
+ |
+ var FilteredAttachmentStrategy = function(event) { |
+ this.event_ = event; |
+ this.listenerMap_ = {}; |
+ }; |
+ |
+ FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) { |
+ var id = AttachFilteredEvent(this.event_.eventName_, |
+ listener.filters || []); |
+ listener.id = id; |
+ this.listenerMap_[id] = listener; |
+ }; |
+ |
+ FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) { |
+ this.detachListener(listener, true); |
+ }; |
+ |
+ FilteredAttachmentStrategy.prototype.detachListener = |
+ function(listener, manual) { |
+ if (listener.id == undefined) |
+ throw new Error("listener.id undefined - '" + listener + "'"); |
+ var id = listener.id; |
+ delete this.listenerMap_[id]; |
+ DetachFilteredEvent(id, manual); |
+ }; |
+ |
+ FilteredAttachmentStrategy.prototype.detach = function(manual) { |
+ for (var i in this.listenerMap_) |
+ this.detachListener(this.listenerMap_[i], manual); |
+ }; |
+ |
// Event object. If opt_eventName is provided, this object represents |
// the unique instance of that named event, and dispatching an event |
// with that name will route through this object's listeners. Note that |
@@ -91,11 +169,20 @@ |
this.eventName_ = opt_eventName; |
this.listeners_ = []; |
this.eventOptions_ = opt_eventOptions || |
- {"supportsListeners": true, "supportsRules": false}; |
+ {supportsListeners: true, |
+ supportsRules: false, |
+ supportsFilters: false, |
+ }; |
if (this.eventOptions_.supportsRules && !opt_eventName) |
throw new Error("Events that support rules require an event name."); |
+ if (this.eventOptions_.supportsFilters) { |
+ this.attachmentStrategy_ = new FilteredAttachmentStrategy(this); |
+ } else { |
+ this.attachmentStrategy_ = new UnfilteredAttachmentStrategy(this); |
+ } |
+ |
// Validate event arguments (the data that is passed to the callbacks) |
// if we are in debug. |
if (opt_argSchemas && |
@@ -165,13 +252,18 @@ |
}; |
// Registers a callback to be called when this event is dispatched. |
- chrome.Event.prototype.addListener = function(cb) { |
+ chrome.Event.prototype.addListener = function(cb, filters) { |
if (!this.eventOptions_.supportsListeners) |
throw new Error("This event does not support listeners."); |
- if (this.listeners_.length == 0) { |
- this.attach_(); |
+ if (filters) { |
+ if (!this.eventOptions_.supportsFilters) |
+ throw new Error("This event does not support filters."); |
+ if (!(filters instanceof Array)) |
+ throw new Error("filters should be an array"); |
} |
- this.listeners_.push(cb); |
+ var listener = {callback: cb, filters: filters}; |
+ this.listeners_.push(listener); |
+ this.attachmentStrategy_.onAddedListener(listener); |
}; |
// Unregisters a callback. |
@@ -183,10 +275,8 @@ |
return; |
} |
- this.listeners_.splice(idx, 1); |
- if (this.listeners_.length == 0) { |
- this.detach_(true); |
- } |
+ var removedListener = this.listeners_.splice(idx, 1)[0]; |
+ this.attachmentStrategy_.onRemovedListener(removedListener); |
}; |
// Test if the given callback is registered for this event. |
@@ -207,7 +297,7 @@ |
// found. |
chrome.Event.prototype.findListener_ = function(cb) { |
for (var i = 0; i < this.listeners_.length; i++) { |
- if (this.listeners_[i] == cb) { |
+ if (this.listeners_[i].callback == cb) { |
return i; |
} |
} |
@@ -228,7 +318,7 @@ |
var results = []; |
for (var i = 0; i < this.listeners_.length; i++) { |
try { |
- var result = this.listeners_[i].apply(null, args); |
+ var result = this.listeners_[i].callback.apply(null, args); |
if (result !== undefined) |
results.push(result); |
} catch (e) { |
@@ -240,37 +330,9 @@ |
return {results: results}; |
}; |
- // Attaches this event object to its name. Only one object can have a given |
- // name. |
- chrome.Event.prototype.attach_ = function() { |
- AttachEvent(this.eventName_); |
- allAttachedEvents[allAttachedEvents.length] = this; |
- if (!this.eventName_) |
- return; |
- |
- if (attachedNamedEvents[this.eventName_]) { |
- throw new Error("chrome.Event '" + this.eventName_ + |
- "' is already attached."); |
- } |
- |
- attachedNamedEvents[this.eventName_] = this; |
- }; |
- |
// Detaches this event object from its name. |
- chrome.Event.prototype.detach_ = function(manual) { |
- var i = allAttachedEvents.indexOf(this); |
- if (i >= 0) |
- delete allAttachedEvents[i]; |
- DetachEvent(this.eventName_, manual); |
- if (!this.eventName_) |
- return; |
- |
- if (!attachedNamedEvents[this.eventName_]) { |
- throw new Error("chrome.Event '" + this.eventName_ + |
- "' is not attached."); |
- } |
- |
- delete attachedNamedEvents[this.eventName_]; |
+ chrome.Event.prototype.detach_ = function() { |
+ this.attachmentStrategy_.detach(false); |
}; |
chrome.Event.prototype.destroy_ = function() { |
@@ -279,6 +341,15 @@ |
this.detach_(false); |
}; |
+ // Gets the declarative API object, or undefined if this extension doesn't |
+ // have access to it. |
+ // |
+ // This is defined as a function (rather than a variable) because it isn't |
+ // accessible until the schema bindings have been generated. |
+ function getDeclarativeAPI() { |
+ return chromeHidden.internalAPIs.declarative; |
+ } |
+ |
chrome.Event.prototype.addRules = function(rules, opt_cb) { |
if (!this.eventOptions_.supportsRules) |
throw new Error("This event does not support rules."); |
@@ -357,7 +428,7 @@ |
for (var i = 0; i < allAttachedEvents.length; ++i) { |
var event = allAttachedEvents[i]; |
if (event) |
- event.detach_(false); |
+ event.detach_(); |
} |
}; |