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

Delta Between Two Patch Sets: libadblockplus-android/src/org/adblockplus/android/AndroidWebRequest.java

Issue 29351744: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Left Patch Set: added enabled/disabled property Created Oct. 10, 2016, 2:04 p.m.
Right Patch Set: changed packages, now using AdblockEngine (original ABPEngine), improved demo app Created Oct. 25, 2016, 11:20 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « libadblockplus-android/pom.xml ('k') | libadblockplus-android/src/org/adblockplus/android/Utils.java » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 package org.adblockplus.android;
19
20 import java.io.BufferedReader;
21 import java.io.InputStreamReader;
22 import java.net.HttpURLConnection;
23 import java.net.URL;
24 import java.util.HashSet;
25 import java.util.List;
26
27 import org.adblockplus.libadblockplus.AdblockPlusException;
28 import org.adblockplus.libadblockplus.FilterEngine;
29 import org.adblockplus.libadblockplus.HeaderEntry;
30 import org.adblockplus.libadblockplus.ServerResponse;
31 import org.adblockplus.libadblockplus.ServerResponse.NsStatus;
32 import org.adblockplus.libadblockplus.WebRequest;
33
34 import android.util.Log;
35
36 public class AndroidWebRequest extends WebRequest
37 {
38 public final static String TAG = Utils.getTag(WebRequest.class);
39
40 private final HashSet<String> subscriptionURLs = new HashSet<String>();
41 private final boolean elemhideEnabled;
42
43 public AndroidWebRequest(boolean enableElemhide)
44 {
45 this.elemhideEnabled = enableElemhide;
46 }
47
48 public AndroidWebRequest()
49 {
50 this(false);
51 }
52
53 private boolean isListedSubscriptionUrl(final URL url)
54 {
55 String toCheck = url.toString();
56
57 final int idx = toCheck.indexOf('?');
58 if (idx != -1)
59 {
60 toCheck = toCheck.substring(0, idx);
61 }
62
63 return this.subscriptionURLs.contains(toCheck);
64 }
65
66 protected void updateSubscriptionURLs(final FilterEngine engine)
67 {
68 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail ableSubscriptions())
69 {
70 this.subscriptionURLs.add(s.getProperty("url").toString());
71 }
72 this.subscriptionURLs.add(engine.getPref("subscriptions_exceptionsurl").toSt ring());
73 }
74
75 @Override
76 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders)
77 {
78 try
79 {
80 final URL url = new URL(urlStr);
81 Log.d(TAG, "Downloading from: " + url);
82
83 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n();
84 connection.setRequestMethod("GET");
85 connection.connect();
86
87 final ServerResponse response = new ServerResponse();
88 response.setResponseStatus(connection.getResponseCode());
89
90 if (response.getResponseStatus() == 200)
91 {
92 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8"));
93 final StringBuilder sb = new StringBuilder();
94
95 String line;
96 while ((line = reader.readLine()) != null)
97 {
98 // We're only appending non-element-hiding filters here.
99 //
100 // See:
101 // https://issues.adblockplus.org/ticket/303
102 //
103 // Follow-up issue for removing this hack:
104 // https://issues.adblockplus.org/ticket/1541
105 //
106 if (this.elemhideEnabled || !isListedSubscriptionUrl(url) || line.inde xOf('#') == -1)
107 {
108 sb.append(line);
109 sb.append('\n');
110 }
111 }
112
113 connection.disconnect();
114
115 response.setStatus(NsStatus.OK);
116 response.setResponse(sb.toString());
117 }
118 else
119 {
120 response.setStatus(NsStatus.ERROR_FAILURE);
121 }
122 Log.d(TAG, "Downloading finished");
123 return response;
124 }
125 catch (final Throwable t)
126 {
127 Log.e(TAG, "WebRequest failed", t);
128 throw new AdblockPlusException("WebRequest failed", t);
129 }
130 }
131 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld