script/dom/
client.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::default::Default;
6
7use dom_struct::dom_struct;
8use servo_url::ServoUrl;
9use uuid::Uuid;
10
11use crate::dom::bindings::codegen::Bindings::ClientBinding::{ClientMethods, FrameType};
12use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
13use crate::dom::bindings::root::{DomRoot, MutNullableDom};
14use crate::dom::bindings::str::{DOMString, USVString};
15use crate::dom::serviceworker::ServiceWorker;
16use crate::dom::window::Window;
17use crate::script_runtime::CanGc;
18
19#[dom_struct]
20pub(crate) struct Client {
21    reflector_: Reflector,
22    active_worker: MutNullableDom<ServiceWorker>,
23    #[no_trace]
24    url: ServoUrl,
25    frame_type: FrameType,
26    #[no_trace]
27    id: Uuid,
28}
29
30impl Client {
31    fn new_inherited(url: ServoUrl) -> Client {
32        Client {
33            reflector_: Reflector::new(),
34            active_worker: Default::default(),
35            url,
36            frame_type: FrameType::None,
37            id: Uuid::new_v4(),
38        }
39    }
40
41    pub(crate) fn new(window: &Window, can_gc: CanGc) -> DomRoot<Client> {
42        reflect_dom_object(
43            Box::new(Client::new_inherited(window.get_url())),
44            window,
45            can_gc,
46        )
47    }
48
49    pub(crate) fn creation_url(&self) -> ServoUrl {
50        self.url.clone()
51    }
52
53    pub(crate) fn get_controller(&self) -> Option<DomRoot<ServiceWorker>> {
54        self.active_worker.get()
55    }
56
57    #[expect(dead_code)]
58    pub(crate) fn set_controller(&self, worker: &ServiceWorker) {
59        self.active_worker.set(Some(worker));
60    }
61}
62
63impl ClientMethods<crate::DomTypeHolder> for Client {
64    /// <https://w3c.github.io/ServiceWorker/#client-url-attribute>
65    fn Url(&self) -> USVString {
66        USVString(self.url.as_str().to_owned())
67    }
68
69    /// <https://w3c.github.io/ServiceWorker/#client-frametype>
70    fn FrameType(&self) -> FrameType {
71        self.frame_type
72    }
73
74    /// <https://w3c.github.io/ServiceWorker/#client-id>
75    fn Id(&self) -> DOMString {
76        let uid_str = format!("{}", self.id);
77        DOMString::from_string(uid_str)
78    }
79}