LEFT | RIGHT |
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; |
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 Scheduler = function() | 46 function updateBadge(tabId) |
52 { | 47 { |
53 this.scheduled = false; | 48 if (!Prefs.show_statsinicon) |
54 }; | |
55 Scheduler.prototype = { | |
56 schedule(delay, func) | |
57 { | |
58 setTimeout(() => | |
59 { | |
60 this.scheduled = false; | |
61 func(); | |
62 }, | |
63 delay); | |
64 | |
65 this.scheduled = true; | |
66 } | |
67 }; | |
68 | |
69 let badgeUpdateScheduler = new Scheduler(); | |
70 | |
71 function updateBadgeNow() | |
72 { | |
73 if (activeTabId == -1) | |
74 return; | 49 return; |
75 | 50 |
76 let page = new ext.Page({id: activeTabId}); | 51 for (let id of (typeof tabId == "undefined" ? activeTabIds : [tabId])) |
77 let blockedCount = blockedPerPage.get(page); | 52 { |
| 53 let page = new ext.Page({id}); |
| 54 let blockedCount = blockedPerPage.get(page); |
78 | 55 |
79 page.browserAction.setBadge(blockedCount && { | 56 page.browserAction.setBadge(blockedCount && { |
80 color: badgeColor, | 57 color: badgeColor, |
81 number: blockedCount | 58 number: blockedCount |
82 }); | 59 }); |
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 } | 60 } |
94 } | 61 } |
95 | 62 |
96 function findAndUpdateActiveTab() | 63 function scheduleBadgeUpdate(tabId) |
97 { | 64 { |
98 browser.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => | 65 if (!badgeUpdateScheduled && Prefs.show_statsinicon && |
| 66 (typeof tabId == "undefined" || activeTabIds.has(tabId))) |
99 { | 67 { |
100 if (tab && activeTabId != tab.id) | 68 setTimeout(() => { badgeUpdateScheduled = false; updateBadge(); }, |
101 { | 69 1000 / badgeRefreshRate); |
102 activeTabId = tab.id; | 70 badgeUpdateScheduled = true; |
103 focusedWindowId = tab.windowId; | 71 } |
104 | |
105 updateBadge(); | |
106 } | |
107 }); | |
108 } | 72 } |
109 | 73 |
110 // 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 |
111 // 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 |
112 // blocked beforehand we display those on the badge now. | 76 // blocked beforehand we display those on the badge now. |
113 browser.webNavigation.onCommitted.addListener(details => | 77 browser.webNavigation.onCommitted.addListener(details => |
114 { | 78 { |
115 if (details.frameId == 0) | 79 if (details.frameId == 0) |
116 updateBadge(details.tabId); | 80 updateBadge(details.tabId); |
117 }); | 81 }); |
118 | 82 |
119 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => | 83 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => |
120 { | 84 { |
121 if (!(filter instanceof BlockingFilter)) | 85 if (!(filter instanceof BlockingFilter)) |
122 return; | 86 return; |
123 | 87 |
124 for (let tabId of tabIds) | 88 for (let tabId of tabIds) |
125 { | 89 { |
126 let page = new ext.Page({id: tabId}); | 90 let page = new ext.Page({id: tabId}); |
127 let blocked = blockedPerPage.get(page) || 0; | 91 let blocked = blockedPerPage.get(page) || 0; |
128 | 92 |
129 blockedPerPage.set(page, ++blocked); | 93 blockedPerPage.set(page, ++blocked); |
130 updateBadge(tabId); | 94 scheduleBadgeUpdate(tabId); |
131 } | 95 } |
132 | 96 |
133 Prefs.blocked_total++; | 97 Prefs.blocked_total++; |
134 }); | 98 }); |
135 | 99 |
136 Prefs.on("show_statsinicon", () => | 100 Prefs.on("show_statsinicon", () => |
137 { | 101 { |
138 browser.tabs.query({}, tabs => | 102 browser.tabs.query({}, tabs => |
139 { | 103 { |
140 for (let tab of tabs) | 104 for (let tab of tabs) |
141 { | 105 { |
142 let page = new ext.Page(tab); | 106 let page = new ext.Page(tab); |
143 | 107 |
144 if (Prefs.show_statsinicon) | 108 if (Prefs.show_statsinicon) |
145 updateBadge(tab.id); | 109 updateBadge(tab.id); |
146 else | 110 else |
147 page.browserAction.setBadge(null); | 111 page.browserAction.setBadge(null); |
148 } | 112 } |
149 }); | 113 }); |
150 }); | 114 }); |
151 | 115 |
152 port.on("stats.getBlockedPerPage", | 116 port.on("stats.getBlockedPerPage", |
153 message => getBlockedPerPage(new ext.Page(message.tab))); | 117 message => getBlockedPerPage(new ext.Page(message.tab))); |
154 | 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 |
155 browser.tabs.onActivated.addListener(tab => | 130 browser.tabs.onActivated.addListener(tab => |
156 { | 131 { |
157 if (tab.windowId == focusedWindowId && activeTabId != tab.tabId) | 132 let lastActiveTabId = activeTabIdByWindowId.get(tab.windowId); |
158 { | 133 if (typeof lastActiveTabId != "undefined") |
159 activeTabId = tab.tabId; | 134 activeTabIds.delete(lastActiveTabId); |
160 updateBadge(); | 135 |
161 } | 136 activeTabIds.add(tab.tabId); |
| 137 activeTabIdByWindowId.set(tab.windowId, tab.tabId); |
| 138 |
| 139 scheduleBadgeUpdate(); |
162 }); | 140 }); |
163 | 141 |
164 if ("windows" in browser) | 142 if ("windows" in browser) |
165 { | 143 { |
166 browser.windows.onFocusChanged.addListener(windowId => | 144 browser.windows.onRemoved.addListener(windowId => |
167 { | 145 { |
168 if (windowId == browser.windows.WINDOW_ID_NONE) | 146 activeTabIds.delete(activeTabIdByWindowId.get(windowId)); |
169 return; | 147 activeTabIdByWindowId.delete(windowId); |
170 | |
171 focusedWindowId = windowId; | |
172 activeTabId = -1; | |
173 | |
174 findAndUpdateActiveTab(); | |
175 }); | 148 }); |
176 } | 149 } |
177 | |
178 findAndUpdateActiveTab(); | |
LEFT | RIGHT |