diff --git a/components/bluetooth/lib.rs b/components/bluetooth/lib.rs index bcfa1b9c4dcd..1f3fe20493be 100644 --- a/components/bluetooth/lib.rs +++ b/components/bluetooth/lib.rs @@ -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())) + } } diff --git a/components/bluetooth_traits/lib.rs b/components/bluetooth_traits/lib.rs index af2314817ef9..85421990f715 100644 --- a/components/bluetooth_traits/lib.rs +++ b/components/bluetooth_traits/lib.rs @@ -91,6 +91,7 @@ pub enum BluetoothRequest { WatchAdvertisements(String, IpcSender), SetRepresentedToNull(Vec, Vec, Vec), IsRepresentedDeviceNull(String, IpcSender), + GetAvailability(IpcSender), Test(String, IpcSender>), Exit, } @@ -107,4 +108,5 @@ pub enum BluetoothResponse { WriteValue(Vec), EnableNotification(()), WatchAdvertisements(()), + GetAvailability(bool), } diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 82d455e5f4fc..82c39782133d 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -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 { + 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())), } } diff --git a/components/script/dom/webidls/Bluetooth.webidl b/components/script/dom/webidls/Bluetooth.webidl index fe87d10aa81c..6299e661474e 100644 --- a/components/script/dom/webidls/Bluetooth.webidl +++ b/components/script/dom/webidls/Bluetooth.webidl @@ -29,8 +29,8 @@ dictionary RequestDeviceOptions { [Pref="dom.bluetooth.enabled"] interface Bluetooth : EventTarget { - // [SecureContext] - // Promise getAvailability(); + [SecureContext] + Promise getAvailability(); [SecureContext] attribute EventHandler onavailabilitychanged; // [SecureContext, SameObject] diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index aca38c422288..6a5ba1ed5639 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -11443,6 +11443,24 @@ {} ] ], + "mozilla/bluetooth/getAvailability/adapter-not-present.html": [ + [ + "/_mozilla/mozilla/bluetooth/getAvailability/adapter-not-present.html", + {} + ] + ], + "mozilla/bluetooth/getAvailability/adapter-off.html": [ + [ + "/_mozilla/mozilla/bluetooth/getAvailability/adapter-off.html", + {} + ] + ], + "mozilla/bluetooth/getAvailability/adapter-on.html": [ + [ + "/_mozilla/mozilla/bluetooth/getAvailability/adapter-on.html", + {} + ] + ], "mozilla/bluetooth/getCharacteristic/blocklisted-characteristic.html": [ [ "/_mozilla/mozilla/bluetooth/getCharacteristic/blocklisted-characteristic.html", @@ -24372,6 +24390,18 @@ "6bba458074ce7d8067b6420d4326cbb133e35304", "testharness" ], + "mozilla/bluetooth/getAvailability/adapter-not-present.html": [ + "e4d5e516bee397ecefee1ef679f48f11b5dabe78", + "testharness" + ], + "mozilla/bluetooth/getAvailability/adapter-off.html": [ + "0e3d7824d57ba676ae52c775fa21d8e6ec6d7073", + "testharness" + ], + "mozilla/bluetooth/getAvailability/adapter-on.html": [ + "7e37c1b939712f5920b607e27a82f34573ca40fd", + "testharness" + ], "mozilla/bluetooth/getCharacteristic/blocklisted-characteristic.html": [ "c75429728dedbc5059aa7c3cc72554bb6a9bbbcb", "testharness" diff --git a/tests/wpt/mozilla/meta/mozilla/bluetooth/interfaces.html.ini b/tests/wpt/mozilla/meta/mozilla/bluetooth/interfaces.html.ini index ef3081a42b3d..372cbf3def39 100644 --- a/tests/wpt/mozilla/meta/mozilla/bluetooth/interfaces.html.ini +++ b/tests/wpt/mozilla/meta/mozilla/bluetooth/interfaces.html.ini @@ -30,9 +30,6 @@ [Bluetooth interface: attribute onserviceremoved] expected: FAIL - [Bluetooth interface: window.navigator.bluetooth must inherit property "getAvailability" with the proper type (0)] - expected: FAIL - [Bluetooth interface: window.navigator.bluetooth must inherit property "referringDevice" with the proper type (2)] expected: FAIL @@ -431,4 +428,3 @@ [BluetoothRemoteGATTDescriptor interface: calling writeValue(BufferSource) on bluetooth_descriptor with too few arguments must throw TypeError] expected: FAIL - diff --git a/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-not-present.html b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-not-present.html new file mode 100644 index 000000000000..d3c868056ff9 --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-not-present.html @@ -0,0 +1,12 @@ + + + + + diff --git a/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-off.html b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-off.html new file mode 100644 index 000000000000..916259203ad4 --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-off.html @@ -0,0 +1,12 @@ + + + + + diff --git a/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-on.html b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-on.html new file mode 100644 index 000000000000..591b5acf9e14 --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/bluetooth/getAvailability/adapter-on.html @@ -0,0 +1,12 @@ + + + + +