Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: chrome/ext/common.js

Issue 5464830253203456: Refactored the abstraction layer to address prerendered pages on Safari caused by leaky abstraction (Closed)
Patch Set: Addressed comments Created April 11, 2014, 2:47 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/ext/background.js ('k') | chrome/ext/content.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
})();
« no previous file with comments | « chrome/ext/background.js ('k') | chrome/ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld