Skip to content

Commit d7ba6ae

Browse files
committed
Added NetworkFilterListTrait to futher provide the flatbuffer impl.
1 parent b904779 commit d7ba6ae

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

src/blocker.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ops::DerefMut;
88
use thiserror::Error;
99

1010
use crate::filters::network::{NetworkFilter, NetworkFilterMaskHelper};
11-
use crate::network_filter_list::NetworkFilterList;
11+
use crate::network_filter_list::{NetworkFilterList, NetworkFilterListTrait};
1212
use crate::regex_manager::{RegexManager, RegexManagerDiscardPolicy};
1313
use crate::request::Request;
1414
use crate::resources::ResourceStorage;
@@ -88,15 +88,18 @@ pub enum BlockerError {
8888
static NO_TAGS: Lazy<HashSet<String>> = Lazy::new(HashSet::new);
8989

9090
/// 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,
100103

101104
// Enabled tags are not serialized - when deserializing, tags of the existing
102105
// instance (the one we are recreating lists into) are maintained
@@ -112,7 +115,10 @@ pub struct Blocker {
112115
pub(crate) regex_manager: std::sync::Mutex<RegexManager>,
113116
}
114117

115-
impl Blocker {
118+
impl<NetworkFilterListType> Blocker<NetworkFilterListType>
119+
where
120+
NetworkFilterListType: NetworkFilterListTrait,
121+
{
116122
/// Decide if a network request (usually from WebRequest API) should be
117123
/// blocked, redirected or allowed.
118124
pub fn check(&self, request: &Request, resources: &ResourceStorage) -> BlockerResult {
@@ -271,7 +277,7 @@ impl Blocker {
271277
}
272278

273279
fn apply_removeparam(
274-
removeparam_filters: &NetworkFilterList,
280+
removeparam_filters: &NetworkFilterListType,
275281
request: &Request,
276282
regex_manager: &mut RegexManager,
277283
) -> Option<String> {
@@ -414,7 +420,7 @@ impl Blocker {
414420
Some(merged)
415421
}
416422

417-
pub fn new(network_filters: Vec<NetworkFilter>, options: &BlockerOptions) -> Blocker {
423+
pub fn new(network_filters: Vec<NetworkFilter>, options: &BlockerOptions) -> Self {
418424
// Capacity of filter subsets estimated based on counts in EasyList and EasyPrivacy - if necessary
419425
// the Vectors will grow beyond the pre-set capacity, but it is more efficient to allocate all at once
420426
// $csp=
@@ -486,17 +492,17 @@ impl Blocker {
486492

487493
tagged_filters_all.shrink_to_fit();
488494

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),
494500
// Don't optimize removeparam, since it can fuse filters without respecting distinct
495501
// 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),
500506
// Tags special case for enabling/disabling them dynamically
501507
tags_enabled: HashSet::new(),
502508
tagged_filters_all,
@@ -618,7 +624,7 @@ impl Blocker {
618624
.filter(|n| n.tag.is_some() && self.tags_enabled.contains(n.tag.as_ref().unwrap()))
619625
.cloned()
620626
.collect();
621-
self.filters_tagged = NetworkFilterList::new(filters, self.enable_optimizations);
627+
self.filters_tagged = NetworkFilterListType::new(filters, self.enable_optimizations);
622628
}
623629

624630
pub fn tags_enabled(&self) -> Vec<String> {

src/network_filter_list.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,36 @@ use crate::regex_manager::RegexManager;
99
use crate::request::Request;
1010
use crate::utils::{fast_hash, Hash};
1111

12+
pub trait NetworkFilterListTrait {
13+
fn new(filters: Vec<NetworkFilter>, optimize: bool) -> Self
14+
where
15+
Self: Sized;
16+
fn optimize(&mut self);
17+
fn add_filter(&mut self, filter: NetworkFilter);
18+
fn filter_exists(&self, filter: &NetworkFilter) -> bool;
19+
20+
fn check(
21+
&self,
22+
request: &Request,
23+
active_tags: &HashSet<String>,
24+
regex_manager: &mut RegexManager,
25+
) -> Option<&NetworkFilter>;
26+
fn check_all(
27+
&self,
28+
request: &Request,
29+
active_tags: &HashSet<String>,
30+
regex_manager: &mut RegexManager,
31+
) -> Vec<&NetworkFilter>;
32+
}
33+
1234
#[derive(Serialize, Deserialize, Default)]
13-
pub(crate) struct NetworkFilterList {
35+
pub struct NetworkFilterList {
1436
#[serde(serialize_with = "crate::data_format::utils::stabilize_hashmap_serialization")]
1537
pub(crate) filter_map: HashMap<Hash, Vec<Arc<NetworkFilter>>>,
1638
}
1739

18-
impl NetworkFilterList {
19-
pub fn new(filters: Vec<NetworkFilter>, optimize: bool) -> NetworkFilterList {
40+
impl NetworkFilterListTrait for NetworkFilterList {
41+
fn new(filters: Vec<NetworkFilter>, optimize: bool) -> NetworkFilterList {
2042
// Compute tokens for all filters
2143
let filter_tokens: Vec<_> = filters
2244
.into_iter()
@@ -64,7 +86,7 @@ impl NetworkFilterList {
6486
self_
6587
}
6688

67-
pub fn optimize(&mut self) {
89+
fn optimize(&mut self) {
6890
let mut optimized_map = HashMap::with_capacity(self.filter_map.len());
6991
for (key, filters) in self.filter_map.drain() {
7092
let mut unoptimized: Vec<NetworkFilter> = Vec::with_capacity(filters.len());
@@ -97,7 +119,7 @@ impl NetworkFilterList {
97119
self.filter_map = optimized_map;
98120
}
99121

100-
pub fn add_filter(&mut self, filter: NetworkFilter) {
122+
fn add_filter(&mut self, filter: NetworkFilter) {
101123
let filter_tokens = filter.get_tokens();
102124
let total_rules = vec_hashmap_len(&self.filter_map);
103125
let filter_pointer = Arc::new(filter);
@@ -128,7 +150,7 @@ impl NetworkFilterList {
128150
}
129151

130152
/// This may not work if the list has been optimized.
131-
pub fn filter_exists(&self, filter: &NetworkFilter) -> bool {
153+
fn filter_exists(&self, filter: &NetworkFilter) -> bool {
132154
let mut tokens: Vec<_> = filter.get_tokens().into_iter().flatten().collect();
133155

134156
if tokens.is_empty() {
@@ -153,7 +175,7 @@ impl NetworkFilterList {
153175
/// match from each would be functionally equivalent. For example, if two different exception
154176
/// filters match a certain request, it doesn't matter _which_ one is matched - the request
155177
/// will be excepted either way.
156-
pub fn check(
178+
fn check(
157179
&self,
158180
request: &Request,
159181
active_tags: &HashSet<String>,
@@ -187,7 +209,7 @@ impl NetworkFilterList {
187209
/// filters where a match from each may carry unique information. For example, if two different
188210
/// `$csp` filters match a certain request, they may each carry a distinct CSP directive, and
189211
/// each directive should be combined for the final result.
190-
pub fn check_all(
212+
fn check_all(
191213
&self,
192214
request: &Request,
193215
active_tags: &HashSet<String>,

0 commit comments

Comments
 (0)