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

Unified Diff: lib/devtools.js

Issue 29418679: Issue 5042 - Adds handling for requests which are not associated with browser tab (Closed)
Patch Set: address nit, redundancies Created May 4, 2017, 10:21 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ext/background.js ('k') | lib/requestBlocker.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/devtools.js
===================================================================
--- a/lib/devtools.js
+++ b/lib/devtools.js
@@ -27,27 +27,34 @@
const {extractHostFromFrame} = require("url");
const {port} = require("messaging");
const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"];
// Mapping of inspected tabs to their devpanel page
// and recorded items. We can't use a PageMap here,
// because data must persist after navigation/reload.
-let panels = Object.create(null);
+let panels = new Map();
function hasPanels()
{
- return Object.keys(panels).length > 0;
+ return panels.size > 0;
+}
+
+function isActivePanel(panel)
+{
+ if (panel && !panel.reload && !panel.reloading)
+ return true;
+ return false;
}
function getActivePanel(page)
{
- let panel = panels[page.id];
- if (panel && !panel.reload && !panel.reloading)
+ let panel = panels.get(page.id);
+ if (isActivePanel(panel))
return panel;
return null;
}
function getFilterInfo(filter)
{
if (!filter)
return null;
@@ -131,22 +138,29 @@
* @param {?boolean} specificOnly Whether generic filters should be ignored
* @param {?BlockingFilter} filter The matched filter or null if there is no
* match
*/
exports.logRequest = function(page, url, type, docDomain,
thirdParty, sitekey,
specificOnly, filter)
{
- let panel = getActivePanel(page);
- if (panel)
+ if (!hasPanels())
+ return;
+ let request = {url, type, docDomain, thirdParty, sitekey, specificOnly};
+ if (page)
{
- let request = {url, type, docDomain, thirdParty, sitekey, specificOnly};
- addRecord(panel, request, filter);
+ let panel = getActivePanel(page);
+ if (panel)
+ addRecord(panel, request, filter);
+ return;
}
+ for (let panel of panels.values())
+ if (isActivePanel(panel))
+ addRecord(panel, request, filter);
};
/**
* Logs active element hiding filters to the devtools panel.
*
* @param {Page} page The page the elements were hidden on
* @param {string[]} selectors The selectors of applied ElemHideFilters
* @param {string[]} filters The text of applied ElemHideEmulationFilters
@@ -208,22 +222,22 @@
/**
* Checks whether a page is inspected by the devtools panel.
*
* @param {Page} page
* @return {boolean}
*/
exports.hasPanel = function(page)
{
- return page.id in panels;
+ return panels.has(page.id);
};
function onBeforeRequest(details)
{
- let panel = panels[details.tabId];
+ let panel = panels.get(details.tabId);
// Clear the devtools panel and reload the inspected tab without caching
// when a new request is issued. However, make sure that we don't end up
// in an infinite recursion if we already triggered a reload.
if (panel.reloading)
{
panel.reloading = false;
}
@@ -238,37 +252,35 @@
if (details.method == "GET")
panel.reload = true;
}
}
function onLoading(page)
{
let tabId = page.id;
- let panel = panels[tabId];
+ let panel = panels.get(tabId);
// Reloading the tab is the only way that allows bypassing all caches, in
// order to see all requests in the devtools panel. Reloading must not be
// performed before the tab changes to "loading", otherwise it will load the
// previous URL.
if (panel && panel.reload)
{
chrome.tabs.reload(tabId, {bypassCache: true});
panel.reload = false;
panel.reloading = true;
}
}
function updateFilters(filters, added)
{
- for (let tabId in panels)
+ for (let panel of panels.values())
{
- let panel = panels[tabId];
-
for (let i = 0; i < panel.records.length; i++)
{
let record = panel.records[i];
// If an added filter matches a request shown in the devtools panel,
// update that record to show the new filter. Ignore filters that aren't
// associated with any sub-resource request. There is no record for these
// if they don't already match. In particular, in case of element hiding
@@ -357,29 +369,29 @@
ext.pages.onLoading.addListener(onLoading);
FilterNotifier.on("filter.added", onFilterAdded);
FilterNotifier.on("filter.removed", onFilterRemoved);
FilterNotifier.on("subscription.added", onSubscriptionAdded);
}
newPort.onDisconnect.addListener(() =>
{
- delete panels[inspectedTabId];
+ delete panels.delete(inspectedTabId);
chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
if (!hasPanels())
{
ext.pages.onLoading.removeListener(onLoading);
FilterNotifier.off("filter.added", onFilterAdded);
FilterNotifier.off("filter.removed", onFilterRemoved);
FilterNotifier.off("subscription.added", onSubscriptionAdded);
}
});
- panels[inspectedTabId] = {port: newPort, records: []};
+ panels.set(inspectedTabId, {port: newPort, records: []});
});
port.on("devtools.traceElemHide", (message, sender) =>
{
logHiddenElements(
sender.page, message.selectors, message.filters,
extractHostFromFrame(sender.frame)
);
« no previous file with comments | « ext/background.js ('k') | lib/requestBlocker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld