Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update WebBluetooth to use Promises
  • Loading branch information
mmatyas authored and dati91 committed Sep 26, 2016
1 parent fb52bb7 commit e05a839
Show file tree
Hide file tree
Showing 32 changed files with 684 additions and 458 deletions.
48 changes: 34 additions & 14 deletions components/script/dom/bluetooth.rs
Expand Up @@ -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.";
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
}
85 changes: 57 additions & 28 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Expand Up @@ -18,12 +18,15 @@ use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::{ByteString, DOMString};
use dom::bluetooth::result_to_promise;
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID};
use dom::promise::Promise;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use std::rc::Rc;

// Maximum length of an attribute value.
// https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 2169)
Expand Down Expand Up @@ -79,26 +82,9 @@ impl BluetoothRemoteGATTCharacteristic {
fn get_instance_id(&self) -> String {
self.instance_id.clone()
}
}

impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-properties
fn Properties(&self) -> Root<BluetoothCharacteristicProperties> {
self.properties.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-service
fn Service(&self) -> Root<BluetoothRemoteGATTService> {
self.service.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-uuid
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
fn get_descriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string();
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security)
Expand All @@ -121,9 +107,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
fn GetDescriptors(&self,
descriptor: Option<BluetoothDescriptorUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
fn get_descriptors(&self,
descriptor: Option<BluetoothDescriptorUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let mut uuid: Option<String> = None;
if let Some(d) = descriptor {
uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string());
Expand Down Expand Up @@ -152,13 +138,8 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue
fn ReadValue(&self) -> Fallible<ByteString> {
fn read_value(&self) -> Fallible<ByteString> {
if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) {
return Err(Security)
}
Expand All @@ -185,7 +166,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
fn write_value(&self, value: Vec<u8>) -> ErrorResult {
if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) {
return Err(Security)
}
Expand Down Expand Up @@ -213,3 +194,51 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}
}
}

impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-properties
fn Properties(&self) -> Root<BluetoothCharacteristicProperties> {
self.properties.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-service
fn Service(&self) -> Root<BluetoothRemoteGATTService> {
self.service.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-uuid
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc<Promise> {
result_to_promise(self.global().r(), self.get_descriptor(descriptor))
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
fn GetDescriptors(&self,
descriptor: Option<BluetoothDescriptorUUID>)
-> Rc<Promise> {
result_to_promise(self.global().r(), self.get_descriptors(descriptor))
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue
fn ReadValue(&self) -> Rc<Promise> {
result_to_promise(self.global().r(), self.read_value())
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) -> Rc<Promise> {
result_to_promise(self.global().r(), self.write_value(value))
}
}
53 changes: 34 additions & 19 deletions components/script/dom/bluetoothremotegattdescriptor.rs
Expand Up @@ -17,9 +17,12 @@ use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::{ByteString, DOMString};
use dom::bluetooth::result_to_promise;
use dom::bluetoothremotegattcharacteristic::{BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH};
use dom::promise::Promise;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use std::rc::Rc;

// http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor
#[dom_struct]
Expand Down Expand Up @@ -66,26 +69,9 @@ impl BluetoothRemoteGATTDescriptor {
fn get_instance_id(&self) -> String {
self.instance_id.clone()
}
}

impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-characteristic
fn Characteristic(&self) -> Root<BluetoothRemoteGATTCharacteristic> {
self.characteristic.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-uuid
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
fn ReadValue(&self) -> Fallible<ByteString> {
fn read_value(&self) -> Fallible<ByteString> {
if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) {
return Err(Security)
}
Expand All @@ -109,7 +95,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
fn write_value(&self, value: Vec<u8>) -> ErrorResult {
if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) {
return Err(Security)
}
Expand All @@ -131,3 +117,32 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}
}
}

impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-characteristic
fn Characteristic(&self) -> Root<BluetoothRemoteGATTCharacteristic> {
self.characteristic.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-uuid
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
fn ReadValue(&self) -> Rc<Promise> {
result_to_promise(self.global().r(), self.read_value())
}

#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
fn WriteValue(&self, value: Vec<u8>) -> Rc<Promise> {
result_to_promise(self.global().r(), self.write_value(value))
}
}

0 comments on commit e05a839

Please sign in to comment.