Skip to main content

script/dom/geolocation/
geolocationposition.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::GeolocationPositionData;
7use js::context::JSContext;
8use script_bindings::codegen::GenericBindings::GeolocationPositionBinding::GeolocationPositionMethods;
9use script_bindings::reflector::{Reflector, reflect_dom_object};
10use script_bindings::root::{Dom, DomRoot};
11
12use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
13use crate::dom::geolocationcoordinates::GeolocationCoordinates;
14use crate::dom::globalscope::GlobalScope;
15
16#[dom_struct]
17pub struct GeolocationPosition {
18    reflector_: Reflector,
19    coords: Dom<GeolocationCoordinates>,
20    timestamp: u64,
21    /// <https://www.w3.org/TR/geolocation/#dfn-ishighaccuracy>
22    is_high_accuracy: bool,
23}
24
25impl GeolocationPosition {
26    fn new_inherited(
27        coords: &GeolocationCoordinates,
28        timestamp: u64,
29        is_high_accuracy: bool,
30    ) -> Self {
31        GeolocationPosition {
32            reflector_: Reflector::new(),
33            coords: Dom::from_ref(coords),
34            timestamp,
35            is_high_accuracy,
36        }
37    }
38
39    /// <https://www.w3.org/TR/geolocation/#dfn-a-new-geolocationposition>
40    ///
41    /// Returns `None` if `data` contains a value that cannot be represented as an unrestricted
42    /// double, in which case the caller should report `POSITION_UNAVAILABLE`.
43    pub(crate) fn from_position_data(
44        cx: &mut JSContext,
45        global: &GlobalScope,
46        data: &GeolocationPositionData,
47        timestamp: u64,
48        is_high_accuracy: bool,
49    ) -> Option<DomRoot<Self>> {
50        // Step 1 and 2. Create the coordinates from positionData.
51        let coords = GeolocationCoordinates::from_position_data(cx, global, data)?;
52        // Step 3. Return a newly created GeolocationPosition.
53        Some(reflect_dom_object(
54            cx,
55            Box::new(Self::new_inherited(&coords, timestamp, is_high_accuracy)),
56            global,
57        ))
58    }
59
60    /// <https://www.w3.org/TR/geolocation/#dfn-ishighaccuracy>
61    pub(crate) fn is_high_accuracy(&self) -> bool {
62        self.is_high_accuracy
63    }
64
65    /// <https://www.w3.org/TR/geolocation/#dom-geolocationposition-timestamp>
66    pub(crate) fn timestamp(&self) -> u64 {
67        self.timestamp
68    }
69}
70
71impl GeolocationPositionMethods<DomTypeHolder> for GeolocationPosition {
72    fn Coords(&self) -> DomRoot<GeolocationCoordinates> {
73        DomRoot::from_ref(&*self.coords.clone())
74    }
75
76    fn Timestamp(&self) -> u64 {
77        self.timestamp
78    }
79}