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

Side by Side Diff: src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java

Issue 29372653: Issue 4813 - UTFDataFormatException while serializing filter (Closed)
Patch Set: Including UTF-8 constant and adding migration/file corruption recovery logic Created March 9, 2017, 7:38 p.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
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
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 import android.content.Context; 42 import android.content.Context;
43 import android.content.Intent; 43 import android.content.Intent;
44 import android.content.SharedPreferences; 44 import android.content.SharedPreferences;
45 import android.content.pm.ResolveInfo; 45 import android.content.pm.ResolveInfo;
46 import android.net.ConnectivityManager; 46 import android.net.ConnectivityManager;
47 import android.net.NetworkInfo; 47 import android.net.NetworkInfo;
48 import android.net.Uri; 48 import android.net.Uri;
49 import android.os.Handler; 49 import android.os.Handler;
50 import android.os.Looper; 50 import android.os.Looper;
51 import android.preference.PreferenceManager; 51 import android.preference.PreferenceManager;
52 import android.text.TextUtils;
52 import android.util.Log; 53 import android.util.Log;
53 54
54 public final class Engine 55 public final class Engine
55 { 56 {
56 private static final String TAG = Engine.class.getSimpleName(); 57 private static final String TAG = Engine.class.getSimpleName();
57 58
58 // TODO make use of this regex's 59 // TODO make use of this regex's
59 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( 60 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile(
60 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); 61 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE);
61 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)"); 62 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)");
62 public static final Pattern RE_FILTER_ELEMHIDE = Pattern 63 public static final Pattern RE_FILTER_ELEMHIDE = Pattern
63 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$"); 64 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$");
64 public static final Pattern RE_FILTER_REGEXP = Pattern 65 public static final Pattern RE_FILTER_REGEXP = Pattern
65 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$"); 66 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$");
66 public static final Pattern RE_FILTER_OPTIONS = Pattern 67 public static final Pattern RE_FILTER_OPTIONS = Pattern
67 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ; 68 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ;
68 public static final Pattern RE_FILTER_CSSPROPERTY = Pattern 69 public static final Pattern RE_FILTER_CSSPROPERTY = Pattern
69 .compile("\\[\\-abp\\-properties=([\"'])([^\"']+)\\1\\]"); 70 .compile("\\[\\-abp\\-properties=([\"'])([^\"']+)\\1\\]");
70 71
71 public static final String USER_FILTERS_TITLE = "__filters"; 72 public static final String USER_FILTERS_TITLE = "__filters";
72 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; 73 public static final String USER_EXCEPTIONS_TITLE = "__exceptions";
73 74
74 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING"; 75 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING";
75 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE"; 76 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE";
76 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt"; 77 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt";
77 78
78 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl"; 79 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl";
79 80
80 private static final String URL_ENCODE_CHARSET = "UTF-8"; 81 public static final String CHARSET_UTF_8 = "UTF-8";
81 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version "; 82 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version ";
82 83
83 // The value below specifies an interval of [x, 2*x[, where x = 84 // The value below specifies an interval of [x, 2*x[, where x =
84 // INITIAL_UPDATE_CHECK_DELAY_SECONDS 85 // INITIAL_UPDATE_CHECK_DELAY_SECONDS
85 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; 86 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5;
86 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; 87 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30;
87 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; 88 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500;
88 89
89 public static final long MILLIS_PER_SECOND = 1000; 90 public static final long MILLIS_PER_SECOND = 1000;
90 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; 91 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 engine.handlerThread.start(); 434 engine.handlerThread.start();
434 435
435 engine.downloader = Downloader.create(context, engine); 436 engine.downloader = Downloader.create(context, engine);
436 437
437 return engine; 438 return engine;
438 } 439 }
439 440
440 public static String readFileAsString(InputStream instream) throws IOException 441 public static String readFileAsString(InputStream instream) throws IOException
441 { 442 {
442 final StringBuilder sb = new StringBuilder(); 443 final StringBuilder sb = new StringBuilder();
443 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 444 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
444 for (int ch = r.read(); ch != -1; ch = r.read()) 445 for (int ch = r.read(); ch != -1; ch = r.read())
445 { 446 {
446 sb.append((char) ch); 447 sb.append((char) ch);
447 } 448 }
448 return sb.toString(); 449 return sb.toString();
449 } 450 }
450 451
451 public static List<String> readLines(InputStream instream) throws IOException 452 public static List<String> readLines(InputStream instream) throws IOException
452 { 453 {
453 final ArrayList<String> list = new ArrayList<String>(); 454 final ArrayList<String> list = new ArrayList<String>();
454 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 455 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
455 for (String line = r.readLine(); line != null; line = r.readLine()) 456 for (String line = r.readLine(); line != null; line = r.readLine())
456 { 457 {
457 list.add(line); 458 list.add(line);
458 } 459 }
459 return list; 460 return list;
460 } 461 }
461 462
462 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion 463 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion
463 { 464 {
464 final File cachedFilterFile = getCachedFilterFile(context); 465 final File cachedFilterFile = getCachedFilterFile(context);
465 if (cachedFilterFile != null && cachedFilterFile.exists()) 466 if (cachedFilterFile != null && cachedFilterFile.exists())
466 { 467 {
467 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); 468 Log.d(TAG, "Cached filter file found: " + cachedFilterFile);
468 return cachedFilterFile; 469 return cachedFilterFile;
469 } 470 }
470 471
471 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); 472 Log.d(TAG, "Cached filter file not found. Using dummy filter file");
472 final File dummyFilterFile = getDummyFilterFile(context); 473 final File dummyFilterFile = getDummyFilterFile(context);
473 if (!dummyFilterFile.exists()) 474 if (!dummyFilterFile.exists())
474 { 475 {
475 Log.d(TAG, "Creating dummy filter file..."); 476 Log.d(TAG, "Creating dummy filter file...");
476 dummyFilterFile.getParentFile().mkdirs(); 477 dummyFilterFile.getParentFile().mkdirs();
477 final BufferedWriter writer = new BufferedWriter( 478 final BufferedWriter writer = new BufferedWriter(
478 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), "UTF-8") ); 479 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), CHARSET_ UTF_8));
479 try 480 try
480 { 481 {
481 writeFilterHeaders(writer); 482 writeFilterHeaders(writer);
482 } 483 }
483 finally 484 finally
484 { 485 {
485 writer.close(); 486 writer.close();
486 } 487 }
487 } 488 }
488 return dummyFilterFile; 489 return dummyFilterFile;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 if (sub.getURL().getQuery() != null) 530 if (sub.getURL().getQuery() != null)
530 { 531 {
531 sb.append('&'); 532 sb.append('&');
532 } 533 }
533 else 534 else
534 { 535 {
535 sb.append('?'); 536 sb.append('?');
536 } 537 }
537 538
538 sb.append("addonName="); 539 sb.append("addonName=");
539 sb.append(URLEncoder.encode(this.appInfo.addonName, URL_ENCODE_CHARSET)); 540 sb.append(URLEncoder.encode(this.appInfo.addonName, CHARSET_UTF_8));
540 sb.append("&addonVersion="); 541 sb.append("&addonVersion=");
541 sb.append(URLEncoder.encode(this.appInfo.addonVersion, URL_ENCODE_CHARSET)); 542 sb.append(URLEncoder.encode(this.appInfo.addonVersion, CHARSET_UTF_8));
542 sb.append("&application="); 543 sb.append("&application=");
543 sb.append(URLEncoder.encode(this.appInfo.application, URL_ENCODE_CHARSET)); 544 sb.append(URLEncoder.encode(this.appInfo.application, CHARSET_UTF_8));
544 sb.append("&applicationVersion="); 545 sb.append("&applicationVersion=");
545 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, URL_ENCODE_CHAR SET)); 546 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, CHARSET_UTF_8)) ;
546 sb.append("&platform="); 547 sb.append("&platform=");
547 sb.append(URLEncoder.encode(this.appInfo.platform, URL_ENCODE_CHARSET)); 548 sb.append(URLEncoder.encode(this.appInfo.platform, CHARSET_UTF_8));
548 sb.append("&platformVersion="); 549 sb.append("&platformVersion=");
549 sb.append(URLEncoder.encode(this.appInfo.platformVersion, URL_ENCODE_CHARSET )); 550 sb.append(URLEncoder.encode(this.appInfo.platformVersion, CHARSET_UTF_8));
550 sb.append("&lastVersion="); 551 sb.append("&lastVersion=");
551 sb.append(sub.getVersion()); 552 sb.append(sub.getVersion());
552 sb.append("&downloadCount="); 553 sb.append("&downloadCount=");
553 final long downloadCount = sub.getDownloadCount(); 554 final long downloadCount = sub.getDownloadCount();
554 if (downloadCount < 5) 555 if (downloadCount < 5)
555 { 556 {
556 sb.append(downloadCount); 557 sb.append(downloadCount);
557 } 558 }
558 else 559 else
559 { 560 {
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 this.headers.putAll(headers); 705 this.headers.putAll(headers);
705 } 706 }
706 } 707 }
707 } 708 }
708 709
709 public void enqueueDownload(final Subscription sub, final boolean forced) thro ws IOException 710 public void enqueueDownload(final Subscription sub, final boolean forced) thro ws IOException
710 { 711 {
711 if (sub.getURL() != null && sub.shouldUpdate(forced)) 712 if (sub.getURL() != null && sub.shouldUpdate(forced))
712 { 713 {
713 final HashMap<String, String> headers = new HashMap<String, String>(); 714 final HashMap<String, String> headers = new HashMap<String, String>();
714 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIE D); 715 if (sub.isMetaDataValid() && sub.isFiltersValid())
715 if (lastModified != null)
716 { 716 {
717 headers.put("If-Modified-Since", lastModified); 717 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIF IED);
718 } 718 if (!TextUtils.isEmpty(lastModified))
719 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); 719 {
720 if (etag != null) 720 headers.put("If-Modified-Since", lastModified);
721 { 721 }
722 headers.put("If-None-Match", etag); 722 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG);
723 if (!TextUtils.isEmpty(etag))
724 {
725 headers.put("If-None-Match", etag);
726 }
723 } 727 }
724 Log.d(TAG, headers.toString()); 728 Log.d(TAG, headers.toString());
725 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); 729 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
726 } 730 }
727 } 731 }
728 732
729 public void connectivityChanged() 733 public void connectivityChanged()
730 { 734 {
731 this.downloader.connectivityChanged(); 735 this.downloader.connectivityChanged();
732 } 736 }
733 } 737 }
OLDNEW

Powered by Google App Engine
This is Rietveld