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
This commit is contained in:
Michelle Tilley 2018-11-28 08:36:00 -08:00 committed by Shelley Vohr
parent 55808dffcd
commit 8f04def7b2
3 changed files with 39 additions and 24 deletions

View file

@ -63,16 +63,17 @@ void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
event_handler_.Run(Event::CANCELLED, ""); event_handler_.Run(Event::CANCELLED, "");
break; break;
case DiscoveryState::IDLE: case DiscoveryState::IDLE:
if (device_list_.empty()) { if (device_map_.empty()) {
auto event = auto event =
++num_retries_ > kMaxScanRetries ? Event::CANCELLED : Event::RESCAN; ++num_retries_ > kMaxScanRetries ? Event::CANCELLED : Event::RESCAN;
event_handler_.Run(event, ""); event_handler_.Run(event, "");
} else { } else {
bool prevent_default = api_web_contents_->Emit( bool prevent_default = api_web_contents_->Emit(
"select-bluetooth-device", device_list_, "select-bluetooth-device", GetDeviceList(),
base::Bind(&OnDeviceChosen, event_handler_)); base::Bind(&OnDeviceChosen, event_handler_));
if (!prevent_default) { 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); event_handler_.Run(Event::SELECTED, device_id);
} }
} }
@ -88,13 +89,21 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
bool is_gatt_connected, bool is_gatt_connected,
bool is_paired, bool is_paired,
int signal_strength_level) { int signal_strength_level) {
DeviceInfo info = {device_id, device_name}; bool changed = false;
device_list_.push_back(info); 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;
}
if (changed) {
// Emit a select-bluetooth-device handler to allow for user to listen for // Emit a select-bluetooth-device handler to allow for user to listen for
// bluetooth device found. // bluetooth device found.
bool prevent_default = bool prevent_default =
api_web_contents_->Emit("select-bluetooth-device", device_list_, api_web_contents_->Emit("select-bluetooth-device", GetDeviceList(),
base::Bind(&OnDeviceChosen, event_handler_)); base::Bind(&OnDeviceChosen, event_handler_));
// If emit not implimented select first device that matches the filters // If emit not implimented select first device that matches the filters
@ -102,15 +111,18 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
if (!prevent_default) { if (!prevent_default) {
event_handler_.Run(Event::SELECTED, device_id); event_handler_.Run(Event::SELECTED, device_id);
} }
}
} }
void BluetoothChooser::RemoveDevice(const std::string& device_id) { std::vector<atom::BluetoothChooser::DeviceInfo>
for (auto it = device_list_.begin(); it != device_list_.end(); ++it) { BluetoothChooser::GetDeviceList() {
if (it->device_id == device_id) { std::vector<atom::BluetoothChooser::DeviceInfo> vec;
device_list_.erase(it); for (const auto& it : device_map_) {
return; DeviceInfo info = {it.first, it.second};
} vec.push_back(info);
} }
return vec;
} }
} // namespace atom } // namespace atom

View file

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_ #ifndef ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
#define ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_ #define ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@ -33,10 +34,10 @@ class BluetoothChooser : public content::BluetoothChooser {
bool is_gatt_connected, bool is_gatt_connected,
bool is_paired, bool is_paired,
int signal_strength_level) override; int signal_strength_level) override;
void RemoveDevice(const std::string& device_id); std::vector<DeviceInfo> GetDeviceList();
private: private:
std::vector<DeviceInfo> device_list_; std::map<std::string, base::string16> device_map_;
api::WebContents* api_web_contents_; api::WebContents* api_web_contents_;
EventHandler event_handler_; EventHandler event_handler_;
int num_retries_ = 0; int num_retries_ = 0;

View file

@ -150,11 +150,13 @@ template("electron_paks") {
"${root_gen_dir}/content/app/strings/content_strings_", "${root_gen_dir}/content/app/strings/content_strings_",
"${root_gen_dir}/ui/strings/app_locale_settings_", "${root_gen_dir}/ui/strings/app_locale_settings_",
"${root_gen_dir}/ui/strings/ui_strings_", "${root_gen_dir}/ui/strings/ui_strings_",
"${root_gen_dir}/device/bluetooth/strings/bluetooth_strings_",
] ]
deps = [ deps = [
"//chrome/app/resources:platform_locale_settings", "//chrome/app/resources:platform_locale_settings",
"//components/strings:components_strings", "//components/strings:components_strings",
"//content/app/strings", "//content/app/strings",
"//device/bluetooth/strings",
"//ui/strings:app_locale_settings", "//ui/strings:app_locale_settings",
"//ui/strings:ui_strings", "//ui/strings:ui_strings",
] ]