Implement GetAvailability for Bluetooth

This commit is contained in:
Attila Dusnoki 2017-02-03 15:31:24 +01:00
parent 8ca1383c41
commit ca7aa6bff8
9 changed files with 98 additions and 9 deletions

View file

@ -246,13 +246,16 @@ impl BluetoothManager {
},
BluetoothRequest::Test(data_set_name, sender) => {
let _ = sender.send(self.test(data_set_name));
}
},
BluetoothRequest::SetRepresentedToNull(service_ids, characteristic_ids, descriptor_ids) => {
self.remove_ids_from_caches(service_ids, characteristic_ids, descriptor_ids)
}
},
BluetoothRequest::IsRepresentedDeviceNull(id, sender) => {
let _ = sender.send(!self.device_is_cached(&id));
}
},
BluetoothRequest::GetAvailability(sender) => {
let _ = sender.send(self.get_availability());
},
BluetoothRequest::Exit => {
break
},
@ -924,4 +927,9 @@ impl BluetoothManager {
// TODO: Implement this when supported in lower level
return Err(BluetoothError::NotSupported);
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
fn get_availability(&mut self) -> BluetoothResponseResult {
Ok(BluetoothResponse::GetAvailability(self.get_adapter().is_ok()))
}
}

View file

@ -91,6 +91,7 @@ pub enum BluetoothRequest {
WatchAdvertisements(String, IpcSender<BluetoothResponseResult>),
SetRepresentedToNull(Vec<String>, Vec<String>, Vec<String>),
IsRepresentedDeviceNull(String, IpcSender<bool>),
GetAvailability(IpcSender<BluetoothResponseResult>),
Test(String, IpcSender<BluetoothResult<()>>),
Exit,
}
@ -107,4 +108,5 @@ pub enum BluetoothResponse {
WriteValue(Vec<u8>),
EnableNotification(()),
WatchAdvertisements(()),
GetAvailability(bool),
}

View file

@ -443,6 +443,18 @@ impl BluetoothMethods for Bluetooth {
return p;
}
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
fn GetAvailability(&self) -> Rc<Promise> {
let p = Promise::new(&self.global());
// Step 1. We did not override the method
// Step 2 - 3. in handle_response
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetAvailability(sender)).unwrap();
p
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-onavailabilitychanged
event_handler!(availabilitychanged, GetOnavailabilitychanged, SetOnavailabilitychanged);
}
@ -466,6 +478,11 @@ impl AsyncBluetoothListener for Bluetooth {
// Step 5.
promise.resolve_native(promise_cx, &bt_device);
},
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
// Step 2 - 3.
BluetoothResponse::GetAvailability(is_available) => {
promise.resolve_native(promise_cx, &is_available);
}
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
}
}

View file

@ -29,8 +29,8 @@ dictionary RequestDeviceOptions {
[Pref="dom.bluetooth.enabled"]
interface Bluetooth : EventTarget {
// [SecureContext]
// Promise<boolean> getAvailability();
[SecureContext]
Promise<boolean> getAvailability();
[SecureContext]
attribute EventHandler onavailabilitychanged;
// [SecureContext, SameObject]