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