1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashSet;
use net_traits::ResourceThreads;
#[derive(Clone, Debug, PartialEq)]
pub struct CacheEntry {
key: String,
}
impl CacheEntry {
pub fn new(key: String) -> Self {
Self { key }
}
pub fn key(&self) -> &str {
&self.key
}
}
/// Provides APIs for managing network-related state.
///
/// `NetworkManager` is responsible for data owned by the networking layer,
/// such as the HTTP cache. This data is not considered site data and is
/// therefore intentionally separate from `SiteDataManager`.
pub struct NetworkManager {
public_resource_threads: ResourceThreads,
private_resource_threads: ResourceThreads,
}
impl NetworkManager {
pub(crate) fn new(
public_resource_threads: ResourceThreads,
private_resource_threads: ResourceThreads,
) -> Self {
Self {
public_resource_threads,
private_resource_threads,
}
}
/// Returns cache entries currently stored in the HTTP cache.
///
/// The returned list contains one [`CacheEntry`] per unique cache key
/// (URL) for which the networking layer currently maintains cached
/// responses.
///
/// Both public and private browsing contexts are included in the result.
///
/// Note: The networking layer currently only implements an in-memory HTTP
/// cache. Support for an on-disk cache is under development.
pub fn cache_entries(&self) -> Vec<CacheEntry> {
let public_entries = self.public_resource_threads.cache_entries();
let private_entries = self.private_resource_threads.cache_entries();
let unique_keys: HashSet<String> = public_entries
.into_iter()
.chain(private_entries)
.map(|entry| entry.key)
.collect();
unique_keys.into_iter().map(CacheEntry::new).collect()
}
/// Clears the network (HTTP) cache.
///
/// This removes all cached network responses maintained by the networking
/// layer for both public and private browsing contexts.
///
/// Note: The networking layer currently only implements an in-memory HTTP
/// cache. Support for an on-disk cache is under development.
pub fn clear_cache(&self) {
self.public_resource_threads.clear_cache();
self.private_resource_threads.clear_cache();
}
}