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

Side by Side Diff: lib/requestBlocker.js

Issue 29737568: Issue 4580 - Removed ext.webRequest.onBeforeRequest (Closed)
Patch Set: Created March 30, 2018, 11:18 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ext/background.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 if (devtools) 83 if (devtools)
84 { 84 {
85 devtools.logRequest( 85 devtools.logRequest(
86 page, url, type, docDomain, 86 page, url, type, docDomain,
87 thirdParty, sitekey, 87 thirdParty, sitekey,
88 specificOnly, filter 88 specificOnly, filter
89 ); 89 );
90 } 90 }
91 } 91 }
92 92
93 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => 93 browser.webRequest.onBeforeRequest.addListener(details =>
94 { 94 {
95 // Never block top-level documents.
96 if (details.type == "main_frame")
97 return;
98
99 // Filter out requests from non web protocols. Ideally, we'd explicitly
100 // specify the protocols we are interested in (i.e. http://, https://,
101 // ws:// and wss://) with the url patterns, given below, when adding this
102 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
103 // protocol and is causing an error if it is given.
104 let url = new URL(details.url);
105 if (url.protocol != "http:" && url.protocol != "https:" &&
106 url.protocol != "ws:" && url.protocol != "wss:")
107 return;
kzar 2018/04/05 11:26:26 Please could you add the braces here, to be consis
Sebastian Noack 2018/04/05 16:04:09 See below.
108
109 // Firefox (only) allows to intercept requests sent by the browser
110 // and other extensions. We don't want to block these.
111 if (details.originUrl)
112 {
113 let originUrl = new URL(details.originUrl);
114 if (originUrl.protocol == "chrome:" ||
115 originUrl.protocol == "moz-extension:")
116 return;
kzar 2018/04/05 11:26:26 Please can you add the braces back, since the rule
Sebastian Noack 2018/04/05 16:04:09 Since when is this the rule? This is the first tim
Manish Jethani 2018/04/06 14:12:48 As far back as I can remember (which is not too fa
117 }
118
119 let frame = ext.getFrame(
kzar 2018/04/05 11:26:26 Seems like previously we didn't call getFrame if d
Sebastian Noack 2018/04/05 16:04:09 getFrame() just returns undefined if there is not
120 details.tabId,
121 // We are looking for the frame that contains the element which
122 // has triggered this request. For most requests (e.g. images) we
123 // can just use the request's frame ID, but for subdocument requests
124 // (e.g. iframes) we must instead use the request's parent frame ID.
125 details.type == "sub_frame" ? details.parentFrameId : details.frameId
126 );
127
128 let page = null;
95 let docDomain = null; 129 let docDomain = null;
96 let sitekey = null; 130 let sitekey = null;
131 let thirdParty = false;
97 let specificOnly = false; 132 let specificOnly = false;
98 let thirdParty = false;
99 let urlString = stringifyURL(url);
100 133
134 if (frame)
135 {
136 page = new ext.Page({id: details.tabId});
101 137
102 if (frame && page)
103 {
104 if (checkWhitelisted(page, frame)) 138 if (checkWhitelisted(page, frame))
105 return true; 139 return;
106 140
107 docDomain = extractHostFromFrame(frame); 141 docDomain = extractHostFromFrame(frame);
108 sitekey = getKey(page, frame); 142 sitekey = getKey(page, frame);
109 thirdParty = isThirdParty(url, docDomain); 143 thirdParty = isThirdParty(url, docDomain);
110 specificOnly = !!checkWhitelisted(page, frame, 144 specificOnly = !!checkWhitelisted(page, frame,
111 RegExpFilter.typeMap.GENERICBLOCK); 145 RegExpFilter.typeMap.GENERICBLOCK);
112 } 146 }
113 147
114 let mappedType = resourceTypes.get(type) || "OTHER"; 148 let urlString = stringifyURL(url);
115 149 let type = resourceTypes.get(details.type) || "OTHER";
116 let filter = defaultMatcher.matchesAny( 150 let filter = defaultMatcher.matchesAny(
117 urlString, RegExpFilter.typeMap[mappedType], 151 urlString, RegExpFilter.typeMap[type],
118 docDomain, thirdParty, sitekey, specificOnly 152 docDomain, thirdParty, sitekey, specificOnly
119 ); 153 );
120 154
121 setTimeout(onBeforeRequestAsync, 0, page, urlString, 155 setTimeout(onBeforeRequestAsync, 0, page, urlString,
122 mappedType, docDomain, 156 type, docDomain,
123 thirdParty, sitekey, 157 thirdParty, sitekey,
124 specificOnly, filter); 158 specificOnly, filter);
125 159
126 return !(filter instanceof BlockingFilter); 160 if (filter instanceof BlockingFilter)
127 }); 161 return {cancel: true};
162 }, {urls: ["<all_urls>"]}, ["blocking"]);
128 163
129 port.on("filters.collapse", (message, sender) => 164 port.on("filters.collapse", (message, sender) =>
130 { 165 {
131 if (checkWhitelisted(sender.page, sender.frame)) 166 if (checkWhitelisted(sender.page, sender.frame))
132 return false; 167 return false;
133 168
134 let typeMask = RegExpFilter.typeMap[message.mediatype]; 169 let typeMask = RegExpFilter.typeMap[message.mediatype];
135 let documentHost = extractHostFromFrame(sender.frame); 170 let documentHost = extractHostFromFrame(sender.frame);
136 let sitekey = getKey(sender.page, sender.frame); 171 let sitekey = getKey(sender.page, sender.frame);
137 let blocked = false; 172 let blocked = false;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 241
207 port.on("request.blockedByRTCWrapper", (msg, sender) => 242 port.on("request.blockedByRTCWrapper", (msg, sender) =>
208 { 243 {
209 return ext.webRequest.onBeforeRequest._dispatch( 244 return ext.webRequest.onBeforeRequest._dispatch(
210 new URL(msg.url), 245 new URL(msg.url),
211 "webrtc", 246 "webrtc",
212 sender.page, 247 sender.page,
213 sender.frame 248 sender.frame
214 ).includes(false); 249 ).includes(false);
215 }); 250 });
OLDNEW
« no previous file with comments | « ext/background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld