mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: allow cancelling of bluetooth requests (#37717)
* fix: allow cancelling of bluetooth requests allows cancelling of bluetooth requests when no devices present Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * docs: update docs to reflect how bluetooth works. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
@@ -27,8 +27,6 @@ namespace electron {
|
||||
|
||||
namespace {
|
||||
|
||||
const int kMaxScanRetries = 5;
|
||||
|
||||
void OnDeviceChosen(const content::BluetoothChooser::EventHandler& handler,
|
||||
const std::string& device_id) {
|
||||
if (device_id.empty()) {
|
||||
@@ -66,29 +64,15 @@ void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
|
||||
}
|
||||
|
||||
void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
|
||||
bool idle_state = false;
|
||||
switch (state) {
|
||||
case DiscoveryState::FAILED_TO_START:
|
||||
refreshing_ = false;
|
||||
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
|
||||
break;
|
||||
return;
|
||||
case DiscoveryState::IDLE:
|
||||
refreshing_ = false;
|
||||
if (device_map_.empty()) {
|
||||
auto event = ++num_retries_ > kMaxScanRetries
|
||||
? content::BluetoothChooserEvent::CANCELLED
|
||||
: content::BluetoothChooserEvent::RESCAN;
|
||||
event_handler_.Run(event, "");
|
||||
} else {
|
||||
bool prevent_default = api_web_contents_->Emit(
|
||||
"select-bluetooth-device", GetDeviceList(),
|
||||
base::BindOnce(&OnDeviceChosen, event_handler_));
|
||||
if (!prevent_default) {
|
||||
auto it = device_map_.begin();
|
||||
auto device_id = it->first;
|
||||
event_handler_.Run(content::BluetoothChooserEvent::SELECTED,
|
||||
device_id);
|
||||
}
|
||||
}
|
||||
idle_state = true;
|
||||
break;
|
||||
case DiscoveryState::DISCOVERING:
|
||||
// The first time this state fires is due to a rescan triggering so set a
|
||||
@@ -101,6 +85,18 @@ void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
bool prevent_default =
|
||||
api_web_contents_->Emit("select-bluetooth-device", GetDeviceList(),
|
||||
base::BindOnce(&OnDeviceChosen, event_handler_));
|
||||
if (!prevent_default && idle_state) {
|
||||
if (device_map_.empty()) {
|
||||
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
|
||||
} else {
|
||||
auto it = device_map_.begin();
|
||||
auto device_id = it->first;
|
||||
event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
|
||||
|
||||
@@ -44,7 +44,6 @@ class BluetoothChooser : public content::BluetoothChooser {
|
||||
std::map<std::string, std::u16string> device_map_;
|
||||
api::WebContents* api_web_contents_;
|
||||
EventHandler event_handler_;
|
||||
int num_retries_ = 0;
|
||||
bool refreshing_ = false;
|
||||
bool rescan_ = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user