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

Unified Diff: lib/stats.js

Issue 29996582: Issue 7257 - Throttle badge updates (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Alternative implementation, update all active tabs Created Feb. 4, 2019, 6:47 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/stats.js
===================================================================
--- a/lib/stats.js
+++ b/lib/stats.js
@@ -20,79 +20,132 @@
"use strict";
const {Prefs} = require("./prefs");
const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses");
const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
const {port} = require("./messaging");
const badgeColor = "#646464";
+
+// 4 fps.
Sebastian Noack 2019/02/04 07:16:41 Nit: This comment seems somewhat redundant. Perhap
Manish Jethani 2019/02/04 07:36:14 I think it's fine to just remove the comment. Don
+const badgeRefreshRate = 4;
+
let blockedPerPage = new ext.PageMap();
let getBlockedPerPage =
/**
* Gets the number of requests blocked on the given page.
*
* @param {Page} page
* @return {Number}
*/
exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0;
-function updateBadge(page, blockedCount)
+let activeTabIds = new Set();
+let activeTabIdByWindowId = new Map();
+
+let badgeUpdateScheduled = false;
+
+function updateBadge(tabId = -1)
Sebastian Noack 2019/02/04 07:16:41 Nit: What would you think about not setting a defa
Manish Jethani 2019/02/04 07:36:14 Works for me, done.
{
- if (Prefs.show_statsinicon)
+ if (!Prefs.show_statsinicon)
+ return;
Sebastian Noack 2019/02/04 07:16:41 The only purpose of the check here seems to be avo
Manish Jethani 2019/02/04 07:36:14 updateBadge is also called directly, not necessari
+
+ for (let id of (tabId == -1 ? activeTabIds : [tabId]))
{
+ let page = new ext.Page({id});
+ let blockedCount = blockedPerPage.get(page);
+
page.browserAction.setBadge(blockedCount && {
color: badgeColor,
number: blockedCount
});
}
}
+function scheduleBadgeUpdate(tabId = -1)
+{
+ if (!badgeUpdateScheduled && Prefs.show_statsinicon &&
+ (tabId == -1 || activeTabIds.has(tabId)))
+ {
+ setTimeout(() => { badgeUpdateScheduled = false; updateBadge(); },
+ 1000 / badgeRefreshRate);
+ badgeUpdateScheduled = true;
+ }
+}
+
// Once nagivation for the tab has been committed to (e.g. it's no longer
// being prerendered) we clear its badge, or if some requests were already
// blocked beforehand we display those on the badge now.
browser.webNavigation.onCommitted.addListener(details =>
{
if (details.frameId == 0)
- {
- let page = new ext.Page({id: details.tabId});
- let blocked = blockedPerPage.get(page);
-
- updateBadge(page, blocked);
- }
+ updateBadge(details.tabId);
});
filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) =>
{
if (!(filter instanceof BlockingFilter))
return;
for (let tabId of tabIds)
{
let page = new ext.Page({id: tabId});
let blocked = blockedPerPage.get(page) || 0;
blockedPerPage.set(page, ++blocked);
- updateBadge(page, blocked);
+ scheduleBadgeUpdate(tabId);
}
Prefs.blocked_total++;
});
Prefs.on("show_statsinicon", () =>
{
browser.tabs.query({}, tabs =>
{
for (let tab of tabs)
{
let page = new ext.Page(tab);
if (Prefs.show_statsinicon)
- updateBadge(page, blockedPerPage.get(page));
+ updateBadge(tab.id);
else
page.browserAction.setBadge(null);
}
});
});
port.on("stats.getBlockedPerPage",
message => getBlockedPerPage(new ext.Page(message.tab)));
+
+browser.tabs.query({active: true}, tabs =>
+{
+ for (let tab of tabs)
+ {
+ activeTabIds.add(tab.id);
+ activeTabIdByWindowId.set(tab.windowId, tab.id);
+ }
+
+ scheduleBadgeUpdate();
+});
+
+browser.tabs.onActivated.addListener(tab =>
+{
+ let lastActiveTabId = activeTabIdByWindowId.get(tab.windowId);
+ if (typeof lastActiveTabId != "undefined")
+ activeTabIds.delete(lastActiveTabId);
+
+ activeTabIds.add(tab.tabId);
+ activeTabIdByWindowId.set(tab.windowId, tab.tabId);
+
+ scheduleBadgeUpdate();
+});
+
+if ("windows" in browser)
+{
+ browser.windows.onRemoved.addListener(windowId =>
+ {
+ activeTabIds.delete(activeTabIdByWindowId.get(windowId));
+ activeTabIdByWindowId.delete(windowId);
+ });
+}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld