Index: chrome/ext/common.js |
=================================================================== |
--- a/chrome/ext/common.js |
+++ b/chrome/ext/common.js |
@@ -17,99 +17,48 @@ |
(function() |
{ |
- /* Events */ |
+ /* Message passing */ |
- SimpleEventTarget = function() |
+ var sendMessage; |
+ if ("runtime" in chrome && "sendMessage" in chrome.runtime) |
+ sendMessage = chrome.runtime.sendMessage; |
+ else if ("sendMessage" in chrome.extension) |
+ sendMessage = chrome.extension.sendMessage; |
+ else |
+ sendMessage = chrome.extension.sendRequest; |
+ |
+ ext._setupMessageListener = function(wrapSender) |
{ |
- this._listeners = []; |
+ var onMessage; |
+ if ("runtime" in chrome && "onMessage" in chrome.runtime) |
+ onMessage = chrome.runtime.onMessage; |
+ else if ("onMessage" in chrome.extension) |
+ onMessage = chrome.extension.onMessage; |
+ else |
+ onMessage = chrome.extension.onRequest; |
+ |
+ onMessage.addListener(function(message, sender, sendResponse) |
+ { |
+ ext.onMessage._dispatch(message, wrapSender(sender), sendResponse); |
+ }); |
}; |
- SimpleEventTarget.prototype = { |
- _onListenerAdded: function(listener, idx) {}, |
- _onListenerRemoved: function(listener, idx) {}, |
- addListener: function(listener) |
+ ext.onMessage = new ext._EventTarget(); |
+ |
+ |
+ /* Background page */ |
+ |
+ ext.backgroundPage = { |
+ sendMessage: sendMessage, |
+ getWindow: function() |
{ |
- var idx = this._listeners.push(listener) - 1; |
- this._onListenerAdded(listener, idx); |
- }, |
- removeListener: function(listener) |
- { |
- var idx = this._listeners.indexOf(listener); |
- if (idx != -1) |
- { |
- this._listeners.splice(idx, 1); |
- this._onListenerRemoved(listener, idx); |
- } |
+ return chrome.extension.getBackgroundPage(); |
} |
}; |
- WrappedEventTarget = function(target) |
- { |
- SimpleEventTarget.call(this); |
- this._wrappedListeners = []; |
- this._target = target; |
- }; |
- WrappedEventTarget.prototype = { |
- __proto__: SimpleEventTarget.prototype, |
- _onListenerAdded: function(listener, idx) |
- { |
- var wrappedListener = this._wrapListener(listener); |
+ /* Utils */ |
- this._wrappedListeners[idx] = wrappedListener; |
- this._target.addListener.call(this._target, wrappedListener); |
- }, |
- _onListenerRemoved: function(listener, idx) |
- { |
- this._target.removeListener(this._wrappedListeners[idx]); |
- this._wrappedListeners.splice(idx, 1); |
- } |
- }; |
- |
- MessageEventTarget = function() |
- { |
- var target; |
- if ("runtime" in chrome && "onMessage" in chrome.runtime) |
- target = chrome.runtime.onMessage; |
- else if ("onMessage" in chrome.extension) |
- target = chrome.extension.onMessage; |
- else |
- target = chrome.extension.onRequest; |
- WrappedEventTarget.call(this, target); |
- }; |
- MessageEventTarget.prototype = { |
- __proto__: WrappedEventTarget.prototype, |
- _wrapSender: function(sender) |
- { |
- return {}; |
- }, |
- _wrapListener: function(listener) |
- { |
- return function(message, sender, sendResponse) |
- { |
- return listener(message, this._wrapSender(sender), sendResponse); |
- }.bind(this); |
- } |
- }; |
- |
- |
- /* API */ |
- |
- ext = { |
- backgroundPage: { |
- getWindow: function() |
- { |
- return chrome.extension.getBackgroundPage(); |
- } |
- }, |
- getURL: chrome.extension.getURL, |
- i18n: chrome.i18n |
- }; |
- |
- if ("runtime" in chrome && "sendMessage" in chrome.runtime) |
- ext.backgroundPage.sendMessage = chrome.runtime.sendMessage; |
- else if ("sendMessage" in chrome.extension) |
- ext.backgroundPage.sendMessage = chrome.extension.sendMessage; |
- else |
- ext.backgroundPage.sendMessage = chrome.extension.sendRequest; |
+ ext.getURL = chrome.extension.getURL; |
+ ext.i18n = chrome.i18n; |
})(); |