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: Created April 19, 2017, 4: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
(...skipping 10 matching lines...) Expand all
21 { 21 {
22 let nonEmptyPageMaps = 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 }; 27 };
28 PageMap.prototype = { 28 PageMap.prototype = {
29 _delete(id) 29 _delete(id)
30 { 30 {
31 let map = this._map; 31 this._map.delete(id);
32 32
33 let deleted = map.delete(id); 33 if (this._map.size == 0)
34
35 if (deleted && map.size == 0)
36 nonEmptyPageMaps.delete(this); 34 nonEmptyPageMaps.delete(this);
37 }, 35 },
38 keys() 36 keys()
39 { 37 {
40 return [...ext.mapIterable(this._map.keys(), ext.getPage)]; 38 return this._map.keys();
41 }, 39 },
42 get(page) 40 get(page)
43 { 41 {
44 return this._map.get(page.id); 42 return this._map.get(page.id);
45 }, 43 },
46 set(page, value) 44 set(page, value)
47 { 45 {
48 let map = this._map; 46 this._map.set(page.id, value);
49 let prevSize = map.size; 47 nonEmptyPageMaps.add(this);
50
51 map.set(page.id, value);
52
53 if (prevSize == 0)
54 nonEmptyPageMaps.add(this);
55 }, 48 },
56 has(page) 49 has(page)
57 { 50 {
58 return this._map.has(page.id); 51 return this._map.has(page.id);
59 }, 52 },
60 clear() 53 clear()
61 { 54 {
62 let map = this._map; 55 this._map.clear();
Sebastian Noack 2017/04/29 22:46:32 Why do you import this property into a local varia
Manish Jethani 2017/05/04 14:47:33 I wrongly assumed that this would make it faster.
63
64 if (map.size == 0)
65 return;
66
67 map.clear();
68 nonEmptyPageMaps.delete(this); 56 nonEmptyPageMaps.delete(this);
69 }, 57 },
70 delete(page) 58 delete(page)
71 { 59 {
72 this._delete(page.id); 60 this._delete(page.id);
73 } 61 }
74 }; 62 };
75 63
76 ext._removeFromAllPageMaps = pageId => 64 ext._removeFromAllPageMaps = pageId =>
77 { 65 {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 let frames = framesOfTabs.get(tabId); 159 let frames = framesOfTabs.get(tabId);
172 if (!frames) 160 if (!frames)
173 { 161 {
174 frames = new Map(); 162 frames = new Map();
175 framesOfTabs.set(tabId, frames); 163 framesOfTabs.set(tabId, frames);
176 } 164 }
177 165
178 let frame = frames.get(frameId); 166 let frame = frames.get(frameId);
179 if (!frame) 167 if (!frame)
180 { 168 {
181 frame = Object.create(null); 169 frame = {};
Sebastian Noack 2017/04/29 22:46:32 Why do we use an object with no prototype here aga
Manish Jethani 2017/05/04 14:47:33 Again I assumed that an object with no prototype w
182 frames.set(frameId, frame); 170 frames.set(frameId, frame);
183 } 171 }
184 172
185 return frame; 173 return frame;
186 } 174 }
187 175
188 function updatePageFrameStructure(frameId, tabId, url, parentFrameId) 176 function updatePageFrameStructure(frameId, tabId, url, parentFrameId)
189 { 177 {
190 if (frameId == 0) 178 if (frameId == 0)
191 { 179 {
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 }); 495 });
508 496
509 497
510 /* Web requests */ 498 /* Web requests */
511 499
512 let framesOfTabs = new Map(); 500 let framesOfTabs = new Map();
513 501
514 ext.getFrame = (tabId, frameId) => 502 ext.getFrame = (tabId, frameId) =>
515 { 503 {
516 let frames = framesOfTabs.get(tabId); 504 let frames = framesOfTabs.get(tabId);
517 if (frames) 505 return frames && frames.get(frameId);
518 return frames.get(frameId);
Sebastian Noack 2017/04/29 22:46:32 How about: return frames && frames.get(frameId)
Manish Jethani 2017/05/04 14:47:33 Done.
519 }; 506 };
520 507
521 let handlerBehaviorChangedQuota = 508 let handlerBehaviorChangedQuota =
522 chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; 509 chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES;
523 510
524 function propagateHandlerBehaviorChange() 511 function propagateHandlerBehaviorChange()
525 { 512 {
526 // Make sure to not call handlerBehaviorChanged() more often than allowed 513 // Make sure to not call handlerBehaviorChanged() more often than allowed
527 // by chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES. 514 // by chrome.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES.
528 // Otherwise Chrome notifies the user that this extension is causing issues. 515 // Otherwise Chrome notifies the user that this extension is causing issues.
(...skipping 29 matching lines...) Expand all
558 { 545 {
559 chrome.webNavigation.getAllFrames({tabId: tab.id}, details => 546 chrome.webNavigation.getAllFrames({tabId: tab.id}, details =>
560 { 547 {
561 if (details && details.length > 0) 548 if (details && details.length > 0)
562 { 549 {
563 let frames = new Map(); 550 let frames = new Map();
564 framesOfTabs.set(tab.id, frames); 551 framesOfTabs.set(tab.id, frames);
565 552
566 for (let detail of details) 553 for (let detail of details)
567 { 554 {
568 let frame = Object.create(null); 555 let frame = {url: new URL(detail.url)};
569 frame.url = new URL(detail.url);
570
571 frames.set(detail.frameId, frame); 556 frames.set(detail.frameId, frame);
572 } 557
573 558 if (detail.parentFrameId != -1)
574 for (let detail of details) 559 frame.parent = frames.get(detail.parentFrameId);
575 {
576 let {parentFrameId} = detail;
577
578 if (parentFrameId != -1)
579 frames.get(detail.frameId).parent = frames.get(parentFrameId);
580 } 560 }
581 } 561 }
582 }); 562 });
583 }); 563 });
584 }); 564 });
585 565
586 chrome.webRequest.onBeforeRequest.addListener(details => 566 chrome.webRequest.onBeforeRequest.addListener(details =>
587 { 567 {
588 // 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
589 // related to a tab or requests loading a top-level document, 569 // related to a tab or requests loading a top-level document,
590 // those should never be blocked. 570 // those should never be blocked.
591 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:")
592 return; 582 return;
593 583
594 // We are looking for the frame that contains the element which 584 // We are looking for the frame that contains the element which
595 // has triggered this request. For most requests (e.g. images) we 585 // has triggered this request. For most requests (e.g. images) we
596 // 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
597 // (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.
598 let {frameId, type} = details; 588 let {frameId, type} = details;
599 if (type == "sub_frame") 589 if (type == "sub_frame")
600 {
601 frameId = details.parentFrameId; 590 frameId = details.parentFrameId;
602 type = "SUBDOCUMENT";
603 }
604 591
605 let frame = ext.getFrame(details.tabId, frameId); 592 let frame = ext.getFrame(details.tabId, frameId);
606 if (frame) 593 if (frame)
607 { 594 {
608 let results = ext.webRequest.onBeforeRequest._dispatch( 595 let results = ext.webRequest.onBeforeRequest._dispatch(
609 new URL(details.url), 596 url, type, new Page({id: details.tabId}), frame
610 type.toUpperCase(),
611 new Page({id: details.tabId}),
612 frame
613 ); 597 );
614 598
615 if (results.indexOf(false) != -1) 599 if (results.indexOf(false) != -1)
616 return {cancel: true}; 600 return {cancel: true};
617 } 601 }
618 }, {urls: ["<all_urls>"]}, ["blocking"]); 602 }, {urls: ["<all_urls>"]}, ["blocking"]);
619 603
620 604
621 /* Message passing */ 605 /* Message passing */
622 606
(...skipping 10 matching lines...) Expand all
633 url: new URL(rawSender.url), 617 url: new URL(rawSender.url),
634 get parent() 618 get parent()
635 { 619 {
636 let frames = framesOfTabs.get(rawSender.tab.id); 620 let frames = framesOfTabs.get(rawSender.tab.id);
637 621
638 if (!frames) 622 if (!frames)
639 return null; 623 return null;
640 624
641 let frame = frames.get(rawSender.frameId); 625 let frame = frames.get(rawSender.frameId);
642 if (frame) 626 if (frame)
643 return frame.parent || null; 627 return frame.parent || null;
Sebastian Noack 2017/04/29 22:46:32 This seems unrelated.
Manish Jethani 2017/05/04 14:47:33 frame.parent is either an object or undefined. I t
644 628
645 return frames.get(0) || null; 629 return frames.get(0) || null;
646 } 630 }
647 }; 631 };
648 } 632 }
649 633
650 return ext.onMessage._dispatch( 634 return ext.onMessage._dispatch(
651 message, sender, sendResponse 635 message, sender, sendResponse
652 ).indexOf(true) != -1; 636 ).indexOf(true) != -1;
653 }); 637 });
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 ext.windows = { 730 ext.windows = {
747 create(createData, callback) 731 create(createData, callback)
748 { 732 {
749 chrome.windows.create(createData, createdWindow => 733 chrome.windows.create(createData, createdWindow =>
750 { 734 {
751 afterTabLoaded(callback)(createdWindow.tabs[0]); 735 afterTabLoaded(callback)(createdWindow.tabs[0]);
752 }); 736 });
753 } 737 }
754 }; 738 };
755 }()); 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