Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/stats.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Patch Set: Created Dec. 21, 2013, 7:48 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 let badgeColor = "#646464"; 26 let badgeColor = "#646464";
27 let statsPerTab = new TabMap(true);
27 28
28 /** 29 /**
29 * Get statistics for specified tab 30 * Get statistics for specified tab
30 * @param {String} key field key 31 * @param {String} key field key
31 * @param {Number} tabId tab ID (leave undefined for total stats) 32 * @param {Number} tabId tab ID (leave undefined for total stats)
32 * @return {Number} field value 33 * @return {Number} field value
33 */ 34 */
34 let getStats = exports.getStats = function getStats(key, tab) 35 let getStats = exports.getStats = function getStats(key, tab)
35 { 36 {
36 if (!tab) 37 if (!tab)
37 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); 38 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0);
38 39
39 let frameData = getFrameData(tab, 0); 40 let tabStats = statsPerTab.get(tab);
40 return (frameData && key in frameData ? frameData[key] : 0); 41 return tabStats && tabStats.blocked || 0;
Felix Dahlke 2014/01/18 13:39:19 We're typically not using and/or as branching oper
Sebastian Noack 2014/01/19 10:19:40 Done.
41 }; 42 };
42 43
43 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab) 44 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab)
44 { 45 {
45 if (action != "filter.hitCount" || !tab) 46 if (action != "filter.hitCount" || !tab)
46 return; 47 return;
47 48
48 let blocked = item instanceof BlockingFilter; 49 let blocked = item instanceof BlockingFilter;
49 50
50 // Increment counts 51 // Increment counts
51 if (blocked) 52 if (blocked)
52 { 53 {
53 if ("blocked" in Prefs.stats_total) 54 if ("blocked" in Prefs.stats_total)
54 Prefs.stats_total.blocked++; 55 Prefs.stats_total.blocked++;
55 else 56 else
56 Prefs.stats_total.blocked = 1; 57 Prefs.stats_total.blocked = 1;
57 Prefs.stats_total = Prefs.stats_total; 58 Prefs.stats_total = Prefs.stats_total;
58 59
59 let frameData = getFrameData(tab, 0); 60 let tabStats = statsPerTab.get(tab);
60 if (frameData) 61 if (!tabStats)
61 { 62 {
62 if ("blocked" in frameData) 63 tabStats = {};
63 frameData.blocked++; 64 statsPerTab.set(tab, tabStats);
64 else
65 frameData.blocked = 1;
66 } 65 }
66 if ("blocked" in tabStats)
67 tabStats.blocked++;
68 else
69 tabStats.blocked = 1;
67 70
68 // Update number in icon 71 // Update number in icon
69 if (Prefs.show_statsinicon) 72 if (Prefs.show_statsinicon)
70 { 73 {
71 tab.browserAction.setBadge({ 74 tab.browserAction.setBadge({
72 color: badgeColor, 75 color: badgeColor,
73 number: frameData.blocked 76 number: tabStats.blocked
74 }); 77 });
75 } 78 }
76 } 79 }
77 }); 80 });
78 81
79 /** 82 /**
80 * Execute function for each tab in any window 83 * Execute function for each tab in any window
81 * @param {Function} func function to be executed 84 * @param {Function} func function to be executed
82 */ 85 */
83 function forEachTab(func) 86 function forEachTab(func)
(...skipping 14 matching lines...) Expand all
98 Prefs.addListener(function(name) 101 Prefs.addListener(function(name)
99 { 102 {
100 if (name != "show_statsinicon") 103 if (name != "show_statsinicon")
101 return; 104 return;
102 105
103 forEachTab(function(tab) 106 forEachTab(function(tab)
104 { 107 {
105 let badge = null; 108 let badge = null;
106 if (Prefs.show_statsinicon) 109 if (Prefs.show_statsinicon)
107 { 110 {
108 let frameData = getFrameData(tab, 0); 111 let tabStats = statsPerTab.get(tab);
109 if (frameData && "blocked" in frameData) 112 if (tabStats && "blocked" in tabStats)
110 { 113 {
111 badge = { 114 badge = {
112 color: badgeColor, 115 color: badgeColor,
113 number: frameData.blocked 116 number: tabStats.blocked
114 }; 117 };
115 } 118 }
116 } 119 }
117 tab.browserAction.setBadge(badge); 120 tab.browserAction.setBadge(badge);
118 }); 121 });
119 }); 122 });
OLDNEW

Powered by Google App Engine
This is Rietveld