| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 package org.adblockplus.sbrowser.contentblocker.engine; | 18 package org.adblockplus.sbrowser.contentblocker.engine; |
| 19 | 19 |
| 20 import java.io.BufferedReader; | 20 import java.io.BufferedReader; |
| 21 import java.io.BufferedWriter; | |
| 21 import java.io.File; | 22 import java.io.File; |
| 23 import java.io.FileOutputStream; | |
| 22 import java.io.IOException; | 24 import java.io.IOException; |
| 23 import java.io.InputStream; | 25 import java.io.InputStream; |
| 24 import java.io.InputStreamReader; | 26 import java.io.InputStreamReader; |
| 27 import java.io.OutputStreamWriter; | |
| 28 import java.io.Writer; | |
| 25 import java.net.URL; | 29 import java.net.URL; |
| 26 import java.net.URLEncoder; | 30 import java.net.URLEncoder; |
| 27 import java.util.ArrayList; | 31 import java.util.ArrayList; |
| 28 import java.util.HashMap; | 32 import java.util.HashMap; |
| 29 import java.util.List; | 33 import java.util.List; |
| 30 import java.util.Map; | 34 import java.util.Map; |
| 31 import java.util.concurrent.LinkedBlockingQueue; | 35 import java.util.concurrent.LinkedBlockingQueue; |
| 32 import java.util.concurrent.TimeUnit; | 36 import java.util.concurrent.TimeUnit; |
| 33 import java.util.concurrent.locks.ReentrantLock; | 37 import java.util.concurrent.locks.ReentrantLock; |
| 34 import java.util.regex.Pattern; | 38 import java.util.regex.Pattern; |
| 35 | 39 |
| 36 import org.adblockplus.adblockplussbrowser.R; | 40 import org.adblockplus.adblockplussbrowser.R; |
| 37 | 41 |
| 38 import android.content.Context; | 42 import android.content.Context; |
| 39 import android.content.Intent; | 43 import android.content.Intent; |
| 40 import android.content.SharedPreferences; | 44 import android.content.SharedPreferences; |
| 41 import android.content.pm.ResolveInfo; | 45 import android.content.pm.ResolveInfo; |
| 42 import android.net.ConnectivityManager; | 46 import android.net.ConnectivityManager; |
| 43 import android.net.NetworkInfo; | 47 import android.net.NetworkInfo; |
| 44 import android.net.Uri; | 48 import android.net.Uri; |
| 45 import android.os.Handler; | 49 import android.os.Handler; |
| 46 import android.os.Looper; | 50 import android.os.Looper; |
| 47 import android.preference.PreferenceManager; | 51 import android.preference.PreferenceManager; |
| 48 import android.util.Log; | 52 import android.util.Log; |
| 49 | 53 |
| 50 public final class Engine | 54 public final class Engine |
| 51 { | 55 { |
| 52 private static final String TAG = Engine.class.getSimpleName(); | 56 private static final String TAG = Engine.class.getSimpleName(); |
| 53 | 57 |
| 58 public static final String ABP_VERSION = "2.0"; | |
|
Felix Dahlke
2016/09/30 07:11:41
I think we can safely hard code it (the way it was
diegocarloslima
2016/09/30 14:06:38
OK I'll revert that change
| |
| 59 | |
| 54 // TODO make use of this regex's | 60 // TODO make use of this regex's |
| 55 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( | 61 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( |
| 56 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); | 62 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); |
| 57 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)"); | 63 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)"); |
| 58 public static final Pattern RE_FILTER_ELEMHIDE = Pattern | 64 public static final Pattern RE_FILTER_ELEMHIDE = Pattern |
| 59 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$"); | 65 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$"); |
| 60 public static final Pattern RE_FILTER_REGEXP = Pattern | 66 public static final Pattern RE_FILTER_REGEXP = Pattern |
| 61 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$"); | 67 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$"); |
| 62 public static final Pattern RE_FILTER_OPTIONS = Pattern | 68 public static final Pattern RE_FILTER_OPTIONS = Pattern |
| 63 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ; | 69 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY_MILLIS; | 163 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY_MILLIS; |
| 158 } | 164 } |
| 159 finally | 165 finally |
| 160 { | 166 { |
| 161 this.unlock(); | 167 this.unlock(); |
| 162 } | 168 } |
| 163 } | 169 } |
| 164 | 170 |
| 165 private void sendUpdateBroadcast() | 171 private void sendUpdateBroadcast() |
| 166 { | 172 { |
| 173 createAndWriteFile(); | |
| 174 | |
| 167 runOnUiThread(new Runnable() | 175 runOnUiThread(new Runnable() |
| 168 { | 176 { |
| 169 @Override | 177 @Override |
| 170 public void run() | 178 public void run() |
| 171 { | 179 { |
| 172 final Intent intent = new Intent(); | 180 final Intent intent = new Intent(); |
| 173 intent.setAction(ACTION_UPDATE); | 181 intent.setAction(ACTION_UPDATE); |
| 174 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack ageName())); | 182 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack ageName())); |
| 175 Engine.this.serviceContext.sendBroadcast(intent); | 183 Engine.this.serviceContext.sendBroadcast(intent); |
| 176 } | 184 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 { | 237 { |
| 230 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled)); | 238 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled)); |
| 231 } | 239 } |
| 232 | 240 |
| 233 void downloadFinished(final String id, final int responseCode, final String re sponse, | 241 void downloadFinished(final String id, final int responseCode, final String re sponse, |
| 234 final Map<String, String> headers) | 242 final Map<String, String> headers) |
| 235 { | 243 { |
| 236 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers)); | 244 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers)); |
| 237 } | 245 } |
| 238 | 246 |
| 239 public File createAndWriteFile() throws IOException | 247 public void createAndWriteFile() |
| 240 { | 248 { |
| 241 this.lock(); | 249 this.lock(); |
| 242 try | 250 try |
| 243 { | 251 { |
| 244 Log.d(TAG, "Writing filters..."); | 252 Log.d(TAG, "Writing filters..."); |
| 245 return this.subscriptions.createAndWriteFile(); | 253 final File filterFile = this.subscriptions.createAndWriteFile(); |
| 254 | |
| 255 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen ces(this.serviceContext); | |
| 256 final String key = this.serviceContext.getString(R.string.key_cached_filte r_path); | |
| 257 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit(); | |
| 258 | |
| 259 } | |
| 260 catch (IOException e) | |
| 261 { | |
| 262 Log.e(TAG, "Failed to write filters", e); | |
| 246 } | 263 } |
| 247 finally | 264 finally |
| 248 { | 265 { |
| 249 this.unlock(); | 266 this.unlock(); |
| 250 } | 267 } |
| 251 } | 268 } |
| 252 | 269 |
| 253 public static void runOnUiThread(final Runnable runnable) | 270 public static void runOnUiThread(final Runnable runnable) |
| 254 { | 271 { |
| 255 new Handler(Looper.getMainLooper()).post(runnable); | 272 new Handler(Looper.getMainLooper()).post(runnable); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 try | 337 try |
| 321 { | 338 { |
| 322 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription sXml); | 339 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription sXml); |
| 323 } | 340 } |
| 324 finally | 341 finally |
| 325 { | 342 { |
| 326 subscriptionsXml.close(); | 343 subscriptionsXml.close(); |
| 327 } | 344 } |
| 328 | 345 |
| 329 Log.d(TAG, "Finished reading 'subscriptions.xml'"); | 346 Log.d(TAG, "Finished reading 'subscriptions.xml'"); |
| 330 engine.subscriptions = Subscriptions.initialize(engine, context.getFilesDir( ), | 347 engine.subscriptions = Subscriptions.initialize(engine, getSubscriptionsDir( context), |
| 331 context.getCacheDir()); | 348 getFilterCacheDir(context)); |
| 332 | 349 |
| 333 final InputStream prefsJson = context.getResources().openRawResource(R.raw.p refs); | 350 final InputStream prefsJson = context.getResources().openRawResource(R.raw.p refs); |
| 334 try | 351 try |
| 335 { | 352 { |
| 336 engine.jsonPrefs = JSONPrefs.create(prefsJson); | 353 engine.jsonPrefs = JSONPrefs.create(prefsJson); |
| 337 } | 354 } |
| 338 finally | 355 finally |
| 339 { | 356 { |
| 340 prefsJson.close(); | 357 prefsJson.close(); |
| 341 } | 358 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 { | 403 { |
| 387 additional++; | 404 additional++; |
| 388 engine.subscriptions.add(sub); | 405 engine.subscriptions.add(sub); |
| 389 } | 406 } |
| 390 } | 407 } |
| 391 | 408 |
| 392 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions"); | 409 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions"); |
| 393 engine.subscriptions.persistSubscriptions(); | 410 engine.subscriptions.persistSubscriptions(); |
| 394 } | 411 } |
| 395 | 412 |
| 413 final File cachedFilterFile = getCachedFilterFile(context); | |
| 414 if (cachedFilterFile == null || !cachedFilterFile.exists()) | |
| 415 { | |
| 416 engine.sendUpdateBroadcast(); | |
| 417 } | |
| 418 | |
| 396 engine.handlerThread = new Thread(new EventHandler(engine)); | 419 engine.handlerThread = new Thread(new EventHandler(engine)); |
| 397 engine.handlerThread.setDaemon(true); | 420 engine.handlerThread.setDaemon(true); |
| 398 engine.handlerThread.start(); | 421 engine.handlerThread.start(); |
| 399 | 422 |
| 400 engine.downloader = Downloader.create(context, engine); | 423 engine.downloader = Downloader.create(context, engine); |
| 401 | 424 |
| 402 return engine; | 425 return engine; |
| 403 } | 426 } |
| 404 | 427 |
| 405 public static String readFileAsString(InputStream instream) throws IOException | 428 public static String readFileAsString(InputStream instream) throws IOException |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 417 { | 440 { |
| 418 final ArrayList<String> list = new ArrayList<String>(); | 441 final ArrayList<String> list = new ArrayList<String>(); |
| 419 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); | 442 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); |
| 420 for (String line = r.readLine(); line != null; line = r.readLine()) | 443 for (String line = r.readLine(); line != null; line = r.readLine()) |
| 421 { | 444 { |
| 422 list.add(line); | 445 list.add(line); |
| 423 } | 446 } |
| 424 return list; | 447 return list; |
| 425 } | 448 } |
| 426 | 449 |
| 450 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion | |
| 451 { | |
| 452 final File cachedFilterFile = getCachedFilterFile(context); | |
| 453 if (cachedFilterFile != null && cachedFilterFile.exists()) | |
| 454 { | |
| 455 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); | |
| 456 return cachedFilterFile; | |
| 457 } | |
| 458 | |
| 459 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); | |
| 460 final File dummyFilterFile = getDummyFilterFile(context); | |
| 461 if (!dummyFilterFile.exists()) | |
| 462 { | |
| 463 Log.d(TAG, "Creating dummy filter file..."); | |
| 464 dummyFilterFile.getParentFile().mkdirs(); | |
| 465 final BufferedWriter writer = new BufferedWriter( | |
| 466 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), "UTF-8") ); | |
| 467 try | |
| 468 { | |
| 469 writeFilterHeaders(writer); | |
| 470 } | |
| 471 finally | |
| 472 { | |
| 473 writer.close(); | |
| 474 } | |
| 475 } | |
| 476 return dummyFilterFile; | |
| 477 } | |
| 478 | |
| 479 public static void writeFilterHeaders(Writer writer) throws IOException | |
| 480 { | |
| 481 writer.write("[Adblock Plus" + ABP_VERSION + "]\n"); | |
|
Felix Dahlke
2016/09/30 07:11:41
Shouldn't there be a space between "Adblock Plus"
| |
| 482 writer.write("! This file was automatically created.\n"); | |
| 483 } | |
| 484 | |
| 485 private static File getCachedFilterFile(Context context) | |
| 486 { | |
| 487 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); | |
| 488 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); | |
| 489 if (cachedFilterPath != null) | |
| 490 { | |
| 491 return new File(cachedFilterPath); | |
| 492 } | |
| 493 | |
| 494 return null; | |
| 495 } | |
| 496 | |
| 497 private static File getDummyFilterFile(Context context) | |
| 498 { | |
| 499 return new File(getFilterCacheDir(context), "dummy.txt"); | |
| 500 } | |
| 501 | |
| 502 private static File getFilterCacheDir(Context context) | |
| 503 { | |
| 504 return new File(context.getCacheDir(), "subscriptions"); | |
| 505 } | |
| 506 | |
| 507 private static File getSubscriptionsDir(Context context) | |
| 508 { | |
| 509 return new File(context.getFilesDir(), "subscriptions"); | |
| 510 } | |
| 511 | |
| 427 URL createDownloadURL(final Subscription sub) throws IOException | 512 URL createDownloadURL(final Subscription sub) throws IOException |
| 428 { | 513 { |
| 429 final StringBuilder sb = new StringBuilder(); | 514 final StringBuilder sb = new StringBuilder(); |
| 430 | 515 |
| 431 sb.append(sub.getURL()); | 516 sb.append(sub.getURL()); |
| 432 if (sub.getURL().getQuery() != null) | 517 if (sub.getURL().getQuery() != null) |
| 433 { | 518 { |
| 434 sb.append('&'); | 519 sb.append('&'); |
| 435 } | 520 } |
| 436 else | 521 else |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 627 Log.d(TAG, headers.toString()); | 712 Log.d(TAG, headers.toString()); |
| 628 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); | 713 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); |
| 629 } | 714 } |
| 630 } | 715 } |
| 631 | 716 |
| 632 public void connectivityChanged() | 717 public void connectivityChanged() |
| 633 { | 718 { |
| 634 this.downloader.connectivityChanged(); | 719 this.downloader.connectivityChanged(); |
| 635 } | 720 } |
| 636 } | 721 } |
| OLD | NEW |