aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bluetooth.rs
diff options
context:
space:
mode:
authorMátyás Mustoha <mmatyas@inf.u-szeged.hu>2016-09-15 10:44:17 +0200
committerAttila Dusnoki <dati91@gmail.com>2016-09-26 20:02:42 +0200
commite05a839d25377c9bb0f209f154f0ea998ddc2151 (patch)
tree7ee1246109c20cf9a8db6dd73c5ad6b526ee4a47 /components/script/dom/bluetooth.rs
parentfb52bb7c8d70f0d7b85bb35d89779d83a25f2bc2 (diff)
downloadservo-e05a839d25377c9bb0f209f154f0ea998ddc2151.tar.gz
servo-e05a839d25377c9bb0f209f154f0ea998ddc2151.zip
Update WebBluetooth to use Promises
Diffstat (limited to 'components/script/dom/bluetooth.rs')
-rw-r--r--components/script/dom/bluetooth.rs48
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))
}
}