Commit c4dfbae6 authored by Mustaq Ahmed's avatar Mustaq Ahmed Committed by Commit Bot

Make BlueTooth/USB requestDevice non-consuming.

The specs for both WebBlueTooth & WebUSB only require a user
activation during a requestDevice call.  So consuming the activation
(vs just checking) is not a strong requirement.  There was a concern
that "not consuming" could possibly mean multiple choosers for a single
user activation but our browser code already handles the situation
gracefully.  Multiple requests to browser is possible from different
tabs anyway, even with (isolated) consumptions in individual tabs.

Therefore it makes sense to remove the consumption behavior here,
which is not spec-ed anyway.  This would fit our goal of a simple
user activation model ("UserActivationV2" project) which should be
simple enough for cross-browser implementation and for a complete spec.

Bug: 786407
Change-Id: I3c9dbf43df34d4cb4a2a33214a6c44847fa7435b
Reviewed-on: https://chromium-review.googlesource.com/775866Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Mustaq Ahmed <mustaq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548768}
parent 55ebb205
...@@ -823,11 +823,12 @@ void WebBluetoothServiceImpl::RequestDeviceImpl( ...@@ -823,11 +823,12 @@ void WebBluetoothServiceImpl::RequestDeviceImpl(
blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
RequestDeviceCallback callback, RequestDeviceCallback callback,
device::BluetoothAdapter* adapter) { device::BluetoothAdapter* adapter) {
// requestDevice() can only be called when processing a user-gesture and any // Calls to requestDevice() require user activation (user gestures). We
// user gesture outside of a chooser should close the chooser. This does // should close any opened chooser when a duplicate requestDevice call is made
// not happen on all platforms so we don't DCHECK that the old one is closed. // with the same user activation or when any gesture occurs outside of the
// We destroy the old chooser before constructing the new one to make sure // opened chooser. This does not happen on all platforms so we don't DCHECK
// they can't conflict. // that the old one is closed. We destroy the old chooser before constructing
// the new one to make sure they can't conflict.
device_chooser_controller_.reset(); device_chooser_controller_.reset();
device_chooser_controller_.reset( device_chooser_controller_.reset(
......
...@@ -6,10 +6,7 @@ ...@@ -6,10 +6,7 @@
<script src="/bluetooth/resources/bluetooth-helpers.js"></script> <script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script> <script>
'use strict'; 'use strict';
const test_desc = 'Consumes a user gesture.'; const test_desc = 'requestDevice calls do not consume user gestures.';
const expected = new DOMException(
'Must be handling a user gesture to show a permission request.',
'SecurityError');
bluetooth_test(() => setUpHealthThermometerAndHeartRateDevices() bluetooth_test(() => setUpHealthThermometerAndHeartRateDevices()
.then(() => callWithTrustedClick(() => { .then(() => callWithTrustedClick(() => {
...@@ -20,8 +17,8 @@ bluetooth_test(() => setUpHealthThermometerAndHeartRateDevices() ...@@ -20,8 +17,8 @@ bluetooth_test(() => setUpHealthThermometerAndHeartRateDevices()
return Promise.all([ return Promise.all([
first.then(device => assert_equals( first.then(device => assert_equals(
device.constructor.name, 'BluetoothDevice')), device.constructor.name, 'BluetoothDevice')),
assert_promise_rejects_with_message(second, second.then(device => assert_equals(
expected, 'A request should consume a user gesture') device.constructor.name, 'BluetoothDevice')),
]); ]);
})), test_desc); })), test_desc);
</script> </script>
...@@ -130,4 +130,24 @@ usb_test(usb => { ...@@ -130,4 +130,24 @@ usb_test(usb => {
}); });
}); });
}, 'ondisconnect event is triggered by removing a device'); }, 'ondisconnect event is triggered by removing a device');
usb_test(() => {
return getFakeDevice().then(({ device, fakeDevice }) => {
navigator.usb.test.onrequestdevice = event => {
event.respondWith(fakeDevice);
}
return callWithTrustedClick(() => {
let first = navigator.usb.requestDevice({ filters: [] });
let second = navigator.usb.requestDevice({ filters: [] });
return Promise.all([
first.then(chosenDevice => {
assert_equals(chosenDevice, device);
}),
second.then(chosenDevice => {
assert_equals(chosenDevice, device);
})
]);
});
});
}, 'multiple requestDevice calls are allowed per user activation');
</script> </script>
...@@ -169,7 +169,7 @@ ScriptPromise Bluetooth::requestDevice(ScriptState* script_state, ...@@ -169,7 +169,7 @@ ScriptPromise Bluetooth::requestDevice(ScriptState* script_state,
// If the algorithm is not allowed to show a popup, reject promise with a // If the algorithm is not allowed to show a popup, reject promise with a
// SecurityError and abort these steps. // SecurityError and abort these steps.
Document* doc = ToDocumentOrNull(context); Document* doc = ToDocumentOrNull(context);
if (!Frame::ConsumeTransientUserActivation(doc ? doc->GetFrame() : nullptr)) { if (!Frame::HasTransientUserActivation(doc ? doc->GetFrame() : nullptr)) {
return ScriptPromise::RejectWithDOMException( return ScriptPromise::RejectWithDOMException(
script_state, script_state,
DOMException::Create( DOMException::Create(
......
...@@ -128,7 +128,7 @@ ScriptPromise USB::requestDevice(ScriptState* script_state, ...@@ -128,7 +128,7 @@ ScriptPromise USB::requestDevice(ScriptState* script_state,
&USB::OnChooserServiceConnectionError, WrapWeakPersistent(this))); &USB::OnChooserServiceConnectionError, WrapWeakPersistent(this)));
} }
if (!Frame::ConsumeTransientUserActivation(frame)) { if (!Frame::HasTransientUserActivation(frame)) {
return ScriptPromise::RejectWithDOMException( return ScriptPromise::RejectWithDOMException(
script_state, script_state,
DOMException::Create( DOMException::Create(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment