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