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: Report unmapped types as OTHER Created May 18, 2017, 9:48 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 15 matching lines...) Expand all
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 (WebExtensions) can't distinguish between 32 // Chrome and Firefox (WebExtensions) can't distinguish between
33 // OBJECT_SUBREQUEST and OBJECT 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)
Sebastian Noack 2017/05/19 11:30:46 How about using a mapping like below? let resou
Manish Jethani 2017/05/19 16:54:42 Yes, that's two lookups now instead of one for the
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 let mappedType = resourceTypeMapping.get(type) || type.toUpperCase();
60 if (!RegExpFilter.typeMap[mappedType])
61 mappedType = "OTHER";
62
63 devtools.logRequest( 62 devtools.logRequest(
64 page, url, 63 page, url, type, docDomain,
65 mappedType, docDomain,
66 thirdParty, sitekey, 64 thirdParty, sitekey,
67 specificOnly, filter 65 specificOnly, filter
68 ); 66 );
69 } 67 }
70 } 68 }
71 69
72 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) =>
73 { 71 {
74 if (checkWhitelisted(page, frame)) 72 if (checkWhitelisted(page, frame))
75 return true; 73 return true;
76 74
77 let urlString = stringifyURL(url); 75 let urlString = stringifyURL(url);
78 let docDomain = extractHostFromFrame(frame); 76 let docDomain = extractHostFromFrame(frame);
79 let thirdParty = isThirdParty(url, docDomain); 77 let thirdParty = isThirdParty(url, docDomain);
80 let sitekey = getKey(page, frame); 78 let sitekey = getKey(page, frame);
81 79
82 let specificOnly = !!checkWhitelisted( 80 let specificOnly = !!checkWhitelisted(
83 page, frame, RegExpFilter.typeMap.GENERICBLOCK 81 page, frame, RegExpFilter.typeMap.GENERICBLOCK
84 ); 82 );
85 83
84 let mappedType = resourceTypes.get(type) || "OTHER";
85
86 let filter = defaultMatcher.matchesAny( 86 let filter = defaultMatcher.matchesAny(
87 urlString, typeMasks.get(type), 87 urlString, RegExpFilter.typeMap[mappedType],
88 docDomain, thirdParty, sitekey, specificOnly 88 docDomain, thirdParty, sitekey, specificOnly
89 ); 89 );
90 90
91 setTimeout(onBeforeRequestAsync, 0, page, urlString, 91 setTimeout(onBeforeRequestAsync, 0, page, urlString,
92 type, docDomain, 92 mappedType, docDomain,
93 thirdParty, sitekey, 93 thirdParty, sitekey,
94 specificOnly, filter); 94 specificOnly, filter);
95 95
96 return !(filter instanceof BlockingFilter); 96 return !(filter instanceof BlockingFilter);
97 }); 97 });
98 98
99 port.on("filters.collapse", (message, sender) => 99 port.on("filters.collapse", (message, sender) =>
100 { 100 {
101 if (checkWhitelisted(sender.page, sender.frame)) 101 if (checkWhitelisted(sender.page, sender.frame))
102 return false; 102 return false;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 if (msg.requestType in chrome.webRequest.ResourceType) 182 if (msg.requestType in chrome.webRequest.ResourceType)
183 return false; 183 return false;
184 184
185 return ext.webRequest.onBeforeRequest._dispatch( 185 return ext.webRequest.onBeforeRequest._dispatch(
186 new URL(msg.url), 186 new URL(msg.url),
187 msg.requestType, 187 msg.requestType,
188 sender.page, 188 sender.page,
189 sender.frame 189 sender.frame
190 ).includes(false); 190 ).includes(false);
191 }); 191 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld