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

Side by Side Diff: lib/devtools.js

Issue 29417597: Issue 5161 - Use maps and sets where appropriate (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created April 19, 2017, 4:33 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
OLDNEW
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 hasPanels()
38 { 38 {
39 return Object.keys(panels).length > 0; 39 return panels.size > 0;
Sebastian Noack 2017/04/29 22:46:32 Now where this check is less obscure, there isn't
Manish Jethani 2017/05/04 14:47:34 Changed here and in popupBlocker.
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 (panel && !panel.reload && !panel.reloading)
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
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 }; 206 };
207 207
208 /** 208 /**
209 * Checks whether a page is inspected by the devtools panel. 209 * Checks whether a page is inspected by the devtools panel.
210 * 210 *
211 * @param {Page} page 211 * @param {Page} page
212 * @return {boolean} 212 * @return {boolean}
213 */ 213 */
214 exports.hasPanel = function(page) 214 exports.hasPanel = function(page)
215 { 215 {
216 return page.id in panels; 216 return panels.has(page.id);
217 }; 217 };
218 218
219 function onBeforeRequest(details) 219 function onBeforeRequest(details)
220 { 220 {
221 let panel = panels[details.tabId]; 221 let panel = panels.get(details.tabId);
222 222
223 // Clear the devtools panel and reload the inspected tab without caching 223 // Clear the devtools panel and reload the inspected tab without caching
224 // when a new request is issued. However, make sure that we don't end up 224 // when a new request is issued. However, make sure that we don't end up
225 // in an infinite recursion if we already triggered a reload. 225 // in an infinite recursion if we already triggered a reload.
226 if (panel.reloading) 226 if (panel.reloading)
227 { 227 {
228 panel.reloading = false; 228 panel.reloading = false;
229 } 229 }
230 else 230 else
231 { 231 {
232 panel.records = []; 232 panel.records = [];
233 panel.port.postMessage({type: "reset"}); 233 panel.port.postMessage({type: "reset"});
234 234
235 // We can't repeat the request if it isn't a GET request. Chrome would 235 // We can't repeat the request if it isn't a GET request. Chrome would
236 // prompt the user to confirm reloading the page, and POST requests are 236 // prompt the user to confirm reloading the page, and POST requests are
237 // known to cause issues on many websites if repeated. 237 // known to cause issues on many websites if repeated.
238 if (details.method == "GET") 238 if (details.method == "GET")
239 panel.reload = true; 239 panel.reload = true;
240 } 240 }
241 } 241 }
242 242
243 function onLoading(page) 243 function onLoading(page)
244 { 244 {
245 let tabId = page.id; 245 let tabId = page.id;
246 let panel = panels[tabId]; 246 let panel = panels.get(tabId);
247 247
248 // Reloading the tab is the only way that allows bypassing all caches, in 248 // Reloading the tab is the only way that allows bypassing all caches, in
249 // order to see all requests in the devtools panel. Reloading must not be 249 // order to see all requests in the devtools panel. Reloading must not be
250 // performed before the tab changes to "loading", otherwise it will load the 250 // performed before the tab changes to "loading", otherwise it will load the
251 // previous URL. 251 // previous URL.
252 if (panel && panel.reload) 252 if (panel && panel.reload)
253 { 253 {
254 chrome.tabs.reload(tabId, {bypassCache: true}); 254 chrome.tabs.reload(tabId, {bypassCache: true});
255 255
256 panel.reload = false; 256 panel.reload = false;
257 panel.reloading = true; 257 panel.reloading = true;
258 } 258 }
259 } 259 }
260 260
261 function updateFilters(filters, added) 261 function updateFilters(filters, added)
262 { 262 {
263 for (let tabId in panels) 263 for (let panel of panels.values())
264 { 264 {
265 let panel = panels[tabId];
266
267 for (let i = 0; i < panel.records.length; i++) 265 for (let i = 0; i < panel.records.length; i++)
268 { 266 {
269 let record = panel.records[i]; 267 let record = panel.records[i];
270 268
271 // If an added filter matches a request shown in the devtools panel, 269 // If an added filter matches a request shown in the devtools panel,
272 // update that record to show the new filter. Ignore filters that aren't 270 // update that record to show the new filter. Ignore filters that aren't
273 // associated with any sub-resource request. There is no record for these 271 // associated with any sub-resource request. There is no record for these
274 // if they don't already match. In particular, in case of element hiding 272 // if they don't already match. In particular, in case of element hiding
275 // filters, we also wouldn't know if any new element matches. 273 // filters, we also wouldn't know if any new element matches.
276 if (added) 274 if (added)
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 if (!hasPanels()) 353 if (!hasPanels())
356 { 354 {
357 ext.pages.onLoading.addListener(onLoading); 355 ext.pages.onLoading.addListener(onLoading);
358 FilterNotifier.on("filter.added", onFilterAdded); 356 FilterNotifier.on("filter.added", onFilterAdded);
359 FilterNotifier.on("filter.removed", onFilterRemoved); 357 FilterNotifier.on("filter.removed", onFilterRemoved);
360 FilterNotifier.on("subscription.added", onSubscriptionAdded); 358 FilterNotifier.on("subscription.added", onSubscriptionAdded);
361 } 359 }
362 360
363 newPort.onDisconnect.addListener(() => 361 newPort.onDisconnect.addListener(() =>
364 { 362 {
365 delete panels[inspectedTabId]; 363 panels.delete(inspectedTabId);
366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 364 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
367 365
368 if (!hasPanels()) 366 if (!hasPanels())
369 { 367 {
370 ext.pages.onLoading.removeListener(onLoading); 368 ext.pages.onLoading.removeListener(onLoading);
371 FilterNotifier.off("filter.added", onFilterAdded); 369 FilterNotifier.off("filter.added", onFilterAdded);
372 FilterNotifier.off("filter.removed", onFilterRemoved); 370 FilterNotifier.off("filter.removed", onFilterRemoved);
373 FilterNotifier.off("subscription.added", onSubscriptionAdded); 371 FilterNotifier.off("subscription.added", onSubscriptionAdded);
374 } 372 }
375 }); 373 });
376 374
377 panels[inspectedTabId] = {port: newPort, records: []}; 375 panels.set(inspectedTabId, {port: newPort, records: []});
378 }); 376 });
379 377
380 port.on("devtools.traceElemHide", (message, sender) => 378 port.on("devtools.traceElemHide", (message, sender) =>
381 { 379 {
382 logHiddenElements( 380 logHiddenElements(
383 sender.page, message.selectors, message.filters, 381 sender.page, message.selectors, message.filters,
384 extractHostFromFrame(sender.frame) 382 extractHostFromFrame(sender.frame)
385 ); 383 );
386 }); 384 });
OLDNEW

Powered by Google App Engine
This is Rietveld