Left: | ||
Right: |
OLD | NEW |
---|---|
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; | |
Manish Jethani
2019/02/03 13:20:21
I found this to be a reasonable refresh rate, YMMV
| |
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 badgeUpdateScheduler = new ext.Scheduler(); | |
52 | |
53 function updateBadgeNow() | |
40 { | 54 { |
41 if (Prefs.show_statsinicon) | 55 if (activeTabId == -1) |
56 return; | |
57 | |
58 let page = new ext.Page({id: activeTabId}); | |
59 let blockedCount = blockedPerPage.get(page); | |
60 | |
61 page.browserAction.setBadge(blockedCount && { | |
62 color: badgeColor, | |
63 number: blockedCount | |
64 }); | |
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) | |
42 { | 72 { |
43 page.browserAction.setBadge(blockedCount && { | 73 // Schedule an update. |
44 color: badgeColor, | 74 badgeUpdateScheduler.schedule(1000 / badgeRefreshRate, updateBadgeNow); |
45 number: blockedCount | |
46 }); | |
47 } | 75 } |
48 } | 76 } |
49 | 77 |
78 function findAndUpdateActiveTab() | |
79 { | |
80 browser.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => | |
81 { | |
82 if (tab && activeTabId != tab.id) | |
83 { | |
84 activeTabId = tab.id; | |
85 focusedWindowId = tab.windowId; | |
86 | |
87 updateBadge(); | |
88 } | |
89 }); | |
90 } | |
91 | |
50 // Once nagivation for the tab has been committed to (e.g. it's no longer | 92 // 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 | 93 // being prerendered) we clear its badge, or if some requests were already |
52 // blocked beforehand we display those on the badge now. | 94 // blocked beforehand we display those on the badge now. |
53 browser.webNavigation.onCommitted.addListener(details => | 95 browser.webNavigation.onCommitted.addListener(details => |
54 { | 96 { |
55 if (details.frameId == 0) | 97 if (details.frameId == 0) |
56 { | 98 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 }); | 99 }); |
63 | 100 |
64 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => | 101 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => |
65 { | 102 { |
66 if (!(filter instanceof BlockingFilter)) | 103 if (!(filter instanceof BlockingFilter)) |
67 return; | 104 return; |
68 | 105 |
69 for (let tabId of tabIds) | 106 for (let tabId of tabIds) |
70 { | 107 { |
71 let page = new ext.Page({id: tabId}); | 108 let page = new ext.Page({id: tabId}); |
72 let blocked = blockedPerPage.get(page) || 0; | 109 let blocked = blockedPerPage.get(page) || 0; |
73 | 110 |
74 blockedPerPage.set(page, ++blocked); | 111 blockedPerPage.set(page, ++blocked); |
75 updateBadge(page, blocked); | 112 updateBadge(tabId); |
76 } | 113 } |
77 | 114 |
78 Prefs.blocked_total++; | 115 Prefs.blocked_total++; |
79 }); | 116 }); |
80 | 117 |
81 Prefs.on("show_statsinicon", () => | 118 Prefs.on("show_statsinicon", () => |
82 { | 119 { |
83 browser.tabs.query({}, tabs => | 120 browser.tabs.query({}, tabs => |
84 { | 121 { |
85 for (let tab of tabs) | 122 for (let tab of tabs) |
86 { | 123 { |
87 let page = new ext.Page(tab); | 124 let page = new ext.Page(tab); |
88 | 125 |
89 if (Prefs.show_statsinicon) | 126 if (Prefs.show_statsinicon) |
90 updateBadge(page, blockedPerPage.get(page)); | 127 updateBadge(tab.id); |
91 else | 128 else |
92 page.browserAction.setBadge(null); | 129 page.browserAction.setBadge(null); |
93 } | 130 } |
94 }); | 131 }); |
95 }); | 132 }); |
96 | 133 |
97 port.on("stats.getBlockedPerPage", | 134 port.on("stats.getBlockedPerPage", |
98 message => getBlockedPerPage(new ext.Page(message.tab))); | 135 message => getBlockedPerPage(new ext.Page(message.tab))); |
136 | |
137 browser.tabs.onActivated.addListener(tab => | |
138 { | |
139 if (tab.windowId == focusedWindowId && activeTabId != tab.tabId) | |
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 { | |
141 activeTabId = tab.tabId; | |
142 updateBadge(); | |
143 } | |
144 }); | |
145 | |
146 if ("windows" in browser) | |
147 { | |
148 browser.windows.onFocusChanged.addListener(windowId => | |
149 { | |
150 if (windowId == browser.windows.WINDOW_ID_NONE) | |
Manish Jethani
2019/02/03 13:20:21
Make sure focusedWindowId always points to a valid
| |
151 return; | |
152 | |
153 focusedWindowId = windowId; | |
154 activeTabId = -1; | |
155 | |
156 findAndUpdateActiveTab(); | |
157 }); | |
158 } | |
159 | |
160 findAndUpdateActiveTab(); | |
OLD | NEW |