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::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::RTCPeerConnectionIceEventBinding::{
11    RTCPeerConnectionIceEventInit, RTCPeerConnectionIceEventMethods,
12};
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::event::Event;
19use crate::dom::rtcicecandidate::RTCIceCandidate;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
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        window: &Window,
44        ty: Atom,
45        candidate: Option<&RTCIceCandidate>,
46        url: Option<DOMString>,
47        trusted: bool,
48        can_gc: CanGc,
49    ) -> DomRoot<RTCPeerConnectionIceEvent> {
50        Self::new_with_proto(window, None, ty, candidate, url, trusted, can_gc)
51    }
52
53    fn new_with_proto(
54        window: &Window,
55        proto: Option<HandleObject>,
56        ty: Atom,
57        candidate: Option<&RTCIceCandidate>,
58        url: Option<DOMString>,
59        trusted: bool,
60        can_gc: CanGc,
61    ) -> DomRoot<RTCPeerConnectionIceEvent> {
62        let e = reflect_dom_object_with_proto(
63            Box::new(RTCPeerConnectionIceEvent::new_inherited(candidate, url)),
64            window,
65            proto,
66            can_gc,
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        window: &Window,
79        proto: Option<HandleObject>,
80        can_gc: CanGc,
81        ty: DOMString,
82        init: &RTCPeerConnectionIceEventInit,
83    ) -> Fallible<DomRoot<RTCPeerConnectionIceEvent>> {
84        Ok(RTCPeerConnectionIceEvent::new_with_proto(
85            window,
86            proto,
87            ty.into(),
88            init.candidate
89                .as_ref()
90                .and_then(|x| x.as_ref())
91                .map(|x| &**x),
92            init.url.as_ref().and_then(|x| x.clone()),
93            false,
94            can_gc,
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}