diff options
Diffstat (limited to 'components/script/dom/bluetoothremotegattdescriptor.rs')
-rw-r--r-- | components/script/dom/bluetoothremotegattdescriptor.rs | 152 |
1 files changed, 88 insertions, 64 deletions
diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index c9e24c499be..39e298f923c 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -1,63 +1,69 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * 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/. */ - + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::BluetoothRemoteGATTCharacteristicMethods; +use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods; +use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; +use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; +use crate::dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer; +use crate::dom::bindings::error::Error::{self, InvalidModification, Network, Security}; +use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::{ByteString, DOMString}; +use crate::dom::bluetooth::{response_async, AsyncBluetoothListener}; +use crate::dom::bluetoothremotegattcharacteristic::{ + BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH, +}; +use crate::dom::globalscope::GlobalScope; +use crate::dom::promise::Promise; +use crate::realms::InRealm; +use bluetooth_traits::blocklist::{uuid_is_blocklisted, Blocklist}; use bluetooth_traits::{BluetoothRequest, BluetoothResponse}; -use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted}; -use dom::bindings::cell::DOMRefCell; -use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding:: - BluetoothRemoteGATTCharacteristicMethods; -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::Error::{self, InvalidModification, Network, Security}; -use dom::bindings::js::{JS, Root}; -use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::bindings::str::{ByteString, DOMString}; -use dom::bluetooth::{AsyncBluetoothListener, response_async}; -use dom::bluetoothremotegattcharacteristic::{BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH}; -use dom::globalscope::GlobalScope; -use dom::promise::Promise; use dom_struct::dom_struct; use ipc_channel::ipc::IpcSender; -use js::jsapi::JSContext; use std::rc::Rc; // http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor #[dom_struct] pub struct BluetoothRemoteGATTDescriptor { reflector_: Reflector, - characteristic: JS<BluetoothRemoteGATTCharacteristic>, + characteristic: Dom<BluetoothRemoteGATTCharacteristic>, uuid: DOMString, - value: DOMRefCell<Option<ByteString>>, + value: DomRefCell<Option<ByteString>>, instance_id: String, } impl BluetoothRemoteGATTDescriptor { - pub fn new_inherited(characteristic: &BluetoothRemoteGATTCharacteristic, - uuid: DOMString, - instance_id: String) - -> BluetoothRemoteGATTDescriptor { + pub fn new_inherited( + characteristic: &BluetoothRemoteGATTCharacteristic, + uuid: DOMString, + instance_id: String, + ) -> BluetoothRemoteGATTDescriptor { BluetoothRemoteGATTDescriptor { reflector_: Reflector::new(), - characteristic: JS::from_ref(characteristic), + characteristic: Dom::from_ref(characteristic), uuid: uuid, - value: DOMRefCell::new(None), + value: DomRefCell::new(None), instance_id: instance_id, } } - pub fn new(global: &GlobalScope, - characteristic: &BluetoothRemoteGATTCharacteristic, - uuid: DOMString, - instanceID: String) - -> Root<BluetoothRemoteGATTDescriptor>{ - reflect_dom_object(box BluetoothRemoteGATTDescriptor::new_inherited(characteristic, - uuid, - instanceID), - global, - BluetoothRemoteGATTDescriptorBinding::Wrap) + pub fn new( + global: &GlobalScope, + characteristic: &BluetoothRemoteGATTCharacteristic, + uuid: DOMString, + instance_id: String, + ) -> DomRoot<BluetoothRemoteGATTDescriptor> { + reflect_dom_object( + Box::new(BluetoothRemoteGATTDescriptor::new_inherited( + characteristic, + uuid, + instance_id, + )), + global, + ) } fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> { @@ -71,8 +77,8 @@ impl BluetoothRemoteGATTDescriptor { impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-characteristic - fn Characteristic(&self) -> Root<BluetoothRemoteGATTCharacteristic> { - Root::from_ref(&self.characteristic) + fn Characteristic(&self) -> DomRoot<BluetoothRemoteGATTCharacteristic> { + DomRoot::from_ref(&self.characteristic) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-uuid @@ -80,26 +86,30 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { self.uuid.clone() } - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value + // 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> { - let p = Promise::new(&self.global()); - let p_cx = p.global().get_cx(); + fn ReadValue(&self, comp: InRealm) -> Rc<Promise> { + let p = Promise::new_in_current_realm(&self.global(), comp); // Step 1. if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) { - p.reject_error(p_cx, Security); + p.reject_error(Security); return p; } // Step 2. - if !self.Characteristic().Service().Device().get_gatt().Connected() { - p.reject_error(p_cx, Network); + if !self + .Characteristic() + .Service() + .Device() + .get_gatt() + .Connected() + { + p.reject_error(Network); return p; } @@ -107,32 +117,41 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // Note: Steps 3 - 4 and substeps of Step 5 are implemented in components/bluetooth/lib.rs // in readValue function and in handle_response function. let sender = response_async(&p, self); - self.get_bluetooth_thread().send( - BluetoothRequest::ReadValue(self.get_instance_id(), sender)).unwrap(); + 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> { - let p = Promise::new(&self.global()); - let p_cx = p.global().get_cx(); + fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer, comp: InRealm) -> Rc<Promise> { + let p = Promise::new_in_current_realm(&self.global(), comp); // Step 1. if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Writes) { - p.reject_error(p_cx, Security); + p.reject_error(Security); return p; } // Step 2 - 3. - if value.len() > MAXIMUM_ATTRIBUTE_LENGTH { - p.reject_error(p_cx, InvalidModification); + let vec = match value { + ArrayBufferViewOrArrayBuffer::ArrayBufferView(avb) => avb.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(ab) => ab.to_vec(), + }; + if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH { + p.reject_error(InvalidModification); return p; } // Step 4. - if !self.Characteristic().Service().Device().get_gatt().Connected() { - p.reject_error(p_cx, Network); + if !self + .Characteristic() + .Service() + .Device() + .get_gatt() + .Connected() + { + p.reject_error(Network); return p; } @@ -140,14 +159,19 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // Note: Steps 5 - 6 and substeps of Step 7 are implemented in components/bluetooth/lib.rs // in writeValue function and in handle_response function. let sender = response_async(&p, self); - self.get_bluetooth_thread().send( - BluetoothRequest::WriteValue(self.get_instance_id(), value, sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothRequest::WriteValue( + self.get_instance_id(), + vec, + sender, + )) + .unwrap(); return p; } } impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor { - fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) { + fn handle_response(&self, response: BluetoothResponse, promise: &Rc<Promise>) { match response { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue BluetoothResponse::ReadValue(result) => { @@ -159,7 +183,7 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor { *self.value.borrow_mut() = Some(value.clone()); // Step 5.4.3. - promise.resolve_native(promise_cx, &value); + promise.resolve_native(&value); }, // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue BluetoothResponse::WriteValue(result) => { @@ -171,9 +195,9 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor { // Step 7.4.3. // TODO: Resolve promise with undefined instead of a value. - promise.resolve_native(promise_cx, &()); + promise.resolve_native(&()); }, - _ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())), + _ => promise.reject_error(Error::Type("Something went wrong...".to_owned())), } } } |