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    #[ignore_malloc_size_of = "Defined in uuid"]
27    #[no_trace]
28    id: Uuid,
29}
30
31impl Client {
32    fn new_inherited(url: ServoUrl) -> Client {
33        Client {
34            reflector_: Reflector::new(),
35            active_worker: Default::default(),
36            url,
37            frame_type: FrameType::None,
38            id: Uuid::new_v4(),
39        }
40    }
41
42    pub(crate) fn new(window: &Window, can_gc: CanGc) -> DomRoot<Client> {
43        reflect_dom_object(
44            Box::new(Client::new_inherited(window.get_url())),
45            window,
46            can_gc,
47        )
48    }
49
50    pub(crate) fn creation_url(&self) -> ServoUrl {
51        self.url.clone()
52    }
53
54    pub(crate) fn get_controller(&self) -> Option<DomRoot<ServiceWorker>> {
55        self.active_worker.get()
56    }
57
58    #[allow(dead_code)]
59    pub(crate) fn set_controller(&self, worker: &ServiceWorker) {
60        self.active_worker.set(Some(worker));
61    }
62}
63
64impl ClientMethods<crate::DomTypeHolder> for Client {
65    // https://w3c.github.io/ServiceWorker/#client-url-attribute
66    fn Url(&self) -> USVString {
67        USVString(self.url.as_str().to_owned())
68    }
69
70    // https://w3c.github.io/ServiceWorker/#client-frametype
71    fn FrameType(&self) -> FrameType {
72        self.frame_type
73    }
74
75    // https://w3c.github.io/ServiceWorker/#client-id
76    fn Id(&self) -> DOMString {
77        let uid_str = format!("{}", self.id);
78        DOMString::from_string(uid_str)
79    }
80}