aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bluetoothremotegattdescriptor.rs
diff options
context:
space:
mode:
authorAttila Dusnoki <dati91@gmail.com>2016-11-08 13:22:33 +0100
committerAttila Dusnoki <dati91@gmail.com>2016-11-08 13:27:56 +0100
commite7e7c74c9ee1a296683e01b97336ef9bdf7f009c (patch)
treeec5c711cc61bd94f3de4c1394b97ae1174eb5d95 /components/script/dom/bluetoothremotegattdescriptor.rs
parent4a7ea7242935a9bea9ac82f82dd22b5ac8aad673 (diff)
downloadservo-e7e7c74c9ee1a296683e01b97336ef9bdf7f009c.tar.gz
servo-e7e7c74c9ee1a296683e01b97336ef9bdf7f009c.zip
Webbluetooth Async behaviour
Diffstat (limited to 'components/script/dom/bluetoothremotegattdescriptor.rs')
-rw-r--r--components/script/dom/bluetoothremotegattdescriptor.rs109
1 files changed, 55 insertions, 54 deletions
diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs
index 30886baf473..4305b98c3a7 100644
--- a/components/script/dom/bluetoothremotegattdescriptor.rs
+++ b/components/script/dom/bluetoothremotegattdescriptor.rs
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-use bluetooth_traits::BluetoothMethodMsg;
+use bluetooth_traits::{BluetoothRequest, BluetoothResponse};
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
@@ -12,16 +12,16 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
-use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::error::Error::{self, InvalidModification, Network, Security};
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::bluetooth::{AsyncBluetoothListener, response_async};
use dom::bluetoothremotegattcharacteristic::{BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH};
use dom::globalscope::GlobalScope;
use dom::promise::Promise;
-use ipc_channel::ipc::{self, IpcSender};
+use ipc_channel::ipc::IpcSender;
+use js::jsapi::JSContext;
use std::rc::Rc;
// http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor
@@ -60,60 +60,13 @@ impl BluetoothRemoteGATTDescriptor {
BluetoothRemoteGATTDescriptorBinding::Wrap)
}
- fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
+ fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
fn get_instance_id(&self) -> String {
self.instance_id.clone()
}
-
- // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
- fn read_value(&self) -> Fallible<ByteString> {
- if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) {
- return Err(Security)
- }
- let (sender, receiver) = ipc::channel().unwrap();
- if !self.Characteristic().Service().Device().Gatt().Connected() {
- return Err(Network)
- }
- self.get_bluetooth_thread().send(
- BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
- let result = receiver.recv().unwrap();
- let value = match result {
- Ok(val) => {
- ByteString::new(val)
- },
- Err(error) => {
- return Err(Error::from(error))
- },
- };
- *self.value.borrow_mut() = Some(value.clone());
- Ok(value)
- }
-
- // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
- fn write_value(&self, value: Vec<u8>) -> ErrorResult {
- if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) {
- return Err(Security)
- }
- if value.len() > MAXIMUM_ATTRIBUTE_LENGTH {
- return Err(InvalidModification)
- }
- if !self.Characteristic().Service().Device().Gatt().Connected() {
- return Err(Network)
- }
- let (sender, receiver) = ipc::channel().unwrap();
- self.get_bluetooth_thread().send(
- BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap();
- let result = receiver.recv().unwrap();
- match result {
- Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))),
- Err(error) => {
- Err(Error::from(error))
- },
- }
- }
}
impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
@@ -135,12 +88,60 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
fn ReadValue(&self) -> Rc<Promise> {
- result_to_promise(&self.global(), self.read_value())
+ let p = Promise::new(&self.global());
+ let p_cx = p.global().get_cx();
+ if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) {
+ p.reject_error(p_cx, Security);
+ return p;
+ }
+ if !self.Characteristic().Service().Device().Gatt().Connected() {
+ p.reject_error(p_cx, Network);
+ return p;
+ }
+ let sender = response_async(&p, self);
+ self.get_bluetooth_thread().send(
+ BluetoothRequest::ReadValue(self.get_instance_id(), sender)).unwrap();
+ return p;
}
#[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(), self.write_value(value))
+ let p = Promise::new(&self.global());
+ let p_cx = p.global().get_cx();
+ if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) {
+ p.reject_error(p_cx, Security);
+ return p;
+ }
+ if value.len() > MAXIMUM_ATTRIBUTE_LENGTH {
+ p.reject_error(p_cx, InvalidModification);
+ return p;
+ }
+ if !self.Characteristic().Service().Device().Gatt().Connected() {
+ p.reject_error(p_cx, Network);
+ return p;
+ }
+ let sender = response_async(&p, self);
+ self.get_bluetooth_thread().send(
+ BluetoothRequest::WriteValue(self.get_instance_id(), value, sender)).unwrap();
+ return p;
+ }
+}
+
+impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor {
+ fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
+ match response {
+ BluetoothResponse::ReadValue(result) => {
+ let value = ByteString::new(result);
+ *self.value.borrow_mut() = Some(value.clone());
+ promise.resolve_native(promise_cx, &value);
+ },
+ BluetoothResponse::WriteValue(result) => {
+ let value = ByteString::new(result);
+ *self.value.borrow_mut() = Some(value.clone());
+ promise.resolve_native(promise_cx, &value);
+ },
+ _ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
+ }
}
}