mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Update WebBluetooth to use Promises
This commit is contained in:
parent
fb52bb7c8d
commit
e05a839d25
32 changed files with 684 additions and 458 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue