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

Side by Side Diff: lib/devtools.js

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

Powered by Google App Engine
This is Rietveld