| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 /** | 18 /** |
| 19 * @fileOverview Provides usage stats | 19 * @fileOverview Provides usage stats |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 let {Prefs} = require("prefs"); | 22 let {Prefs} = require("prefs"); |
| 23 let {BlockingFilter} = require("filterClasses"); | 23 let {BlockingFilter} = require("filterClasses"); |
| 24 let {FilterNotifier} = require("filterNotifier"); | 24 let {FilterNotifier} = require("filterNotifier"); |
| 25 | 25 |
| 26 let badgeColor = "#646464"; | 26 let badgeColor = "#646464"; |
| 27 let statsPerTab = new TabMap(true); | 27 let statsPerPage = new ext.PageMap(); |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Get statistics for specified tab | 30 * Get statistics for specified page |
| 31 * @param {String} key field key | 31 * @param {String} key field key |
| 32 * @param {Number} tabId tab ID (leave undefined for total stats) | 32 * @param {Page} page field page |
| 33 * @return {Number} field value | 33 * @return {Number} field value |
| 34 */ | 34 */ |
| 35 let getStats = exports.getStats = function getStats(key, tab) | 35 let getStats = exports.getStats = function getStats(key, page) |
| 36 { | 36 { |
| 37 if (!tab) | 37 if (!page) |
| 38 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); | 38 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); |
| 39 | 39 |
| 40 let tabStats = statsPerTab.get(tab); | 40 let pageStats = statsPerPage.get(page); |
| 41 return tabStats ? tabStats.blocked : 0; | 41 return pageStats ? pageStats.blocked : 0; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab) | 44 FilterNotifier.addListener(function(action, item, newValue, oldValue, page) |
| 45 { | 45 { |
| 46 if (action != "filter.hitCount" || !tab) | 46 if (action != "filter.hitCount" || !page) |
| 47 return; | 47 return; |
| 48 | 48 |
| 49 let blocked = item instanceof BlockingFilter; | 49 let blocked = item instanceof BlockingFilter; |
| 50 | 50 |
| 51 // Increment counts | 51 // Increment counts |
| 52 if (blocked) | 52 if (blocked) |
| 53 { | 53 { |
| 54 if ("blocked" in Prefs.stats_total) | 54 if ("blocked" in Prefs.stats_total) |
| 55 Prefs.stats_total.blocked++; | 55 Prefs.stats_total.blocked++; |
| 56 else | 56 else |
| 57 Prefs.stats_total.blocked = 1; | 57 Prefs.stats_total.blocked = 1; |
| 58 Prefs.stats_total = Prefs.stats_total; | 58 Prefs.stats_total = Prefs.stats_total; |
| 59 | 59 |
| 60 let tabStats = statsPerTab.get(tab); | 60 let pageStats = statsPerPage.get(page); |
| 61 if (!tabStats) | 61 if (!pageStats) |
| 62 { | 62 { |
| 63 tabStats = {}; | 63 pageStats = {}; |
| 64 statsPerTab.set(tab, tabStats); | 64 statsPerPage.set(page, pageStats); |
| 65 } | 65 } |
| 66 if ("blocked" in tabStats) | 66 if ("blocked" in pageStats) |
| 67 tabStats.blocked++; | 67 pageStats.blocked++; |
| 68 else | 68 else |
| 69 tabStats.blocked = 1; | 69 pageStats.blocked = 1; |
| 70 | 70 |
| 71 // Update number in icon | 71 // Update number in icon |
| 72 if (Prefs.show_statsinicon) | 72 if (Prefs.show_statsinicon) |
| 73 { | 73 { |
| 74 tab.browserAction.setBadge({ | 74 page.browserAction.setBadge({ |
| 75 color: badgeColor, | 75 color: badgeColor, |
| 76 number: tabStats.blocked | 76 number: pageStats.blocked |
| 77 }); | 77 }); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 }); | 80 }); |
| 81 | 81 |
| 82 /** | |
| 83 * Execute function for each tab in any window | |
| 84 * @param {Function} func function to be executed | |
| 85 */ | |
| 86 function forEachTab(func) | |
| 87 { | |
| 88 ext.windows.getAll(function(windows) | |
| 89 { | |
| 90 for each (let window in windows) | |
| 91 { | |
| 92 window.getAllTabs(function(tabs) | |
| 93 { | |
| 94 for (let i = 0; i < tabs.length; i++) | |
| 95 func(tabs[i]); | |
| 96 }); | |
| 97 } | |
| 98 }); | |
| 99 } | |
| 100 | |
| 101 Prefs.addListener(function(name) | 82 Prefs.addListener(function(name) |
| 102 { | 83 { |
| 103 if (name != "show_statsinicon") | 84 if (name != "show_statsinicon") |
| 104 return; | 85 return; |
| 105 | 86 |
| 106 forEachTab(function(tab) | 87 ext.pages.query({}, function(page) |
| 107 { | 88 { |
| 108 let badge = null; | 89 let badge = null; |
| 109 if (Prefs.show_statsinicon) | 90 if (Prefs.show_statsinicon) |
| 110 { | 91 { |
| 111 let tabStats = statsPerTab.get(tab); | 92 let pageStats = statsPerPage.get(page); |
| 112 if (tabStats && "blocked" in tabStats) | 93 if (pageStats && "blocked" in pageStats) |
| 113 { | 94 { |
| 114 badge = { | 95 badge = { |
| 115 color: badgeColor, | 96 color: badgeColor, |
| 116 number: tabStats.blocked | 97 number: pageStats.blocked |
| 117 }; | 98 }; |
| 118 } | 99 } |
| 119 } | 100 } |
| 120 tab.browserAction.setBadge(badge); | 101 page.browserAction.setBadge(badge); |
| 121 }); | 102 }); |
| 122 }); | 103 }); |
| OLD | NEW |