Browse Source

fix: prevent bluetooth device list from growing without bound (backport: 4-0-x) (#15866)

* fix: include bluetooth strings in build

* fix: prevent bluetooth device list from growing without bound
trop[bot] 6 years ago
parent
commit
99a9f8a01b
3 changed files with 39 additions and 24 deletions
  1. 34 22
      atom/browser/lib/bluetooth_chooser.cc
  2. 3 2
      atom/browser/lib/bluetooth_chooser.h
  3. 2 0
      electron_paks.gni

+ 34 - 22
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<atom::BluetoothChooser::DeviceInfo>
+BluetoothChooser::GetDeviceList() {
+  std::vector<atom::BluetoothChooser::DeviceInfo> vec;
+  for (const auto& it : device_map_) {
+    DeviceInfo info = {it.first, it.second};
+    vec.push_back(info);
+  }
+
+  return vec;
+}
+
 }  // namespace atom

+ 3 - 2
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 <map>
 #include <string>
 #include <vector>
 
@@ -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<DeviceInfo> GetDeviceList();
 
  private:
-  std::vector<DeviceInfo> device_list_;
+  std::map<std::string, base::string16> device_map_;
   api::WebContents* api_web_contents_;
   EventHandler event_handler_;
   int num_retries_ = 0;

+ 2 - 0
electron_paks.gni

@@ -149,10 +149,12 @@ 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 = [
       "//components/strings:components_strings",
       "//content/app/strings",
+      "//device/bluetooth/strings",
       "//ui/strings:app_locale_settings",
       "//ui/strings:ui_strings",
     ]