diff options
author | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-08 18:35:34 +0000 |
---|---|---|
committer | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-08 18:35:34 +0000 |
commit | a40caa97fbdae3760f52f95f6b265bd1f39b19ae (patch) | |
tree | b98dceab49c4efb854c9923660735cbf96addbcd /chrome/renderer/resources/event_bindings.js | |
parent | 1b812ea42f713908a9034fcf2a26e8d4a8a86a04 (diff) | |
download | chromium_src-a40caa97fbdae3760f52f95f6b265bd1f39b19ae.zip chromium_src-a40caa97fbdae3760f52f95f6b265bd1f39b19ae.tar.gz chromium_src-a40caa97fbdae3760f52f95f6b265bd1f39b19ae.tar.bz2 |
Add aa's Event class to our javascript bindings and use it in our extension
message passing API.
Review URL: http://codereview.chromium.org/62069
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13371 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/resources/event_bindings.js')
-rw-r--r-- | chrome/renderer/resources/event_bindings.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/chrome/renderer/resources/event_bindings.js b/chrome/renderer/resources/event_bindings.js new file mode 100644 index 0000000..de5b3a9 --- /dev/null +++ b/chrome/renderer/resources/event_bindings.js @@ -0,0 +1,120 @@ +var chromium = chromium || {}; +(function () { + native function AttachEvent(eventName); + native function DetachEvent(eventName); + + // 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. + // + // Example: + // chromium.ontabchanged = new Event('tabchanged'); + // chromium.ontabchanged.addListener(function(data) { alert(data); }); + // chromium.Event.dispatch_('tabchanged', 'hi'); + // will result in an alert dialog that says 'hi'. + chromium.Event = function(opt_eventName) { + this.eventName_ = opt_eventName; + this.listeners_ = []; + }; + + // A map of event names to the event object that is registered to that name. + chromium.Event.attached_ = {}; + + // Dispatches a named event with the given JSON data, which is deserialized + // before dispatch. + chromium.Event.dispatchJSON_ = function(name, data) { + if (chromium.Event.attached_[name]) { + if (data) { + data = chromium.json.deserialize_(data); + } + chromium.Event.attached_[name].dispatch_(data); + } + }; + + // Dispatches a named event with the given object data. + chromium.Event.dispatch_ = function(name, data) { + if (chromium.Event.attached_[name]) { + chromium.Event.attached_[name].dispatch(data); + } + }; + + // Registers a callback to be called when this event is dispatched. + chromium.Event.prototype.addListener = function(cb) { + this.listeners_.push(cb); + if (this.listeners_.length == 1) { + this.attach_(); + } + }; + + // Unregisters a callback. + chromium.Event.prototype.removeListener = function(cb) { + var idx = this.findListener_(cb); + if (idx == -1) { + return; + } + + this.listeners_.splice(idx, 1); + if (this.listeners_.length == 0) { + this.detach_(); + } + }; + + // Test if the given callback is registered for this event. + chromium.Event.prototype.hasListener = function(cb) { + return this.findListeners_(cb) > -1; + }; + + // Returns the index of the given callback if registered, or -1 if not + // found. + chromium.Event.prototype.findListener_ = function(cb) { + for (var i = 0; i < this.listeners_.length; i++) { + if (this.listeners_[i] == cb) { + return i; + } + } + + return -1; + }; + + // Dispatches this event object to all listeners, passing all supplied + // arguments to this function each listener. + chromium.Event.prototype.dispatch = function(varargs) { + var args = Array.prototype.slice.call(arguments); + for (var i = 0; i < this.listeners_.length; i++) { + try { + this.listeners_[i].apply(null, args); + } catch (e) { + console.error(e); + } + } + }; + + // Attaches this event object to its name. Only one object can have a given + // name. + chromium.Event.prototype.attach_ = function() { + AttachEvent(this.eventName_); + if (!this.eventName_) + return; + + if (chromium.Event.attached_[this.eventName_]) { + throw new Error("chromium.Event '" + this.eventName_ + + "' is already attached."); + } + + chromium.Event.attached_[this.eventName_] = this; + }; + + // Detaches this event object from its name. + chromium.Event.prototype.detach_ = function() { + DetachEvent(this.eventName_); + if (!this.eventName_) + return; + + if (!chromium.Event.attached_[this.eventName_]) { + throw new Error("chromium.Event '" + this.eventName_ + + "' is not attached."); + } + + delete chromium.Event.attached_[this.eventName_]; + }; +})(); |