Step annotations for WebBluetooth functions

This commit is contained in:
Zakor Gyula 2016-11-24 15:35:08 +01:00 committed by Attila Dusnoki
parent 89c4219a36
commit 8dd100f74f
7 changed files with 367 additions and 34 deletions

View file

@ -65,19 +65,42 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
fn Connect(&self) -> Rc<Promise> {
// Step 1.
let p = Promise::new(&self.global());
let sender = response_async(&p, self);
// TODO: Step 2: Implement representedDevice internal slot for BluetoothDevice.
// TODO: Step 3: Check if the UA is currently using the Bluetooth system.
// TODO: Step 4: Implement activeAlgorithms internal slot for BluetoothRemoteGATTServer.
// TODO: Step 5.1 - 5.2: Implement activeAlgorithms, representedDevice internal slots
// and the` garbage-collect the connection` algorithm.
// Note: Steps 5.1.1 and 5.1.3 are in components/bluetooth/lib.rs in the gatt_server_connect function.
// Steps 5.2.4 - 5.2.5 are in response function.
self.get_bluetooth_thread().send(
BluetoothRequest::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap();
// Step 5: return promise.
return p;
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect
fn Disconnect(&self) -> ErrorResult {
// TODO: Step 1: Implement activeAlgorithms internal slot for BluetoothRemoteGATTServer.
// TODO: Step 2: Check if this.connected is false here too.
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothRequest::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap();
let server = receiver.recv().unwrap();
// TODO: Step 3: Implement the `clean up the disconnected device` algorithm.
// TODO: Step 4: Implement representedDevice internal slot for BluetoothDevice.
// TODO: Step 5: Implement the `garbage-collect the connection` algorithm.
match server {
Ok(connected) => {
self.connected.set(connected);
@ -92,8 +115,12 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Rc<Promise> {
// TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot.
// Subsequent steps are relative to https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
// Step 1.
let uuid = match BluetoothUUID::service(service) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
@ -101,14 +128,23 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
return p;
}
};
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// TODO: Step 5: Implement representedDevice internal slot for BluetoothDevice.
// Note: Steps 6 - 7 are implemented in components/bluetooth/lib.rs in get_primary_service function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
@ -118,10 +154,14 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self, service: Option<BluetoothServiceUUID>) -> Rc<Promise> {
// TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot.
// Subsequent steps are relative to https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let mut uuid: Option<String> = None;
if let Some(s) = service {
// Step 1.
uuid = match BluetoothUUID::service(s) {
Ok(uuid) => Some(uuid.to_string()),
Err(e) => {
@ -130,16 +170,24 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}
};
if let Some(ref uuid) = uuid {
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// TODO: Step 5: Implement representedDevice internal slot for BluetoothDevice.
// Note: Steps 6 - 7 are implemented in components/bluetooth/lib.rs in get_primary_services function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
@ -150,10 +198,17 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
match response {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
BluetoothResponse::GATTServerConnect(connected) => {
// Step 5.2.4.
self.connected.set(connected);
// Step 5.2.5.
promise.resolve_native(promise_cx, self);
},
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7.
BluetoothResponse::GetPrimaryService(service) => {
let context = self.device.get().get_context();
let mut service_map = context.get_service_map().borrow_mut();
@ -168,6 +223,9 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
service_map.insert(service.instance_id, MutHeap::new(&bt_service));
promise.resolve_native(promise_cx, &bt_service);
},
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7.
BluetoothResponse::GetPrimaryServices(services_vec) => {
let mut services = vec!();
let context = self.device.get().get_context();