1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::cell::Cell;

use dom_struct::dom_struct;
use js::rust::{HandleObject, HandleValue};
use script_traits::BroadcastMsg;
use uuid::Uuid;

use crate::dom::bindings::codegen::Bindings::BroadcastChannelBinding::BroadcastChannelMethods;
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};

#[dom_struct]
pub struct BroadcastChannel {
    eventtarget: EventTarget,
    name: DOMString,
    closed: Cell<bool>,
    #[no_trace]
    id: Uuid,
}

impl BroadcastChannel {
    fn new(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        name: DOMString,
        can_gc: CanGc,
    ) -> DomRoot<BroadcastChannel> {
        let channel = reflect_dom_object_with_proto(
            Box::new(BroadcastChannel::new_inherited(name)),
            global,
            proto,
            can_gc,
        );
        global.track_broadcast_channel(&channel);
        channel
    }

    pub fn new_inherited(name: DOMString) -> BroadcastChannel {
        BroadcastChannel {
            eventtarget: EventTarget::new_inherited(),
            name,
            closed: Default::default(),
            id: Uuid::new_v4(),
        }
    }

    /// The unique Id of this channel.
    /// Used for filtering out the sender from the local broadcast.
    pub fn id(&self) -> &Uuid {
        &self.id
    }

    /// Is this channel closed?
    pub fn closed(&self) -> bool {
        self.closed.get()
    }
}

impl BroadcastChannelMethods for BroadcastChannel {
    /// <https://html.spec.whatwg.org/multipage/#broadcastchannel>
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        name: DOMString,
    ) -> DomRoot<BroadcastChannel> {
        BroadcastChannel::new(global, proto, name, can_gc)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage>
    fn PostMessage(&self, cx: SafeJSContext, message: HandleValue) -> ErrorResult {
        // Step 3, if closed.
        if self.closed.get() {
            return Err(Error::InvalidState);
        }

        // Step 6, StructuredSerialize(message).
        let data = structuredclone::write(cx, message, None)?;

        let global = self.global();

        let msg = BroadcastMsg {
            origin: global.origin().immutable().clone(),
            channel_name: self.Name().to_string(),
            data,
        };

        global.schedule_broadcast(msg, &self.id);
        Ok(())
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-broadcastchannel-name>
    fn Name(&self) -> DOMString {
        self.name.clone()
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-broadcastchannel-close>
    fn Close(&self) {
        self.closed.set(true);
    }

    // <https://html.spec.whatwg.org/multipage/#handler-broadcastchannel-onmessageerror>
    event_handler!(messageerror, GetOnmessageerror, SetOnmessageerror);

    // <https://html.spec.whatwg.org/multipage/#handler-broadcastchannel-onmessage>
    event_handler!(message, GetOnmessage, SetOnmessage);
}