| LEFT | RIGHT |
| 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-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 /** | 26 /** |
| 27 * Base class for individual stats | 27 * Get statistics for specified tab |
| 28 * @param {Object} stats simple object representation of stats | 28 * @param {String} key field key |
| 29 * @param {Number} tabId tab ID (leave undefined for total stats) |
| 30 * @return {Number} field value |
| 29 */ | 31 */ |
| 30 let StatsObject = exports.StatsObject = function(stats) | 32 let getStats = exports.getStats = function getStats(key, tabId) |
| 31 { | 33 { |
| 32 if (typeof stats == "undefined") | 34 if (tabId) |
| 33 return; | 35 { |
| 34 | 36 let frameData = getFrameData(tabId, 0); |
| 35 this.blocked = stats.blocked || this.blocked; | 37 return (frameData && key in frameData ? frameData[key] : 0); |
| 36 } | 38 } |
| 37 StatsObject.prototype = { | 39 else |
| 38 /** | 40 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); |
| 39 * Number of blocked resources | |
| 40 * @type Number | |
| 41 */ | |
| 42 blocked: 0 | |
| 43 }; | 41 }; |
| 44 | 42 |
| 45 let total = new StatsObject(Prefs["stats_total"]); | 43 FilterNotifier.addListener(function(action, item, newValue, oldValue, tabId) |
| 46 | |
| 47 let Stats = exports.Stats = { | |
| 48 /** | |
| 49 * Total counts | |
| 50 */ | |
| 51 get total() | |
| 52 { | |
| 53 return total; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 FilterNotifier.addListener(function(action, item, tabId) | |
| 58 { | 44 { |
| 59 if (action != "document.stats") | 45 if (action != "filter.hitCount") |
| 60 return; | 46 return; |
| 61 | 47 |
| 62 var blocked = item instanceof BlockingFilter; | 48 var blocked = item instanceof BlockingFilter; |
| 63 | 49 |
| 64 // increment counts | 50 // Increment counts |
| 65 if (blocked) | 51 if (blocked) |
| 66 total.blocked++; | 52 { |
| 67 | 53 if ("blocked" in Prefs.stats_total) |
| 68 ChromeCompat.tabs.sendMessage(tabId, { | 54 Prefs.stats_total.blocked++; |
| 69 reqtype: "update-stats", | 55 else |
| 70 blocked: blocked | 56 Prefs.stats_total.blocked = 1; |
| 71 }, null); | 57 |
| 72 | 58 let frameData = getFrameData(tabId, 0); |
| 73 Prefs["stats_total"] = total; | 59 if (frameData) |
| 60 { |
| 61 if ("blocked" in frameData) |
| 62 frameData.blocked++; |
| 63 else |
| 64 frameData.blocked = 1; |
| 65 } |
| 66 } |
| 74 }); | 67 }); |
| LEFT | RIGHT |