@@ -8,7 +8,7 @@ use std::ops::DerefMut;
8
8
use thiserror:: Error ;
9
9
10
10
use crate :: filters:: network:: { NetworkFilter , NetworkFilterMaskHelper } ;
11
- use crate :: network_filter_list:: NetworkFilterList ;
11
+ use crate :: network_filter_list:: { NetworkFilterList , NetworkFilterListTrait } ;
12
12
use crate :: regex_manager:: { RegexManager , RegexManagerDiscardPolicy } ;
13
13
use crate :: request:: Request ;
14
14
use crate :: resources:: ResourceStorage ;
@@ -88,15 +88,18 @@ pub enum BlockerError {
88
88
static NO_TAGS : Lazy < HashSet < String > > = Lazy :: new ( HashSet :: new) ;
89
89
90
90
/// Stores network filters for efficient querying.
91
- pub struct Blocker {
92
- pub ( crate ) csp : NetworkFilterList ,
93
- pub ( crate ) exceptions : NetworkFilterList ,
94
- pub ( crate ) importants : NetworkFilterList ,
95
- pub ( crate ) redirects : NetworkFilterList ,
96
- pub ( crate ) removeparam : NetworkFilterList ,
97
- pub ( crate ) filters_tagged : NetworkFilterList ,
98
- pub ( crate ) filters : NetworkFilterList ,
99
- pub ( crate ) generic_hide : NetworkFilterList ,
91
+ pub struct Blocker < NetworkFilterListType = NetworkFilterList >
92
+ where
93
+ NetworkFilterList : NetworkFilterListTrait ,
94
+ {
95
+ pub ( crate ) csp : NetworkFilterListType ,
96
+ pub ( crate ) exceptions : NetworkFilterListType ,
97
+ pub ( crate ) importants : NetworkFilterListType ,
98
+ pub ( crate ) redirects : NetworkFilterListType ,
99
+ pub ( crate ) removeparam : NetworkFilterListType ,
100
+ pub ( crate ) filters_tagged : NetworkFilterListType ,
101
+ pub ( crate ) filters : NetworkFilterListType ,
102
+ pub ( crate ) generic_hide : NetworkFilterListType ,
100
103
101
104
// Enabled tags are not serialized - when deserializing, tags of the existing
102
105
// instance (the one we are recreating lists into) are maintained
@@ -112,7 +115,10 @@ pub struct Blocker {
112
115
pub ( crate ) regex_manager : std:: sync:: Mutex < RegexManager > ,
113
116
}
114
117
115
- impl Blocker {
118
+ impl < NetworkFilterListType > Blocker < NetworkFilterListType >
119
+ where
120
+ NetworkFilterListType : NetworkFilterListTrait ,
121
+ {
116
122
/// Decide if a network request (usually from WebRequest API) should be
117
123
/// blocked, redirected or allowed.
118
124
pub fn check ( & self , request : & Request , resources : & ResourceStorage ) -> BlockerResult {
@@ -271,7 +277,7 @@ impl Blocker {
271
277
}
272
278
273
279
fn apply_removeparam (
274
- removeparam_filters : & NetworkFilterList ,
280
+ removeparam_filters : & NetworkFilterListType ,
275
281
request : & Request ,
276
282
regex_manager : & mut RegexManager ,
277
283
) -> Option < String > {
@@ -414,7 +420,7 @@ impl Blocker {
414
420
Some ( merged)
415
421
}
416
422
417
- pub fn new ( network_filters : Vec < NetworkFilter > , options : & BlockerOptions ) -> Blocker {
423
+ pub fn new ( network_filters : Vec < NetworkFilter > , options : & BlockerOptions ) -> Self {
418
424
// Capacity of filter subsets estimated based on counts in EasyList and EasyPrivacy - if necessary
419
425
// the Vectors will grow beyond the pre-set capacity, but it is more efficient to allocate all at once
420
426
// $csp=
@@ -486,17 +492,17 @@ impl Blocker {
486
492
487
493
tagged_filters_all. shrink_to_fit ( ) ;
488
494
489
- Blocker {
490
- csp : NetworkFilterList :: new ( csp, options. enable_optimizations ) ,
491
- exceptions : NetworkFilterList :: new ( exceptions, options. enable_optimizations ) ,
492
- importants : NetworkFilterList :: new ( importants, options. enable_optimizations ) ,
493
- redirects : NetworkFilterList :: new ( redirects, options. enable_optimizations ) ,
495
+ Self {
496
+ csp : NetworkFilterListType :: new ( csp, options. enable_optimizations ) ,
497
+ exceptions : NetworkFilterListType :: new ( exceptions, options. enable_optimizations ) ,
498
+ importants : NetworkFilterListType :: new ( importants, options. enable_optimizations ) ,
499
+ redirects : NetworkFilterListType :: new ( redirects, options. enable_optimizations ) ,
494
500
// Don't optimize removeparam, since it can fuse filters without respecting distinct
495
501
// queryparam values
496
- removeparam : NetworkFilterList :: new ( removeparam, false ) ,
497
- filters_tagged : NetworkFilterList :: new ( Vec :: new ( ) , options. enable_optimizations ) ,
498
- filters : NetworkFilterList :: new ( filters, options. enable_optimizations ) ,
499
- generic_hide : NetworkFilterList :: new ( generic_hide, options. enable_optimizations ) ,
502
+ removeparam : NetworkFilterListType :: new ( removeparam, false ) ,
503
+ filters_tagged : NetworkFilterListType :: new ( Vec :: new ( ) , options. enable_optimizations ) ,
504
+ filters : NetworkFilterListType :: new ( filters, options. enable_optimizations ) ,
505
+ generic_hide : NetworkFilterListType :: new ( generic_hide, options. enable_optimizations ) ,
500
506
// Tags special case for enabling/disabling them dynamically
501
507
tags_enabled : HashSet :: new ( ) ,
502
508
tagged_filters_all,
@@ -618,7 +624,7 @@ impl Blocker {
618
624
. filter ( |n| n. tag . is_some ( ) && self . tags_enabled . contains ( n. tag . as_ref ( ) . unwrap ( ) ) )
619
625
. cloned ( )
620
626
. collect ( ) ;
621
- self . filters_tagged = NetworkFilterList :: new ( filters, self . enable_optimizations ) ;
627
+ self . filters_tagged = NetworkFilterListType :: new ( filters, self . enable_optimizations ) ;
622
628
}
623
629
624
630
pub fn tags_enabled ( & self ) -> Vec < String > {
0 commit comments