refactor: bluetooth in serial chooser when exclusively wireless serial ports are expected (#45743)

* refactor: bluetooth in serial chooser when exclusively wireless serial ports are expected

5737296

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: review feedback

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-02-21 10:30:34 -06:00 committed by GitHub
parent 4015b8c48e
commit 51babe4592
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 4 deletions

View file

@ -12,6 +12,7 @@
#include "base/scoped_observation.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/serial_chooser.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "services/device/public/mojom/serial.mojom-forward.h"
#include "shell/browser/serial/serial_chooser_context.h"
#include "third_party/blink/public/mojom/serial/serial.mojom-forward.h"
@ -31,7 +32,8 @@ class ElectronSerialDelegate;
// SerialChooserController provides data for the Serial API permission prompt.
class SerialChooserController final
: private SerialChooserContext::PortObserver {
: private SerialChooserContext::PortObserver,
private device::BluetoothAdapter::Observer {
public:
SerialChooserController(
content::RenderFrameHost* render_frame_host,
@ -55,12 +57,21 @@ class SerialChooserController final
void OnPermissionRevoked(const url::Origin& origin) override {}
void OnSerialChooserContextShutdown() override;
// BluetoothAdapter::Observer
void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
bool powered) override;
private:
api::Session* GetSession();
void GetDevices();
void OnGetDevices(std::vector<device::mojom::SerialPortInfoPtr> ports);
bool DisplayDevice(const device::mojom::SerialPortInfo& port) const;
void RunCallback(device::mojom::SerialPortInfoPtr port);
void OnDeviceChosen(const std::string& port_id);
void OnGetAdapter(base::OnceClosure callback,
scoped_refptr<device::BluetoothAdapter> adapter);
// Whether it will only show ports from bluetooth devices.
[[nodiscard]] bool IsWirelessSerialPortOnly() const;
base::WeakPtr<content::WebContents> web_contents_;
@ -77,8 +88,12 @@ class SerialChooserController final
std::vector<device::mojom::SerialPortInfoPtr> ports_;
base::WeakPtr<ElectronSerialDelegate> serial_delegate_;
scoped_refptr<device::BluetoothAdapter> adapter_;
base::ScopedObservation<device::BluetoothAdapter,
device::BluetoothAdapter::Observer>
adapter_observation_{this};
base::WeakPtr<ElectronSerialDelegate> serial_delegate_;
content::GlobalRenderFrameHostId render_frame_host_id_;
base::WeakPtrFactory<SerialChooserController> weak_factory_{this};