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: 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:
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 const badgeRefreshRate = 4;
29
28 let blockedPerPage = new ext.PageMap(); 30 let blockedPerPage = new ext.PageMap();
29 31
30 let getBlockedPerPage = 32 let getBlockedPerPage =
31 /** 33 /**
32 * Gets the number of requests blocked on the given page. 34 * Gets the number of requests blocked on the given page.
33 * 35 *
34 * @param {Page} page 36 * @param {Page} page
35 * @return {Number} 37 * @return {Number}
36 */ 38 */
37 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; 39 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0;
38 40
39 function updateBadge(page, blockedCount) 41 let activeTabIds = new Set();
42 let activeTabIdByWindowId = new Map();
43
44 let badgeUpdateScheduled = false;
45
46 function updateBadge(tabId)
40 { 47 {
41 if (Prefs.show_statsinicon) 48 if (!Prefs.show_statsinicon)
49 return;
50
51 for (let id of (typeof tabId == "undefined" ? activeTabIds : [tabId]))
42 { 52 {
53 let page = new ext.Page({id});
54 let blockedCount = blockedPerPage.get(page);
55
43 page.browserAction.setBadge(blockedCount && { 56 page.browserAction.setBadge(blockedCount && {
44 color: badgeColor, 57 color: badgeColor,
45 number: blockedCount 58 number: blockedCount
46 }); 59 });
47 } 60 }
48 } 61 }
49 62
63 function scheduleBadgeUpdate(tabId)
64 {
65 if (!badgeUpdateScheduled && Prefs.show_statsinicon &&
66 (typeof tabId == "undefined" || activeTabIds.has(tabId)))
67 {
68 setTimeout(() => { badgeUpdateScheduled = false; updateBadge(); },
69 1000 / badgeRefreshRate);
70 badgeUpdateScheduled = true;
71 }
72 }
73
50 // 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
51 // 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
52 // blocked beforehand we display those on the badge now. 76 // blocked beforehand we display those on the badge now.
53 browser.webNavigation.onCommitted.addListener(details => 77 browser.webNavigation.onCommitted.addListener(details =>
54 { 78 {
55 if (details.frameId == 0) 79 if (details.frameId == 0)
56 { 80 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 }); 81 });
63 82
64 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => 83 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) =>
65 { 84 {
66 if (!(filter instanceof BlockingFilter)) 85 if (!(filter instanceof BlockingFilter))
67 return; 86 return;
68 87
69 for (let tabId of tabIds) 88 for (let tabId of tabIds)
70 { 89 {
71 let page = new ext.Page({id: tabId}); 90 let page = new ext.Page({id: tabId});
72 let blocked = blockedPerPage.get(page) || 0; 91 let blocked = blockedPerPage.get(page) || 0;
73 92
74 blockedPerPage.set(page, ++blocked); 93 blockedPerPage.set(page, ++blocked);
75 updateBadge(page, blocked); 94 scheduleBadgeUpdate(tabId);
76 } 95 }
77 96
78 Prefs.blocked_total++; 97 Prefs.blocked_total++;
79 }); 98 });
80 99
81 Prefs.on("show_statsinicon", () => 100 Prefs.on("show_statsinicon", () =>
82 { 101 {
83 browser.tabs.query({}, tabs => 102 browser.tabs.query({}, tabs =>
84 { 103 {
85 for (let tab of tabs) 104 for (let tab of tabs)
86 { 105 {
87 let page = new ext.Page(tab); 106 let page = new ext.Page(tab);
88 107
89 if (Prefs.show_statsinicon) 108 if (Prefs.show_statsinicon)
90 updateBadge(page, blockedPerPage.get(page)); 109 updateBadge(tab.id);
91 else 110 else
92 page.browserAction.setBadge(null); 111 page.browserAction.setBadge(null);
93 } 112 }
94 }); 113 });
95 }); 114 });
96 115
97 port.on("stats.getBlockedPerPage", 116 port.on("stats.getBlockedPerPage",
98 message => getBlockedPerPage(new ext.Page(message.tab))); 117 message => getBlockedPerPage(new ext.Page(message.tab)));
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
130 browser.tabs.onActivated.addListener(tab =>
131 {
132 let lastActiveTabId = activeTabIdByWindowId.get(tab.windowId);
133 if (typeof lastActiveTabId != "undefined")
134 activeTabIds.delete(lastActiveTabId);
135
136 activeTabIds.add(tab.tabId);
137 activeTabIdByWindowId.set(tab.windowId, tab.tabId);
138
139 scheduleBadgeUpdate();
140 });
141
142 if ("windows" in browser)
143 {
144 browser.windows.onRemoved.addListener(windowId =>
145 {
146 activeTabIds.delete(activeTabIdByWindowId.get(windowId));
147 activeTabIdByWindowId.delete(windowId);
148 });
149 }
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