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: Remove blank line Created May 17, 2017, 9:47 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 | « ext/background.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
37 function hasPanels()
38 {
39 return Object.keys(panels).length > 0;
40 }
41 36
42 function getActivePanel(page) 37 function getActivePanel(page)
43 { 38 {
44 let panel = panels[page.id]; 39 let panel = panels.get(page.id);
45 if (panel && !panel.reload && !panel.reloading) 40 if (panel && !panel.reload && !panel.reloading)
46 return panel; 41 return panel;
47 return null; 42 return null;
48 } 43 }
49 44
50 function getFilterInfo(filter) 45 function getFilterInfo(filter)
51 { 46 {
52 if (!filter) 47 if (!filter)
53 return null; 48 return null;
54 49
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 }; 201 };
207 202
208 /** 203 /**
209 * Checks whether a page is inspected by the devtools panel. 204 * Checks whether a page is inspected by the devtools panel.
210 * 205 *
211 * @param {Page} page 206 * @param {Page} page
212 * @return {boolean} 207 * @return {boolean}
213 */ 208 */
214 exports.hasPanel = function(page) 209 exports.hasPanel = function(page)
215 { 210 {
216 return page.id in panels; 211 return panels.has(page.id);
217 }; 212 };
218 213
219 function onBeforeRequest(details) 214 function onBeforeRequest(details)
220 { 215 {
221 let panel = panels[details.tabId]; 216 let panel = panels.get(details.tabId);
222 217
223 // Clear the devtools panel and reload the inspected tab without caching 218 // 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 219 // 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. 220 // in an infinite recursion if we already triggered a reload.
226 if (panel.reloading) 221 if (panel.reloading)
227 { 222 {
228 panel.reloading = false; 223 panel.reloading = false;
229 } 224 }
230 else 225 else
231 { 226 {
232 panel.records = []; 227 panel.records = [];
233 panel.port.postMessage({type: "reset"}); 228 panel.port.postMessage({type: "reset"});
234 229
235 // We can't repeat the request if it isn't a GET request. Chrome would 230 // 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 231 // prompt the user to confirm reloading the page, and POST requests are
237 // known to cause issues on many websites if repeated. 232 // known to cause issues on many websites if repeated.
238 if (details.method == "GET") 233 if (details.method == "GET")
239 panel.reload = true; 234 panel.reload = true;
240 } 235 }
241 } 236 }
242 237
243 function onLoading(page) 238 function onLoading(page)
244 { 239 {
245 let tabId = page.id; 240 let tabId = page.id;
246 let panel = panels[tabId]; 241 let panel = panels.get(tabId);
247 242
248 // Reloading the tab is the only way that allows bypassing all caches, in 243 // 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 244 // 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 245 // performed before the tab changes to "loading", otherwise it will load the
251 // previous URL. 246 // previous URL.
252 if (panel && panel.reload) 247 if (panel && panel.reload)
253 { 248 {
254 chrome.tabs.reload(tabId, {bypassCache: true}); 249 chrome.tabs.reload(tabId, {bypassCache: true});
255 250
256 panel.reload = false; 251 panel.reload = false;
257 panel.reloading = true; 252 panel.reloading = true;
258 } 253 }
259 } 254 }
260 255
261 function updateFilters(filters, added) 256 function updateFilters(filters, added)
262 { 257 {
263 for (let tabId in panels) 258 for (let panel of panels.values())
264 { 259 {
265 let panel = panels[tabId];
266
267 for (let i = 0; i < panel.records.length; i++) 260 for (let i = 0; i < panel.records.length; i++)
268 { 261 {
269 let record = panel.records[i]; 262 let record = panel.records[i];
270 263
271 // If an added filter matches a request shown in the devtools panel, 264 // 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 265 // 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 266 // 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 267 // 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. 268 // filters, we also wouldn't know if any new element matches.
276 if (added) 269 if (added)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 338
346 chrome.webRequest.onBeforeRequest.addListener( 339 chrome.webRequest.onBeforeRequest.addListener(
347 localOnBeforeRequest, 340 localOnBeforeRequest,
348 { 341 {
349 urls: ["<all_urls>"], 342 urls: ["<all_urls>"],
350 types: ["main_frame"], 343 types: ["main_frame"],
351 tabId: inspectedTabId 344 tabId: inspectedTabId
352 } 345 }
353 ); 346 );
354 347
355 if (!hasPanels()) 348 if (panels.size == 0)
356 { 349 {
357 ext.pages.onLoading.addListener(onLoading); 350 ext.pages.onLoading.addListener(onLoading);
358 FilterNotifier.on("filter.added", onFilterAdded); 351 FilterNotifier.on("filter.added", onFilterAdded);
359 FilterNotifier.on("filter.removed", onFilterRemoved); 352 FilterNotifier.on("filter.removed", onFilterRemoved);
360 FilterNotifier.on("subscription.added", onSubscriptionAdded); 353 FilterNotifier.on("subscription.added", onSubscriptionAdded);
361 } 354 }
362 355
363 newPort.onDisconnect.addListener(() => 356 newPort.onDisconnect.addListener(() =>
364 { 357 {
365 delete panels[inspectedTabId]; 358 panels.delete(inspectedTabId);
366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 359 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
367 360
368 if (!hasPanels()) 361 if (panels.size == 0)
369 { 362 {
370 ext.pages.onLoading.removeListener(onLoading); 363 ext.pages.onLoading.removeListener(onLoading);
371 FilterNotifier.off("filter.added", onFilterAdded); 364 FilterNotifier.off("filter.added", onFilterAdded);
372 FilterNotifier.off("filter.removed", onFilterRemoved); 365 FilterNotifier.off("filter.removed", onFilterRemoved);
373 FilterNotifier.off("subscription.added", onSubscriptionAdded); 366 FilterNotifier.off("subscription.added", onSubscriptionAdded);
374 } 367 }
375 }); 368 });
376 369
377 panels[inspectedTabId] = {port: newPort, records: []}; 370 panels.set(inspectedTabId, {port: newPort, records: []});
378 }); 371 });
379 372
380 port.on("devtools.traceElemHide", (message, sender) => 373 port.on("devtools.traceElemHide", (message, sender) =>
381 { 374 {
382 logHiddenElements( 375 logHiddenElements(
383 sender.page, message.selectors, message.filters, 376 sender.page, message.selectors, message.filters,
384 extractHostFromFrame(sender.frame) 377 extractHostFromFrame(sender.frame)
385 ); 378 );
386 }); 379 });
OLDNEW
« no previous file with comments | « ext/background.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld