From 8f04def7b204e024a4109176122572e052065d16 Mon Sep 17 00:00:00 2001 From: Michelle Tilley Date: Wed, 28 Nov 2018 08:36:00 -0800 Subject: [PATCH] fix: prevent bluetooth device list from growing without bound (#15805) * fix: include bluetooth strings in build * fix: prevent bluetooth device list from growing without bound --- atom/browser/lib/bluetooth_chooser.cc | 56 ++++++++++++++++----------- atom/browser/lib/bluetooth_chooser.h | 5 ++- electron_paks.gni | 2 + 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/atom/browser/lib/bluetooth_chooser.cc b/atom/browser/lib/bluetooth_chooser.cc index cbc7b1f28dcf..2a70be9fdc1a 100644 --- a/atom/browser/lib/bluetooth_chooser.cc +++ b/atom/browser/lib/bluetooth_chooser.cc @@ -63,16 +63,17 @@ void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) { event_handler_.Run(Event::CANCELLED, ""); break; case DiscoveryState::IDLE: - if (device_list_.empty()) { + if (device_map_.empty()) { auto event = ++num_retries_ > kMaxScanRetries ? Event::CANCELLED : Event::RESCAN; event_handler_.Run(event, ""); } else { bool prevent_default = api_web_contents_->Emit( - "select-bluetooth-device", device_list_, + "select-bluetooth-device", GetDeviceList(), base::Bind(&OnDeviceChosen, event_handler_)); if (!prevent_default) { - auto device_id = device_list_[0].device_id; + auto it = device_map_.begin(); + auto device_id = it->first; event_handler_.Run(Event::SELECTED, device_id); } } @@ -88,29 +89,40 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id, bool is_gatt_connected, bool is_paired, int signal_strength_level) { - DeviceInfo info = {device_id, device_name}; - device_list_.push_back(info); - - // Emit a select-bluetooth-device handler to allow for user to listen for - // bluetooth device found. - bool prevent_default = - api_web_contents_->Emit("select-bluetooth-device", device_list_, - base::Bind(&OnDeviceChosen, event_handler_)); - - // If emit not implimented select first device that matches the filters - // provided. - if (!prevent_default) { - event_handler_.Run(Event::SELECTED, device_id); + bool changed = false; + auto entry = device_map_.find(device_id); + if (entry == device_map_.end()) { + device_map_[device_id] = device_name; + changed = true; + } else if (should_update_name) { + entry->second = device_name; + changed = true; } -} -void BluetoothChooser::RemoveDevice(const std::string& device_id) { - for (auto it = device_list_.begin(); it != device_list_.end(); ++it) { - if (it->device_id == device_id) { - device_list_.erase(it); - return; + if (changed) { + // Emit a select-bluetooth-device handler to allow for user to listen for + // bluetooth device found. + bool prevent_default = + api_web_contents_->Emit("select-bluetooth-device", GetDeviceList(), + base::Bind(&OnDeviceChosen, event_handler_)); + + // If emit not implimented select first device that matches the filters + // provided. + if (!prevent_default) { + event_handler_.Run(Event::SELECTED, device_id); } } } +std::vector +BluetoothChooser::GetDeviceList() { + std::vector vec; + for (const auto& it : device_map_) { + DeviceInfo info = {it.first, it.second}; + vec.push_back(info); + } + + return vec; +} + } // namespace atom diff --git a/atom/browser/lib/bluetooth_chooser.h b/atom/browser/lib/bluetooth_chooser.h index ad23751056ff..e304caa58980 100644 --- a/atom/browser/lib/bluetooth_chooser.h +++ b/atom/browser/lib/bluetooth_chooser.h @@ -5,6 +5,7 @@ #ifndef ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_ #define ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_ +#include #include #include @@ -33,10 +34,10 @@ class BluetoothChooser : public content::BluetoothChooser { bool is_gatt_connected, bool is_paired, int signal_strength_level) override; - void RemoveDevice(const std::string& device_id); + std::vector GetDeviceList(); private: - std::vector device_list_; + std::map device_map_; api::WebContents* api_web_contents_; EventHandler event_handler_; int num_retries_ = 0; diff --git a/electron_paks.gni b/electron_paks.gni index 843b965c91df..8ed51da58207 100644 --- a/electron_paks.gni +++ b/electron_paks.gni @@ -150,11 +150,13 @@ template("electron_paks") { "${root_gen_dir}/content/app/strings/content_strings_", "${root_gen_dir}/ui/strings/app_locale_settings_", "${root_gen_dir}/ui/strings/ui_strings_", + "${root_gen_dir}/device/bluetooth/strings/bluetooth_strings_", ] deps = [ "//chrome/app/resources:platform_locale_settings", "//components/strings:components_strings", "//content/app/strings", + "//device/bluetooth/strings", "//ui/strings:app_locale_settings", "//ui/strings:ui_strings", ]