Skip to main content

script/dom/webrtc/
rtcpeerconnectioniceevent.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::RTCPeerConnectionIceEventBinding::{
13    RTCPeerConnectionIceEventInit, RTCPeerConnectionIceEventMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::rtcicecandidate::RTCIceCandidate;
21use crate::dom::window::Window;
22
23#[dom_struct]
24pub(crate) struct RTCPeerConnectionIceEvent {
25    event: Event,
26    candidate: Option<Dom<RTCIceCandidate>>,
27    url: Option<DOMString>,
28}
29
30impl RTCPeerConnectionIceEvent {
31    pub(crate) fn new_inherited(
32        candidate: Option<&RTCIceCandidate>,
33        url: Option<DOMString>,
34    ) -> RTCPeerConnectionIceEvent {
35        RTCPeerConnectionIceEvent {
36            event: Event::new_inherited(),
37            candidate: candidate.map(Dom::from_ref),
38            url,
39        }
40    }
41
42    pub(crate) fn new(
43        cx: &mut JSContext,
44        window: &Window,
45        ty: Atom,
46        candidate: Option<&RTCIceCandidate>,
47        url: Option<DOMString>,
48        trusted: bool,
49    ) -> DomRoot<RTCPeerConnectionIceEvent> {
50        Self::new_with_proto(cx, window, None, ty, candidate, url, trusted)
51    }
52
53    fn new_with_proto(
54        cx: &mut JSContext,
55        window: &Window,
56        proto: Option<HandleObject>,
57        ty: Atom,
58        candidate: Option<&RTCIceCandidate>,
59        url: Option<DOMString>,
60        trusted: bool,
61    ) -> DomRoot<RTCPeerConnectionIceEvent> {
62        let e = reflect_dom_object_with_proto_and_cx(
63            Box::new(RTCPeerConnectionIceEvent::new_inherited(candidate, url)),
64            window,
65            proto,
66            cx,
67        );
68        let evt = e.upcast::<Event>();
69        evt.init_event(ty, false, false); // XXXManishearth bubbles/cancelable?
70        evt.set_trusted(trusted);
71        e
72    }
73}
74
75impl RTCPeerConnectionIceEventMethods<crate::DomTypeHolder> for RTCPeerConnectionIceEvent {
76    /// <https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnectioniceevent-constructor>
77    fn Constructor(
78        cx: &mut JSContext,
79        window: &Window,
80        proto: Option<HandleObject>,
81        ty: DOMString,
82        init: &RTCPeerConnectionIceEventInit,
83    ) -> Fallible<DomRoot<RTCPeerConnectionIceEvent>> {
84        Ok(RTCPeerConnectionIceEvent::new_with_proto(
85            cx,
86            window,
87            proto,
88            ty.into(),
89            init.candidate
90                .as_ref()
91                .and_then(|x| x.as_ref())
92                .map(|x| &**x),
93            init.url.as_ref().and_then(|x| x.clone()),
94            false,
95        ))
96    }
97
98    /// <https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnectioniceevent-candidate>
99    fn GetCandidate(&self) -> Option<DomRoot<RTCIceCandidate>> {
100        self.candidate.as_ref().map(|x| DomRoot::from_ref(&**x))
101    }
102
103    /// <https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnectioniceevent-url>
104    fn GetUrl(&self) -> Option<DOMString> {
105        self.url.clone()
106    }
107
108    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
109    fn IsTrusted(&self) -> bool {
110        self.event.IsTrusted()
111    }
112}