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

Side by Side Diff: assets/js/FilterListener.jsm

Issue 8482109: ABP/Android JavaScript code (Closed)
Patch Set: ABP/Android JavaScript code Created Nov. 13, 2012, 9:44 a.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 | « assets/js/FilterClasses.jsm ('k') | assets/js/FilterNotifier.jsm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This Source Code is subject to the terms of the Mozilla Public License
3 * version 2.0 (the "License"). You can obtain a copy of the License at
4 * http://mozilla.org/MPL/2.0/.
5 */
6
7 //
8 // This file has been generated automatically from Adblock Plus source code
9 //
10
11 (function (_patchFunc2) {
12 const cacheVersion = 3;
13 var batchMode = false;
14 var isDirty = 0;
15 var FilterListener = {
16 startup: function () {
17 FilterNotifier.addListener(function (action, item, newValue, oldValue) {
18 if (/^filter\.(.*)/.test(action))
19 onFilterChange(RegExp["$1"], item, newValue, oldValue);
20 else
21 if (/^subscription\.(.*)/.test(action))
22 onSubscriptionChange(RegExp["$1"], item, newValue, oldValue);
23 else
24 onGenericChange(action, item);
25 }
26 );
27 ElemHide.init();
28 var initialized = false;
29 var cacheFile = Utils.resolveFilePath(Prefs.data_directory);
30 cacheFile.append("cache.js");
31 if (cacheFile.exists()) {
32 try {
33 var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInst ance(Ci.nsIFileInputStream);
34 stream.init(cacheFile, 1, 292, 0);
35 var json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
36 var cache = json.decodeFromStream(stream, "UTF-8");
37 stream.close();
38 if (cache.version == cacheVersion && cache.patternsTimestamp == Filter Storage.sourceFile.clone().lastModifiedTime) {
39 defaultMatcher.fromCache(cache);
40 ElemHide.fromCache(cache);
41 var loadDone = false;
42 function trapProperty(obj, prop) {
43 var origValue = obj[prop];
44 delete obj[prop];
45 obj.__defineGetter__(prop, function () {
46 delete obj[prop];
47 obj[prop] = origValue;
48 if (!loadDone) {
49 loadDone = true;
50 FilterStorage.loadFromDisk(null, true);
51 }
52 return obj[prop];
53 }
54 );
55 obj.__defineSetter__(prop, function (value) {
56 delete obj[prop];
57 return obj[prop] = value;
58 }
59 );
60 }
61 for (var _loopIndex0 = 0;
62 _loopIndex0 < ["fileProperties", "subscriptions", "knownSubscription s", "addSubscription", "removeSubscription", "updateSubscriptionFilters", "addFi lter", "removeFilter", "increaseHitCount", "resetHitCounts"].length; ++ _loopInd ex0) {
63 var prop = ["fileProperties", "subscriptions", "knownSubscriptions ", "addSubscription", "removeSubscription", "updateSubscriptionFilters", "addFil ter", "removeFilter", "increaseHitCount", "resetHitCounts"][_loopIndex0];
64 trapProperty(FilterStorage, prop);
65 }
66 trapProperty(Filter, "fromText");
67 trapProperty(Filter, "knownFilters");
68 trapProperty(Subscription, "fromURL");
69 trapProperty(Subscription, "knownSubscriptions");
70 initialized = true;
71 ElemHide.apply();
72 }
73 }
74 catch (e){
75 Cu.reportError(e);
76 }
77 }
78 if (!initialized)
79 FilterStorage.loadFromDisk();
80 Utils.observerService.addObserver(FilterListenerPrivate, "browser:purge-se ssion-history", true);
81 }
82 ,
83 shutdown: function () {
84 if (isDirty > 0)
85 FilterStorage.saveToDisk();
86 }
87 ,
88 get batchMode() {
89 return batchMode;
90 }
91 ,
92 set batchMode(value) {
93 batchMode = value;
94 flushElemHide();
95 }
96 ,
97 setDirty: function (factor) {
98 if (factor == 0 && isDirty > 0)
99 isDirty = 1;
100 else
101 isDirty += factor;
102 if (isDirty >= 1 && !filtersFlushScheduled) {
103 Utils.runAsync(flushFiltersInternal);
104 filtersFlushScheduled = true;
105 }
106 }
107
108 };
109 var FilterListenerPrivate = {
110 observe: function (subject, topic, data) {
111 if (topic == "browser:purge-session-history" && Prefs.clearStatsOnHistoryP urge) {
112 FilterStorage.resetHitCounts();
113 FilterListener.setDirty(0);
114 Prefs.recentReports = "[]";
115 }
116 }
117 ,
118 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIOb server])
119 };
120 var elemhideFlushScheduled = false;
121 function flushElemHide() {
122 if (elemhideFlushScheduled)
123 return ;
124 Utils.runAsync(flushElemHideInternal);
125 elemhideFlushScheduled = true;
126 }
127 function flushElemHideInternal() {
128 elemhideFlushScheduled = false;
129 if (!batchMode && ElemHide.isDirty)
130 ElemHide.apply();
131 }
132 var filtersFlushScheduled = false;
133 function flushFiltersInternal() {
134 filtersFlushScheduled = false;
135 FilterStorage.saveToDisk();
136 }
137 function addFilter(filter) {
138 if (!(filter instanceof ActiveFilter) || filter.disabled)
139 return ;
140 var hasEnabled = false;
141 for (var i = 0;
142 i < filter.subscriptions.length; i++)
143 if (!filter.subscriptions[i].disabled)
144 hasEnabled = true;
145 if (!hasEnabled)
146 return ;
147 if (filter instanceof RegExpFilter)
148 defaultMatcher.add(filter);
149 else
150 if (filter instanceof ElemHideFilter)
151 ElemHide.add(filter);
152 }
153 function removeFilter(filter) {
154 if (!(filter instanceof ActiveFilter))
155 return ;
156 if (!filter.disabled) {
157 var hasEnabled = false;
158 for (var i = 0;
159 i < filter.subscriptions.length; i++)
160 if (!filter.subscriptions[i].disabled)
161 hasEnabled = true;
162 if (hasEnabled)
163 return ;
164 }
165 if (filter instanceof RegExpFilter)
166 defaultMatcher.remove(filter);
167 else
168 if (filter instanceof ElemHideFilter)
169 ElemHide.remove(filter);
170 }
171 function onSubscriptionChange(action, subscription, newValue, oldValue) {
172 if (action == "homepage" || action == "downloadStatus" || action == "lastDow nload")
173 FilterListener.setDirty(0.2);
174 else
175 FilterListener.setDirty(1);
176 if (action != "added" && action != "removed" && action != "disabled" && acti on != "updated")
177 return ;
178 if (action != "removed" && !(subscription.url in FilterStorage.knownSubscrip tions)) {
179 return ;
180 }
181 if ((action == "added" || action == "removed" || action == "updated") && sub scription.disabled) {
182 return ;
183 }
184 if (action == "added" || action == "removed" || action == "disabled") {
185 var method = (action == "added" || (action == "disabled" && newValue == fa lse) ? addFilter : removeFilter);
186 if (subscription.filters)
187 subscription.filters.forEach(method);
188 }
189 else
190 if (action == "updated") {
191 subscription.oldFilters.forEach(removeFilter);
192 subscription.filters.forEach(addFilter);
193 }
194 flushElemHide();
195 }
196 function onFilterChange(action, filter, newValue, oldValue) {
197 if (action == "hitCount" || action == "lastHit")
198 FilterListener.setDirty(0.0001);
199 else
200 if (action == "disabled" || action == "moved")
201 FilterListener.setDirty(0.2);
202 else
203 FilterListener.setDirty(1);
204 if (action != "added" && action != "removed" && action != "disabled")
205 return ;
206 if ((action == "added" || action == "removed") && filter.disabled) {
207 return ;
208 }
209 if (action == "added" || (action == "disabled" && newValue == false))
210 addFilter(filter);
211 else
212 removeFilter(filter);
213 flushElemHide();
214 }
215 function onGenericChange(action) {
216 if (action == "load") {
217 isDirty = 0;
218 defaultMatcher.clear();
219 ElemHide.clear();
220 for (var _loopIndex1 = 0;
221 _loopIndex1 < FilterStorage.subscriptions.length; ++ _loopIndex1) {
222 var subscription = FilterStorage.subscriptions[_loopIndex1];
223 if (!subscription.disabled)
224 subscription.filters.forEach(addFilter);
225 }
226 flushElemHide();
227 }
228 else
229 if (action == "save") {
230 isDirty = 0;
231 var cache = {
232 version: cacheVersion,
233 patternsTimestamp: FilterStorage.sourceFile.clone().lastModifiedTime
234 };
235 defaultMatcher.toCache(cache);
236 ElemHide.toCache(cache);
237 var cacheFile = Utils.resolveFilePath(Prefs.data_directory);
238 cacheFile.append("cache.js");
239 try {
240 cacheFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, 493);
241 }
242 catch (e){}
243 try {
244 var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].creat eInstance(Ci.nsIFileOutputStream);
245 fileStream.init(cacheFile, 2 | 8 | 32, 420, 0);
246 var json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
247 if (Utils.versionComparator.compare(Utils.platformVersion, "5.0") >= 0 ) {
248 json.encodeToStream(fileStream, "UTF-8", false, cache);
249 fileStream.close();
250 }
251 else {
252 var stream = Cc["@mozilla.org/intl/converter-output-stream;1"].creat eInstance(Ci.nsIConverterOutputStream);
253 stream.init(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.D EFAULT_REPLACEMENT_CHARACTER);
254 stream.writeString(json.encode(cache));
255 stream.close();
256 }
257 }
258 catch (e){
259 delete FilterStorage.fileProperties.cacheTimestamp;
260 Cu.reportError(e);
261 }
262 }
263 }
264 if (typeof _patchFunc2 != "undefined")
265 eval("(" + _patchFunc2.toString() + ")()");
266 window.FilterListener = FilterListener;
267 }
268 )(window.FilterListenerPatch);
OLDNEW
« no previous file with comments | « assets/js/FilterClasses.jsm ('k') | assets/js/FilterNotifier.jsm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld