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

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

Issue 8482109: ABP/Android JavaScript code (Closed)
Patch Set: ABP/Android JavaScript code Created Nov. 12, 2012, 9:32 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
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 (_patchFunc11) {
12 const formatVersion = 4;
13 var FilterStorage = {
14 get formatVersion() {
15 return formatVersion;
16 },
17 get sourceFile() {
18 var file = null;
19 if (Prefs.patternsfile) {
20 file = Utils.resolveFilePath(Prefs.patternsfile);
21 }
22 if (!file) {
23 file = Utils.resolveFilePath(Prefs.data_directory);
24 if (file)
25 file.append("patterns.ini");
26 }
27 if (!file) {
28 try {
29 file = Utils.resolveFilePath(Prefs.defaultBranch.getCharPref("data_dir ectory"));
30 if (file)
31 file.append("patterns.ini");
32 }
33 catch (e){}
34 }
35 if (!file)
36 Cu.reportError("Adblock Plus: Failed to resolve filter file location fro m extensions.adblockplus.patternsfile preference");
37 this.__defineGetter__("sourceFile", function () {
38 return file;
39 });
40 return this.sourceFile;
41 }
42 ,
43 fileProperties: {
44 __proto__: null
45 },
46 subscriptions: [],
47 knownSubscriptions: {
48 __proto__: null
49 },
50 getGroupForFilter: function (filter) {
51 var generalSubscription = null;
52 for (var _loopIndex0 = 0;
53 _loopIndex0 < FilterStorage.subscriptions.length; ++ _loopIndex0) {
54 var subscription = FilterStorage.subscriptions[_loopIndex0];
55 if (subscription instanceof SpecialSubscription) {
56 if (subscription.isDefaultFor(filter))
57 return subscription;
58 if (!generalSubscription && (!subscription.defaults || !subscription.d efaults.length))
59 generalSubscription = subscription;
60 }
61 }
62 return generalSubscription;
63 }
64 ,
65 addSubscription: function (subscription, silent) {
66 if (subscription.url in FilterStorage.knownSubscriptions)
67 return ;
68 FilterStorage.subscriptions.push(subscription);
69 FilterStorage.knownSubscriptions[subscription.url] = subscription;
70 addSubscriptionFilters(subscription);
71 if (!silent)
72 FilterNotifier.triggerListeners("subscription.added", subscription);
73 }
74 ,
75 removeSubscription: function (subscription, silent) {
76 for (var i = 0;
77 i < FilterStorage.subscriptions.length; i++) {
78 if (FilterStorage.subscriptions[i].url == subscription.url) {
79 removeSubscriptionFilters(subscription);
80 FilterStorage.subscriptions.splice(i--, 1);
81 delete FilterStorage.knownSubscriptions[subscription.url];
82 if (!silent)
83 FilterNotifier.triggerListeners("subscription.removed", subscription );
84 return ;
85 }
86 }
87 }
88 ,
89 moveSubscription: function (subscription, insertBefore) {
90 var currentPos = FilterStorage.subscriptions.indexOf(subscription);
91 if (currentPos < 0)
92 return ;
93 var newPos = insertBefore ? FilterStorage.subscriptions.indexOf(insertBefo re) : -1;
94 if (newPos < 0)
95 newPos = FilterStorage.subscriptions.length;
96 if (currentPos < newPos)
97 newPos--;
98 if (currentPos == newPos)
99 return ;
100 FilterStorage.subscriptions.splice(currentPos, 1);
101 FilterStorage.subscriptions.splice(newPos, 0, subscription);
102 FilterNotifier.triggerListeners("subscription.moved", subscription);
103 }
104 ,
105 updateSubscriptionFilters: function (subscription, filters) {
106 removeSubscriptionFilters(subscription);
107 subscription.oldFilters = subscription.filters;
108 subscription.filters = filters;
109 addSubscriptionFilters(subscription);
110 FilterNotifier.triggerListeners("subscription.updated", subscription);
111 delete subscription.oldFilters;
112 if (subscription instanceof SpecialSubscription && !subscription.filters.l ength && subscription.disabled)
113 subscription.disabled = false;
114 }
115 ,
116 addFilter: function (filter, subscription, position, silent) {
117 if (!subscription) {
118 if (filter.subscriptions.some(function (s) {
119 return s instanceof SpecialSubscription;
120 }))
121 return ;
122 subscription = FilterStorage.getGroupForFilter(filter);
123 }
124 if (!subscription) {
125 subscription = SpecialSubscription.createForFilter(filter);
126 this.addSubscription(subscription);
127 return ;
128 }
129 if (typeof position == "undefined")
130 position = subscription.filters.length;
131 if (filter.subscriptions.indexOf(subscription) < 0)
132 filter.subscriptions.push(subscription);
133 subscription.filters.splice(position, 0, filter);
134 if (!silent)
135 FilterNotifier.triggerListeners("filter.added", filter, subscription, po sition);
136 }
137 ,
138 removeFilter: function (filter, subscription, position) {
139 var subscriptions = (subscription ? [subscription] : filter.subscriptions. slice());
140 for (var i = 0;
141 i < subscriptions.length; i++) {
142 var subscription = subscriptions[i];
143 if (subscription instanceof SpecialSubscription) {
144 var positions = [];
145 if (typeof position == "undefined") {
146 var index = -1;
147 do {
148 index = subscription.filters.indexOf(filter, index + 1);
149 if (index >= 0)
150 positions.push(index);
151 }
152 while (index >= 0);
153 }
154 else
155 positions.push(position);
156 for (var j = positions.length - 1;
157 j >= 0; j--) {
158 var position = positions[j];
159 if (subscription.filters[position] == filter) {
160 subscription.filters.splice(position, 1);
161 if (subscription.filters.indexOf(filter) < 0) {
162 var index = filter.subscriptions.indexOf(subscription);
163 if (index >= 0)
164 filter.subscriptions.splice(index, 1);
165 }
166 FilterNotifier.triggerListeners("filter.removed", filter, subscrip tion, position);
167 }
168 }
169 }
170 }
171 }
172 ,
173 moveFilter: function (filter, subscription, oldPosition, newPosition) {
174 if (!(subscription instanceof SpecialSubscription) || subscription.filters [oldPosition] != filter)
175 return ;
176 newPosition = Math.min(Math.max(newPosition, 0), subscription.filters.leng th - 1);
177 if (oldPosition == newPosition)
178 return ;
179 subscription.filters.splice(oldPosition, 1);
180 subscription.filters.splice(newPosition, 0, filter);
181 FilterNotifier.triggerListeners("filter.moved", filter, subscription, oldP osition, newPosition);
182 }
183 ,
184 increaseHitCount: function (filter) {
185 if (!Prefs.savestats || Prefs.privateBrowsing || !(filter instanceof Activ eFilter))
186 return ;
187 filter.hitCount++;
188 filter.lastHit = Date.now();
189 }
190 ,
191 resetHitCounts: function (filters) {
192 if (!filters) {
193 filters = [];
194 for (var _loopIndex2 = 0;
195 _loopIndex2 < Filter.knownFilters.length; ++ _loopIndex2) {
196 var filter = Filter.knownFilters[_loopIndex2];
197 filters.push(filter);
198 }
199 }
200 for (var _loopIndex1 = 0;
201 _loopIndex1 < filters.length; ++ _loopIndex1) {
202 var filter = filters[_loopIndex1];
203 filter.hitCount = 0;
204 filter.lastHit = 0;
205 }
206 }
207 ,
208 loadFromDisk: function (sourceFile, silent) {
209 if (!silent) {
210 Filter.knownFilters = {
211 __proto__: null
212 };
213 Subscription.knownSubscriptions = {
214 __proto__: null
215 };
216 }
217 var explicitFile = true;
218 if (!sourceFile) {
219 sourceFile = FilterStorage.sourceFile;
220 explicitFile = false;
221 if (!sourceFile || !sourceFile.exists()) {
222 var patternsURL = Utils.ioService.newURI("chrome://adblockplus-default s/content/patterns.ini", null, null);
223 patternsURL = Utils.chromeRegistry.convertChromeURL(patternsURL);
224 if (patternsURL instanceof Ci.nsIFileURL)
225 sourceFile = patternsURL.file;
226 }
227 }
228 var userFilters = null;
229 var backup = 0;
230 while (true) {
231 FilterStorage.subscriptions = [];
232 FilterStorage.knownSubscriptions = {
233 __proto__: null
234 };
235 try {
236 if (sourceFile && sourceFile.exists()) {
237 var fileStream = Cc["@mozilla.org/network/file-input-stream;1"].crea teInstance(Ci.nsIFileInputStream);
238 fileStream.init(sourceFile, 1, 292, 0);
239 var stream = Cc["@mozilla.org/intl/converter-input-stream;1"].create Instance(Ci.nsIConverterInputStream);
240 stream.init(fileStream, "UTF-8", 16384, 0);
241 stream = stream.QueryInterface(Ci.nsIUnicharLineInputStream);
242 userFilters = parseIniFile(stream);
243 stream.close();
244 if (!FilterStorage.subscriptions.length) {
245 throw new Error("No data in the file");
246 }
247 }
248 break;
249 }
250 catch (e){
251 Cu.reportError("Adblock Plus: Failed to read filters from file " + sou rceFile.path);
252 Cu.reportError(e);
253 }
254 if (explicitFile)
255 break;
256 sourceFile = FilterStorage.sourceFile;
257 if (!sourceFile)
258 break;
259 var part1 = sourceFile.leafName;
260 var part2 = "";
261 if (/^(.*)(\.\w+)$/.test(part1)) {
262 part1 = RegExp["$1"];
263 part2 = RegExp["$2"];
264 }
265 sourceFile = sourceFile.clone();
266 sourceFile.leafName = part1 + "-backup" + (++ backup) + part2;
267 }
268 for (var _loopIndex3 = 0;
269 _loopIndex3 < ["~il~", "~wl~", "~fl~", "~eh~"].length; ++ _loopIndex3) {
270 var specialSubscription = ["~il~", "~wl~", "~fl~", "~eh~"][_loopIndex3];
271 if (specialSubscription in FilterStorage.knownSubscriptions) {
272 var subscription = Subscription.fromURL(specialSubscription);
273 if (subscription.filters.length == 0)
274 FilterStorage.removeSubscription(subscription, true);
275 }
276 }
277 if (userFilters) {
278 for (var _loopIndex4 = 0;
279 _loopIndex4 < userFilters.length; ++ _loopIndex4) {
280 var filter = userFilters[_loopIndex4];
281 filter = Filter.fromText(filter);
282 if (filter)
283 FilterStorage.addFilter(filter, null, undefined, true);
284 }
285 }
286 if (!silent)
287 FilterNotifier.triggerListeners("load");
288 }
289 ,
290 saveToDisk: function (targetFile) {
291 var explicitFile = true;
292 if (!targetFile) {
293 targetFile = FilterStorage.sourceFile;
294 explicitFile = false;
295 }
296 if (!targetFile)
297 return ;
298 try {
299 targetFile.normalize();
300 }
301 catch (e){}
302 try {
303 targetFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, 493);
304 }
305 catch (e){}
306 var tempFile = targetFile.clone();
307 tempFile.leafName += "-temp";
308 var fileStream, stream;
309 try {
310 fileStream = Cc["@mozilla.org/network/safe-file-output-stream;1"].create Instance(Ci.nsIFileOutputStream);
311 fileStream.init(tempFile, 2 | 8 | 32, 420, 0);
312 stream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstanc e(Ci.nsIConverterOutputStream);
313 stream.init(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAU LT_REPLACEMENT_CHARACTER);
314 }
315 catch (e){
316 Cu.reportError(e);
317 return ;
318 }
319 const maxBufLength = 1024;
320 var buf = ["# Adblock Plus preferences", "version=" + formatVersion];
321 var lineBreak = Utils.getLineBreak();
322 function writeBuffer() {
323 stream.writeString(buf.join(lineBreak) + lineBreak);
324 buf.splice(0, buf.length);
325 }
326 var saved = {
327 __proto__: null
328 };
329 for (var _loopIndex5 = 0;
330 _loopIndex5 < FilterStorage.subscriptions.length; ++ _loopIndex5) {
331 var subscription = FilterStorage.subscriptions[_loopIndex5];
332 if (subscription instanceof ExternalSubscription)
333 continue;
334 for (var _loopIndex7 = 0;
335 _loopIndex7 < subscription.filters.length; ++ _loopIndex7) {
336 var filter = subscription.filters[_loopIndex7];
337 if (!(filter.text in saved)) {
338 filter.serialize(buf);
339 saved[filter.text] = filter;
340 if (buf.length > maxBufLength)
341 writeBuffer();
342 }
343 }
344 }
345 for (var _loopIndex6 = 0;
346 _loopIndex6 < FilterStorage.subscriptions.length; ++ _loopIndex6) {
347 var subscription = FilterStorage.subscriptions[_loopIndex6];
348 if (subscription instanceof ExternalSubscription)
349 continue;
350 buf.push("");
351 subscription.serialize(buf);
352 if (subscription.filters.length) {
353 buf.push("", "[Subscription filters]");
354 subscription.serializeFilters(buf);
355 }
356 if (buf.length > maxBufLength)
357 writeBuffer();
358 }
359 try {
360 stream.writeString(buf.join(lineBreak) + lineBreak);
361 stream.flush();
362 fileStream.QueryInterface(Ci.nsISafeOutputStream).finish();
363 }
364 catch (e){
365 Cu.reportError(e);
366 return ;
367 }
368 if (!explicitFile && targetFile.exists()) {
369 var part1 = targetFile.leafName;
370 var part2 = "";
371 if (/^(.*)(\.\w+)$/.test(part1)) {
372 part1 = RegExp["$1"];
373 part2 = RegExp["$2"];
374 }
375 var doBackup = (Prefs.patternsbackups > 0);
376 if (doBackup) {
377 var lastBackup = targetFile.clone();
378 lastBackup.leafName = part1 + "-backup1" + part2;
379 if (lastBackup.exists() && (Date.now() - lastBackup.lastModifiedTime) / 3600000 < Prefs.patternsbackupinterval)
380 doBackup = false;
381 }
382 if (doBackup) {
383 var backupFile = targetFile.clone();
384 backupFile.leafName = part1 + "-backup" + Prefs.patternsbackups + part 2;
385 try {
386 backupFile.remove(false);
387 }
388 catch (e){}
389 for (var i = Prefs.patternsbackups - 1;
390 i >= 0; i--) {
391 backupFile.leafName = part1 + (i > 0 ? "-backup" + i : "") + part2;
392 try {
393 backupFile.moveTo(backupFile.parent, part1 + "-backup" + (i + 1) + part2);
394 }
395 catch (e){}
396 }
397 }
398 }
399 else
400 if (targetFile.exists())
401 targetFile.remove(false);
402 tempFile.moveTo(targetFile.parent, targetFile.leafName);
403 if (!explicitFile)
404 FilterNotifier.triggerListeners("save");
405 }
406 ,
407 getBackupFiles: function () {
408 var result = [];
409 var part1 = FilterStorage.sourceFile.leafName;
410 var part2 = "";
411 if (/^(.*)(\.\w+)$/.test(part1)) {
412 part1 = RegExp["$1"];
413 part2 = RegExp["$2"];
414 }
415 for (var i = 1;
416 ;
417 i++) {
418 var file = FilterStorage.sourceFile.clone();
419 file.leafName = part1 + "-backup" + i + part2;
420 if (file.exists())
421 result.push(file);
422 else
423 break;
424 }
425 return result;
426 }
427
428 };
429 function addSubscriptionFilters(subscription) {
430 if (!(subscription.url in FilterStorage.knownSubscriptions))
431 return ;
432 for (var _loopIndex8 = 0;
433 _loopIndex8 < subscription.filters.length; ++ _loopIndex8) {
434 var filter = subscription.filters[_loopIndex8];
435 filter.subscriptions.push(subscription);
436 }
437 }
438 function removeSubscriptionFilters(subscription) {
439 if (!(subscription.url in FilterStorage.knownSubscriptions))
440 return ;
441 for (var _loopIndex9 = 0;
442 _loopIndex9 < subscription.filters.length; ++ _loopIndex9) {
443 var filter = subscription.filters[_loopIndex9];
444 var i = filter.subscriptions.indexOf(subscription);
445 if (i >= 0)
446 filter.subscriptions.splice(i, 1);
447 }
448 }
449 function parseIniFile(stream) {
450 var wantObj = true;
451 FilterStorage.fileProperties = {
452
453 };
454 var curObj = FilterStorage.fileProperties;
455 var curSection = null;
456 var line = {
457
458 };
459 var haveMore = true;
460 var userFilters = null;
461 while (true) {
462 if (haveMore)
463 haveMore = stream.readLine(line);
464 else
465 line.value = "[end]";
466 var val = line.value;
467 if (wantObj === true && /^(\w+)=(.*)$/.test(val))
468 curObj[RegExp["$1"]] = RegExp["$2"];
469 else
470 if (/^\s*\[(.+)\]\s*$/.test(val)) {
471 var newSection = RegExp["$1"].toLowerCase();
472 if (curObj) {
473 switch (curSection) {
474 case "filter": ;
475 case "pattern": {
476 if ("text" in curObj)
477 Filter.fromObject(curObj);
478 break;
479 }
480 case "subscription": {
481 var subscription = Subscription.fromObject(curObj);
482 if (subscription)
483 FilterStorage.addSubscription(subscription, true);
484 break;
485 }
486 case "subscription filters": ;
487 case "subscription patterns": {
488 if (FilterStorage.subscriptions.length) {
489 var subscription = FilterStorage.subscriptions[FilterStorage.s ubscriptions.length - 1];
490 for (var _loopIndex10 = 0;
491 _loopIndex10 < curObj.length; ++ _loopIndex10) {
492 var text = curObj[_loopIndex10];
493 var filter = Filter.fromText(text);
494 if (filter) {
495 subscription.filters.push(filter);
496 filter.subscriptions.push(subscription);
497 }
498 }
499 }
500 break;
501 }
502 case "user patterns": {
503 userFilters = curObj;
504 break;
505 }
506 }
507 }
508 if (newSection == "end")
509 break;
510 curSection = newSection;
511 switch (curSection) {
512 case "filter": ;
513 case "pattern": ;
514 case "subscription": {
515 wantObj = true;
516 curObj = {
517
518 };
519 break;
520 }
521 case "subscription filters": ;
522 case "subscription patterns": ;
523 case "user patterns": {
524 wantObj = false;
525 curObj = [];
526 break;
527 }
528 default: {
529 wantObj = undefined;
530 curObj = null;
531 }
532 }
533 }
534 else
535 if (wantObj === false && val)
536 curObj.push(val.replace(/\\\[/g, "["));
537 }
538 return userFilters;
539 }
540 if (typeof _patchFunc11 != "undefined")
541 eval("(" + _patchFunc11.toString() + ")()");
542 window.FilterStorage = FilterStorage;
543 }
544 )(window.FilterStoragePatch);
OLDNEW

Powered by Google App Engine
This is Rietveld