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: Use chrome.runtime.onConnect directly Created Feb. 2, 2016, 11:19 a.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.getOwnPropertyNames(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.type == request.type &&
86 record.request.url == request.url &&
87 record.request.docDomain == request.docDomain &&
88
89 // Matched element hiding filters don't relate to a particular request,
90 // so we also have to match the CSS selector in order to distinguish them.
91 (record.filter && record.filter.selector) == (filter && filter.selector)
92 );
93 }
94
95 function addRecord(panel, request, filter)
96 {
97 if (!hasRecord(panel, request, filter))
98 {
99 panel.port.sendMessage({
100 type: "add-record",
101 request: getRequestInfo(request),
102 filter: getFilterInfo(filter)
103 });
104
105 panel.records.push({
106 request: request,
107 filter: filter
108 });
109 }
110 }
111
112 function matchRequest(request)
113 {
114 return defaultMatcher.matchesAny(
115 request.url,
116 RegExpFilter.typeMap[request.type],
117 request.docDomain,
118 request.thirdParty,
119 request.sitekey,
120 request.specificOnly
121 );
122 }
123
124 /**
125 * Logs a request to the devtools panel.
126 *
127 * @param {Page} page The page the request occured on
128 * @param {string] url The URL of the request
129 * @param {string} type The request type
130 * @param {string} docDomain The IDN-decoded hostname of the document
131 * @param {boolean} thirdParty Whether the origin of the request and docume nt differs
132 * @param {string} [sitekey] The active sitekey if there is any
133 * @param {boolean} [specificOnly] Whether generic filters should be ignored
134 * @param {BlockingFilter} [filter] The matched filter or null if there is no ma tch
135 */
136 exports.logRequest = function(page, url, type, docDomain,
137 thirdParty, sitekey,
138 specificOnly, filter)
139 {
140 let panel = getActivePanel(page);
141 if (panel)
142 {
143 let request = {
144 url: url,
145 type: type,
146 docDomain: docDomain,
147 thirdParty: thirdParty,
148 sitekey: sitekey,
149 specificOnly: specificOnly
150 };
151
152 addRecord(panel, request, filter);
153 }
154 };
155
156 /**
157 * Logs active element hiding filters to the devtools panel.
158 *
159 * @param {Page} page The page the elements were hidden on
160 * @param {string[]} selectors The CSS selectors of active elemhide filters
161 * @param {string} docDomain The IDN-decoded hostname of the document
162 */
163 exports.logHiddenElements = function(page, selectors, docDomain)
164 {
165 let panel = getActivePanel(page);
166 {
167 for (let subscription of FilterStorage.subscriptions)
168 {
169 if (subscription.disabled)
170 continue;
171
172 for (let filter of subscription.filters)
173 {
174 if (!(filter instanceof ElemHideFilter))
175 continue;
176 if (selectors.indexOf(filter.selector) == -1)
177 continue;
178 if (!filter.isActiveOnDomain(docDomain))
179 continue;
180
181 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter);
182 }
183 }
184 }
185 };
186
187 /**
188 * Logs a whitelisting filter, that disables (some kind of)
189 * blocking for a particular document, to the devtools panel.
190 *
191 * @param {Page} page The page the whitelisting is active on
192 * @param {string} url The url of the whitelisted document
193 * @param {number} typeMask The bit mask of whitelisting types checked fo r
194 * @param {string} docDomain The IDN-decoded hostname of the parent docume nt
195 * @param {WhitelistFilter} filter The matched whitelisting filter
196 */
197 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter )
198 {
199 let panel = getActivePanel(page);
200 if (panel)
201 {
202 for (let type of nonRequestTypes)
203 {
204 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
205 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter);
206 }
207 }
208 };
209
210 /**
211 * Checks whether a page is inspected by the devtools panel.
212 *
213 * @param {Page} page
214 * @return {boolean}
215 */
216 exports.hasPanel = function(page)
217 {
218 return page._id in panels;
219 };
220
221 function onBeforeRequest(details)
222 {
223 let panel = panels[details.tabId];
224
225 // Clear the devtools panel and reload the inspected tab without caching
226 // when a new request is issued. However, make sure that we don't end up
227 // in an infinite recursion if we already triggered a reload.
228 if (panel.reloading)
229 {
230 panel.reloading = false;
231 }
232 else
233 {
234 panel.records = [];
235 panel.port.sendMessage({type: "reset"});
236
237 // We can't repeat the request if it isn't a GET request. Chrome would
238 // prompt the user to confirm reloading the page, and POST requests are
239 // known to cause issues on many websites if repeated.
240 if (details.method == "GET")
241 panel.reload = true;
242 }
243 }
244
245 function onLoading(page)
246 {
247 let tabId = page._id;
248 let panel = panels[tabId];
249
250 // Reloading the tab is the only way that allows bypassing all caches, in
251 // order to see all requests in the devtools panel. Reloading must not be
252 // performed before the tab changes to "loading", otherwise it will load the
253 // previous URL.
254 if (panel && panel.reload)
255 {
256 chrome.tabs.reload(tabId, {bypassCache: true});
257
258 panel.reload = false;
259 panel.reloading = true;
260 }
261 }
262
263 function onFilterChange(action, arg)
264 {
265 let added, filters;
266 switch (action)
267 {
268 case "filter.added":
269 added = true;
270 filters = [arg];
271 break;
272
273 case "filter.removed":
274 added = false;
275 filters = [arg];
276 break;
277
278 // When there haven't ever been any user filters before, the subscription is
279 // added, triggering a "subscription.added" instead of a "filter.added" even t.
280 case "subscription.added":
281 if (arg instanceof SpecialSubscription)
282 {
283 added = true;
284 filters = arg.filters;
285 break;
286 }
287
288 default:
289 return;
290 }
291
292 for (let tabId in panels)
293 {
294 let panel = panels[tabId];
295
296 for (let i = 0; i < panel.records.length; i++)
297 {
298 let record = panel.records[i];
299
300 // If an added filter matches a request shown in the devtools panel,
301 // update that record to show the new filter. Ignore filters that aren't
302 // associated with any sub-resource request. There is no record for these
303 // if they don't already match. In particular, in case of element hiding
304 // filters, we also wouldn't know if any new element matches.
305 if (added)
306 {
307 if (nonRequestTypes.indexOf(record.request.type) != -1)
308 continue;
309
310 let filter = matchRequest(record.request);
311 if (filters.indexOf(filter) == -1)
312 continue;
313
314 record.filter = filter;
315 }
316
317 // If a filter shown in the devtools panel got removed, update that
318 // record to show the filter that matches now, or none, instead.
319 // For filters that aren't associated with any sub-resource request,
320 // just remove the record. We wouldn't know whether another filter
321 // matches instead until the page is reloaded.
322 else
323 {
324 if (filters.indexOf(record.filter) == -1)
325 continue;
326
327 if (nonRequestTypes.indexOf(record.request.type) != -1)
328 {
329 panel.port.sendMessage({
330 type: "remove-record",
331 index: i
332 });
333 panel.records.splice(i--, 1);
334 continue;
335 }
336
337 record.filter = matchRequest(record.request);
338 }
339
340 panel.port.sendMessage({
341 type: "update-record",
342 index: i,
343 request: getRequestInfo(record.request),
344 filter: getFilterInfo(record.filter)
345 });
346 }
347 }
348 }
349
350 chrome.runtime.onConnect.addListener(port =>
351 {
352 let match = port.name.match(/^devtools-(\d+)$/);
353 if (!match)
354 return;
355
356 let inspectedTabId = parseInt(match[1], 10);
357 let localOnBeforeRequest = onBeforeRequest.bind();
358
359 chrome.webRequest.onBeforeRequest.addListener(
360 localOnBeforeRequest,
361 {
362 urls: ["<all_urls>"],
363 types: ["main_frame"],
364 tabId: inspectedTabId
365 }
366 );
367
368 if (!hasPanels())
369 {
370 ext.pages.onLoading.addListener(onLoading);
371 FilterNotifier.addListener(onFilterChange);
372 }
373
374 port.onDisconnect.addListener(() =>
375 {
376 delete panels[inspectedTabId];
377 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
378
379 if (!hasPanels())
380 {
381 FilterNotifier.removeListener(onFilterChange);
382 ext.pages.onLoading.removeListener(onLoading);
383 }
384 });
385
386 panels[inspectedTabId] = {port: port, records: []};
387 });
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