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

Delta Between Two Patch Sets: lib/requestBlocker.js

Issue 29705719: Issue 6402 - Split filter hit / request logging out into own API (Closed)
Left Patch Set: Rebased Created April 30, 2018, 1:01 p.m.
Right Patch Set: Addressed Manish's feedback Created May 9, 2018, 5:53 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 | « lib/popupBlocker.js ('k') | lib/whitelisting.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-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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module requestBlocker */ 18 /** @module requestBlocker */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {Filter, RegExpFilter, BlockingFilter} = 22 const {Filter, RegExpFilter, BlockingFilter} =
23 require("../adblockpluscore/lib/filterClasses"); 23 require("../adblockpluscore/lib/filterClasses");
24 const {Subscription} = require("../adblockpluscore/lib/subscriptionClasses"); 24 const {Subscription} = require("../adblockpluscore/lib/subscriptionClasses");
25 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); 25 const {defaultMatcher} = require("../adblockpluscore/lib/matcher");
26 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 26 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
27 const {Prefs} = require("./prefs"); 27 const {Prefs} = require("./prefs");
28 const {checkWhitelisted, getKey} = require("./whitelisting"); 28 const {checkWhitelisted, getKey} = require("./whitelisting");
29 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("./url"); 29 const {extractHostFromFrame, isThirdParty} = require("./url");
30 const {port} = require("./messaging"); 30 const {port} = require("./messaging");
31 const {logRequest: hitLoggerLogRequest} = require("./hitLogger"); 31 const {logRequest: hitLoggerLogRequest} = require("./hitLogger");
32 32
33 const extensionProtocol = new URL(browser.extension.getURL("")).protocol; 33 const extensionProtocol = new URL(browser.extension.getURL("")).protocol;
34 34
35 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. 35 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests.
36 if (!browser.webRequest.ResourceType || 36 if (!browser.webRequest.ResourceType ||
37 !("OBJECT_SUBREQUEST" in browser.webRequest.ResourceType)) 37 !("OBJECT_SUBREQUEST" in browser.webRequest.ResourceType))
38 { 38 {
39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 return [ 81 return [
82 extractHostFromFrame(frame, originUrl), 82 extractHostFromFrame(frame, originUrl),
83 getKey(page, frame, originUrl), 83 getKey(page, frame, originUrl),
84 !!checkWhitelisted(page, frame, originUrl, 84 !!checkWhitelisted(page, frame, originUrl,
85 RegExpFilter.typeMap.GENERICBLOCK) 85 RegExpFilter.typeMap.GENERICBLOCK)
86 ]; 86 ];
87 } 87 }
88 88
89 function matchRequest(url, type, docDomain, sitekey, specificOnly) 89 function matchRequest(url, type, docDomain, sitekey, specificOnly)
90 { 90 {
91 let urlString = stringifyURL(url);
92 let thirdParty = isThirdParty(url, docDomain); 91 let thirdParty = isThirdParty(url, docDomain);
93 92 let filter = defaultMatcher.matchesAny(url.href, RegExpFilter.typeMap[type],
94 return [ 93 docDomain, thirdParty,
95 defaultMatcher.matchesAny(urlString, RegExpFilter.typeMap[type], 94 sitekey, specificOnly);
96 docDomain, thirdParty, sitekey, specificOnly), 95 return [filter, thirdParty];
97 urlString,
98 thirdParty
99 ];
100 } 96 }
101 97
102 function getRelatedTabIds(details) 98 function getRelatedTabIds(details)
103 { 99 {
104 // This is the common case, the request is associated with a single tab. 100 // This is the common case, the request is associated with a single tab.
105 // If tabId is -1, its not (e.g. the request was sent by 101 // If tabId is -1, its not (e.g. the request was sent by
106 // a Service/Shared Worker) and we have to identify the related tabs. 102 // a Service/Shared Worker) and we have to identify the related tabs.
107 if (details.tabId != -1) 103 if (details.tabId != -1)
108 return Promise.resolve([details.tabId]); 104 return Promise.resolve([details.tabId]);
109 105
110 let url; // Firefox provides "originUrl" indicating the 106 let url; // Firefox provides "originUrl" indicating the
111 if (details.originUrl) // URL of the tab that caused this request. 107 if (details.originUrl) // URL of the tab that caused this request.
112 url = details.originUrl; // In case of Service/Shared Worker, this is the 108 url = details.originUrl; // In case of Service/Shared Worker, this is the
113 // URL of the tab that caused the worker to spawn. 109 // URL of the tab that caused the worker to spawn.
114 110
115 else if (details.initiator) // Chromium >=63 provides "intiator" which 111 else if (details.initiator) // Chromium >=63 provides "intiator" which
116 url = details.initiator + "/*"; // is equivalent to "originUrl" on Firefox 112 url = details.initiator + "/*"; // is equivalent to "originUrl" on Firefox
117 // except that its not a full URL but just 113 // except that its not a full URL but just
118 // an origin (proto + host). 114 // an origin (proto + host).
119 else 115 else
120 return Promise.resolve([]); 116 return Promise.resolve([]);
121 117
122 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id)); 118 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id));
123 } 119 }
124 120
125 function logRequest(tabIds, url, type, docDomain, thirdParty, 121 function logRequest(tabIds, request, filter)
126 sitekey, specificOnly, filter)
127 { 122 {
128 if (filter) 123 if (filter)
129 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds); 124 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
130 125
131 hitLoggerLogRequest( 126 hitLoggerLogRequest(tabIds, request, filter);
132 tabIds, url, type, docDomain,
133 thirdParty, sitekey,
134 specificOnly, filter
135 );
136 } 127 }
137 128
138 browser.webRequest.onBeforeRequest.addListener(details => 129 browser.webRequest.onBeforeRequest.addListener(details =>
139 { 130 {
140 // Never block top-level documents. 131 // Never block top-level documents.
141 if (details.type == "main_frame") 132 if (details.type == "main_frame")
142 return; 133 return;
143 134
144 // Filter out requests from non web protocols. Ideally, we'd explicitly 135 // Filter out requests from non web protocols. Ideally, we'd explicitly
145 // specify the protocols we are interested in (i.e. http://, https://, 136 // specify the protocols we are interested in (i.e. http://, https://,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // this can also indicate a request sent by a Shared/Service Worker). 175 // this can also indicate a request sent by a Shared/Service Worker).
185 if (!frame && !originUrl) 176 if (!frame && !originUrl)
186 return; 177 return;
187 178
188 if (checkWhitelisted(page, frame, originUrl)) 179 if (checkWhitelisted(page, frame, originUrl))
189 return; 180 return;
190 181
191 let type = resourceTypes.get(details.type) || "OTHER"; 182 let type = resourceTypes.get(details.type) || "OTHER";
192 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame, 183 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame,
193 originUrl); 184 originUrl);
194 let [filter, urlString, thirdParty] = matchRequest(url, type, docDomain, 185 let [filter, thirdParty] = matchRequest(url, type, docDomain,
195 sitekey, specificOnly); 186 sitekey, specificOnly);
196 187
197 getRelatedTabIds(details).then(tabIds => 188 getRelatedTabIds(details).then(tabIds =>
198 { 189 {
199 logRequest(tabIds, urlString, type, docDomain, 190 logRequest(
200 thirdParty, sitekey, specificOnly, filter); 191 tabIds,
192 {url: details.url, type, docDomain, thirdParty, sitekey, specificOnly},
193 filter
194 );
201 }); 195 });
202 196
203 if (filter instanceof BlockingFilter) 197 if (filter instanceof BlockingFilter)
204 return {cancel: true}; 198 return {cancel: true};
205 }, {urls: ["<all_urls>"]}, ["blocking"]); 199 }, {urls: ["<all_urls>"]}, ["blocking"]);
206 200
207 port.on("filters.collapse", (message, sender) => 201 port.on("filters.collapse", (message, sender) =>
208 { 202 {
209 let {page, frame} = sender; 203 let {page, frame} = sender;
210 204
(...skipping 20 matching lines...) Expand all
231 return blocked && Prefs.hidePlaceholders; 225 return blocked && Prefs.hidePlaceholders;
232 }); 226 });
233 227
234 port.on("request.blockedByRTCWrapper", (msg, sender) => 228 port.on("request.blockedByRTCWrapper", (msg, sender) =>
235 { 229 {
236 let {page, frame} = sender; 230 let {page, frame} = sender;
237 231
238 if (checkWhitelisted(page, frame)) 232 if (checkWhitelisted(page, frame))
239 return false; 233 return false;
240 234
235 let {url} = msg;
241 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); 236 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame);
242 let [filter, url, thirdParty] = matchRequest(new URL(msg.url), 237 let [filter, thirdParty] = matchRequest(new URL(url), "WEBRTC", docDomain,
243 "WEBRTC", docDomain, 238 sitekey, specificOnly);
244 sitekey, specificOnly); 239 logRequest(
245 240 [sender.page.id],
246 logRequest([sender.page.id], url, "WEBRTC", docDomain, 241 {url, type: "WEBRTC", docDomain, thirdParty, sitekey, specificOnly},
247 thirdParty, sitekey, specificOnly, filter); 242 filter
243 );
248 244
249 return filter instanceof BlockingFilter; 245 return filter instanceof BlockingFilter;
250 }); 246 });
251 247
252 let ignoreFilterNotifications = false; 248 let ignoreFilterNotifications = false;
253 let handlerBehaviorChangedQuota = 249 let handlerBehaviorChangedQuota =
254 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; 250 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES;
255 251
256 function propagateHandlerBehaviorChange() 252 function propagateHandlerBehaviorChange()
257 { 253 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 } 305 }
310 306
311 FilterNotifier.on("subscription.added", onFilterChange); 307 FilterNotifier.on("subscription.added", onFilterChange);
312 FilterNotifier.on("subscription.removed", onFilterChange); 308 FilterNotifier.on("subscription.removed", onFilterChange);
313 FilterNotifier.on("subscription.updated", onFilterChange); 309 FilterNotifier.on("subscription.updated", onFilterChange);
314 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); 310 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true));
315 FilterNotifier.on("filter.added", onFilterChange); 311 FilterNotifier.on("filter.added", onFilterChange);
316 FilterNotifier.on("filter.removed", onFilterChange); 312 FilterNotifier.on("filter.removed", onFilterChange);
317 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); 313 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true));
318 FilterNotifier.on("load", onFilterChange); 314 FilterNotifier.on("load", onFilterChange);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld