2 * Copyright © 2018-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.helpers;
22 import android.content.res.AssetManager;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
31 public class BlocklistHelper {
32 // Describe the schema of the string array in each entry of the resource requests array list.
33 public final static int REQUEST_DISPOSITION = 0;
34 public final static int REQUEST_URL = 1;
35 public final static int REQUEST_BLOCKLIST = 2;
36 public final static int REQUEST_SUBLIST = 3;
37 public final static int REQUEST_BLOCKLIST_ENTRIES = 4;
38 public final static int REQUEST_BLOCKLIST_ORIGINAL_ENTRY = 5;
40 // The request disposition options.
41 public final static String REQUEST_DEFAULT = "0";
42 public final static String REQUEST_ALLOWED = "1";
43 public final static String REQUEST_THIRD_PARTY = "2";
44 public final static String REQUEST_BLOCKED = "3";
47 public final static String MAIN_WHITELIST = "1";
48 public final static String FINAL_WHITELIST = "2";
49 public final static String DOMAIN_WHITELIST = "3";
50 public final static String DOMAIN_INITIAL_WHITELIST = "4";
51 public final static String DOMAIN_FINAL_WHITELIST = "5";
52 public final static String THIRD_PARTY_WHITELIST = "6";
53 public final static String THIRD_PARTY_DOMAIN_WHITELIST = "7";
54 public final static String THIRD_PARTY_DOMAIN_INITIAL_WHITELIST = "8";
57 public final static String MAIN_BLACKLIST = "9";
58 public final static String INITIAL_BLACKLIST = "10";
59 public final static String FINAL_BLACKLIST = "11";
60 public final static String DOMAIN_BLACKLIST = "12";
61 public final static String DOMAIN_INITIAL_BLACKLIST = "13";
62 public final static String DOMAIN_FINAL_BLACKLIST = "14";
63 public final static String DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "15";
64 public final static String THIRD_PARTY_BLACKLIST = "16";
65 public final static String THIRD_PARTY_INITIAL_BLACKLIST = "17";
66 public final static String THIRD_PARTY_DOMAIN_BLACKLIST = "18";
67 public final static String THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST = "19";
68 public final static String THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST = "20";
69 public final static String THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "21";
70 public final static String REGULAR_EXPRESSION_BLACKLIST = "22";
72 public ArrayList<List<String[]>> parseBlocklist(AssetManager assets, String blocklistName) {
73 // Initialize the header list.
74 List<String[]> headers = new ArrayList<>(); // 0.
76 // Initialize the whitelists.
77 List<String[]> mainWhitelist = new ArrayList<>(); // 1.
78 List<String[]> finalWhitelist = new ArrayList<>(); // 2.
79 List<String[]> domainWhitelist = new ArrayList<>(); // 3.
80 List<String[]> domainInitialWhitelist = new ArrayList<>(); // 4.
81 List<String[]> domainFinalWhitelist = new ArrayList<>(); // 5.
82 List<String[]> thirdPartyWhitelist = new ArrayList<>(); // 6.
83 List<String[]> thirdPartyDomainWhitelist = new ArrayList<>(); // 7.
84 List<String[]> thirdPartyDomainInitialWhitelist = new ArrayList<>(); // 8.
86 // Initialize the blacklists
87 List<String[]> mainBlacklist = new ArrayList<>(); // 9.
88 List<String[]> initialBlacklist = new ArrayList<>(); // 10.
89 List<String[]> finalBlacklist = new ArrayList<>(); // 11.
90 List<String[]> domainBlacklist = new ArrayList<>(); // 12.
91 List<String[]> domainInitialBlacklist = new ArrayList<>(); // 13.
92 List<String[]> domainFinalBlacklist = new ArrayList<>(); // 14.
93 List<String[]> domainRegularExpressionBlacklist = new ArrayList<>(); // 15.
94 List<String[]> thirdPartyBlacklist = new ArrayList<>(); // 16.
95 List<String[]> thirdPartyInitialBlacklist = new ArrayList<>(); // 17.
96 List<String[]> thirdPartyDomainBlacklist = new ArrayList<>(); // 18.
97 List<String[]> thirdPartyDomainInitialBlacklist = new ArrayList<>(); // 19.
98 List<String[]> regularExpressionBlacklist = new ArrayList<>(); // 20.
99 List<String[]> thirdPartyRegularExpressionBlacklist = new ArrayList<>(); // 21.
100 List<String[]> thirdPartyDomainRegularExpressionBlacklist = new ArrayList<>(); // 22.
103 // Populate the block lists. The `try` is required by `InputStreamReader`.
105 // Load the block list into a `BufferedReader`.
106 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(assets.open(blocklistName)));
108 // Create a string for storing the block list entries.
109 String blocklistEntry;
111 // Parse the block list.
112 while ((blocklistEntry = bufferedReader.readLine()) != null) {
113 // Store the original block list entry.
114 String originalBlocklistEntry = blocklistEntry;
116 // Remove any `^` from the block list entry. Privacy Browser does not process them in the interest of efficiency.
117 blocklistEntry = blocklistEntry.replace("^", "");
119 //noinspection StatementWithEmptyBody
120 if (blocklistEntry.contains("##") || blocklistEntry.contains("#?#") || blocklistEntry.contains("#@#") || blocklistEntry.startsWith("[")) {
121 // Entries that contain `##`, `#?#`, and `#@#` are for hiding elements in the main page's HTML. Entries that start with `[` describe the AdBlock compatibility level.
122 // Do nothing. Privacy Browser does not currently use these entries.
124 //Log.i("Blocklists", "Not added: " + blocklistEntry);
125 } else //noinspection StatementWithEmptyBody
126 if (blocklistEntry.contains("$csp=script-src")) { // Ignore entries that contain `$csp=script-src`.
127 // Do nothing. It is uncertain what this directive is even supposed to mean, and it is blocking entire websites like androidcentral.com. https://redmine.stoutner.com/issues/306.
129 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
130 } else //noinspection StatementWithEmptyBody
131 if (blocklistEntry.contains("$websocket") || blocklistEntry.contains("$third-party,websocket") || blocklistEntry.contains("$script,websocket")) { // Ignore entries with `websocket`.
132 // Do nothing. Privacy Browser does not differentiate between websocket requests and other requests and these entries cause a lot of false positives.
134 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
135 } else if (blocklistEntry.startsWith("!")) { // Comment entries.
136 if (blocklistEntry.startsWith("! Version:")) {
137 // Get the list version number.
138 String[] listVersion = {blocklistEntry.substring(11)};
140 // Store the list version in the headers list.
141 headers.add(listVersion);
144 if (blocklistEntry.startsWith("! Title:")) {
145 // Get the list title.
146 String[] listTitle = {blocklistEntry.substring(9)};
148 // Store the list title in the headers list.
149 headers.add(listTitle);
152 //Log.i("Blocklists", "Not added: " + blocklistEntry);
153 } else if (blocklistEntry.startsWith("@@")) { // Entries that begin with `@@` are whitelists.
155 blocklistEntry = blocklistEntry.substring(2);
157 // Strip out any initial `||`. Privacy Browser doesn't differentiate items that only match against the end of the domain name.
158 if (blocklistEntry.startsWith("||")) {
159 blocklistEntry = blocklistEntry.substring(2);
162 if (blocklistEntry.contains("$")) { // Filter entries.
163 //noinspection StatementWithEmptyBody
164 if (blocklistEntry.contains("~third-party")) { // Ignore entries that contain `~third-party`.
167 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
168 } else if (blocklistEntry.contains("third-party")) { // Third-party white list entries.
169 if (blocklistEntry.contains("domain=")) { // Third-party domain white list entries.
171 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
172 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
173 String domains = filters.substring(filters.indexOf("domain=") + 7);
175 //noinspection StatementWithEmptyBody
176 if (domains.contains("~")) { // It is uncertain what a `~` domain means inside an `@@` entry.
179 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
180 } else if (blocklistEntry.startsWith("|")) { // Third-party domain initial white list entries.
181 // Strip out the initial `|`.
182 entry = entry.substring(1);
184 //noinspection StatementWithEmptyBody
185 if (entry.equals("http://") || entry.equals("https://")) { // Ignore generic entries.
186 // Do nothing. These entries are designed for filter options that Privacy Browser does not use.
188 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
189 } else { // Process third-party domain initial white list entries.
190 // Process each domain.
192 // Create a string to keep track of the current domain.
195 if (domains.contains("|")) { // There is more than one domain in the list.
196 // Get the first domain from the list.
197 domain = domains.substring(0, domains.indexOf("|"));
199 // Remove the first domain from the list.
200 domains = domains.substring(domains.indexOf("|") + 1);
201 } else { // There is only one domain in the list.
205 if (entry.contains("*")) { // Process a third-party domain initial white list double entry.
206 // Get the index of the wildcard.
207 int wildcardIndex = entry.indexOf("*");
209 // Split the entry into components.
210 String firstEntry = entry.substring(0, wildcardIndex);
211 String secondEntry = entry.substring(wildcardIndex + 1);
213 // Create an entry string array.
214 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
216 // Add the entry to the white list.
217 thirdPartyDomainInitialWhitelist.add(domainDoubleEntry);
219 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial white list added: " + domain + " , " + firstEntry + " , " + secondEntry +
220 // " - " + originalBlocklistEntry);
221 } else { // Process a third-party domain initial white list single entry.
222 // Create a domain entry string array.
223 String[] domainEntry = {domain, entry, originalBlocklistEntry};
225 // Add the entry to the third party domain initial white list.
226 thirdPartyDomainInitialWhitelist.add(domainEntry);
228 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial white list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
230 } while (domains.contains("|"));
232 } else { // Third-party domain entries.
233 // Process each domain.
235 // Create a string to keep track of the current domain.
238 if (domains.contains("|")) { // three is more than one domain in the list.
239 // Get the first domain from the list.
240 domain = domains.substring(0, domains.indexOf("|"));
242 // Remove the first domain from the list.
243 domains = domains.substring(domains.indexOf("|") + 1);
244 } else { // There is only one domain in the list.
248 // Remove any trailing `*` from the entry.
249 if (entry.endsWith("*")) {
250 entry = entry.substring(0, entry.length() - 1);
253 if (entry.contains("*")) { // Process a third-party domain double entry.
254 // Get the index of the wildcard.
255 int wildcardIndex = entry.indexOf("*");
257 // Split the entry into components.
258 String firstEntry = entry.substring(0, wildcardIndex);
259 String secondEntry = entry.substring(wildcardIndex + 1);
261 // Create an entry string array.
262 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
264 // Add the entry to the white list.
265 thirdPartyDomainWhitelist.add(domainDoubleEntry);
267 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
268 // originalBlocklistEntry);
269 } else { // Process a third-party domain single entry.
270 // Create an entry string array.
271 String[] domainEntry = {domain, entry, originalBlocklistEntry};
273 // Add the entry to the white list.
274 thirdPartyDomainWhitelist.add(domainEntry);
276 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
278 } while (domains.contains("|"));
280 } else { // Process third-party white list entries.
282 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
284 if (entry.contains("*")) { // There are two or more entries.
285 // Get the index of the wildcard.
286 int wildcardIndex = entry.indexOf("*");
288 // Split the entry into components.
289 String firstEntry = entry.substring(0, wildcardIndex);
290 String secondEntry = entry.substring(wildcardIndex + 1);
292 if (secondEntry.contains("*")) { // There are three or more entries.
293 // Get the index of the wildcard.
294 int secondWildcardIndex = secondEntry.indexOf("*");
296 // Split the entry into components.
297 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
298 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
300 if (thirdEntry.contains("*")) { // There are four or more entries.
301 // Get the index of the wildcard.
302 int thirdWildcardIndex = thirdEntry.indexOf("*");
304 // Split the entry into components.
305 String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
306 String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
308 if (fourthEntry.contains("*")) { // Process a third-party white list quintuple entry.
309 // Get the index of the wildcard.
310 int fourthWildcardIndex = fourthEntry.indexOf("*");
312 // Split the entry into components.
313 String realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex);
314 String fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1);
316 // Create an entry string array.
317 String[] quintupleEntry = {firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlocklistEntry};
319 // Add the entry to the white list.
320 thirdPartyWhitelist.add(quintupleEntry);
322 //Log.i("Blocklists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
323 // realFourthEntry + " , " + fifthEntry + " - " + originalBlocklistEntry);
324 } else { // Process a third-party white list quadruple entry.
325 // Create an entry string array.
326 String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry};
328 // Add the entry to the white list.
329 thirdPartyWhitelist.add(quadrupleEntry);
331 //Log.i("Blocklists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
332 // fourthEntry + " - " + originalBlocklistEntry);
334 } else { // Process a third-party white list triple entry.
335 // Create an entry string array.
336 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
338 // Add the entry to the white list.
339 thirdPartyWhitelist.add(tripleEntry);
341 //Log.i("Blocklists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
342 // originalBlocklistEntry);
344 } else { // Process a third-party white list double entry.
345 // Create an entry string array.
346 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
348 // Add the entry to the white list.
349 thirdPartyWhitelist.add(doubleEntry);
351 //Log.i("Blocklists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
353 } else { // Process a third-party white list single entry.
354 // Create an entry string array.
355 String[] singleEntry = {entry, originalBlocklistEntry};
357 // Add the entry to the white list.
358 thirdPartyWhitelist.add(singleEntry);
360 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain white list added: " + entry + " - " + originalBlocklistEntry);
363 } else if (blocklistEntry.contains("domain=")) { // Process domain white list entries.
365 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
366 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
367 String domains = filters.substring(filters.indexOf("domain=") + 7);
369 if (entry.startsWith("|")) { // Initial domain white list entries.
370 // Strip the initial `|`.
371 entry = entry.substring(1);
373 //noinspection StatementWithEmptyBody
374 if (entry.equals("http://") || entry.equals("https://")) { // Ignore generic entries.
375 // Do nothing. These entries are designed for filter options that Privacy Browser does not use.
377 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
378 } else { // Initial domain white list entry.
379 // Process each domain.
381 // Create a string to keep track of the current domain.
384 if (domains.contains("|")) { // There is more than one domain in the list.
385 // Get the first domain from the list.
386 domain = domains.substring(0, domains.indexOf("|"));
388 // Remove the first domain from the list.
389 domains = domains.substring(domains.indexOf("|") + 1);
390 } else { // There is only one domain in the list.
394 if (entry.contains("*")) { // There are two or more entries.
395 // Get the index of the wildcard.
396 int wildcardIndex = entry.indexOf("*");
398 // Split the entry into components.
399 String firstEntry = entry.substring(0, wildcardIndex);
400 String secondEntry = entry.substring(wildcardIndex + 1);
402 if (secondEntry.contains("*")) { // Process a domain initial triple entry.
403 // Get the index of the wildcard.
404 int secondWildcardIndex = secondEntry.indexOf("*");
406 // Split the entry into components.
407 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
408 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
410 // Create an entry string array.
411 String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
413 // Add the entry to the white list.
414 domainInitialWhitelist.add(domainTripleEntry);
416 //Log.i("Blocklists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
417 // thirdEntry + " - " + originalBlocklistEntry);
418 } else { // Process a domain initial double entry.
419 // Create an entry string array.
420 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
422 // Add the entry to the white list.
423 domainInitialWhitelist.add(domainDoubleEntry);
425 //Log.i("Blocklists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
426 // originalBlocklistEntry);
428 } else { // Process a domain initial single entry.
429 // Create an entry string array.
430 String[] domainEntry = {domain, entry, originalBlocklistEntry};
432 // Add the entry to the white list.
433 domainInitialWhitelist.add(domainEntry);
435 //Log.i("Blocklists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
437 } while (domains.contains("|"));
439 } else if (entry.endsWith("|")) { // Final domain white list entries.
440 // Strip the `|` from the end of the entry.
441 entry = entry.substring(0, entry.length() - 1);
443 // Process each domain.
445 // Create a string to keep track of the current domain.
448 if (domains.contains("|")) { // There is more than one domain in the list.
449 // Get the first domain from the list.
450 domain = domains.substring(0, domains.indexOf("|"));
452 // Remove the first domain from the list.
453 domains = domains.substring(domains.indexOf("|") + 1);
454 } else { // There is only one domain in the list.
458 if (entry.contains("*")) { // Process a domain final white list double entry.
459 // Get the index of the wildcard.
460 int wildcardIndex = entry.indexOf("*");
462 // Split the entry into components.
463 String firstEntry = entry.substring(0, wildcardIndex);
464 String secondEntry = entry.substring(wildcardIndex + 1);
466 // Create an entry string array.
467 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
469 // Add the entry to the white list.
470 domainFinalWhitelist.add(domainDoubleEntry);
472 //Log.i("Blocklists", headers.get(1)[0] + " domain final white list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
473 // originalBlocklistEntry);
474 } else { // Process a domain final white list single entry.
475 // create an entry string array.
476 String[] domainEntry = {domain, entry, originalBlocklistEntry};
478 // Add the entry to the white list.
479 domainFinalWhitelist.add(domainEntry);
481 //Log.i("Blocklists", headers.get(1)[0] + " domain final white list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
483 } while (domains.contains("|"));
485 } else { // Standard domain white list entries with filters.
486 //noinspection StatementWithEmptyBody
487 if (domains.contains("~")) { // It is uncertain what a `~` domain means inside an `@@` entry.
490 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
492 // Process each domain.
494 // Create a string to keep track of the current domain.
497 if (domains.contains("|")) { // There is more than one domain in the list.
498 // Get the first domain from the list.
499 domain = domains.substring(0, domains.indexOf("|"));
501 // Remove the first domain from the list.
502 domains = domains.substring(domains.indexOf("|") + 1);
503 } else { // There is only one domain in the list.
507 if (entry.contains("*")) { // There are two or more entries.
508 // Get the index of the wildcard.
509 int wildcardIndex = entry.indexOf("*");
511 // Split the entry into components.
512 String firstEntry = entry.substring(0, wildcardIndex);
513 String secondEntry = entry.substring(wildcardIndex + 1);
515 if (secondEntry.contains("*")) { // There are three or more entries.
516 // Get the index of the wildcard.
517 int secondWildcardIndex = secondEntry.indexOf("*");
519 // Split the entry into components.
520 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
521 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
523 if (thirdEntry.contains("*")) { // Process a domain white list quadruple entry.
524 // Get the index of the wildcard.
525 int thirdWildcardIndex = thirdEntry.indexOf("*");
527 // Split the entry into components.
528 String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
529 String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
531 // Create an entry string array.
532 String[] domainQuadrupleEntry = {domain, firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry};
534 // Add the entry to the white list.
535 domainWhitelist.add(domainQuadrupleEntry);
537 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
538 // realThirdEntry + " , " + fourthEntry + " - " + originalBlocklistEntry);
539 } else { // Process a domain white list triple entry.
540 // Create an entry string array.
541 String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
543 // Add the entry to the white list.
544 domainWhitelist.add(domainTripleEntry);
546 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
547 // thirdEntry + " - " + originalBlocklistEntry);
549 } else { // Process a domain white list double entry.
550 // Create an entry string array.
551 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
553 // Add the entry to the white list.
554 domainWhitelist.add(domainDoubleEntry);
556 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
557 // originalBlocklistEntry);
559 } else { // Process a domain white list single entry.
560 // Create an entry string array.
561 String[] domainEntry = {domain, entry, originalBlocklistEntry};
563 // Add the entry to the white list.
564 domainWhitelist.add(domainEntry);
566 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added : " + domain + " , " + entry + " - " + originalBlocklistEntry);
568 } while (domains.contains("|"));
571 } // Ignore all other filter entries.
572 } else if (blocklistEntry.endsWith("|")) { // Final white list entries.
573 // Remove the final `|` from the entry.
574 String entry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
576 if (entry.contains("*")) { // Process a final white list double entry
577 // Get the index of the wildcard.
578 int wildcardIndex = entry.indexOf("*");
580 // split the entry into components.
581 String firstEntry = entry.substring(0, wildcardIndex);
582 String secondEntry = entry.substring(wildcardIndex + 1);
584 // Create an entry string array.
585 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
587 // Add the entry to the white list.
588 finalWhitelist.add(doubleEntry);
590 //Log.i("Blocklists", headers.get(1)[0] + " final white list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
591 } else { // Process a final white list single entry.
592 // Create an entry string array.
593 String[] singleEntry = {entry, originalBlocklistEntry};
595 // Add the entry to the white list.
596 finalWhitelist.add(singleEntry);
598 //Log.i("Blocklists", headers.get(1)[0] + " final white list added: " + entry + " - " + originalBlocklistEntry);
600 } else { // Main white list entries.
601 if (blocklistEntry.contains("*")) { // There are two or more entries.
602 // Get the index of the wildcard.
603 int wildcardIndex = blocklistEntry.indexOf("*");
605 // Split the entry into components.
606 String firstEntry = blocklistEntry.substring(0, wildcardIndex);
607 String secondEntry = blocklistEntry.substring(wildcardIndex + 1);
609 if (secondEntry.contains("*")) { // Process a main white list triple entry.
610 // Get the index of the wildcard.
611 int secondWildcardIndex = secondEntry.indexOf("*");
613 // Split the entry into components.
614 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
615 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
617 // Create an entry string array.
618 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
620 // Add the entry to the white list.
621 mainWhitelist.add(tripleEntry);
623 //Log.i("Blocklists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
624 } else { // Process a main white list double entry.
625 // Create an entry string array.
626 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
628 // Add the entry to the white list.
629 mainWhitelist.add(doubleEntry);
631 //Log.i("Blocklists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
633 } else { // Process a main white list single entry.
634 // Create an entry string array.
635 String[] singleEntry = {blocklistEntry, originalBlocklistEntry};
637 // Add the entry to the white list.
638 mainWhitelist.add(singleEntry);
640 //Log.i("Blocklists", headers.get(1)[0] + " main white list added: " + blocklistEntry + " - " + originalBlocklistEntry);
643 } else if (blocklistEntry.endsWith("|")) { // Final black list entries.
644 // Strip out the final "|"
645 String entry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
647 // Strip out any initial `||`. They are redundant in this case because the block list entry is being matched against the end of the URL.
648 if (entry.startsWith("||")) {
649 entry = entry.substring(2);
652 if (entry.contains("*")) { // Process a final black list double entry.
653 // Get the index of the wildcard.
654 int wildcardIndex = entry.indexOf("*");
656 // Split the entry into components.
657 String firstEntry = entry.substring(0, wildcardIndex);
658 String secondEntry = entry.substring(wildcardIndex + 1);
660 // Create an entry string array.
661 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
663 // Add the entry to the black list.
664 finalBlacklist.add(doubleEntry);
666 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
667 } else { // Process a final black list single entry.
668 // create an entry string array.
669 String[] singleEntry = {entry, originalBlocklistEntry};
671 // Add the entry to the black list.
672 finalBlacklist.add(singleEntry);
674 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + entry + " - " + originalBlocklistEntry);
676 } else if (blocklistEntry.contains("$")) { // Entries with filter options.
677 // Strip out any initial `||`. These will be treated like any other entry.
678 if (blocklistEntry.startsWith("||")) {
679 blocklistEntry = blocklistEntry.substring(2);
682 if (blocklistEntry.contains("third-party")) { // Third-party entries.
683 //noinspection StatementWithEmptyBody
684 if (blocklistEntry.contains("~third-party")) { // Third-party filter white list entries.
685 // Do not process these white list entries. They are designed to combine with block filters that Privacy Browser doesn't use, like `subdocument` and `xmlhttprequest`.
687 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
688 } else if (blocklistEntry.contains("domain=")) { // Third-party domain entries.
689 if (blocklistEntry.startsWith("|")) { // Third-party domain initial entries.
690 // Strip the initial `|`.
691 blocklistEntry = blocklistEntry.substring(1);
694 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
695 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
696 String domains = filters.substring(filters.indexOf("domain=") + 7);
698 //noinspection StatementWithEmptyBody
699 if (entry.equals("http:") || entry.equals("https:") || entry.equals("http://") || entry.equals("https://")) { // Ignore generic entries.
700 // Do nothing. These entries will almost entirely disable the website.
701 // Often the original entry blocks filter options like `$script`, which Privacy Browser does not differentiate.
703 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
704 } else { // Third-party domain initial entries.
705 // Process each domain.
707 // Create a string to keep track of the current domain.
710 if (domains.contains("|")) { // There is more than one domain in the list.
711 // Get the first domain from the list.
712 domain = domains.substring(0, domains.indexOf("|"));
714 // Remove the first domain from the list.
715 domains = domains.substring(domains.indexOf("|") + 1);
716 } else { // There is only one domain in the list.
720 if (entry.contains("*")) { // Three are two or more entries.
721 // Get the index of the wildcard.
722 int wildcardIndex = entry.indexOf("*");
724 // Split the entry into components.
725 String firstEntry = entry.substring(0, wildcardIndex);
726 String secondEntry = entry.substring(wildcardIndex + 1);
728 if (secondEntry.contains("*")) { // Process a third-party domain initial black list triple entry.
729 // Get the index of the wildcard.
730 int secondWildcardIndex = secondEntry.indexOf("*");
732 // Split the entry into components.
733 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
734 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
736 // Create an entry string array.
737 String[] tripleDomainEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
739 // Add the entry to the black list.
740 thirdPartyDomainInitialBlacklist.add(tripleDomainEntry);
742 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + firstEntry + " , " + realSecondEntry +
743 // " , " + thirdEntry + " - " + originalBlocklistEntry);
744 } else { // Process a third-party domain initial black list double entry.
745 // Create an entry string array.
746 String[] doubleDomainEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
748 // Add the entry to the black list.
749 thirdPartyDomainInitialBlacklist.add(doubleDomainEntry);
751 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + firstEntry + " , " + secondEntry +
752 // " - " + originalBlocklistEntry);
754 } else { // Process a third-party domain initial black list single entry.
755 // Create an entry string array.
756 String[] singleEntry = {domain, entry, originalBlocklistEntry};
758 // Add the entry to the black list.
759 thirdPartyDomainInitialBlacklist.add(singleEntry);
761 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
763 } while (domains.contains("|"));
765 } else if (blocklistEntry.contains("\\")) { // Process a third-party domain black list regular expression.
766 // Parse the entry. At least one regular expression in this entry contains `$`, so the parser uses `/$`.
767 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("/$") + 1);
768 String filters = blocklistEntry.substring(blocklistEntry.indexOf("/$") + 2);
769 String domains = filters.substring(filters.indexOf("domain=") + 7);
771 // Process each domain.
773 // Create a string to keep track of the current domain.
776 if (domains.contains("|")) { // There is more than one domain in the list.
777 // Get the first domain from the list.
778 domain = domains.substring(0, domains.indexOf("|"));
780 // Remove the first domain from the list.
781 domains = domains.substring(domains.indexOf("|") + 1);
782 } else { // There is only one domain in the list.
786 // Create an entry string array.
787 String[] domainEntry = {domain, entry, originalBlocklistEntry};
789 // Add the entry to the black list.
790 thirdPartyDomainRegularExpressionBlacklist.add(domainEntry);
792 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain regular expression black list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
793 } while (domains.contains("|"));
794 } else { // Third-party domain entries.
796 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
797 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
798 String domains = filters.substring(filters.indexOf("domain=") + 7);
800 // Strip any trailing "*" from the entry.
801 if (entry.endsWith("*")) {
802 entry = entry.substring(0, entry.length() - 1);
805 // Track if any third-party white list filters are applied.
806 boolean whitelistDomain = false;
808 // Process each domain.
810 // Create a string to keep track of the current domain.
813 if (domains.contains("|")) { // There is more than one domain in the list.
814 // Get the first domain from the list.
815 domain = domains.substring(0, domains.indexOf("|"));
817 // Remove the first domain from the list.
818 domains = domains.substring(domains.indexOf("|") + 1);
819 } else { // The is only one domain in the list.
823 // Differentiate between block list domains and white list domains.
824 if (domain.startsWith("~")) { // White list third-party domain entry.
825 // Strip the initial `~`.
826 domain = domain.substring(1);
828 // Set the white list domain flag.
829 whitelistDomain = true;
831 if (entry.contains("*")) { // Process a third-party domain white list double entry.
832 // Get the index of the wildcard.
833 int wildcardIndex = entry.indexOf("*");
835 // Split the entry into components.
836 String firstEntry = entry.substring(0, wildcardIndex);
837 String secondEntry = entry.substring(wildcardIndex + 1);
839 // Create an entry string array.
840 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
842 // Add the entry to the white list.
843 thirdPartyDomainWhitelist.add(domainDoubleEntry);
845 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
846 // originalBlocklistEntry);
847 } else { // Process a third-party domain white list single entry.
848 // Create an entry string array.
849 String[] domainEntry = {domain, entry, originalBlocklistEntry};
851 // Add the entry to the white list.
852 thirdPartyDomainWhitelist.add(domainEntry);
854 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
856 } else { // Third-party domain black list entries.
857 if (entry.contains("*")) { // Process a third-party domain black list double entry.
858 // Get the index of the wildcard.
859 int wildcardIndex = entry.indexOf("*");
861 // Split the entry into components.
862 String firstEntry = entry.substring(0, wildcardIndex);
863 String secondEntry = entry.substring(wildcardIndex + 1);
865 // Create an entry string array.
866 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
868 // Add the entry to the black list
869 thirdPartyDomainBlacklist.add(domainDoubleEntry);
871 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain black list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
872 // originalBlocklistEntry);
873 } else { // Process a third-party domain black list single entry.
874 // Create an entry string array.
875 String[] domainEntry = {domain, entry, originalBlocklistEntry};
877 // Add the entry to the black list.
878 thirdPartyDomainBlacklist.add(domainEntry);
880 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain block list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
883 } while (domains.contains("|"));
885 // Add a third-party black list entry if a white list domain was processed.
886 if (whitelistDomain) {
887 if (entry.contains("*")) { // Process a third-party black list double entry.
888 // Get the index of the wildcard.
889 int wildcardIndex = entry.indexOf("*");
891 // Split the entry into components.
892 String firstEntry = entry.substring(0, wildcardIndex);
893 String secondEntry = entry.substring(wildcardIndex + 1);
895 // Create an entry string array.
896 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
898 // Add the entry to the black list.
899 thirdPartyBlacklist.add(doubleEntry);
901 //Log.i("Blocklists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
902 } else { // Process a third-party black list single entry.
903 // Create an entry string array.
904 String[] singleEntry = {entry, originalBlocklistEntry};
906 // Add an entry to the black list.
907 thirdPartyBlacklist.add(singleEntry);
909 //Log.i("Blocklists", headers.get(1)[0] + " third-party black list added: " + entry + " - " + originalBlocklistEntry);
913 } else if (blocklistEntry.startsWith("|")) { // Third-party initial black list entries.
914 // Strip the initial `|`.
915 blocklistEntry = blocklistEntry.substring(1);
918 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
920 if (entry.contains("*")) { // Process a third-party initial black list double entry.
921 // Get the index of the wildcard.
922 int wildcardIndex = entry.indexOf("*");
924 // Split the entry into components.
925 String firstEntry = entry.substring(0, wildcardIndex);
926 String secondEntry = entry.substring(wildcardIndex + 1);
928 // Create an entry string array.
929 String[] thirdPartyDoubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
931 // Add the entry to the black list.
932 thirdPartyInitialBlacklist.add(thirdPartyDoubleEntry);
934 //Log.i("Blocklists", headers.get(1)[0] + " third-party initial black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
935 } else { // Process a third-party initial black list single entry.
936 // Create an entry string array.
937 String[] singleEntry = {entry, originalBlocklistEntry};
939 // Add the entry to the black list.
940 thirdPartyInitialBlacklist.add(singleEntry);
942 //Log.i("Blocklists", headers.get(1)[0] + " third-party initial black list added: " + entry + " - " + originalBlocklistEntry);
944 } else if (blocklistEntry.contains("\\")) { // Process a regular expression black list entry.
945 // Prepare a string to hold the entry.
949 if (blocklistEntry.contains("$/$")) { // The first `$` is part of the regular expression.
950 entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$/$") + 2);
951 } else { // The only `$` indicates the filter options.
952 entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
955 // Create an entry string array.
956 String[] singleEntry = {entry, originalBlocklistEntry};
958 // Add the entry to the black list.
959 thirdPartyRegularExpressionBlacklist.add(singleEntry);
961 //Log.i("Blocklists", headers.get(1)[0] + " third-party regular expression black list added: " + entry + " - " + originalBlocklistEntry);
962 } else if (blocklistEntry.contains("*")) { // Third-party and regular expression black list entries.
964 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
966 if (entry.endsWith("*")) { // Process a third-party black list single entry.
967 // Strip the final `*`.
968 entry = entry.substring(0, entry.length() - 1);
970 // Create an entry string array.
971 String[] singleEntry = {entry, originalBlocklistEntry};
973 // Add the entry to the black list.
974 thirdPartyBlacklist.add(singleEntry);
976 //Log.i("Blocklists", headers.get(1)[0] + " third party black list added: " + entry + " - " + originalBlocklistEntry);
977 } else { // There are two or more entries.
978 // Get the index of the wildcard.
979 int wildcardIndex = entry.indexOf("*");
981 // Split the entry into components.
982 String firstEntry = entry.substring(0, wildcardIndex);
983 String secondEntry = entry.substring(wildcardIndex + 1);
985 if (secondEntry.contains("*")) { // There are three or more entries.
986 // Get the index of the wildcard.
987 int secondWildcardIndex = secondEntry.indexOf("*");
989 // Split the entry into components.
990 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
991 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
993 if (thirdEntry.contains("*")) { // Process a third-party black list quadruple entry.
994 // Get the index of the wildcard.
995 int thirdWildcardIndex = thirdEntry.indexOf("*");
997 // Split the entry into components.
998 String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
999 String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
1001 // Create an entry string array.
1002 String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry};
1004 // Add the entry to the black list.
1005 thirdPartyBlacklist.add(quadrupleEntry);
1007 //Log.i("Blocklists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1008 // fourthEntry + " - " + originalBlocklistEntry);
1009 } else { // Process a third-party black list triple entry.
1010 // Create an entry string array.
1011 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1013 // Add the entry to the black list.
1014 thirdPartyBlacklist.add(tripleEntry);
1016 //Log.i("Blocklists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
1017 // originalBlocklistEntry);
1019 } else { // Process a third-party black list double entry.
1020 // Create an entry string array.
1021 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1023 // Add the entry to the black list.
1024 thirdPartyBlacklist.add(doubleEntry);
1026 //Log.i("Blocklists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1029 } else { // Process a third party black list single entry.
1031 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1033 // Create an entry string array.
1034 String[] singleEntry = {entry, originalBlocklistEntry};
1036 // Add the entry to the black list.
1037 thirdPartyBlacklist.add(singleEntry);
1039 //Log.i("Blocklists", headers.get(1)[0] + " third party black list added: " + entry + " - " + originalBlocklistEntry);
1041 } else if (blocklistEntry.substring(blocklistEntry.indexOf("$")).contains("domain=")) { // Domain entries.
1042 if (blocklistEntry.contains("~")) { // Domain white list entries.
1043 // Separate the filters.
1044 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1045 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
1046 String domains = filters.substring(filters.indexOf("domain=") + 7);
1048 // Strip any final `*` from the entry. They are redundant.
1049 if (entry.endsWith("*")) {
1050 entry = entry.substring(0, entry.length() - 1);
1053 // Process each domain.
1055 // Create a string to keep track of the current domain.
1058 if (domains.contains("|")) { // There is more than one domain in the list.
1059 // Get the first domain from the list.
1060 domain = domains.substring(0, domains.indexOf("|"));
1062 // Remove the first domain from the list.
1063 domains = domains.substring(domains.indexOf("|") + 1);
1064 } else { // There is only one domain in the list.
1068 // Strip the initial `~`.
1069 domain = domain.substring(1);
1071 if (entry.contains("*")) { // There are two or more entries.
1072 // Get the index of the wildcard.
1073 int wildcardIndex = entry.indexOf("*");
1075 // Split the entry into components.
1076 String firstEntry = entry.substring(0, wildcardIndex);
1077 String secondEntry = entry.substring(wildcardIndex + 1);
1079 if (secondEntry.contains("*")) { // Process a domain white list triple entry.
1080 // Get the index of the wildcard.
1081 int secondWildcardIndex = secondEntry.indexOf("*");
1083 // Split the entry into components.
1084 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1085 String thirdEntry = secondEntry.substring((secondWildcardIndex + 1));
1087 // Create an entry string array.
1088 String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1090 // Add the entry to the white list.
1091 domainWhitelist.add(domainTripleEntry);
1093 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1094 // " - " + originalBlocklistEntry);
1095 } else { // Process a domain white list double entry.
1096 // Create an entry string array.
1097 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
1099 // Add the entry to the white list.
1100 domainWhitelist.add(domainDoubleEntry);
1102 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1104 } else { // Process a domain white list single entry.
1105 // Create an entry string array.
1106 String[] domainEntry = {domain, entry, originalBlocklistEntry};
1108 // Add the entry to the white list.
1109 domainWhitelist.add(domainEntry);
1111 //Log.i("Blocklists", headers.get(1)[0] + " domain white list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1113 } while (domains.contains("|"));
1114 } else { // Domain black list entries.
1115 // Separate the filters.
1116 String entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1117 String filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1);
1118 String domains = filters.substring(filters.indexOf("domain=") + 7);
1120 // Only process the item if the entry is not null. For example, some lines begin with `$websocket`, which create a null entry.
1121 if (!entry.equals("")) {
1122 // Process each domain.
1124 // Create a string to keep track of the current domain.
1127 if (domains.contains("|")) { // There is more than one domain in the list.
1128 // Get the first domain from the list.
1129 domain = domains.substring(0, domains.indexOf("|"));
1131 // Remove the first domain from the list.
1132 domains = domains.substring(domains.indexOf("|") + 1);
1133 } else { // There is only one domain in the list.
1137 if (entry.startsWith("|")) { // Domain initial black list entries.
1138 // Remove the initial `|`;
1139 String entryBase = entry.substring(1);
1141 //noinspection StatementWithEmptyBody
1142 if (entryBase.equals("http://") || entryBase.equals("https://")) {
1143 // Do nothing. These entries will entirely block the website.
1144 // Often the original entry blocks `$script` but Privacy Browser does not currently differentiate between scripts and other entries.
1146 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
1147 } else { // Process a domain initial black list entry
1148 // Create an entry string array.
1149 String[] domainEntry = {domain, entryBase, originalBlocklistEntry};
1151 // Add the entry to the black list.
1152 domainInitialBlacklist.add(domainEntry);
1154 //Log.i("Blocklists", headers.get(1)[0] + " domain initial black list added: " + domain + " , " + entryBase + " - " + originalBlocklistEntry);
1156 } else if (entry.endsWith("|")) { // Domain final black list entries.
1157 // Remove the final `|`.
1158 String entryBase = entry.substring(0, entry.length() - 1);
1160 if (entryBase.contains("*")) { // Process a domain final black list double entry.
1161 // Get the index of the wildcard.
1162 int wildcardIndex = entry.indexOf("*");
1164 // Split the entry into components.
1165 String firstEntry = entryBase.substring(0, wildcardIndex);
1166 String secondEntry = entryBase.substring(wildcardIndex + 1);
1168 // Create an entry string array.
1169 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
1171 // Add the entry to the black list.
1172 domainFinalBlacklist.add(domainDoubleEntry);
1174 //Log.i("Blocklists", headers.get(1)[0] + " domain final black list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
1175 // originalBlocklistEntry);
1176 } else { // Process a domain final black list single entry.
1177 // Create an entry string array.
1178 String[] domainEntry = {domain, entryBase, originalBlocklistEntry};
1180 // Add the entry to the black list.
1181 domainFinalBlacklist.add(domainEntry);
1183 //Log.i("Blocklists", headers.get(1)[0] + " domain final black list added: " + domain + " , " + entryBase + " - " + originalBlocklistEntry);
1185 } else if (entry.contains("\\")) { // Process a domain regular expression black list entry.
1186 // Create an entry string array.
1187 String[] domainEntry = {domain, entry, originalBlocklistEntry};
1189 // Add the entry to the black list.
1190 domainRegularExpressionBlacklist.add(domainEntry);
1192 //Log.i("Blocklists", headers.get(1)[0] + " domain regular expression black list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1193 } else if (entry.contains("*")) { // There are two or more entries.
1194 // Get the index of the wildcard.
1195 int wildcardIndex = entry.indexOf("*");
1197 // Split the entry into components.
1198 String firstEntry = entry.substring(0, wildcardIndex);
1199 String secondEntry = entry.substring(wildcardIndex + 1);
1201 if (secondEntry.contains("*")) { // Process a domain black list triple entry.
1202 // Get the index of the wildcard.
1203 int secondWildcardIndex = secondEntry.indexOf("*");
1205 // Split the entry into components.
1206 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1207 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1209 // Create an entry string array.
1210 String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1212 // Add the entry to the black list.
1213 domainBlacklist.add(domainTripleEntry);
1215 //Log.i("Blocklists", headers.get(1)[0] + " domain black list added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1216 // " - " + originalBlocklistEntry);
1217 } else { // Process a domain black list double entry.
1218 // Create an entry string array.
1219 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlocklistEntry};
1221 // Add the entry to the black list.
1222 domainBlacklist.add(domainDoubleEntry);
1224 //Log.i("Blocklists", headers.get(1)[0] + " domain black list added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
1225 // originalBlocklistEntry);
1227 } else { // Process a domain black list single entry.
1228 // Create an entry string array.
1229 String[] domainEntry = {domain, entry, originalBlocklistEntry};
1231 // Add the entry to the black list.
1232 domainBlacklist.add(domainEntry);
1234 //Log.i("Blocklists", headers.get(1)[0] + " domain black list added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1236 } while (domains.contains("|"));
1239 } else if (blocklistEntry.contains("~")) { // White list entries. Privacy Browser does not differentiate against these filter options, so they are just generally white listed.
1240 // Remove the filter options.
1241 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1243 // Strip any trailing `*`.
1244 if (blocklistEntry.endsWith("*")) {
1245 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
1248 if (blocklistEntry.contains("*")) { // Process a white list double entry.
1249 // Get the index of the wildcard.
1250 int wildcardIndex = blocklistEntry.indexOf("*");
1252 // Split the entry into components.
1253 String firstEntry = blocklistEntry.substring(0, wildcardIndex);
1254 String secondEntry = blocklistEntry.substring(wildcardIndex + 1);
1256 // Create an entry string array.
1257 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1259 // Add the entry to the white list.
1260 mainWhitelist.add(doubleEntry);
1262 //Log.i("Blocklists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1263 } else { // Process a white list single entry.
1264 // Create an entry string array.
1265 String[] singleEntry = {blocklistEntry, originalBlocklistEntry};
1267 // Add the entry to the white list.
1268 mainWhitelist.add(singleEntry);
1270 //Log.i("Blocklists", headers.get(1)[0] + " main white list added: " + blocklistEntry + " - + " + originalBlocklistEntry);
1272 } else if (blocklistEntry.contains("\\")) { // Process a regular expression black list entry.
1273 // Remove the filter options.
1274 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1276 // Create an entry string array.
1277 String[] singleEntry = {blocklistEntry, originalBlocklistEntry};
1279 // Add the entry to the black list.
1280 regularExpressionBlacklist.add(singleEntry);
1282 //Log.i("Blocklists", headers.get(1)[0] + " regular expression black list added: " + blocklistEntry + " - " + originalBlocklistEntry);
1283 } else { // Black list entries.
1284 // Remove the filter options.
1285 if (!blocklistEntry.contains("$file")) { // EasyPrivacy contains an entry with `$file` that does not have filter options.
1286 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"));
1289 // Strip any trailing `*`. These are redundant.
1290 if (blocklistEntry.endsWith("*")) {
1291 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
1294 if (blocklistEntry.startsWith("|")) { // Initial black list entries.
1295 // Strip the initial `|`.
1296 String entry = blocklistEntry.substring(1);
1298 if (entry.contains("*")) { // Process an initial black list double entry.
1299 // Get the index of the wildcard.
1300 int wildcardIndex = entry.indexOf("*");
1302 // Split the entry into components.
1303 String firstEntry = entry.substring(0, wildcardIndex);
1304 String secondEntry = entry.substring(wildcardIndex + 1);
1306 // Create an entry string array.
1307 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1309 // Add the entry to the black list.
1310 initialBlacklist.add(doubleEntry);
1312 //Log.i("Blocklists", headers.get(1)[0] + " initial black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1313 } else { // Process an initial black list single entry.
1314 // Create an entry string array.
1315 String[] singleEntry = {entry, originalBlocklistEntry};
1317 // Add the entry to the black list.
1318 initialBlacklist.add(singleEntry);
1320 //Log.i("Blocklists", headers.get(1)[0] + " initial black list added: " + entry + " - " + originalBlocklistEntry);
1322 } else if (blocklistEntry.endsWith("|")) { // Final black list entries.
1323 // Ignore entries with `object` filters. They can block entire websites and don't have any meaning in the context of Privacy Browser.
1324 if (!originalBlocklistEntry.contains("$object")) {
1325 // Strip the final `|`.
1326 String entry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
1328 if (entry.contains("*")) { // There are two or more entries.
1329 // Get the index of the wildcard.
1330 int wildcardIndex = entry.indexOf("*");
1332 // Split the entry into components.
1333 String firstEntry = entry.substring(0, wildcardIndex);
1334 String secondEntry = entry.substring(wildcardIndex + 1);
1336 if (secondEntry.contains("*")) { // Process a final black list triple entry.
1337 // Get the index of the wildcard.
1338 int secondWildcardIndex = secondEntry.indexOf("*");
1340 // Split the entry into components.
1341 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1342 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1344 // Create an entry string array.
1345 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1347 // Add the entry to the black list.
1348 finalBlacklist.add(tripleEntry);
1350 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
1351 // originalBlocklistEntry);
1352 } else { // Process a final black list double entry.
1353 // Create an entry string array.
1354 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1356 // Add the entry to the black list.
1357 finalBlacklist.add(doubleEntry);
1359 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1361 } else { // Process a final black list single entry.
1362 // Create an entry sting array.
1363 String[] singleEntry = {entry, originalBlocklistEntry};
1365 // Add the entry to the black list.
1366 finalBlacklist.add(singleEntry);
1368 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + entry + " - " + originalBlocklistEntry);
1371 } else if (blocklistEntry.contains("*")) { // There are two or more entries.
1372 // Get the index of the wildcard.
1373 int wildcardIndex = blocklistEntry.indexOf("*");
1375 // Split the entry into components.
1376 String firstEntry = blocklistEntry.substring(0, wildcardIndex);
1377 String secondEntry = blocklistEntry.substring(wildcardIndex + 1);
1379 if (secondEntry.contains("*")) { // Process a main black list triple entry.
1380 // Get the index of the wildcard.
1381 int secondWildcardIndex = secondEntry.indexOf("*");
1383 // Split the entry into components.
1384 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1385 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1387 // Create an entry string array.
1388 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1390 // Add the entry to the black list.
1391 mainBlacklist.add(tripleEntry);
1393 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
1394 } else { // Process a main black list double entry.
1395 // Create an entry string array.
1396 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1398 // Add the entry to the black list.
1399 mainBlacklist.add(doubleEntry);
1401 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1403 } else { // Process a main black list single entry.
1404 // Create an entry string array.
1405 String[] singleEntry = {blocklistEntry, originalBlocklistEntry};
1407 // Add the entry to the black list.
1408 mainBlacklist.add(singleEntry);
1410 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + blocklistEntry + " - " + originalBlocklistEntry);
1413 } else { // Main black list entries
1414 // Strip out any initial `||`. These will be treated like any other entry.
1415 if (blocklistEntry.startsWith("||")) {
1416 blocklistEntry = blocklistEntry.substring(2);
1419 // Strip out any initial `*`.
1420 if (blocklistEntry.startsWith("*")) {
1421 blocklistEntry = blocklistEntry.substring(1);
1424 // Strip out any trailing `*`.
1425 if (blocklistEntry.endsWith("*")) {
1426 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
1429 if (blocklistEntry.startsWith("|")) { // Initial black list entries.
1430 // Strip the initial `|`.
1431 String entry = blocklistEntry.substring(1);
1433 if (entry.contains("*")) { // Process an initial black list double entry.
1434 // Get the index of the wildcard.
1435 int wildcardIndex = entry.indexOf("*");
1437 // Split the entry into components.
1438 String firstEntry = entry.substring(0, wildcardIndex);
1439 String secondEntry = entry.substring(wildcardIndex + 1);
1441 // Create an entry string array.
1442 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1444 // Add the entry to the black list.
1445 initialBlacklist.add(doubleEntry);
1447 //Log.i("Blocklists", headers.get(1)[0] + " initial black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1448 } else { // Process an initial black list single entry.
1449 // Create an entry string array.
1450 String[] singleEntry = {entry, originalBlocklistEntry};
1452 // Add the entry to the black list.
1453 initialBlacklist.add(singleEntry);
1455 //Log.i("Blocklists", headers.get(1)[0] + " initial black list added: " + entry + " - " + originalBlocklistEntry);
1457 } else if (blocklistEntry.endsWith("|")) { // Final black list entries.
1458 // Strip the final `|`.
1459 String entry = blocklistEntry.substring(0, blocklistEntry.length() - 1);
1461 if (entry.contains("*")) { // There are two or more entries.
1462 // Get the index of the wildcard.
1463 int wildcardIndex = entry.indexOf("*");
1465 // Split the entry into components.
1466 String firstEntry = entry.substring(0, wildcardIndex);
1467 String secondEntry = entry.substring(wildcardIndex + 1);
1469 if (secondEntry.contains("*")) { // Process a final black list triple entry.
1470 // Get the index of the wildcard.
1471 int secondWildcardIndex = secondEntry.indexOf("*");
1473 // Split the entry into components.
1474 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1475 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1477 // Create an entry string array.
1478 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1480 // Add the entry to the black list.
1481 finalBlacklist.add(tripleEntry);
1483 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
1484 // originalBlocklistEntry);
1485 } else { // Process a final black list double entry.
1486 // Create an entry string array.
1487 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1489 // Add the entry to the black list.
1490 finalBlacklist.add(doubleEntry);
1492 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1494 } else { // Process a final black list single entry.
1495 // Create an entry string array.
1496 String[] singleEntry = {entry, originalBlocklistEntry};
1498 // Add the entry to the black list.
1499 finalBlacklist.add(singleEntry);
1501 //Log.i("Blocklists", headers.get(1)[0] + " final black list added: " + entry + " - " + originalBlocklistEntry);
1503 } else { // Main black list entries.
1504 if (blocklistEntry.contains("*")) { // There are two or more entries.
1505 // Get the index of the wildcard.
1506 int wildcardIndex = blocklistEntry.indexOf("*");
1508 // Split the entry into components.
1509 String firstEntry = blocklistEntry.substring(0, wildcardIndex);
1510 String secondEntry = blocklistEntry.substring(wildcardIndex + 1);
1512 if (secondEntry.contains("*")) { // There are three or more entries.
1513 // Get the index of the wildcard.
1514 int secondWildcardIndex = secondEntry.indexOf("*");
1516 // Split the entry into components.
1517 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1518 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1520 if (thirdEntry.contains("*")) { // There are four or more entries.
1521 // Get the index of the wildcard.
1522 int thirdWildcardIndex = thirdEntry.indexOf("*");
1524 // Split the entry into components.
1525 String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
1526 String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
1528 if (fourthEntry.contains("*")) { // Process a main black list quintuple entry.
1529 // Get the index of the wildcard.
1530 int fourthWildcardIndex = fourthEntry.indexOf("*");
1532 // Split the entry into components.
1533 String realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex);
1534 String fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1);
1536 // Create an entry string array.
1537 String[] quintupleEntry = {firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlocklistEntry};
1539 // Add the entry to the black list.
1540 mainBlacklist.add(quintupleEntry);
1542 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1543 // realFourthEntry + " , " + fifthEntry + " - " + originalBlocklistEntry);
1544 } else { // Process a main black list quadruple entry.
1545 // Create an entry string array.
1546 String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry};
1548 // Add the entry to the black list.
1549 mainBlacklist.add(quadrupleEntry);
1551 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1552 // fourthEntry + " - " + originalBlocklistEntry);
1554 } else { // Process a main black list triple entry.
1555 // Create an entry string array.
1556 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry};
1558 // Add the entry to the black list.
1559 mainBlacklist.add(tripleEntry);
1561 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
1563 } else { // Process a main black list double entry.
1564 // Create an entry string array.
1565 String[] doubleEntry = {firstEntry, secondEntry, originalBlocklistEntry};
1567 // Add the entry to the black list.
1568 mainBlacklist.add(doubleEntry);
1570 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1572 } else { // Process a main black list single entry.
1573 // Create an entry string array.
1574 String[] singleEntry = {blocklistEntry, originalBlocklistEntry};
1576 // Add the entry to the black list.
1577 mainBlacklist.add(singleEntry);
1579 //Log.i("Blocklists", headers.get(1)[0] + " main black list added: " + blocklistEntry + " - " + originalBlocklistEntry);
1584 // Close `bufferedReader`.
1585 bufferedReader.close();
1586 } catch (IOException e) {
1587 // The asset exists, so the `IOException` will never be thrown.
1590 // Initialize the combined list.
1591 ArrayList<List<String[]>> combinedLists = new ArrayList<>();
1593 // Add the headers (0).
1594 combinedLists.add(headers); // 0.
1596 // Add the white lists (1-8).
1597 combinedLists.add(mainWhitelist); // 1.
1598 combinedLists.add(finalWhitelist); // 2.
1599 combinedLists.add(domainWhitelist); // 3.
1600 combinedLists.add(domainInitialWhitelist); // 4.
1601 combinedLists.add(domainFinalWhitelist); // 5.
1602 combinedLists.add(thirdPartyWhitelist); // 6.
1603 combinedLists.add(thirdPartyDomainWhitelist); // 7.
1604 combinedLists.add(thirdPartyDomainInitialWhitelist); // 8.
1606 // Add the black lists (9-22).
1607 combinedLists.add(mainBlacklist); // 9.
1608 combinedLists.add(initialBlacklist); // 10.
1609 combinedLists.add(finalBlacklist); // 11.
1610 combinedLists.add(domainBlacklist); // 12.
1611 combinedLists.add(domainInitialBlacklist); // 13.
1612 combinedLists.add(domainFinalBlacklist); // 14.
1613 combinedLists.add(domainRegularExpressionBlacklist); // 15.
1614 combinedLists.add(thirdPartyBlacklist); // 16.
1615 combinedLists.add(thirdPartyInitialBlacklist); // 17.
1616 combinedLists.add(thirdPartyDomainBlacklist); // 18.
1617 combinedLists.add(thirdPartyDomainInitialBlacklist); // 19.
1618 combinedLists.add(thirdPartyRegularExpressionBlacklist); // 20.
1619 combinedLists.add(thirdPartyDomainRegularExpressionBlacklist); // 21.
1620 combinedLists.add(regularExpressionBlacklist); // 22.
1622 return combinedLists;
1625 public String[] checkBlocklist(String currentDomain, String resourceUrl, boolean isThirdPartyRequest, ArrayList<List<String[]>> blocklist) {
1626 // Get the blocklist name.
1627 String BLOCK_LIST_NAME_STRING = blocklist.get(0).get(1)[0];
1629 // Assert that currentDomain != null only if this is a third party request. Apparently, lint can't tell that this isn't redundant.
1630 //noinspection RedundantIfStatement
1631 if (isThirdPartyRequest) {
1632 assert currentDomain != null;
1635 // Process the white lists.
1637 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(MAIN_WHITELIST))) {
1638 switch (whitelistEntry.length) {
1639 case 2: // There is one entry.
1640 if (resourceUrl.contains(whitelistEntry[0])) {
1641 // Return a whitelist match request allowed.
1642 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whitelistEntry[0], whitelistEntry[1]};
1646 case 3: // There are two entries.
1647 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1648 // Return a whitelist match request allowed.
1649 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1653 case 4: // There are three entries.
1654 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1655 // Return a whitelist match request allowed.
1656 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2], whitelistEntry[3]};
1662 // Final white list.
1663 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(FINAL_WHITELIST))) {
1664 if (whitelistEntry.length == 2) { // There is one entry.
1665 if (resourceUrl.contains(whitelistEntry[0])) {
1666 // Return a whitelist match request allowed.
1667 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_WHITELIST, whitelistEntry[0], whitelistEntry[1]};
1669 } else { // There are two entries.
1670 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1671 // Return a whitelist match request allowed.
1672 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1677 // Only check the domain lists if the current domain is not null (like `about:blank`).
1678 if (currentDomain != null) {
1679 // Domain white list.
1680 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(DOMAIN_WHITELIST))) {
1681 switch (whitelistEntry.length) {
1682 case 3: // There is one entry.
1683 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1684 // Return a whitelist match request allowed.
1685 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1689 case 4: // There are two entries.
1690 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1691 // Return a whitelist match request allowed.
1692 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1697 case 5: // There are three entries.
1698 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
1699 // Return a whitelist match request allowed.
1700 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2] + "\n" +
1701 whitelistEntry[3], whitelistEntry[4]};
1705 case 6: // There are four entries.
1706 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
1707 resourceUrl.contains(whitelistEntry[4])) {
1708 // Return a whitelist match request allowed.
1709 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2] + "\n" +
1710 whitelistEntry[3] + "\n" + whitelistEntry[4], whitelistEntry[5]};
1716 // Domain initial white list.
1717 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(DOMAIN_INITIAL_WHITELIST))) {
1718 switch (whitelistEntry.length) {
1719 case 3: // There is one entry.
1720 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
1721 // Return a whitelist match request allowed.
1722 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1726 case 4: // There are two entries.
1727 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1728 // Return a whitelist match request allowed.
1729 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1734 case 5: // There are three entries.
1735 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.startsWith(whitelistEntry[3])) {
1736 // Return a whitelist match request allowed.
1737 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2] + "\n" +
1738 whitelistEntry[3], whitelistEntry[4]};
1744 // Domain final white list.
1745 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(DOMAIN_FINAL_WHITELIST))) {
1746 switch (whitelistEntry.length) {
1747 case 3: // There is one entry;
1748 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.endsWith(whitelistEntry[1])) {
1749 // Return a whitelist match request allowed.
1750 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1754 case 4: // There are two entries;
1755 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.endsWith(whitelistEntry[2])) {
1756 // Return a whitelist match request allowed.
1757 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1765 // Only check the third-party white lists if this is a third-party request.
1766 if (isThirdPartyRequest) {
1767 // Third-party white list.
1768 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_WHITELIST))) {
1769 switch (whitelistEntry.length) {
1770 case 2: // There is one entry
1771 if (resourceUrl.contains(whitelistEntry[0])) {
1772 // Return a whitelist match request allowed.
1773 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whitelistEntry[0], whitelistEntry[1]};
1777 case 3: // There are two entries.
1778 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1779 // Return a whitelist match request allowed.
1780 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1784 case 4: // There are three entries.
1785 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1786 // Return a whitelist match request allowed.
1787 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1792 case 5: // There are four entries.
1793 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
1794 // Return a whitelist match request allowed.
1795 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2] + "\n" +
1796 whitelistEntry[3], whitelistEntry[4]};
1800 case 6: // There are five entries.
1801 if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
1802 resourceUrl.contains(whitelistEntry[4])) {
1803 // Return a whitelist match request allowed.
1804 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2] + "\n" +
1805 whitelistEntry[3] + "\n" + whitelistEntry[4], whitelistEntry[5]};
1811 // Third-party domain white list.
1812 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_DOMAIN_WHITELIST))) {
1813 if (whitelistEntry.length == 3) { // There is one entry.
1814 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1815 // Return a whitelist match request allowed.
1816 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1818 } else { // There are two entries.
1819 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1820 // Return a whitelist match request allowed.
1821 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1827 // Third-party domain initial white list.
1828 for (String[] whitelistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_DOMAIN_INITIAL_WHITELIST))) {
1829 if (whitelistEntry.length == 3) { // There is one entry.
1830 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
1831 // Return a whitelist match request allowed.
1832 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1], whitelistEntry[2]};
1834 } else { // There are two entries.
1835 if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1836 // Return a whitelist match request allowed.
1837 return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whitelistEntry[0] + "\n" + whitelistEntry[1] + "\n" + whitelistEntry[2],
1844 // Process the black lists.
1846 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(MAIN_BLACKLIST))) {
1847 switch (blacklistEntry.length) {
1848 case 2: // There is one entry.
1849 if (resourceUrl.contains(blacklistEntry[0])) {
1850 // Return a blacklist match request blocked.
1851 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
1855 case 3: // There are two entries.
1856 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1857 // Return a blacklist match request blocked.
1858 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1862 case 4: // There are three entries.
1863 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
1864 // Return a blacklist match request blocked.
1865 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2], blacklistEntry[3]};
1869 case 5: // There are four entries.
1870 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
1871 // Return a blacklist match request blocked.
1872 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2] + "\n" +
1873 blacklistEntry[3], blacklistEntry[4]};
1877 case 6: // There are five entries.
1878 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3]) &&
1879 resourceUrl.contains(blacklistEntry[4])) {
1880 // Return a blacklist match request blocked.
1881 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2] + "\n" +
1882 blacklistEntry[3] + "\n" + blacklistEntry[4], blacklistEntry[5]};
1888 // Initial black list.
1889 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(INITIAL_BLACKLIST))) {
1890 if (blacklistEntry.length == 2) { // There is one entry.
1891 if (resourceUrl.startsWith(blacklistEntry[0])) {
1892 // Return a blacklist match request blocked.
1893 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
1895 } else { // There are two entries
1896 if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1897 // Return a blacklist match request blocked.
1898 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1903 // Final black list.
1904 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(FINAL_BLACKLIST))) {
1905 switch (blacklistEntry.length) {
1906 case 2: // There is one entry.
1907 if (resourceUrl.endsWith(blacklistEntry[0])) {
1908 // Return a blacklist match request blocked.
1909 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
1913 case 3: // There are two entries.
1914 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
1915 // Return a blacklist match request blocked.
1916 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1920 case 4: // There are three entries.
1921 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
1922 // Return a blacklist match request blocked.
1923 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2], blacklistEntry[3]};
1929 // Only check the domain lists if the current domain is not null (like `about:blank`).
1930 if (currentDomain != null) {
1931 // Domain black list.
1932 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(DOMAIN_BLACKLIST))) {
1933 switch (blacklistEntry.length) {
1934 case 3: // There is one entry.
1935 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1936 // Return a blacklist match request blocked.
1937 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1941 case 4: // There are two entries.
1942 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
1943 // Return a blacklist match request blocked.
1944 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2],
1949 case 5: // There are three entries.
1950 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
1951 // Return a blacklist match request blocked.
1952 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2] + "\n" +
1953 blacklistEntry[3], blacklistEntry[4]};
1959 // Domain initial black list.
1960 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(DOMAIN_INITIAL_BLACKLIST))) {
1961 // Store the entry in the resource request log.
1962 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
1963 // Return a blacklist match request blocked.
1964 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1968 // Domain final black list.
1969 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(DOMAIN_FINAL_BLACKLIST))) {
1970 switch (blacklistEntry.length) {
1971 case 3: // There is one entry.
1972 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
1973 // Return a blacklist match request blocked.
1974 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1978 case 4: // There are two entries.
1979 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
1980 // Return a blacklist match request blocked.
1981 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2],
1988 // Domain regular expression black list.
1989 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(DOMAIN_REGULAR_EXPRESSION_BLACKLIST))) {
1990 if (currentDomain.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
1991 // Return a blacklist match request blocked.
1992 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
1997 // Only check the third-party black lists if this is a third-party request.
1998 if (isThirdPartyRequest) {
1999 // Third-party black list.
2000 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_BLACKLIST))) {
2001 switch (blacklistEntry.length) {
2002 case 2: // There is one entry.
2003 if (resourceUrl.contains(blacklistEntry[0])) {
2004 // Return a blacklist match request blocked.
2005 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
2009 case 3: // There are two entries.
2010 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
2011 // Return a blacklist match request blocked.
2012 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
2016 case 4: // There are three entries.
2017 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
2018 // Return a blacklist match request blocked.
2019 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2],
2024 case 5: // There are four entries.
2025 if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
2026 // Return a blacklist match request blocked.
2027 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2] + "\n" +
2028 blacklistEntry[3], blacklistEntry[4]};
2034 // Third-party initial black list.
2035 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_INITIAL_BLACKLIST))) {
2036 if (blacklistEntry.length == 2) { // There is one entry.
2037 if (resourceUrl.startsWith(blacklistEntry[0])) {
2038 // Return a blacklist match request blocked.
2039 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
2041 } else { // There are two entries.
2042 if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
2043 // Return a blacklist match request blocked.
2044 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
2049 // Third-party domain black list.
2050 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_DOMAIN_BLACKLIST))) {
2051 if (blacklistEntry.length == 3) { // There is one entry.
2052 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
2053 // Return a blacklist match request blocked.
2054 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
2056 } else { // There are two entries.
2057 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
2058 // Return a blacklist match request blocked.
2059 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" + blacklistEntry[2],
2065 // Third-party domain initial black list.
2066 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST))) {
2067 switch (blacklistEntry.length) {
2068 case 3: // There is one entry.
2069 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
2070 // Return a blacklist match request blocked.
2071 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
2075 case 4: // There are two entries.
2076 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
2077 // Return a blacklist match request blocked.
2078 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" +
2079 blacklistEntry[2], blacklistEntry[3]};
2083 case 5: // There are three entries.
2084 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
2085 // Return a blacklist match request blocked.
2086 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1] + "\n" +
2087 blacklistEntry[2] + "\n" + blacklistEntry[3], blacklistEntry[4]};
2093 // Third-party regular expression black list.
2094 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST))) {
2095 if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
2096 // Return a blacklist match request blocked.
2097 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
2101 // Third-party domain regular expression black list.
2102 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST))) {
2103 if (currentDomain.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
2104 // Return a blacklist match request blocked.
2105 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0] + "\n" + blacklistEntry[1], blacklistEntry[2]};
2110 // Regular expression black list.
2111 for (String[] blacklistEntry : blocklist.get(Integer.valueOf(REGULAR_EXPRESSION_BLACKLIST))) {
2112 if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
2113 // Return a blacklist match request blocked.
2114 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1]};
2118 // Return a no match request default.
2119 return new String[] {REQUEST_DEFAULT};