Index: chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
diff --git a/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
index f8c98f6166bd12c38b3cb99b6e5e16635b6893e8..4df2f33eb7bef8d6c020cc420fa52c2b288eaae5 100644 |
--- a/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
+++ b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc |
@@ -49,19 +49,20 @@ base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
} |
size_t BluetoothChooserController::NumOptions() const { |
- return device_names_and_ids_.size(); |
+ return device_ids_.size(); |
} |
base::string16 BluetoothChooserController::GetOption(size_t index) const { |
- DCHECK_LT(index, device_names_and_ids_.size()); |
- const base::string16& device_name = device_names_and_ids_[index].first; |
- const auto& it = device_name_map_.find(device_name); |
+ DCHECK_LT(index, device_ids_.size()); |
+ const std::string& device_id = device_ids_[index]; |
+ const auto& device_name_it = device_id_to_name_map_.find(device_id); |
juncai
2016/08/04 21:52:23
nit: DCHECK(device_name_it != device_id_to_name_ma
ortuno
2016/08/05 02:16:09
Done.
|
+ const auto& it = device_name_map_.find(device_name_it->second); |
DCHECK(it != device_name_map_.end()); |
return it->second == 1 |
- ? device_name |
+ ? device_name_it->second |
: l10n_util::GetStringFUTF16( |
- IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, |
- base::UTF8ToUTF16(device_names_and_ids_[index].second)); |
+ IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
+ device_name_it->second, base::UTF8ToUTF16(device_id)); |
} |
void BluetoothChooserController::RefreshOptions() { |
@@ -74,9 +75,9 @@ base::string16 BluetoothChooserController::GetStatus() const { |
} |
void BluetoothChooserController::Select(size_t index) { |
- DCHECK_LT(index, device_names_and_ids_.size()); |
+ DCHECK_LT(index, device_ids_.size()); |
event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
- device_names_and_ids_[index].second); |
+ device_ids_[index]); |
} |
void BluetoothChooserController::Cancel() { |
@@ -148,31 +149,56 @@ void BluetoothChooserController::OnDiscoveryStateChanged( |
} |
} |
-void BluetoothChooserController::AddDevice(const std::string& device_id, |
- const base::string16& device_name) { |
- device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); |
+void BluetoothChooserController::AddOrUpdateDevice( |
+ const std::string& device_id, |
+ const base::string16& device_name) { |
+ auto result = device_id_to_name_map_.insert({device_id, device_name}); |
+ if (!result.second) { |
+ // TODO(ortuno): Update device's name if necessary. |
+ // https://crbug.com/634366 |
+ return; |
+ } |
+ |
+ device_ids_.push_back(device_id); |
++device_name_map_[device_name]; |
if (view()) |
- view()->OnOptionAdded(device_names_and_ids_.size() - 1); |
+ view()->OnOptionAdded(device_ids_.size() - 1); |
} |
void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
- for (auto it = device_names_and_ids_.begin(); |
- it != device_names_and_ids_.end(); ++it) { |
- if (it->second == device_id) { |
- size_t index = it - device_names_and_ids_.begin(); |
- DCHECK_GT(device_name_map_[it->first], 0); |
- if (--device_name_map_[it->first] == 0) |
- device_name_map_.erase(it->first); |
- device_names_and_ids_.erase(it); |
- if (view()) |
- view()->OnOptionRemoved(index); |
- return; |
+ const auto& name_it = device_id_to_name_map_.find(device_id); |
+ if (name_it == device_id_to_name_map_.end()) { |
juncai
2016/08/04 21:52:23
nit: no {} for single line if statement.
ortuno
2016/08/05 02:16:08
Done.
|
+ return; |
+ } |
+ |
+ int index = 0; |
juncai
2016/08/04 21:52:23
use size_t instead of int.
ortuno
2016/08/05 02:16:09
Done.
|
+ for (const auto& saved_device_id : device_ids_) { |
+ if (saved_device_id != device_id) { |
+ index++; |
juncai
2016/08/04 21:52:23
nit: ++index;
ortuno
2016/08/05 02:16:09
Done.
|
+ continue; |
+ } |
+ |
+ device_ids_.erase(device_ids_.begin() + index); |
+ |
+ const auto& it = device_name_map_.find(name_it->second); |
+ DCHECK(it != device_name_map_.end()); |
+ DCHECK_GT(it->second, 0); |
+ --(it->second); |
+ |
+ if (it->second == 0) { |
juncai
2016/08/04 21:52:25
nit: use if (--it->second == 0) ?
no {} for singl
ortuno
2016/08/05 02:16:09
Done.
|
+ device_name_map_.erase(it); |
} |
+ |
+ device_id_to_name_map_.erase(name_it); |
+ |
+ if (view()) |
+ view()->OnOptionRemoved(index); |
+ return; |
} |
} |
void BluetoothChooserController::ClearAllDevices() { |
- device_names_and_ids_.clear(); |
+ device_ids_.clear(); |
+ device_id_to_name_map_.clear(); |
device_name_map_.clear(); |
} |