use embedder_traits::{
GeolocationError, GeolocationPositionData, GeolocationRequestOptions, GeolocationResult,
GeolocationWatchId,
};
use servo_base::generic_channel::GenericCallback;
use servo_config::pref;
use crate::WebView;
pub struct PositionRequest {
result_sender: GenericCallback<GeolocationResult>,
options: GeolocationRequestOptions,
response_sent: bool,
}
impl PositionRequest {
pub(crate) fn new(
result_sender: GenericCallback<GeolocationResult>,
options: GeolocationRequestOptions,
) -> Self {
Self {
result_sender,
options,
response_sent: false,
}
}
pub fn options(&self) -> &GeolocationRequestOptions {
&self.options
}
pub fn success(mut self, position: GeolocationPositionData) {
let _ = self.result_sender.send(Ok(position));
self.response_sent = true;
}
pub fn failure(mut self, error: GeolocationError) {
let _ = self.result_sender.send(Err(error));
self.response_sent = true;
}
}
impl Drop for PositionRequest {
fn drop(&mut self) {
if !self.response_sent {
let _ = self
.result_sender
.send(Err(GeolocationError::position_unavailable(
"No response sent for request.",
)));
}
}
}
pub struct PositionWatch {
id: GeolocationWatchId,
result_sender: GenericCallback<GeolocationResult>,
options: GeolocationRequestOptions,
}
impl PositionWatch {
pub(crate) fn new(
id: GeolocationWatchId,
result_sender: GenericCallback<GeolocationResult>,
options: GeolocationRequestOptions,
) -> Self {
Self {
id,
result_sender,
options,
}
}
pub fn id(&self) -> GeolocationWatchId {
self.id.clone()
}
pub fn options(&self) -> &GeolocationRequestOptions {
&self.options
}
pub fn update(&self, position: GeolocationPositionData) {
let _ = self.result_sender.send(Ok(position));
}
pub fn error(&self, error: GeolocationError) {
let _ = self.result_sender.send(Err(error));
}
}
pub trait GeolocationDelegate {
fn request_position(&self, _webview: WebView, request: PositionRequest) {
match default_position_response() {
Ok(position) => request.success(position),
Err(error) => request.failure(error),
}
}
fn start_watch(&self, _webview: WebView, watch: PositionWatch) {
match default_position_response() {
Ok(position) => watch.update(position),
Err(error) => watch.error(error),
}
}
fn stop_watch(&self, _webview: WebView, _id: &GeolocationWatchId) {}
}
fn default_position_response() -> GeolocationResult {
if !pref!(dom_geolocation_test_position_enabled) {
return Err(GeolocationError::position_unavailable(
"No geolocation backend is available.",
));
}
Ok(GeolocationPositionData {
accuracy: pref!(dom_geolocation_test_accuracy),
latitude: pref!(dom_geolocation_test_latitude),
longitude: pref!(dom_geolocation_test_longitude),
altitude: None,
altitude_accuracy: None,
heading: Some(pref!(dom_geolocation_test_heading)),
speed: Some(pref!(dom_geolocation_test_speed)),
})
}
pub(crate) struct DefaultGeolocationDelegate;
impl GeolocationDelegate for DefaultGeolocationDelegate {}