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

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

Issue 29374674: Issue 4864 - Start using ESLint for adblockpluschrome (Closed)
Patch Set: Use var for ext declarations again Created Feb. 8, 2017, 9:02 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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 sendMessage(message, responseCallback) 50 sendMessage(message, responseCallback)
51 { 51 {
52 chrome.tabs.sendMessage(this.id, message, responseCallback); 52 chrome.tabs.sendMessage(this.id, message, responseCallback);
53 } 53 }
54 }; 54 };
55 55
56 ext.getPage = id => new Page({id: parseInt(id, 10)}); 56 ext.getPage = id => new Page({id: parseInt(id, 10)});
57 57
58 function afterTabLoaded(callback) 58 function afterTabLoaded(callback)
59 { 59 {
60 return openedTab => 60 return openedTab =>
61 { 61 {
62 let onUpdated = (tabId, changeInfo, tab) => 62 let onUpdated = (tabId, changeInfo, tab) =>
63 { 63 {
64 if (tabId == openedTab.id && changeInfo.status == "complete") 64 if (tabId == openedTab.id && changeInfo.status == "complete")
65 { 65 {
66 chrome.tabs.onUpdated.removeListener(onUpdated); 66 chrome.tabs.onUpdated.removeListener(onUpdated);
67 callback(new Page(openedTab)); 67 callback(new Page(openedTab));
68 } 68 }
69 }; 69 };
70 chrome.tabs.onUpdated.addListener(onUpdated); 70 chrome.tabs.onUpdated.addListener(onUpdated);
71 }; 71 };
72 } 72 }
73 73
74 ext.pages = { 74 ext.pages = {
75 open(url, callback) 75 open(url, callback)
76 { 76 {
77 chrome.tabs.create({url: url}, callback && afterTabLoaded(callback)); 77 chrome.tabs.create({url}, callback && afterTabLoaded(callback));
78 }, 78 },
79 query(info, callback) 79 query(info, callback)
80 { 80 {
81 let rawInfo = {}; 81 let rawInfo = {};
82 for (let property in info) 82 for (let property in info)
83 { 83 {
84 switch (property) 84 switch (property)
85 { 85 {
86 case "active": 86 case "active":
87 case "lastFocusedWindow": 87 case "lastFocusedWindow":
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 let frame = createFrame(details.tabId, details.frameId); 124 let frame = createFrame(details.tabId, details.frameId);
125 frame.parent = framesOfTabs[details.tabId][details.parentFrameId] || null; 125 frame.parent = framesOfTabs[details.tabId][details.parentFrameId] || null;
126 }); 126 });
127 127
128 let eagerlyUpdatedPages = new ext.PageMap(); 128 let eagerlyUpdatedPages = new ext.PageMap();
129 129
130 ext._updatePageFrameStructure = (frameId, tabId, url, eager) => 130 ext._updatePageFrameStructure = (frameId, tabId, url, eager) =>
131 { 131 {
132 if (frameId == 0) 132 if (frameId == 0)
133 { 133 {
134 let page = new Page({id: tabId, url: url}); 134 let page = new Page({id: tabId, url});
135 135
136 if (eagerlyUpdatedPages.get(page) != url) 136 if (eagerlyUpdatedPages.get(page) != url)
137 { 137 {
138 ext._removeFromAllPageMaps(tabId); 138 ext._removeFromAllPageMaps(tabId);
139 139
140 // When a sitekey header is received we must immediately update the page 140 // When a sitekey header is received we must immediately update the page
141 // structure in order to record and use the key. We want to avoid 141 // structure in order to record and use the key. We want to avoid
142 // trashing the page structure if the onCommitted event is then fired 142 // trashing the page structure if the onCommitted event is then fired
143 // for the page. 143 // for the page.
144 if (eager) 144 if (eager)
145 eagerlyUpdatedPages.set(page, url); 145 eagerlyUpdatedPages.set(page, url);
146 146
147 chrome.tabs.get(tabId, () => 147 chrome.tabs.get(tabId, () =>
148 { 148 {
149 // If the tab is prerendered, chrome.tabs.get() sets 149 // If the tab is prerendered, chrome.tabs.get() sets
150 // chrome.runtime.lastError and we have to dispatch the onLoading even t, 150 // chrome.runtime.lastError and we have to dispatch the
151 // since the onUpdated event isn't dispatched for prerendered tabs. 151 // onLoading event, since the onUpdated event isn't
152 // However, we have to keep relying on the unUpdated event for tabs th at 152 // dispatched for prerendered tabs. However, we have to
153 // are already visible. Otherwise browser action changes get overridde n 153 // keep relying on the unUpdated event for tabs that are
154 // when Chrome automatically resets them on navigation. 154 // already visible. Otherwise browser action changes get
155 // overridden when Chrome automatically resets them on
156 // navigation.
155 if (chrome.runtime.lastError) 157 if (chrome.runtime.lastError)
156 ext.pages.onLoading._dispatch(page); 158 ext.pages.onLoading._dispatch(page);
157 }); 159 });
158 } 160 }
159 } 161 }
160 162
161 // Update frame URL in frame structure 163 // Update frame URL in frame structure
162 let frame = createFrame(tabId, frameId); 164 let frame = createFrame(tabId, frameId);
163 frame.url = new URL(url); 165 frame.url = new URL(url);
164 }; 166 };
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 chrome.browserAction.setBadgeBackgroundColor({ 229 chrome.browserAction.setBadgeBackgroundColor({
228 tabId: this._tabId, 230 tabId: this._tabId,
229 color: this._changes.badgeColor 231 color: this._changes.badgeColor
230 }); 232 });
231 } 233 }
232 234
233 this._changes = null; 235 this._changes = null;
234 }, 236 },
235 _queueChanges() 237 _queueChanges()
236 { 238 {
237 chrome.tabs.get(this._tabId, function() 239 chrome.tabs.get(this._tabId, () =>
238 { 240 {
239 // If the tab is prerendered, chrome.tabs.get() sets 241 // If the tab is prerendered, chrome.tabs.get() sets
240 // chrome.runtime.lastError and we have to delay our changes 242 // chrome.runtime.lastError and we have to delay our changes
241 // until the currently visible tab is replaced with the 243 // until the currently visible tab is replaced with the
242 // prerendered tab. Otherwise chrome.browserAction.set* fails. 244 // prerendered tab. Otherwise chrome.browserAction.set* fails.
243 if (chrome.runtime.lastError) 245 if (chrome.runtime.lastError)
244 { 246 {
245 let onReplaced = (addedTabId, removedTabId) => 247 let onReplaced = (addedTabId, removedTabId) =>
246 { 248 {
247 if (addedTabId == this._tabId) 249 if (addedTabId == this._tabId)
248 { 250 {
249 chrome.tabs.onReplaced.removeListener(onReplaced); 251 chrome.tabs.onReplaced.removeListener(onReplaced);
250 this._applyChanges(); 252 this._applyChanges();
251 } 253 }
252 }; 254 };
253 chrome.tabs.onReplaced.addListener(onReplaced); 255 chrome.tabs.onReplaced.addListener(onReplaced);
254 } 256 }
255 else 257 else
256 { 258 {
257 this._applyChanges(); 259 this._applyChanges();
258 } 260 }
259 }.bind(this)); 261 });
260 }, 262 },
261 _addChange(name, value) 263 _addChange(name, value)
262 { 264 {
263 if (!this._changes) 265 if (!this._changes)
264 { 266 {
265 this._changes = {}; 267 this._changes = {};
266 this._queueChanges(); 268 this._queueChanges();
267 } 269 }
268 270
269 this._changes[name] = value; 271 this._changes[name] = value;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 373
372 /* Web requests */ 374 /* Web requests */
373 375
374 let framesOfTabs = Object.create(null); 376 let framesOfTabs = Object.create(null);
375 377
376 ext.getFrame = (tabId, frameId) => 378 ext.getFrame = (tabId, frameId) =>
377 { 379 {
378 return (framesOfTabs[tabId] || {})[frameId]; 380 return (framesOfTabs[tabId] || {})[frameId];
379 }; 381 };
380 382
381 let handlerBehaviorChangedQuota = chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANG ED_CALLS_PER_10_MINUTES; 383 let handlerBehaviorChangedQuota = (
384 chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES
385 );
Sebastian Noack 2017/02/09 01:04:49 The parenthesis here seem unnecessary.
kzar 2017/02/20 10:27:29 Done.
382 386
383 function propagateHandlerBehaviorChange() 387 function propagateHandlerBehaviorChange()
384 { 388 {
385 // Make sure to not call handlerBehaviorChanged() more often than allowed 389 // Make sure to not call handlerBehaviorChanged() more often than allowed
386 // by chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES. 390 // by chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES.
387 // Otherwise Chrome notifies the user that this extension is causing issues. 391 // Otherwise Chrome notifies the user that this extension is causing issues.
388 if (handlerBehaviorChangedQuota > 0) 392 if (handlerBehaviorChangedQuota > 0)
389 { 393 {
390 chrome.webNavigation.onBeforeNavigate.removeListener(propagateHandlerBehav iorChange); 394 chrome.webNavigation.onBeforeNavigate.removeListener(
395 propagateHandlerBehaviorChange
Sebastian Noack 2017/02/09 01:04:49 I wonder whether we can perhaps shorten the variab
kzar 2017/02/20 10:27:28 I don't mind the wrapping too much here and I can'
396 );
391 chrome.webRequest.handlerBehaviorChanged(); 397 chrome.webRequest.handlerBehaviorChanged();
392 398
393 handlerBehaviorChangedQuota--; 399 handlerBehaviorChangedQuota--;
394 setTimeout(() => { handlerBehaviorChangedQuota++; }, 600000); 400 setTimeout(() => { handlerBehaviorChangedQuota++; }, 600000);
395 } 401 }
396 } 402 }
397 403
398 ext.webRequest = { 404 ext.webRequest = {
399 onBeforeRequest: new ext._EventTarget(), 405 onBeforeRequest: new ext._EventTarget(),
400 handlerBehaviorChanged() 406 handlerBehaviorChanged()
401 { 407 {
402 // Defer handlerBehaviorChanged() until navigation occurs. 408 // Defer handlerBehaviorChanged() until navigation occurs.
403 // There wouldn't be any visible effect when calling it earlier, 409 // There wouldn't be any visible effect when calling it earlier,
404 // but it's an expensive operation and that way we avoid to call 410 // but it's an expensive operation and that way we avoid to call
405 // it multiple times, if multiple filters are added/removed. 411 // it multiple times, if multiple filters are added/removed.
406 let onBeforeNavigate = chrome.webNavigation.onBeforeNavigate; 412 let {onBeforeNavigate} = chrome.webNavigation;
407 if (!onBeforeNavigate.hasListener(propagateHandlerBehaviorChange)) 413 if (!onBeforeNavigate.hasListener(propagateHandlerBehaviorChange))
408 onBeforeNavigate.addListener(propagateHandlerBehaviorChange); 414 onBeforeNavigate.addListener(propagateHandlerBehaviorChange);
409 } 415 }
410 }; 416 };
411 417
412 chrome.tabs.query({}, tabs => 418 chrome.tabs.query({}, tabs =>
413 { 419 {
414 tabs.forEach(tab => 420 tabs.forEach(tab =>
415 { 421 {
416 chrome.webNavigation.getAllFrames({tabId: tab.id}, details => 422 chrome.webNavigation.getAllFrames({tabId: tab.id}, details =>
417 { 423 {
418 if (details && details.length > 0) 424 if (details && details.length > 0)
419 { 425 {
420 let frames = framesOfTabs[tab.id] = Object.create(null); 426 let frames = framesOfTabs[tab.id] = Object.create(null);
421 427
422 for (let i = 0; i < details.length; i++) 428 for (let i = 0; i < details.length; i++)
423 frames[details[i].frameId] = {url: new URL(details[i].url), parent: null}; 429 {
430 frames[details[i].frameId] = {
431 url: new URL(details[i].url),
432 parent: null
433 };
434 }
424 435
425 for (let i = 0; i < details.length; i++) 436 for (let i = 0; i < details.length; i++)
426 { 437 {
427 let parentFrameId = details[i].parentFrameId; 438 let {parentFrameId} = details[i];
428 439
429 if (parentFrameId != -1) 440 if (parentFrameId != -1)
430 frames[details[i].frameId].parent = frames[parentFrameId]; 441 frames[details[i].frameId].parent = frames[parentFrameId];
431 } 442 }
432 } 443 }
433 }); 444 });
434 }); 445 });
435 }); 446 });
436 447
437 chrome.webRequest.onBeforeRequest.addListener(details => 448 chrome.webRequest.onBeforeRequest.addListener(details =>
438 { 449 {
439 // The high-level code isn't interested in requests that aren't 450 // The high-level code isn't interested in requests that aren't
440 // related to a tab or requests loading a top-level document, 451 // related to a tab or requests loading a top-level document,
441 // those should never be blocked. 452 // those should never be blocked.
442 if (details.tabId == -1 || details.type == "main_frame") 453 if (details.tabId == -1 || details.type == "main_frame")
443 return; 454 return;
444 455
445 // We are looking for the frame that contains the element which 456 // We are looking for the frame that contains the element which
446 // has triggered this request. For most requests (e.g. images) we 457 // has triggered this request. For most requests (e.g. images) we
447 // can just use the request's frame ID, but for subdocument requests 458 // can just use the request's frame ID, but for subdocument requests
448 // (e.g. iframes) we must instead use the request's parent frame ID. 459 // (e.g. iframes) we must instead use the request's parent frame ID.
449 let frameId; 460 let {frameId, type} = details;
450 let requestType; 461 if (type == "sub_frame")
451 if (details.type == "sub_frame")
452 { 462 {
453 frameId = details.parentFrameId; 463 frameId = details.parentFrameId;
454 requestType = "SUBDOCUMENT"; 464 type = "SUBDOCUMENT";
455 }
456 else
457 {
458 frameId = details.frameId;
459 requestType = details.type.toUpperCase();
460 } 465 }
461 466
462 let frame = ext.getFrame(details.tabId, frameId); 467 let frame = ext.getFrame(details.tabId, frameId);
463 if (frame) 468 if (frame)
464 { 469 {
465 let results = ext.webRequest.onBeforeRequest._dispatch( 470 let results = ext.webRequest.onBeforeRequest._dispatch(
466 new URL(details.url), 471 new URL(details.url),
467 requestType, 472 type.toUpperCase(),
468 new Page({id: details.tabId}), 473 new Page({id: details.tabId}),
469 frame 474 frame
470 ); 475 );
471 476
472 if (results.indexOf(false) != -1) 477 if (results.indexOf(false) != -1)
473 return {cancel: true}; 478 return {cancel: true};
474 } 479 }
475 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); 480 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]);
476 481
477 482
(...skipping 19 matching lines...) Expand all
497 502
498 let frame = frames[rawSender.frameId]; 503 let frame = frames[rawSender.frameId];
499 if (frame) 504 if (frame)
500 return frame.parent; 505 return frame.parent;
501 506
502 return frames[0]; 507 return frames[0];
503 } 508 }
504 }; 509 };
505 } 510 }
506 511
507 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true) != -1; 512 let results = ext.onMessage._dispatch(message, sender, sendResponse);
513 return results.indexOf(true) != -1;
Sebastian Noack 2017/02/09 01:04:48 This would be another way to wrap the code here, w
kzar 2017/02/20 10:27:29 I've wrapped it in a similar way which I preferred
508 }); 514 });
509 515
510 516
511 /* Storage */ 517 /* Storage */
512 518
513 ext.storage = { 519 ext.storage = {
514 get(keys, callback) 520 get(keys, callback)
515 { 521 {
516 chrome.storage.local.get(keys, callback); 522 chrome.storage.local.get(keys, callback);
517 }, 523 },
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 ext.windows = { 607 ext.windows = {
602 create(createData, callback) 608 create(createData, callback)
603 { 609 {
604 chrome.windows.create(createData, createdWindow => 610 chrome.windows.create(createData, createdWindow =>
605 { 611 {
606 afterTabLoaded(callback)(createdWindow.tabs[0]); 612 afterTabLoaded(callback)(createdWindow.tabs[0]);
607 }); 613 });
608 } 614 }
609 }; 615 };
610 } 616 }
OLDNEW

Powered by Google App Engine
This is Rietveld