Skip to main content

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_constellation_traits::EmbedderToConstellationMessage;
9
10use crate::proxies::ConstellationProxy;
11
12#[derive(Clone, Debug, PartialEq)]
13pub struct CacheEntry {
14    key: String,
15}
16
17impl CacheEntry {
18    pub fn new(key: String) -> Self {
19        Self { key }
20    }
21
22    pub fn key(&self) -> &str {
23        &self.key
24    }
25}
26
27/// Provides APIs for managing network-related state.
28///
29/// `NetworkManager` is responsible for data owned by the networking layer,
30/// such as the HTTP cache. This data is not considered site data and is
31/// therefore intentionally separate from `SiteDataManager`.
32pub struct NetworkManager {
33    public_resource_threads: ResourceThreads,
34    private_resource_threads: ResourceThreads,
35    constellation_proxy: ConstellationProxy,
36}
37
38impl NetworkManager {
39    pub(crate) fn new(
40        public_resource_threads: ResourceThreads,
41        private_resource_threads: ResourceThreads,
42        constellation_proxy: ConstellationProxy,
43    ) -> Self {
44        Self {
45            public_resource_threads,
46            private_resource_threads,
47            constellation_proxy,
48        }
49    }
50
51    /// Returns cache entries currently stored in the HTTP cache.
52    ///
53    /// The returned list contains one [`CacheEntry`] per unique cache key
54    /// (URL) for which the networking layer currently maintains cached
55    /// responses.
56    ///
57    /// Both public and private browsing contexts are included in the result.
58    ///
59    /// Note: The networking layer currently only implements an in-memory HTTP
60    /// cache. Support for an on-disk cache is under development.
61    pub fn cache_entries(&self) -> Vec<CacheEntry> {
62        let public_entries = self.public_resource_threads.cache_entries();
63        let private_entries = self.private_resource_threads.cache_entries();
64
65        let unique_keys: HashSet<String> = public_entries
66            .into_iter()
67            .chain(private_entries)
68            .map(|entry| entry.key)
69            .collect();
70
71        unique_keys.into_iter().map(CacheEntry::new).collect()
72    }
73
74    /// Clears the network (HTTP) cache.
75    ///
76    /// This removes all cached network responses maintained by the networking
77    /// layer for both public and private browsing contexts.
78    ///
79    /// Note: The networking layer currently only implements an in-memory HTTP
80    /// cache. Support for an on-disk cache is under development.
81    pub fn clear_cache(&self) {
82        self.public_resource_threads.clear_cache();
83        self.private_resource_threads.clear_cache();
84    }
85
86    /// Set the network online state. This controls the value of [`navigator.onLine`]
87    /// and may fire `online` and `offline` events in web content.
88    ///
89    /// [`navigator.onLine`]: https://html.spec.whatwg.org/multipage/#dom-navigator-online
90    pub fn set_online_state(&self, online: bool) {
91        self.constellation_proxy
92            .send(EmbedderToConstellationMessage::SetNetworkOnlineState(
93                online,
94            ));
95    }
96}