Skip to main content

script/dom/webrtc/
rtcdatachannelevent.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::RTCDataChannelEventBinding::{
13    RTCDataChannelEventInit, RTCDataChannelEventMethods,
14};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::event::Event;
19use crate::dom::rtcdatachannel::RTCDataChannel;
20use crate::dom::window::Window;
21
22#[dom_struct]
23pub(crate) struct RTCDataChannelEvent {
24    event: Event,
25    channel: Dom<RTCDataChannel>,
26}
27
28impl RTCDataChannelEvent {
29    fn new_inherited(channel: &RTCDataChannel) -> RTCDataChannelEvent {
30        RTCDataChannelEvent {
31            event: Event::new_inherited(),
32            channel: Dom::from_ref(channel),
33        }
34    }
35
36    pub(crate) fn new(
37        cx: &mut JSContext,
38        window: &Window,
39        type_: Atom,
40        bubbles: bool,
41        cancelable: bool,
42        channel: &RTCDataChannel,
43    ) -> DomRoot<RTCDataChannelEvent> {
44        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, channel)
45    }
46
47    fn new_with_proto(
48        cx: &mut JSContext,
49        window: &Window,
50        proto: Option<HandleObject>,
51        type_: Atom,
52        bubbles: bool,
53        cancelable: bool,
54        channel: &RTCDataChannel,
55    ) -> DomRoot<RTCDataChannelEvent> {
56        let event = reflect_dom_object_with_proto_and_cx(
57            Box::new(RTCDataChannelEvent::new_inherited(channel)),
58            window,
59            proto,
60            cx,
61        );
62        {
63            let event = event.upcast::<Event>();
64            event.init_event(type_, bubbles, cancelable);
65        }
66        event
67    }
68}
69
70impl RTCDataChannelEventMethods<crate::DomTypeHolder> for RTCDataChannelEvent {
71    /// <https://www.w3.org/TR/webrtc/#dom-rtcdatachannelevent-constructor>
72    fn Constructor(
73        cx: &mut JSContext,
74        window: &Window,
75        proto: Option<HandleObject>,
76        type_: DOMString,
77        init: &RTCDataChannelEventInit,
78    ) -> DomRoot<RTCDataChannelEvent> {
79        RTCDataChannelEvent::new_with_proto(
80            cx,
81            window,
82            proto,
83            Atom::from(type_),
84            init.parent.bubbles,
85            init.parent.cancelable,
86            &init.channel,
87        )
88    }
89
90    /// <https://www.w3.org/TR/webrtc/#dom-datachannelevent-channel>
91    fn Channel(&self) -> DomRoot<RTCDataChannel> {
92        DomRoot::from_ref(&*self.channel)
93    }
94
95    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
96    fn IsTrusted(&self) -> bool {
97        self.event.IsTrusted()
98    }
99}