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

Delta Between Two Patch Sets: ext/background.js

Issue 29417597: Issue 5161 - Use maps and sets where appropriate (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Replace nonEmptyPageMaps with allPageMaps Created May 18, 2017, 10:33 p.m.
Right Patch Set: Minor unrelated changes Created May 21, 2017, 10:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/devtools.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 let allPageMaps = new Set(); 22 let nonEmptyPageMaps = new Set();
23 23
24 let PageMap = ext.PageMap = function() 24 let PageMap = ext.PageMap = function()
25 { 25 {
26 this._map = new Map(); 26 this._map = new Map();
27
28 allPageMaps.add(this);
29 }; 27 };
30 PageMap.prototype = { 28 PageMap.prototype = {
31 _delete(id) 29 _delete(id)
32 { 30 {
33 this._map.delete(id); 31 this._map.delete(id);
32
33 if (this._map.size == 0)
34 nonEmptyPageMaps.delete(this);
34 }, 35 },
35 keys() 36 keys()
36 { 37 {
37 return this._map.keys(); 38 return this._map.keys();
38 }, 39 },
39 get(page) 40 get(page)
40 { 41 {
41 return this._map.get(page.id); 42 return this._map.get(page.id);
42 }, 43 },
43 set(page, value) 44 set(page, value)
44 { 45 {
45 this._map.set(page.id, value); 46 this._map.set(page.id, value);
47 nonEmptyPageMaps.add(this);
46 }, 48 },
47 has(page) 49 has(page)
48 { 50 {
49 return this._map.has(page.id); 51 return this._map.has(page.id);
50 }, 52 },
51 clear() 53 clear()
52 { 54 {
53 this._map.clear(); 55 this._map.clear();
56 nonEmptyPageMaps.delete(this);
54 }, 57 },
55 delete(page) 58 delete(page)
56 { 59 {
57 this._delete(page.id); 60 this._delete(page.id);
58 } 61 }
59 }; 62 };
60 63
61 ext._removeFromAllPageMaps = pageId => 64 ext._removeFromAllPageMaps = pageId =>
62 { 65 {
63 for (let pageMap of allPageMaps) 66 for (let pageMap of nonEmptyPageMaps)
64 pageMap._delete(pageId); 67 pageMap._delete(pageId);
65 }; 68 };
66 69
67 /* Pages */ 70 /* Pages */
68 71
69 let Page = ext.Page = function(tab) 72 let Page = ext.Page = function(tab)
70 { 73 {
71 this.id = tab.id; 74 this.id = tab.id;
72 this._url = tab.url && new URL(tab.url); 75 this._url = tab.url && new URL(tab.url);
73 76
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 tabs.forEach(tab => 544 tabs.forEach(tab =>
542 { 545 {
543 chrome.webNavigation.getAllFrames({tabId: tab.id}, details => 546 chrome.webNavigation.getAllFrames({tabId: tab.id}, details =>
544 { 547 {
545 if (details && details.length > 0) 548 if (details && details.length > 0)
546 { 549 {
547 let frames = new Map(); 550 let frames = new Map();
548 framesOfTabs.set(tab.id, frames); 551 framesOfTabs.set(tab.id, frames);
549 552
550 for (let detail of details) 553 for (let detail of details)
551 frames.set(detail.frameId, {url: new URL(detail.url)});
552
553 for (let detail of details)
554 { 554 {
555 let {parentFrameId} = detail; 555 let frame = {url: new URL(detail.url)};
556 556 frames.set(detail.frameId, frame);
557 if (parentFrameId != -1) 557
558 frames.get(detail.frameId).parent = frames.get(parentFrameId); 558 if (detail.parentFrameId != -1)
559 frame.parent = frames.get(detail.parentFrameId);
559 } 560 }
560 } 561 }
561 }); 562 });
562 }); 563 });
563 }); 564 });
564 565
565 chrome.webRequest.onBeforeRequest.addListener(details => 566 chrome.webRequest.onBeforeRequest.addListener(details =>
566 { 567 {
567 // The high-level code isn't interested in requests that aren't 568 // The high-level code isn't interested in requests that aren't
568 // related to a tab or requests loading a top-level document, 569 // related to a tab or requests loading a top-level document,
569 // those should never be blocked. 570 // those should never be blocked.
570 if (details.tabId == -1 || details.type == "main_frame") 571 if (details.tabId == -1 || details.type == "main_frame")
572 return;
573
574 // Filter out requests from non web protocols. Ideally, we'd explicitly
575 // specify the protocols we are interested in (i.e. http://, https://,
576 // ws:// and wss://) with the url patterns, given below, when adding this
577 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
578 // protocol and is causing an error if it is given.
579 let url = new URL(details.url);
580 if (url.protocol != "http:" && url.protocol != "https:" &&
581 url.protocol != "ws:" && url.protocol != "wss:")
571 return; 582 return;
572 583
573 // We are looking for the frame that contains the element which 584 // We are looking for the frame that contains the element which
574 // has triggered this request. For most requests (e.g. images) we 585 // has triggered this request. For most requests (e.g. images) we
575 // can just use the request's frame ID, but for subdocument requests 586 // can just use the request's frame ID, but for subdocument requests
576 // (e.g. iframes) we must instead use the request's parent frame ID. 587 // (e.g. iframes) we must instead use the request's parent frame ID.
577 let {frameId, type} = details; 588 let {frameId, type} = details;
578 if (type == "sub_frame") 589 if (type == "sub_frame")
579 {
580 frameId = details.parentFrameId; 590 frameId = details.parentFrameId;
581 type = "SUBDOCUMENT";
582 }
583 591
584 let frame = ext.getFrame(details.tabId, frameId); 592 let frame = ext.getFrame(details.tabId, frameId);
585 if (frame) 593 if (frame)
586 { 594 {
587 let results = ext.webRequest.onBeforeRequest._dispatch( 595 let results = ext.webRequest.onBeforeRequest._dispatch(
588 new URL(details.url), 596 url, type, new Page({id: details.tabId}), frame
589 type.toUpperCase(),
590 new Page({id: details.tabId}),
591 frame
592 ); 597 );
593 598
594 if (results.indexOf(false) != -1) 599 if (results.indexOf(false) != -1)
595 return {cancel: true}; 600 return {cancel: true};
596 } 601 }
597 }, {urls: ["<all_urls>"]}, ["blocking"]); 602 }, {urls: ["<all_urls>"]}, ["blocking"]);
598 603
599 604
600 /* Message passing */ 605 /* Message passing */
601 606
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 ext.windows = { 730 ext.windows = {
726 create(createData, callback) 731 create(createData, callback)
727 { 732 {
728 chrome.windows.create(createData, createdWindow => 733 chrome.windows.create(createData, createdWindow =>
729 { 734 {
730 afterTabLoaded(callback)(createdWindow.tabs[0]); 735 afterTabLoaded(callback)(createdWindow.tabs[0]);
731 }); 736 });
732 } 737 }
733 }; 738 };
734 }()); 739 }());
LEFTRIGHT
« no previous file | lib/devtools.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld