diff options
Diffstat (limited to 'components/script/dom/bluetooth.rs')
-rw-r--r-- | components/script/dom/bluetooth.rs | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 2940a211bf3..eecaf6489be 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -15,10 +15,13 @@ use dom::bindings::str::DOMString; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; +use js::conversions::ToJSValConvertible; use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; use net_traits::bluetooth_thread::{BluetoothError, BluetoothMethodMsg}; +use std::rc::Rc; const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices."; const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way."; @@ -61,6 +64,22 @@ impl Bluetooth { global_ref.as_window().bluetooth_thread() } + fn request_device(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> { + // Step 1. + // TODO(#4282): Reject promise. + if (option.filters.is_some() && option.acceptAllDevices) || + (option.filters.is_none() && !option.acceptAllDevices) { + return Err(Type(OPTIONS_ERROR.to_owned())); + } + // Step 2. + if !option.acceptAllDevices { + return self.request_bluetooth_devices(&option.filters, &option.optionalServices); + } + + self.request_bluetooth_devices(&None, &option.optionalServices) + // TODO(#4282): Step 3-5: Reject and resolve promise. + } + // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices fn request_bluetooth_devices(&self, filters: &Option<Vec<BluetoothRequestDeviceFilter>>, @@ -252,6 +271,18 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) service_data_uuid)) } +#[allow(unrooted_must_root)] +pub fn result_to_promise<T: ToJSValConvertible>(global_ref: GlobalRef, + bluetooth_result: Fallible<T>) + -> Rc<Promise> { + let p = Promise::new(global_ref); + match bluetooth_result { + Ok(v) => p.resolve_native(p.global().r().get_cx(), &v), + Err(e) => p.reject_error(p.global().r().get_cx(), e), + } + p +} + impl From<BluetoothError> for Error { fn from(error: BluetoothError) -> Self { match error { @@ -265,20 +296,9 @@ impl From<BluetoothError> for Error { } impl BluetoothMethods for Bluetooth { + #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice - fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> { - // Step 1. - // TODO(#4282): Reject promise. - if (option.filters.is_some() && option.acceptAllDevices) || - (option.filters.is_none() && !option.acceptAllDevices) { - return Err(Type(OPTIONS_ERROR.to_owned())); - } - // Step 2. - if !option.acceptAllDevices { - return self.request_bluetooth_devices(&option.filters, &option.optionalServices); - } - - self.request_bluetooth_devices(&None, &option.optionalServices) - // TODO(#4282): Step 3-5: Reject and resolve promise. + fn RequestDevice(&self, option: &RequestDeviceOptions) -> Rc<Promise> { + result_to_promise(self.global().r(), self.request_device(option)) } } |