From 01bda807e1f107f607b767281c62d5f9ddb582b0 Mon Sep 17 00:00:00 2001 From: reillyg Date: Thu, 11 Feb 2016 13:38:49 -0800 Subject: webusb: Reject the promise if the device is already open. This change adds the necessary plumbing for the USB Mojo service implementation to detect when a device is already open and to report this error so that the WebUSB API can reject the promise returned by USBDevice::open() with an InvalidStateError. BUG=492204 Review URL: https://codereview.chromium.org/1693433003 Cr-Commit-Position: refs/heads/master@{#375003} --- device/usb/mojo/device_impl.cc | 7 +++++-- device/usb/mojo/device_impl_unittest.cc | 17 +++++++++++++---- device/usb/public/interfaces/device.mojom | 3 +++ 3 files changed, 21 insertions(+), 6 deletions(-) (limited to 'device') diff --git a/device/usb/mojo/device_impl.cc b/device/usb/mojo/device_impl.cc index 724e911..0d1c64f 100644 --- a/device/usb/mojo/device_impl.cc +++ b/device/usb/mojo/device_impl.cc @@ -251,8 +251,11 @@ void DeviceImpl::GetConfiguration(const GetConfigurationCallback& callback) { } void DeviceImpl::Open(const OpenCallback& callback) { - device_->Open( - base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); + if (device_handle_) + callback.Run(OpenDeviceError::ALREADY_OPEN); + else + device_->Open( + base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); } void DeviceImpl::Close(const CloseCallback& callback) { diff --git a/device/usb/mojo/device_impl_unittest.cc b/device/usb/mojo/device_impl_unittest.cc index 626edd9..c6c2e49 100644 --- a/device/usb/mojo/device_impl_unittest.cc +++ b/device/usb/mojo/device_impl_unittest.cc @@ -459,10 +459,19 @@ TEST_F(USBDeviceImplTest, Open) { EXPECT_CALL(mock_device(), Open(_)); - base::RunLoop loop; - device->Open( - base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, loop.QuitClosure())); - loop.Run(); + { + base::RunLoop loop; + device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, + loop.QuitClosure())); + loop.Run(); + } + + { + base::RunLoop loop; + device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::ALREADY_OPEN, + loop.QuitClosure())); + loop.Run(); + } EXPECT_CALL(mock_handle(), Close()); } diff --git a/device/usb/public/interfaces/device.mojom b/device/usb/public/interfaces/device.mojom index 25addb2..de7bd5f 100644 --- a/device/usb/public/interfaces/device.mojom +++ b/device/usb/public/interfaces/device.mojom @@ -10,6 +10,9 @@ enum OpenDeviceError { // The operating system denied access to the device. ACCESS_DENIED, + + // The device is already open. + ALREADY_OPEN, }; enum TransferDirection { -- cgit v1.1