]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/CheckFilterListHelper.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / CheckFilterListHelper.kt
1 /*
2  * Copyright 2018-2019,2021-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers
21
22 import java.util.ArrayList
23 import java.util.regex.Pattern
24
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"
30
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]
35
36         // Process the allow lists.
37         // Main allow list.
38         for (allowListEntry in filterList[MAIN_ALLOWLIST.toInt()]) {
39             when (allowListEntry.size) {
40                 // There is one entry.
41                 2 -> if (resourceUrl.contains(allowListEntry[0])) {
42                     // Allow the request.
43                     return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, allowListEntry[0], allowListEntry[1])
44                 }
45
46                 // There are two entries.
47                 3 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
48                     // Allow the request.
49                     return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
50                 }
51
52                 // There are three entries.
53                 4 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
54                     // Allow the request.
55                     return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, MAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
56                 }
57             }
58         }
59
60         // Final allow list.
61         for (allowListEntry in filterList[FINAL_ALLOWLIST.toInt()]) {
62             when (allowListEntry.size) {
63                 // There is one entry.
64                 2 -> if (resourceUrl.contains(allowListEntry[0])) {
65                     // Allow the request.
66                     return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, FINAL_ALLOWLIST, allowListEntry[0], allowListEntry[1])
67                 }
68
69                 // There are two entries.
70                 3 -> if (resourceUrl.contains(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1])) {
71                     // Allow the request.
72                     return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, FINAL_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
73                 }
74             }
75         }
76
77         // Only check the domain lists if the current domain is not null (like `about:blank`).
78         if (currentDomain != null) {
79             // Domain allow list.
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])) {
84                         // Allow the request.
85                         return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}", allowListEntry[2])
86                     }
87
88                     // There are two entries.
89                     4 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2])) {
90                         // Allow the request.
91                         return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}", allowListEntry[3])
92                     }
93
94                     // There are three entries.
95                     5 -> if (currentDomain.endsWith(allowListEntry[0]) && resourceUrl.contains(allowListEntry[1]) && resourceUrl.contains(allowListEntry[2]) && resourceUrl.contains(allowListEntry[3])) {
96                         // Allow the request.
97                         return arrayOf(REQUEST_ALLOWED, resourceUrl, filterListName, DOMAIN_ALLOWLIST, "${allowListEntry[0]}\n${allowListEntry[1]}\n${allowListEntry[2]}\n${allowListEntry[3]}",
98                             allowListEntry[4])
99                     }
100
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])) {
104
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])
108                     }
109                 }
110             }
111
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])
119                     }
120
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])
125                     }
126
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]}",
131                             allowListEntry[4])
132                     }
133                 }
134             }
135
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])
143                     }
144
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])
149                     }
150                 }
151             }
152         }
153
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])
163                     }
164
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])
169                     }
170
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])
175                     }
176
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]}",
181                             allowListEntry[4])
182                     }
183
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])) {
187
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])
191                     }
192                 }
193             }
194
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])
202                     }
203
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])
208                     }
209                 }
210             }
211
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])
219                     }
220
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])
225                     }
226                 }
227             }
228         }
229
230         // Process the block lists.
231         // Main block list.
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])
238                 }
239
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])
244                 }
245
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])
250                 }
251
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])
256                 }
257
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]}",
263                         blockListEntry[5])
264                 }
265             }
266         }
267
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])
275                 }
276
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])
281                 }
282             }
283         }
284
285         // Final block list.
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])
292                 }
293
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])
298                 }
299
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])
304                 }
305             }
306         }
307
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])
317                     }
318
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])
323                     }
324
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]}",
329                             blockListEntry[4])
330                     }
331                 }
332             }
333
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])
340                 }
341             }
342
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])
350                     }
351
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])
356                     }
357                 }
358             }
359
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])
365                 }
366             }
367         }
368
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])
378                     }
379
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])
384                     }
385
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])
390                     }
391
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]}",
396                             blockListEntry[4])
397                     }
398                 }
399             }
400
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])
408                     }
409
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])
414                     }
415                 }
416             }
417
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])
425                     }
426
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])
431                     }
432                 }
433             }
434
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])
442                     }
443
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])
448                     }
449
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])
455                     }
456                 }
457             }
458
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])
464                 }
465             }
466
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])
472                 }
473             }
474         }
475
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])
481             }
482         }
483
484         // Return a default result.
485         return arrayOf(REQUEST_DEFAULT)
486     }
487 }