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

Side by Side Diff: lib/stats.js

Issue 29996582: Issue 7257 - Throttle badge updates (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Move Scheduler to lib/stats.js Created Feb. 4, 2019, 5:21 a.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 | « no previous file | no next file » | 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-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;
31
28 let blockedPerPage = new ext.PageMap(); 32 let blockedPerPage = new ext.PageMap();
29 33
30 let getBlockedPerPage = 34 let getBlockedPerPage =
31 /** 35 /**
32 * Gets the number of requests blocked on the given page. 36 * Gets the number of requests blocked on the given page.
33 * 37 *
34 * @param {Page} page 38 * @param {Page} page
35 * @return {Number} 39 * @return {Number}
36 */ 40 */
37 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; 41 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0;
38 42
39 function updateBadge(page, blockedCount) 43 // The ID of the focused window. If all windows lose focus, this is the ID of
44 // the last focused window.
45 let focusedWindowId = -1;
46
47 // The ID of the active tab in the focused window. This can be unknown (-1) on
48 // startup and (briefly) when the focus shifts to a different window.
49 let activeTabId = -1;
50
51 let Scheduler = function()
40 { 52 {
41 if (Prefs.show_statsinicon) 53 this.scheduled = false;
54 };
55 Scheduler.prototype = {
56 schedule(delay, func)
42 { 57 {
43 page.browserAction.setBadge(blockedCount && { 58 setTimeout(() =>
44 color: badgeColor, 59 {
45 number: blockedCount 60 this.scheduled = false;
46 }); 61 func();
62 },
63 delay);
64
65 this.scheduled = true;
47 } 66 }
67 };
68
69 let badgeUpdateScheduler = new Scheduler();
70
71 function updateBadgeNow()
72 {
73 if (activeTabId == -1)
74 return;
75
76 let page = new ext.Page({id: activeTabId});
77 let blockedCount = blockedPerPage.get(page);
78
79 page.browserAction.setBadge(blockedCount && {
80 color: badgeColor,
81 number: blockedCount
82 });
83 }
84
85 function updateBadge(tabId = -1)
86 {
87 if (!badgeUpdateScheduler.scheduled &&
88 (tabId == activeTabId || tabId == -1) && activeTabId != -1 &&
89 Prefs.show_statsinicon)
90 {
91 // Schedule an update.
92 badgeUpdateScheduler.schedule(1000 / badgeRefreshRate, updateBadgeNow);
93 }
94 }
95
96 function findAndUpdateActiveTab()
97 {
98 browser.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) =>
99 {
100 if (tab && activeTabId != tab.id)
101 {
102 activeTabId = tab.id;
103 focusedWindowId = tab.windowId;
104
105 updateBadge();
106 }
107 });
48 } 108 }
49 109
50 // Once nagivation for the tab has been committed to (e.g. it's no longer 110 // Once nagivation for the tab has been committed to (e.g. it's no longer
51 // being prerendered) we clear its badge, or if some requests were already 111 // being prerendered) we clear its badge, or if some requests were already
52 // blocked beforehand we display those on the badge now. 112 // blocked beforehand we display those on the badge now.
53 browser.webNavigation.onCommitted.addListener(details => 113 browser.webNavigation.onCommitted.addListener(details =>
54 { 114 {
55 if (details.frameId == 0) 115 if (details.frameId == 0)
56 { 116 updateBadge(details.tabId);
57 let page = new ext.Page({id: details.tabId});
58 let blocked = blockedPerPage.get(page);
59
60 updateBadge(page, blocked);
61 }
62 }); 117 });
63 118
64 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => 119 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) =>
65 { 120 {
66 if (!(filter instanceof BlockingFilter)) 121 if (!(filter instanceof BlockingFilter))
67 return; 122 return;
68 123
69 for (let tabId of tabIds) 124 for (let tabId of tabIds)
70 { 125 {
71 let page = new ext.Page({id: tabId}); 126 let page = new ext.Page({id: tabId});
72 let blocked = blockedPerPage.get(page) || 0; 127 let blocked = blockedPerPage.get(page) || 0;
73 128
74 blockedPerPage.set(page, ++blocked); 129 blockedPerPage.set(page, ++blocked);
75 updateBadge(page, blocked); 130 updateBadge(tabId);
76 } 131 }
77 132
78 Prefs.blocked_total++; 133 Prefs.blocked_total++;
79 }); 134 });
80 135
81 Prefs.on("show_statsinicon", () => 136 Prefs.on("show_statsinicon", () =>
82 { 137 {
83 browser.tabs.query({}, tabs => 138 browser.tabs.query({}, tabs =>
84 { 139 {
85 for (let tab of tabs) 140 for (let tab of tabs)
86 { 141 {
87 let page = new ext.Page(tab); 142 let page = new ext.Page(tab);
88 143
89 if (Prefs.show_statsinicon) 144 if (Prefs.show_statsinicon)
90 updateBadge(page, blockedPerPage.get(page)); 145 updateBadge(tab.id);
91 else 146 else
92 page.browserAction.setBadge(null); 147 page.browserAction.setBadge(null);
93 } 148 }
94 }); 149 });
95 }); 150 });
96 151
97 port.on("stats.getBlockedPerPage", 152 port.on("stats.getBlockedPerPage",
98 message => getBlockedPerPage(new ext.Page(message.tab))); 153 message => getBlockedPerPage(new ext.Page(message.tab)));
154
155 browser.tabs.onActivated.addListener(tab =>
156 {
157 if (tab.windowId == focusedWindowId && activeTabId != tab.tabId)
158 {
159 activeTabId = tab.tabId;
160 updateBadge();
161 }
162 });
163
164 if ("windows" in browser)
165 {
166 browser.windows.onFocusChanged.addListener(windowId =>
167 {
168 if (windowId == browser.windows.WINDOW_ID_NONE)
169 return;
170
171 focusedWindowId = windowId;
172 activeTabId = -1;
173
174 findAndUpdateActiveTab();
175 });
176 }
177
178 findAndUpdateActiveTab();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld