]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/CheckBlocklistHelper.kt
Split the blocklist helper. https://redmine.stoutner.com/issues/953
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / CheckBlocklistHelper.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 import java.util.ArrayList
22 import java.util.regex.Pattern
23
24 // Define the request disposition options.
25 const val REQUEST_DEFAULT = "0"
26 const val REQUEST_ALLOWED = "1"
27 const val REQUEST_THIRD_PARTY = "2"
28 const val REQUEST_BLOCKED = "3"
29
30 class CheckBlocklistHelper {
31     fun checkBlocklist(currentDomain: String?, resourceUrl: String, isThirdPartyRequest: Boolean, blocklist: ArrayList<List<Array<String>>>): Array<String> {
32         // Get the blocklist name.
33         val blocklistName = blocklist[0][1][0]
34
35         // Process the whitelists.
36         // Main whitelist.
37         for (whitelistEntry in blocklist[MAIN_WHITELIST.toInt()]) {
38             when (whitelistEntry.size) {
39                 // There is one entry.
40                 2 -> if (resourceUrl.contains(whitelistEntry[0])) {
41                     // Return a whitelist match request allowed.
42                     return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, whitelistEntry[0], whitelistEntry[1])
43                 }
44
45                 // There are two entries.
46                 3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
47                     // Return a whitelist match request allowed.
48                     return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2]
49                     )
50                 }
51
52                 // There are three entries.
53                 4 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
54                     // Return a whitelist match request allowed.
55                     return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, MAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
56                 }
57             }
58         }
59
60         // Final whitelist.
61         for (whitelistEntry in blocklist[FINAL_WHITELIST.toInt()]) {
62             when (whitelistEntry.size) {
63                 // There is one entry.
64                 2 -> if (resourceUrl.contains(whitelistEntry[0])) {
65                     // Return a whitelist match request allowed.
66                     return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, FINAL_WHITELIST, whitelistEntry[0], whitelistEntry[1])
67                 }
68
69                 // There are two entries.
70                 3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
71                     // Return a whitelist match request allowed.
72                     return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[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 whitelist.
80             for (whitelistEntry in blocklist[DOMAIN_WHITELIST.toInt()]) {
81                 when (whitelistEntry.size) {
82                     // There is one entry.
83                     3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
84                         // Return a whitelist match request allowed.
85                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
86                     }
87
88                     // There are two entries.
89                     4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
90                         // Return a whitelist match request allowed.
91                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
92                     }
93
94                     // There are three entries.
95                     5 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
96                         // Return a whitelist match request allowed.
97                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}", whitelistEntry[4])
98                     }
99
100                     // There are four entries.
101                     6 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
102                         resourceUrl.contains(whitelistEntry[4])) {
103                         // Return a whitelist match request allowed.
104                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_WHITELIST,
105                             "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}\n${whitelistEntry[4]}", whitelistEntry[5])
106                     }
107                 }
108             }
109
110             // Domain initial whitelist.
111             for (whitelistEntry in blocklist[DOMAIN_INITIAL_WHITELIST.toInt()]) {
112                 when (whitelistEntry.size) {
113                     // There is one entry.
114                     3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
115                         // Return a whitelist match request allowed.
116                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}".trimIndent(), whitelistEntry[2])
117                     }
118
119                     // There are two entries.
120                     4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
121                         // Return a whitelist match request allowed.
122                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
123                     }
124
125                     // There are three entries.
126                     5 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.startsWith(whitelistEntry[3])) {
127                         // Return a whitelist match request allowed.
128                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}",
129                             whitelistEntry[4])
130                     }
131                 }
132             }
133
134             // Domain final whitelist.
135             for (whitelistEntry in blocklist[DOMAIN_FINAL_WHITELIST.toInt()]) {
136                 when (whitelistEntry.size) {
137                     // There is one entry.
138                     3 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.endsWith(whitelistEntry[1])) {
139                         // Return a whitelist match request allowed.
140                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
141                     }
142
143                     // There are two entries.
144                     4 -> if (currentDomain.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.endsWith(whitelistEntry[2])) {
145                         // Return a whitelist match request allowed.
146                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, DOMAIN_FINAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
147                     }
148                 }
149             }
150         }
151
152         // Only check the third-party whitelists if this is a third-party request.
153         if (isThirdPartyRequest) {
154             // Third-party whitelist.
155             for (whitelistEntry in blocklist[THIRD_PARTY_WHITELIST.toInt()]) {
156                 when (whitelistEntry.size) {
157                     // There is one entry.
158                     2 -> if (resourceUrl.contains(whitelistEntry[0])) {
159                         // Return a whitelist match request allowed.
160                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, whitelistEntry[0], whitelistEntry[1])
161                     }
162
163                     // There are two entries.
164                     3 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
165                         // Return a whitelist match request allowed.
166                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}", whitelistEntry[2])
167                     }
168
169                     // There are three entries.
170                     4 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
171                         // Return a whitelist match request allowed.
172                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
173                     }
174
175                     // There are four entries.
176                     5 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3])) {
177                         // Return a whitelist match request allowed.
178                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}",
179                             whitelistEntry[4])
180                     }
181
182                     // There are five entries.
183                     6 -> if (resourceUrl.contains(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2]) && resourceUrl.contains(whitelistEntry[3]) &&
184                         resourceUrl.contains(whitelistEntry[4])) {
185                         // Return a whitelist match request allowed.
186                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_WHITELIST,
187                             "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}\n${whitelistEntry[3]}\n${whitelistEntry[4]}", whitelistEntry[5])
188                     }
189                 }
190             }
191
192             // Third-party domain whitelist.
193             for (whitelistEntry in blocklist[THIRD_PARTY_DOMAIN_WHITELIST.toInt()]) {
194                 when (whitelistEntry.size) {
195                     // There is one entry.
196                     3 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1])) {
197                         // Return a whitelist match request allowed.
198                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n", whitelistEntry[2])
199                     }
200
201                     // There are two entries.
202                     4 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.contains(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
203                         // Return a whitelist match request allowed.
204                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
205                     }
206                 }
207             }
208
209             // Third-party domain initial whitelist.
210             for (whitelistEntry in blocklist[THIRD_PARTY_DOMAIN_INITIAL_WHITELIST.toInt()]) {
211                 when (whitelistEntry.size) {
212                     // There is one entry.
213                     3 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1])) {
214                         // Return a whitelist match request allowed.
215                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n", whitelistEntry[2])
216                     }
217
218                     // There are two entries.
219                     4 -> if (currentDomain!!.endsWith(whitelistEntry[0]) && resourceUrl.startsWith(whitelistEntry[1]) && resourceUrl.contains(whitelistEntry[2])) {
220                         // Return a whitelist match request allowed.
221                         return arrayOf(REQUEST_ALLOWED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_WHITELIST, "${whitelistEntry[0]}\n${whitelistEntry[1]}\n${whitelistEntry[2]}", whitelistEntry[3])
222                     }
223                 }
224             }
225         }
226
227         // Process the blacklists.
228         // Main blacklist.
229         for (blacklistEntry in blocklist[MAIN_BLACKLIST.toInt()]) {
230             when (blacklistEntry.size) {
231                 // There is one entry.
232                 2 -> if (resourceUrl.contains(blacklistEntry[0])) {
233                     // Return a blacklist match request blocked.
234                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
235                 }
236
237                 // There are two entries.
238                 3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
239                     // Return a blacklist match request blocked.
240                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
241                 }
242
243                 // There are three entries.
244                 4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
245                     // Return a blacklist match request blocked.
246                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
247                 }
248
249                 // There are four entries.
250                 5 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
251                     // Return a blacklist match request blocked.
252                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
253                 }
254
255                 // There are five entries.
256                 6 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3]) &&
257                     resourceUrl.contains(blacklistEntry[4])) {
258                     // Return a blacklist match request blocked.
259                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, MAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}\n${blacklistEntry[4]}",
260                         blacklistEntry[5])
261                 }
262             }
263         }
264
265         // Initial blacklist.
266         for (blacklistEntry in blocklist[INITIAL_BLACKLIST.toInt()]) {
267             when (blacklistEntry.size) {
268                 // There is one entry.
269                 2 -> if (resourceUrl.startsWith(blacklistEntry[0])) {
270                     // Return a blacklist match request blocked.
271                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
272                 }
273
274                 // There are two entries
275                 3 -> if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
276                     // Return a blacklist match request blocked.
277                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
278                 }
279             }
280         }
281
282         // Final blacklist.
283         for (blacklistEntry in blocklist[FINAL_BLACKLIST.toInt()]) {
284             when (blacklistEntry.size) {
285                 // There is one entry.
286                 2 -> if (resourceUrl.endsWith(blacklistEntry[0])) {
287                     // Return a blacklist match request blocked.
288                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
289                 }
290
291                 // There are two entries.
292                 3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
293                     // Return a blacklist match request blocked.
294                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
295                 }
296
297                 // There are three entries.
298                 4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
299                     // Return a blacklist match request blocked.
300                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
301                 }
302             }
303         }
304
305         // Only check the domain lists if the current domain is not null (like `about:blank`).
306         if (currentDomain != null) {
307             // Domain blacklist.
308             for (blacklistEntry in blocklist[DOMAIN_BLACKLIST.toInt()]) {
309                 when (blacklistEntry.size) {
310                     // There is one entry.
311                     3 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
312                         // Return a blacklist match request blocked.
313                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
314                     }
315
316                     // There are two entries.
317                     4 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
318                         // Return a blacklist match request blocked.
319                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
320                     }
321
322                     // There are three entries.
323                     5 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
324                         // Return a blacklist match request blocked.
325                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
326                     }
327                 }
328             }
329
330             // Domain initial blacklist.
331             for (blacklistEntry in blocklist[DOMAIN_INITIAL_BLACKLIST.toInt()]) {
332                 // Store the entry in the resource request log.
333                 if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
334                     // Return a blacklist match request blocked.
335                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
336                 }
337             }
338
339             // Domain final blacklist.
340             for (blacklistEntry in blocklist[DOMAIN_FINAL_BLACKLIST.toInt()]) {
341                 when (blacklistEntry.size) {
342                     // There is one entry.
343                     3 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.endsWith(blacklistEntry[1])) {
344                         // Return a blacklist match request blocked.
345                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
346                     }
347
348                     // There are two entries.
349                     4 -> if (currentDomain.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.endsWith(blacklistEntry[2])) {
350                         // Return a blacklist match request blocked.
351                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_FINAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
352                     }
353                 }
354             }
355
356             // Domain regular expression blacklist.
357             for (blacklistEntry in blocklist[DOMAIN_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
358                 if (currentDomain.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
359                     // Return a blacklist match request blocked.
360                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, DOMAIN_REGULAR_EXPRESSION_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
361                 }
362             }
363         }
364
365         // Only check the third-party blacklists if this is a third-party request.
366         if (isThirdPartyRequest) {
367             // Third-party blacklist.
368             for (blacklistEntry in blocklist[THIRD_PARTY_BLACKLIST.toInt()]) {
369                 when (blacklistEntry.size) {
370                     // There is one entry.
371                     2 -> if (resourceUrl.contains(blacklistEntry[0])) {
372                         // Return a blacklist match request blocked.
373                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
374                     }
375
376                     // There are two entries.
377                     3 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
378                         // Return a blacklist match request blocked.
379                         return arrayOf(
380                             REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n", blacklistEntry[2])
381                     }
382
383                     // There are three entries.
384                     4 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
385                         // Return a blacklist match request blocked.
386                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
387                     }
388
389                     // There are four entries.
390                     5 -> if (resourceUrl.contains(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
391                         // Return a blacklist match request blocked.
392                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}",
393                             blacklistEntry[4])
394                     }
395                 }
396             }
397
398             // Third-party initial blacklist.
399             for (blacklistEntry in blocklist[THIRD_PARTY_INITIAL_BLACKLIST.toInt()]) {
400                 when (blacklistEntry.size) {
401                     // There is one entry.
402                     2 -> if (resourceUrl.startsWith(blacklistEntry[0])) {
403                         // Return a blacklist match request blocked.
404                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_INITIAL_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
405                     }
406
407                     // There are two entries.
408                     3 -> if (resourceUrl.startsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
409                         // Return a blacklist match request blocked.
410                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
411                     }
412                 }
413             }
414
415             // Third-party domain blacklist.
416             for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_BLACKLIST.toInt()]) {
417                 when (blacklistEntry.size) {
418                     // There is one entry.
419                     3 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1])) {
420                         // Return a blacklist match request blocked.
421                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
422                     }
423
424                     // There are two entries.
425                     4 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.contains(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
426                         // Return a blacklist match request blocked.
427                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
428                     }
429                 }
430             }
431
432             // Third-party domain initial blacklist.
433             for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST.toInt()]) {
434                 when (blacklistEntry.size) {
435                     // There is one entry.
436                     3 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1])) {
437                         // Return a blacklist match request blocked.
438                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n", blacklistEntry[2])
439                     }
440
441                     // There are two entries.
442                     4 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2])) {
443                         // Return a blacklist match request blocked.
444                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}", blacklistEntry[3])
445                     }
446
447                     // There are three entries.
448                     5 -> if (currentDomain!!.endsWith(blacklistEntry[0]) && resourceUrl.startsWith(blacklistEntry[1]) && resourceUrl.contains(blacklistEntry[2]) && resourceUrl.contains(blacklistEntry[3])) {
449                         // Return a blacklist match request blocked.
450                         return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST,
451                             "${blacklistEntry[0]}\n${blacklistEntry[1]}\n${blacklistEntry[2]}\n${blacklistEntry[3]}", blacklistEntry[4])
452                     }
453                 }
454             }
455
456             // Third-party regular expression blacklist.
457             for (blacklistEntry in blocklist[THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
458                 if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
459                     // Return a blacklist match request blocked.
460                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
461                 }
462             }
463
464             // Third-party domain regular expression blacklist.
465             for (blacklistEntry in blocklist[THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
466                 if (currentDomain!!.endsWith(blacklistEntry[0]) && Pattern.matches(blacklistEntry[1], resourceUrl)) {
467                     // Return a blacklist match request blocked.
468                     return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST, "${blacklistEntry[0]}\n${blacklistEntry[1]}", blacklistEntry[2])
469                 }
470             }
471         }
472
473         // Regular expression blacklist.
474         for (blacklistEntry in blocklist[REGULAR_EXPRESSION_BLACKLIST.toInt()]) {
475             if (Pattern.matches(blacklistEntry[0], resourceUrl)) {
476                 // Return a blacklist match request blocked.
477                 return arrayOf(REQUEST_BLOCKED, resourceUrl, blocklistName, REGULAR_EXPRESSION_BLACKLIST, blacklistEntry[0], blacklistEntry[1])
478             }
479         }
480
481         // Return a no match request default.
482         return arrayOf(REQUEST_DEFAULT)
483     }
484 }