Skip to main content

script/dom/webrtc/
rtcicecandidate.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::{Reflector, reflect_dom_object_with_proto};
10
11use crate::dom::bindings::codegen::Bindings::RTCIceCandidateBinding::{
12    RTCIceCandidateInit, RTCIceCandidateMethods,
13};
14use crate::dom::bindings::error::{Error, Fallible};
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::window::Window;
18
19#[dom_struct]
20pub(crate) struct RTCIceCandidate {
21    reflector: Reflector,
22    candidate: TracedDOMString,
23    sdp_m_id: Option<TracedDOMString>,
24    sdp_m_line_index: Option<u16>,
25    username_fragment: Option<TracedDOMString>,
26}
27
28impl RTCIceCandidate {
29    pub(crate) fn new_inherited(
30        candidate: DOMString,
31        sdp_m_id: Option<DOMString>,
32        sdp_m_line_index: Option<u16>,
33        username_fragment: Option<DOMString>,
34    ) -> RTCIceCandidate {
35        RTCIceCandidate {
36            reflector: Reflector::new(),
37            candidate: candidate.traced(),
38            sdp_m_id: sdp_m_id.map(|domstring| domstring.traced()),
39            sdp_m_line_index,
40            username_fragment: username_fragment.map(|domstring| domstring.traced()),
41        }
42    }
43
44    pub(crate) fn new(
45        cx: &mut JSContext,
46        window: &Window,
47        candidate: DOMString,
48        sdp_m_id: Option<DOMString>,
49        sdp_m_line_index: Option<u16>,
50        username_fragment: Option<DOMString>,
51    ) -> DomRoot<RTCIceCandidate> {
52        Self::new_with_proto(
53            cx,
54            window,
55            None,
56            candidate,
57            sdp_m_id,
58            sdp_m_line_index,
59            username_fragment,
60        )
61    }
62
63    fn new_with_proto(
64        cx: &mut JSContext,
65        window: &Window,
66        proto: Option<HandleObject>,
67        candidate: DOMString,
68        sdp_m_id: Option<DOMString>,
69        sdp_m_line_index: Option<u16>,
70        username_fragment: Option<DOMString>,
71    ) -> DomRoot<RTCIceCandidate> {
72        reflect_dom_object_with_proto(
73            cx,
74            Box::new(RTCIceCandidate::new_inherited(
75                candidate,
76                sdp_m_id,
77                sdp_m_line_index,
78                username_fragment,
79            )),
80            window,
81            proto,
82        )
83    }
84}
85
86impl RTCIceCandidateMethods<crate::DomTypeHolder> for RTCIceCandidate {
87    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-constructor>
88    fn Constructor(
89        cx: &mut JSContext,
90        window: &Window,
91        proto: Option<HandleObject>,
92        config: &RTCIceCandidateInit,
93    ) -> Fallible<DomRoot<RTCIceCandidate>> {
94        if config.sdpMid.is_none() && config.sdpMLineIndex.is_none() {
95            return Err(Error::Type(
96                c"one of sdpMid and sdpMLineIndex must be set".to_owned(),
97            ));
98        }
99        Ok(RTCIceCandidate::new_with_proto(
100            cx,
101            window,
102            proto,
103            config.candidate.clone(),
104            config.sdpMid.clone(),
105            config.sdpMLineIndex,
106            config.usernameFragment.clone(),
107        ))
108    }
109
110    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-candidate>
111    fn Candidate(&self) -> DOMString {
112        self.candidate.clone().root()
113    }
114
115    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-sdpmid>
116    fn GetSdpMid(&self) -> Option<DOMString> {
117        self.sdp_m_id.clone().map(|domstring| domstring.root())
118    }
119
120    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-sdpmlineindex>
121    fn GetSdpMLineIndex(&self) -> Option<u16> {
122        self.sdp_m_line_index
123    }
124
125    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-usernamefragment>
126    fn GetUsernameFragment(&self) -> Option<DOMString> {
127        self.username_fragment
128            .clone()
129            .map(|domstring| domstring.root())
130    }
131
132    /// <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-tojson>
133    fn ToJSON(&self) -> RTCIceCandidateInit {
134        RTCIceCandidateInit {
135            candidate: self.candidate.clone().root(),
136            sdpMid: self.sdp_m_id.clone().map(|domstring| domstring.root()),
137            sdpMLineIndex: self.sdp_m_line_index,
138            usernameFragment: self
139                .username_fragment
140                .clone()
141                .map(|domstring| domstring.root()),
142        }
143    }
144}