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::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::RTCDataChannelEventBinding::{
11    RTCDataChannelEventInit, RTCDataChannelEventMethods,
12};
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::event::Event;
18use crate::dom::rtcdatachannel::RTCDataChannel;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
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        window: &Window,
38        type_: Atom,
39        bubbles: bool,
40        cancelable: bool,
41        channel: &RTCDataChannel,
42        can_gc: CanGc,
43    ) -> DomRoot<RTCDataChannelEvent> {
44        Self::new_with_proto(window, None, type_, bubbles, cancelable, channel, can_gc)
45    }
46
47    fn new_with_proto(
48        window: &Window,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: bool,
52        cancelable: bool,
53        channel: &RTCDataChannel,
54        can_gc: CanGc,
55    ) -> DomRoot<RTCDataChannelEvent> {
56        let event = reflect_dom_object_with_proto(
57            Box::new(RTCDataChannelEvent::new_inherited(channel)),
58            window,
59            proto,
60            can_gc,
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        window: &Window,
74        proto: Option<HandleObject>,
75        can_gc: CanGc,
76        type_: DOMString,
77        init: &RTCDataChannelEventInit,
78    ) -> DomRoot<RTCDataChannelEvent> {
79        RTCDataChannelEvent::new_with_proto(
80            window,
81            proto,
82            Atom::from(type_),
83            init.parent.bubbles,
84            init.parent.cancelable,
85            &init.channel,
86            can_gc,
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}