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

Side by Side Diff: chrome/background.js

Issue 16067002: Added Safari Support (Closed)
Patch Set: Added missing copyright disclaimers and websql implementation Created Oct. 23, 2013, 2:46 p.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 | « block.js ('k') | chrome/common.js » ('j') | chrome/common.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 (function() {
19 /* Events */
20
21 var TabEventTarget = function() {
22 WrappedEventTarget.apply(this, arguments);
23
24 this._tabs = {};
25
26 this._sharedListener = this._sharedListener.bind(this);
27 this._removeTab = this._removeTab.bind(this);
28 };
29 TabEventTarget.prototype = {
30 __proto__: WrappedEventTarget.prototype,
31 _bindToTab: function(tab) {
32 return {
33 addListener: function(listener) {
34 var listeners = this._tabs[tab._id];
35
36 if (!listeners) {
37 this._tabs[tab._id] = listeners = [];
38
39 if (Object.keys(this._tabs).length == 1)
40 this.addListener(this._sharedListener);
41
42 tab.onRemoved.addListener(this._removeTab);
43 }
44
45 listeners.push(listener);
46 }.bind(this),
47 removeListener: function(listener) {
48 var listeners = this._tabs[tab._id];
49 if (!listeners)
50 return;
51
52 var idx = listeners.indexOf(listener);
53 if (idx == -1)
54 return;
55
56 listeners.splice(idx, 1);
57
58 if (listeners.length == 0)
59 tab.onRemoved.removeListener(this._removeTab);
60 else {
61 if (listeners.length > 1)
62 return;
63 if (listeners[0] != this._removeTab)
64 return;
65 }
66
67 this._removeTab(tab);
68 }.bind(this)
69 };
70 },
71 _sharedListener: function(tab) {
72 var listeners = this._tabs[tab._id];
73
74 if (!listeners)
75 return;
76
77 // copy listeners before calling them, because of they
78 // might add or remove other listeners, which must not be
79 // taken into account before the next occurrance of the event
80 listeners = listeners.slice(0);
81
82 for (var i = 0; i < listeners.length; i++)
83 listeners[i](tab);
84 },
85 _removeTab: function(tab) {
86 delete this._tabs[tab._id];
87
88 if (Object.keys(this._tabs).length == 0)
89 this.removeListener(this._sharedListener);
90 }
91 };
92
93 var BeforeNavigateTabEventTarget = function() {
94 TabEventTarget.call(this, chrome.tabs.onUpdated);
95 };
96 BeforeNavigateTabEventTarget.prototype = {
97 __proto__: TabEventTarget.prototype,
98 _wrapListener: function(listener) {
99 return function(id, info, tab) {
100 if ("url" in info)
101 listener(new Tab(tab));
102 };
103 }
104 };
105
106 var CompletedTabEventTarget = function() {
107 TabEventTarget.call(this, chrome.tabs.onUpdated);
108 };
109 CompletedTabEventTarget.prototype = {
110 __proto__: TabEventTarget.prototype,
111 _wrapListener: function(listener) {
112 return function(id, info, tab) {
113 if (info.status == "complete")
114 listener(new Tab(tab));
115 };
116 }
117 };
118
119 var ActivatedTabEventTarget = function() {
120 TabEventTarget.call(this, chrome.tabs.onActivated);
121 };
122 ActivatedTabEventTarget.prototype = {
123 __proto__: TabEventTarget.prototype,
124 _wrapListener: function(listener) {
125 return function(info) {
126 chrome.tabs.get(info.tabId, function(tab) {
127 listener(new Tab(tab));
128 });
129 };
130 }
131 }
132
133 var RemovedTabEventTarget = function() {
134 TabEventTarget.call(this, chrome.tabs.onRemoved);
135 };
136 RemovedTabEventTarget.prototype = {
137 __proto__: TabEventTarget.prototype,
138 _wrapListener: function(listener) {
139 return function(id) {
140 listener(new Tab({id: id}));
141 };
142 }
143 };
144
145 var BeforeRequestEventTarget = function() {
146 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest);
147 };
148 BeforeRequestEventTarget.prototype = {
149 __proto__: WrappedEventTarget.prototype,
150 _wrapListener: function(listener) {
151 return function(details) {
152 var tab = null;
153
154 if (details.tabId != -1)
155 tab = new Tab({id: details.tabId});
156
157 return {cancel: listener(
158 details.url,
159 details.type,
160 tab,
161 details.frameId,
162 details.parentFrameId
163 ) === false};
164 };
165 },
166 _prepareExtraArguments: function(urls) {
167 return [urls ? {urls: urls} : {}, ["blocking"]];
168 }
169 };
170
171
172 /* Tabs */
173
174 var PageAction = function(tabId) {
175 this._tabId = tabId;
176 };
177 PageAction.prototype = {
178 setIcon: function(path) {
179 chrome.pageAction.setIcon({tabId: this._tabId, path: path});
180 },
181 setTitle: function(title) {
182 chrome.pageAction.setTitle({tabId: this._tabId, title: title});
183 },
184 hide: function() {
185 chrome.pageAction.hide(this._tabId);
186 },
187 show: function() {
188 chrome.pageAction.show(this._tabId);
189 }
190 };
191
192 Tab = function(tab) {
193 this._id = tab.id;
194
195 this.url = tab.url;
196 this.pageAction = new PageAction(tab.id);
197
198 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this);
199 this.onCompleted = ext.tabs.onCompleted._bindToTab(this);
200 this.onActivated = ext.tabs.onActivated._bindToTab(this);
201 this.onRemoved = ext.tabs.onRemoved._bindToTab(this);
202 };
203 Tab.prototype = {
204 close: function() {
205 chrome.tabs.remove(this._id);
206 },
207 activate: function() {
208 chrome.tabs.update(this._id, {selected: true});
209 },
210 sendMessage: function(message, responseCallback) {
211 chrome.tabs.sendMessage(this._id, message, responseCallback);
212 }
213 };
214
215 TabMap = function() {
216 this._map = {};
217 this.delete = this.delete.bind(this);
218 };
219 TabMap.prototype = {
220 get: function(tab) {
221 return (this._map[tab._id] || {}).value;
222 },
223 set: function(tab, value) {
224 if (!(tab._id in this._map))
225 tab.onRemoved.addListener(this.delete);
226
227 this._map[tab._id] = {tab: tab, value: value};
228 },
229 has: function(tab) {
230 return tab._id in this._map;
231 },
232 clear: function() {
233 for (var id in this._map)
234 this.delete(this._map[id].tab);
235 }
236 };
237 TabMap.prototype["delete"] = function(tab) {
238 delete this._map[tab._id];
239 tab.onRemoved.removeListener(this.delete);
240 };
241
242
243 /* Windows */
244
245 Window = function(win) {
246 this._id = win.id;
247 this.visible = win.status != "minimized";
248 };
249 Window.prototype = {
250 getAllTabs: function(callback) {
251 chrome.tabs.query({windowId: this._id}, function(tabs) {
252 callback(tabs.map(function(tab) {
253 return new Tab(tab);
254 }));
255 });
256 },
257 getActiveTab: function(callback) {
258 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) {
259 callback(new Tab(tabs[0]));
260 });
261 },
262 openTab: function(url, callback) {
263 var props = {windowId: this._id, url: url};
264
265 if (!callback)
266 chrome.tabs.create(props);
267 else
268 chrome.tabs.create(props, function(tab) {
269 callback(new Tab(tab));
270 });
271 }
272 };
273
274
275 /* API */
276
277 ext.windows = {
278 getAll: function(callback) {
279 chrome.windows.getAll(function(windows) {
280 callback(windows.map(function(win) {
281 return new Window(win);
282 }));
283 });
284 },
285 getLastFocused: function(callback) {
286 chrome.windows.getLastFocused(function(win) {
287 callback(new Window(win));
288 });
289 }
290 };
291
292 ext.tabs = {
293 onBeforeNavigate: new BeforeNavigateTabEventTarget(),
294 onCompleted: new CompletedTabEventTarget(),
295 onActivated: new ActivatedTabEventTarget(),
296 onRemoved: new RemovedTabEventTarget()
297 };
298
299 ext.webRequest = {
300 onBeforeRequest: new BeforeRequestEventTarget(),
301 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
302 };
303 })();
OLDNEW
« no previous file with comments | « block.js ('k') | chrome/common.js » ('j') | chrome/common.js » ('J')

Powered by Google App Engine
This is Rietveld