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

Unified Diff: chrome/ext/common.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Patch Set: Addressed another comment Created Jan. 20, 2014, 8:50 a.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
@@ -19,42 +19,54 @@
{
/* Events */
+ SimpleEventTarget = function()
+ {
+ this._listeners = [];
+ };
+ SimpleEventTarget.prototype = {
+ _onListenerAdded: function(listener, idx) {},
+ _onListenerRemoved: function(listener, idx) {},
+
+ addListener: function(listener)
+ {
+ 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);
+ }
+ }
+ };
+
WrappedEventTarget = function(target)
{
- this._listeners = [];
+ SimpleEventTarget.call(this);
+
this._wrappedListeners = [];
this._target = target;
};
WrappedEventTarget.prototype = {
- _prepareExtraArguments: function()
+ __proto__: SimpleEventTarget.prototype,
+ _onListenerAdded: function(listener, idx)
{
- return [];
+ var wrappedListener = this._wrapListener(listener);
+
+ this._wrappedListeners[idx] = wrappedListener;
+ this._target.addListener.call(this._target, wrappedListener);
},
- addListener: function(listener)
+ _onListenerRemoved: function(listener, idx)
{
- var extraArgs = Array.prototype.slice.call(arguments, 1);
- extraArgs = this._prepareExtraArguments.apply(this, extraArgs);
-
- var wrappedListener = this._wrapListener(listener);
- this._listeners.push(listener);
- this._wrappedListeners.push(wrappedListener);
-
- this._target.addListener.apply(this._target, [wrappedListener].concat(extraArgs));
- },
- removeListener: function(listener)
- {
- var idx = this._listeners.indexOf(listener);
-
- if (idx != -1) {
- this._target.removeListener(this._wrappedListeners[idx]);
-
- this._listeners.splice(idx, 1);
- this._wrappedListeners.splice(idx, 1);
- }
+ this._target.removeListener(this._wrappedListeners[idx]);
+ this._wrappedListeners.splice(idx, 1);
}
};
- var MessageEventTarget = function()
+ MessageEventTarget = function()
{
var target;
if ("runtime" in chrome && "onMessage" in chrome.runtime)
@@ -67,13 +79,16 @@
};
MessageEventTarget.prototype = {
__proto__: WrappedEventTarget.prototype,
- _wrapListener: function(listener) {
+ _wrapSender: function(sender)
+ {
+ return {};
+ },
+ _wrapListener: function(listener)
+ {
return function(message, sender, sendResponse)
{
- if ("Tab" in window && sender.tab && sender.tab.id >= 0)
- sender.tab = new Tab(sender.tab);
- return listener(message, sender, sendResponse);
- };
+ return listener(message, this._wrapSender(sender), sendResponse);
+ }.bind(this);
}
};
@@ -88,7 +103,6 @@
}
},
getURL: chrome.extension.getURL,
- onMessage: new MessageEventTarget(),
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