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

Delta Between Two Patch Sets: lib/filterListener.js

Issue 29336735: Issue 394 - hit statistics tool data collection (core) (Closed)
Left Patch Set: Created Feb. 19, 2016, 5:26 p.m.
Right Patch Set: Use Downloader to send the data to server Created April 6, 2016, 2:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/downloader.js ('k') | lib/filterStorage.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 Component synchronizing filter storage with Matcher instances a nd ElemHide. 19 * @fileOverview Component synchronizing filter storage with Matcher instances a nd ElemHide.
20 */ 20 */
21 21
22 "use strict";
23
22 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 24 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
23 Cu.import("resource://gre/modules/Services.jsm"); 25 Cu.import("resource://gre/modules/Services.jsm");
24 26
25 let {FilterStorage} = require("filterStorage"); 27 let {FilterStorage} = require("filterStorage");
26 let {FilterNotifier} = require("filterNotifier"); 28 let {FilterNotifier} = require("filterNotifier");
27 let {ElemHide} = require("elemHide"); 29 let {ElemHide} = require("elemHide");
28 let {CSSRules} = require("cssRules"); 30 let {CSSRules} = require("cssRules");
29 let {defaultMatcher} = require("matcher"); 31 let {defaultMatcher} = require("matcher");
30 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} = 32 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} =
31 require("filterClasses"); 33 require("filterClasses");
32 let {Prefs} = require("prefs"); 34 let {Prefs} = require("prefs");
33 let {FilterHits} = require("filterHits"); 35 let FilterHits = null;
Wladimir Palant 2016/02/29 14:40:30 This needs to be included conditionally - this fil
saroyanm 2016/03/18 18:24:47 Done.
36 try
37 {
38 ({FilterHits} = require("filterHits"));
39 }
40 catch (e) {}
34 41
35 /** 42 /**
36 * Value of the FilterListener.batchMode property. 43 * Value of the FilterListener.batchMode property.
37 * @type Boolean 44 * @type Boolean
38 */ 45 */
39 let batchMode = false; 46 let batchMode = false;
40 47
41 /** 48 /**
42 * Increases on filter changes, filters will be saved if it exceeds 1. 49 * Increases on filter changes, filters will be saved if it exceeds 1.
43 * @type Integer 50 * @type Integer
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 * Observer listening to history purge actions. 95 * Observer listening to history purge actions.
89 * @class 96 * @class
90 */ 97 */
91 let HistoryPurgeObserver = 98 let HistoryPurgeObserver =
92 { 99 {
93 observe: function(subject, topic, data) 100 observe: function(subject, topic, data)
94 { 101 {
95 if (topic == "browser:purge-session-history" && Prefs.clearStatsOnHistoryPur ge) 102 if (topic == "browser:purge-session-history" && Prefs.clearStatsOnHistoryPur ge)
96 { 103 {
97 FilterStorage.resetHitCounts(); 104 FilterStorage.resetHitCounts();
98 FilterHits.resetFilterHits();
99 FilterListener.setDirty(0); // Force saving to disk 105 FilterListener.setDirty(0); // Force saving to disk
106 if (FilterHits)
107 FilterHits.resetFilterHits();
100 108
101 Prefs.recentReports = []; 109 Prefs.recentReports = [];
102 } 110 }
103 }, 111 },
104 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) 112 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver])
105 }; 113 };
106 114
107 /** 115 /**
108 * Initializes filter listener on startup, registers the necessary hooks. 116 * Initializes filter listener on startup, registers the necessary hooks.
109 */ 117 */
110 function init() 118 function init()
111 { 119 {
112 FilterNotifier.addListener(function(action, item, newValue, oldValue) 120 FilterNotifier.on("filter.hitCount", onFilterHitCount);
113 { 121 FilterNotifier.on("filter.lastHit", onFilterLastHit);
114 let match = /^(\w+)\.(.*)/.exec(action); 122 FilterNotifier.on("filter.added", onFilterAdded);
115 if (match && match[1] == "filter") 123 FilterNotifier.on("filter.removed", onFilterRemoved);
116 onFilterChange(match[2], item, newValue, oldValue); 124 FilterNotifier.on("filter.disabled", onFilterDisabled);
117 else if (match && match[1] == "subscription") 125 FilterNotifier.on("filter.moved", onGenericChange);
118 onSubscriptionChange(match[2], item, newValue, oldValue); 126
119 else 127 FilterNotifier.on("subscription.added", onSubscriptionAdded);
120 onGenericChange(action, item); 128 FilterNotifier.on("subscription.removed", onSubscriptionRemoved);
121 }); 129 FilterNotifier.on("subscription.disabled", onSubscriptionDisabled);
130 FilterNotifier.on("subscription.updated", onSubscriptionUpdated);
131 FilterNotifier.on("subscription.moved", onGenericChange);
132 FilterNotifier.on("subscription.title", onGenericChange);
133 FilterNotifier.on("subscription.fixedTitle", onGenericChange);
134 FilterNotifier.on("subscription.homepage", onGenericChange);
135 FilterNotifier.on("subscription.downloadStatus", onGenericChange);
136 FilterNotifier.on("subscription.lastCheck", onGenericChange);
137 FilterNotifier.on("subscription.errors", onGenericChange);
138
139 FilterNotifier.on("load", onLoad);
140 FilterNotifier.on("save", onSave);
141
122 142
123 if ("nsIStyleSheetService" in Ci) 143 if ("nsIStyleSheetService" in Ci)
124 ElemHide.init(); 144 ElemHide.init();
125 else 145 else
126 flushElemHide = function() {}; // No global stylesheet in Chrome & Co. 146 flushElemHide = function() {}; // No global stylesheet in Chrome & Co.
127 FilterStorage.loadFromDisk(); 147 FilterStorage.loadFromDisk();
128 148
129 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true); 149 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true);
130 onShutdown.add(function() 150 onShutdown.add(function()
131 { 151 {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 defaultMatcher.remove(filter); 215 defaultMatcher.remove(filter);
196 else if (filter instanceof ElemHideBase) 216 else if (filter instanceof ElemHideBase)
197 { 217 {
198 if (filter instanceof CSSPropertyFilter) 218 if (filter instanceof CSSPropertyFilter)
199 CSSRules.remove(filter); 219 CSSRules.remove(filter);
200 else 220 else
201 ElemHide.remove(filter); 221 ElemHide.remove(filter);
202 } 222 }
203 } 223 }
204 224
205 /** 225 function onSubscriptionAdded(subscription)
206 * Subscription change listener 226 {
207 */ 227 FilterListener.setDirty(1);
208 function onSubscriptionChange(action, subscription, newValue, oldValue) 228
209 { 229 if (!subscription.disabled)
210 FilterListener.setDirty(1); 230 {
211 231 subscription.filters.forEach(addFilter);
212 if (action != "added" && action != "removed" && action != "disabled" && action != "updated") 232 flushElemHide();
213 return; 233 }
214 234 }
215 if (action != "removed" && !(subscription.url in FilterStorage.knownSubscripti ons)) 235
216 { 236 function onSubscriptionRemoved(subscription)
217 // Ignore updates for subscriptions not in the list 237 {
218 return; 238 FilterListener.setDirty(1);
219 } 239
220 240 if (!subscription.disabled)
221 if ((action == "added" || action == "removed" || action == "updated") && subsc ription.disabled) 241 {
222 { 242 subscription.filters.forEach(removeFilter);
223 // Ignore adding/removing/updating of disabled subscriptions 243 flushElemHide();
224 return; 244 }
225 } 245 }
226 246
227 if (action == "added" || action == "removed" || action == "disabled") 247 function onSubscriptionDisabled(subscription, newValue)
228 { 248 {
229 let method = (action == "added" || (action == "disabled" && newValue == fals e) ? addFilter : removeFilter); 249 FilterListener.setDirty(1);
230 if (subscription.filters) 250
231 subscription.filters.forEach(method); 251 if (subscription.url in FilterStorage.knownSubscriptions)
232 } 252 {
233 else if (action == "updated") 253 if (newValue == false)
254 subscription.filters.forEach(addFilter);
255 else
256 subscription.filters.forEach(removeFilter);
257 flushElemHide();
258 }
259 }
260
261 function onSubscriptionUpdated(subscription)
262 {
263 FilterListener.setDirty(1);
264
265 if (subscription.url in FilterStorage.knownSubscriptions &&
266 !subscription.disabled)
234 { 267 {
235 subscription.oldFilters.forEach(removeFilter); 268 subscription.oldFilters.forEach(removeFilter);
236 subscription.filters.forEach(addFilter); 269 subscription.filters.forEach(addFilter);
237 } 270 flushElemHide();
238 271 }
239 flushElemHide(); 272 }
240 } 273
241 274 function onFilterHitCount(filter, newValue)
242 /** 275 {
243 * Filter change listener 276 if (newValue == 0)
244 */
245 function onFilterChange(action, filter, newValue, oldValue)
246 {
247 if (action == "hitCount" && newValue == 0)
248 {
249 // Filter hits are being reset, make sure these changes are saved.
250 FilterListener.setDirty(0); 277 FilterListener.setDirty(0);
251 } 278 else
252 else if (action == "hitCount" || action == "lastHit")
253 FilterListener.setDirty(0.002); 279 FilterListener.setDirty(0.002);
254 else 280 }
255 FilterListener.setDirty(1); 281
256 282 function onFilterLastHit()
257 if (action != "added" && action != "removed" && action != "disabled") 283 {
258 return; 284 FilterListener.setDirty(0.002);
259 285 }
260 if ((action == "added" || action == "removed") && filter.disabled) 286
261 { 287 function onFilterAdded(filter)
262 // Ignore adding/removing of disabled filters 288 {
263 return; 289 FilterListener.setDirty(1);
264 } 290
265 291 if (!filter.disabled)
266 if (action == "added" || (action == "disabled" && newValue == false)) 292 {
293 addFilter(filter);
294 flushElemHide();
295 }
296 }
297
298 function onFilterRemoved(filter)
299 {
300 FilterListener.setDirty(1);
301
302 if (!filter.disabled)
303 {
304 removeFilter(filter);
305 flushElemHide();
306 }
307 }
308
309 function onFilterDisabled(filter, newValue)
310 {
311 FilterListener.setDirty(1);
312
313 if (newValue == false)
267 addFilter(filter); 314 addFilter(filter);
268 else 315 else
269 removeFilter(filter); 316 removeFilter(filter);
270 flushElemHide(); 317 flushElemHide();
271 } 318 }
272 319
273 /** 320 function onGenericChange()
274 * Generic notification listener 321 {
275 */ 322 FilterListener.setDirty(1);
276 function onGenericChange(action) 323 }
277 { 324
278 if (action == "load") 325 function onLoad()
279 { 326 {
280 isDirty = 0; 327 isDirty = 0;
281 328
282 defaultMatcher.clear(); 329 defaultMatcher.clear();
283 ElemHide.clear(); 330 ElemHide.clear();
284 CSSRules.clear(); 331 CSSRules.clear();
285 for (let subscription of FilterStorage.subscriptions) 332 for (let subscription of FilterStorage.subscriptions)
286 if (!subscription.disabled) 333 if (!subscription.disabled)
287 subscription.filters.forEach(addFilter); 334 subscription.filters.forEach(addFilter);
288 flushElemHide(); 335 flushElemHide();
289 } 336 }
290 else if (action == "save") 337
291 isDirty = 0; 338 function onSave()
292 } 339 {
340 isDirty = 0;
341 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld