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 function removeTrailingDots(string) | |
19 { | |
20 return string.replace(/\.+$/, ""); | |
21 } | |
22 | |
23 /** | |
24 * Checks whether a request is third party for the given document, uses | |
25 * information from the public suffix list to determine the effective domain | |
26 * name for the document. | |
27 */ | |
28 function isThirdParty(requestHost, documentHost) | |
29 { | |
30 requestHost = removeTrailingDots(requestHost); | |
31 documentHost = removeTrailingDots(documentHost); | |
32 | |
33 // Extract domain name - leave IP addresses unchanged, otherwise leave only ba
se domain | |
34 var documentDomain = getBaseDomain(documentHost); | |
35 if (requestHost.length > documentDomain.length) | |
36 return (requestHost.substr(requestHost.length - documentDomain.length - 1) !
= "." + documentDomain); | |
37 else | |
38 return (requestHost != documentDomain); | |
39 } | |
40 | |
41 function reportError(exp) | |
42 { | |
43 Android.print("Error: " + exp); | |
44 if (typeof exp == "string") | |
45 { | |
46 Android.showToast(exp); | |
47 } | |
48 Android.print(exp.stack); | |
49 } | |
50 | |
51 function MatcherPatch() | |
52 { | |
53 // Very ugly - we need to rewrite _checkEntryMatch() function to make sure | |
54 // it calls Filter.fromText() instead of assuming that the filter exists. | |
55 var origFunction = Matcher.prototype._checkEntryMatch.toString(); | |
56 var newFunction = origFunction.replace(/\bFilter\.knownFilters\[(.*?)\];/g, "F
ilter.fromText($1);"); | |
57 eval("Matcher.prototype._checkEntryMatch = " + newFunction); | |
58 } | |
59 | |
60 var window = this; | |
61 | |
62 var Components = | |
63 { | |
64 interfaces: | |
65 { | |
66 nsIFile: {DIRECTORY_TYPE: 0}, | |
67 nsIFileURL: function() {}, | |
68 nsIFileInputStream: null, | |
69 nsIFileOutputStream: null, | |
70 nsIHttpChannel: function() {}, | |
71 nsIConverterInputStream: {DEFAULT_REPLACEMENT_CHARACTER: null}, | |
72 nsIConverterOutputStream: null, | |
73 nsIUnicharLineInputStream: null, | |
74 nsISafeOutputStream: null, | |
75 nsITimer: {TYPE_REPEATING_SLACK: 0}, | |
76 nsIInterfaceRequestor: null, | |
77 nsIChannelEventSink: null | |
78 }, | |
79 classes: | |
80 { | |
81 "@mozilla.org/network/file-input-stream;1": | |
82 { | |
83 createInstance: function() | |
84 { | |
85 return new FakeInputStream(); | |
86 } | |
87 }, | |
88 "@mozilla.org/network/file-output-stream;1": | |
89 { | |
90 createInstance: function() | |
91 { | |
92 return new FakeOutputStream(); | |
93 } | |
94 }, | |
95 "@mozilla.org/dom/json;1": | |
96 { | |
97 createInstance: function() { | |
98 return { | |
99 decodeFromStream: function(stream, encoding) | |
100 { | |
101 var line = {}; | |
102 var haveMore = true; | |
103 var s = new String(); | |
104 while (true) | |
105 { | |
106 if (haveMore) | |
107 haveMore = stream.readLine(line); | |
108 else | |
109 break; | |
110 s += line.value; | |
111 } | |
112 return JSON.parse(s); | |
113 }, | |
114 encodeToStream: function(stream, encoding, something, obj) | |
115 { | |
116 var s = JSON.stringify(obj); | |
117 stream.writeString(s); | |
118 } | |
119 } | |
120 } | |
121 }, | |
122 "@mozilla.org/timer;1": | |
123 { | |
124 createInstance: function() | |
125 { | |
126 return new FakeTimer(); | |
127 } | |
128 } | |
129 }, | |
130 results: {}, | |
131 utils: { | |
132 reportError: reportError | |
133 }, | |
134 manager: null, | |
135 ID: function() | |
136 { | |
137 return null; | |
138 }, | |
139 Constructor: function() | |
140 { | |
141 // This method is only used to get XMLHttpRequest constructor | |
142 return XMLHttpRequest; | |
143 } | |
144 }; | |
145 const Cc = Components.classes; | |
146 const Ci = Components.interfaces; | |
147 const Cr = Components.results; | |
148 const Cu = Components.utils; | |
149 | |
150 Cc["@mozilla.org/intl/converter-input-stream;1"] = Cc["@mozilla.org/network/file
-input-stream;1"]; | |
151 Cc["@mozilla.org/network/safe-file-output-stream;1"] = Cc["@mozilla.org/intl/con
verter-output-stream;1"] = Cc["@mozilla.org/network/file-output-stream;1"]; | |
152 | |
153 var Prefs = | |
154 { | |
155 patternsbackups: 5, | |
156 patternsbackupinterval: 24, | |
157 data_directory: _datapath, | |
158 savestats: false, | |
159 privateBrowsing: false, | |
160 get subscriptions_autoupdate() { return Android.canAutoupdate() }, | |
161 subscriptions_fallbackerrors: 5, | |
162 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%V
ERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNE
LSTATUS%&responseStatus=%RESPONSESTATUS%", | |
163 addListener: function() {} | |
164 }; | |
165 | |
166 var Utils = | |
167 { | |
168 systemPrincipal: null, | |
169 getString: function(id) | |
170 { | |
171 return id; | |
172 }, | |
173 getLineBreak: function() | |
174 { | |
175 return "\n"; | |
176 }, | |
177 resolveFilePath: function(path) | |
178 { | |
179 return new FakeFile(path); | |
180 }, | |
181 ioService: | |
182 { | |
183 newURI: function(uri) | |
184 { | |
185 if (!uri.length || uri[0] == "~") | |
186 throw new Error("Invalid URI"); | |
187 | |
188 /^([^:\/]*)/.test(uri); | |
189 var scheme = RegExp.$1.toLowerCase(); | |
190 | |
191 return {scheme: scheme, spec: uri}; | |
192 } | |
193 }, | |
194 observerService: | |
195 { | |
196 addObserver: function() {}, | |
197 removeObserver: function() {} | |
198 }, | |
199 chromeRegistry: | |
200 { | |
201 convertChromeURL: function() {} | |
202 }, | |
203 runAsync: function(callback, thisPtr) | |
204 { | |
205 var params = Array.prototype.slice.call(arguments, 2); | |
206 Android.setTimeout(function() | |
207 { | |
208 callback.apply(thisPtr, params); | |
209 }, 0); | |
210 }, | |
211 addonVersion: _version, | |
212 platformVersion: "10.0", | |
213 get appLocale() | |
214 { | |
215 Android.getLocale(); | |
216 }, | |
217 generateChecksum: function(lines) | |
218 { | |
219 // We cannot calculate MD5 checksums yet :-( | |
220 return null; | |
221 }, | |
222 makeURI: function(url) | |
223 { | |
224 return Utils.ioService.newURI(url); | |
225 }, | |
226 checkLocalePrefixMatch: function(prefixes) | |
227 { | |
228 if (!prefixes) | |
229 return null; | |
230 | |
231 var list = prefixes.split(","); | |
232 for (var i = 0; i < list.length; i++) | |
233 if (new RegExp("^" + list[i] + "\\b").test(Utils.appLocale)) | |
234 return list[i]; | |
235 | |
236 return null; | |
237 }, | |
238 versionComparator: | |
239 { | |
240 compare: function(v1, v2) | |
241 { | |
242 var parts1 = v1.split("."); | |
243 var parts2 = v2.split("."); | |
244 for (var i = 0; i < Math.max(parts1.length, parts2.length); i++) | |
245 { | |
246 // TODO: Handle non-integer version parts properly | |
247 var part1 = parseInt(i < parts1.length ? parts1[i] : "0"); | |
248 var part2 = parseInt(i < parts2.length ? parts2[i] : "0"); | |
249 if (part1 != part2) | |
250 return part1 - part2; | |
251 } | |
252 return 0; | |
253 } | |
254 } | |
255 }; | |
256 | |
257 var XPCOMUtils = | |
258 { | |
259 generateQI: function() {} | |
260 }; | |
261 | |
262 function FakeFile(path) | |
263 { | |
264 this.path = path; | |
265 } | |
266 FakeFile.prototype = | |
267 { | |
268 get leafName() | |
269 { | |
270 return this.path; | |
271 }, | |
272 set leafName(value) | |
273 { | |
274 this.path = value; | |
275 }, | |
276 append: function(path) | |
277 { | |
278 this.path += _separator + path; | |
279 }, | |
280 clone: function() | |
281 { | |
282 return new FakeFile(this.path); | |
283 }, | |
284 exists: function() | |
285 { | |
286 return Android.fileExists(this.path); | |
287 }, | |
288 remove: function() | |
289 { | |
290 Android.fileRemove(this.path); | |
291 }, | |
292 moveTo: function(parent, newPath) | |
293 { | |
294 Android.fileRename(this.path, newPath); | |
295 }, | |
296 get lastModifiedTime() | |
297 { | |
298 return Android.fileLastModified(this.path); | |
299 }, | |
300 get parent() | |
301 { | |
302 return {create: function() {}}; | |
303 }, | |
304 normalize: function() {} | |
305 }; | |
306 | |
307 function FakeInputStream() | |
308 { | |
309 } | |
310 FakeInputStream.prototype = | |
311 { | |
312 lines: null, | |
313 currentIndex: 0, | |
314 | |
315 init: function(file) | |
316 { | |
317 if (file instanceof FakeInputStream) | |
318 this.lines = file.lines; | |
319 else | |
320 this.lines = Android.fileRead(file.path).split(/\n/); | |
321 }, | |
322 readLine: function(line) | |
323 { | |
324 if (this.currentIndex < this.lines.length) | |
325 line.value = this.lines[this.currentIndex]; | |
326 this.currentIndex++; | |
327 return (this.currentIndex < this.lines.length); | |
328 }, | |
329 close: function() {}, | |
330 QueryInterface: function() | |
331 { | |
332 return this; | |
333 } | |
334 }; | |
335 | |
336 function FakeOutputStream() | |
337 { | |
338 } | |
339 FakeOutputStream.prototype = | |
340 { | |
341 file: null, | |
342 buffer: null, | |
343 | |
344 init: function(file) | |
345 { | |
346 if (file instanceof FakeOutputStream) | |
347 { | |
348 this.file = file.file; | |
349 this.buffer = file.buffer; | |
350 } | |
351 else | |
352 { | |
353 this.file = file; | |
354 this.buffer = []; | |
355 } | |
356 }, | |
357 writeString: function(string) | |
358 { | |
359 this.buffer.push(string); | |
360 }, | |
361 close: function() | |
362 { | |
363 Android.fileWrite(this.file.path, this.buffer.join("")); | |
364 }, | |
365 finish: function() | |
366 { | |
367 this.close(); | |
368 }, | |
369 flush: function() {}, | |
370 QueryInterface: function() | |
371 { | |
372 return this; | |
373 } | |
374 }; | |
375 | |
376 function FakeTimer() | |
377 { | |
378 } | |
379 FakeTimer.prototype = | |
380 { | |
381 delay: 0, | |
382 callback: null, | |
383 initWithCallback: function(callback, delay) | |
384 { | |
385 this.callback = callback; | |
386 this.delay = delay; | |
387 this.scheduleTimeout(); | |
388 }, | |
389 scheduleTimeout: function() | |
390 { | |
391 var me = this; | |
392 Android.setTimeout(function() | |
393 { | |
394 try | |
395 { | |
396 me.callback(); | |
397 } | |
398 catch(e) | |
399 { | |
400 reportError(e); | |
401 } | |
402 me.scheduleTimeout(); | |
403 }, this.delay); | |
404 } | |
405 }; | |
406 | |
407 function ElemHidePatch() | |
408 { | |
409 /** | |
410 * Returns a list of selectors to be applied on a particular domain. With | |
411 * specificOnly parameter set to true only the rules listing specific domains | |
412 * will be considered. | |
413 */ | |
414 ElemHide.getSelectorsForDomain = function(/**String*/ domain, /**Boolean*/ spe
cificOnly) | |
415 { | |
416 var result = []; | |
417 for (var key in filterByKey) | |
418 { | |
419 var filter = Filter.knownFilters[filterByKey[key]]; | |
420 if (specificOnly && (!filter.domains || filter.domains[""])) | |
421 continue; | |
422 | |
423 if (filter.isActiveOnDomain(domain)) | |
424 result.push(filter.selector); | |
425 } | |
426 if (result.length) | |
427 return "<style type=\"text/css\">" + result.join(',\r\n') + " { display: n
one !important }</style>"; | |
428 else | |
429 return null; | |
430 }; | |
431 | |
432 ElemHide.init = function() {}; | |
433 } | |
434 | |
435 /** | |
436 * Removes all subscriptions from storage. | |
437 */ | |
438 function clearSubscriptions() | |
439 { | |
440 while (FilterStorage.subscriptions.length) | |
441 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]); | |
442 } | |
443 | |
444 /** | |
445 * Adds selected subscription to storage. | |
446 */ | |
447 function addSubscription(jsonSub) | |
448 { | |
449 var newSub = JSON.parse(jsonSub); | |
450 | |
451 var subscription = Subscription.fromURL(newSub["url"]); | |
452 if (subscription) | |
453 { | |
454 subscription.disabled = false; | |
455 subscription.title = newSub["title"]; | |
456 subscription.homepage = newSub["homepage"]; | |
457 if (subscription instanceof DownloadableSubscription && !subscription.lastDo
wnload) | |
458 { | |
459 Synchronizer.execute(subscription); | |
460 } | |
461 FilterStorage.addSubscription(subscription); | |
462 FilterStorage.saveToDisk(); | |
463 } | |
464 } | |
465 | |
466 /** | |
467 * Forces subscriptions refresh. | |
468 */ | |
469 function refreshSubscriptions() | |
470 { | |
471 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | |
472 { | |
473 var subscription = FilterStorage.subscriptions[i]; | |
474 if (subscription instanceof DownloadableSubscription) | |
475 Synchronizer.execute(subscription, true, true); | |
476 } | |
477 } | |
478 | |
479 /** | |
480 * Verifies that subscriptions are loaded and returns flag of subscription prese
nce. | |
481 */ | |
482 function verifySubscriptions() | |
483 { | |
484 var hasSubscriptions = false; | |
485 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | |
486 { | |
487 var subscription = FilterStorage.subscriptions[i]; | |
488 if (subscription instanceof DownloadableSubscription) | |
489 { | |
490 hasSubscriptions = true; | |
491 updateSubscriptionStatus(subscription); | |
492 if (!subscription.lastDownload) | |
493 { | |
494 Synchronizer.execute(subscription); | |
495 } | |
496 } | |
497 } | |
498 return hasSubscriptions; | |
499 } | |
500 | |
501 /** | |
502 * Callback for subscription status updates. | |
503 */ | |
504 function updateSubscriptionStatus(subscription) | |
505 { | |
506 var status = ""; | |
507 var time = 0; | |
508 if (Synchronizer.isExecuting(subscription.url)) | |
509 status = "synchronize_in_progress"; | |
510 else if (subscription.downloadStatus && subscription.downloadStatus != "synchr
onize_ok") | |
511 status = subscription.downloadStatus; | |
512 else if (subscription.lastDownload > 0) | |
513 { | |
514 time = subscription.lastDownload * 1000; | |
515 status = "synchronize_last_at"; | |
516 } | |
517 else | |
518 status = "synchronize_never"; | |
519 | |
520 Android.setStatus(status, time); | |
521 } | |
522 | |
523 function onFilterChange(action, subscription, param1, param2) | |
524 { | |
525 switch (action) | |
526 { | |
527 case "subscription.lastDownload": | |
528 case "subscription.downloadStatus": | |
529 updateSubscriptionStatus(subscription); | |
530 break; | |
531 } | |
532 } | |
533 | |
534 function startInteractive() | |
535 { | |
536 FilterNotifier.addListener(onFilterChange); | |
537 } | |
538 | |
539 function stopInteractive() | |
540 { | |
541 FilterNotifier.removeListener(onFilterChange); | |
542 } | |
543 | |
544 function matchesAny(url, query, reqHost, refHost, accept) | |
545 { | |
546 var contentType = null; | |
547 var thirdParty = true; | |
548 | |
549 if (accept != "") | |
550 { | |
551 if (accept.indexOf("text/css") != -1) | |
552 contentType = "STYLESHEET"; | |
553 else if (accept.indexOf("image/*" != -1)) | |
554 contentType = "IMAGE"; | |
555 } | |
556 | |
557 if (contentType == null) | |
558 { | |
559 var lurl = url.toLowerCase(); | |
560 if (/\.js$/.test(lurl)) | |
561 contentType = "SCRIPT"; | |
562 else if (/\.css$/.test(lurl)) | |
563 contentType = "STYLESHEET"; | |
564 else if (/\.(?:gif|png|jpe?g|bmp|ico)$/.test(lurl)) | |
565 contentType = "IMAGE"; | |
566 else if (/\.(?:ttf|woff)$/.test(lurl)) | |
567 contentType = "FONT"; | |
568 } | |
569 if (contentType == null) | |
570 contentType = "OTHER"; | |
571 | |
572 if (refHost != "") | |
573 thirdParty = isThirdParty(reqHost, refHost); | |
574 | |
575 if (query != "") | |
576 url = url + "?" + query; | |
577 | |
578 var filter = defaultMatcher.matchesAny(url, contentType, null, thirdParty); | |
579 | |
580 // hack: if there is no referrer block only if filter is domain-specific | |
581 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting) | |
582 if (filter != null && refHost == "" && filter.text.indexOf("||") != 0) | |
583 filter = null; | |
584 | |
585 return (filter != null && !(filter instanceof WhitelistFilter)); | |
586 } | |
587 | |
588 Android.load("XMLHttpRequest.jsm"); | |
589 Android.load("FilterNotifier.jsm"); | |
590 Android.load("FilterClasses.jsm"); | |
591 Android.load("SubscriptionClasses.jsm"); | |
592 Android.load("FilterStorage.jsm"); | |
593 Android.load("FilterListener.jsm"); | |
594 Android.load("Matcher.jsm"); | |
595 Android.load("ElemHide.jsm"); | |
596 Android.load("Synchronizer.jsm"); | |
597 | |
598 FilterListener.startup(); | |
599 Synchronizer.startup(); | |
600 | |
601 Android.load("publicSuffixList.js"); | |
602 Android.load("punycode.js"); | |
603 Android.load("basedomain.js"); | |
OLD | NEW |