Skip to main content

servo_constellation/
serviceworker.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
5#[cfg(feature = "multiprocess")]
6use ipc_channel::IpcError;
7use serde::{Deserialize, Serialize};
8use servo_base::generic_channel::GenericSender;
9#[cfg(feature = "multiprocess")]
10use servo_config::opts::{self, Opts};
11#[cfg(feature = "multiprocess")]
12use servo_config::{prefs, prefs::Preferences};
13use servo_constellation_traits::{SWManagerSenders, ServiceWorkerManagerFactory};
14use servo_url::ImmutableOrigin;
15
16#[cfg(feature = "multiprocess")]
17use crate::process_manager::Process;
18#[cfg(feature = "multiprocess")]
19use crate::sandboxing::UnprivilegedContent;
20#[cfg(feature = "multiprocess")]
21use crate::sandboxing::spawn_multiprocess;
22
23/// Conceptually, this is glue to start an agent-cluster for a service worker agent.
24/// <https://html.spec.whatwg.org/multipage/#obtain-a-service-worker-agent>
25#[derive(Deserialize, Serialize)]
26pub struct ServiceWorkerUnprivilegedContent {
27    #[cfg(feature = "multiprocess")]
28    pub opts: Opts,
29    #[cfg(feature = "multiprocess")]
30    pub prefs: Box<Preferences>,
31    senders: SWManagerSenders,
32    origin: ImmutableOrigin,
33    lifeline_sender: Option<GenericSender<()>>,
34}
35
36impl ServiceWorkerUnprivilegedContent {
37    pub fn new(
38        senders: SWManagerSenders,
39        origin: ImmutableOrigin,
40        lifeline_sender: Option<GenericSender<()>>,
41    ) -> ServiceWorkerUnprivilegedContent {
42        ServiceWorkerUnprivilegedContent {
43            #[cfg(feature = "multiprocess")]
44            opts: (*opts::get()).clone(),
45            #[cfg(feature = "multiprocess")]
46            prefs: Box::new(prefs::get().clone()),
47            senders,
48            origin,
49            lifeline_sender,
50        }
51    }
52
53    /// Start the agent-cluster.
54    pub fn start<SWF>(self)
55    where
56        SWF: ServiceWorkerManagerFactory,
57    {
58        SWF::create(self.senders, self.origin);
59    }
60
61    /// Start the agent-cluster in it's own process.
62    #[cfg(feature = "multiprocess")]
63    pub fn spawn_multiprocess(self) -> Result<Process, IpcError> {
64        spawn_multiprocess(UnprivilegedContent::ServiceWorker(self))
65    }
66}