Commit 2c014f94 authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

serial: Initialize Bluetooth enumerator in GetPort()

The DCHECK in serial_port_manager_impl.cc:97 is reachable if the
kEnableBluetoothSerialPortProfileInSerialApi flag is disabled or
GetDevices() is not called first and an unrecognized token is passed to
GetPort(). This change adds a check for the flag and initializes the
Bluetooth enumerator as necessary.

Bug: 1043300
Change-Id: Ib38e297f61cbc86bb9ab567084919a9e268196e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2446846
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Matt Reynolds <mattreynolds@chromium.org>
Auto-Submit: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarMatt Reynolds <mattreynolds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813457}
parent 568372e8
......@@ -94,14 +94,22 @@ void SerialPortManagerImpl::GetPort(
return;
}
DCHECK(bluetooth_enumerator_);
base::Optional<std::string> address =
bluetooth_enumerator_->GetAddressFromToken(token);
if (address) {
ui_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&BluetoothSerialPortImpl::Create,
bluetooth_enumerator_->GetAdapter(), *address,
std::move(receiver), std::move(watcher)));
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableBluetoothSerialPortProfileInSerialApi)) {
if (!bluetooth_enumerator_) {
bluetooth_enumerator_ =
std::make_unique<BluetoothSerialDeviceEnumerator>();
observed_enumerator_.Add(bluetooth_enumerator_.get());
}
base::Optional<std::string> address =
bluetooth_enumerator_->GetAddressFromToken(token);
if (address) {
ui_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&BluetoothSerialPortImpl::Create,
bluetooth_enumerator_->GetAdapter(), *address,
std::move(receiver), std::move(watcher)));
}
}
}
......
......@@ -225,6 +225,19 @@ TEST_F(SerialPortManagerImplTest, GetDevices) {
loop.Run();
}
TEST_F(SerialPortManagerImplTest, GetUnknownPort) {
mojo::Remote<mojom::SerialPortManager> port_manager;
Bind(port_manager.BindNewPipeAndPassReceiver());
mojo::Remote<mojom::SerialPort> serial_port;
port_manager->GetPort(base::UnguessableToken::Create(),
/*use_alternate_path=*/false,
serial_port.BindNewPipeAndPassReceiver(),
/*watcher=*/mojo::NullRemote());
serial_port.FlushForTesting();
EXPECT_FALSE(serial_port.is_connected());
}
TEST_F(SerialPortManagerImplTest, PortRemovedAndAdded) {
SetupBluetoothEnumerator();
mojo::Remote<mojom::SerialPortManager> port_manager;
......
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