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

Delta Between Two Patch Sets: lib/filterListener.js

Issue 29853570: Issue 6908 - Pass updated subscription's old filters as an event argument (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Aug. 11, 2018, 2:52 p.m.
Right Patch Set: Rebase Created Aug. 29, 2018, 6:03 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 | « no previous file | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Component synchronizing filter storage with Matcher 21 * @fileOverview Component synchronizing filter storage with Matcher
22 * instances and ElemHide. 22 * instances and ElemHide.
23 */ 23 */
24 24
25 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); 25 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
26 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); 26 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
27 27
28 const {FilterStorage} = require("./filterStorage"); 28 const {FilterStorage} = require("./filterStorage");
29 const {FilterNotifier} = require("./filterNotifier"); 29 const {filterNotifier} = require("./filterNotifier");
30 const {ElemHide} = require("./elemHide"); 30 const {ElemHide} = require("./elemHide");
31 const {ElemHideEmulation} = require("./elemHideEmulation"); 31 const {ElemHideEmulation} = require("./elemHideEmulation");
32 const {ElemHideExceptions} = require("./elemHideExceptions");
32 const {Snippets} = require("./snippets"); 33 const {Snippets} = require("./snippets");
33 const {defaultMatcher} = require("./matcher"); 34 const {defaultMatcher} = require("./matcher");
34 const {ActiveFilter, RegExpFilter, 35 const {ActiveFilter, RegExpFilter,
35 ElemHideBase, ElemHideEmulationFilter, 36 ElemHideBase, ElemHideFilter, ElemHideEmulationFilter,
36 SnippetFilter} = require("./filterClasses"); 37 SnippetFilter} = require("./filterClasses");
37 const {SpecialSubscription} = require("./subscriptionClasses"); 38 const {SpecialSubscription} = require("./subscriptionClasses");
38 const {Prefs} = require("prefs"); 39 const {Prefs} = require("prefs");
39 40
40 /** 41 /**
41 * Increases on filter changes, filters will be saved if it exceeds 1. 42 * Increases on filter changes, filters will be saved if it exceeds 1.
42 * @type {number} 43 * @type {number}
43 */ 44 */
44 let isDirty = 0; 45 let isDirty = 0;
45 46
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 QueryInterface: XPCOMUtils.generateQI( 90 QueryInterface: XPCOMUtils.generateQI(
90 [Ci.nsISupportsWeakReference, Ci.nsIObserver] 91 [Ci.nsISupportsWeakReference, Ci.nsIObserver]
91 ) 92 )
92 }; 93 };
93 94
94 /** 95 /**
95 * Initializes filter listener on startup, registers the necessary hooks. 96 * Initializes filter listener on startup, registers the necessary hooks.
96 */ 97 */
97 function init() 98 function init()
98 { 99 {
99 FilterNotifier.on("filter.hitCount", onFilterHitCount); 100 filterNotifier.on("filter.hitCount", onFilterHitCount);
100 FilterNotifier.on("filter.lastHit", onFilterLastHit); 101 filterNotifier.on("filter.lastHit", onFilterLastHit);
101 FilterNotifier.on("filter.added", onFilterAdded); 102 filterNotifier.on("filter.added", onFilterAdded);
102 FilterNotifier.on("filter.removed", onFilterRemoved); 103 filterNotifier.on("filter.removed", onFilterRemoved);
103 FilterNotifier.on("filter.disabled", onFilterDisabled); 104 filterNotifier.on("filter.disabled", onFilterDisabled);
104 FilterNotifier.on("filter.moved", onGenericChange); 105 filterNotifier.on("filter.moved", onGenericChange);
105 106
106 FilterNotifier.on("subscription.added", onSubscriptionAdded); 107 filterNotifier.on("subscription.added", onSubscriptionAdded);
107 FilterNotifier.on("subscription.removed", onSubscriptionRemoved); 108 filterNotifier.on("subscription.removed", onSubscriptionRemoved);
108 FilterNotifier.on("subscription.disabled", onSubscriptionDisabled); 109 filterNotifier.on("subscription.disabled", onSubscriptionDisabled);
109 FilterNotifier.on("subscription.updated", onSubscriptionUpdated); 110 filterNotifier.on("subscription.updated", onSubscriptionUpdated);
110 FilterNotifier.on("subscription.moved", onGenericChange); 111 filterNotifier.on("subscription.moved", onGenericChange);
111 FilterNotifier.on("subscription.title", onGenericChange); 112 filterNotifier.on("subscription.title", onGenericChange);
112 FilterNotifier.on("subscription.fixedTitle", onGenericChange); 113 filterNotifier.on("subscription.fixedTitle", onGenericChange);
113 FilterNotifier.on("subscription.homepage", onGenericChange); 114 filterNotifier.on("subscription.homepage", onGenericChange);
114 FilterNotifier.on("subscription.downloadStatus", onGenericChange); 115 filterNotifier.on("subscription.downloadStatus", onGenericChange);
115 FilterNotifier.on("subscription.lastCheck", onGenericChange); 116 filterNotifier.on("subscription.lastCheck", onGenericChange);
116 FilterNotifier.on("subscription.errors", onGenericChange); 117 filterNotifier.on("subscription.errors", onGenericChange);
117 118
118 FilterNotifier.on("load", onLoad); 119 filterNotifier.on("load", onLoad);
119 FilterNotifier.on("save", onSave); 120 filterNotifier.on("save", onSave);
120 121
121 FilterStorage.loadFromDisk(); 122 FilterStorage.loadFromDisk();
122 123
123 Services.obs.addObserver(HistoryPurgeObserver, 124 Services.obs.addObserver(HistoryPurgeObserver,
124 "browser:purge-session-history", true); 125 "browser:purge-session-history", true);
125 onShutdown.add(() => 126 onShutdown.add(() =>
126 { 127 {
127 Services.obs.removeObserver(HistoryPurgeObserver, 128 Services.obs.removeObserver(HistoryPurgeObserver,
128 "browser:purge-session-history"); 129 "browser:purge-session-history");
129 }); 130 });
130 } 131 }
131 init(); 132 init();
132 133
133 /** 134 /**
134 * Notifies Matcher instances or ElemHide object about a new filter 135 * Notifies Matcher instances or ElemHide object about a new filter
135 * if necessary. 136 * if necessary.
136 * @param {Filter} filter filter that has been added 137 * @param {Filter} filter filter that has been added
137 */ 138 */
138 function addFilter(filter) 139 function addFilter(filter)
139 { 140 {
140 if (!(filter instanceof ActiveFilter) || filter.disabled) 141 if (!(filter instanceof ActiveFilter) || filter.disabled)
141 return; 142 return;
142 143
143 let hasEnabled = false; 144 let hasEnabled = false;
144 let allowSnippets = false; 145 let allowSnippets = false;
145 for (let i = 0; i < filter.subscriptions.length; i++) 146 for (let subscription of filter.subscriptions)
146 { 147 {
147 let subscription = filter.subscriptions[i];
148
149 if (!subscription.disabled) 148 if (!subscription.disabled)
150 { 149 {
151 hasEnabled = true; 150 hasEnabled = true;
152 151
153 // Allow snippets to be executed only by the circumvention lists or the 152 // Allow snippets to be executed only by the circumvention lists or the
154 // user's own filters. 153 // user's own filters.
155 if (subscription.type == "circumvention" || 154 if (subscription.type == "circumvention" ||
156 subscription instanceof SpecialSubscription) 155 subscription instanceof SpecialSubscription)
157 { 156 {
158 allowSnippets = true; 157 allowSnippets = true;
159 break; 158 break;
160 } 159 }
161 } 160 }
162 } 161 }
163 if (!hasEnabled) 162 if (!hasEnabled)
164 return; 163 return;
165 164
166 if (filter instanceof RegExpFilter) 165 if (filter instanceof RegExpFilter)
167 defaultMatcher.add(filter); 166 defaultMatcher.add(filter);
168 else if (filter instanceof ElemHideBase) 167 else if (filter instanceof ElemHideBase)
169 { 168 {
170 if (filter instanceof ElemHideEmulationFilter) 169 if (filter instanceof ElemHideFilter)
170 ElemHide.add(filter);
171 else if (filter instanceof ElemHideEmulationFilter)
171 ElemHideEmulation.add(filter); 172 ElemHideEmulation.add(filter);
172 else 173 else
173 ElemHide.add(filter); 174 ElemHideExceptions.add(filter);
174 } 175 }
175 else if (allowSnippets && filter instanceof SnippetFilter) 176 else if (allowSnippets && filter instanceof SnippetFilter)
176 Snippets.add(filter); 177 Snippets.add(filter);
177 } 178 }
178 179
179 /** 180 /**
180 * Notifies Matcher instances or ElemHide object about removal of a filter 181 * Notifies Matcher instances or ElemHide object about removal of a filter
181 * if necessary. 182 * if necessary.
182 * @param {Filter} filter filter that has been removed 183 * @param {Filter} filter filter that has been removed
183 */ 184 */
184 function removeFilter(filter) 185 function removeFilter(filter)
185 { 186 {
186 if (!(filter instanceof ActiveFilter)) 187 if (!(filter instanceof ActiveFilter))
187 return; 188 return;
188 189
189 if (!filter.disabled) 190 if (!filter.disabled)
190 { 191 {
191 let hasEnabled = false; 192 let hasEnabled = false;
192 for (let i = 0; i < filter.subscriptions.length; i++) 193 for (let subscription of filter.subscriptions)
193 { 194 {
194 if (!filter.subscriptions[i].disabled) 195 if (!subscription.disabled)
195 { 196 {
196 hasEnabled = true; 197 hasEnabled = true;
197 break; 198 break;
198 } 199 }
199 } 200 }
200 if (hasEnabled) 201 if (hasEnabled)
201 return; 202 return;
202 } 203 }
203 204
204 if (filter instanceof RegExpFilter) 205 if (filter instanceof RegExpFilter)
205 defaultMatcher.remove(filter); 206 defaultMatcher.remove(filter);
206 else if (filter instanceof ElemHideBase) 207 else if (filter instanceof ElemHideBase)
207 { 208 {
208 if (filter instanceof ElemHideEmulationFilter) 209 if (filter instanceof ElemHideFilter)
210 ElemHide.remove(filter);
211 else if (filter instanceof ElemHideEmulationFilter)
209 ElemHideEmulation.remove(filter); 212 ElemHideEmulation.remove(filter);
210 else 213 else
211 ElemHide.remove(filter); 214 ElemHideExceptions.remove(filter);
212 } 215 }
213 else if (filter instanceof SnippetFilter) 216 else if (filter instanceof SnippetFilter)
214 Snippets.remove(filter); 217 Snippets.remove(filter);
215 } 218 }
216 219
217 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241]; 220 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241];
218 221
219 function addFilters(filters) 222 function addFilters(filters)
220 { 223 {
221 // We add filters using pseudo-random ordering. Reason is that ElemHide will 224 // We add filters using pseudo-random ordering. Reason is that ElemHide will
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 FilterListener.setDirty(1); 327 FilterListener.setDirty(1);
325 } 328 }
326 329
327 function onLoad() 330 function onLoad()
328 { 331 {
329 isDirty = 0; 332 isDirty = 0;
330 333
331 defaultMatcher.clear(); 334 defaultMatcher.clear();
332 ElemHide.clear(); 335 ElemHide.clear();
333 ElemHideEmulation.clear(); 336 ElemHideEmulation.clear();
337 ElemHideExceptions.clear();
334 Snippets.clear(); 338 Snippets.clear();
335 for (let subscription of FilterStorage.subscriptions) 339 for (let subscription of FilterStorage.subscriptions)
336 { 340 {
337 if (!subscription.disabled) 341 if (!subscription.disabled)
338 addFilters(subscription.filters); 342 addFilters(subscription.filters);
339 } 343 }
340 } 344 }
341 345
342 function onSave() 346 function onSave()
343 { 347 {
344 isDirty = 0; 348 isDirty = 0;
345 } 349 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld