servo/network_manager.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::collections::HashSet;
6
7use net_traits::ResourceThreads;
8use servo_base::generic_channel;
9use servo_constellation_traits::EmbedderToConstellationMessage;
10
11use crate::proxies::ConstellationProxy;
12
13#[derive(Clone, Debug, PartialEq)]
14pub struct CacheEntry {
15 key: String,
16}
17
18impl CacheEntry {
19 pub fn new(key: String) -> Self {
20 Self { key }
21 }
22
23 pub fn key(&self) -> &str {
24 &self.key
25 }
26}
27
28/// Provides APIs for managing network-related state.
29///
30/// `NetworkManager` is responsible for data owned by the networking layer,
31/// such as the HTTP cache. This data is not considered site data and is
32/// therefore intentionally separate from `SiteDataManager`.
33pub struct NetworkManager {
34 public_resource_threads: ResourceThreads,
35 private_resource_threads: ResourceThreads,
36 constellation_proxy: ConstellationProxy,
37}
38
39impl NetworkManager {
40 pub(crate) fn new(
41 public_resource_threads: ResourceThreads,
42 private_resource_threads: ResourceThreads,
43 constellation_proxy: ConstellationProxy,
44 ) -> Self {
45 Self {
46 public_resource_threads,
47 private_resource_threads,
48 constellation_proxy,
49 }
50 }
51
52 /// Returns cache entries currently stored in the HTTP cache.
53 ///
54 /// The returned list contains one [`CacheEntry`] per unique cache key
55 /// (URL) for which the networking layer currently maintains cached
56 /// responses.
57 ///
58 /// Both public and private browsing contexts are included in the result.
59 ///
60 /// Note: The networking layer currently only implements an in-memory HTTP
61 /// cache. Support for an on-disk cache is under development.
62 pub fn cache_entries(&self) -> Vec<CacheEntry> {
63 let public_entries = self.public_resource_threads.cache_entries();
64 let private_entries = self.private_resource_threads.cache_entries();
65
66 let unique_keys: HashSet<String> = public_entries
67 .into_iter()
68 .chain(private_entries)
69 .map(|entry| entry.key)
70 .collect();
71
72 unique_keys.into_iter().map(CacheEntry::new).collect()
73 }
74
75 /// Clears the network (HTTP) cache.
76 ///
77 /// This removes all cached network responses maintained by the networking
78 /// layer for both public and private browsing contexts.
79 ///
80 /// Note: The networking layer currently only implements an in-memory HTTP
81 /// cache. Support for an on-disk cache is under development.
82 pub fn clear_cache(&self) {
83 self.public_resource_threads.clear_cache();
84 self.private_resource_threads.clear_cache();
85 }
86
87 /// Set the network online state. This controls the value of [`navigator.onLine`]
88 /// and may fire `online` and `offline` events in web content.
89 ///
90 /// [`navigator.onLine`]: https://html.spec.whatwg.org/multipage/#dom-navigator-online
91 pub fn set_online_state(&self, online: bool) {
92 self.constellation_proxy
93 .send(EmbedderToConstellationMessage::SetNetworkOnlineState(
94 online,
95 ));
96 }
97
98 /// Stores the http cache to disk.
99 pub fn store_http_cache_to_disk(&self) {
100 let (sender, receiver) = generic_channel::channel().unwrap();
101 self.constellation_proxy
102 .send(EmbedderToConstellationMessage::StoreCacheToDisk(sender));
103 let _ = receiver.recv();
104 }
105}