Skip to content

Commit

Permalink
fix: prevent bluetooth device list from growing without bound (#15805)
Browse files Browse the repository at this point in the history
* fix: include bluetooth strings in build

* fix: prevent bluetooth device list from growing without bound
  • Loading branch information
BinaryMuse authored and codebytere committed Nov 28, 2018
1 parent 55808df commit 8f04def
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
56 changes: 34 additions & 22 deletions atom/browser/lib/bluetooth_chooser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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
5 changes: 3 additions & 2 deletions atom/browser/lib/bluetooth_chooser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_
#define ATOM_BROWSER_LIB_BLUETOOTH_CHOOSER_H_

#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions electron_paks.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down

0 comments on commit 8f04def

Please sign in to comment.