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

Side by Side Diff: chrome/ext/background.js

Issue 5464830253203456: Refactored the abstraction layer to address prerendered pages on Safari caused by leaky abstraction (Closed)
Patch Set: Created Feb. 22, 2014, 10:45 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
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 /* Pages */
21 21
22 var TabEventTarget = function() 22 var sendMessage = chrome.tabs.sendMessage || chrome.tabs.sendRequest;
23
24 var Page = ext.Page = function(tab)
23 { 25 {
24 WrappedEventTarget.apply(this, arguments); 26 this._id = tab.id;
27 this._url = tab.url;
25 28
26 this._tabs = {}; 29 this.browserAction = new BrowserAction(tab.id);
30 };
31 Page.prototype = {
32 get url()
33 {
34 // usually our Page objects are created from Chrome's Tab objects, which
35 // provide the url. So we can return the url given in the constructor.
36 if (this._url != null)
37 return this._url;
27 38
28 this._sharedListener = this._sharedListener.bind(this); 39 // but sometimes we only have the tab id when we create a Page object.
29 this._removeTab = this._removeTab.bind(this); 40 // In that case we get the url from top frame of the tab, recorded by
30 }; 41 // the onBeforeRequest handler.
31 TabEventTarget.prototype = { 42 var frames = framesOfTabs[this._id];
32 __proto__: WrappedEventTarget.prototype, 43 if (frames)
33 _bindToTab: function(tab) 44 {
45 var frame = frames[0];
46 if (frame)
47 return frame.url;
48 }
49 },
50 activate: function()
34 { 51 {
35 return { 52 chrome.tabs.update(this._id, {selected: true});
36 addListener: function(listener)
37 {
38 var listeners = this._tabs[tab._id];
39
40 if (!listeners)
41 {
42 this._tabs[tab._id] = listeners = [];
43
44 if (Object.keys(this._tabs).length == 1)
45 this.addListener(this._sharedListener);
46
47 tab.onRemoved.addListener(this._removeTab);
48 }
49
50 listeners.push(listener);
51 }.bind(this),
52 removeListener: function(listener)
53 {
54 var listeners = this._tabs[tab._id];
55 if (!listeners)
56 return;
57
58 var idx = listeners.indexOf(listener);
59 if (idx == -1)
60 return;
61
62 listeners.splice(idx, 1);
63
64 if (listeners.length == 0)
65 tab.onRemoved.removeListener(this._removeTab);
66 else
67 {
68 if (listeners.length > 1)
69 return;
70 if (listeners[0] != this._removeTab)
71 return;
72 }
73
74 this._removeTab(tab);
75 }.bind(this)
76 };
77 }, 53 },
78 _sharedListener: function(tab) 54 sendMessage: function(message, responseCallback)
79 { 55 {
80 var listeners = this._tabs[tab._id]; 56 sendMessage(this._id, message, responseCallback);
81
82 if (!listeners)
83 return;
84
85 // copy listeners before calling them, because they might
86 // add or remove other listeners, which must not be taken
87 // into account before the next occurrence of the event
88 listeners = listeners.slice(0);
89
90 for (var i = 0; i < listeners.length; i++)
91 listeners[i](tab);
92 },
93 _removeTab: function(tab)
94 {
95 delete this._tabs[tab._id];
96
97 if (Object.keys(this._tabs).length == 0)
98 this.removeListener(this._sharedListener);
99 } 57 }
100 }; 58 };
101 59
102 var BeforeNavigateTabEventTarget = function() 60 ext.pages = {
103 { 61 open: function(url, callback)
104 TabEventTarget.call(this, chrome.webNavigation.onBeforeNavigate);
105 };
106 BeforeNavigateTabEventTarget.prototype = {
107 __proto__: TabEventTarget.prototype,
108 _wrapListener: function(listener)
109 { 62 {
110 return function(details) 63 if (callback)
64 chrome.tabs.create({url: url}, function(tab) { callback(new Page(tab)); });
65 else
66 chrome.tabs.create({url: url});
67 },
68 query: function(info, callback)
69 {
70 var rawInfo = {};
71 var visibleWindow = null;
72
73 for (var property in info)
111 { 74 {
112 if (details.frameId == 0) 75 switch (property)
113 listener(new Tab({id: details.tabId, url: details.url})); 76 {
114 }; 77 case "active":
115 } 78 case "lastFocusedWindow":
79 rawInfo[property] = info[property];
80 break;
81 case "visibleWindow":
82 visibleWindow = info[property];
83 break;
84 }
85 }
86
87 chrome.tabs.query(rawInfo, function(tabs)
88 {
89 if (visibleWindow != null && tabs.length > 0)
90 {
91 var windows = {__proto__: null};
92 var pending = 0;
93
94 for (var i = 0; i < tabs.length; i++)
95 {
96 var windowId = tabs[i].windowId;
97 if (!(windowId in windows))
98 {
99 windows[windowId] = null;
100 pending++;
101
102 chrome.windows.get(windowId, null, function(win)
103 {
104 if (visibleWindow != (win.state != "minimized"))
Wladimir Palant 2014/04/04 14:00:35 That's quite a bit of code just to exclude minimiz
Sebastian Noack 2014/04/07 13:15:25 We need that for the icon animation, in order to a
Wladimir Palant 2014/04/11 13:02:35 It seems that the original code was only animating
Sebastian Noack 2014/04/11 14:47:45 Done.
105 {
106 for (var j = 0; j < tabs.length; j++)
107 {
108 if (tabs[j].windowId == win.id)
109 tabs.splice(j--, 1);
110 }
111 }
112
113 if (--pending == 0)
114 callback(tabs.map(function(tab)
115 {
116 return new Page(tab);
117 }));
118 });
119 }
120 }
121 }
122 else
123 {
124 callback(tabs.map(function(tab)
125 {
126 return new Page(tab);
127 }));
128 }
129 });
130 },
131 onLoading: new ext._EventTarget()
116 }; 132 };
117 133
118 var LoadingTabEventTarget = function() 134 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
119 { 135 {
120 TabEventTarget.call(this, chrome.tabs.onUpdated); 136 if (changeInfo.status == "loading")
121 }; 137 ext.pages.onLoading._dispatch(new Page(tab));
122 LoadingTabEventTarget.prototype = { 138 });
123 __proto__: TabEventTarget.prototype,
124 _wrapListener: function(listener)
125 {
126 return function(id, info, tab)
127 {
128 if (info.status == "loading")
129 listener(new Tab(tab));
130 };
131 }
132 };
133 139
134 var CompletedTabEventTarget = function() 140 chrome.webNavigation.onBeforeNavigate.addListener(function(details)
135 { 141 {
136 TabEventTarget.call(this, chrome.tabs.onUpdated); 142 if (details.frameId == 0)
137 }; 143 ext._removeFromAllPageMaps(details.tabId);
138 CompletedTabEventTarget.prototype = { 144 });
Wladimir Palant 2014/04/04 14:00:35 Just wondering: will this also be triggered for na
Sebastian Noack 2014/04/07 13:15:25 It isn't triggered for javascript: URLs entered in
Wladimir Palant 2014/04/11 13:02:35 I did some testing and it seems that using onBefor
139 __proto__: TabEventTarget.prototype,
140 _wrapListener: function(listener)
141 {
142 return function(id, info, tab)
143 {
144 if (info.status == "complete")
145 listener(new Tab(tab));
146 };
147 }
148 };
149 145
150 var ActivatedTabEventTarget = function() 146 chrome.tabs.onRemoved.addListener(function(tabId)
151 { 147 {
152 TabEventTarget.call(this, chrome.tabs.onActivated); 148 ext._removeFromAllPageMaps(tabId);
153 }; 149 delete framesOfTabs[tabId];
154 ActivatedTabEventTarget.prototype = { 150 });
155 __proto__: TabEventTarget.prototype,
156 _wrapListener: function(listener)
157 {
158 return function(info)
159 {
160 chrome.tabs.get(info.tabId, function(tab)
161 {
162 listener(new Tab(tab));
163 });
164 };
165 }
166 }
167
168 var RemovedTabEventTarget = function()
169 {
170 TabEventTarget.call(this, chrome.tabs.onRemoved);
171 };
172 RemovedTabEventTarget.prototype = {
173 __proto__: TabEventTarget.prototype,
174 _wrapListener: function(listener)
175 {
176 return function(id) { listener(new Tab({id: id})); };
177 }
178 };
179
180 var BackgroundMessageEventTarget = function()
181 {
182 MessageEventTarget.call(this);
183 }
184 BackgroundMessageEventTarget.prototype = {
185 __proto__: MessageEventTarget.prototype,
186 _wrapSender: function(sender)
187 {
188 var tab = new Tab(sender.tab);
189 return {tab: tab, frame: new Frame({url: sender.url, tab: tab})};
190 }
191 };
192 151
193 152
194 /* Tabs */ 153 /* Browser actions */
195
196 var sendMessage = chrome.tabs.sendMessage || chrome.tabs.sendRequest;
197 154
198 var BrowserAction = function(tabId) 155 var BrowserAction = function(tabId)
199 { 156 {
200 this._tabId = tabId; 157 this._tabId = tabId;
201 }; 158 };
202 BrowserAction.prototype = { 159 BrowserAction.prototype = {
203 setIcon: function(path) 160 setIcon: function(path)
204 { 161 {
205 chrome.browserAction.setIcon({tabId: this._tabId, path: path}); 162 chrome.browserAction.setIcon({tabId: this._tabId, path: path});
206 }, 163 },
(...skipping 19 matching lines...) Expand all
226 if ("number" in badge) 183 if ("number" in badge)
227 { 184 {
228 chrome.browserAction.setBadgeText({ 185 chrome.browserAction.setBadgeText({
229 tabId: this._tabId, 186 tabId: this._tabId,
230 text: badge.number.toString() 187 text: badge.number.toString()
231 }); 188 });
232 } 189 }
233 } 190 }
234 }; 191 };
235 192
236 Tab = function(tab)
237 {
238 this._id = tab.id;
239 this._url = tab.url;
240
241 this.browserAction = new BrowserAction(tab.id);
242
243 this.onLoading = ext.tabs.onLoading._bindToTab(this);
244 this.onCompleted = ext.tabs.onCompleted._bindToTab(this);
245 this.onActivated = ext.tabs.onActivated._bindToTab(this);
246 this.onRemoved = ext.tabs.onRemoved._bindToTab(this);
247
248 // the "beforeNavigate" event in Safari isn't dispatched when a new URL
249 // was entered into the address bar. So we can only use it only on Chrome,
250 // but we have to hide it from the browser-independent high level code.
251 this._onBeforeNavigate = ext.tabs._onBeforeNavigate._bindToTab(this);
252 };
253 Tab.prototype = {
254 get url()
255 {
256 // usually our Tab objects are created from chrome Tab objects, which
257 // provide the url. So we can return the url given in the constructor.
258 if (this._url != null)
259 return this._url;
260
261 // but sometimes we only have the id when we create a Tab object.
262 // In that case we get the url from top frame of the tab, recorded by
263 // the onBeforeRequest handler.
264 var frames = framesOfTabs.get(this);
265 if (frames)
266 {
267 var frame = frames[0];
268 if (frame)
269 return frame.url;
270 }
271 },
272 close: function()
273 {
274 chrome.tabs.remove(this._id);
275 },
276 activate: function()
277 {
278 chrome.tabs.update(this._id, {selected: true});
279 },
280 sendMessage: function(message, responseCallback)
281 {
282 sendMessage(this._id, message, responseCallback);
283 }
284 };
285
286 TabMap = function(deleteOnPageUnload)
287 {
288 this._map = {};
289
290 this._delete = this._delete.bind(this);
291 this._deleteOnPageUnload = deleteOnPageUnload;
292 };
293 TabMap.prototype = {
294 get: function(tab)
295 {
296 return (this._map[tab._id] || {}).value;
297 },
298 set: function(tab, value)
299 {
300 if (!(tab._id in this._map))
301 {
302 tab.onRemoved.addListener(this._delete);
303 if (this._deleteOnPageUnload)
304 tab._onBeforeNavigate.addListener(this._delete);
305 }
306
307 this._map[tab._id] = {tab: tab, value: value};
308 },
309 has: function(tab)
310 {
311 return tab._id in this._map;
312 },
313 clear: function()
314 {
315 for (var id in this._map)
316 this.delete(this._map[id].tab);
317 },
318 _delete: function(tab)
319 {
320 // delay so that other event handlers can still lookup this tab
321 setTimeout(this.delete.bind(this, tab), 0);
322 },
323 delete: function(tab)
324 {
325 delete this._map[tab._id];
326
327 tab.onRemoved.removeListener(this._delete);
328 tab._onBeforeNavigate.removeListener(this._delete);
329 }
330 };
331
332
333 /* Windows */
334
335 Window = function(win)
336 {
337 this._id = win.id;
338 this.visible = win.status != "minimized";
339 };
340 Window.prototype = {
341 getAllTabs: function(callback)
342 {
343 chrome.tabs.query({windowId: this._id}, function(tabs)
344 {
345 callback(tabs.map(function(tab) { return new Tab(tab); }));
346 });
347 },
348 getActiveTab: function(callback)
349 {
350 chrome.tabs.query({windowId: this._id, active: true}, function(tabs)
351 {
352 callback(new Tab(tabs[0]));
353 });
354 },
355 openTab: function(url, callback)
356 {
357 var props = {windowId: this._id, url: url};
358
359 if (!callback)
360 chrome.tabs.create(props);
361 else
362 chrome.tabs.create(props, function(tab)
363 {
364 callback(new Tab(tab));
365 });
366 }
367 };
368
369 193
370 /* Frames */ 194 /* Frames */
371 195
372 var framesOfTabs = new TabMap(); 196 var framesOfTabs = {__proto__: null};
373 197
374 Frame = function(params) 198 var Frame = ext.Frame = function(params)
375 { 199 {
376 this._tab = params.tab; 200 this._frameId = params.frameId;
377 this._id = params.id; 201 this._tabId = params.tabId;
378 this._url = params.url; 202 this._url = params.url;
379 }; 203 };
380 Frame.prototype = { 204 Frame.prototype = {
381 get url() 205 get url()
382 { 206 {
383 if (this._url != null) 207 if (this._url != null)
384 return this._url; 208 return this._url;
385 209
386 var frames = framesOfTabs.get(this._tab); 210 var frames = framesOfTabs[this._tabId];
387 if (frames) 211 if (frames)
388 { 212 {
389 var frame = frames[this._id]; 213 var frame = frames[this._frameId];
390 if (frame) 214 if (frame)
391 return frame.url; 215 return frame.url;
392 } 216 }
393 }, 217 },
394 get parent() 218 get parent()
395 { 219 {
396 var frames = framesOfTabs.get(this._tab); 220 var frames = framesOfTabs[this._tabId];
397 if (frames) 221 if (frames)
398 { 222 {
399 var frame; 223 var frame;
400 if (this._id != null) 224 if (this._frameId != null)
401 frame = frames[this._id]; 225 frame = frames[this._frameId];
402 else 226 else
403 { 227 {
404 // the frame ID wasn't available when we created 228 // the frame ID wasn't available when we created
405 // the Frame object (e.g. for the onMessage event), 229 // the Frame object (e.g. for the onMessage event),
406 // so we have to find the frame details by their URL. 230 // so we have to find the frame details by their URL.
407 for (var frameId in frames) 231 for (var frameId in frames)
408 { 232 {
409 if (frames[frameId].url == this._url) 233 if (frames[frameId].url == this._url)
410 { 234 {
411 frame = frames[frameId]; 235 frame = frames[frameId];
412 break; 236 break;
413 } 237 }
414 } 238 }
415 } 239 }
416 240
417 if (!frame || frame.parent == -1) 241 if (!frame || frame.parent == -1)
418 return null; 242 return null;
419 243
420 return new Frame({id: frame.parent, tab: this._tab}); 244 return new Frame({frameId: frame.parent, tabId: this._tabId});
421 } 245 }
422 } 246 }
423 }; 247 };
424 248
425 249
426 /* Web request blocking */ 250 /* Web requests */
251
252 ext.webRequest = {
253 onBeforeRequest: new ext._EventTarget(true),
254 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
255 };
427 256
428 chrome.webRequest.onBeforeRequest.addListener(function(details) 257 chrome.webRequest.onBeforeRequest.addListener(function(details)
429 { 258 {
430 // the high-level code isn't interested in requests that aren't related 259 // the high-level code isn't interested in requests that aren't related
431 // to a tab and since those can only be handled in Chrome, we ignore 260 // to a tab and since those can only be handled in Chrome, we ignore
432 // them here instead of in the browser independent high-level code. 261 // them here instead of in the browser independent high-level code.
433 if (details.tabId == -1) 262 if (details.tabId == -1)
434 return; 263 return;
435 264
436 var tab = new Tab({id: details.tabId}); 265 var page = new Page({id: details.tabId});
437 var frames = framesOfTabs.get(tab); 266 var frames = framesOfTabs[details.tabId];
438 267
439 if (!frames) 268 if (!frames)
440 { 269 {
441 frames = []; 270 frames = framesOfTabs[details.tabId] = [];
442 framesOfTabs.set(tab, frames);
443 271
444 // assume that the first request belongs to the top frame. Chrome 272 // assume that the first request belongs to the top frame. Chrome
445 // may give the top frame the type "object" instead of "main_frame". 273 // may give the top frame the type "object" instead of "main_frame".
446 // https://code.google.com/p/chromium/issues/detail?id=281711 274 // https://code.google.com/p/chromium/issues/detail?id=281711
447 if (frameId == 0) 275 if (frameId == 0)
448 details.type = "main_frame"; 276 details.type = "main_frame";
449 } 277 }
450 278
451 var frameId; 279 var frameId;
452 if (details.type == "main_frame" || details.type == "sub_frame") 280 if (details.type == "main_frame" || details.type == "sub_frame")
(...skipping 20 matching lines...) Expand all
473 301
474 // however when the src of the frame is a javascript: or data: URL, we 302 // however when the src of the frame is a javascript: or data: URL, we
475 // don't know the frame either. But since we know the top-level frame we 303 // don't know the frame either. But since we know the top-level frame we
476 // can just pretend that we are in the top-level frame, in order to have 304 // can just pretend that we are in the top-level frame, in order to have
477 // at least most domain-based filter rules working. 305 // at least most domain-based filter rules working.
478 frameId = 0; 306 frameId = 0;
479 if (details.type == "sub_frame") 307 if (details.type == "sub_frame")
480 frames[details.frameId].parent = frameId; 308 frames[details.frameId].parent = frameId;
481 } 309 }
482 310
483 var frame = new Frame({id: frameId, tab: tab}); 311 var frame = new Frame({frameId: frameId, tabId: details.tabId});
484 312
485 for (var i = 0; i < ext.webRequest.onBeforeRequest._listeners.length; i++) 313 if (!ext.webRequest.onBeforeRequest._dispatch(details.url, details.type, pag e, frame))
486 { 314 return {cancel: true};
487 if (ext.webRequest.onBeforeRequest._listeners[i](details.url, details.type , tab, frame) === false)
488 return {cancel: true};
489 }
490 }, {urls: ["<all_urls>"]}, ["blocking"]); 315 }, {urls: ["<all_urls>"]}, ["blocking"]);
491 316
492 317
493 /* API */ 318 /* Context menus */
494
495 ext.windows = {
496 getAll: function(callback)
497 {
498 chrome.windows.getAll(function(windows)
499 {
500 callback(windows.map(function(win)
501 {
502 return new Window(win);
503 }));
504 });
505 },
506 getLastFocused: function(callback)
507 {
508 chrome.windows.getLastFocused(function(win)
509 {
510 callback(new Window(win));
511 });
512 }
513 };
514
515 ext.tabs = {
516 onLoading: new LoadingTabEventTarget(),
517 onCompleted: new CompletedTabEventTarget(),
518 onActivated: new ActivatedTabEventTarget(),
519 onRemoved: new RemovedTabEventTarget(),
520
521 // the "beforeNavigate" event in Safari isn't dispatched when a new URL
522 // was entered into the address bar. So we can only use it only on Chrome,
523 // but we have to hide it from the browser-independent high level code.
524 _onBeforeNavigate: new BeforeNavigateTabEventTarget()
525 };
526
527 ext.webRequest = {
528 onBeforeRequest: new SimpleEventTarget(),
529 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
530 };
531 319
532 var contextMenuItems = []; 320 var contextMenuItems = [];
533 var isContextMenuHidden = true; 321 var isContextMenuHidden = true;
322
534 ext.contextMenus = { 323 ext.contextMenus = {
535 addMenuItem: function(title, contexts, onclick) 324 addMenuItem: function(title, contexts, onclick)
536 { 325 {
537 contextMenuItems.push({ 326 contextMenuItems.push({
538 title: title, 327 title: title,
539 contexts: contexts, 328 contexts: contexts,
540 onclick: function(info, tab) 329 onclick: function(info, tab)
541 { 330 {
542 onclick(info.srcUrl, new Tab(tab)); 331 onclick(info.srcUrl, new Page(tab));
543 } 332 }
544 }); 333 });
545 this.showMenuItems(); 334 this.showMenuItems();
546 }, 335 },
547 removeMenuItems: function() 336 removeMenuItems: function()
548 { 337 {
549 contextMenuItems = []; 338 contextMenuItems = [];
550 this.hideMenuItems(); 339 this.hideMenuItems();
551 }, 340 },
552 showMenuItems: function() 341 showMenuItems: function()
(...skipping 18 matching lines...) Expand all
571 hideMenuItems: function() 360 hideMenuItems: function()
572 { 361 {
573 if (isContextMenuHidden) 362 if (isContextMenuHidden)
574 return; 363 return;
575 364
576 chrome.contextMenus.removeAll(); 365 chrome.contextMenus.removeAll();
577 isContextMenuHidden = true; 366 isContextMenuHidden = true;
578 } 367 }
579 }; 368 };
580 369
581 ext.onMessage = new BackgroundMessageEventTarget(); 370
371 /* Message passing */
372
373 ext._setupMessageListener(function(sender)
374 {
375 return {
376 page: new Page(sender.tab),
377 frame: new Frame({url: sender.url, tabId: sender.tab.id})
378 };
379 });
582 })(); 380 })();
OLDNEW

Powered by Google App Engine
This is Rietveld