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

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

Issue 29445587: Issue 5223 - App is freezing sometimes (Closed)
Patch Set: Created May 22, 2017, 10:04 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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 android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.net.ConnectivityManager;
27 import android.net.NetworkInfo;
28 import android.net.Uri;
29 import android.os.AsyncTask;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.preference.PreferenceManager;
33 import android.text.TextUtils;
34 import android.util.Log;
35
36 import org.adblockplus.adblockplussbrowser.R;
37
20 import java.io.BufferedReader; 38 import java.io.BufferedReader;
21 import java.io.BufferedWriter; 39 import java.io.BufferedWriter;
22 import java.io.File; 40 import java.io.File;
23 import java.io.FileOutputStream; 41 import java.io.FileOutputStream;
24 import java.io.IOException; 42 import java.io.IOException;
25 import java.io.InputStream; 43 import java.io.InputStream;
26 import java.io.InputStreamReader; 44 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter; 45 import java.io.OutputStreamWriter;
28 import java.io.Writer; 46 import java.io.Writer;
29 import java.net.URI; 47 import java.net.URI;
30 import java.net.URISyntaxException; 48 import java.net.URISyntaxException;
31 import java.net.URL; 49 import java.net.URL;
32 import java.net.URLEncoder; 50 import java.net.URLEncoder;
33 import java.util.ArrayList; 51 import java.util.ArrayList;
34 import java.util.Collections; 52 import java.util.Collections;
35 import java.util.HashMap; 53 import java.util.HashMap;
36 import java.util.List; 54 import java.util.List;
37 import java.util.Map; 55 import java.util.Map;
38 import java.util.Set; 56 import java.util.Set;
39 import java.util.TreeSet; 57 import java.util.TreeSet;
40 import java.util.concurrent.LinkedBlockingQueue; 58 import java.util.concurrent.LinkedBlockingQueue;
41 import java.util.concurrent.TimeUnit; 59 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.locks.ReentrantLock; 60 import java.util.concurrent.locks.ReentrantLock;
43 import java.util.regex.Pattern; 61 import java.util.regex.Pattern;
44 62
45 import org.adblockplus.adblockplussbrowser.R;
46 import org.adblockplus.sbrowser.contentblocker.MainPreferences;
47
48 import android.content.Context;
49 import android.content.Intent;
50 import android.content.SharedPreferences;
51 import android.content.pm.PackageInfo;
52 import android.content.pm.PackageManager;
53 import android.content.pm.ResolveInfo;
54 import android.net.ConnectivityManager;
55 import android.net.NetworkInfo;
56 import android.net.Uri;
57 import android.os.Handler;
58 import android.os.Looper;
59 import android.preference.PreferenceManager;
60 import android.text.TextUtils;
61 import android.util.Log;
62
63 public final class Engine 63 public final class Engine
64 { 64 {
65 private static final String TAG = Engine.class.getSimpleName(); 65 private static final String TAG = Engine.class.getSimpleName();
66 66
67 // TODO make use of this regex's 67 // TODO make use of this regex's
68 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( 68 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile(
69 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); 69 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE);
70 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)"); 70 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)");
71 public static final Pattern RE_FILTER_ELEMHIDE = Pattern 71 public static final Pattern RE_FILTER_ELEMHIDE = Pattern
72 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$"); 72 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$");
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 } 273 }
274 274
275 void downloadFinished(final String id, final int responseCode, final String re sponse, 275 void downloadFinished(final String id, final int responseCode, final String re sponse,
276 final Map<String, String> headers) 276 final Map<String, String> headers)
277 { 277 {
278 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers)); 278 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers));
279 } 279 }
280 280
281 private void createAndWriteFile() 281 private void createAndWriteFile()
282 { 282 {
283 this.lock(); 283 new CreateAndWriteFileAsyncTask().execute(serviceContext, subscriptions);
diegocarloslima 2017/05/25 21:20:45 I might be wrong, but it seems that the freezing i
jens 2017/06/01 15:32:08 Acknowledged.
284 try
285 {
286 Log.d(TAG, "Writing filters...");
287 final File filterFile = this.subscriptions.createAndWriteFile();
288 writeWhitelistedWebsites(this.serviceContext, filterFile);
289
290 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen ces(this.serviceContext);
291 final String key = this.serviceContext.getString(R.string.key_cached_filte r_path);
292 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit();
293
294 Log.d(TAG, "Cleaning up cache...");
295 final File dummyFile = getDummyFilterFile(this.serviceContext);
296 final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi les();
297 if (cacheDirFiles != null)
298 {
299 for (final File file : cacheDirFiles)
300 {
301 if (!file.equals(dummyFile) && !file.equals(filterFile))
302 {
303 Log.d(TAG, "Deleting file:" + file);
304 file.delete();
305 }
306 }
307 }
308 }
309 catch (IOException e)
310 {
311 Log.e(TAG, "Failed to write filters", e);
312 }
313 finally
314 {
315 this.unlock();
316 }
317 } 284 }
318 285
319 public static void runOnUiThread(final Runnable runnable) 286 public static void runOnUiThread(final Runnable runnable)
320 { 287 {
321 new Handler(Looper.getMainLooper()).post(runnable); 288 new Handler(Looper.getMainLooper()).post(runnable);
322 } 289 }
323 290
324 public boolean isAcceptableAdsEnabled() 291 public boolean isAcceptableAdsEnabled()
325 { 292 {
326 this.lock(); 293 this.lock();
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 } 766 }
800 Log.d(TAG, headers.toString()); 767 Log.d(TAG, headers.toString());
801 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); 768 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
802 } 769 }
803 } 770 }
804 771
805 public void connectivityChanged() 772 public void connectivityChanged()
806 { 773 {
807 this.downloader.connectivityChanged(); 774 this.downloader.connectivityChanged();
808 } 775 }
776
777 private static class CreateAndWriteFileAsyncTask extends AsyncTask<Object, Voi d, Void>
778 {
779 private static final String TAG = CreateAndWriteFileAsyncTask.class.getSimpl eName();
780
781 @Override
782 protected Void doInBackground(Object... params)
783 {
784 final Context serviceContext = (Context) params[0];
785 final Subscriptions subscriptions = (Subscriptions) params[1];
786
787 createAndWriteFile(serviceContext, subscriptions);
788 return null;
789 }
790
791 private void createAndWriteFile(Context serviceContext, Subscriptions subscr iptions)
792 {
793 try
794 {
795 Log.d(TAG, "Writing filters...");
796 final File filterFile = subscriptions.createAndWriteFile();
797 writeWhitelistedWebsites(serviceContext, filterFile);
798
799 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPrefer ences(serviceContext);
800 final String key = serviceContext.getString(R.string.key_cached_filter_p ath);
801 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit();
802
803 Log.d(TAG, "Cleaning up cache...");
804 final File dummyFile = getDummyFilterFile(serviceContext);
805 final File[] cacheDirFiles = getFilterCacheDir(serviceContext).listFiles ();
806 if (cacheDirFiles != null)
807 {
808 for (final File file : cacheDirFiles)
809 {
810 if (!file.equals(dummyFile) && !file.equals(filterFile))
811 {
812 Log.d(TAG, "Deleting file:" + file);
813 file.delete();
814 }
815 }
816 }
817 }
818 catch (IOException e)
819 {
820 Log.e(TAG, "Failed to write filters", e);
821 }
822 }
823
824 private void writeWhitelistedWebsites(Context context, File filterFile) thro ws IOException
825 {
826 Log.d(TAG, "Writing whitelisted websites...");
827 final SharedPreferences prefs =
828 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCo ntext());
829 final String key = context.getString(R.string.key_whitelisted_websites);
830
831 final Set<String> whitelistedWebsites = new TreeSet<>();
832 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>emp tySet()));
833
834 try (BufferedWriter w = new BufferedWriter(
835 new OutputStreamWriter(new FileOutputStream(filterFile, true), Engine. CHARSET_UTF_8))) {
836 for (final String url : whitelistedWebsites) {
837 try {
838 final URI uri = new URI(url);
839 final String host = uri.getHost() != null ? uri.getHost() : uri.getP ath();
840 w.write("@@||" + host + "^$document");
841 w.write('\n');
842 } catch (URISyntaxException e) {
843 Log.w(TAG, "Failed to parse whitelisted website: " + url);
844 }
845 }
846 }
847 }
848
849 private static File getDummyFilterFile(Context context)
850 {
851 return new File(getFilterCacheDir(context), "dummy.txt");
852 }
853
854 private static File getFilterCacheDir(Context context)
855 {
856 return new File(context.getCacheDir(), "subscriptions");
857 }
858 }
809 } 859 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld