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

Delta Between Two Patch Sets: lib/devtools.js

Issue 6393086494113792: Issue 154 - Added devtools panel showing blocked and blockable items (Closed)
Left Patch Set: Rebased and refactored Created Jan. 28, 2016, 8:19 p.m.
Right Patch Set: Adapt for UI changes generating domain specific filters when necessary Created Feb. 3, 2016, 10:40 a.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 | « include.preload.js ('k') | lib/filterComposer.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 "use strict"; 18 "use strict";
19 19
20 let {WhitelistFilter, ElemHideFilter} = require("filterClasses"); 20 let {RegExpFilter, WhitelistFilter, ElemHideFilter} = require("filterClasses");
21 let {SpecialSubscription} = require("subscriptionClasses"); 21 let {SpecialSubscription} = require("subscriptionClasses");
22 let {FilterStorage} = require("filterStorage"); 22 let {FilterStorage} = require("filterStorage");
23 let {defaultMatcher} = require("matcher"); 23 let {defaultMatcher} = require("matcher");
24 let {FilterNotifier} = require("filterNotifier"); 24 let {FilterNotifier} = require("filterNotifier");
25 25
26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"];
27
26 // Mapping of inspected tabs to their devpanel page 28 // Mapping of inspected tabs to their devpanel page
27 // and recorded items. We can't use a PageMap here, 29 // and recorded items. We can't use a PageMap here,
28 // because data must persist after navigation/reload. 30 // because data must persist after navigation/reload.
29 let panels = Object.create(null); 31 let panels = Object.create(null);
30 32
31 function hasPanels() 33 function hasPanels()
32 { 34 {
33 return Object.getOwnPropertyNames(panels).length > 0; 35 return Object.keys(panels).length > 0;
34 } 36 }
35 37
36 function getRequestInfo(request) 38 function getActivePanel(page)
37 { 39 {
38 return { 40 let panel = panels[page._id];
39 url: request.url, 41 if(panel && !panel.reload && !panel.reloading)
40 type: request.type, 42 return panel;
41 docDomain: request.docDomain 43 return null;
42 };
43 } 44 }
44 45
45 function getFilterInfo(filter) 46 function getFilterInfo(filter)
46 { 47 {
47 if (!filter) 48 if (!filter)
48 return null; 49 return null;
49 50
50 let userDefined = false; 51 let userDefined = false;
51 let subscriptionTitle = null; 52 let subscriptionTitle = null;
52 53
(...skipping 12 matching lines...) Expand all
65 text: filter.text, 66 text: filter.text,
66 whitelisted: filter instanceof WhitelistFilter, 67 whitelisted: filter instanceof WhitelistFilter,
67 userDefined: userDefined, 68 userDefined: userDefined,
68 subscription: subscriptionTitle 69 subscription: subscriptionTitle
69 }; 70 };
70 } 71 }
71 72
72 function hasRecord(panel, request, filter) 73 function hasRecord(panel, request, filter)
73 { 74 {
74 return panel.records.some(record => 75 return panel.records.some(record =>
75 record.request.type == request.type &&
76 record.request.url == request.url && 76 record.request.url == request.url &&
77 record.request.docDomain == request.docDomain && 77 record.request.docDomain == request.docDomain &&
78 78
79 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already
80 // a DOCUMENT exception which disables all means of blocking.
81 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) ! = -1
82 : record.request.type == request.type) &&
83
79 // Matched element hiding filters don't relate to a particular request, 84 // Matched element hiding filters don't relate to a particular request,
80 // so we also have to match the CSS selector in order to distinguish them. 85 // so we also have to match the CSS selector in order to distinguish them.
81 (request.type != "ELEMHIDE" || record.filter.selector == filter.selector) 86 (record.filter && record.filter.selector) == (filter && filter.selector)
82 ); 87 );
83 } 88 }
84 89
85 function addRecord(panel, request, filter) 90 function addRecord(panel, request, filter)
86 { 91 {
87 if (!hasRecord(panel, request, filter)) 92 if (!hasRecord(panel, request, filter))
88 { 93 {
89 panel.panel.sendMessage({ 94 panel.port.postMessage({
90 type: "add-record", 95 type: "add-record",
91 request: getRequestInfo(request), 96 request: request,
92 filter: getFilterInfo(filter) 97 filter: getFilterInfo(filter)
93 }); 98 });
94 99
95 panel.records.push({ 100 panel.records.push({
96 request: request, 101 request: request,
97 filter: filter 102 filter: filter
98 }); 103 });
99 } 104 }
100 } 105 }
101 106
102 function matchRequest(request) 107 function matchRequest(request)
103 { 108 {
104 return defaultMatcher.matchesAny( 109 return defaultMatcher.matchesAny(
105 request.url, 110 request.url,
106 request.type, 111 RegExpFilter.typeMap[request.type],
107 request.docDomain, 112 request.docDomain,
108 request.thirdParty, 113 request.thirdParty,
109 request.sitekey, 114 request.sitekey,
110 request.specificOnly 115 request.specificOnly
111 ); 116 );
112 } 117 }
113 118
114 /** 119 /**
115 * Logs a request in the devtools panel. 120 * Logs a request to the devtools panel.
116 * 121 *
117 * @param {Page} page The page the request occured on 122 * @param {Page} page The page the request occured on
118 * @param {string] url The URL of the request 123 * @param {string} url The URL of the request
119 * @param {string} type The request type 124 * @param {string} type The request type
120 * @param {string} docDomain The IDN-decoded hostname of the document 125 * @param {string} docDomain The IDN-decoded hostname of the document
121 * @param {boolean} thirdParty Whether the origin of the request and documen t differs 126 * @param {boolean} thirdParty Whether the origin of the request and documen t differs
122 * @param {string} [sitekey] The active sitekey if there is any 127 * @param {?string} sitekey The active sitekey if there is any
123 * @param {boolean} [specificOnly] Whether generic filters should be ignored 128 * @param {?boolean} specificOnly Whether generic filters should be ignored
124 * @param {filter} [filter] The matched filter or null if there is no mat ch 129 * @param {?BlockingFilter} filter The matched filter or null if there is no mat ch
125 */ 130 */
126 exports.logRequest = function(page, url, type, docDomain, thirdParty, sitekey, s pecificOnly, filter) 131 exports.logRequest = function(page, url, type, docDomain,
127 { 132 thirdParty, sitekey,
128 let panel = panels[page._id]; 133 specificOnly, filter)
129 if (panel && !panel.reload && !panel.reloading) 134 {
135 let panel = getActivePanel(page);
136 if (panel)
130 { 137 {
131 let request = { 138 let request = {
132 url: url, 139 url: url,
133 type: type, 140 type: type,
134 docDomain: docDomain, 141 docDomain: docDomain,
135 thirdParty: thirdParty, 142 thirdParty: thirdParty,
136 sitekey: sitekey, 143 sitekey: sitekey,
137 specificOnly: specificOnly 144 specificOnly: specificOnly
138 }; 145 };
139 146
140 addRecord(panel, request, filter); 147 addRecord(panel, request, filter);
141 } 148 }
142 }; 149 };
143 150
144 /** 151 /**
145 * Logs active element hiding filters in the devtools panel. 152 * Logs active element hiding filters to the devtools panel.
146 * 153 *
147 * @param {Page} page The page the elements were hidden on 154 * @param {Page} page The page the elements were hidden on
148 * @param {string[]} selectors The CSS selectors of active elemhide filters 155 * @param {string[]} selectors The CSS selectors of active elemhide filters
149 * @param {string} docDomain The IDN-decoded hostname of the document 156 * @param {string} docDomain The IDN-decoded hostname of the document
150 */ 157 */
151 exports.logHiddenElements = function(page, selectors, docDomain) 158 exports.logHiddenElements = function(page, selectors, docDomain)
152 { 159 {
153 let panel = panels[page._id]; 160 let panel = getActivePanel(page);
154 if (panel && !panel.reload && !panel.reloading)
155 { 161 {
156 for (let subscription of FilterStorage.subscriptions) 162 for (let subscription of FilterStorage.subscriptions)
157 { 163 {
158 if (subscription.disabled) 164 if (subscription.disabled)
159 continue; 165 continue;
160 166
161 for (let filter of subscription.filters) 167 for (let filter of subscription.filters)
162 { 168 {
163 if (!(filter instanceof ElemHideFilter)) 169 if (!(filter instanceof ElemHideFilter))
164 continue; 170 continue;
165 if (selectors.indexOf(filter.selector) == -1) 171 if (selectors.indexOf(filter.selector) == -1)
166 continue; 172 continue;
167 if (!filter.isActiveOnDomain(docDomain)) 173 if (!filter.isActiveOnDomain(docDomain))
168 continue; 174 continue;
169 175
170 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); 176 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter);
171 } 177 }
178 }
179 }
180 };
181
182 /**
183 * Logs a whitelisting filter, that disables (some kind of)
184 * blocking for a particular document, to the devtools panel.
185 *
186 * @param {Page} page The page the whitelisting is active on
187 * @param {string} url The url of the whitelisted document
188 * @param {number} typeMask The bit mask of whitelisting types checked fo r
189 * @param {string} docDomain The IDN-decoded hostname of the parent docume nt
190 * @param {WhitelistFilter} filter The matched whitelisting filter
191 */
192 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter )
193 {
194 let panel = getActivePanel(page);
195 if (panel)
196 {
197 for (let type of nonRequestTypes)
198 {
199 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
200 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter);
172 } 201 }
173 } 202 }
174 }; 203 };
175 204
176 /** 205 /**
177 * Checks whether a page is inspected by the devtools panel. 206 * Checks whether a page is inspected by the devtools panel.
178 * 207 *
179 * @param {Page} page 208 * @param {Page} page
180 * @return {boolean} 209 * @return {boolean}
181 */ 210 */
182 exports.hasPanel = function(page) 211 exports.hasPanel = function(page)
183 { 212 {
184 return page._id in panels; 213 return page._id in panels;
185 }; 214 };
186 215
187 function onBeforeRequest(details) 216 function onBeforeRequest(details)
188 { 217 {
189 let panel = panels[details.tabId]; 218 let panel = panels[details.tabId];
190 219
191 // Clear the devtools panel and reload the inspected tab without caching 220 // Clear the devtools panel and reload the inspected tab without caching
192 // when a new request is issued. However, make sure that we don't end up 221 // when a new request is issued. However, make sure that we don't end up
193 // in an infinite recursion if we already triggered a reload. 222 // in an infinite recursion if we already triggered a reload.
194 if (panel.reloading) 223 if (panel.reloading)
195 { 224 {
196 panel.reloading = false; 225 panel.reloading = false;
197 } 226 }
198 else 227 else
199 { 228 {
200 panel.records = []; 229 panel.records = [];
201 panel.panel.sendMessage({type: "reset"}); 230 panel.port.postMessage({type: "reset"});
202 231
203 // We can't repeat the request if it isn't a GET request. Chrome would 232 // We can't repeat the request if it isn't a GET request. Chrome would
204 // prompt the user to confirm reloading the page, and POST requests are 233 // prompt the user to confirm reloading the page, and POST requests are
205 // known to cause issues on many websites if repeated. 234 // known to cause issues on many websites if repeated.
206 if (details.method == "GET") 235 if (details.method == "GET")
207 panel.reload = true; 236 panel.reload = true;
208 } 237 }
209 } 238 }
210 239
211 function onLoading(page) 240 function onLoading(page)
212 { 241 {
213 let tabId = page._id; 242 let tabId = page._id;
214 let panel = panels[tabId]; 243 let panel = panels[tabId];
215 244
216 // Reloading the tab is the only way that allows bypassing all caches, in 245 // Reloading the tab is the only way that allows bypassing all caches, in
217 // order to see all requests in the devtools panel. Reloading must not be 246 // order to see all requests in the devtools panel. Reloading must not be
218 // performed before the tab changes to "loading", otherwise it will load the 247 // performed before the tab changes to "loading", otherwise it will load the
219 // previous URL. 248 // previous URL.
220 if (panel && panel.reload) 249 if (panel && panel.reload)
221 { 250 {
222 chrome.tabs.reload(tabId, {bypassCache: true}); 251 chrome.tabs.reload(tabId, {bypassCache: true});
223 252
224 panel.reload = false; 253 panel.reload = false;
225 panel.reloading = true; 254 panel.reloading = true;
226 } 255 }
227 } 256 }
228 257
229 function onFilterChange(action, arg) 258 function onFilterChange(action, arg)
230 { 259 {
231 // When there haven't ever been any user filters before, the
232 // subscription is added, but no "filter.added" event is triggered.
233 let added, filters; 260 let added, filters;
234 if (action == "subscription.added") 261 switch (action)
235 { 262 {
236 if (!(arg instanceof SpecialSubscription)) 263 case "filter.added":
264 added = true;
265 filters = [arg];
266 break;
267
268 case "filter.removed":
269 added = false;
270 filters = [arg];
271 break;
272
273 // When there haven't ever been any user filters before, the subscription is
274 // added, triggering a "subscription.added" instead of a "filter.added" even t.
275 case "subscription.added":
276 if (arg instanceof SpecialSubscription)
277 {
278 added = true;
279 filters = arg.filters;
280 break;
281 }
282
283 default:
237 return; 284 return;
238
239 added = true;
240 filters = arg.filters;
241 }
242 else
243 {
244 added = (action == "filter.added");
245
246 if (!added && action != "filter.removed")
247 return;
248
249 filters = [arg];
250 } 285 }
251 286
252 for (let tabId in panels) 287 for (let tabId in panels)
253 { 288 {
254 let panel = panels[tabId]; 289 let panel = panels[tabId];
255 290
256 for (let i = 0; i < panel.records.length; i++) 291 for (let i = 0; i < panel.records.length; i++)
257 { 292 {
258 let record = panel.records[i]; 293 let record = panel.records[i];
259 294
260 // If an added filter matches a request shown in the devtools panel, 295 // If an added filter matches a request shown in the devtools panel,
261 // update that record to show the new filter. Ignore element hiding 296 // update that record to show the new filter. Ignore filters that aren't
262 // filters since there are only records for element hiding filters that 297 // associated with any sub-resource request. There is no record for these
263 // already match, and also we don't know if any new element matches. 298 // if they don't already match. In particular, in case of element hiding
299 // filters, we also wouldn't know if any new element matches.
264 if (added) 300 if (added)
265 { 301 {
266 if (record.request.type == "ELEMHIDE") 302 if (nonRequestTypes.indexOf(record.request.type) != -1)
267 continue; 303 continue;
268 304
269 let filter = matchRequest(record.request); 305 let filter = matchRequest(record.request);
270 if (filters.indexOf(filter) == -1) 306 if (filters.indexOf(filter) == -1)
271 continue; 307 continue;
272 308
273 record.filter = filter; 309 record.filter = filter;
274 } 310 }
275 311
276 // If a filter shown in the devtools panel got removed, update that 312 // If a filter shown in the devtools panel got removed, update that
277 // record to show the filter (if any) that matches now instead. 313 // record to show the filter that matches now, or none, instead.
278 // However, for element hiding, just remove the record, since we don't 314 // For filters that aren't associated with any sub-resource request,
279 // know whether another filter would match until the page is reloaded. 315 // just remove the record. We wouldn't know whether another filter
316 // matches instead until the page is reloaded.
280 else 317 else
281 { 318 {
282 if (filters.indexOf(record.filter) == -1) 319 if (filters.indexOf(record.filter) == -1)
283 continue; 320 continue;
284 321
285 if (record.request.type == "ELEMHIDE") 322 if (nonRequestTypes.indexOf(record.request.type) != -1)
286 { 323 {
287 panel.panel.sendMessage({ 324 panel.port.postMessage({
288 type: "remove-record", 325 type: "remove-record",
289 index: i 326 index: i
290 }); 327 });
291 panel.records.splice(i--, 1); 328 panel.records.splice(i--, 1);
292 continue; 329 continue;
293 } 330 }
294 331
295 record.filter = matchRequest(record.request); 332 record.filter = matchRequest(record.request);
296 } 333 }
297 334
298 panel.panel.sendMessage({ 335 panel.port.postMessage({
299 type: "update-record", 336 type: "update-record",
300 index: i, 337 index: i,
301 request: getRequestInfo(record.request), 338 request: record.request,
302 filter: getFilterInfo(record.filter) 339 filter: getFilterInfo(record.filter)
303 }); 340 });
304 } 341 }
305 } 342 }
306 } 343 }
307 344
308 ext.devtools.onCreated.addListener(function(panel) 345 chrome.runtime.onConnect.addListener(port =>
309 { 346 {
310 let inspectedTabId = panel.inspectedTabId; 347 let match = port.name.match(/^devtools-(\d+)$/);
348 if (!match)
349 return;
350
351 let inspectedTabId = parseInt(match[1], 10);
311 let localOnBeforeRequest = onBeforeRequest.bind(); 352 let localOnBeforeRequest = onBeforeRequest.bind();
312 353
313 chrome.webRequest.onBeforeRequest.addListener( 354 chrome.webRequest.onBeforeRequest.addListener(
314 localOnBeforeRequest, 355 localOnBeforeRequest,
315 { 356 {
316 urls: ["<all_urls>"], 357 urls: ["<all_urls>"],
317 types: ["main_frame"], 358 types: ["main_frame"],
318 tabId: inspectedTabId 359 tabId: inspectedTabId
319 } 360 }
320 ); 361 );
321 362
322 if (!hasPanels()) 363 if (!hasPanels())
323 { 364 {
324 ext.pages.onLoading.addListener(onLoading); 365 ext.pages.onLoading.addListener(onLoading);
325 FilterNotifier.addListener(onFilterChange); 366 FilterNotifier.addListener(onFilterChange);
326 } 367 }
327 368
328 panel.onRemoved.addListener(function() 369 port.onDisconnect.addListener(() =>
329 { 370 {
330 delete panels[inspectedTabId]; 371 delete panels[inspectedTabId];
331 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 372 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
332 373
333 if (!hasPanels()) 374 if (!hasPanels())
334 { 375 {
335 FilterNotifier.removeListener(onFilterChange); 376 FilterNotifier.removeListener(onFilterChange);
336 ext.pages.onLoading.removeListener(onLoading); 377 ext.pages.onLoading.removeListener(onLoading);
337 } 378 }
338 }); 379 });
339 380
340 panels[inspectedTabId] = {panel: panel, records: []}; 381 panels[inspectedTabId] = {port: port, records: []};
341 }); 382 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld