From 40235da2d3c6eb5b9e36f9b63b6f96698f2f64c3 Mon Sep 17 00:00:00 2001 From: Anthony Urena Date: Sat, 24 Mar 2018 10:53:48 -0400 Subject: [PATCH 1/3] Switches WriteValue to use BufferSource --- .../script/dom/bluetoothremotegattdescriptor.rs | 11 ++++++++--- .../dom/webidls/BluetoothRemoteGATTDescriptor.webidl | 5 ++--- .../writeValue/descriptor/write-updates-value.html | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 94023d6ffa0..54a6b1142b3 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -11,6 +11,7 @@ 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::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer; use dom::bindings::error::Error::{self, InvalidModification, Network, Security}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; use dom::bindings::root::{Dom, DomRoot}; @@ -114,7 +115,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue - fn WriteValue(&self, value: Vec) -> Rc { + fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer) -> Rc { let p = Promise::new(&self.global()); // Step 1. @@ -124,7 +125,11 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } // Step 2 - 3. - if value.len() > MAXIMUM_ATTRIBUTE_LENGTH { + let v = match value { + ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), + }; + if v.len() > MAXIMUM_ATTRIBUTE_LENGTH { p.reject_error(InvalidModification); return p; } @@ -140,7 +145,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // 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(); + BluetoothRequest::WriteValue(self.get_instance_id(), v, sender)).unwrap(); return p; } } diff --git a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl index a202975013c..82aa80cc580 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl @@ -11,7 +11,6 @@ interface BluetoothRemoteGATTDescriptor { readonly attribute DOMString uuid; readonly attribute ByteString? value; Promise readValue(); - //Promise readValue(); - Promise writeValue(sequence value); - //Promise writeValue(BufferSource value); + // Promise readValue(); + Promise writeValue(BufferSource value); }; diff --git a/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html b/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html index cfc2fde1c21..26ad0c4403d 100644 --- a/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html +++ b/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html @@ -16,8 +16,8 @@ promise_test(() => { .then(characteristic => characteristic.getDescriptor(number_of_digitals.name)) .then(descriptor => { assert_equals(descriptor.value, null); - return descriptor.writeValue(asciiToDecimal('foo')) - .then(() => assert_equals(descriptor.value, 'foo')); + return descriptor.writeValue(Uint8Array.of(1, 2)) + .then(() => assert_equals(descriptor.value, "\x01\x02")); }); }, 'A regular write request to a writable descriptor should update value.'); From 5bf2e71407d7d0cdde5d5da381dce545135ae93a Mon Sep 17 00:00:00 2001 From: Anthony Urena Date: Sat, 24 Mar 2018 16:10:28 -0400 Subject: [PATCH 2/3] Switches characteristic to use BufferSource --- .../script/dom/bluetoothremotegattcharacteristic.rs | 12 +++++++++--- .../script/dom/bluetoothremotegattdescriptor.rs | 6 +++--- .../webidls/BluetoothRemoteGATTCharacteristic.webidl | 3 +-- .../dom/webidls/BluetoothRemoteGATTDescriptor.webidl | 2 +- .../characteristic/write-updates-value.html | 4 ++-- .../writeValue/descriptor/write-updates-value.html | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 20037c9920c..a8dc2d39a5a 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -12,6 +12,7 @@ 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::UnionTypes::ArrayBufferViewOrArrayBuffer; use dom::bindings::error::Error::{self, InvalidModification, Network, NotSupported, Security}; use dom::bindings::inheritance::Castable; use dom::bindings::reflector::{DomObject, reflect_dom_object}; @@ -155,7 +156,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue - fn WriteValue(&self, value: Vec) -> Rc { + fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer) -> Rc { let p = Promise::new(&self.global()); // Step 1. @@ -165,7 +166,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // Step 2 - 3. - if value.len() > MAXIMUM_ATTRIBUTE_LENGTH { + let vec = match value { + ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), + }; + + if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH { p.reject_error(InvalidModification); return p; } @@ -190,7 +196,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // 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(); + BluetoothRequest::WriteValue(self.get_instance_id(), vec, sender)).unwrap(); return p; } diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 54a6b1142b3..e120648d661 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -125,11 +125,11 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } // Step 2 - 3. - let v = match value { + let vec = match value { ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), }; - if v.len() > MAXIMUM_ATTRIBUTE_LENGTH { + if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH { p.reject_error(InvalidModification); return p; } @@ -145,7 +145,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // 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(), v, sender)).unwrap(); + BluetoothRequest::WriteValue(self.get_instance_id(), vec, sender)).unwrap(); return p; } } diff --git a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl index 3e086fc21ca..07cedd97421 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl @@ -16,8 +16,7 @@ interface BluetoothRemoteGATTCharacteristic : EventTarget { getDescriptors(optional BluetoothDescriptorUUID descriptor); Promise readValue(); //Promise readValue(); - Promise writeValue(sequence value); - //Promise writeValue(BufferSource value); + Promise writeValue(BufferSource value); Promise startNotifications(); Promise stopNotifications(); }; diff --git a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl index 82aa80cc580..243b86fe155 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl @@ -11,6 +11,6 @@ interface BluetoothRemoteGATTDescriptor { readonly attribute DOMString uuid; readonly attribute ByteString? value; Promise readValue(); - // Promise readValue(); + //Promise readValue(); Promise writeValue(BufferSource value); }; diff --git a/tests/wpt/mozilla/tests/bluetooth/writeValue/characteristic/write-updates-value.html b/tests/wpt/mozilla/tests/bluetooth/writeValue/characteristic/write-updates-value.html index a504a49f2d8..23851862a23 100644 --- a/tests/wpt/mozilla/tests/bluetooth/writeValue/characteristic/write-updates-value.html +++ b/tests/wpt/mozilla/tests/bluetooth/writeValue/characteristic/write-updates-value.html @@ -15,8 +15,8 @@ promise_test(() => { .then(service => service.getCharacteristic(device_name.name)) .then(characteristic => { assert_equals(characteristic.value, null); - return characteristic.writeValue(asciiToDecimal('foo')) - .then(() => assert_equals(characteristic.value, 'foo')); + return characteristic.writeValue(Uint8Array.of(1, 2)) + .then(() => assert_equals(characteristic.value, '\x01\x02')); }); }, 'A regular write request to a writable characteristic should update value.'); diff --git a/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html b/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html index 26ad0c4403d..14dc5b027e4 100644 --- a/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html +++ b/tests/wpt/mozilla/tests/bluetooth/writeValue/descriptor/write-updates-value.html @@ -17,7 +17,7 @@ promise_test(() => { .then(descriptor => { assert_equals(descriptor.value, null); return descriptor.writeValue(Uint8Array.of(1, 2)) - .then(() => assert_equals(descriptor.value, "\x01\x02")); + .then(() => assert_equals(descriptor.value, '\x01\x02')); }); }, 'A regular write request to a writable descriptor should update value.'); From ef0e93c9b163ae1f61e583014372e5f4a623074c Mon Sep 17 00:00:00 2001 From: Anthony Urena Date: Sat, 24 Mar 2018 19:40:38 -0400 Subject: [PATCH 3/3] Updates Bluetooth type to use BufferSource --- components/script/dom/bluetooth.rs | 14 +++++++++++--- .../dom/bluetoothremotegattcharacteristic.rs | 4 ++-- .../script/dom/bluetoothremotegattdescriptor.rs | 4 ++-- components/script/dom/webidls/Bluetooth.webidl | 6 ++---- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index e0dd0f00e10..b980562c46d 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -14,7 +14,7 @@ use dom::bindings::codegen::Bindings::BluetoothPermissionResultBinding::Bluetoot use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerBinding:: BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::PermissionStatusBinding::{PermissionName, PermissionState}; -use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; +use dom::bindings::codegen::UnionTypes::{ArrayBufferViewOrArrayBuffer, StringOrUnsignedLong}; use dom::bindings::error::Error::{self, Network, Security, Type}; use dom::bindings::error::Fallible; use dom::bindings::refcounted::{Trusted, TrustedPromise}; @@ -442,12 +442,20 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible Fallible<(Vec, Vec)> { // Step 1. - let data_prefix = bdfi.dataPrefix.clone().unwrap_or(vec![]); + let data_prefix = match bdfi.dataPrefix { + Some(ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref avb)) => avb.to_vec(), + Some(ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref ab)) => ab.to_vec(), + None => vec![] + }; // Step 2. // If no mask present, mask will be a sequence of 0xFF bytes the same length as dataPrefix. // Masking dataPrefix with this, leaves dataPrefix untouched. - let mask = bdfi.mask.clone().unwrap_or(vec![0xFF; data_prefix.len()]); + let mask = match bdfi.mask { + Some(ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref avb)) => avb.to_vec(), + Some(ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref ab)) => ab.to_vec(), + None => vec![0xFF; data_prefix.len()] + }; // Step 3. if mask.len() != data_prefix.len() { diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index a8dc2d39a5a..8034623ff5c 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -167,8 +167,8 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // Step 2 - 3. let vec = match value { - ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), - ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBufferView(avb) => avb.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(ab) => ab.to_vec(), }; if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH { diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index e120648d661..493550ee91b 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -126,8 +126,8 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { // Step 2 - 3. let vec = match value { - ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), - ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBufferView(avb) => avb.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(ab) => ab.to_vec(), }; if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH { p.reject_error(InvalidModification); diff --git a/components/script/dom/webidls/Bluetooth.webidl b/components/script/dom/webidls/Bluetooth.webidl index 7fc4c7392ad..f891b5f38a5 100644 --- a/components/script/dom/webidls/Bluetooth.webidl +++ b/components/script/dom/webidls/Bluetooth.webidl @@ -5,10 +5,8 @@ // https://webbluetoothcg.github.io/web-bluetooth/#bluetooth dictionary BluetoothDataFilterInit { - // BufferSource dataPrefix; - sequence dataPrefix; - // BufferSource mask; - sequence mask; + BufferSource dataPrefix; + BufferSource mask; }; dictionary BluetoothLEScanFilterInit {