script/dom/geolocation/
geolocation.rs1use std::cell::{Cell, RefCell};
5use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use rustc_hash::FxHashSet;
9use script_bindings::codegen::GenericBindings::GeolocationBinding::Geolocation_Binding::GeolocationMethods;
10use script_bindings::codegen::GenericBindings::GeolocationBinding::{
11 PositionCallback, PositionOptions,
12};
13use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
14use script_bindings::reflector::Reflector;
15use script_bindings::root::DomRoot;
16use script_bindings::script_runtime::CanGc;
17
18use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
19use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
20use crate::dom::globalscope::GlobalScope;
21
22#[dom_struct]
23pub struct Geolocation {
24 reflector_: Reflector,
25 watch_ids: RefCell<FxHashSet<u32>>,
27 next_watch_id: Cell<u32>,
28}
29
30impl Geolocation {
31 fn new_inherited() -> Self {
32 Geolocation {
33 reflector_: Reflector::new(),
34 watch_ids: RefCell::new(FxHashSet::default()),
35 next_watch_id: Cell::new(1),
36 }
37 }
38
39 pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<Self> {
40 reflect_dom_object(Box::new(Self::new_inherited()), global, can_gc)
41 }
42}
43
44impl GeolocationMethods<DomTypeHolder> for Geolocation {
45 fn GetCurrentPosition(
47 &self,
48 _success_callback: Rc<PositionCallback<DomTypeHolder>>,
49 _options: &PositionOptions,
50 ) {
51 }
60
61 fn WatchPosition(
63 &self,
64 _success_callback: Rc<PositionCallback<DomTypeHolder>>,
65 _options: &PositionOptions,
66 ) -> i32 {
67 if !self.global().as_window().Document().is_active() {
69 return 0;
72 }
73 let watch_id = self.next_watch_id.get();
75 self.next_watch_id.set(watch_id + 1);
76 self.watch_ids.borrow_mut().insert(watch_id);
78 watch_id as i32
82 }
83
84 fn ClearWatch(&self, watch_id: i32) {
86 let watch_id = u32::try_from(watch_id).ok();
87 if let Some(id) = watch_id {
88 self.watch_ids.borrow_mut().remove(&id);
89 }
90 }
91}