| 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.IOException; | 21 import java.io.IOException; |
| 22 import java.io.InputStreamReader; | 22 import java.io.InputStreamReader; |
| 23 import java.net.HttpURLConnection; | 23 import java.net.HttpURLConnection; |
| 24 import java.net.URL; | 24 import java.net.URL; |
| 25 import java.nio.charset.StandardCharsets; |
| 25 import java.util.HashMap; | 26 import java.util.HashMap; |
| 26 import java.util.HashSet; | 27 import java.util.HashSet; |
| 27 import java.util.Map; | 28 import java.util.Map; |
| 28 import java.util.Map.Entry; | 29 import java.util.Map.Entry; |
| 29 import java.util.concurrent.LinkedBlockingQueue; | 30 import java.util.concurrent.LinkedBlockingQueue; |
| 30 import java.util.concurrent.TimeUnit; | 31 import java.util.concurrent.TimeUnit; |
| 31 import java.util.concurrent.locks.ReentrantLock; | 32 import java.util.concurrent.locks.ReentrantLock; |
| 32 import android.annotation.SuppressLint; | 33 import android.annotation.SuppressLint; |
| 33 import android.content.Context; | 34 import android.content.Context; |
| 34 import android.util.Log; | 35 import android.util.Log; |
| 35 | 36 |
| 36 @SuppressLint("DefaultLocale") | 37 @SuppressLint("DefaultLocale") |
| 37 final class Downloader | 38 final class Downloader |
| 38 { | 39 { |
| 39 private static final int MAX_RETRIES = 5; | 40 private static final int MAX_RETRIES = 5; |
| 40 | 41 |
| 41 private static final String TAG = Downloader.class.getSimpleName(); | 42 private static final String TAG = Downloader.class.getSimpleName(); |
| 42 private final Engine engine; | 43 private final Engine engine; |
| 43 private final ReentrantLock accessLock = new ReentrantLock(); | 44 private final ReentrantLock accessLock = new ReentrantLock(); |
| 44 private Thread downloaderThread; | 45 private Thread downloaderThread; |
| 45 private LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlockingQueu
e<DownloadJob>(); | 46 private LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlockingQueu
e<>(); |
| 46 private HashSet<String> enqueuedIds = new HashSet<String>(); | 47 private HashSet<String> enqueuedIds = new HashSet<>(); |
| 47 private boolean downloaderEnabled = true; | 48 private boolean downloaderEnabled = true; |
| 48 | 49 |
| 49 private Downloader(final Engine engine) | 50 private Downloader(final Engine engine) |
| 50 { | 51 { |
| 51 this.engine = engine; | 52 this.engine = engine; |
| 52 } | 53 } |
| 53 | 54 |
| 54 void lock() | 55 void lock() |
| 55 { | 56 { |
| 56 this.accessLock.lock(); | 57 this.accessLock.lock(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 final String key = connection.getHeaderFieldKey(i); | 95 final String key = connection.getHeaderFieldKey(i); |
| 95 if (key == null) | 96 if (key == null) |
| 96 { | 97 { |
| 97 break; | 98 break; |
| 98 } | 99 } |
| 99 final String value = connection.getHeaderField(i); | 100 final String value = connection.getHeaderField(i); |
| 100 job.responseHeaders.put(key.toLowerCase(), value); | 101 job.responseHeaders.put(key.toLowerCase(), value); |
| 101 } | 102 } |
| 102 | 103 |
| 103 final StringBuilder sb = new StringBuilder(); | 104 final StringBuilder sb = new StringBuilder(); |
| 104 final BufferedReader r = new BufferedReader(new InputStreamReader(connection
.getInputStream(), | 105 try (final BufferedReader r = new BufferedReader(new InputStreamReader( |
| 105 Engine.CHARSET_UTF_8)); | 106 connection.getInputStream(), StandardCharsets.UTF_8))) |
| 106 try | |
| 107 { | 107 { |
| 108 for (int ch = r.read(); ch != -1; ch = r.read()) | 108 for (int ch = r.read(); ch != -1; ch = r.read()) |
| 109 { | 109 { |
| 110 sb.append((char) ch); | 110 sb.append((char) ch); |
| 111 } | 111 } |
| 112 job.responseText = sb.toString(); | 112 job.responseText = sb.toString(); |
| 113 } | 113 } |
| 114 finally | |
| 115 { | |
| 116 r.close(); | |
| 117 } | |
| 118 } | 114 } |
| 119 | 115 |
| 120 public void enqueueDownload(final URL url, final String id, final Map<String,
String> headers) | 116 public void enqueueDownload(final URL url, final String id, final Map<String,
String> headers) |
| 121 { | 117 { |
| 122 this.lock(); | 118 this.lock(); |
| 123 try | 119 try |
| 124 { | 120 { |
| 125 if (!this.enqueuedIds.contains(id)) | 121 if (!this.enqueuedIds.contains(id)) |
| 126 { | 122 { |
| 127 this.enqueuedIds.add(id); | 123 this.enqueuedIds.add(id); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 153 | 149 |
| 154 public DownloaderHandler(final Downloader downloader) | 150 public DownloaderHandler(final Downloader downloader) |
| 155 { | 151 { |
| 156 this.downloader = downloader; | 152 this.downloader = downloader; |
| 157 } | 153 } |
| 158 | 154 |
| 159 @Override | 155 @Override |
| 160 public void run() | 156 public void run() |
| 161 { | 157 { |
| 162 Log.d(TAG, "Handler thread started"); | 158 Log.d(TAG, "Handler thread started"); |
| 163 final LinkedBlockingQueue<DownloadJob> reQueue = new LinkedBlockingQueue<D
ownloadJob>(); | 159 final LinkedBlockingQueue<DownloadJob> reQueue = new LinkedBlockingQueue<>
(); |
| 164 boolean interrupted = false; | 160 boolean interrupted = false; |
| 165 while (!interrupted) | 161 while (!interrupted) |
| 166 { | 162 { |
| 167 DownloadJob job = null; | 163 DownloadJob job = null; |
| 168 try | 164 try |
| 169 { | 165 { |
| 170 if (!this.downloader.downloaderEnabled) | 166 if (!this.downloader.downloaderEnabled) |
| 171 { | 167 { |
| 172 Thread.sleep(30000); | 168 Thread.sleep(30000); |
| 173 continue; | 169 continue; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 238 } |
| 243 } | 239 } |
| 244 Log.d(TAG, "Handler thread finished"); | 240 Log.d(TAG, "Handler thread finished"); |
| 245 } | 241 } |
| 246 } | 242 } |
| 247 | 243 |
| 248 private static class DownloadJob | 244 private static class DownloadJob |
| 249 { | 245 { |
| 250 private final URL url; | 246 private final URL url; |
| 251 private final String id; | 247 private final String id; |
| 252 private final HashMap<String, String> headers = new HashMap<String, String>(
); | 248 private final HashMap<String, String> headers = new HashMap<>(); |
| 253 private int retryCount = 0; | 249 private int retryCount = 0; |
| 254 | 250 |
| 255 private int responseCode = 0; | 251 private int responseCode = 0; |
| 256 private HashMap<String, String> responseHeaders = new HashMap<String, String
>(); | 252 private HashMap<String, String> responseHeaders = new HashMap<>(); |
| 257 private String responseText = null; | 253 private String responseText = null; |
| 258 | 254 |
| 259 public DownloadJob(final URL url, final String id, final Map<String, String>
headers) | 255 public DownloadJob(final URL url, final String id, final Map<String, String>
headers) |
| 260 { | 256 { |
| 261 this.url = url; | 257 this.url = url; |
| 262 this.id = id; | 258 this.id = id; |
| 263 if (headers != null) | 259 if (headers != null) |
| 264 { | 260 { |
| 265 this.headers.putAll(headers); | 261 this.headers.putAll(headers); |
| 266 } | 262 } |
| 267 } | 263 } |
| 268 } | 264 } |
| 269 } | 265 } |
| OLD | NEW |