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: Addressed comments Created April 11, 2014, 2:47 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 | « background.js ('k') | chrome/ext/common.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-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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)
111 { 64 {
112 if (details.frameId == 0) 65 chrome.tabs.create({url: url}, function(openedTab)
113 listener(new Tab({id: details.tabId, url: details.url})); 66 {
114 }; 67 var onUpdated = function(tabId, changeInfo, tab)
115 } 68 {
69 if (tabId == openedTab.id && changeInfo.status == "complete")
70 {
71 chrome.tabs.onUpdated.removeListener(onUpdated);
72 callback(new Page(tab));
73 }
74 };
75 chrome.tabs.onUpdated.addListener(onUpdated);
76 });
77 }
78 else
79 chrome.tabs.create({url: url});
80 },
81 query: function(info, callback)
82 {
83 var rawInfo = {};
84 for (var property in info)
85 {
86 switch (property)
87 {
88 case "active":
89 case "lastFocusedWindow":
90 rawInfo[property] = info[property];
91 }
92 }
93
94 chrome.tabs.query(rawInfo, function(tabs)
95 {
96 callback(tabs.map(function(tab)
97 {
98 return new Page(tab);
99 }));
100 });
101 },
102 onLoading: new ext._EventTarget()
116 }; 103 };
117 104
118 var LoadingTabEventTarget = function() 105 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
119 { 106 {
120 TabEventTarget.call(this, chrome.tabs.onUpdated); 107 if (changeInfo.status == "loading")
121 }; 108 ext.pages.onLoading._dispatch(new Page(tab));
122 LoadingTabEventTarget.prototype = { 109 });
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 110
134 var CompletedTabEventTarget = function() 111 chrome.webNavigation.onBeforeNavigate.addListener(function(details)
135 { 112 {
136 TabEventTarget.call(this, chrome.tabs.onUpdated); 113 if (details.frameId == 0)
137 }; 114 ext._removeFromAllPageMaps(details.tabId);
138 CompletedTabEventTarget.prototype = { 115 });
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 116
150 var ActivatedTabEventTarget = function() 117 chrome.tabs.onRemoved.addListener(function(tabId)
151 { 118 {
152 TabEventTarget.call(this, chrome.tabs.onActivated); 119 ext._removeFromAllPageMaps(tabId);
153 }; 120 delete framesOfTabs[tabId];
154 ActivatedTabEventTarget.prototype = { 121 });
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
190 //url parameter is missing in sender object (Chrome v28 and below)
191 if (!("url" in sender))
192 sender.url = tab.url;
193 return {tab: tab, frame: new Frame({url: sender.url, tab: tab})};
194 }
195 };
196 122
197 123
198 /* Tabs */ 124 /* Browser actions */
199
200 var sendMessage = chrome.tabs.sendMessage || chrome.tabs.sendRequest;
201 125
202 var BrowserAction = function(tabId) 126 var BrowserAction = function(tabId)
203 { 127 {
204 this._tabId = tabId; 128 this._tabId = tabId;
205 }; 129 };
206 BrowserAction.prototype = { 130 BrowserAction.prototype = {
207 setIcon: function(path) 131 setIcon: function(path)
208 { 132 {
209 var paths = {}; 133 var paths = {};
210 for (var i = 1; i <= 2; i++) 134 for (var i = 1; i <= 2; i++)
(...skipping 26 matching lines...) Expand all
237 if ("number" in badge) 161 if ("number" in badge)
238 { 162 {
239 chrome.browserAction.setBadgeText({ 163 chrome.browserAction.setBadgeText({
240 tabId: this._tabId, 164 tabId: this._tabId,
241 text: badge.number.toString() 165 text: badge.number.toString()
242 }); 166 });
243 } 167 }
244 } 168 }
245 }; 169 };
246 170
247 Tab = function(tab)
248 {
249 this._id = tab.id;
250 this._url = tab.url;
251
252 this.browserAction = new BrowserAction(tab.id);
253
254 this.onLoading = ext.tabs.onLoading._bindToTab(this);
255 this.onCompleted = ext.tabs.onCompleted._bindToTab(this);
256 this.onActivated = ext.tabs.onActivated._bindToTab(this);
257 this.onRemoved = ext.tabs.onRemoved._bindToTab(this);
258
259 // the "beforeNavigate" event in Safari isn't dispatched when a new URL
260 // was entered into the address bar. So we can only use it only on Chrome,
261 // but we have to hide it from the browser-independent high level code.
262 this._onBeforeNavigate = ext.tabs._onBeforeNavigate._bindToTab(this);
263 };
264 Tab.prototype = {
265 get url()
266 {
267 // usually our Tab objects are created from chrome Tab objects, which
268 // provide the url. So we can return the url given in the constructor.
269 if (this._url != null)
270 return this._url;
271
272 // but sometimes we only have the id when we create a Tab object.
273 // In that case we get the url from top frame of the tab, recorded by
274 // the onBeforeRequest handler.
275 var frames = framesOfTabs.get(this);
276 if (frames)
277 {
278 var frame = frames[0];
279 if (frame)
280 return frame.url;
281 }
282 },
283 close: function()
284 {
285 chrome.tabs.remove(this._id);
286 },
287 activate: function()
288 {
289 chrome.tabs.update(this._id, {selected: true});
290 },
291 sendMessage: function(message, responseCallback)
292 {
293 sendMessage(this._id, message, responseCallback);
294 }
295 };
296
297 TabMap = function(deleteOnPageUnload)
298 {
299 this._map = {};
300
301 this._delete = this._delete.bind(this);
302 this._deleteOnPageUnload = deleteOnPageUnload;
303 };
304 TabMap.prototype = {
305 get: function(tab)
306 {
307 return (this._map[tab._id] || {}).value;
308 },
309 set: function(tab, value)
310 {
311 if (!(tab._id in this._map))
312 {
313 tab.onRemoved.addListener(this._delete);
314 if (this._deleteOnPageUnload)
315 tab._onBeforeNavigate.addListener(this._delete);
316 }
317
318 this._map[tab._id] = {tab: tab, value: value};
319 },
320 has: function(tab)
321 {
322 return tab._id in this._map;
323 },
324 clear: function()
325 {
326 for (var id in this._map)
327 this.delete(this._map[id].tab);
328 },
329 _delete: function(tab)
330 {
331 // delay so that other event handlers can still lookup this tab
332 setTimeout(this.delete.bind(this, tab), 0);
333 },
334 delete: function(tab)
335 {
336 delete this._map[tab._id];
337
338 tab.onRemoved.removeListener(this._delete);
339 tab._onBeforeNavigate.removeListener(this._delete);
340 }
341 };
342
343
344 /* Windows */
345
346 Window = function(win)
347 {
348 this._id = win.id;
349 this.visible = win.status != "minimized";
350 };
351 Window.prototype = {
352 getAllTabs: function(callback)
353 {
354 chrome.tabs.query({windowId: this._id}, function(tabs)
355 {
356 callback(tabs.map(function(tab) { return new Tab(tab); }));
357 });
358 },
359 getActiveTab: function(callback)
360 {
361 chrome.tabs.query({windowId: this._id, active: true}, function(tabs)
362 {
363 callback(new Tab(tabs[0]));
364 });
365 },
366 openTab: function(url, callback)
367 {
368 var props = {windowId: this._id, url: url};
369
370 if (!callback)
371 chrome.tabs.create(props);
372 else
373 chrome.tabs.create(props, function(tab)
374 {
375 callback(new Tab(tab));
376 });
377 }
378 };
379
380 171
381 /* Frames */ 172 /* Frames */
382 173
383 var framesOfTabs = new TabMap(); 174 var framesOfTabs = {__proto__: null};
384 175
385 Frame = function(params) 176 var Frame = ext.Frame = function(params)
386 { 177 {
387 this._tab = params.tab; 178 this._frameId = params.frameId;
388 this._id = params.id; 179 this._tabId = params.tabId;
389 this._url = params.url; 180 this._url = params.url;
390 }; 181 };
391 Frame.prototype = { 182 Frame.prototype = {
392 get url() 183 get url()
393 { 184 {
394 if (this._url != null) 185 if (this._url != null)
395 return this._url; 186 return this._url;
396 187
397 var frames = framesOfTabs.get(this._tab); 188 var frames = framesOfTabs[this._tabId];
398 if (frames) 189 if (frames)
399 { 190 {
400 var frame = frames[this._id]; 191 var frame = frames[this._frameId];
401 if (frame) 192 if (frame)
402 return frame.url; 193 return frame.url;
403 } 194 }
404 }, 195 },
405 get parent() 196 get parent()
406 { 197 {
407 var frames = framesOfTabs.get(this._tab); 198 var frames = framesOfTabs[this._tabId];
408 if (frames) 199 if (frames)
409 { 200 {
410 var frame; 201 var frame;
411 if (this._id != null) 202 if (this._frameId != null)
412 frame = frames[this._id]; 203 frame = frames[this._frameId];
413 else 204 else
414 { 205 {
415 // the frame ID wasn't available when we created 206 // the frame ID wasn't available when we created
416 // the Frame object (e.g. for the onMessage event), 207 // the Frame object (e.g. for the onMessage event),
417 // so we have to find the frame details by their URL. 208 // so we have to find the frame details by their URL.
418 for (var frameId in frames) 209 for (var frameId in frames)
419 { 210 {
420 if (frames[frameId].url == this._url) 211 if (frames[frameId].url == this._url)
421 { 212 {
422 frame = frames[frameId]; 213 frame = frames[frameId];
423 break; 214 break;
424 } 215 }
425 } 216 }
426 } 217 }
427 218
428 if (!frame || frame.parent == -1) 219 if (!frame || frame.parent == -1)
429 return null; 220 return null;
430 221
431 return new Frame({id: frame.parent, tab: this._tab}); 222 return new Frame({frameId: frame.parent, tabId: this._tabId});
432 } 223 }
433 } 224 }
434 }; 225 };
435 226
436 227
437 /* Web request blocking */ 228 /* Web requests */
229
230 ext.webRequest = {
231 onBeforeRequest: new ext._EventTarget(true),
232 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
233 };
438 234
439 chrome.webRequest.onBeforeRequest.addListener(function(details) 235 chrome.webRequest.onBeforeRequest.addListener(function(details)
440 { 236 {
441 try 237 try
442 { 238 {
443 // the high-level code isn't interested in requests that aren't related 239 // the high-level code isn't interested in requests that aren't related
444 // to a tab and since those can only be handled in Chrome, we ignore 240 // to a tab and since those can only be handled in Chrome, we ignore
445 // them here instead of in the browser independent high-level code. 241 // them here instead of in the browser independent high-level code.
446 if (details.tabId == -1) 242 if (details.tabId == -1)
447 return; 243 return;
448 244
449 var tab = new Tab({id: details.tabId}); 245 var page = new Tab({id: details.tabId});
450 var frames = framesOfTabs.get(tab); 246 var frames = framesOfTabs[details.tabId];
451 247
452 if (!frames) 248 if (!frames)
453 { 249 {
454 frames = []; 250 frames = framesOfTabs[details.tabId] = [];
455 framesOfTabs.set(tab, frames);
456 251
457 // assume that the first request belongs to the top frame. Chrome 252 // assume that the first request belongs to the top frame. Chrome
458 // may give the top frame the type "object" instead of "main_frame". 253 // may give the top frame the type "object" instead of "main_frame".
459 // https://code.google.com/p/chromium/issues/detail?id=281711 254 // https://code.google.com/p/chromium/issues/detail?id=281711
460 if (frameId == 0) 255 if (frameId == 0)
461 details.type = "main_frame"; 256 details.type = "main_frame";
462 } 257 }
463 258
464 var frameId; 259 var frameId;
465 if (details.type == "main_frame" || details.type == "sub_frame") 260 if (details.type == "main_frame" || details.type == "sub_frame")
(...skipping 20 matching lines...) Expand all
486 281
487 // however when the src of the frame is a javascript: or data: URL, we 282 // however when the src of the frame is a javascript: or data: URL, we
488 // don't know the frame either. But since we know the top-level frame we 283 // don't know the frame either. But since we know the top-level frame we
489 // can just pretend that we are in the top-level frame, in order to have 284 // can just pretend that we are in the top-level frame, in order to have
490 // at least most domain-based filter rules working. 285 // at least most domain-based filter rules working.
491 frameId = 0; 286 frameId = 0;
492 if (details.type == "sub_frame") 287 if (details.type == "sub_frame")
493 frames[details.frameId].parent = frameId; 288 frames[details.frameId].parent = frameId;
494 } 289 }
495 290
496 var frame = new Frame({id: frameId, tab: tab}); 291 var frame = new Frame({id: frameId, tabId: details.tabId});
497 292
498 for (var i = 0; i < ext.webRequest.onBeforeRequest._listeners.length; i++) 293 if (!ext.webRequest.onBeforeRequest._dispatch(details.url, details.type, p age, frame))
499 { 294 return {cancel: true};
500 if (ext.webRequest.onBeforeRequest._listeners[i](details.url, details.ty pe, tab, frame) === false)
501 return {cancel: true};
502 }
503 } 295 }
504 catch (e) 296 catch (e)
505 { 297 {
506 // recent versions of Chrome cancel the request when an error occurs in 298 // recent versions of Chrome cancel the request when an error occurs in
507 // the onBeforeRequest listener. However in our case it is preferred, to 299 // the onBeforeRequest listener. However in our case it is preferred, to
508 // let potentially some ads through, rather than blocking legit requests. 300 // let potentially some ads through, rather than blocking legit requests.
509 console.error(e); 301 console.error(e);
510 } 302 }
511 }, {urls: ["<all_urls>"]}, ["blocking"]); 303 }, {urls: ["<all_urls>"]}, ["blocking"]);
512 304
513 305
514 /* API */ 306 /* Context menus */
515
516 ext.windows = {
517 getAll: function(callback)
518 {
519 chrome.windows.getAll(function(windows)
520 {
521 callback(windows.map(function(win)
522 {
523 return new Window(win);
524 }));
525 });
526 },
527 getLastFocused: function(callback)
528 {
529 chrome.windows.getLastFocused(function(win)
530 {
531 callback(new Window(win));
532 });
533 }
534 };
535
536 ext.tabs = {
537 onLoading: new LoadingTabEventTarget(),
538 onCompleted: new CompletedTabEventTarget(),
539 onActivated: new ActivatedTabEventTarget(),
540 onRemoved: new RemovedTabEventTarget(),
541
542 // the "beforeNavigate" event in Safari isn't dispatched when a new URL
543 // was entered into the address bar. So we can only use it only on Chrome,
544 // but we have to hide it from the browser-independent high level code.
545 _onBeforeNavigate: new BeforeNavigateTabEventTarget()
546 };
547
548 ext.webRequest = {
549 onBeforeRequest: new SimpleEventTarget(),
550 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
551 };
552
553 ext.storage = localStorage;
554 307
555 var contextMenuItems = []; 308 var contextMenuItems = [];
556 var isContextMenuHidden = true; 309 var isContextMenuHidden = true;
310
557 ext.contextMenus = { 311 ext.contextMenus = {
558 addMenuItem: function(title, contexts, onclick) 312 addMenuItem: function(title, contexts, onclick)
559 { 313 {
560 contextMenuItems.push({ 314 contextMenuItems.push({
561 title: title, 315 title: title,
562 contexts: contexts, 316 contexts: contexts,
563 onclick: function(info, tab) 317 onclick: function(info, tab)
564 { 318 {
565 onclick(info.srcUrl, new Tab(tab)); 319 onclick(info.srcUrl, new Page(tab));
566 } 320 }
567 }); 321 });
568 this.showMenuItems(); 322 this.showMenuItems();
569 }, 323 },
570 removeMenuItems: function() 324 removeMenuItems: function()
571 { 325 {
572 contextMenuItems = []; 326 contextMenuItems = [];
573 this.hideMenuItems(); 327 this.hideMenuItems();
574 }, 328 },
575 showMenuItems: function() 329 showMenuItems: function()
(...skipping 18 matching lines...) Expand all
594 hideMenuItems: function() 348 hideMenuItems: function()
595 { 349 {
596 if (isContextMenuHidden) 350 if (isContextMenuHidden)
597 return; 351 return;
598 352
599 chrome.contextMenus.removeAll(); 353 chrome.contextMenus.removeAll();
600 isContextMenuHidden = true; 354 isContextMenuHidden = true;
601 } 355 }
602 }; 356 };
603 357
604 ext.onMessage = new BackgroundMessageEventTarget(); 358
359 /* Message passing */
360
361 ext._setupMessageListener(function(sender)
362 {
363 return {
364 page: new Page(sender.tab),
365 frame: new Frame({url: sender.url, tabId: sender.tab.id})
366 };
367 });
368
369
370 /* Storage */
371
372 ext.storage = localStorage;
605 })(); 373 })();
OLDNEW
« no previous file with comments | « background.js ('k') | chrome/ext/common.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld