 Issue 29417597:
  Issue 5161 - Use maps and sets where appropriate  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/
    
  
    Issue 29417597:
  Issue 5161 - Use maps and sets where appropriate  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 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 Loading... | |
| 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 if (this._map.delete(id) && this._map.size == 0) | 31 this._map.delete(id); | 
| 32 | |
| 33 if (this._map.size == 0) | |
| 32 nonEmptyPageMaps.delete(this); | 34 nonEmptyPageMaps.delete(this); | 
| 33 }, | 35 }, | 
| 34 keys() | 36 keys() | 
| 35 { | 37 { | 
| 36 return this._map.keys(); | 38 return this._map.keys(); | 
| 37 }, | 39 }, | 
| 38 get(page) | 40 get(page) | 
| 39 { | 41 { | 
| 40 return this._map.get(page.id); | 42 return this._map.get(page.id); | 
| 41 }, | 43 }, | 
| 42 set(page, value) | 44 set(page, value) | 
| 43 { | 45 { | 
| 44 let prevSize = this._map.size; | |
| 45 | |
| 46 this._map.set(page.id, value); | 46 this._map.set(page.id, value); | 
| 47 | 47 nonEmptyPageMaps.add(this); | 
| 48 if (prevSize == 0) | |
| 
Sebastian Noack
2017/05/17 06:46:48
Why not just calling nonEmptyPageMaps.add() no mat
 
Manish Jethani
2017/05/17 21:53:24
Well, add could be an expensive operation, dependi
 
Sebastian Noack
2017/05/18 12:23:00
It seems there will be up to 7 PageMap objects at
 
Manish Jethani
2017/05/18 21:16:37
Fair enough.
 
Sebastian Noack
2017/05/18 21:42:12
That is basically what I meant to suggest. However
 
Manish Jethani
2017/05/18 22:39:09
I was hoping to get rid of all the methods entirel
 
Sebastian Noack
2017/05/19 12:01:14
Hmm, perhaps we should have stayed with the nonEmp
 
Manish Jethani
2017/05/19 16:20:34
Yeah, I wasn't too excited about this change eithe
 
Sebastian Noack
2017/05/19 17:33:14
That'd be awesome. :)
 | |
| 49 nonEmptyPageMaps.add(this); | |
| 50 }, | 48 }, | 
| 51 has(page) | 49 has(page) | 
| 52 { | 50 { | 
| 53 return this._map.has(page.id); | 51 return this._map.has(page.id); | 
| 54 }, | 52 }, | 
| 55 clear() | 53 clear() | 
| 56 { | 54 { | 
| 57 if (this._map.size == 0) | |
| 58 return; | |
| 59 | |
| 60 this._map.clear(); | 55 this._map.clear(); | 
| 61 nonEmptyPageMaps.delete(this); | 56 nonEmptyPageMaps.delete(this); | 
| 62 }, | 57 }, | 
| 63 delete(page) | 58 delete(page) | 
| 64 { | 59 { | 
| 65 this._delete(page.id); | 60 this._delete(page.id); | 
| 66 } | 61 } | 
| 67 }; | 62 }; | 
| 68 | 63 | 
| 69 ext._removeFromAllPageMaps = pageId => | 64 ext._removeFromAllPageMaps = pageId => | 
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 tabs.forEach(tab => | 544 tabs.forEach(tab => | 
| 550 { | 545 { | 
| 551 chrome.webNavigation.getAllFrames({tabId: tab.id}, details => | 546 chrome.webNavigation.getAllFrames({tabId: tab.id}, details => | 
| 552 { | 547 { | 
| 553 if (details && details.length > 0) | 548 if (details && details.length > 0) | 
| 554 { | 549 { | 
| 555 let frames = new Map(); | 550 let frames = new Map(); | 
| 556 framesOfTabs.set(tab.id, frames); | 551 framesOfTabs.set(tab.id, frames); | 
| 557 | 552 | 
| 558 for (let detail of details) | 553 for (let detail of details) | 
| 559 frames.set(detail.frameId, {url: new URL(detail.url)}); | |
| 560 | |
| 561 for (let detail of details) | |
| 562 { | 554 { | 
| 563 let {parentFrameId} = detail; | 555 let frame = {url: new URL(detail.url)}; | 
| 564 | 556 frames.set(detail.frameId, frame); | 
| 565 if (parentFrameId != -1) | 557 | 
| 566 frames.get(detail.frameId).parent = frames.get(parentFrameId); | 558 if (detail.parentFrameId != -1) | 
| 559 frame.parent = frames.get(detail.parentFrameId); | |
| 567 } | 560 } | 
| 568 } | 561 } | 
| 569 }); | 562 }); | 
| 570 }); | 563 }); | 
| 571 }); | 564 }); | 
| 572 | 565 | 
| 573 chrome.webRequest.onBeforeRequest.addListener(details => | 566 chrome.webRequest.onBeforeRequest.addListener(details => | 
| 574 { | 567 { | 
| 575 // 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 | 
| 576 // related to a tab or requests loading a top-level document, | 569 // related to a tab or requests loading a top-level document, | 
| 577 // those should never be blocked. | 570 // those should never be blocked. | 
| 578 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:") | |
| 579 return; | 582 return; | 
| 580 | 583 | 
| 581 // We are looking for the frame that contains the element which | 584 // We are looking for the frame that contains the element which | 
| 582 // has triggered this request. For most requests (e.g. images) we | 585 // has triggered this request. For most requests (e.g. images) we | 
| 583 // 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 | 
| 584 // (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. | 
| 585 let {frameId, type} = details; | 588 let {frameId, type} = details; | 
| 586 if (type == "sub_frame") | 589 if (type == "sub_frame") | 
| 587 { | |
| 588 frameId = details.parentFrameId; | 590 frameId = details.parentFrameId; | 
| 589 type = "SUBDOCUMENT"; | |
| 590 } | |
| 591 | 591 | 
| 592 let frame = ext.getFrame(details.tabId, frameId); | 592 let frame = ext.getFrame(details.tabId, frameId); | 
| 593 if (frame) | 593 if (frame) | 
| 594 { | 594 { | 
| 595 let results = ext.webRequest.onBeforeRequest._dispatch( | 595 let results = ext.webRequest.onBeforeRequest._dispatch( | 
| 596 new URL(details.url), | 596 url, type, new Page({id: details.tabId}), frame | 
| 597 type.toUpperCase(), | |
| 598 new Page({id: details.tabId}), | |
| 599 frame | |
| 600 ); | 597 ); | 
| 601 | 598 | 
| 602 if (results.indexOf(false) != -1) | 599 if (results.indexOf(false) != -1) | 
| 603 return {cancel: true}; | 600 return {cancel: true}; | 
| 604 } | 601 } | 
| 605 }, {urls: ["<all_urls>"]}, ["blocking"]); | 602 }, {urls: ["<all_urls>"]}, ["blocking"]); | 
| 606 | 603 | 
| 607 | 604 | 
| 608 /* Message passing */ | 605 /* Message passing */ | 
| 609 | 606 | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 620 url: new URL(rawSender.url), | 617 url: new URL(rawSender.url), | 
| 621 get parent() | 618 get parent() | 
| 622 { | 619 { | 
| 623 let frames = framesOfTabs.get(rawSender.tab.id); | 620 let frames = framesOfTabs.get(rawSender.tab.id); | 
| 624 | 621 | 
| 625 if (!frames) | 622 if (!frames) | 
| 626 return null; | 623 return null; | 
| 627 | 624 | 
| 628 let frame = frames.get(rawSender.frameId); | 625 let frame = frames.get(rawSender.frameId); | 
| 629 if (frame) | 626 if (frame) | 
| 630 return frame.parent || null; | 627 return frame.parent || null; | 
| 
Sebastian Noack
2017/05/17 06:46:48
Any particular reason you force the return value t
 
Manish Jethani
2017/05/17 21:53:24
Just for consistency, nothing else.
 | |
| 631 | 628 | 
| 632 return frames.get(0) || null; | 629 return frames.get(0) || null; | 
| 633 } | 630 } | 
| 634 }; | 631 }; | 
| 635 } | 632 } | 
| 636 | 633 | 
| 637 return ext.onMessage._dispatch( | 634 return ext.onMessage._dispatch( | 
| 638 message, sender, sendResponse | 635 message, sender, sendResponse | 
| 639 ).indexOf(true) != -1; | 636 ).indexOf(true) != -1; | 
| 640 }); | 637 }); | 
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 733 ext.windows = { | 730 ext.windows = { | 
| 734 create(createData, callback) | 731 create(createData, callback) | 
| 735 { | 732 { | 
| 736 chrome.windows.create(createData, createdWindow => | 733 chrome.windows.create(createData, createdWindow => | 
| 737 { | 734 { | 
| 738 afterTabLoaded(callback)(createdWindow.tabs[0]); | 735 afterTabLoaded(callback)(createdWindow.tabs[0]); | 
| 739 }); | 736 }); | 
| 740 } | 737 } | 
| 741 }; | 738 }; | 
| 742 }()); | 739 }()); | 
| LEFT | RIGHT |