script/dom/
broadcastchannel.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 http://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6
7use constellation_traits::BroadcastChannelMsg;
8use dom_struct::dom_struct;
9use js::rust::{HandleObject, HandleValue};
10use uuid::Uuid;
11
12use crate::dom::bindings::codegen::Bindings::BroadcastChannelBinding::BroadcastChannelMethods;
13use crate::dom::bindings::error::{Error, ErrorResult};
14use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::bindings::structuredclone;
18use crate::dom::eventtarget::EventTarget;
19use crate::dom::globalscope::GlobalScope;
20use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
21
22#[dom_struct]
23pub(crate) struct BroadcastChannel {
24    eventtarget: EventTarget,
25    name: DOMString,
26    closed: Cell<bool>,
27    #[no_trace]
28    id: Uuid,
29}
30
31impl BroadcastChannel {
32    fn new(
33        global: &GlobalScope,
34        proto: Option<HandleObject>,
35        name: DOMString,
36        can_gc: CanGc,
37    ) -> DomRoot<BroadcastChannel> {
38        let channel = reflect_dom_object_with_proto(
39            Box::new(BroadcastChannel::new_inherited(name)),
40            global,
41            proto,
42            can_gc,
43        );
44        global.track_broadcast_channel(&channel);
45        channel
46    }
47
48    pub(crate) fn new_inherited(name: DOMString) -> BroadcastChannel {
49        BroadcastChannel {
50            eventtarget: EventTarget::new_inherited(),
51            name,
52            closed: Default::default(),
53            id: Uuid::new_v4(),
54        }
55    }
56
57    /// The unique Id of this channel.
58    /// Used for filtering out the sender from the local broadcast.
59    pub(crate) fn id(&self) -> &Uuid {
60        &self.id
61    }
62
63    /// Is this channel closed?
64    pub(crate) fn closed(&self) -> bool {
65        self.closed.get()
66    }
67}
68
69impl BroadcastChannelMethods<crate::DomTypeHolder> for BroadcastChannel {
70    /// <https://html.spec.whatwg.org/multipage/#broadcastchannel>
71    fn Constructor(
72        global: &GlobalScope,
73        proto: Option<HandleObject>,
74        can_gc: CanGc,
75        name: DOMString,
76    ) -> DomRoot<BroadcastChannel> {
77        BroadcastChannel::new(global, proto, name, can_gc)
78    }
79
80    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage>
81    fn PostMessage(&self, cx: SafeJSContext, message: HandleValue) -> ErrorResult {
82        // Step 3, if closed.
83        if self.closed.get() {
84            return Err(Error::InvalidState);
85        }
86
87        // Step 6, StructuredSerialize(message).
88        let data = structuredclone::write(cx, message, None)?;
89
90        let global = self.global();
91
92        let msg = BroadcastChannelMsg {
93            origin: global.origin().immutable().clone(),
94            channel_name: self.Name().to_string(),
95            data,
96        };
97
98        global.schedule_broadcast(msg, &self.id);
99        Ok(())
100    }
101
102    /// <https://html.spec.whatwg.org/multipage/#dom-broadcastchannel-name>
103    fn Name(&self) -> DOMString {
104        self.name.clone()
105    }
106
107    /// <https://html.spec.whatwg.org/multipage/#dom-broadcastchannel-close>
108    fn Close(&self) {
109        self.closed.set(true);
110    }
111
112    // <https://html.spec.whatwg.org/multipage/#handler-broadcastchannel-onmessageerror>
113    event_handler!(messageerror, GetOnmessageerror, SetOnmessageerror);
114
115    // <https://html.spec.whatwg.org/multipage/#handler-broadcastchannel-onmessage>
116    event_handler!(message, GetOnmessage, SetOnmessage);
117}