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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/ext/background.js ('k') | chrome/ext/content.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function() 18 (function()
19 { 19 {
20 /* Events */ 20 /* Events */
21 21
22 SimpleEventTarget = function()
23 {
24 this._listeners = [];
25 };
26 SimpleEventTarget.prototype = {
27 _onListenerAdded: function(listener, idx) {},
28 _onListenerRemoved: function(listener, idx) {},
29
30 addListener: function(listener)
31 {
32 var idx = this._listeners.push(listener) - 1;
33 this._onListenerAdded(listener, idx);
34 },
35 removeListener: function(listener)
36 {
37 var idx = this._listeners.indexOf(listener);
38 if (idx != -1)
39 {
40 this._listeners.splice(idx, 1);
41 this._onListenerRemoved(listener, idx);
42 }
43 }
44 };
45
22 WrappedEventTarget = function(target) 46 WrappedEventTarget = function(target)
23 { 47 {
24 this._listeners = []; 48 SimpleEventTarget.call(this);
49
25 this._wrappedListeners = []; 50 this._wrappedListeners = [];
26 this._target = target; 51 this._target = target;
27 }; 52 };
28 WrappedEventTarget.prototype = { 53 WrappedEventTarget.prototype = {
29 _prepareExtraArguments: function() 54 __proto__: SimpleEventTarget.prototype,
55 _onListenerAdded: function(listener, idx)
30 { 56 {
31 return []; 57 var wrappedListener = this._wrapListener(listener);
58
59 this._wrappedListeners[idx] = wrappedListener;
60 this._target.addListener.call(this._target, wrappedListener);
32 }, 61 },
33 addListener: function(listener) 62 _onListenerRemoved: function(listener, idx)
34 { 63 {
35 var extraArgs = Array.prototype.slice.call(arguments, 1); 64 this._target.removeListener(this._wrappedListeners[idx]);
36 extraArgs = this._prepareExtraArguments.apply(this, extraArgs); 65 this._wrappedListeners.splice(idx, 1);
37
38 var wrappedListener = this._wrapListener(listener);
39 this._listeners.push(listener);
40 this._wrappedListeners.push(wrappedListener);
41
42 this._target.addListener.apply(this._target, [wrappedListener].concat(extr aArgs));
43 },
44 removeListener: function(listener)
45 {
46 var idx = this._listeners.indexOf(listener);
47
48 if (idx != -1) {
49 this._target.removeListener(this._wrappedListeners[idx]);
50
51 this._listeners.splice(idx, 1);
52 this._wrappedListeners.splice(idx, 1);
53 }
54 } 66 }
55 }; 67 };
56 68
57 var MessageEventTarget = function() 69 MessageEventTarget = function()
58 { 70 {
59 var target; 71 var target;
60 if ("runtime" in chrome && "onMessage" in chrome.runtime) 72 if ("runtime" in chrome && "onMessage" in chrome.runtime)
61 target = chrome.runtime.onMessage; 73 target = chrome.runtime.onMessage;
62 else if ("onMessage" in chrome.extension) 74 else if ("onMessage" in chrome.extension)
63 target = chrome.extension.onMessage; 75 target = chrome.extension.onMessage;
64 else 76 else
65 target = chrome.extension.onRequest; 77 target = chrome.extension.onRequest;
66 WrappedEventTarget.call(this, target); 78 WrappedEventTarget.call(this, target);
67 }; 79 };
68 MessageEventTarget.prototype = { 80 MessageEventTarget.prototype = {
69 __proto__: WrappedEventTarget.prototype, 81 __proto__: WrappedEventTarget.prototype,
70 _wrapListener: function(listener) { 82 _wrapSender: function(sender)
83 {
84 return {};
85 },
86 _wrapListener: function(listener)
87 {
71 return function(message, sender, sendResponse) 88 return function(message, sender, sendResponse)
72 { 89 {
73 if ("Tab" in window && sender.tab && sender.tab.id >= 0) 90 return listener(message, this._wrapSender(sender), sendResponse);
74 sender.tab = new Tab(sender.tab); 91 }.bind(this);
75 return listener(message, sender, sendResponse);
76 };
77 } 92 }
78 }; 93 };
79 94
80 95
81 /* API */ 96 /* API */
82 97
83 ext = { 98 ext = {
84 backgroundPage: { 99 backgroundPage: {
85 getWindow: function() 100 getWindow: function()
86 { 101 {
87 return chrome.extension.getBackgroundPage(); 102 return chrome.extension.getBackgroundPage();
88 } 103 }
89 }, 104 },
90 getURL: chrome.extension.getURL, 105 getURL: chrome.extension.getURL,
91 onMessage: new MessageEventTarget(),
92 i18n: chrome.i18n 106 i18n: chrome.i18n
93 }; 107 };
94 108
95 if ("runtime" in chrome && "sendMessage" in chrome.runtime) 109 if ("runtime" in chrome && "sendMessage" in chrome.runtime)
96 ext.backgroundPage.sendMessage = chrome.runtime.sendMessage; 110 ext.backgroundPage.sendMessage = chrome.runtime.sendMessage;
97 else if ("sendMessage" in chrome.extension) 111 else if ("sendMessage" in chrome.extension)
98 ext.backgroundPage.sendMessage = chrome.extension.sendMessage; 112 ext.backgroundPage.sendMessage = chrome.extension.sendMessage;
99 else 113 else
100 ext.backgroundPage.sendMessage = chrome.extension.sendRequest; 114 ext.backgroundPage.sendMessage = chrome.extension.sendRequest;
101 })(); 115 })();
OLDNEW
« 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