diff options
author | yvt <i@yvt.jp> | 2021-07-10 17:24:27 +0900 |
---|---|---|
committer | yvt <i@yvt.jp> | 2021-07-10 17:55:42 +0900 |
commit | 01a7de50ab1843d85295f9dccad7f4c099e7208c (patch) | |
tree | ee53fb6e8889deb7b880ee969e6c662e6128d210 /components/script/dom/bluetoothremotegattcharacteristic.rs | |
parent | ff8d2cdbbfc7a9dc7f38b7dd47cb350fde39388f (diff) | |
parent | 94b613fbdaa2b98f2179fc0bbda13c64e6fa0d38 (diff) | |
download | servo-01a7de50ab1843d85295f9dccad7f4c099e7208c.tar.gz servo-01a7de50ab1843d85295f9dccad7f4c099e7208c.zip |
Merge remote-tracking branch 'upstream/master' into feat-cow-infra
`tests/wpt/web-platform-tests/html/browsers/origin/cross-origin-objects/cross-origin-objects.html`
was reverted to the upstream version.
Diffstat (limited to 'components/script/dom/bluetoothremotegattcharacteristic.rs')
-rw-r--r-- | components/script/dom/bluetoothremotegattcharacteristic.rs | 257 |
1 files changed, 144 insertions, 113 deletions
diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index d6a39c154e3..fd01653c84d 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -1,33 +1,32 @@ /* 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::BluetoothCharacteristicPropertiesBinding::BluetoothCharacteristicPropertiesMethods; +use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::BluetoothRemoteGATTCharacteristicMethods; +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, NotSupported, Security, +}; +use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::reflector::{reflect_dom_object, DomObject}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::{ByteString, DOMString}; +use crate::dom::bluetooth::{get_gatt_children, response_async, AsyncBluetoothListener}; +use crate::dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; +use crate::dom::bluetoothremotegattservice::BluetoothRemoteGATTService; +use crate::dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; +use crate::dom::eventtarget::EventTarget; +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, GATTType}; -use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted}; -use dom::bindings::cell::DOMRefCell; -use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding:: - BluetoothCharacteristicPropertiesMethods; -use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding; -use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding:: - BluetoothRemoteGATTCharacteristicMethods; -use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; -use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; -use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; -use dom::bindings::error::Error::{self, InvalidModification, Network, NotSupported, Security}; -use dom::bindings::inheritance::Castable; -use dom::bindings::js::{JS, Root}; -use dom::bindings::reflector::{DomObject, reflect_dom_object}; -use dom::bindings::str::{ByteString, DOMString}; -use dom::bluetooth::{AsyncBluetoothListener, get_gatt_children, response_async}; -use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; -use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; -use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; -use dom::eventtarget::EventTarget; -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; // Maximum length of an attribute value. @@ -38,41 +37,46 @@ pub const MAXIMUM_ATTRIBUTE_LENGTH: usize = 512; #[dom_struct] pub struct BluetoothRemoteGATTCharacteristic { eventtarget: EventTarget, - service: JS<BluetoothRemoteGATTService>, + service: Dom<BluetoothRemoteGATTService>, uuid: DOMString, - properties: JS<BluetoothCharacteristicProperties>, - value: DOMRefCell<Option<ByteString>>, + properties: Dom<BluetoothCharacteristicProperties>, + value: DomRefCell<Option<ByteString>>, instance_id: String, } impl BluetoothRemoteGATTCharacteristic { - pub fn new_inherited(service: &BluetoothRemoteGATTService, - uuid: DOMString, - properties: &BluetoothCharacteristicProperties, - instance_id: String) - -> BluetoothRemoteGATTCharacteristic { + pub fn new_inherited( + service: &BluetoothRemoteGATTService, + uuid: DOMString, + properties: &BluetoothCharacteristicProperties, + instance_id: String, + ) -> BluetoothRemoteGATTCharacteristic { BluetoothRemoteGATTCharacteristic { eventtarget: EventTarget::new_inherited(), - service: JS::from_ref(service), + service: Dom::from_ref(service), uuid: uuid, - properties: JS::from_ref(properties), - value: DOMRefCell::new(None), + properties: Dom::from_ref(properties), + value: DomRefCell::new(None), instance_id: instance_id, } } - pub fn new(global: &GlobalScope, - service: &BluetoothRemoteGATTService, - uuid: DOMString, - properties: &BluetoothCharacteristicProperties, - instanceID: String) - -> Root<BluetoothRemoteGATTCharacteristic> { - reflect_dom_object(box BluetoothRemoteGATTCharacteristic::new_inherited(service, - uuid, - properties, - instanceID), - global, - BluetoothRemoteGATTCharacteristicBinding::Wrap) + pub fn new( + global: &GlobalScope, + service: &BluetoothRemoteGATTService, + uuid: DOMString, + properties: &BluetoothCharacteristicProperties, + instance_id: String, + ) -> DomRoot<BluetoothRemoteGATTCharacteristic> { + reflect_dom_object( + Box::new(BluetoothRemoteGATTCharacteristic::new_inherited( + service, + uuid, + properties, + instance_id, + )), + global, + ) } fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> { @@ -86,13 +90,13 @@ impl BluetoothRemoteGATTCharacteristic { impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-properties - fn Properties(&self) -> Root<BluetoothCharacteristicProperties> { - Root::from_ref(&self.properties) + fn Properties(&self) -> DomRoot<BluetoothCharacteristicProperties> { + DomRoot::from_ref(&self.properties) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-service - fn Service(&self) -> Root<BluetoothRemoteGATTService> { - Root::from_ref(&self.service) + fn Service(&self) -> DomRoot<BluetoothRemoteGATTService> { + DomRoot::from_ref(&self.service) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-uuid @@ -100,20 +104,30 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris self.uuid.clone() } - #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc<Promise> { - get_gatt_children(self, true, BluetoothUUID::descriptor, Some(descriptor), self.get_instance_id(), - self.Service().Device().get_gatt().Connected(), GATTType::Descriptor) + get_gatt_children( + self, + true, + BluetoothUUID::descriptor, + Some(descriptor), + self.get_instance_id(), + self.Service().Device().get_gatt().Connected(), + GATTType::Descriptor, + ) } - #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors - fn GetDescriptors(&self, - descriptor: Option<BluetoothDescriptorUUID>) - -> Rc<Promise> { - get_gatt_children(self, false, BluetoothUUID::descriptor, descriptor, self.get_instance_id(), - self.Service().Device().get_gatt().Connected(), GATTType::Descriptor) + fn GetDescriptors(&self, descriptor: Option<BluetoothDescriptorUUID>) -> Rc<Promise> { + get_gatt_children( + self, + false, + BluetoothUUID::descriptor, + descriptor, + self.get_instance_id(), + self.Service().Device().get_gatt().Connected(), + GATTType::Descriptor, + ) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value @@ -121,21 +135,19 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris self.value.borrow().clone() } - #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-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.Service().Device().get_gatt().Connected() { - p.reject_error(p_cx, Network); + p.reject_error(Network); return p; } @@ -143,39 +155,43 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // Step 5.1. if !self.Properties().Read() { - p.reject_error(p_cx, NotSupported); + p.reject_error(NotSupported); return p; } // Note: Steps 3 - 4 and the remaining 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-bluetoothremotegattcharacteristic-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.Service().Device().get_gatt().Connected() { - p.reject_error(p_cx, Network); + p.reject_error(Network); return p; } @@ -183,42 +199,45 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // Step 7.1. if !(self.Properties().Write() || - self.Properties().WriteWithoutResponse() || - self.Properties().AuthenticatedSignedWrites()) { - p.reject_error(p_cx, NotSupported); + self.Properties().WriteWithoutResponse() || + self.Properties().AuthenticatedSignedWrites()) + { + p.reject_error(NotSupported); return p; } // Note: Steps 5 - 6 and the remaining 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; } - #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-startnotifications - fn StartNotifications(&self) -> Rc<Promise> { - let p = Promise::new(&self.global()); - let p_cx = p.global().get_cx(); + fn StartNotifications(&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.Service().Device().get_gatt().Connected() { - p.reject_error(p_cx, Network); + p.reject_error(Network); return p; } // Step 5. - if !(self.Properties().Notify() || - self.Properties().Indicate()) { - p.reject_error(p_cx, NotSupported); + if !(self.Properties().Notify() || self.Properties().Indicate()) { + p.reject_error(NotSupported); return p; } @@ -227,51 +246,62 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // Note: Steps 3 - 4, 7 - 11 are implemented in components/bluetooth/lib.rs in enable_notification function // and in handle_response function. let sender = response_async(&p, self); - self.get_bluetooth_thread().send( - BluetoothRequest::EnableNotification(self.get_instance_id(), - true, - sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothRequest::EnableNotification( + self.get_instance_id(), + true, + sender, + )) + .unwrap(); return p; } - #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-stopnotifications - fn StopNotifications(&self) -> Rc<Promise> { - let p = Promise::new(&self.global()); + fn StopNotifications(&self, comp: InRealm) -> Rc<Promise> { + let p = Promise::new_in_current_realm(&self.global(), comp); let sender = response_async(&p, self); // TODO: Step 3 - 4: Implement `active notification context set` for BluetoothRemoteGATTCharacteristic, // Note: Steps 1 - 2, and part of Step 4 and Step 5 are implemented in components/bluetooth/lib.rs // in enable_notification function and in handle_response function. - self.get_bluetooth_thread().send( - BluetoothRequest::EnableNotification(self.get_instance_id(), - false, - sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothRequest::EnableNotification( + self.get_instance_id(), + false, + sender, + )) + .unwrap(); return p; } // https://webbluetoothcg.github.io/web-bluetooth/#dom-characteristiceventhandlers-oncharacteristicvaluechanged - event_handler!(characteristicvaluechanged, GetOncharacteristicvaluechanged, SetOncharacteristicvaluechanged); + event_handler!( + characteristicvaluechanged, + GetOncharacteristicvaluechanged, + SetOncharacteristicvaluechanged + ); } impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic { - fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) { + fn handle_response(&self, response: BluetoothResponse, promise: &Rc<Promise>) { let device = self.Service().Device(); match response { // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // Step 7. BluetoothResponse::GetDescriptors(descriptors_vec, single) => { if single { - promise.resolve_native(promise_cx, &device.get_or_create_descriptor(&descriptors_vec[0], &self)); + promise.resolve_native( + &device.get_or_create_descriptor(&descriptors_vec[0], &self), + ); return; } - let mut descriptors = vec!(); + let mut descriptors = vec![]; for descriptor in descriptors_vec { let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self); descriptors.push(bt_descriptor); } - promise.resolve_native(promise_cx, &descriptors); + promise.resolve_native(&descriptors); }, // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue BluetoothResponse::ReadValue(result) => { @@ -283,10 +313,11 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic { *self.value.borrow_mut() = Some(value.clone()); // Step 5.5.3. - self.upcast::<EventTarget>().fire_bubbling_event(atom!("characteristicvaluechanged")); + self.upcast::<EventTarget>() + .fire_bubbling_event(atom!("characteristicvaluechanged")); // Step 5.5.4. - promise.resolve_native(promise_cx, &value); + promise.resolve_native(&value); }, // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue BluetoothResponse::WriteValue(result) => { @@ -297,7 +328,7 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic { *self.value.borrow_mut() = Some(ByteString::new(result)); // Step 7.5.3. - promise.resolve_native(promise_cx, &()); + promise.resolve_native(&()); }, // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-startnotifications // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-stopnotifications @@ -307,9 +338,9 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic { // (StartNotification) Step 11. // (StopNotification) Step 5. - promise.resolve_native(promise_cx, self); + promise.resolve_native(self); }, - _ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())), + _ => promise.reject_error(Error::Type("Something went wrong...".to_owned())), } } } |