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

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

Issue 29389555: Issue 5010 - Allow to preload subscription files (Closed)
Left Patch Set: Created March 20, 2017, 8:11 a.m.
Right Patch Set: force downloading actually Created March 30, 2017, 10:12 p.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
LEFTRIGHT
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 "https://easylist-downloads.adblockplus.org/liste_fr+easylist.txt"; 69 "https://easylist-downloads.adblockplus.org/liste_fr+easylist.txt";
70 public static final String EASYLIST_ROMANIAN = 70 public static final String EASYLIST_ROMANIAN =
71 "https://easylist-downloads.adblockplus.org/rolist+easylist.txt"; 71 "https://easylist-downloads.adblockplus.org/rolist+easylist.txt";
72 public static final String EASYLIST_RUSSIAN = 72 public static final String EASYLIST_RUSSIAN =
73 "https://easylist-downloads.adblockplus.org/ruadlist+easylist.txt"; 73 "https://easylist-downloads.adblockplus.org/ruadlist+easylist.txt";
74 public static final String ACCEPTABLE_ADS = 74 public static final String ACCEPTABLE_ADS =
75 "https://easylist-downloads.adblockplus.org/exceptionrules.txt"; 75 "https://easylist-downloads.adblockplus.org/exceptionrules.txt";
76 76
77 private Context context; 77 private Context context;
78 private WebRequest request; 78 private WebRequest request;
79 private Map<String, Integer> URLtoResouceIdMap; 79 private Map<String, Integer> urlToResourceIdMap;
80 private Storage storage; 80 private Storage storage;
81 private Listener listener; 81 private Listener listener;
82 82
83 /** 83 /**
84 * Constructor 84 * Constructor
85 * @param context android context 85 * @param context android context
86 * @param request wrapped request to perform the request if it's not preloaded subscription requested 86 * @param request wrapped request to perform the request if it's not preloaded subscription requested
87 * @param URLtoResourceIdMap map URL -> android resource id for preloaded subs criptions 87 * @param urlToResourceIdMap map URL -> android resource id for preloaded subs criptions
88 * See AndroidWebRequestResourceWrapper.EASYLIST_... constants 88 * See AndroidWebRequestResourceWrapper.EASYLIST_... constants
89 * @param storage Storage impl to remember served interceptions 89 * @param storage Storage impl to remember served interceptions
90 */ 90 */
91 public AndroidWebRequestResourceWrapper(Context context, WebRequest request, 91 public AndroidWebRequestResourceWrapper(Context context, WebRequest request,
92 Map<String, Integer> URLtoResourceIdMa p, 92 Map<String, Integer> urlToResourceIdMa p,
anton 2017/03/20 08:28:46 using map allows to pass multiple mappings and des
93 Storage storage) 93 Storage storage)
94 { 94 {
95 this.context = context; 95 this.context = context;
96 this.request = request; 96 this.request = request;
97 this.URLtoResouceIdMap = Collections.synchronizedMap(URLtoResourceIdMap); 97 this.urlToResourceIdMap = Collections.synchronizedMap(urlToResourceIdMap);
98 this.storage = storage; 98 this.storage = storage;
99 } 99 }
100 100
101 public Listener getListener() 101 public Listener getListener()
102 { 102 {
103 return listener; 103 return listener;
104 } 104 }
105 105
106 public void setListener(Listener listener) 106 public void setListener(Listener listener)
107 { 107 {
108 this.listener = listener; 108 this.listener = listener;
109 } 109 }
110 110
111 @Override 111 @Override
112 public ServerResponse httpGET(String url, List<HeaderEntry> headers) 112 public ServerResponse httpGET(String url, List<HeaderEntry> headers)
113 { 113 {
114 // since parameters may vary we need to ignore them 114 // since parameters may vary we need to ignore them
115 String urlWithoutParams = url.substring(0, url.indexOf("?")); 115 String urlWithoutParams = url.substring(0, url.indexOf("?"));
116 Integer resourceId = URLtoResouceIdMap.get(urlWithoutParams); 116 Integer resourceId = urlToResourceIdMap.get(urlWithoutParams);
117 117
118 if (resourceId != null) 118 if (resourceId != null)
119 { 119 {
120 if (!storage.contains(urlWithoutParams)) 120 if (!storage.contains(urlWithoutParams))
121 { 121 {
122 Log.w(TAG, "Intercepting request for " + url + " with resource #" + reso urceId.intValue()); 122 Log.w(TAG, "Intercepting request for " + url + " with resource #" + reso urceId.intValue());
123 ServerResponse response = buildResourceContentResponse(resourceId); 123 ServerResponse response = buildResourceContentResponse(resourceId);
124 storage.put(urlWithoutParams); 124 storage.put(urlWithoutParams);
125 125
126 if (listener != null) 126 if (listener != null)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 @Override 199 @Override
200 public void dispose() 200 public void dispose()
201 { 201 {
202 request.dispose(); 202 request.dispose();
203 super.dispose(); 203 super.dispose();
204 } 204 }
205 205
206 /** 206 /**
207 * Listener for events 207 * Listener for events
208 */ 208 */
209 public interface Listener 209 public interface Listener
anton 2017/03/20 08:28:45 listener is introduced for the only purpose - we n
210 { 210 {
211 void onIntercepted(String url, int resourceId); 211 void onIntercepted(String url, int resourceId);
212 } 212 }
213 213
214 /** 214 /**
215 * Interface to remember intercepted subscription requests 215 * Interface to remember intercepted subscription requests
216 */ 216 */
217 public interface Storage 217 public interface Storage
218 { 218 {
219 void put(String url); 219 void put(String url);
220 boolean contains(String url); 220 boolean contains(String url);
221 } 221 }
222 222
223 /** 223 /**
224 * Storage impl in Shared Preferences 224 * Storage impl in Shared Preferences
225 */ 225 */
226 public static class SharedPrefsStorage implements Storage 226 public static class SharedPrefsStorage implements Storage
anton 2017/03/20 08:28:46 The only iml for now. Since we use SharedPrefsStor
227 { 227 {
228 private static final String URLS = "urls"; 228 private static final String URLS = "urls";
229 229
230 private SharedPreferences prefs; 230 private SharedPreferences prefs;
231 private Set<String> urls; 231 private Set<String> urls;
232 232
233 public SharedPrefsStorage(SharedPreferences prefs) 233 public SharedPrefsStorage(SharedPreferences prefs)
234 { 234 {
235 this.prefs = prefs; 235 this.prefs = prefs;
236 this.urls = prefs.getStringSet(URLS, new HashSet<String>()); 236 this.urls = prefs.getStringSet(URLS, new HashSet<String>());
(...skipping 10 matching lines...) Expand all
247 .commit(); 247 .commit();
248 } 248 }
249 249
250 @Override 250 @Override
251 public synchronized boolean contains(String url) 251 public synchronized boolean contains(String url)
252 { 252 {
253 return urls.contains(url); 253 return urls.contains(url);
254 } 254 }
255 } 255 }
256 } 256 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld