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

Delta Between Two Patch Sets: lib/requestBlocker.js

Issue 29421712: Issue 5184 - Support Firefox-specific webRequest types (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Address comments to Patch Set 2 Created May 18, 2017, 8:46 p.m.
Right Patch Set: Use for..in Created May 20, 2017, 6:54 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 | « ext/background.js ('k') | no next file » | 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 11 matching lines...) Expand all
22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses"); 22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses");
23 const {Subscription} = require("subscriptionClasses"); 23 const {Subscription} = require("subscriptionClasses");
24 const {defaultMatcher} = require("matcher"); 24 const {defaultMatcher} = require("matcher");
25 const {FilterNotifier} = require("filterNotifier"); 25 const {FilterNotifier} = require("filterNotifier");
26 const {Prefs} = require("prefs"); 26 const {Prefs} = require("prefs");
27 const {checkWhitelisted, getKey} = require("whitelisting"); 27 const {checkWhitelisted, getKey} = require("whitelisting");
28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); 28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url");
29 const {port} = require("messaging"); 29 const {port} = require("messaging");
30 const devtools = require("devtools"); 30 const devtools = require("devtools");
31 31
32 // Chrome and Firefox can't distinguish between OBJECT_SUBREQUEST and OBJECT 32 // Chrome and Firefox (WebExtensions) can't distinguish between
Sebastian Noack 2017/05/18 21:33:41 Nit: Thanks for updating the comment, but it's sti
Manish Jethani 2017/05/18 21:49:34 Done.
33 // requests. 33 // OBJECT_SUBREQUEST and OBJECT requests.
34 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 34 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
35 35
36 let resourceTypeMapping = new Map([ 36 // Map of content types reported by the browser to the respecitve content types
37 ["beacon", "PING"], 37 // used by Adblock Plus. Other content types are simply mapped to OTHER.
38 ["imageset", "IMAGE"], 38 let resourceTypes = new Map(function*()
39 ["sub_frame", "SUBDOCUMENT"] 39 {
40 ]); 40 for (let type in RegExpFilter.typeMap)
41 yield [type.toLowerCase(), type];
41 42
42 let typeMasks = new Map( 43 yield ["sub_frame", "SUBDOCUMENT"];
43 Object.keys(chrome.webRequest.ResourceType) 44
44 .map(typeKey => chrome.webRequest.ResourceType[typeKey]) 45 // Treat navigator.sendBeacon() the same as <a ping>, it's essentially the
45 .map(type => [type, RegExpFilter.typeMap[resourceTypeMapping.get(type)] || 46 // same concept - merely generalized.
46 RegExpFilter.typeMap[type.toUpperCase()] || 47 yield ["beacon", "PING"];
47 RegExpFilter.typeMap.OTHER]) 48
48 ); 49 // Treat <img srcset> and <picture> the same as other images.
50 yield ["imageset", "IMAGE"];
51 }());
49 52
50 function onBeforeRequestAsync(page, url, type, docDomain, 53 function onBeforeRequestAsync(page, url, type, docDomain,
51 thirdParty, sitekey, 54 thirdParty, sitekey,
52 specificOnly, filter) 55 specificOnly, filter)
53 { 56 {
54 if (filter) 57 if (filter)
55 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); 58 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
56 59
57 if (devtools) 60 if (devtools)
58 { 61 {
59 devtools.logRequest( 62 devtools.logRequest(
60 page, url, 63 page, url, type, docDomain,
61 resourceTypeMapping.get(type) || type.toUpperCase(), docDomain,
Sebastian Noack 2017/05/18 21:33:42 In case of the resource types we handle as OTHER,
Manish Jethani 2017/05/18 21:49:34 Oops, somehow I missed this. Fixed.
62 thirdParty, sitekey, 64 thirdParty, sitekey,
63 specificOnly, filter 65 specificOnly, filter
64 ); 66 );
65 } 67 }
66 } 68 }
67 69
68 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) =>
69 { 71 {
70 if (checkWhitelisted(page, frame)) 72 if (checkWhitelisted(page, frame))
71 return true; 73 return true;
72 74
73 let urlString = stringifyURL(url); 75 let urlString = stringifyURL(url);
74 let docDomain = extractHostFromFrame(frame); 76 let docDomain = extractHostFromFrame(frame);
75 let thirdParty = isThirdParty(url, docDomain); 77 let thirdParty = isThirdParty(url, docDomain);
76 let sitekey = getKey(page, frame); 78 let sitekey = getKey(page, frame);
77 79
78 let specificOnly = !!checkWhitelisted( 80 let specificOnly = !!checkWhitelisted(
79 page, frame, RegExpFilter.typeMap.GENERICBLOCK 81 page, frame, RegExpFilter.typeMap.GENERICBLOCK
80 ); 82 );
81 83
84 let mappedType = resourceTypes.get(type) || "OTHER";
85
82 let filter = defaultMatcher.matchesAny( 86 let filter = defaultMatcher.matchesAny(
83 urlString, typeMasks.get(type), 87 urlString, RegExpFilter.typeMap[mappedType],
84 docDomain, thirdParty, sitekey, specificOnly 88 docDomain, thirdParty, sitekey, specificOnly
85 ); 89 );
86 90
87 setTimeout(onBeforeRequestAsync, 0, page, urlString, 91 setTimeout(onBeforeRequestAsync, 0, page, urlString,
88 type, docDomain, 92 mappedType, docDomain,
89 thirdParty, sitekey, 93 thirdParty, sitekey,
90 specificOnly, filter); 94 specificOnly, filter);
91 95
92 return !(filter instanceof BlockingFilter); 96 return !(filter instanceof BlockingFilter);
93 }); 97 });
94 98
95 port.on("filters.collapse", (message, sender) => 99 port.on("filters.collapse", (message, sender) =>
96 { 100 {
97 if (checkWhitelisted(sender.page, sender.frame)) 101 if (checkWhitelisted(sender.page, sender.frame))
98 return false; 102 return false;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 if (msg.requestType in chrome.webRequest.ResourceType) 182 if (msg.requestType in chrome.webRequest.ResourceType)
179 return false; 183 return false;
180 184
181 return ext.webRequest.onBeforeRequest._dispatch( 185 return ext.webRequest.onBeforeRequest._dispatch(
182 new URL(msg.url), 186 new URL(msg.url),
183 msg.requestType, 187 msg.requestType,
184 sender.page, 188 sender.page,
185 sender.frame 189 sender.frame
186 ).includes(false); 190 ).includes(false);
187 }); 191 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld