2 * Copyright 2018-2019,2021-2023 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 java.util.ArrayList
23 import java.util.regex.Pattern
25 // Define the request disposition options.
26 const val REQUEST_DEFAULT = "0"
27 const val REQUEST_ALLOWED = "1"
28 const val REQUEST_THIRD_PARTY = "2"
29 const val REQUEST_BLOCKED = "3"
31 class CheckFilterListHelper {
32 fun checkFilterList(currentDomain: String?, resourceUrl: String, isThirdPartyRequest: Boolean, filterList: ArrayList<List<Array<String>>>): Array<String> {
33 // Get the filter list name.
34 val filterListName = filterList[0][1][0]
36 // Process the allow lists.
38 for (allowListEntry in filterList[MAIN_ALLOWLIST.toInt()]) {
39 when (allowListEntry.size) {
40 // There is one entry.
41 2 -> if (resourceUrl.contains(allowListEntry[0])) {
43 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, allowListEntry[0], allowListEntry[1])
46 // There are two entries.
47 3 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
49 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
52 // There are three entries.
53 4 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
55 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
61 for (allowListEntry in filterList[FINAL_ALLOWLIST.toInt()]) {
62 when (allowListEntry.size) {
63 // There is one entry.
64 2 -> if (resourceUrl.contains(allowListEntry[0])) {
66 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, FINAL_ALLOWLIST, allowListEntry[0], allowListEntry[1])
69 // There are two entries.
70 3 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
72 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, FINAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
77 // Only check the domain lists if the current domain is not null (like `about:blank`).
78 if (currentDomain != null) {
80 for (allowListEntry in filterList[DOMAIN_ALLOWLIST.toInt()]) {
81 when (allowListEntry.size) {
82 // There is one entry.
83 3 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
85 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
88 // There are two entries.
89 4 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
91 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
94 // There are three entries.
95 5 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.contains(allowListEntry[3])) {
97 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}",
101 // There are four entries.
102 6 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.contains(allowListEntry[3]) &&
103 resourceUrl.contains(allowListEntry[4])) {
105 // Allow the request.
106 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST,
107 "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}\n${allowListEntry[4]}", allowListEntry[5])
112 // Domain initial allow list.
113 for (allowListEntry in filterList[DOMAIN_INITIAL_ALLOWLIST.toInt()]) {
114 when (allowListEntry.size) {
115 // There is one entry.
116 3 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.startsWith(allowListEntry[1])) {
117 // Allow the request.
118 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_INITIAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
121 // There are two entries.
122 4 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.startsWith(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
123 // Allow the request.
124 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_INITIAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
127 // There are three entries.
128 5 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.startsWith(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.startsWith(allowListEntry[3])) {
129 // Allow the request.
130 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_INITIAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}",
136 // Domain final allow list.
137 for (allowListEntry in filterList[DOMAIN_FINAL_ALLOWLIST.toInt()]) {
138 when (allowListEntry.size) {
139 // There is one entry.
140 3 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.endsWith(allowListEntry[1])) {
141 // Allow the request.
142 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_FINAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
145 // There are two entries.
146 4 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.endsWith(allowListEntry[2])) {
147 // Allow the request.
148 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_FINAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
154 // Only check the third-party allow lists if this is a third-party request.
155 if (isThirdPartyRequest) {
156 // Third-party allow list.
157 for (allowListEntry in filterList[THIRD_PARTY_ALLOWLIST.toInt()]) {
158 when (allowListEntry.size) {
159 // There is one entry.
160 2 -> if (resourceUrl.contains(allowListEntry[0])) {
161 // Allow the request.
162 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_ALLOWLIST, allowListEntry[0], allowListEntry[1])
165 // There are two entries.
166 3 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
167 // Allow the request.
168 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
171 // There are three entries.
172 4 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
173 // Allow the request.
174 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
177 // There are four entries.
178 5 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.contains(allowListEntry[3])) {
179 // Allow the request.
180 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}",
184 // There are five entries.
185 6 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.contains(allowListEntry[3]) &&
186 resourceUrl.contains(allowListEntry[4])) {
188 // Allow the request.
189 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_ALLOWLIST,
190 "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}\n${allowListEntry[4]}", allowListEntry[5])
195 // Third-party domain allow list.
196 for (allowListEntry in filterList[THIRD_PARTY_DOMAIN_ALLOWLIST.toInt()]) {
197 when (allowListEntry.size) {
198 // There is one entry.
199 3 -> if (currentDomain!!.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
200 // Allow the request.
201 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n", allowListEntry[2])
204 // There are two entries.
205 4 -> if (currentDomain!!.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
206 // Allow the request.
207 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
212 // Third-party domain initial allow list.
213 for (allowListEntry in filterList[THIRD_PARTY_DOMAIN_INITIAL_ALLOWLIST.toInt()]) {
214 when (allowListEntry.size) {
215 // There is one entry.
216 3 -> if (currentDomain!!.endsWith(allowListEntry[0]) && resourceUrl.startsWith(allowListEntry[1])) {
217 // Allow the request.
218 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_INITIAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n", allowListEntry[2])
221 // There are two entries.
222 4 -> if (currentDomain!!.endsWith(allowListEntry[0]) && resourceUrl.startsWith(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
223 // Allow the request.
224 return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
230 // Process the block lists.
232 for (blockListEntry in filterList[MAIN_BLOCKLIST.toInt()]) {
233 when (blockListEntry.size) {
234 // There is one entry.
235 2 -> if (resourceUrl.contains(blockListEntry[0])) {
236 // Block the request.
237 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, MAIN_BLOCKLIST, blockListEntry[0], blockListEntry[1])
240 // There are two entries.
241 3 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
242 // Block the request.
243 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, MAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
246 // There are three entries.
247 4 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2])) {
248 // Block the request.
249 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, MAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
252 // There are four entries.
253 5 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2]) && resourceUrl.contains(blockListEntry[3])) {
254 // Block the request.
255 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, MAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}\n${blockListEntry[3]}", blockListEntry[4])
258 // There are five entries.
259 6 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2]) && resourceUrl.contains(blockListEntry[3]) &&
260 resourceUrl.contains(blockListEntry[4])) {
261 // Block the request.
262 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, MAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}\n${blockListEntry[3]}\n${blockListEntry[4]}",
268 // Initial block list.
269 for (blockListEntry in filterList[INITIAL_BLOCKLIST.toInt()]) {
270 when (blockListEntry.size) {
271 // There is one entry.
272 2 -> if (resourceUrl.startsWith(blockListEntry[0])) {
273 // Block the request.
274 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, INITIAL_BLOCKLIST, blockListEntry[0], blockListEntry[1])
277 // There are two entries
278 3 -> if (resourceUrl.startsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
279 // Block the request.
280 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, INITIAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
286 for (blockListEntry in filterList[FINAL_BLOCKLIST.toInt()]) {
287 when (blockListEntry.size) {
288 // There is one entry.
289 2 -> if (resourceUrl.endsWith(blockListEntry[0])) {
290 // Block the request.
291 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, FINAL_BLOCKLIST, blockListEntry[0], blockListEntry[1])
294 // There are two entries.
295 3 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.endsWith(blockListEntry[1])) {
296 // Block the request.
297 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, FINAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
300 // There are three entries.
301 4 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.endsWith(blockListEntry[2])) {
302 // Block the request.
303 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, FINAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
308 // Only check the domain lists if the current domain is not null (like `about:blank`).
309 if (currentDomain != null) {
310 // Domain block list.
311 for (blockListEntry in filterList[DOMAIN_BLOCKLIST.toInt()]) {
312 when (blockListEntry.size) {
313 // There is one entry.
314 3 -> if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
315 // Block the request.
316 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
319 // There are two entries.
320 4 -> if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2])) {
321 // Block the request.
322 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
325 // There are three entries.
326 5 -> if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2]) && resourceUrl.contains(blockListEntry[3])) {
327 // Block the request.
328 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}\n${blockListEntry[3]}",
334 // Domain initial block list.
335 for (blockListEntry in filterList[DOMAIN_INITIAL_BLOCKLIST.toInt()]) {
336 // Store the entry in the resource request log.
337 if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.startsWith(blockListEntry[1])) {
338 // Block the request.
339 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_INITIAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
343 // Domain final block list.
344 for (blockListEntry in filterList[DOMAIN_FINAL_BLOCKLIST.toInt()]) {
345 when (blockListEntry.size) {
346 // There is one entry.
347 3 -> if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.endsWith(blockListEntry[1])) {
348 // Block the request.
349 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_FINAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
352 // There are two entries.
353 4 -> if (currentDomain.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.endsWith(blockListEntry[2])) {
354 // Block the request.
355 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_FINAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
360 // Domain regular expression block list.
361 for (blockListEntry in filterList[DOMAIN_REGULAR_EXPRESSION_BLOCKLIST.toInt()]) {
362 if (currentDomain.endsWith(blockListEntry[0]) && Pattern.matches(blockListEntry[1], resourceUrl)) {
363 // Block the request.
364 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, DOMAIN_REGULAR_EXPRESSION_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
369 // Only check the third-party block lists if this is a third-party request.
370 if (isThirdPartyRequest) {
371 // Third-party block list.
372 for (blockListEntry in filterList[THIRD_PARTY_BLOCKLIST.toInt()]) {
373 when (blockListEntry.size) {
374 // There is one entry.
375 2 -> if (resourceUrl.contains(blockListEntry[0])) {
376 // Block the request.
377 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_BLOCKLIST, blockListEntry[0], blockListEntry[1])
380 // There are two entries.
381 3 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
382 // Block the request.
383 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n", blockListEntry[2])
386 // There are three entries.
387 4 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2])) {
388 // Block the request.
389 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
392 // There are four entries.
393 5 -> if (resourceUrl.contains(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2]) && resourceUrl.contains(blockListEntry[3])) {
394 // Block the request.
395 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}\n${blockListEntry[3]}",
401 // Third-party initial block list.
402 for (blockListEntry in filterList[THIRD_PARTY_INITIAL_BLOCKLIST.toInt()]) {
403 when (blockListEntry.size) {
404 // There is one entry.
405 2 -> if (resourceUrl.startsWith(blockListEntry[0])) {
406 // Block the request.
407 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_INITIAL_BLOCKLIST, blockListEntry[0], blockListEntry[1])
410 // There are two entries.
411 3 -> if (resourceUrl.startsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
412 // Block the request.
413 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_INITIAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
418 // Third-party domain block list.
419 for (blockListEntry in filterList[THIRD_PARTY_DOMAIN_BLOCKLIST.toInt()]) {
420 when (blockListEntry.size) {
421 // There is one entry.
422 3 -> if (currentDomain!!.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1])) {
423 // Block the request.
424 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
427 // There are two entries.
428 4 -> if (currentDomain!!.endsWith(blockListEntry[0]) && resourceUrl.contains(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2])) {
429 // Block the request.
430 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
435 // Third-party domain initial block list.
436 for (blockListEntry in filterList[THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST.toInt()]) {
437 when (blockListEntry.size) {
438 // There is one entry.
439 3 -> if (currentDomain!!.endsWith(blockListEntry[0]) && resourceUrl.startsWith(blockListEntry[1])) {
440 // Block the request.
441 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n", blockListEntry[2])
444 // There are two entries.
445 4 -> if (currentDomain!!.endsWith(blockListEntry[0]) && resourceUrl.startsWith(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2])) {
446 // Block the request.
447 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}", blockListEntry[3])
450 // There are three entries.
451 5 -> if (currentDomain!!.endsWith(blockListEntry[0]) && resourceUrl.startsWith(blockListEntry[1]) && resourceUrl.contains(blockListEntry[2]) && resourceUrl.contains(blockListEntry[3])) {
452 // Block the request.
453 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST,
454 "${blockListEntry[0]}\n${blockListEntry[1]}\n${blockListEntry[2]}\n${blockListEntry[3]}", blockListEntry[4])
459 // Third-party regular expression block list.
460 for (blockListEntry in filterList[THIRD_PARTY_REGULAR_EXPRESSION_BLOCKLIST.toInt()]) {
461 if (Pattern.matches(blockListEntry[0], resourceUrl)) {
462 // Block the request.
463 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_REGULAR_EXPRESSION_BLOCKLIST, blockListEntry[0], blockListEntry[1])
467 // Third-party domain regular expression block list.
468 for (blockListEntry in filterList[THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLOCKLIST.toInt()]) {
469 if (currentDomain!!.endsWith(blockListEntry[0]) && Pattern.matches(blockListEntry[1], resourceUrl)) {
470 // Block the request.
471 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLOCKLIST, "${blockListEntry[0]}\n${blockListEntry[1]}", blockListEntry[2])
476 // Regular expression block list.
477 for (blockListEntry in filterList[REGULAR_EXPRESSION_BLOCKLIST.toInt()]) {
478 if (Pattern.matches(blockListEntry[0], resourceUrl)) {
479 // Block the request.
480 return arrayOf(REQUEST_BLOCKED, resourceUrl, filterListName, REGULAR_EXPRESSION_BLOCKLIST, blockListEntry[0], blockListEntry[1])
484 // Return a default result.
485 return arrayOf(REQUEST_DEFAULT)