Skip to main content

script/dom/geolocation/
geolocationpositionerror.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 embedder_traits::{GeolocationError, GeolocationErrorKind};
7use js::context::JSContext;
8use script_bindings::codegen::GenericBindings::GeolocationPositionErrorBinding::GeolocationPositionErrorConstants::{PERMISSION_DENIED, POSITION_UNAVAILABLE, TIMEOUT};
9use script_bindings::codegen::GenericBindings::GeolocationPositionErrorBinding::GeolocationPositionErrorMethods;
10use script_bindings::domstring::TracedDOMString;
11use script_bindings::reflector::{Reflector, reflect_dom_object};
12use script_bindings::root::DomRoot;
13use script_bindings::str::DOMString;
14
15use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
16use crate::dom::globalscope::GlobalScope;
17
18#[dom_struct]
19pub struct GeolocationPositionError {
20    reflector_: Reflector,
21    code: u16,
22    message: TracedDOMString,
23}
24
25impl GeolocationPositionError {
26    fn new_inherited(code: u16, message: DOMString) -> Self {
27        GeolocationPositionError {
28            reflector_: Reflector::new(),
29            code,
30            message: message.traced(),
31        }
32    }
33
34    fn new(
35        cx: &mut JSContext,
36        global: &GlobalScope,
37        code: u16,
38        message: DOMString,
39    ) -> DomRoot<Self> {
40        reflect_dom_object(cx, Box::new(Self::new_inherited(code, message)), global)
41    }
42
43    pub(crate) fn permission_denied(
44        cx: &mut JSContext,
45        global: &GlobalScope,
46        message: DOMString,
47    ) -> DomRoot<Self> {
48        Self::new(cx, global, PERMISSION_DENIED, message)
49    }
50
51    pub(crate) fn position_unavailable(
52        cx: &mut JSContext,
53        global: &GlobalScope,
54        message: DOMString,
55    ) -> DomRoot<Self> {
56        Self::new(cx, global, POSITION_UNAVAILABLE, message)
57    }
58
59    pub(crate) fn timeout(
60        cx: &mut JSContext,
61        global: &GlobalScope,
62        message: DOMString,
63    ) -> DomRoot<Self> {
64        Self::new(cx, global, TIMEOUT, message)
65    }
66
67    /// Create the error described by a GeolocationError reported by the embedding layer.
68    pub(crate) fn from_embedder_error(
69        cx: &mut JSContext,
70        global: &GlobalScope,
71        error: &GeolocationError,
72    ) -> DomRoot<Self> {
73        let code = match error.kind {
74            GeolocationErrorKind::PermissionDenied => PERMISSION_DENIED,
75            GeolocationErrorKind::PositionUnavailable => POSITION_UNAVAILABLE,
76            GeolocationErrorKind::Timeout => TIMEOUT,
77        };
78        Self::new(cx, global, code, DOMString::from(error.message.clone()))
79    }
80}
81
82impl GeolocationPositionErrorMethods<DomTypeHolder> for GeolocationPositionError {
83    fn Code(&self) -> u16 {
84        self.code
85    }
86
87    fn Message(&self) -> DOMString {
88        self.message.clone().root()
89    }
90}