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

Side by Side Diff: lib/filterListener.js

Issue 29345663: Issue 4140 - Remove Firefox-specific element hiding functionality (Closed)
Patch Set: Created June 8, 2016, 8:23 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
« no previous file with comments | « lib/elemHide.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 16 matching lines...) Expand all
27 let {FilterStorage} = require("filterStorage"); 27 let {FilterStorage} = require("filterStorage");
28 let {FilterNotifier} = require("filterNotifier"); 28 let {FilterNotifier} = require("filterNotifier");
29 let {ElemHide} = require("elemHide"); 29 let {ElemHide} = require("elemHide");
30 let {CSSRules} = require("cssRules"); 30 let {CSSRules} = require("cssRules");
31 let {defaultMatcher} = require("matcher"); 31 let {defaultMatcher} = require("matcher");
32 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} = 32 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} =
33 require("filterClasses"); 33 require("filterClasses");
34 let {Prefs} = require("prefs"); 34 let {Prefs} = require("prefs");
35 35
36 /** 36 /**
37 * Value of the FilterListener.batchMode property.
38 * @type Boolean
39 */
40 let batchMode = false;
41
42 /**
43 * Increases on filter changes, filters will be saved if it exceeds 1. 37 * Increases on filter changes, filters will be saved if it exceeds 1.
44 * @type Integer 38 * @type Integer
45 */ 39 */
46 let isDirty = 0; 40 let isDirty = 0;
47 41
48 /** 42 /**
49 * This object can be used to change properties of the filter change listeners. 43 * This object can be used to change properties of the filter change listeners.
50 * @class 44 * @class
51 */ 45 */
52 let FilterListener = 46 let FilterListener =
53 { 47 {
54 /** 48 /**
55 * Set to true when executing many changes, changes will only be fully applied after this variable is set to false again.
56 * @type Boolean
57 */
58 get batchMode()
59 {
60 return batchMode;
61 },
62 set batchMode(value)
63 {
64 batchMode = value;
65 flushElemHide();
66 },
67
68 /**
69 * Increases "dirty factor" of the filters and calls FilterStorage.saveToDisk( ) 49 * Increases "dirty factor" of the filters and calls FilterStorage.saveToDisk( )
70 * if it becomes 1 or more. Save is executed delayed to prevent multiple 50 * if it becomes 1 or more. Save is executed delayed to prevent multiple
71 * subsequent calls. If the parameter is 0 it forces saving filters if any 51 * subsequent calls. If the parameter is 0 it forces saving filters if any
72 * changes were recorded after the previous save. 52 * changes were recorded after the previous save.
73 */ 53 */
74 setDirty: function(/**Integer*/ factor) 54 setDirty: function(/**Integer*/ factor)
75 { 55 {
76 if (factor == 0 && isDirty > 0) 56 if (factor == 0 && isDirty > 0)
77 isDirty = 1; 57 isDirty = 1;
78 else 58 else
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 FilterNotifier.on("subscription.title", onGenericChange); 104 FilterNotifier.on("subscription.title", onGenericChange);
125 FilterNotifier.on("subscription.fixedTitle", onGenericChange); 105 FilterNotifier.on("subscription.fixedTitle", onGenericChange);
126 FilterNotifier.on("subscription.homepage", onGenericChange); 106 FilterNotifier.on("subscription.homepage", onGenericChange);
127 FilterNotifier.on("subscription.downloadStatus", onGenericChange); 107 FilterNotifier.on("subscription.downloadStatus", onGenericChange);
128 FilterNotifier.on("subscription.lastCheck", onGenericChange); 108 FilterNotifier.on("subscription.lastCheck", onGenericChange);
129 FilterNotifier.on("subscription.errors", onGenericChange); 109 FilterNotifier.on("subscription.errors", onGenericChange);
130 110
131 FilterNotifier.on("load", onLoad); 111 FilterNotifier.on("load", onLoad);
132 FilterNotifier.on("save", onSave); 112 FilterNotifier.on("save", onSave);
133 113
134
135 if ("nsIStyleSheetService" in Ci)
136 ElemHide.init();
137 else
138 flushElemHide = function() {}; // No global stylesheet in Chrome & Co.
139 FilterStorage.loadFromDisk(); 114 FilterStorage.loadFromDisk();
140 115
141 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true); 116 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true);
142 onShutdown.add(function() 117 onShutdown.add(function()
143 { 118 {
144 Services.obs.removeObserver(HistoryPurgeObserver, "browser:purge-session-his tory"); 119 Services.obs.removeObserver(HistoryPurgeObserver, "browser:purge-session-his tory");
145 }); 120 });
146 } 121 }
147 init(); 122 init();
148 123
149 /** 124 /**
150 * Calls ElemHide.apply() if necessary.
151 */
152 function flushElemHide()
153 {
154 if (!batchMode && ElemHide.isDirty)
155 ElemHide.apply();
156 }
157
158 /**
159 * Notifies Matcher instances or ElemHide object about a new filter 125 * Notifies Matcher instances or ElemHide object about a new filter
160 * if necessary. 126 * if necessary.
161 * @param {Filter} filter filter that has been added 127 * @param {Filter} filter filter that has been added
162 */ 128 */
163 function addFilter(filter) 129 function addFilter(filter)
164 { 130 {
165 if (!(filter instanceof ActiveFilter) || filter.disabled) 131 if (!(filter instanceof ActiveFilter) || filter.disabled)
166 return; 132 return;
167 133
168 let hasEnabled = false; 134 let hasEnabled = false;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 203
238 for (let i = 0; i < len; i++, current = (current + step) % len) 204 for (let i = 0; i < len; i++, current = (current + step) % len)
239 addFilter(filters[current]); 205 addFilter(filters[current]);
240 } 206 }
241 207
242 function onSubscriptionAdded(subscription) 208 function onSubscriptionAdded(subscription)
243 { 209 {
244 FilterListener.setDirty(1); 210 FilterListener.setDirty(1);
245 211
246 if (!subscription.disabled) 212 if (!subscription.disabled)
247 {
248 addFilters(subscription.filters); 213 addFilters(subscription.filters);
249 flushElemHide();
250 }
251 } 214 }
252 215
253 function onSubscriptionRemoved(subscription) 216 function onSubscriptionRemoved(subscription)
254 { 217 {
255 FilterListener.setDirty(1); 218 FilterListener.setDirty(1);
256 219
257 if (!subscription.disabled) 220 if (!subscription.disabled)
258 {
259 subscription.filters.forEach(removeFilter); 221 subscription.filters.forEach(removeFilter);
260 flushElemHide();
261 }
262 } 222 }
263 223
264 function onSubscriptionDisabled(subscription, newValue) 224 function onSubscriptionDisabled(subscription, newValue)
265 { 225 {
266 FilterListener.setDirty(1); 226 FilterListener.setDirty(1);
267 227
268 if (subscription.url in FilterStorage.knownSubscriptions) 228 if (subscription.url in FilterStorage.knownSubscriptions)
269 { 229 {
270 if (newValue == false) 230 if (newValue == false)
271 addFilters(subscription.filters); 231 addFilters(subscription.filters);
272 else 232 else
273 subscription.filters.forEach(removeFilter); 233 subscription.filters.forEach(removeFilter);
274 flushElemHide();
275 } 234 }
276 } 235 }
277 236
278 function onSubscriptionUpdated(subscription) 237 function onSubscriptionUpdated(subscription)
279 { 238 {
280 FilterListener.setDirty(1); 239 FilterListener.setDirty(1);
281 240
282 if (subscription.url in FilterStorage.knownSubscriptions && 241 if (subscription.url in FilterStorage.knownSubscriptions &&
283 !subscription.disabled) 242 !subscription.disabled)
284 { 243 {
285 subscription.oldFilters.forEach(removeFilter); 244 subscription.oldFilters.forEach(removeFilter);
286 addFilters(subscription.filters); 245 addFilters(subscription.filters);
287 flushElemHide();
288 } 246 }
289 } 247 }
290 248
291 function onFilterHitCount(filter, newValue) 249 function onFilterHitCount(filter, newValue)
292 { 250 {
293 if (newValue == 0) 251 if (newValue == 0)
294 FilterListener.setDirty(0); 252 FilterListener.setDirty(0);
295 else 253 else
296 FilterListener.setDirty(0.002); 254 FilterListener.setDirty(0.002);
297 } 255 }
298 256
299 function onFilterLastHit() 257 function onFilterLastHit()
300 { 258 {
301 FilterListener.setDirty(0.002); 259 FilterListener.setDirty(0.002);
302 } 260 }
303 261
304 function onFilterAdded(filter) 262 function onFilterAdded(filter)
305 { 263 {
306 FilterListener.setDirty(1); 264 FilterListener.setDirty(1);
307 265
308 if (!filter.disabled) 266 if (!filter.disabled)
309 {
310 addFilter(filter); 267 addFilter(filter);
311 flushElemHide();
312 }
313 } 268 }
314 269
315 function onFilterRemoved(filter) 270 function onFilterRemoved(filter)
316 { 271 {
317 FilterListener.setDirty(1); 272 FilterListener.setDirty(1);
318 273
319 if (!filter.disabled) 274 if (!filter.disabled)
320 {
321 removeFilter(filter); 275 removeFilter(filter);
322 flushElemHide();
323 }
324 } 276 }
325 277
326 function onFilterDisabled(filter, newValue) 278 function onFilterDisabled(filter, newValue)
327 { 279 {
328 FilterListener.setDirty(1); 280 FilterListener.setDirty(1);
329 281
330 if (newValue == false) 282 if (newValue == false)
331 addFilter(filter); 283 addFilter(filter);
332 else 284 else
333 removeFilter(filter); 285 removeFilter(filter);
334 flushElemHide();
335 } 286 }
336 287
337 function onGenericChange() 288 function onGenericChange()
338 { 289 {
339 FilterListener.setDirty(1); 290 FilterListener.setDirty(1);
340 } 291 }
341 292
342 function onLoad() 293 function onLoad()
343 { 294 {
344 isDirty = 0; 295 isDirty = 0;
345 296
346 defaultMatcher.clear(); 297 defaultMatcher.clear();
347 ElemHide.clear(); 298 ElemHide.clear();
348 CSSRules.clear(); 299 CSSRules.clear();
349 for (let subscription of FilterStorage.subscriptions) 300 for (let subscription of FilterStorage.subscriptions)
350 if (!subscription.disabled) 301 if (!subscription.disabled)
351 addFilters(subscription.filters); 302 addFilters(subscription.filters);
352 flushElemHide();
353 } 303 }
354 304
355 function onSave() 305 function onSave()
356 { 306 {
357 isDirty = 0; 307 isDirty = 0;
358 } 308 }
OLDNEW
« no previous file with comments | « lib/elemHide.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld