2 * Copyright © 2018-2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. 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.regex.Pattern
30 class BlocklistHelper {
32 // Describe the schema of the string array in each entry of the resource requests array list.
33 const val REQUEST_DISPOSITION = 0
34 const val REQUEST_URL = 1
35 const val REQUEST_BLOCKLIST = 2
36 const val REQUEST_SUBLIST = 3
37 const val REQUEST_BLOCKLIST_ENTRIES = 4
38 const val REQUEST_BLOCKLIST_ORIGINAL_ENTRY = 5
40 // The request disposition options.
41 const val REQUEST_DEFAULT = "0"
42 const val REQUEST_ALLOWED = "1"
43 const val REQUEST_THIRD_PARTY = "2"
44 const val REQUEST_BLOCKED = "3"
47 const val MAIN_WHITELIST = "1"
48 const val FINAL_WHITELIST = "2"
49 const val DOMAIN_WHITELIST = "3"
50 const val DOMAIN_INITIAL_WHITELIST = "4"
51 const val DOMAIN_FINAL_WHITELIST = "5"
52 const val THIRD_PARTY_WHITELIST = "6"
53 const val THIRD_PARTY_DOMAIN_WHITELIST = "7"
54 const val THIRD_PARTY_DOMAIN_INITIAL_WHITELIST = "8"
57 const val MAIN_BLACKLIST = "9"
58 const val INITIAL_BLACKLIST = "10"
59 const val FINAL_BLACKLIST = "11"
60 const val DOMAIN_BLACKLIST = "12"
61 const val DOMAIN_INITIAL_BLACKLIST = "13"
62 const val DOMAIN_FINAL_BLACKLIST = "14"
63 const val DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "15"
64 const val THIRD_PARTY_BLACKLIST = "16"
65 const val THIRD_PARTY_INITIAL_BLACKLIST = "17"
66 const val THIRD_PARTY_DOMAIN_BLACKLIST = "18"
67 const val THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST = "19"
68 const val THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST = "20"
69 const val THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "21"
70 const val REGULAR_EXPRESSION_BLACKLIST = "22"
73 fun parseBlocklist(assetManager: AssetManager, blocklistName: String): ArrayList<List<Array<String>>> {
74 // Initialize the header list.
75 val headers: MutableList<Array<String>> = ArrayList() // 0.
77 // Initialize the whitelists.
78 val mainWhitelist: MutableList<Array<String>> = ArrayList() // 1.
79 val finalWhitelist: MutableList<Array<String>> = ArrayList() // 2.
80 val domainWhitelist: MutableList<Array<String>> = ArrayList() // 3.
81 val domainInitialWhitelist: MutableList<Array<String>> = ArrayList() // 4.
82 val domainFinalWhitelist: MutableList<Array<String>> = ArrayList() // 5.
83 val thirdPartyWhitelist: MutableList<Array<String>> = ArrayList() // 6.
84 val thirdPartyDomainWhitelist: MutableList<Array<String>> = ArrayList() // 7.
85 val thirdPartyDomainInitialWhitelist: MutableList<Array<String>> = ArrayList() // 8.
87 // Initialize the blacklists.
88 val mainBlacklist: MutableList<Array<String>> = ArrayList() // 9.
89 val initialBlacklist: MutableList<Array<String>> = ArrayList() // 10.
90 val finalBlacklist: MutableList<Array<String>> = ArrayList() // 11.
91 val domainBlacklist: MutableList<Array<String>> = ArrayList() // 12.
92 val domainInitialBlacklist: MutableList<Array<String>> = ArrayList() // 13.
93 val domainFinalBlacklist: MutableList<Array<String>> = ArrayList() // 14.
94 val domainRegularExpressionBlacklist: MutableList<Array<String>> = ArrayList() // 15.
95 val thirdPartyBlacklist: MutableList<Array<String>> = ArrayList() // 16.
96 val thirdPartyInitialBlacklist: MutableList<Array<String>> = ArrayList() // 17.
97 val thirdPartyDomainBlacklist: MutableList<Array<String>> = ArrayList() // 18.
98 val thirdPartyDomainInitialBlacklist: MutableList<Array<String>> = ArrayList() // 19.
99 val regularExpressionBlacklist: MutableList<Array<String>> = ArrayList() // 20.
100 val thirdPartyRegularExpressionBlacklist: MutableList<Array<String>> = ArrayList() // 21.
101 val thirdPartyDomainRegularExpressionBlacklist: MutableList<Array<String>> = ArrayList() // 22.
103 // Populate the blocklists. The `try` is required by input stream reader.
105 // Load the blocklist into a buffered reader.
106 val bufferedReader = BufferedReader(InputStreamReader(assetManager.open(blocklistName)))
108 // Create strings for storing the block list entries.
109 var blocklistEntry: String
110 var originalBlocklistEntry: String
112 // Parse the block list.
113 bufferedReader.forEachLine {
114 // Store the original block list entry.
115 originalBlocklistEntry = it
117 // Remove any `^` from the block list entry. Privacy Browser does not process them in the interest of efficiency.
118 blocklistEntry = it.replace("^", "")
121 if (blocklistEntry.contains("##") || blocklistEntry.contains("#?#") || blocklistEntry.contains("#@#") || blocklistEntry.startsWith("[")) {
122 // Entries that contain `##`, `#?#`, and `#@#` are for hiding elements in the main page's HTML. Entries that start with `[` describe the AdBlock compatibility level.
123 // Do nothing. Privacy Browser does not currently use these entries.
125 //Log.i("Blocklists", "Not added: " + blocklistEntry);
126 } else 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 if (blocklistEntry.contains("\$websocket") || blocklistEntry.contains("\$third-party,websocket") || blocklistEntry.contains("\$script,websocket")) {
131 // 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 val listVersion = arrayOf(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 val listTitle = arrayOf(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 if (blocklistEntry.contains("~third-party")) { // Ignore entries that contain `~third-party`.
166 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
167 } else if (blocklistEntry.contains("third-party")) { // Third-party whitelist entries.
168 if (blocklistEntry.contains("domain=")) { // Third-party domain whitelist entries.
170 var entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
171 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
172 var domains = filters.substring(filters.indexOf("domain=") + 7)
174 if (domains.contains("~")) { // It is uncertain what a `~` domain means inside an `@@` entry.
177 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
178 } else if (blocklistEntry.startsWith("|")) { // Third-party domain initial whitelist entries.
179 // Strip out the initial `|`.
180 entry = entry.substring(1)
182 if (entry == "http://" || entry == "https://") { // Ignore generic entries.
183 // Do nothing. These entries are designed for filter options that Privacy Browser does not use.
185 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
186 } else { // Process third-party domain initial whitelist entries.
187 // Process each domain.
189 // Create a string to keep track of the current domain.
192 if (domains.contains("|")) { // There is more than one domain in the list.
193 // Get the first domain from the list.
194 domain = domains.substring(0, domains.indexOf("|"))
196 // Remove the first domain from the list.
197 domains = domains.substring(domains.indexOf("|") + 1)
198 } else { // There is only one domain in the list.
202 if (entry.contains("*")) { // Process a third-party domain initial whitelist double entry.
203 // Get the index of the wildcard.
204 val wildcardIndex = entry.indexOf("*")
206 // Split the entry into components.
207 val firstEntry = entry.substring(0, wildcardIndex)
208 val secondEntry = entry.substring(wildcardIndex + 1)
210 // Create an entry string array.
211 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
213 // Add the entry to the whitelist.
214 thirdPartyDomainInitialWhitelist.add(domainDoubleEntry)
216 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial whitelist added: " + domain + " , " + firstEntry + " , " + secondEntry +
217 // " - " + originalBlocklistEntry);
218 } else { // Process a third-party domain initial whitelist single entry.
219 // Create a domain entry string array.
220 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
222 // Add the entry to the third party domain initial whitelist.
223 thirdPartyDomainInitialWhitelist.add(domainEntry)
225 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial whitelist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
227 } while (domains.contains("|"))
229 } else { // Third-party domain entries.
230 // Process each domain.
232 // Create a string to keep track of the current domain.
235 if (domains.contains("|")) { // three is more than one domain in the list.
236 // Get the first domain from the list.
237 domain = domains.substring(0, domains.indexOf("|"))
239 // Remove the first domain from the list.
240 domains = domains.substring(domains.indexOf("|") + 1)
241 } else { // There is only one domain in the list.
245 // Remove any trailing `*` from the entry.
246 if (entry.endsWith("*")) {
247 entry = entry.substring(0, entry.length - 1)
250 if (entry.contains("*")) { // Process a third-party domain double entry.
251 // Get the index of the wildcard.
252 val wildcardIndex = entry.indexOf("*")
254 // Split the entry into components.
255 val firstEntry = entry.substring(0, wildcardIndex)
256 val secondEntry = entry.substring(wildcardIndex + 1)
258 // Create an entry string array.
259 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
261 // Add the entry to the whitelist.
262 thirdPartyDomainWhitelist.add(domainDoubleEntry)
264 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain whitelist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
265 // originalBlocklistEntry);
266 } else { // Process a third-party domain single entry.
267 // Create an entry string array.
268 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
270 // Add the entry to the whitelist.
271 thirdPartyDomainWhitelist.add(domainEntry)
273 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain whitelist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
275 } while (domains.contains("|"))
277 } else { // Process third-party whitelist entries.
279 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
281 if (entry.contains("*")) { // There are two or more entries.
282 // Get the index of the wildcard.
283 val wildcardIndex = entry.indexOf("*")
285 // Split the entry into components.
286 val firstEntry = entry.substring(0, wildcardIndex)
287 val secondEntry = entry.substring(wildcardIndex + 1)
289 if (secondEntry.contains("*")) { // There are three or more entries.
290 // Get the index of the wildcard.
291 val secondWildcardIndex = secondEntry.indexOf("*")
293 // Split the entry into components.
294 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
295 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
297 if (thirdEntry.contains("*")) { // There are four or more entries.
298 // Get the index of the wildcard.
299 val thirdWildcardIndex = thirdEntry.indexOf("*")
301 // Split the entry into components.
302 val realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex)
303 val fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1)
305 if (fourthEntry.contains("*")) { // Process a third-party whitelist quintuple entry.
306 // Get the index of the wildcard.
307 val fourthWildcardIndex = fourthEntry.indexOf("*")
309 // Split the entry into components.
310 val realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex)
311 val fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1)
313 // Create an entry string array.
314 val quintupleEntry = arrayOf(firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlocklistEntry)
316 // Add the entry to the whitelist.
317 thirdPartyWhitelist.add(quintupleEntry)
319 //Log.i("Blocklists", headers.get(1)[0] + " third-party whitelist added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
320 // realFourthEntry + " , " + fifthEntry + " - " + originalBlocklistEntry);
321 } else { // Process a third-party whitelist quadruple entry.
322 // Create an entry string array.
323 val quadrupleEntry = arrayOf(firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry)
325 // Add the entry to the whitelist.
326 thirdPartyWhitelist.add(quadrupleEntry)
328 //Log.i("Blocklists", headers.get(1)[0] + " third-party whitelist added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
329 // fourthEntry + " - " + originalBlocklistEntry);
331 } else { // Process a third-party whitelist triple entry.
332 // Create an entry string array.
333 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
335 // Add the entry to the whitelist.
336 thirdPartyWhitelist.add(tripleEntry)
338 //Log.i("Blocklists", headers.get(1)[0] + " third-party whitelist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
339 // originalBlocklistEntry);
341 } else { // Process a third-party whitelist double entry.
342 // Create an entry string array.
343 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
345 // Add the entry to the whitelist.
346 thirdPartyWhitelist.add(doubleEntry)
348 //Log.i("Blocklists", headers.get(1)[0] + " third-party whitelist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
350 } else { // Process a third-party whitelist single entry.
351 // Create an entry string array.
352 val singleEntry = arrayOf(entry, originalBlocklistEntry)
354 // Add the entry to the whitelist.
355 thirdPartyWhitelist.add(singleEntry)
357 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain whitelist added: " + entry + " - " + originalBlocklistEntry);
360 } else if (blocklistEntry.contains("domain=")) { // Process domain whitelist entries.
362 var entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
363 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
364 var domains = filters.substring(filters.indexOf("domain=") + 7)
366 if (entry.startsWith("|")) { // Initial domain whitelist entries.
367 // Strip the initial `|`.
368 entry = entry.substring(1)
370 if (entry == "http://" || entry == "https://") { // Ignore generic entries.
371 // Do nothing. These entries are designed for filter options that Privacy Browser does not use.
373 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
374 } else { // Initial domain whitelist entry.
375 // Process each domain.
377 // Create a string to keep track of the current domain.
380 if (domains.contains("|")) { // There is more than one domain in the list.
381 // Get the first domain from the list.
382 domain = domains.substring(0, domains.indexOf("|"))
384 // Remove the first domain from the list.
385 domains = domains.substring(domains.indexOf("|") + 1)
386 } else { // There is only one domain in the list.
390 if (entry.contains("*")) { // There are two or more entries.
391 // Get the index of the wildcard.
392 val wildcardIndex = entry.indexOf("*")
394 // Split the entry into components.
395 val firstEntry = entry.substring(0, wildcardIndex)
396 val secondEntry = entry.substring(wildcardIndex + 1)
398 if (secondEntry.contains("*")) { // Process a domain initial triple entry.
399 // Get the index of the wildcard.
400 val secondWildcardIndex = secondEntry.indexOf("*")
402 // Split the entry into components.
403 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
404 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
406 // Create an entry string array.
407 val domainTripleEntry = arrayOf(domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
409 // Add the entry to the whitelist.
410 domainInitialWhitelist.add(domainTripleEntry)
412 //Log.i("Blocklists", headers.get(1)[0] + " domain initial whitelist entry added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
413 // thirdEntry + " - " + originalBlocklistEntry);
414 } else { // Process a domain initial double entry.
415 // Create an entry string array.
416 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
418 // Add the entry to the whitelist.
419 domainInitialWhitelist.add(domainDoubleEntry)
421 //Log.i("Blocklists", headers.get(1)[0] + " domain initial whitelist entry added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
422 // originalBlocklistEntry);
424 } else { // Process a domain initial single entry.
425 // Create an entry string array.
426 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
428 // Add the entry to the whitelist.
429 domainInitialWhitelist.add(domainEntry)
431 //Log.i("Blocklists", headers.get(1)[0] + " domain initial whitelist entry added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
433 } while (domains.contains("|"))
435 } else if (entry.endsWith("|")) { // Final domain whitelist entries.
436 // Strip the `|` from the end of the entry.
437 entry = entry.substring(0, entry.length - 1)
439 // Process each domain.
441 // Create a string to keep track of the current domain.
444 if (domains.contains("|")) { // There is more than one domain in the list.
445 // Get the first domain from the list.
446 domain = domains.substring(0, domains.indexOf("|"))
448 // Remove the first domain from the list.
449 domains = domains.substring(domains.indexOf("|") + 1)
450 } else { // There is only one domain in the list.
454 if (entry.contains("*")) { // Process a domain final whitelist double entry.
455 // Get the index of the wildcard.
456 val wildcardIndex = entry.indexOf("*")
458 // Split the entry into components.
459 val firstEntry = entry.substring(0, wildcardIndex)
460 val secondEntry = entry.substring(wildcardIndex + 1)
462 // Create an entry string array.
463 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
465 // Add the entry to the whitelist.
466 domainFinalWhitelist.add(domainDoubleEntry)
468 //Log.i("Blocklists", headers.get(1)[0] + " domain final whitelist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
469 // originalBlocklistEntry);
470 } else { // Process a domain final whitelist single entry.
471 // create an entry string array.
472 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
474 // Add the entry to the whitelist.
475 domainFinalWhitelist.add(domainEntry)
477 //Log.i("Blocklists", headers.get(1)[0] + " domain final whitelist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
479 } while (domains.contains("|"))
480 } else { // Standard domain whitelist entries with filters.
481 if (domains.contains("~")) { // It is uncertain what a `~` domain means inside an `@@` entry.
484 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
486 // Process each domain.
488 // Create a string to keep track of the current domain.
491 if (domains.contains("|")) { // There is more than one domain in the list.
492 // Get the first domain from the list.
493 domain = domains.substring(0, domains.indexOf("|"))
495 // Remove the first domain from the list.
496 domains = domains.substring(domains.indexOf("|") + 1)
497 } else { // There is only one domain in the list.
501 if (entry.contains("*")) { // There are two or more entries.
502 // Get the index of the wildcard.
503 val wildcardIndex = entry.indexOf("*")
505 // Split the entry into components.
506 val firstEntry = entry.substring(0, wildcardIndex)
507 val secondEntry = entry.substring(wildcardIndex + 1)
509 if (secondEntry.contains("*")) { // There are three or more entries.
510 // Get the index of the wildcard.
511 val secondWildcardIndex = secondEntry.indexOf("*")
513 // Split the entry into components.
514 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
515 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
517 if (thirdEntry.contains("*")) { // Process a domain whitelist quadruple entry.
518 // Get the index of the wildcard.
519 val thirdWildcardIndex = thirdEntry.indexOf("*")
521 // Split the entry into components.
522 val realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex)
523 val fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1)
525 // Create an entry string array.
526 val domainQuadrupleEntry = arrayOf(domain, firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry)
528 // Add the entry to the whitelist.
529 domainWhitelist.add(domainQuadrupleEntry)
531 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
532 // realThirdEntry + " , " + fourthEntry + " - " + originalBlocklistEntry);
533 } else { // Process a domain whitelist triple entry.
534 // Create an entry string array.
535 val domainTripleEntry = arrayOf(domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
537 // Add the entry to the whitelist.
538 domainWhitelist.add(domainTripleEntry)
540 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
541 // thirdEntry + " - " + originalBlocklistEntry);
543 } else { // Process a domain whitelist double entry.
544 // Create an entry string array.
545 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
547 // Add the entry to the whitelist.
548 domainWhitelist.add(domainDoubleEntry)
550 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added : " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
551 // originalBlocklistEntry);
553 } else { // Process a domain whitelist single entry.
554 // Create an entry string array.
555 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
557 // Add the entry to the whitelist.
558 domainWhitelist.add(domainEntry)
560 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added : " + domain + " , " + entry + " - " + originalBlocklistEntry);
562 } while (domains.contains("|"))
565 } // Ignore all other filter entries.
566 } else if (blocklistEntry.endsWith("|")) { // Final whitelist entries.
567 // Remove the final `|` from the entry.
568 val entry = blocklistEntry.substring(0, blocklistEntry.length - 1)
570 if (entry.contains("*")) { // Process a final whitelist double entry
571 // Get the index of the wildcard.
572 val wildcardIndex = entry.indexOf("*")
574 // split the entry into components.
575 val firstEntry = entry.substring(0, wildcardIndex)
576 val secondEntry = entry.substring(wildcardIndex + 1)
578 // Create an entry string array.
579 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
581 // Add the entry to the whitelist.
582 finalWhitelist.add(doubleEntry)
584 //Log.i("Blocklists", headers.get(1)[0] + " final whitelist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
585 } else { // Process a final whitelist single entry.
586 // Create an entry string array.
587 val singleEntry = arrayOf(entry, originalBlocklistEntry)
589 // Add the entry to the whitelist.
590 finalWhitelist.add(singleEntry)
592 //Log.i("Blocklists", headers.get(1)[0] + " final whitelist added: " + entry + " - " + originalBlocklistEntry);
594 } else { // Main whitelist entries.
595 if (blocklistEntry.contains("*")) { // There are two or more entries.
596 // Get the index of the wildcard.
597 val wildcardIndex = blocklistEntry.indexOf("*")
599 // Split the entry into components.
600 val firstEntry = blocklistEntry.substring(0, wildcardIndex)
601 val secondEntry = blocklistEntry.substring(wildcardIndex + 1)
603 if (secondEntry.contains("*")) { // Process a main whitelist triple entry.
604 // Get the index of the wildcard.
605 val secondWildcardIndex = secondEntry.indexOf("*")
607 // Split the entry into components.
608 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
609 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
611 // Create an entry string array.
612 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
614 // Add the entry to the whitelist.
615 mainWhitelist.add(tripleEntry)
617 //Log.i("Blocklists", headers.get(1)[0] + " main whitelist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
618 } else { // Process a main whitelist double entry.
619 // Create an entry string array.
620 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
622 // Add the entry to the whitelist.
623 mainWhitelist.add(doubleEntry)
625 //Log.i("Blocklists", headers.get(1)[0] + " main whitelist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
627 } else { // Process a main whitelist single entry.
628 // Create an entry string array.
629 val singleEntry = arrayOf(blocklistEntry, originalBlocklistEntry)
631 // Add the entry to the whitelist.
632 mainWhitelist.add(singleEntry)
634 //Log.i("Blocklists", headers.get(1)[0] + " main whitelist added: " + blocklistEntry + " - " + originalBlocklistEntry);
637 } else if (blocklistEntry.endsWith("|")) { // Final blacklist entries.
638 // Strip out the final "|"
639 var entry = blocklistEntry.substring(0, blocklistEntry.length - 1)
641 // Strip out any initial `||`. They are redundant in this case because the blocklist entry is being matched against the end of the URL.
642 if (entry.startsWith("||")) {
643 entry = entry.substring(2)
646 if (entry.contains("*")) { // Process a final blacklist double entry.
647 // Get the index of the wildcard.
648 val wildcardIndex = entry.indexOf("*")
650 // Split the entry into components.
651 val firstEntry = entry.substring(0, wildcardIndex)
652 val secondEntry = entry.substring(wildcardIndex + 1)
654 // Create an entry string array.
655 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
657 // Add the entry to the blacklist.
658 finalBlacklist.add(doubleEntry)
660 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
661 } else { // Process a final blacklist single entry.
662 // create an entry string array.
663 val singleEntry = arrayOf(entry, originalBlocklistEntry)
665 // Add the entry to the blacklist.
666 finalBlacklist.add(singleEntry)
668 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + entry + " - " + originalBlocklistEntry);
670 } else if (blocklistEntry.contains("$")) { // Entries with filter options.
671 // Strip out any initial `||`. These will be treated like any other entry.
672 if (blocklistEntry.startsWith("||")) {
673 blocklistEntry = blocklistEntry.substring(2)
676 if (blocklistEntry.contains("third-party")) { // Third-party entries.
677 if (blocklistEntry.contains("~third-party")) { // Third-party filter whitelist entries.
678 // Do not process these whitelist entries. They are designed to combine with block filters that Privacy Browser doesn't use, like `subdocument` and `xmlhttprequest`.
680 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
681 } else if (blocklistEntry.contains("domain=")) { // Third-party domain entries.
682 if (blocklistEntry.startsWith("|")) { // Third-party domain initial entries.
683 // Strip the initial `|`.
684 blocklistEntry = blocklistEntry.substring(1)
687 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
688 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
689 var domains = filters.substring(filters.indexOf("domain=") + 7)
691 if (entry == "http:" || entry == "https:" || entry == "http://" || entry == "https://") { // Ignore generic entries.
692 // Do nothing. These entries will almost entirely disable the website.
693 // Often the original entry blocks filter options like `$script`, which Privacy Browser does not differentiate.
695 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
696 } else { // Third-party domain initial entries.
697 // Process each domain.
699 // Create a string to keep track of the current domain.
702 if (domains.contains("|")) { // There is more than one domain in the list.
703 // Get the first domain from the list.
704 domain = domains.substring(0, domains.indexOf("|"))
706 // Remove the first domain from the list.
707 domains = domains.substring(domains.indexOf("|") + 1)
708 } else { // There is only one domain in the list.
712 if (entry.contains("*")) { // Three are two or more entries.
713 // Get the index of the wildcard.
714 val wildcardIndex = entry.indexOf("*")
716 // Split the entry into components.
717 val firstEntry = entry.substring(0, wildcardIndex)
718 val secondEntry = entry.substring(wildcardIndex + 1)
720 if (secondEntry.contains("*")) { // Process a third-party domain initial blacklist triple entry.
721 // Get the index of the wildcard.
722 val secondWildcardIndex = secondEntry.indexOf("*")
724 // Split the entry into components.
725 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
726 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
728 // Create an entry string array.
729 val tripleDomainEntry = arrayOf(domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
731 // Add the entry to the blacklist.
732 thirdPartyDomainInitialBlacklist.add(tripleDomainEntry)
734 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial blacklist added: " + domain + " , " + firstEntry + " , " + realSecondEntry +
735 // " , " + thirdEntry + " - " + originalBlocklistEntry);
736 } else { // Process a third-party domain initial blacklist double entry.
737 // Create an entry string array.
738 val doubleDomainEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
740 // Add the entry to the blacklist.
741 thirdPartyDomainInitialBlacklist.add(doubleDomainEntry)
743 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial blacklist added: " + domain + " , " + firstEntry + " , " + secondEntry +
744 // " - " + originalBlocklistEntry);
746 } else { // Process a third-party domain initial blacklist single entry.
747 // Create an entry string array.
748 val singleEntry = arrayOf(domain, entry, originalBlocklistEntry)
750 // Add the entry to the blacklist.
751 thirdPartyDomainInitialBlacklist.add(singleEntry)
753 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain initial blacklist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
755 } while (domains.contains("|"))
757 } else if (blocklistEntry.contains("\\")) { // Process a third-party domain blacklist regular expression.
758 // Parse the entry. At least one regular expression in this entry contains `$`, so the parser uses `/$`.
759 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("/$") + 1)
760 val filters = blocklistEntry.substring(blocklistEntry.indexOf("/$") + 2)
761 var domains = filters.substring(filters.indexOf("domain=") + 7)
763 // Process each domain.
765 // Create a string to keep track of the current domain.
768 if (domains.contains("|")) { // There is more than one domain in the list.
769 // Get the first domain from the list.
770 domain = domains.substring(0, domains.indexOf("|"))
772 // Remove the first domain from the list.
773 domains = domains.substring(domains.indexOf("|") + 1)
774 } else { // There is only one domain in the list.
778 // Create an entry string array.
779 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
781 // Add the entry to the blacklist.
782 thirdPartyDomainRegularExpressionBlacklist.add(domainEntry)
784 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain regular expression blacklist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
785 } while (domains.contains("|"))
786 } else { // Third-party domain entries.
788 var entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
789 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
790 var domains = filters.substring(filters.indexOf("domain=") + 7)
792 // Strip any trailing "*" from the entry.
793 if (entry.endsWith("*")) {
794 entry = entry.substring(0, entry.length - 1)
797 // Track if any third-party whitelist filters are applied.
798 var whitelistDomain = false
800 // Process each domain.
802 // Create a string to keep track of the current domain.
805 if (domains.contains("|")) { // There is more than one domain in the list.
806 // Get the first domain from the list.
807 domain = domains.substring(0, domains.indexOf("|"))
809 // Remove the first domain from the list.
810 domains = domains.substring(domains.indexOf("|") + 1)
811 } else { // The is only one domain in the list.
815 // Differentiate between blocklist domains and whitelist domains.
816 if (domain.startsWith("~")) { // Whitelist third-party domain entry.
817 // Strip the initial `~`.
818 domain = domain.substring(1)
820 // Set the whitelist domain flag.
821 whitelistDomain = true
823 if (entry.contains("*")) { // Process a third-party domain whitelist double entry.
824 // Get the index of the wildcard.
825 val wildcardIndex = entry.indexOf("*")
827 // Split the entry into components.
828 val firstEntry = entry.substring(0, wildcardIndex)
829 val secondEntry = entry.substring(wildcardIndex + 1)
831 // Create an entry string array.
832 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
834 // Add the entry to the whitelist.
835 thirdPartyDomainWhitelist.add(domainDoubleEntry)
837 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain whitelist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
838 // originalBlocklistEntry);
839 } else { // Process a third-party domain whitelist single entry.
840 // Create an entry string array.
841 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
843 // Add the entry to the whitelist.
844 thirdPartyDomainWhitelist.add(domainEntry)
846 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain whitelist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
848 } else { // Third-party domain blacklist entries.
849 if (entry.contains("*")) { // Process a third-party domain blacklist double entry.
850 // Get the index of the wildcard.
851 val wildcardIndex = entry.indexOf("*")
853 // Split the entry into components.
854 val firstEntry = entry.substring(0, wildcardIndex)
855 val secondEntry = entry.substring(wildcardIndex + 1)
857 // Create an entry string array.
858 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
860 // Add the entry to the blacklist
861 thirdPartyDomainBlacklist.add(domainDoubleEntry)
863 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain blacklist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
864 // originalBlocklistEntry);
865 } else { // Process a third-party domain blacklist single entry.
866 // Create an entry string array.
867 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
869 // Add the entry to the blacklist.
870 thirdPartyDomainBlacklist.add(domainEntry)
872 //Log.i("Blocklists", headers.get(1)[0] + " third-party domain blocklist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
875 } while (domains.contains("|"))
877 // Add a third-party blacklist entry if a whitelist domain was processed.
878 if (whitelistDomain) {
879 if (entry.contains("*")) { // Process a third-party blacklist double entry.
880 // Get the index of the wildcard.
881 val wildcardIndex = entry.indexOf("*")
883 // Split the entry into components.
884 val firstEntry = entry.substring(0, wildcardIndex)
885 val secondEntry = entry.substring(wildcardIndex + 1)
887 // Create an entry string array.
888 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
890 // Add the entry to the blacklist.
891 thirdPartyBlacklist.add(doubleEntry)
893 //Log.i("Blocklists", headers.get(1)[0] + " third-party blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
894 } else { // Process a third-party blacklist single entry.
895 // Create an entry string array.
896 val singleEntry = arrayOf(entry, originalBlocklistEntry)
898 // Add an entry to the blacklist.
899 thirdPartyBlacklist.add(singleEntry)
901 //Log.i("Blocklists", headers.get(1)[0] + " third-party blacklist added: " + entry + " - " + originalBlocklistEntry);
905 } else if (blocklistEntry.startsWith("|")) { // Third-party initial blacklist entries.
906 // Strip the initial `|`.
907 blocklistEntry = blocklistEntry.substring(1)
910 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
911 if (entry.contains("*")) { // Process a third-party initial blacklist double entry.
912 // Get the index of the wildcard.
913 val wildcardIndex = entry.indexOf("*")
915 // Split the entry into components.
916 val firstEntry = entry.substring(0, wildcardIndex)
917 val secondEntry = entry.substring(wildcardIndex + 1)
919 // Create an entry string array.
920 val thirdPartyDoubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
922 // Add the entry to the blacklist.
923 thirdPartyInitialBlacklist.add(thirdPartyDoubleEntry)
925 //Log.i("Blocklists", headers.get(1)[0] + " third-party initial blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
926 } else { // Process a third-party initial blacklist single entry.
927 // Create an entry string array.
928 val singleEntry = arrayOf(entry, originalBlocklistEntry)
930 // Add the entry to the blacklist.
931 thirdPartyInitialBlacklist.add(singleEntry)
933 //Log.i("Blocklists", headers.get(1)[0] + " third-party initial blacklist added: " + entry + " - " + originalBlocklistEntry);
935 } else if (blocklistEntry.contains("\\")) { // Process a regular expression blacklist entry.
936 // Prepare a string to hold the entry.
939 val entry: String = if (blocklistEntry.contains("$/$")) { // The first `$` is part of the regular expression.
940 blocklistEntry.substring(0, blocklistEntry.indexOf("$/$") + 2)
941 } else { // The only `$` indicates the filter options.
942 blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
945 // Create an entry string array.
946 val singleEntry = arrayOf(entry, originalBlocklistEntry)
948 // Add the entry to the blacklist.
949 thirdPartyRegularExpressionBlacklist.add(singleEntry)
951 //Log.i("Blocklists", headers.get(1)[0] + " third-party regular expression blacklist added: " + entry + " - " + originalBlocklistEntry);
952 } else if (blocklistEntry.contains("*")) { // Third-party and regular expression blacklist entries.
954 var entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
956 if (entry.endsWith("*")) { // Process a third-party blacklist single entry.
957 // Strip the final `*`.
958 entry = entry.substring(0, entry.length - 1)
960 // Create an entry string array.
961 val singleEntry = arrayOf(entry, originalBlocklistEntry)
963 // Add the entry to the blacklist.
964 thirdPartyBlacklist.add(singleEntry)
966 //Log.i("Blocklists", headers.get(1)[0] + " third party blacklist added: " + entry + " - " + originalBlocklistEntry);
967 } else { // There are two or more entries.
968 // Get the index of the wildcard.
969 val wildcardIndex = entry.indexOf("*")
971 // Split the entry into components.
972 val firstEntry = entry.substring(0, wildcardIndex)
973 val secondEntry = entry.substring(wildcardIndex + 1)
975 if (secondEntry.contains("*")) { // There are three or more entries.
976 // Get the index of the wildcard.
977 val secondWildcardIndex = secondEntry.indexOf("*")
979 // Split the entry into components.
980 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
981 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
983 if (thirdEntry.contains("*")) { // Process a third-party blacklist quadruple entry.
984 // Get the index of the wildcard.
985 val thirdWildcardIndex = thirdEntry.indexOf("*")
987 // Split the entry into components.
988 val realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex)
989 val fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1)
991 // Create an entry string array.
992 val quadrupleEntry = arrayOf(firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry)
994 // Add the entry to the blacklist.
995 thirdPartyBlacklist.add(quadrupleEntry)
997 //Log.i("Blocklists", headers.get(1)[0] + " third-party blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
998 // fourthEntry + " - " + originalBlocklistEntry);
999 } else { // Process a third-party blacklist triple entry.
1000 // Create an entry string array.
1001 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1003 // Add the entry to the blacklist.
1004 thirdPartyBlacklist.add(tripleEntry)
1006 //Log.i("Blocklists", headers.get(1)[0] + " third-party blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
1007 // originalBlocklistEntry);
1009 } else { // Process a third-party blacklist double entry.
1010 // Create an entry string array.
1011 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1013 // Add the entry to the blacklist.
1014 thirdPartyBlacklist.add(doubleEntry)
1016 //Log.i("Blocklists", headers.get(1)[0] + " third-party blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1019 } else { // Process a third party blacklist single entry.
1021 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1023 // Create an entry string array.
1024 val singleEntry = arrayOf(entry, originalBlocklistEntry)
1026 // Add the entry to the blacklist.
1027 thirdPartyBlacklist.add(singleEntry)
1029 //Log.i("Blocklists", headers.get(1)[0] + " third party blacklist added: " + entry + " - " + originalBlocklistEntry);
1031 } else if (blocklistEntry.substring(blocklistEntry.indexOf("$")).contains("domain=")) { // Domain entries.
1032 if (blocklistEntry.contains("~")) { // Domain whitelist entries.
1033 // Separate the filters.
1034 var entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1035 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
1036 var domains = filters.substring(filters.indexOf("domain=") + 7)
1038 // Strip any final `*` from the entry. They are redundant.
1039 if (entry.endsWith("*")) {
1040 entry = entry.substring(0, entry.length - 1)
1043 // Process each domain.
1045 // Create a string to keep track of the current domain.
1048 if (domains.contains("|")) { // There is more than one domain in the list.
1049 // Get the first domain from the list.
1050 domain = domains.substring(0, domains.indexOf("|"))
1052 // Remove the first domain from the list.
1053 domains = domains.substring(domains.indexOf("|") + 1)
1054 } else { // There is only one domain in the list.
1058 // Strip the initial `~`.
1059 domain = domain.substring(1)
1060 if (entry.contains("*")) { // There are two or more entries.
1061 // Get the index of the wildcard.
1062 val wildcardIndex = entry.indexOf("*")
1064 // Split the entry into components.
1065 val firstEntry = entry.substring(0, wildcardIndex)
1066 val secondEntry = entry.substring(wildcardIndex + 1)
1068 if (secondEntry.contains("*")) { // Process a domain whitelist triple entry.
1069 // Get the index of the wildcard.
1070 val secondWildcardIndex = secondEntry.indexOf("*")
1072 // Split the entry into components.
1073 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1074 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1076 // Create an entry string array.
1077 val domainTripleEntry = arrayOf(domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1079 // Add the entry to the whitelist.
1080 domainWhitelist.add(domainTripleEntry)
1082 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1083 // " - " + originalBlocklistEntry);
1084 } else { // Process a domain whitelist double entry.
1085 // Create an entry string array.
1086 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
1088 // Add the entry to the whitelist.
1089 domainWhitelist.add(domainDoubleEntry)
1091 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1093 } else { // Process a domain whitelist single entry.
1094 // Create an entry string array.
1095 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
1097 // Add the entry to the whitelist.
1098 domainWhitelist.add(domainEntry)
1100 //Log.i("Blocklists", headers.get(1)[0] + " domain whitelist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1102 } while (domains.contains("|"))
1103 } else { // Domain blacklist entries.
1104 // Separate the filters.
1105 val entry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1106 val filters = blocklistEntry.substring(blocklistEntry.indexOf("$") + 1)
1107 var domains = filters.substring(filters.indexOf("domain=") + 7)
1109 // Only process the item if the entry is not null. For example, some lines begin with `$websocket`, which create a null entry.
1111 // Process each domain.
1113 // Create a string to keep track of the current domain.
1116 if (domains.contains("|")) { // There is more than one domain in the list.
1117 // Get the first domain from the list.
1118 domain = domains.substring(0, domains.indexOf("|"))
1120 // Remove the first domain from the list.
1121 domains = domains.substring(domains.indexOf("|") + 1)
1122 } else { // There is only one domain in the list.
1126 if (entry.startsWith("|")) { // Domain initial blacklist entries.
1127 // Remove the initial `|`;
1128 val entryBase = entry.substring(1)
1130 if (entryBase == "http://" || entryBase == "https://") {
1131 // Do nothing. These entries will entirely block the website.
1132 // Often the original entry blocks `$script` but Privacy Browser does not currently differentiate between scripts and other entries.
1134 //Log.i("Blocklists", headers.get(1)[0] + " not added: " + originalBlocklistEntry);
1135 } else { // Process a domain initial blacklist entry
1136 // Create an entry string array.
1137 val domainEntry = arrayOf(domain, entryBase, originalBlocklistEntry)
1139 // Add the entry to the blacklist.
1140 domainInitialBlacklist.add(domainEntry)
1142 //Log.i("Blocklists", headers.get(1)[0] + " domain initial blacklist added: " + domain + " , " + entryBase + " - " + originalBlocklistEntry);
1144 } else if (entry.endsWith("|")) { // Domain final blacklist entries.
1145 // Remove the final `|`.
1146 val entryBase = entry.substring(0, entry.length - 1)
1148 if (entryBase.contains("*")) { // Process a domain final blacklist double entry.
1149 // Get the index of the wildcard.
1150 val wildcardIndex = entry.indexOf("*")
1152 // Split the entry into components.
1153 val firstEntry = entryBase.substring(0, wildcardIndex)
1154 val secondEntry = entryBase.substring(wildcardIndex + 1)
1156 // Create an entry string array.
1157 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
1159 // Add the entry to the blacklist.
1160 domainFinalBlacklist.add(domainDoubleEntry)
1162 //Log.i("Blocklists", headers.get(1)[0] + " domain final blacklist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
1163 // originalBlocklistEntry);
1164 } else { // Process a domain final blacklist single entry.
1165 // Create an entry string array.
1166 val domainEntry = arrayOf(domain, entryBase, originalBlocklistEntry)
1168 // Add the entry to the blacklist.
1169 domainFinalBlacklist.add(domainEntry)
1171 //Log.i("Blocklists", headers.get(1)[0] + " domain final blacklist added: " + domain + " , " + entryBase + " - " + originalBlocklistEntry);
1173 } else if (entry.contains("\\")) { // Process a domain regular expression blacklist entry.
1174 // Create an entry string array.
1175 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
1177 // Add the entry to the blacklist.
1178 domainRegularExpressionBlacklist.add(domainEntry)
1180 //Log.i("Blocklists", headers.get(1)[0] + " domain regular expression blacklist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1181 } else if (entry.contains("*")) { // There are two or more entries.
1182 // Get the index of the wildcard.
1183 val wildcardIndex = entry.indexOf("*")
1185 // Split the entry into components.
1186 val firstEntry = entry.substring(0, wildcardIndex)
1187 val secondEntry = entry.substring(wildcardIndex + 1)
1189 if (secondEntry.contains("*")) { // Process a domain blacklist triple entry.
1190 // Get the index of the wildcard.
1191 val secondWildcardIndex = secondEntry.indexOf("*")
1193 // Split the entry into components.
1194 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1195 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1197 // Create an entry string array.
1198 val domainTripleEntry = arrayOf(domain, firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1200 // Add the entry to the blacklist.
1201 domainBlacklist.add(domainTripleEntry)
1203 //Log.i("Blocklists", headers.get(1)[0] + " domain blacklist added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1204 // " - " + originalBlocklistEntry);
1205 } else { // Process a domain blacklist double entry.
1206 // Create an entry string array.
1207 val domainDoubleEntry = arrayOf(domain, firstEntry, secondEntry, originalBlocklistEntry)
1209 // Add the entry to the blacklist.
1210 domainBlacklist.add(domainDoubleEntry)
1212 //Log.i("Blocklists", headers.get(1)[0] + " domain blacklist added: " + domain + " , " + firstEntry + " , " + secondEntry + " - " +
1213 // originalBlocklistEntry);
1215 } else { // Process a domain blacklist single entry.
1216 // Create an entry string array.
1217 val domainEntry = arrayOf(domain, entry, originalBlocklistEntry)
1219 // Add the entry to the blacklist.
1220 domainBlacklist.add(domainEntry)
1222 //Log.i("Blocklists", headers.get(1)[0] + " domain blacklist added: " + domain + " , " + entry + " - " + originalBlocklistEntry);
1224 } while (domains.contains("|"))
1227 } else if (blocklistEntry.contains("~")) { // Whitelist entries. Privacy Browser does not differentiate against these filter options, so they are just generally whitelisted.
1228 // Remove the filter options.
1229 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1231 // Strip any trailing `*`.
1232 if (blocklistEntry.endsWith("*")) {
1233 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length - 1)
1236 if (blocklistEntry.contains("*")) { // Process a whitelist double entry.
1237 // Get the index of the wildcard.
1238 val wildcardIndex = blocklistEntry.indexOf("*")
1240 // Split the entry into components.
1241 val firstEntry = blocklistEntry.substring(0, wildcardIndex)
1242 val secondEntry = blocklistEntry.substring(wildcardIndex + 1)
1244 // Create an entry string array.
1245 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1247 // Add the entry to the whitelist.
1248 mainWhitelist.add(doubleEntry)
1250 //Log.i("Blocklists", headers.get(1)[0] + " main whitelist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1251 } else { // Process a whitelist single entry.
1252 // Create an entry string array.
1253 val singleEntry = arrayOf(blocklistEntry, originalBlocklistEntry)
1255 // Add the entry to the whitelist.
1256 mainWhitelist.add(singleEntry)
1258 //Log.i("Blocklists", headers.get(1)[0] + " main whitelist added: " + blocklistEntry + " - + " + originalBlocklistEntry);
1260 } else if (blocklistEntry.contains("\\")) { // Process a regular expression blacklist entry.
1261 // Remove the filter options.
1262 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1264 // Create an entry string array.
1265 val singleEntry = arrayOf(blocklistEntry, originalBlocklistEntry)
1267 // Add the entry to the blacklist.
1268 regularExpressionBlacklist.add(singleEntry)
1270 //Log.i("Blocklists", headers.get(1)[0] + " regular expression blacklist added: " + blocklistEntry + " - " + originalBlocklistEntry);
1271 } else { // Blacklist entries.
1272 // Remove the filter options.
1273 if (!blocklistEntry.contains("\$file")) { // EasyPrivacy contains an entry with `$file` that does not have filter options.
1274 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.indexOf("$"))
1277 // Strip any trailing `*`. These are redundant.
1278 if (blocklistEntry.endsWith("*")) {
1279 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length - 1)
1282 if (blocklistEntry.startsWith("|")) { // Initial blacklist entries.
1283 // Strip the initial `|`.
1284 val entry = blocklistEntry.substring(1)
1286 if (entry.contains("*")) { // Process an initial blacklist double entry.
1287 // Get the index of the wildcard.
1288 val wildcardIndex = entry.indexOf("*")
1290 // Split the entry into components.
1291 val firstEntry = entry.substring(0, wildcardIndex)
1292 val secondEntry = entry.substring(wildcardIndex + 1)
1294 // Create an entry string array.
1295 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1297 // Add the entry to the blacklist.
1298 initialBlacklist.add(doubleEntry)
1300 //Log.i("Blocklists", headers.get(1)[0] + " initial blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1301 } else { // Process an initial blacklist single entry.
1302 // Create an entry string array.
1303 val singleEntry = arrayOf(entry, originalBlocklistEntry)
1305 // Add the entry to the blacklist.
1306 initialBlacklist.add(singleEntry)
1308 //Log.i("Blocklists", headers.get(1)[0] + " initial blacklist added: " + entry + " - " + originalBlocklistEntry);
1310 } else if (blocklistEntry.endsWith("|")) { // Final blacklist entries.
1311 // Ignore entries with `object` filters. They can block entire websites and don't have any meaning in the context of Privacy Browser.
1312 if (!originalBlocklistEntry.contains("\$object")) {
1313 // Strip the final `|`.
1314 val entry = blocklistEntry.substring(0, blocklistEntry.length - 1)
1316 if (entry.contains("*")) { // There are two or more entries.
1317 // Get the index of the wildcard.
1318 val wildcardIndex = entry.indexOf("*")
1320 // Split the entry into components.
1321 val firstEntry = entry.substring(0, wildcardIndex)
1322 val secondEntry = entry.substring(wildcardIndex + 1)
1324 if (secondEntry.contains("*")) { // Process a final blacklist triple entry.
1325 // Get the index of the wildcard.
1326 val secondWildcardIndex = secondEntry.indexOf("*")
1328 // Split the entry into components.
1329 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1330 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1332 // Create an entry string array.
1333 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1335 // Add the entry to the blacklist.
1336 finalBlacklist.add(tripleEntry)
1338 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " +
1339 // originalBlocklistEntry);
1340 } else { // Process a final blacklist double entry.
1341 // Create an entry string array.
1342 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1344 // Add the entry to the blacklist.
1345 finalBlacklist.add(doubleEntry)
1347 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1349 } else { // Process a final blacklist single entry.
1350 // Create an entry sting array.
1351 val singleEntry = arrayOf(entry, originalBlocklistEntry)
1353 // Add the entry to the blacklist.
1354 finalBlacklist.add(singleEntry)
1356 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + entry + " - " + originalBlocklistEntry);
1359 } else if (blocklistEntry.contains("*")) { // There are two or more entries.
1360 // Get the index of the wildcard.
1361 val wildcardIndex = blocklistEntry.indexOf("*")
1363 // Split the entry into components.
1364 val firstEntry = blocklistEntry.substring(0, wildcardIndex)
1365 val secondEntry = blocklistEntry.substring(wildcardIndex + 1)
1367 if (secondEntry.contains("*")) { // Process a main blacklist triple entry.
1368 // Get the index of the wildcard.
1369 val secondWildcardIndex = secondEntry.indexOf("*")
1371 // Split the entry into components.
1372 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1373 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1375 // Create an entry string array.
1376 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1378 // Add the entry to the blacklist.
1379 mainBlacklist.add(tripleEntry)
1381 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
1382 } else { // Process a main blacklist double entry.
1383 // Create an entry string array.
1384 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1386 // Add the entry to the blacklist.
1387 mainBlacklist.add(doubleEntry)
1389 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1391 } else { // Process a main blacklist single entry.
1392 // Create an entry string array.
1393 val singleEntry = arrayOf(blocklistEntry, originalBlocklistEntry)
1395 // Add the entry to the blacklist.
1396 mainBlacklist.add(singleEntry)
1398 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + blocklistEntry + " - " + originalBlocklistEntry);
1401 } else { // Main blacklist entries
1402 // Strip out any initial `||`. These will be treated like any other entry.
1403 if (blocklistEntry.startsWith("||")) {
1404 blocklistEntry = blocklistEntry.substring(2)
1407 // Strip out any initial `*`.
1408 if (blocklistEntry.startsWith("*")) {
1409 blocklistEntry = blocklistEntry.substring(1)
1412 // Strip out any trailing `*`.
1413 if (blocklistEntry.endsWith("*")) {
1414 blocklistEntry = blocklistEntry.substring(0, blocklistEntry.length - 1)
1417 if (blocklistEntry.startsWith("|")) { // Initial blacklist entries.
1418 // Strip the initial `|`.
1419 val entry = blocklistEntry.substring(1)
1421 if (entry.contains("*")) { // Process an initial blacklist double entry.
1422 // Get the index of the wildcard.
1423 val wildcardIndex = entry.indexOf("*")
1425 // Split the entry into components.
1426 val firstEntry = entry.substring(0, wildcardIndex)
1427 val secondEntry = entry.substring(wildcardIndex + 1)
1429 // Create an entry string array.
1430 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1432 // Add the entry to the blacklist.
1433 initialBlacklist.add(doubleEntry)
1435 //Log.i("Blocklists", headers.get(1)[0] + " initial blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1436 } else { // Process an initial blacklist single entry.
1437 // Create an entry string array.
1438 val singleEntry = arrayOf(entry, originalBlocklistEntry)
1440 // Add the entry to the blacklist.
1441 initialBlacklist.add(singleEntry)
1443 //Log.i("Blocklists", headers.get(1)[0] + " initial blacklist added: " + entry + " - " + originalBlocklistEntry);
1445 } else if (blocklistEntry.endsWith("|")) { // Final blacklist entries.
1446 // Strip the final `|`.
1447 val entry = blocklistEntry.substring(0, blocklistEntry.length - 1)
1449 if (entry.contains("*")) { // There are two or more entries.
1450 // Get the index of the wildcard.
1451 val wildcardIndex = entry.indexOf("*")
1453 // Split the entry into components.
1454 val firstEntry = entry.substring(0, wildcardIndex)
1455 val secondEntry = entry.substring(wildcardIndex + 1)
1457 if (secondEntry.contains("*")) { // Process a final blacklist triple entry.
1458 // Get the index of the wildcard.
1459 val secondWildcardIndex = secondEntry.indexOf("*")
1461 // Split the entry into components.
1462 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1463 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1465 // Create an entry string array.
1466 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1468 // Add the entry to the blacklist.
1469 finalBlacklist.add(tripleEntry)
1471 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
1472 } else { // Process a final blacklist double entry.
1473 // Create an entry string array.
1474 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1476 // Add the entry to the blacklist.
1477 finalBlacklist.add(doubleEntry)
1479 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1481 } else { // Process a final blacklist single entry.
1482 // Create an entry string array.
1483 val singleEntry = arrayOf(entry, originalBlocklistEntry)
1485 // Add the entry to the blacklist.
1486 finalBlacklist.add(singleEntry)
1488 //Log.i("Blocklists", headers.get(1)[0] + " final blacklist added: " + entry + " - " + originalBlocklistEntry);
1490 } else { // Main blacklist entries.
1491 if (blocklistEntry.contains("*")) { // There are two or more entries.
1492 // Get the index of the wildcard.
1493 val wildcardIndex = blocklistEntry.indexOf("*")
1495 // Split the entry into components.
1496 val firstEntry = blocklistEntry.substring(0, wildcardIndex)
1497 val secondEntry = blocklistEntry.substring(wildcardIndex + 1)
1499 if (secondEntry.contains("*")) { // There are three or more entries.
1500 // Get the index of the wildcard.
1501 val secondWildcardIndex = secondEntry.indexOf("*")
1503 // Split the entry into components.
1504 val realSecondEntry = secondEntry.substring(0, secondWildcardIndex)
1505 val thirdEntry = secondEntry.substring(secondWildcardIndex + 1)
1507 if (thirdEntry.contains("*")) { // There are four or more entries.
1508 // Get the index of the wildcard.
1509 val thirdWildcardIndex = thirdEntry.indexOf("*")
1511 // Split the entry into components.
1512 val realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex)
1513 val fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1)
1515 if (fourthEntry.contains("*")) { // Process a main blacklist quintuple entry.
1516 // Get the index of the wildcard.
1517 val fourthWildcardIndex = fourthEntry.indexOf("*")
1519 // Split the entry into components.
1520 val realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex)
1521 val fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1)
1523 // Create an entry string array.
1524 val quintupleEntry = arrayOf(firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlocklistEntry)
1526 // Add the entry to the blacklist.
1527 mainBlacklist.add(quintupleEntry)
1529 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " + realFourthEntry + " , " +
1530 // fifthEntry + " - " + originalBlocklistEntry);
1531 } else { // Process a main blacklist quadruple entry.
1532 // Create an entry string array.
1533 val quadrupleEntry = arrayOf(firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlocklistEntry)
1535 // Add the entry to the blacklist.
1536 mainBlacklist.add(quadrupleEntry)
1538 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " + fourthEntry + " - " +
1539 // originalBlocklistEntry);
1541 } else { // Process a main blacklist triple entry.
1542 // Create an entry string array.
1543 val tripleEntry = arrayOf(firstEntry, realSecondEntry, thirdEntry, originalBlocklistEntry)
1545 // Add the entry to the blacklist.
1546 mainBlacklist.add(tripleEntry)
1548 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + " - " + originalBlocklistEntry);
1550 } else { // Process a main blacklist double entry.
1551 // Create an entry string array.
1552 val doubleEntry = arrayOf(firstEntry, secondEntry, originalBlocklistEntry)
1554 // Add the entry to the blacklist.
1555 mainBlacklist.add(doubleEntry)
1557 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + firstEntry + " , " + secondEntry + " - " + originalBlocklistEntry);
1559 } else { // Process a main blacklist single entry.
1560 // Create an entry string array.
1561 val singleEntry = arrayOf(blocklistEntry, originalBlocklistEntry)
1563 // Add the entry to the blacklist.
1564 mainBlacklist.add(singleEntry)
1566 //Log.i("Blocklists", headers.get(1)[0] + " main blacklist added: " + blocklistEntry + " - " + originalBlocklistEntry);
1571 // Close `bufferedReader`.
1572 bufferedReader.close()
1573 } catch (e: IOException) {
1574 // The asset exists, so the `IOException` will never be thrown.
1577 // Initialize the combined list.
1578 val combinedLists = ArrayList<List<Array<String>>>()
1580 // Add the headers (0).
1581 combinedLists.add(headers) // 0.
1583 // Add the whitelists (1-8).
1584 combinedLists.add(mainWhitelist) // 1.
1585 combinedLists.add(finalWhitelist) // 2.
1586 combinedLists.add(domainWhitelist) // 3.
1587 combinedLists.add(domainInitialWhitelist) // 4.
1588 combinedLists.add(domainFinalWhitelist) // 5.
1589 combinedLists.add(thirdPartyWhitelist) // 6.
1590 combinedLists.add(thirdPartyDomainWhitelist) // 7.
1591 combinedLists.add(thirdPartyDomainInitialWhitelist) // 8.
1593 // Add the blacklists (9-22).
1594 combinedLists.add(mainBlacklist) // 9.
1595 combinedLists.add(initialBlacklist) // 10.
1596 combinedLists.add(finalBlacklist) // 11.
1597 combinedLists.add(domainBlacklist) // 12.
1598 combinedLists.add(domainInitialBlacklist) // 13.
1599 combinedLists.add(domainFinalBlacklist) // 14.
1600 combinedLists.add(domainRegularExpressionBlacklist) // 15.
1601 combinedLists.add(thirdPartyBlacklist) // 16.
1602 combinedLists.add(thirdPartyInitialBlacklist) // 17.
1603 combinedLists.add(thirdPartyDomainBlacklist) // 18.
1604 combinedLists.add(thirdPartyDomainInitialBlacklist) // 19.
1605 combinedLists.add(thirdPartyRegularExpressionBlacklist) // 20.
1606 combinedLists.add(thirdPartyDomainRegularExpressionBlacklist) // 21.
1607 combinedLists.add(regularExpressionBlacklist) // 22.
1609 // Return the combined lists.
1610 return combinedLists
1613 fun checkBlocklist(currentDomain: String?, resourceUrl: String, isThirdPartyRequest: Boolean, blocklist: ArrayList<List<Array<String>>>): Array<String> {
1614 // Get the blocklist name.
1615 val blocklistName = blocklist[0][1][0]
1617 // Process the whitelists.
1619 for (whitelistEntry in blocklist[MAIN_WHITELIST.toInt()]) {
1620 when (whitelistEntry.size) {
1621 // There is one entry.
1622 2 -> if (resourceUrl.contains(whitelistEntry[0])) {
1623 // Return a whitelist match request allowed.
1624 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, whitelistEntry[0], whitelistEntry[1])
1627 // There are two entries.
1628 3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1629 // Return a whitelist match request allowed.
1630 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2]
1634 // There are three entries.
1635 4 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1636 // Return a whitelist match request allowed.
1637 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1643 for (whitelistEntry in blocklist[FINAL_WHITELIST.toInt()]) {
1644 when (whitelistEntry.size) {
1645 // There is one entry.
1646 2 -> if (resourceUrl.contains(whitelistEntry[0])) {
1647 // Return a whitelist match request allowed.
1648 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, FINAL_WHITELIST, whitelistEntry[0], whitelistEntry[1])
1651 // There are two entries.
1652 3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1653 // Return a whitelist match request allowed.
1654 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
1659 // Only check the domain lists if the current domain is not null (like `about:blank`).
1660 if (currentDomain != null) {
1661 // Domain whitelist.
1662 for (whitelistEntry in blocklist[DOMAIN_WHITELIST.toInt()]) {
1663 when (whitelistEntry.size) {
1664 // There is one entry.
1665 3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1666 // Return a whitelist match request allowed.
1667 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
1670 // There are two entries.
1671 4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1672 // Return a whitelist match request allowed.
1673 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1676 // There are three entries.
1677 5 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
1678 // Return a whitelist match request allowed.
1679 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}", whitelistEntry[4])
1682 // There are four entries.
1683 6 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
1684 resourceUrl.contains(whitelistEntry[4])) {
1685 // Return a whitelist match request allowed.
1686 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST,
1687 "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}\n${whitelistEntry[4]}", whitelistEntry[5])
1692 // Domain initial whitelist.
1693 for (whitelistEntry in blocklist[DOMAIN_INITIAL_WHITELIST.toInt()]) {
1694 when (whitelistEntry.size) {
1695 // There is one entry.
1696 3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
1697 // Return a whitelist match request allowed.
1698 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}".trimIndent(), whitelistEntry[2])
1701 // There are two entries.
1702 4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1703 // Return a whitelist match request allowed.
1704 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1707 // There are three entries.
1708 5 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.startsWith(whitelistEntry[3])) {
1709 // Return a whitelist match request allowed.
1710 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}",
1716 // Domain final whitelist.
1717 for (whitelistEntry in blocklist[DOMAIN_FINAL_WHITELIST.toInt()]) {
1718 when (whitelistEntry.size) {
1719 // There is one entry.
1720 3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.endsWith(whitelistEntry[1])) {
1721 // Return a whitelist match request allowed.
1722 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
1725 // There are two entries.
1726 4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.endsWith(whitelistEntry[2])) {
1727 // Return a whitelist match request allowed.
1728 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1734 // Only check the third-party whitelists if this is a third-party request.
1735 if (isThirdPartyRequest) {
1736 // Third-party whitelist.
1737 for (whitelistEntry in blocklist[THIRD_PARTY_WHITELIST.toInt()]) {
1738 when (whitelistEntry.size) {
1739 // There is one entry.
1740 2 -> if (resourceUrl.contains(whitelistEntry[0])) {
1741 // Return a whitelist match request allowed.
1742 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, whitelistEntry[0], whitelistEntry[1])
1745 // There are two entries.
1746 3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1747 // Return a whitelist match request allowed.
1748 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
1751 // There are three entries.
1752 4 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1753 // Return a whitelist match request allowed.
1754 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1757 // There are four entries.
1758 5 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
1759 // Return a whitelist match request allowed.
1760 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}",
1764 // There are five entries.
1765 6 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
1766 resourceUrl.contains(whitelistEntry[4])) {
1767 // Return a whitelist match request allowed.
1768 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST,
1769 "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}\n${whitelistEntry[4]}", whitelistEntry[5])
1774 // Third-party domain whitelist.
1775 for (whitelistEntry in blocklist[THIRD_PARTY_DOMAIN_WHITELIST.toInt()]) {
1776 when (whitelistEntry.size) {
1777 // There is one entry.
1778 3 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
1779 // Return a whitelist match request allowed.
1780 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n", whitelistEntry[2])
1783 // There are two entries.
1784 4 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1785 // Return a whitelist match request allowed.
1786 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1791 // Third-party domain initial whitelist.
1792 for (whitelistEntry in blocklist[THIRD_PARTY_DOMAIN_INITIAL_WHITELIST.toInt()]) {
1793 when (whitelistEntry.size) {
1794 // There is one entry.
1795 3 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
1796 // Return a whitelist match request allowed.
1797 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n", whitelistEntry[2])
1800 // There are two entries.
1801 4 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
1802 // Return a whitelist match request allowed.
1803 return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
1809 // Process the blacklists.
1811 for (blacklistEntry in blocklist[MAIN_BLACKLIST.toInt()]) {
1812 when (blacklistEntry.size) {
1813 // There is one entry.
1814 2 -> if (resourceUrl.contains(blacklistEntry[0])) {
1815 // Return a blacklist match request blocked.
1816 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
1819 // There are two entries.
1820 3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1821 // Return a blacklist match request blocked.
1822 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1825 // There are three entries.
1826 4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
1827 // Return a blacklist match request blocked.
1828 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
1831 // There are four entries.
1832 5 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
1833 // Return a blacklist match request blocked.
1834 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
1837 // There are five entries.
1838 6 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3]) &&
1839 resourceUrl.contains(blacklistEntry[4])) {
1840 // Return a blacklist match request blocked.
1841 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}\n${blacklistEntry[4]}",
1847 // Initial blacklist.
1848 for (blacklistEntry in blocklist[INITIAL_BLACKLIST.toInt()]) {
1849 when (blacklistEntry.size) {
1850 // There is one entry.
1851 2 -> if (resourceUrl.startsWith(blacklistEntry[0])) {
1852 // Return a blacklist match request blocked.
1853 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
1856 // There are two entries
1857 3 -> if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1858 // Return a blacklist match request blocked.
1859 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1865 for (blacklistEntry in blocklist[FINAL_BLACKLIST.toInt()]) {
1866 when (blacklistEntry.size) {
1867 // There is one entry.
1868 2 -> if (resourceUrl.endsWith(blacklistEntry[0])) {
1869 // Return a blacklist match request blocked.
1870 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
1873 // There are two entries.
1874 3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
1875 // Return a blacklist match request blocked.
1876 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1879 // There are three entries.
1880 4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
1881 // Return a blacklist match request blocked.
1882 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
1887 // Only check the domain lists if the current domain is not null (like `about:blank`).
1888 if (currentDomain != null) {
1889 // Domain blacklist.
1890 for (blacklistEntry in blocklist[DOMAIN_BLACKLIST.toInt()]) {
1891 when (blacklistEntry.size) {
1892 // There is one entry.
1893 3 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1894 // Return a blacklist match request blocked.
1895 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1898 // There are two entries.
1899 4 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
1900 // Return a blacklist match request blocked.
1901 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
1904 // There are three entries.
1905 5 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
1906 // Return a blacklist match request blocked.
1907 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
1912 // Domain initial blacklist.
1913 for (blacklistEntry in blocklist[DOMAIN_INITIAL_BLACKLIST.toInt()]) {
1914 // Store the entry in the resource request log.
1915 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
1916 // Return a blacklist match request blocked.
1917 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1921 // Domain final blacklist.
1922 for (blacklistEntry in blocklist[DOMAIN_FINAL_BLACKLIST.toInt()]) {
1923 when (blacklistEntry.size) {
1924 // There is one entry.
1925 3 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
1926 // Return a blacklist match request blocked.
1927 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1930 // There are two entries.
1931 4 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
1932 // Return a blacklist match request blocked.
1933 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
1938 // Domain regular expression blacklist.
1939 for (blacklistEntry in blocklist[DOMAIN_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
1940 if (currentDomain.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
1941 // Return a blacklist match request blocked.
1942 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_REGULAR_EXPRESSION_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1947 // Only check the third-party blacklists if this is a third-party request.
1948 if (isThirdPartyRequest) {
1949 // Third-party blacklist.
1950 for (blacklistEntry in blocklist[THIRD_PARTY_BLACKLIST.toInt()]) {
1951 when (blacklistEntry.size) {
1952 // There is one entry.
1953 2 -> if (resourceUrl.contains(blacklistEntry[0])) {
1954 // Return a blacklist match request blocked.
1955 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
1958 // There are two entries.
1959 3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1960 // Return a blacklist match request blocked.
1962 REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n", blacklistEntry[2])
1965 // There are three entries.
1966 4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
1967 // Return a blacklist match request blocked.
1968 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
1971 // There are four entries.
1972 5 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
1973 // Return a blacklist match request blocked.
1974 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}",
1980 // Third-party initial blacklist.
1981 for (blacklistEntry in blocklist[THIRD_PARTY_INITIAL_BLACKLIST.toInt()]) {
1982 when (blacklistEntry.size) {
1983 // There is one entry.
1984 2 -> if (resourceUrl.startsWith(blacklistEntry[0])) {
1985 // Return a blacklist match request blocked.
1986 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
1989 // There are two entries.
1990 3 -> if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
1991 // Return a blacklist match request blocked.
1992 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
1997 // Third-party domain blacklist.
1998 for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_BLACKLIST.toInt()]) {
1999 when (blacklistEntry.size) {
2000 // There is one entry.
2001 3 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
2002 // Return a blacklist match request blocked.
2003 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
2006 // There are two entries.
2007 4 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
2008 // Return a blacklist match request blocked.
2009 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
2014 // Third-party domain initial blacklist.
2015 for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST.toInt()]) {
2016 when (blacklistEntry.size) {
2017 // There is one entry.
2018 3 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
2019 // Return a blacklist match request blocked.
2020 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n", blacklistEntry[2])
2023 // There are two entries.
2024 4 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
2025 // Return a blacklist match request blocked.
2026 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
2029 // There are three entries.
2030 5 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
2031 // Return a blacklist match request blocked.
2032 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST,
2033 "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
2038 // Third-party regular expression blacklist.
2039 for (blacklistEntry in blocklist[THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
2040 if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
2041 // Return a blacklist match request blocked.
2042 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
2046 // Third-party domain regular expression blacklist.
2047 for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
2048 if (currentDomain!!.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
2049 // Return a blacklist match request blocked.
2050 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
2055 // Regular expression blacklist.
2056 for (blacklistEntry in blocklist[REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
2057 if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
2058 // Return a blacklist match request blocked.
2059 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
2063 // Return a no match request default.
2064 return arrayOf(REQUEST_DEFAULT)