Commit a255d1be authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Fix ownership of BluetoothAdapter in BluetoothDeviceChooserController

BluetoothAdapter is a reference counted object and so
BluetoothDeviceChooserController should own it using a scoped_refptr.
Fixing this requires also fixing BluetoothAdapterFactoryWrapper's
AcquireAdapterCallback to take a scoped_refptr rather than a raw
pointer. A test for proper ownership has been added.

Bug: 1024121
Change-Id: I6342322e059f9cbff2a0d5f073f6bccfb0ca7c36
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1914536Reviewed-by: default avatarMatt Reynolds <mattreynolds@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715206}
parent 912b6e6e
...@@ -232,8 +232,8 @@ void RecordScanningDuration(const base::TimeDelta& duration) { ...@@ -232,8 +232,8 @@ void RecordScanningDuration(const base::TimeDelta& duration) {
BluetoothDeviceChooserController::BluetoothDeviceChooserController( BluetoothDeviceChooserController::BluetoothDeviceChooserController(
WebBluetoothServiceImpl* web_bluetooth_service, WebBluetoothServiceImpl* web_bluetooth_service,
RenderFrameHost* render_frame_host, RenderFrameHost* render_frame_host,
device::BluetoothAdapter* adapter) scoped_refptr<device::BluetoothAdapter> adapter)
: adapter_(adapter), : adapter_(std::move(adapter)),
web_bluetooth_service_(web_bluetooth_service), web_bluetooth_service_(web_bluetooth_service),
render_frame_host_(render_frame_host), render_frame_host_(render_frame_host),
web_contents_(WebContents::FromRenderFrameHost(render_frame_host_)), web_contents_(WebContents::FromRenderFrameHost(render_frame_host_)),
......
...@@ -48,7 +48,7 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final { ...@@ -48,7 +48,7 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final {
BluetoothDeviceChooserController( BluetoothDeviceChooserController(
WebBluetoothServiceImpl* web_bluetooth_service_, WebBluetoothServiceImpl* web_bluetooth_service_,
RenderFrameHost* render_frame_host, RenderFrameHost* render_frame_host,
device::BluetoothAdapter* adapter); scoped_refptr<device::BluetoothAdapter> adapter);
~BluetoothDeviceChooserController(); ~BluetoothDeviceChooserController();
// This function performs the following checks before starting a discovery // This function performs the following checks before starting a discovery
...@@ -127,7 +127,7 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final { ...@@ -127,7 +127,7 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final {
static int64_t scan_duration_; static int64_t scan_duration_;
// The adapter used to get existing devices and start a discovery session. // The adapter used to get existing devices and start a discovery session.
device::BluetoothAdapter* adapter_; scoped_refptr<device::BluetoothAdapter> adapter_;
// The WebBluetoothServiceImpl that owns this instance. // The WebBluetoothServiceImpl that owns this instance.
WebBluetoothServiceImpl* web_bluetooth_service_; WebBluetoothServiceImpl* web_bluetooth_service_;
// The RenderFrameHost that owns web_bluetooth_service_. // The RenderFrameHost that owns web_bluetooth_service_.
......
...@@ -644,7 +644,8 @@ void WebBluetoothServiceImpl::GetAvailability( ...@@ -644,7 +644,8 @@ void WebBluetoothServiceImpl::GetAvailability(
} }
auto get_availability_impl = base::BindOnce( auto get_availability_impl = base::BindOnce(
[](GetAvailabilityCallback callback, device::BluetoothAdapter* adapter) { [](GetAvailabilityCallback callback,
scoped_refptr<device::BluetoothAdapter> adapter) {
std::move(callback).Run(adapter->IsPresent()); std::move(callback).Run(adapter->IsPresent());
}, },
std::move(callback)); std::move(callback));
...@@ -1236,7 +1237,7 @@ void WebBluetoothServiceImpl::RequestScanningStartImpl( ...@@ -1236,7 +1237,7 @@ void WebBluetoothServiceImpl::RequestScanningStartImpl(
mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client, mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client,
blink::mojom::WebBluetoothRequestLEScanOptionsPtr options, blink::mojom::WebBluetoothRequestLEScanOptionsPtr options,
RequestScanningStartCallback callback, RequestScanningStartCallback callback,
device::BluetoothAdapter* adapter) { scoped_refptr<device::BluetoothAdapter> adapter) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
// The renderer should never send invalid options. // The renderer should never send invalid options.
...@@ -1354,7 +1355,7 @@ void WebBluetoothServiceImpl::OnDiscoverySessionError() { ...@@ -1354,7 +1355,7 @@ void WebBluetoothServiceImpl::OnDiscoverySessionError() {
void WebBluetoothServiceImpl::RequestDeviceImpl( void WebBluetoothServiceImpl::RequestDeviceImpl(
blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
RequestDeviceCallback callback, RequestDeviceCallback callback,
device::BluetoothAdapter* adapter) { scoped_refptr<device::BluetoothAdapter> adapter) {
// The renderer should never send invalid options. // The renderer should never send invalid options.
if (IsRequestDeviceOptionsInvalid(options)) { if (IsRequestDeviceOptionsInvalid(options)) {
CrashRendererAndClosePipe(bad_message::BDH_INVALID_OPTIONS); CrashRendererAndClosePipe(bad_message::BDH_INVALID_OPTIONS);
...@@ -1369,8 +1370,8 @@ void WebBluetoothServiceImpl::RequestDeviceImpl( ...@@ -1369,8 +1370,8 @@ void WebBluetoothServiceImpl::RequestDeviceImpl(
// the new one to make sure they can't conflict. // 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(new BluetoothDeviceChooserController(
new BluetoothDeviceChooserController(this, render_frame_host_, adapter)); this, render_frame_host_, std::move(adapter)));
// TODO(crbug.com/730593): Remove AdaptCallbackForRepeating() by updating // TODO(crbug.com/730593): Remove AdaptCallbackForRepeating() by updating
// the callee interface. // the callee interface.
......
...@@ -91,6 +91,8 @@ class CONTENT_EXPORT WebBluetoothServiceImpl ...@@ -91,6 +91,8 @@ class CONTENT_EXPORT WebBluetoothServiceImpl
BluetoothDeviceScanningPromptController* prompt_controller); BluetoothDeviceScanningPromptController* prompt_controller);
private: private:
FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest,
ClearStateDuringRequestDevice);
FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, PermissionAllowed); FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, PermissionAllowed);
FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest,
PermissionPromptCanceled); PermissionPromptCanceled);
...@@ -245,13 +247,13 @@ class CONTENT_EXPORT WebBluetoothServiceImpl ...@@ -245,13 +247,13 @@ class CONTENT_EXPORT WebBluetoothServiceImpl
void RequestDeviceImpl( void RequestDeviceImpl(
blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
RequestDeviceCallback callback, RequestDeviceCallback callback,
device::BluetoothAdapter* adapter); scoped_refptr<device::BluetoothAdapter> adapter);
void RequestScanningStartImpl( void RequestScanningStartImpl(
mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client, mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client,
blink::mojom::WebBluetoothRequestLEScanOptionsPtr options, blink::mojom::WebBluetoothRequestLEScanOptionsPtr options,
RequestScanningStartCallback callback, RequestScanningStartCallback callback,
device::BluetoothAdapter* adapter); scoped_refptr<device::BluetoothAdapter> adapter);
void OnStartDiscoverySession( void OnStartDiscoverySession(
mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client, mojo::AssociatedRemote<blink::mojom::WebBluetoothScanClient> client,
......
...@@ -180,6 +180,20 @@ class WebBluetoothServiceImplTest : public RenderViewHostImplTestHarness { ...@@ -180,6 +180,20 @@ class WebBluetoothServiceImplTest : public RenderViewHostImplTestHarness {
DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImplTest); DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImplTest);
}; };
TEST_F(WebBluetoothServiceImplTest, ClearStateDuringRequestDevice) {
auto options = blink::mojom::WebBluetoothRequestDeviceOptions::New();
options->accept_all_devices = true;
base::RunLoop loop;
service_->RequestDevice(
std::move(options),
base::BindLambdaForTesting(
[&loop](blink::mojom::WebBluetoothResult,
blink::mojom::WebBluetoothDevicePtr) { loop.Quit(); }));
service_->ClearState();
loop.Run();
}
TEST_F(WebBluetoothServiceImplTest, PermissionAllowed) { TEST_F(WebBluetoothServiceImplTest, PermissionAllowed) {
blink::mojom::WebBluetoothLeScanFilterPtr filter = CreateScanFilter("a", "b"); blink::mojom::WebBluetoothLeScanFilterPtr filter = CreateScanFilter("a", "b");
base::Optional<WebBluetoothServiceImpl::ScanFilters> filters; base::Optional<WebBluetoothServiceImpl::ScanFilters> filters;
......
...@@ -50,10 +50,9 @@ void BluetoothAdapterFactoryWrapper::AcquireAdapter( ...@@ -50,10 +50,9 @@ void BluetoothAdapterFactoryWrapper::AcquireAdapter(
DCHECK(!GetAdapter(observer)); DCHECK(!GetAdapter(observer));
AddAdapterObserver(observer); AddAdapterObserver(observer);
if (adapter_.get()) { if (adapter_) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE, base::BindOnce(std::move(callback), adapter_));
base::BindOnce(std::move(callback), base::Unretained(adapter_.get())));
return; return;
} }
...@@ -99,7 +98,7 @@ void BluetoothAdapterFactoryWrapper::OnGetAdapter( ...@@ -99,7 +98,7 @@ void BluetoothAdapterFactoryWrapper::OnGetAdapter(
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
set_adapter(adapter); set_adapter(adapter);
std::move(continuation).Run(adapter_.get()); std::move(continuation).Run(adapter_);
} }
bool BluetoothAdapterFactoryWrapper::HasAdapter( bool BluetoothAdapterFactoryWrapper::HasAdapter(
......
...@@ -23,7 +23,8 @@ namespace device { ...@@ -23,7 +23,8 @@ namespace device {
// http://crbug.com/603291 // http://crbug.com/603291
class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFactoryWrapper { class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFactoryWrapper {
public: public:
using AcquireAdapterCallback = base::OnceCallback<void(BluetoothAdapter*)>; using AcquireAdapterCallback =
base::OnceCallback<void(scoped_refptr<BluetoothAdapter>)>;
~BluetoothAdapterFactoryWrapper(); ~BluetoothAdapterFactoryWrapper();
......
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