servo/
geolocation_delegate.rs1use embedder_traits::{
6 GeolocationError, GeolocationPositionData, GeolocationRequestOptions, GeolocationResult,
7 GeolocationWatchId,
8};
9use servo_base::generic_channel::GenericCallback;
10use servo_config::pref;
11
12use crate::WebView;
13#[cfg(all(feature = "geolocation", target_os = "linux", not(target_env = "ohos")))]
14use crate::geoclue::Geoclue;
15
16pub struct PositionRequest {
19 result_sender: GenericCallback<GeolocationResult>,
20 options: GeolocationRequestOptions,
21 response_sent: bool,
22}
23
24impl PositionRequest {
25 pub(crate) fn new(
26 result_sender: GenericCallback<GeolocationResult>,
27 options: GeolocationRequestOptions,
28 ) -> Self {
29 Self {
30 result_sender,
31 options,
32 response_sent: false,
33 }
34 }
35
36 pub fn options(&self) -> &GeolocationRequestOptions {
38 &self.options
39 }
40
41 pub fn success(mut self, position: GeolocationPositionData) {
43 let _ = self.result_sender.send(Ok(position));
44 self.response_sent = true;
45 }
46
47 pub fn failure(mut self, error: GeolocationError) {
49 let _ = self.result_sender.send(Err(error));
50 self.response_sent = true;
51 }
52}
53
54impl Drop for PositionRequest {
55 fn drop(&mut self) {
56 if !self.response_sent {
57 let _ = self
58 .result_sender
59 .send(Err(GeolocationError::position_unavailable(
60 "No response sent for request.",
61 )));
62 }
63 }
64}
65
66pub struct PositionWatch {
69 id: GeolocationWatchId,
70 result_sender: GenericCallback<GeolocationResult>,
71 options: GeolocationRequestOptions,
72}
73
74impl PositionWatch {
75 pub(crate) fn new(
76 id: GeolocationWatchId,
77 result_sender: GenericCallback<GeolocationResult>,
78 options: GeolocationRequestOptions,
79 ) -> Self {
80 Self {
81 id,
82 result_sender,
83 options,
84 }
85 }
86
87 pub fn id(&self) -> GeolocationWatchId {
89 self.id.clone()
90 }
91
92 pub fn options(&self) -> &GeolocationRequestOptions {
94 &self.options
95 }
96
97 pub fn update(&self, position: GeolocationPositionData) {
99 let _ = self.result_sender.send(Ok(position));
100 }
101
102 pub fn error(&self, error: GeolocationError) {
105 let _ = self.result_sender.send(Err(error));
106 }
107}
108
109pub trait GeolocationDelegate {
111 fn request_position(&self, _webview: WebView, request: PositionRequest) {
113 match default_position_response() {
114 Ok(position) => request.success(position),
115 Err(error) => request.failure(error),
116 }
117 }
118
119 fn start_watch(&self, _webview: WebView, watch: PositionWatch) {
121 match default_position_response() {
122 Ok(position) => watch.update(position),
123 Err(error) => watch.error(error),
124 }
125 }
126
127 fn stop_watch(&self, _webview: WebView, _id: &GeolocationWatchId) {}
130}
131
132fn default_position_response() -> GeolocationResult {
135 if !pref!(dom_geolocation_test_position_enabled) {
136 return Err(GeolocationError::position_unavailable(
137 "No geolocation backend is available.",
138 ));
139 }
140
141 Ok(GeolocationPositionData {
142 accuracy: pref!(dom_geolocation_test_accuracy),
143 latitude: pref!(dom_geolocation_test_latitude),
144 longitude: pref!(dom_geolocation_test_longitude),
145 altitude: None,
146 altitude_accuracy: None,
147 heading: Some(pref!(dom_geolocation_test_heading)),
148 speed: Some(pref!(dom_geolocation_test_speed)),
149 })
150}
151
152#[derive(Default)]
155pub(crate) struct DefaultGeolocationDelegate {}
156
157#[cfg(all(feature = "geolocation", target_os = "linux", not(target_env = "ohos")))]
158impl DefaultGeolocationDelegate {
159 fn backend(&self) -> Option<&Geoclue> {
162 if pref!(dom_geolocation_test_position_enabled) {
163 return None;
164 }
165
166 Geoclue::singleton()
167 }
168}
169
170impl GeolocationDelegate for DefaultGeolocationDelegate {
171 fn request_position(&self, _webview: WebView, request: PositionRequest) {
172 #[cfg(all(feature = "geolocation", target_os = "linux", not(target_env = "ohos")))]
173 if let Some(geoclue) = self.backend() {
174 return geoclue.request_position(request);
175 }
176 match default_position_response() {
177 Ok(position) => request.success(position),
178 Err(error) => request.failure(error),
179 }
180 }
181
182 fn start_watch(&self, _webview: WebView, watch: PositionWatch) {
183 #[cfg(all(feature = "geolocation", target_os = "linux", not(target_env = "ohos")))]
184 if let Some(geoclue) = self.backend() {
185 return geoclue.start_watch(watch);
186 }
187 match default_position_response() {
188 Ok(position) => watch.update(position),
189 Err(error) => watch.error(error),
190 }
191 }
192
193 fn stop_watch(&self, _webview: WebView, _id: &GeolocationWatchId) {
194 #[cfg(all(feature = "geolocation", target_os = "linux", not(target_env = "ohos")))]
195 if let Some(geoclue) = self.backend() {
196 geoclue.stop_watch(_id);
197 }
198 }
199}