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

Side by Side Diff: lib/filterListener.js

Issue 29354827: Issue 4394 - Create a filter class for element hiding emulation filters [WIP] (Closed)
Patch Set: Created Sept. 23, 2016, 4:21 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 <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"; 22 "use strict";
23 23
24 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 24 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
25 Cu.import("resource://gre/modules/Services.jsm"); 25 Cu.import("resource://gre/modules/Services.jsm");
26 26
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 {ElemHideEmulation} = require("elemHideEmulation");
31 let {defaultMatcher} = require("matcher"); 31 let {defaultMatcher} = require("matcher");
32 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} = 32 let {ActiveFilter, RegExpFilter, ElemHideBase, ElemHideEmulationFilter} =
33 require("filterClasses"); 33 require("filterClasses");
34 let {Prefs} = require("prefs"); 34 let {Prefs} = require("prefs");
35 35
36 /** 36 /**
37 * 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.
38 * @type Integer 38 * @type Integer
39 */ 39 */
40 let isDirty = 0; 40 let isDirty = 0;
41 41
42 /** 42 /**
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 for (let i = 0; i < filter.subscriptions.length; i++) 135 for (let i = 0; i < filter.subscriptions.length; i++)
136 if (!filter.subscriptions[i].disabled) 136 if (!filter.subscriptions[i].disabled)
137 hasEnabled = true; 137 hasEnabled = true;
138 if (!hasEnabled) 138 if (!hasEnabled)
139 return; 139 return;
140 140
141 if (filter instanceof RegExpFilter) 141 if (filter instanceof RegExpFilter)
142 defaultMatcher.add(filter); 142 defaultMatcher.add(filter);
143 else if (filter instanceof ElemHideBase) 143 else if (filter instanceof ElemHideBase)
144 { 144 {
145 if (filter instanceof CSSPropertyFilter) 145 if (filter instanceof ElemHideEmulationFilter)
146 CSSRules.add(filter); 146 ElemHideEmulation.add(filter);
147 else 147 else
148 ElemHide.add(filter); 148 ElemHide.add(filter);
149 } 149 }
150 } 150 }
151 151
152 /** 152 /**
153 * Notifies Matcher instances or ElemHide object about removal of a filter 153 * Notifies Matcher instances or ElemHide object about removal of a filter
154 * if necessary. 154 * if necessary.
155 * @param {Filter} filter filter that has been removed 155 * @param {Filter} filter filter that has been removed
156 */ 156 */
157 function removeFilter(filter) 157 function removeFilter(filter)
158 { 158 {
159 if (!(filter instanceof ActiveFilter)) 159 if (!(filter instanceof ActiveFilter))
160 return; 160 return;
161 161
162 if (!filter.disabled) 162 if (!filter.disabled)
163 { 163 {
164 let hasEnabled = false; 164 let hasEnabled = false;
165 for (let i = 0; i < filter.subscriptions.length; i++) 165 for (let i = 0; i < filter.subscriptions.length; i++)
166 if (!filter.subscriptions[i].disabled) 166 if (!filter.subscriptions[i].disabled)
167 hasEnabled = true; 167 hasEnabled = true;
168 if (hasEnabled) 168 if (hasEnabled)
169 return; 169 return;
170 } 170 }
171 171
172 if (filter instanceof RegExpFilter) 172 if (filter instanceof RegExpFilter)
173 defaultMatcher.remove(filter); 173 defaultMatcher.remove(filter);
174 else if (filter instanceof ElemHideBase) 174 else if (filter instanceof ElemHideBase)
175 { 175 {
176 if (filter instanceof CSSPropertyFilter) 176 if (filter instanceof ElemHideEmulationFilter)
177 CSSRules.remove(filter); 177 ElemHideEmulation.remove(filter);
178 else 178 else
179 ElemHide.remove(filter); 179 ElemHide.remove(filter);
180 } 180 }
181 } 181 }
182 182
183 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241]; 183 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241];
184 184
185 function addFilters(filters) 185 function addFilters(filters)
186 { 186 {
187 // We add filters using pseudo-random ordering. Reason is that ElemHide will 187 // We add filters using pseudo-random ordering. Reason is that ElemHide will
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 { 289 {
290 FilterListener.setDirty(1); 290 FilterListener.setDirty(1);
291 } 291 }
292 292
293 function onLoad() 293 function onLoad()
294 { 294 {
295 isDirty = 0; 295 isDirty = 0;
296 296
297 defaultMatcher.clear(); 297 defaultMatcher.clear();
298 ElemHide.clear(); 298 ElemHide.clear();
299 CSSRules.clear(); 299 ElemHideEmulation.clear();
300 for (let subscription of FilterStorage.subscriptions) 300 for (let subscription of FilterStorage.subscriptions)
301 if (!subscription.disabled) 301 if (!subscription.disabled)
302 addFilters(subscription.filters); 302 addFilters(subscription.filters);
303 } 303 }
304 304
305 function onSave() 305 function onSave()
306 { 306 {
307 isDirty = 0; 307 isDirty = 0;
308 } 308 }
OLDNEW

Powered by Google App Engine
This is Rietveld