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

Delta Between Two Patch Sets: lib/requestBlocker.js

Issue 29760707: Issue 6622 - Implement $rewrite filter option (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: comment fixes Created May 11, 2018, 3:58 p.m.
Right Patch Set: Updated proper revision for adblockpluscore. No dependency change for ui Created May 18, 2018, 3:27 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 | « dependencies ('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-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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 if (!(browser.webRequest.ResourceType)) 63 if (!(browser.webRequest.ResourceType))
64 return; 64 return;
65 65
66 for (let type in browser.webRequest.ResourceType) 66 for (let type in browser.webRequest.ResourceType)
67 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER"; 67 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER";
68 68
69 // WEBRTC gets addressed through a workaround, even if the webRequest API is 69 // WEBRTC gets addressed through a workaround, even if the webRequest API is
70 // lacking support to block this kind of a request. 70 // lacking support to block this kind of a request.
71 yield "WEBRTC"; 71 yield "WEBRTC";
72 72
73 // These filter types aren't mapped to resource types. 73 // POPUP, CSP and ELEMHIDE filters aren't mapped to resource types.
kzar 2018/05/15 14:30:29 Nit: Mind removing this unrelated change?
hub 2018/05/15 16:31:26 Done.
74 yield "POPUP"; 74 yield "POPUP";
75 yield "ELEMHIDE"; 75 yield "ELEMHIDE";
76 yield "CSP"; 76 yield "CSP";
77 }()); 77 }());
78 78
79 function getDocumentInfo(page, frame, originUrl) 79 function getDocumentInfo(page, frame, originUrl)
80 { 80 {
81 return [ 81 return [
82 extractHostFromFrame(frame, originUrl), 82 extractHostFromFrame(frame, originUrl),
83 getKey(page, frame, originUrl), 83 getKey(page, frame, originUrl),
(...skipping 17 matching lines...) Expand all
101 // 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
102 // 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.
103 if (details.tabId != -1) 103 if (details.tabId != -1)
104 return Promise.resolve([details.tabId]); 104 return Promise.resolve([details.tabId]);
105 105
106 let url; // Firefox provides "originUrl" indicating the 106 let url; // Firefox provides "originUrl" indicating the
107 if (details.originUrl) // URL of the tab that caused this request. 107 if (details.originUrl) // URL of the tab that caused this request.
108 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
109 // URL of the tab that caused the worker to spawn. 109 // URL of the tab that caused the worker to spawn.
110 110
111 else if (details.initiator) // Chromium >=63 provides "intiator" which 111 else if (details.initiator && details.initiator != "null")
112 url = details.initiator + "/*"; // is equivalent to "originUrl" on Firefox 112 url = details.initiator + "/*"; // Chromium >=63 provides "intiator" which
113 // is equivalent to "originUrl" on Firefox
113 // except that its not a full URL but just 114 // except that its not a full URL but just
114 // an origin (proto + host). 115 // an origin (proto + host).
115 else 116 else
116 return Promise.resolve([]); 117 return Promise.resolve([]);
117 118
118 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id)); 119 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id));
119 } 120 }
120 121
121 function logRequest(tabIds, request, filter) 122 function logRequest(tabIds, request, filter)
122 { 123 {
(...skipping 15 matching lines...) Expand all
138 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket 139 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
139 // protocol and is causing an error if it is given. 140 // protocol and is causing an error if it is given.
140 let url = new URL(details.url); 141 let url = new URL(details.url);
141 if (url.protocol != "http:" && url.protocol != "https:" && 142 if (url.protocol != "http:" && url.protocol != "https:" &&
142 url.protocol != "ws:" && url.protocol != "wss:") 143 url.protocol != "ws:" && url.protocol != "wss:")
143 return; 144 return;
144 145
145 // Firefox provides us with the full origin URL, while Chromium (>=63) 146 // Firefox provides us with the full origin URL, while Chromium (>=63)
146 // provides only the protocol + host of the (top-level) document which 147 // provides only the protocol + host of the (top-level) document which
147 // the request originates from through the "initiator" property. 148 // the request originates from through the "initiator" property.
148 let originUrl = details.originUrl ? new URL(details.originUrl) : 149 let originUrl = null;
149 details.initiator ? new URL(details.initiator) : null; 150 if (details.originUrl)
151 originUrl = new URL(details.originUrl);
152 else if (details.initiator && details.initiator != "null")
153 originUrl = new URL(details.initiator);
150 154
151 // Ignore requests sent by extensions or by Firefox itself: 155 // Ignore requests sent by extensions or by Firefox itself:
152 // * Firefox intercepts requests sent by any extensions, indicated with 156 // * Firefox intercepts requests sent by any extensions, indicated with
153 // an "originURL" starting with "moz-extension:". 157 // an "originURL" starting with "moz-extension:".
154 // * Chromium intercepts requests sent by this extension only, indicated 158 // * Chromium intercepts requests sent by this extension only, indicated
155 // on Chromium >=63 with an "initiator" starting with "chrome-extension:". 159 // on Chromium >=63 with an "initiator" starting with "chrome-extension:".
156 // * On Firefox, requests that don't relate to any document or extension are 160 // * On Firefox, requests that don't relate to any document or extension are
157 // indicated with an "originUrl" starting with "chrome:". 161 // indicated with an "originUrl" starting with "chrome:".
158 if (originUrl && (originUrl.protocol == extensionProtocol || 162 if (originUrl && (originUrl.protocol == extensionProtocol ||
159 originUrl.protocol == "chrome:")) 163 originUrl.protocol == "chrome:"))
160 return; 164 return;
161 165
162 let page = new ext.Page({id: details.tabId}); 166 let page = new ext.Page({id: details.tabId});
163 let frame = ext.getFrame( 167 let frame = ext.getFrame(
164 details.tabId, 168 details.tabId,
165 // We are looking for the frame that contains the element which 169 // We are looking for the frame that contains the element which
166 // has triggered this request. For most requests (e.g. images) we 170 // has triggered this request. For most requests (e.g. images) we
167 // can just use the request's frame ID, but for subdocument requests 171 // can just use the request's frame ID, but for subdocument requests
168 // (e.g. iframes) we must instead use the request's parent frame ID. 172 // (e.g. iframes) we must instead use the request's parent frame ID.
169 details.type == "sub_frame" ? details.parentFrameId : details.frameId 173 details.type == "sub_frame" ? details.parentFrameId : details.frameId
170 ); 174 );
171 175
172 // On Chromium >= 63, if both the frame is unknown and we haven't get 176 // On Chromium >= 63, if both the frame is unknown and we haven't get
173 // an "initator", this implies a request sent by the browser itself 177 // an "initiator", this implies a request sent by the browser itself
174 // (on older versions of Chromium, due to the lack of "initator", 178 // (on older versions of Chromium, due to the lack of "initiator",
175 // this can also indicate a request sent by a Shared/Service Worker). 179 // this can also indicate a request sent by a Shared/Service Worker).
176 if (!frame && !originUrl) 180 if (!frame && !originUrl)
177 return; 181 return;
178 182
179 if (checkWhitelisted(page, frame, originUrl)) 183 if (checkWhitelisted(page, frame, originUrl))
180 return; 184 return;
181 185
182 let type = resourceTypes.get(details.type) || "OTHER"; 186 let type = resourceTypes.get(details.type) || "OTHER";
183 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame, 187 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame,
184 originUrl); 188 originUrl);
185 let [filter, thirdParty] = matchRequest(url, type, docDomain, 189 let [filter, thirdParty] = matchRequest(url, type, docDomain,
186 sitekey, specificOnly); 190 sitekey, specificOnly);
187 191
188 let result; 192 let result;
189 let rewrittenTo; 193 let rewrittenUrl;
kzar 2018/05/15 14:30:29 Nit: I think rewrittenUrl would be a nicer name.
Manish Jethani 2018/05/15 14:44:23 I'll second that.
Sebastian Noack 2018/05/15 14:44:36 Note that the varialbe name here is consistent wit
hub 2018/05/15 16:31:26 I'll do that.
190 194
191 if (filter instanceof BlockingFilter) 195 if (filter instanceof BlockingFilter)
192 { 196 {
193 if (filter.rewrite) 197 if (filter.rewrite)
194 { 198 {
195 rewrittenTo = filter.rewriteUrl(details.url); 199 rewrittenUrl = filter.rewriteUrl(details.url);
196 // If no rewrite happened (error, different origin), we'll 200 // If no rewrite happened (error, different origin), we'll
197 // return undefined in order to avoid an "infinite" loop. 201 // return undefined in order to avoid an "infinite" loop.
198 if (rewrittenTo != details.url) 202 if (rewrittenUrl != details.url)
199 result = {redirectUrl: rewrittenTo}; 203 result = {redirectUrl: rewrittenUrl};
200 } 204 }
201 else 205 else
202 result = {cancel: true}; 206 result = {cancel: true};
203 } 207 }
204 208
205 getRelatedTabIds(details).then(tabIds => 209 getRelatedTabIds(details).then(tabIds =>
206 { 210 {
207 logRequest( 211 logRequest(
208 tabIds, 212 tabIds,
209 { 213 {
210 url: details.url, type, docDomain, thirdParty, 214 url: details.url, type, docDomain, thirdParty,
211 sitekey, specificOnly, rewrittenTo 215 sitekey, specificOnly, rewrittenUrl
212 }, 216 },
213 filter 217 filter
214 ); 218 );
215 }); 219 });
216 220
217 return result; 221 return result;
218 }, {urls: ["<all_urls>"]}, ["blocking"]); 222 }, {urls: ["<all_urls>"]}, ["blocking"]);
219 223
220 port.on("filters.collapse", (message, sender) => 224 port.on("filters.collapse", (message, sender) =>
221 { 225 {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 } 328 }
325 329
326 FilterNotifier.on("subscription.added", onFilterChange); 330 FilterNotifier.on("subscription.added", onFilterChange);
327 FilterNotifier.on("subscription.removed", onFilterChange); 331 FilterNotifier.on("subscription.removed", onFilterChange);
328 FilterNotifier.on("subscription.updated", onFilterChange); 332 FilterNotifier.on("subscription.updated", onFilterChange);
329 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); 333 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true));
330 FilterNotifier.on("filter.added", onFilterChange); 334 FilterNotifier.on("filter.added", onFilterChange);
331 FilterNotifier.on("filter.removed", onFilterChange); 335 FilterNotifier.on("filter.removed", onFilterChange);
332 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); 336 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true));
333 FilterNotifier.on("load", onFilterChange); 337 FilterNotifier.on("load", onFilterChange);
LEFTRIGHT
« dependencies ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld