OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h" | 5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/net/referrer.h" | 11 #include "chrome/browser/net/referrer.h" |
10 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
11 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" | 13 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" |
12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 15 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
14 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
15 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
16 #include "content/browser/bluetooth/bluetooth_metrics.h" | 18 #include "content/browser/bluetooth/bluetooth_metrics.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 53 |
52 size_t BluetoothChooserController::NumOptions() const { | 54 size_t BluetoothChooserController::NumOptions() const { |
53 return device_ids_.size(); | 55 return device_ids_.size(); |
54 } | 56 } |
55 | 57 |
56 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 58 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
57 DCHECK_LT(index, device_ids_.size()); | 59 DCHECK_LT(index, device_ids_.size()); |
58 const std::string& device_id = device_ids_[index]; | 60 const std::string& device_id = device_ids_[index]; |
59 const auto& device_name_it = device_id_to_name_map_.find(device_id); | 61 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
60 DCHECK(device_name_it != device_id_to_name_map_.end()); | 62 DCHECK(device_name_it != device_id_to_name_map_.end()); |
61 const auto& it = device_name_map_.find(device_name_it->second); | 63 const auto& it = device_name_counts_.find(device_name_it->second); |
62 DCHECK(it != device_name_map_.end()); | 64 DCHECK(it != device_name_counts_.end()); |
63 return it->second == 1 | 65 return it->second == 1 |
64 ? device_name_it->second | 66 ? device_name_it->second |
65 : l10n_util::GetStringFUTF16( | 67 : l10n_util::GetStringFUTF16( |
66 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, | 68 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
67 device_name_it->second, base::UTF8ToUTF16(device_id)); | 69 device_name_it->second, base::UTF8ToUTF16(device_id)); |
68 } | 70 } |
69 | 71 |
70 void BluetoothChooserController::RefreshOptions() { | 72 void BluetoothChooserController::RefreshOptions() { |
71 if (event_handler_.is_null()) | 73 if (event_handler_.is_null()) |
72 return; | 74 return; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 165 } |
164 } | 166 } |
165 | 167 |
166 void BluetoothChooserController::AddOrUpdateDevice( | 168 void BluetoothChooserController::AddOrUpdateDevice( |
167 const std::string& device_id, | 169 const std::string& device_id, |
168 bool should_update_name, | 170 bool should_update_name, |
169 const base::string16& device_name, | 171 const base::string16& device_name, |
170 bool is_gatt_connected, | 172 bool is_gatt_connected, |
171 bool is_paired, | 173 bool is_paired, |
172 const int8_t* rssi) { | 174 const int8_t* rssi) { |
173 auto result = device_id_to_name_map_.insert({device_id, device_name}); | 175 auto name_it = device_id_to_name_map_.find(device_id); |
174 if (!result.second) { | 176 if (name_it != device_id_to_name_map_.end()) { |
175 // TODO(ortuno): Update device's information. | 177 if (should_update_name) { |
176 // https://crbug.com/634366 Update name | 178 base::string16 previous_device_name = name_it->second; |
| 179 name_it->second = device_name; |
| 180 |
| 181 const auto& it = device_name_counts_.find(previous_device_name); |
| 182 DCHECK(it != device_name_counts_.end()); |
| 183 DCHECK_GT(it->second, 0); |
| 184 |
| 185 if (--(it->second) == 0) |
| 186 device_name_counts_.erase(it); |
| 187 |
| 188 ++device_name_counts_[device_name]; |
| 189 } |
| 190 |
| 191 size_t index = std::distance( |
| 192 device_ids_.begin(), |
| 193 std::find(device_ids_.begin(), device_ids_.end(), device_id)); |
| 194 DCHECK_NE(device_ids_.size(), index); |
177 // http://crbug.com/543466 Update connection and paired status | 195 // http://crbug.com/543466 Update connection and paired status |
178 // http://crbug.com/629689 Update RSSI. | 196 // http://crbug.com/629689 Update RSSI. |
| 197 if (view()) |
| 198 view()->OnOptionUpdated(index); |
179 return; | 199 return; |
180 } | 200 } |
181 | 201 |
182 device_ids_.push_back(device_id); | 202 device_ids_.push_back(device_id); |
183 ++device_name_map_[device_name]; | 203 device_id_to_name_map_.insert({device_id, device_name}); |
| 204 ++device_name_counts_[device_name]; |
184 if (view()) | 205 if (view()) |
185 view()->OnOptionAdded(device_ids_.size() - 1); | 206 view()->OnOptionAdded(device_ids_.size() - 1); |
186 } | 207 } |
187 | 208 |
188 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 209 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
189 const auto& name_it = device_id_to_name_map_.find(device_id); | 210 const auto& name_it = device_id_to_name_map_.find(device_id); |
190 if (name_it == device_id_to_name_map_.end()) | 211 if (name_it == device_id_to_name_map_.end()) |
191 return; | 212 return; |
192 | 213 |
193 size_t index = 0; | 214 size_t index = 0; |
194 for (const auto& saved_device_id : device_ids_) { | 215 for (const auto& saved_device_id : device_ids_) { |
195 if (saved_device_id != device_id) { | 216 if (saved_device_id != device_id) { |
196 ++index; | 217 ++index; |
197 continue; | 218 continue; |
198 } | 219 } |
199 | 220 |
200 device_ids_.erase(device_ids_.begin() + index); | 221 device_ids_.erase(device_ids_.begin() + index); |
201 | 222 |
202 const auto& it = device_name_map_.find(name_it->second); | 223 const auto& it = device_name_counts_.find(name_it->second); |
203 DCHECK(it != device_name_map_.end()); | 224 DCHECK(it != device_name_counts_.end()); |
204 DCHECK_GT(it->second, 0); | 225 DCHECK_GT(it->second, 0); |
205 | 226 |
206 if (--(it->second) == 0) | 227 if (--(it->second) == 0) |
207 device_name_map_.erase(it); | 228 device_name_counts_.erase(it); |
208 | 229 |
209 device_id_to_name_map_.erase(name_it); | 230 device_id_to_name_map_.erase(name_it); |
210 | 231 |
211 if (view()) | 232 if (view()) |
212 view()->OnOptionRemoved(index); | 233 view()->OnOptionRemoved(index); |
213 return; | 234 return; |
214 } | 235 } |
215 } | 236 } |
216 | 237 |
217 void BluetoothChooserController::ResetEventHandler() { | 238 void BluetoothChooserController::ResetEventHandler() { |
218 event_handler_.Reset(); | 239 event_handler_.Reset(); |
219 } | 240 } |
220 | 241 |
221 void BluetoothChooserController::ClearAllDevices() { | 242 void BluetoothChooserController::ClearAllDevices() { |
222 device_ids_.clear(); | 243 device_ids_.clear(); |
223 device_id_to_name_map_.clear(); | 244 device_id_to_name_map_.clear(); |
224 device_name_map_.clear(); | 245 device_name_counts_.clear(); |
225 } | 246 } |
OLD | NEW |