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

Delta Between Two Patch Sets: lib/devtools.js

Issue 29418679: Issue 5042 - Adds handling for requests which are not associated with browser tab (Closed)
Left Patch Set: Created April 20, 2017, 9:43 p.m.
Right Patch Set: fix whitespace, add comment abut null page, remove redundancies Created June 2, 2017, 5:01 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 | « ext/background.js ('k') | lib/requestBlocker.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 14 matching lines...) Expand all
25 const {defaultMatcher} = require("matcher"); 25 const {defaultMatcher} = require("matcher");
26 const {FilterNotifier} = require("filterNotifier"); 26 const {FilterNotifier} = require("filterNotifier");
27 const {extractHostFromFrame} = require("url"); 27 const {extractHostFromFrame} = require("url");
28 const {port} = require("messaging"); 28 const {port} = require("messaging");
29 29
30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; 30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"];
31 31
32 // Mapping of inspected tabs to their devpanel page 32 // Mapping of inspected tabs to their devpanel page
33 // and recorded items. We can't use a PageMap here, 33 // and recorded items. We can't use a PageMap here,
34 // because data must persist after navigation/reload. 34 // because data must persist after navigation/reload.
35 let panels = Object.create(null); 35 let panels = new Map();
36 36
37 function hasPanels() 37 function isActivePanel(panel)
38 { 38 {
39 return Object.keys(panels).length > 0; 39 return panel && !panel.reload && !panel.reloading;
40 } 40 }
41 41
42 function getActivePanel(page) 42 function getActivePanel(page)
43 { 43 {
44 let panel = panels[page.id]; 44 let panel = panels.get(page.id);
45 if (panel && !panel.reload && !panel.reloading) 45 if (isActivePanel(panel))
46 return panel; 46 return panel;
47 return null; 47 return null;
48 } 48 }
49 49
50 function getFilterInfo(filter) 50 function getFilterInfo(filter)
51 { 51 {
52 if (!filter) 52 if (!filter)
53 return null; 53 return null;
54 54
55 let userDefined = false; 55 let userDefined = false;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 request.docDomain, 114 request.docDomain,
115 request.thirdParty, 115 request.thirdParty,
116 request.sitekey, 116 request.sitekey,
117 request.specificOnly 117 request.specificOnly
118 ); 118 );
119 } 119 }
120 120
121 /** 121 /**
122 * Logs a request to the devtools panel. 122 * Logs a request to the devtools panel.
123 * 123 *
124 * @param {Page} page The page the request occured on 124 * @param {?Page} page The page the request occured on or null if
125 * the request isn't associated with a page
125 * @param {string} url The URL of the request 126 * @param {string} url The URL of the request
126 * @param {string} type The request type 127 * @param {string} type The request type
127 * @param {string} docDomain The IDN-decoded hostname of the document 128 * @param {string} docDomain The IDN-decoded hostname of the document
128 * @param {boolean} thirdParty Whether the origin of the request and 129 * @param {boolean} thirdParty Whether the origin of the request and
129 * document differs 130 * document differs
130 * @param {?string} sitekey The active sitekey if there is any 131 * @param {?string} sitekey The active sitekey if there is any
131 * @param {?boolean} specificOnly Whether generic filters should be ignored 132 * @param {?boolean} specificOnly Whether generic filters should be ignored
132 * @param {?BlockingFilter} filter The matched filter or null if there is no 133 * @param {?BlockingFilter} filter The matched filter or null if there is no
133 * match 134 * match
134 */ 135 */
135 exports.logRequest = function(page, url, type, docDomain, 136 exports.logRequest = function(page, url, type, docDomain,
136 thirdParty, sitekey, 137 thirdParty, sitekey,
137 specificOnly, filter) 138 specificOnly, filter)
138 { 139 {
140 if (panels.size == 0)
141 return;
142
139 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; 143 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly};
Sebastian Noack 2017/04/21 08:39:53 Perhaps, we should bail if there are no panels, be
Jon Sonesen 2017/04/24 08:40:56 Acknowledged.
140 if (page) 144 for (let [tabId, panel] of panels)
141 { 145 if ((!page || page.id == tabId) && isActivePanel(panel))
142 let panel = getActivePanel(page);
143 if (panel)
144 addRecord(panel, request, filter); 146 addRecord(panel, request, filter);
145 return;
146 }
147 for (let panelId in panels)
Sebastian Noack 2017/04/21 08:39:53 Some of the code added here, and in this file in g
Jon Sonesen 2017/04/24 08:40:56 Awesome, I agree here. I am wondering though, shou
148 addRecord(panels[panelId], request, filter);
Sebastian Noack 2017/04/21 08:39:53 getActivePanel(), called when logging for a specif
Jon Sonesen 2017/04/24 08:40:56 Acknowledged.
149 }; 147 };
150 148
151 /** 149 /**
152 * Logs active element hiding filters to the devtools panel. 150 * Logs active element hiding filters to the devtools panel.
153 * 151 *
154 * @param {Page} page The page the elements were hidden on 152 * @param {Page} page The page the elements were hidden on
155 * @param {string[]} selectors The selectors of applied ElemHideFilters 153 * @param {string[]} selectors The selectors of applied ElemHideFilters
156 * @param {string[]} filters The text of applied ElemHideEmulationFilters 154 * @param {string[]} filters The text of applied ElemHideEmulationFilters
157 * @param {string} docDomain The IDN-decoded hostname of the document 155 * @param {string} docDomain The IDN-decoded hostname of the document
158 */ 156 */
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 }; 208 };
211 209
212 /** 210 /**
213 * Checks whether a page is inspected by the devtools panel. 211 * Checks whether a page is inspected by the devtools panel.
214 * 212 *
215 * @param {Page} page 213 * @param {Page} page
216 * @return {boolean} 214 * @return {boolean}
217 */ 215 */
218 exports.hasPanel = function(page) 216 exports.hasPanel = function(page)
219 { 217 {
220 return page.id in panels; 218 return panels.has(page.id);
221 }; 219 };
222 220
223 function onBeforeRequest(details) 221 function onBeforeRequest(details)
224 { 222 {
225 let panel = panels[details.tabId]; 223 let panel = panels.get(details.tabId);
226 224
227 // Clear the devtools panel and reload the inspected tab without caching 225 // Clear the devtools panel and reload the inspected tab without caching
228 // when a new request is issued. However, make sure that we don't end up 226 // when a new request is issued. However, make sure that we don't end up
229 // in an infinite recursion if we already triggered a reload. 227 // in an infinite recursion if we already triggered a reload.
230 if (panel.reloading) 228 if (panel.reloading)
231 { 229 {
232 panel.reloading = false; 230 panel.reloading = false;
233 } 231 }
234 else 232 else
235 { 233 {
236 panel.records = []; 234 panel.records = [];
237 panel.port.postMessage({type: "reset"}); 235 panel.port.postMessage({type: "reset"});
238 236
239 // We can't repeat the request if it isn't a GET request. Chrome would 237 // We can't repeat the request if it isn't a GET request. Chrome would
240 // prompt the user to confirm reloading the page, and POST requests are 238 // prompt the user to confirm reloading the page, and POST requests are
241 // known to cause issues on many websites if repeated. 239 // known to cause issues on many websites if repeated.
242 if (details.method == "GET") 240 if (details.method == "GET")
243 panel.reload = true; 241 panel.reload = true;
244 } 242 }
245 } 243 }
246 244
247 function onLoading(page) 245 function onLoading(page)
248 { 246 {
249 let tabId = page.id; 247 let tabId = page.id;
250 let panel = panels[tabId]; 248 let panel = panels.get(tabId);
251 249
252 // Reloading the tab is the only way that allows bypassing all caches, in 250 // Reloading the tab is the only way that allows bypassing all caches, in
253 // order to see all requests in the devtools panel. Reloading must not be 251 // order to see all requests in the devtools panel. Reloading must not be
254 // performed before the tab changes to "loading", otherwise it will load the 252 // performed before the tab changes to "loading", otherwise it will load the
255 // previous URL. 253 // previous URL.
256 if (panel && panel.reload) 254 if (panel && panel.reload)
257 { 255 {
258 chrome.tabs.reload(tabId, {bypassCache: true}); 256 chrome.tabs.reload(tabId, {bypassCache: true});
259 257
260 panel.reload = false; 258 panel.reload = false;
261 panel.reloading = true; 259 panel.reloading = true;
262 } 260 }
263 } 261 }
264 262
265 function updateFilters(filters, added) 263 function updateFilters(filters, added)
266 { 264 {
267 for (let tabId in panels) 265 for (let panel of panels.values())
268 { 266 {
269 let panel = panels[tabId];
270
271 for (let i = 0; i < panel.records.length; i++) 267 for (let i = 0; i < panel.records.length; i++)
272 { 268 {
273 let record = panel.records[i]; 269 let record = panel.records[i];
274 270
275 // If an added filter matches a request shown in the devtools panel, 271 // If an added filter matches a request shown in the devtools panel,
276 // update that record to show the new filter. Ignore filters that aren't 272 // update that record to show the new filter. Ignore filters that aren't
277 // associated with any sub-resource request. There is no record for these 273 // associated with any sub-resource request. There is no record for these
278 // if they don't already match. In particular, in case of element hiding 274 // if they don't already match. In particular, in case of element hiding
279 // filters, we also wouldn't know if any new element matches. 275 // filters, we also wouldn't know if any new element matches.
280 if (added) 276 if (added)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 345
350 chrome.webRequest.onBeforeRequest.addListener( 346 chrome.webRequest.onBeforeRequest.addListener(
351 localOnBeforeRequest, 347 localOnBeforeRequest,
352 { 348 {
353 urls: ["http://*/*", "https://*/*"], 349 urls: ["http://*/*", "https://*/*"],
354 types: ["main_frame"], 350 types: ["main_frame"],
355 tabId: inspectedTabId 351 tabId: inspectedTabId
356 } 352 }
357 ); 353 );
358 354
359 if (!hasPanels()) 355 if (panels.size == 0)
360 { 356 {
361 ext.pages.onLoading.addListener(onLoading); 357 ext.pages.onLoading.addListener(onLoading);
362 FilterNotifier.on("filter.added", onFilterAdded); 358 FilterNotifier.on("filter.added", onFilterAdded);
363 FilterNotifier.on("filter.removed", onFilterRemoved); 359 FilterNotifier.on("filter.removed", onFilterRemoved);
364 FilterNotifier.on("subscription.added", onSubscriptionAdded); 360 FilterNotifier.on("subscription.added", onSubscriptionAdded);
365 } 361 }
366 362
367 newPort.onDisconnect.addListener(() => 363 newPort.onDisconnect.addListener(() =>
368 { 364 {
369 delete panels[inspectedTabId]; 365 panels.delete(inspectedTabId);
370 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
371 367
372 if (!hasPanels()) 368 if (panels.size == 0)
373 { 369 {
374 ext.pages.onLoading.removeListener(onLoading); 370 ext.pages.onLoading.removeListener(onLoading);
375 FilterNotifier.off("filter.added", onFilterAdded); 371 FilterNotifier.off("filter.added", onFilterAdded);
376 FilterNotifier.off("filter.removed", onFilterRemoved); 372 FilterNotifier.off("filter.removed", onFilterRemoved);
377 FilterNotifier.off("subscription.added", onSubscriptionAdded); 373 FilterNotifier.off("subscription.added", onSubscriptionAdded);
378 } 374 }
379 }); 375 });
380 376
381 panels[inspectedTabId] = {port: newPort, records: []}; 377 panels.set(inspectedTabId, {port: newPort, records: []});
382 }); 378 });
383 379
384 port.on("devtools.traceElemHide", (message, sender) => 380 port.on("devtools.traceElemHide", (message, sender) =>
385 { 381 {
386 logHiddenElements( 382 logHiddenElements(
387 sender.page, message.selectors, message.filters, 383 sender.page, message.selectors, message.filters,
388 extractHostFromFrame(sender.frame) 384 extractHostFromFrame(sender.frame)
389 ); 385 );
390 }); 386 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld