fix: navigator.bluetooth.requestDevice (#27902)

* fix: navigator.bluetooth.requestDevice

* cleanup lint and add test

* update bluetooth test to handle no bluetooth adapter available

* update bluetooth test to handle bluetooth permission denied
This commit is contained in:
John Kleinschmidt 2021-02-26 14:10:27 -05:00 committed by GitHub
parent bd940b2904
commit d57fd6cef0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 269 additions and 2 deletions

View file

@ -44,7 +44,9 @@ BluetoothChooser::BluetoothChooser(api::WebContents* contents,
const EventHandler& event_handler)
: api_web_contents_(contents), event_handler_(event_handler) {}
BluetoothChooser::~BluetoothChooser() = default;
BluetoothChooser::~BluetoothChooser() {
event_handler_.Reset();
}
void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
switch (presence) {
@ -65,9 +67,11 @@ void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
switch (state) {
case DiscoveryState::FAILED_TO_START:
refreshing_ = false;
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
break;
case DiscoveryState::IDLE:
refreshing_ = false;
if (device_map_.empty()) {
auto event = ++num_retries_ > kMaxScanRetries
? content::BluetoothChooserEvent::CANCELLED
@ -86,6 +90,14 @@ void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
}
break;
case DiscoveryState::DISCOVERING:
// The first time this state fires is due to a rescan triggering so set a
// flag to ignore devices
if (!refreshing_) {
refreshing_ = true;
} else {
// The second time this state fires we are now safe to pick a device
refreshing_ = false;
}
break;
}
}
@ -96,6 +108,11 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
bool is_gatt_connected,
bool is_paired,
int signal_strength_level) {
if (refreshing_) {
// If the list of bluetooth devices is currently being generated don't fire
// an event
return;
}
bool changed = false;
auto entry = device_map_.find(device_id);
if (entry == device_map_.end()) {

View file

@ -41,6 +41,7 @@ class BluetoothChooser : public content::BluetoothChooser {
api::WebContents* api_web_contents_;
EventHandler event_handler_;
int num_retries_ = 0;
bool refreshing_ = false;
DISALLOW_COPY_AND_ASSIGN(BluetoothChooser);
};