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