script/dom/webrtc/
rtcerrorevent.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::RTCErrorEventBinding::{
11    RTCErrorEventInit, RTCErrorEventMethods,
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::rtcerror::RTCError;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct RTCErrorEvent {
24    event: Event,
25    error: Dom<RTCError>,
26}
27
28impl RTCErrorEvent {
29    fn new_inherited(error: &RTCError) -> RTCErrorEvent {
30        RTCErrorEvent {
31            event: Event::new_inherited(),
32            error: Dom::from_ref(error),
33        }
34    }
35
36    pub(crate) fn new(
37        window: &Window,
38        type_: Atom,
39        bubbles: bool,
40        cancelable: bool,
41        error: &RTCError,
42        can_gc: CanGc,
43    ) -> DomRoot<RTCErrorEvent> {
44        Self::new_with_proto(window, None, type_, bubbles, cancelable, error, 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        error: &RTCError,
54        can_gc: CanGc,
55    ) -> DomRoot<RTCErrorEvent> {
56        let event = reflect_dom_object_with_proto(
57            Box::new(RTCErrorEvent::new_inherited(error)),
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 RTCErrorEventMethods<crate::DomTypeHolder> for RTCErrorEvent {
71    // https://www.w3.org/TR/webrtc/#dom-rtcerrorevent-constructor
72    fn Constructor(
73        window: &Window,
74        proto: Option<HandleObject>,
75        can_gc: CanGc,
76        type_: DOMString,
77        init: &RTCErrorEventInit,
78    ) -> DomRoot<RTCErrorEvent> {
79        RTCErrorEvent::new_with_proto(
80            window,
81            proto,
82            Atom::from(type_),
83            init.parent.bubbles,
84            init.parent.cancelable,
85            &init.error,
86            can_gc,
87        )
88    }
89
90    // https://www.w3.org/TR/webrtc/#dom-rtcerrorevent-error
91    fn Error(&self) -> DomRoot<RTCError> {
92        DomRoot::from_ref(&*self.error)
93    }
94
95    // https://dom.spec.whatwg.org/#dom-event-istrusted
96    fn IsTrusted(&self) -> bool {
97        self.event.IsTrusted()
98    }
99}