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

Delta Between Two Patch Sets: lib/stats.js

Issue 29996582: Issue 7257 - Throttle badge updates (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created Feb. 3, 2019, 6:14 a.m.
Right Patch Set: Check for undefined Created Feb. 4, 2019, 7:33 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 | « no previous file | no next file » | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module stats */ 18 /** @module stats */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {Prefs} = require("./prefs"); 22 const {Prefs} = require("./prefs");
23 const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses"); 23 const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses");
24 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 24 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
25 const {port} = require("./messaging"); 25 const {port} = require("./messaging");
26 26
27 const badgeColor = "#646464"; 27 const badgeColor = "#646464";
28
29 // 4 fps.
30 const badgeRefreshRate = 4; 28 const badgeRefreshRate = 4;
Manish Jethani 2019/02/03 13:20:21 I found this to be a reasonable refresh rate, YMMV
31 29
32 let blockedPerPage = new ext.PageMap(); 30 let blockedPerPage = new ext.PageMap();
33 31
34 let getBlockedPerPage = 32 let getBlockedPerPage =
35 /** 33 /**
36 * Gets the number of requests blocked on the given page. 34 * Gets the number of requests blocked on the given page.
37 * 35 *
38 * @param {Page} page 36 * @param {Page} page
39 * @return {Number} 37 * @return {Number}
40 */ 38 */
41 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; 39 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0;
42 40
43 // The ID of the focused window. If all windows lose focus, this is the ID of 41 let activeTabIds = new Set();
44 // the last focused window. 42 let activeTabIdByWindowId = new Map();
45 let focusedWindowId = -1;
46 43
47 // The ID of the active tab in the focused window. This can be unknown (-1) on 44 let badgeUpdateScheduled = false;
48 // startup and (briefly) when the focus shifts to a different window.
49 let activeTabId = -1;
50 45
51 let badgeUpdateScheduler = new ext.Scheduler(); 46 function updateBadge(tabId)
52
53 function updateBadgeNow()
54 { 47 {
55 if (activeTabId == -1) 48 if (!Prefs.show_statsinicon)
56 return; 49 return;
57 50
58 let page = new ext.Page({id: activeTabId}); 51 for (let id of (typeof tabId == "undefined" ? activeTabIds : [tabId]))
59 let blockedCount = blockedPerPage.get(page); 52 {
53 let page = new ext.Page({id});
54 let blockedCount = blockedPerPage.get(page);
60 55
61 page.browserAction.setBadge(blockedCount && { 56 page.browserAction.setBadge(blockedCount && {
62 color: badgeColor, 57 color: badgeColor,
63 number: blockedCount 58 number: blockedCount
64 }); 59 });
65 }
66
67 function updateBadge(tabId = -1)
68 {
69 if (!badgeUpdateScheduler.scheduled &&
Manish Jethani 2019/02/03 13:20:21 The conditions are ordered from most likely to lea
70 (tabId == activeTabId || tabId == -1) && activeTabId != -1 &&
71 Prefs.show_statsinicon)
72 {
73 // Schedule an update.
74 badgeUpdateScheduler.schedule(1000 / badgeRefreshRate, updateBadgeNow);
75 } 60 }
76 } 61 }
77 62
78 function findAndUpdateActiveTab() 63 function scheduleBadgeUpdate(tabId)
79 { 64 {
80 browser.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => 65 if (!badgeUpdateScheduled && Prefs.show_statsinicon &&
66 (typeof tabId == "undefined" || activeTabIds.has(tabId)))
81 { 67 {
82 if (tab && activeTabId != tab.id) 68 setTimeout(() => { badgeUpdateScheduled = false; updateBadge(); },
83 { 69 1000 / badgeRefreshRate);
84 activeTabId = tab.id; 70 badgeUpdateScheduled = true;
85 focusedWindowId = tab.windowId; 71 }
86
87 updateBadge();
88 }
89 });
90 } 72 }
91 73
92 // Once nagivation for the tab has been committed to (e.g. it's no longer 74 // Once nagivation for the tab has been committed to (e.g. it's no longer
93 // being prerendered) we clear its badge, or if some requests were already 75 // being prerendered) we clear its badge, or if some requests were already
94 // blocked beforehand we display those on the badge now. 76 // blocked beforehand we display those on the badge now.
95 browser.webNavigation.onCommitted.addListener(details => 77 browser.webNavigation.onCommitted.addListener(details =>
96 { 78 {
97 if (details.frameId == 0) 79 if (details.frameId == 0)
98 updateBadge(details.tabId); 80 updateBadge(details.tabId);
99 }); 81 });
100 82
101 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => 83 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) =>
102 { 84 {
103 if (!(filter instanceof BlockingFilter)) 85 if (!(filter instanceof BlockingFilter))
104 return; 86 return;
105 87
106 for (let tabId of tabIds) 88 for (let tabId of tabIds)
107 { 89 {
108 let page = new ext.Page({id: tabId}); 90 let page = new ext.Page({id: tabId});
109 let blocked = blockedPerPage.get(page) || 0; 91 let blocked = blockedPerPage.get(page) || 0;
110 92
111 blockedPerPage.set(page, ++blocked); 93 blockedPerPage.set(page, ++blocked);
112 updateBadge(tabId); 94 scheduleBadgeUpdate(tabId);
113 } 95 }
114 96
115 Prefs.blocked_total++; 97 Prefs.blocked_total++;
116 }); 98 });
117 99
118 Prefs.on("show_statsinicon", () => 100 Prefs.on("show_statsinicon", () =>
119 { 101 {
120 browser.tabs.query({}, tabs => 102 browser.tabs.query({}, tabs =>
121 { 103 {
122 for (let tab of tabs) 104 for (let tab of tabs)
123 { 105 {
124 let page = new ext.Page(tab); 106 let page = new ext.Page(tab);
125 107
126 if (Prefs.show_statsinicon) 108 if (Prefs.show_statsinicon)
127 updateBadge(tab.id); 109 updateBadge(tab.id);
128 else 110 else
129 page.browserAction.setBadge(null); 111 page.browserAction.setBadge(null);
130 } 112 }
131 }); 113 });
132 }); 114 });
133 115
134 port.on("stats.getBlockedPerPage", 116 port.on("stats.getBlockedPerPage",
135 message => getBlockedPerPage(new ext.Page(message.tab))); 117 message => getBlockedPerPage(new ext.Page(message.tab)));
136 118
119 browser.tabs.query({active: true}, tabs =>
120 {
121 for (let tab of tabs)
122 {
123 activeTabIds.add(tab.id);
124 activeTabIdByWindowId.set(tab.windowId, tab.id);
125 }
126
127 scheduleBadgeUpdate();
128 });
129
137 browser.tabs.onActivated.addListener(tab => 130 browser.tabs.onActivated.addListener(tab =>
138 { 131 {
139 if (tab.windowId == focusedWindowId && activeTabId != tab.tabId) 132 let lastActiveTabId = activeTabIdByWindowId.get(tab.windowId);
Manish Jethani 2019/02/03 13:20:21 We care about only the focused window, not other w
Sebastian Noack 2019/02/03 23:18:03 But it's possible that several windows showing the
Manish Jethani 2019/02/04 05:33:04 Yes, that's possible, and in that case only the nu
Sebastian Noack 2019/02/04 06:32:04 I think my case is at least not any less common th
Manish Jethani 2019/02/04 06:57:17 Yes, but those are two different things. Windows i
140 { 133 if (typeof lastActiveTabId != "undefined")
141 activeTabId = tab.tabId; 134 activeTabIds.delete(lastActiveTabId);
142 updateBadge(); 135
143 } 136 activeTabIds.add(tab.tabId);
137 activeTabIdByWindowId.set(tab.windowId, tab.tabId);
138
139 scheduleBadgeUpdate();
144 }); 140 });
145 141
146 if ("windows" in browser) 142 if ("windows" in browser)
147 { 143 {
148 browser.windows.onFocusChanged.addListener(windowId => 144 browser.windows.onRemoved.addListener(windowId =>
149 { 145 {
150 if (windowId == browser.windows.WINDOW_ID_NONE) 146 activeTabIds.delete(activeTabIdByWindowId.get(windowId));
Manish Jethani 2019/02/03 13:20:21 Make sure focusedWindowId always points to a valid
151 return; 147 activeTabIdByWindowId.delete(windowId);
152
153 focusedWindowId = windowId;
154 activeTabId = -1;
155
156 findAndUpdateActiveTab();
157 }); 148 });
158 } 149 }
159
160 findAndUpdateActiveTab();
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld