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

Powered by Google App Engine
This is Rietveld