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> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 const content::BluetoothChooser::EventHandler& event_handler) | 35 const content::BluetoothChooser::EventHandler& event_handler) |
36 : ChooserController(owner, | 36 : ChooserController(owner, |
37 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, | 37 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, |
38 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 38 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
39 event_handler_(event_handler), | 39 event_handler_(event_handler), |
40 no_devices_text_(l10n_util::GetStringUTF16( | 40 no_devices_text_(l10n_util::GetStringUTF16( |
41 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} | 41 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} |
42 | 42 |
43 BluetoothChooserController::~BluetoothChooserController() {} | 43 BluetoothChooserController::~BluetoothChooserController() {} |
44 | 44 |
| 45 bool BluetoothChooserController::ShouldShowIconBeforeText() const { |
| 46 return true; |
| 47 } |
| 48 |
45 base::string16 BluetoothChooserController::GetNoOptionsText() const { | 49 base::string16 BluetoothChooserController::GetNoOptionsText() const { |
46 return no_devices_text_; | 50 return no_devices_text_; |
47 } | 51 } |
48 | 52 |
49 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 53 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
50 return l10n_util::GetStringUTF16( | 54 return l10n_util::GetStringUTF16( |
51 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 55 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
52 } | 56 } |
53 | 57 |
54 size_t BluetoothChooserController::NumOptions() const { | 58 size_t BluetoothChooserController::NumOptions() const { |
55 return device_ids_.size(); | 59 return devices_.size(); |
| 60 } |
| 61 |
| 62 int BluetoothChooserController::GetSignalStrengthLevel(size_t index) const { |
| 63 return devices_[index].signal_strength_level; |
56 } | 64 } |
57 | 65 |
58 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 66 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
59 DCHECK_LT(index, device_ids_.size()); | 67 DCHECK_LT(index, devices_.size()); |
60 const std::string& device_id = device_ids_[index]; | 68 const std::string& device_id = devices_[index].id; |
61 const auto& device_name_it = device_id_to_name_map_.find(device_id); | 69 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
62 DCHECK(device_name_it != device_id_to_name_map_.end()); | 70 DCHECK(device_name_it != device_id_to_name_map_.end()); |
63 const auto& it = device_name_counts_.find(device_name_it->second); | 71 const auto& it = device_name_counts_.find(device_name_it->second); |
64 DCHECK(it != device_name_counts_.end()); | 72 DCHECK(it != device_name_counts_.end()); |
65 return it->second == 1 | 73 return it->second == 1 |
66 ? device_name_it->second | 74 ? device_name_it->second |
67 : l10n_util::GetStringFUTF16( | 75 : l10n_util::GetStringFUTF16( |
68 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, | 76 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
69 device_name_it->second, base::UTF8ToUTF16(device_id)); | 77 device_name_it->second, base::UTF8ToUTF16(device_id)); |
70 } | 78 } |
71 | 79 |
72 void BluetoothChooserController::RefreshOptions() { | 80 void BluetoothChooserController::RefreshOptions() { |
73 if (event_handler_.is_null()) | 81 if (event_handler_.is_null()) |
74 return; | 82 return; |
75 ClearAllDevices(); | 83 ClearAllDevices(); |
76 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | 84 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); |
77 } | 85 } |
78 | 86 |
79 base::string16 BluetoothChooserController::GetStatus() const { | 87 base::string16 BluetoothChooserController::GetStatus() const { |
80 return status_text_; | 88 return status_text_; |
81 } | 89 } |
82 | 90 |
83 void BluetoothChooserController::Select(size_t index) { | 91 void BluetoothChooserController::Select(size_t index) { |
84 if (event_handler_.is_null()) { | 92 if (event_handler_.is_null()) { |
85 content::RecordRequestDeviceOutcome( | 93 content::RecordRequestDeviceOutcome( |
86 content::UMARequestDeviceOutcome:: | 94 content::UMARequestDeviceOutcome:: |
87 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); | 95 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); |
88 return; | 96 return; |
89 } | 97 } |
90 DCHECK_LT(index, device_ids_.size()); | 98 DCHECK_LT(index, devices_.size()); |
91 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 99 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
92 device_ids_[index]); | 100 devices_[index].id); |
93 } | 101 } |
94 | 102 |
95 void BluetoothChooserController::Cancel() { | 103 void BluetoothChooserController::Cancel() { |
96 if (event_handler_.is_null()) | 104 if (event_handler_.is_null()) |
97 return; | 105 return; |
98 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 106 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
99 std::string()); | 107 std::string()); |
100 } | 108 } |
101 | 109 |
102 void BluetoothChooserController::Close() { | 110 void BluetoothChooserController::Close() { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 break; | 172 break; |
165 } | 173 } |
166 } | 174 } |
167 | 175 |
168 void BluetoothChooserController::AddOrUpdateDevice( | 176 void BluetoothChooserController::AddOrUpdateDevice( |
169 const std::string& device_id, | 177 const std::string& device_id, |
170 bool should_update_name, | 178 bool should_update_name, |
171 const base::string16& device_name, | 179 const base::string16& device_name, |
172 bool is_gatt_connected, | 180 bool is_gatt_connected, |
173 bool is_paired, | 181 bool is_paired, |
174 const int8_t* rssi) { | 182 int signal_strength_level) { |
175 auto name_it = device_id_to_name_map_.find(device_id); | 183 auto name_it = device_id_to_name_map_.find(device_id); |
176 if (name_it != device_id_to_name_map_.end()) { | 184 if (name_it != device_id_to_name_map_.end()) { |
177 if (should_update_name) { | 185 if (should_update_name) { |
178 base::string16 previous_device_name = name_it->second; | 186 base::string16 previous_device_name = name_it->second; |
179 name_it->second = device_name; | 187 name_it->second = device_name; |
180 | 188 |
181 const auto& it = device_name_counts_.find(previous_device_name); | 189 const auto& it = device_name_counts_.find(previous_device_name); |
182 DCHECK(it != device_name_counts_.end()); | 190 DCHECK(it != device_name_counts_.end()); |
183 DCHECK_GT(it->second, 0); | 191 DCHECK_GT(it->second, 0); |
184 | 192 |
185 if (--(it->second) == 0) | 193 if (--(it->second) == 0) |
186 device_name_counts_.erase(it); | 194 device_name_counts_.erase(it); |
187 | 195 |
188 ++device_name_counts_[device_name]; | 196 ++device_name_counts_[device_name]; |
189 } | 197 } |
190 | 198 |
191 size_t index = std::distance( | 199 auto device_it = |
192 device_ids_.begin(), | 200 std::find_if(devices_.begin(), devices_.end(), |
193 std::find(device_ids_.begin(), device_ids_.end(), device_id)); | 201 [&device_id](const BluetoothDeviceInfo& device) { |
194 DCHECK_NE(device_ids_.size(), index); | 202 return device.id == device_id; |
| 203 }); |
| 204 |
| 205 DCHECK(device_it != devices_.end()); |
195 // http://crbug.com/543466 Update connection and paired status | 206 // http://crbug.com/543466 Update connection and paired status |
196 // http://crbug.com/629689 Update RSSI. | 207 |
| 208 // When Bluetooth device scanning stops, the |signal_strength_level| |
| 209 // is -1, and in this case, should still use the previously stored |
| 210 // signal strength level value. |
| 211 if (signal_strength_level != -1) |
| 212 device_it->signal_strength_level = signal_strength_level; |
197 if (view()) | 213 if (view()) |
198 view()->OnOptionUpdated(index); | 214 view()->OnOptionUpdated(device_it - devices_.begin()); |
199 return; | 215 return; |
200 } | 216 } |
201 | 217 |
202 device_ids_.push_back(device_id); | 218 devices_.push_back({device_id, signal_strength_level}); |
203 device_id_to_name_map_.insert({device_id, device_name}); | 219 device_id_to_name_map_.insert({device_id, device_name}); |
204 ++device_name_counts_[device_name]; | 220 ++device_name_counts_[device_name]; |
205 if (view()) | 221 if (view()) |
206 view()->OnOptionAdded(device_ids_.size() - 1); | 222 view()->OnOptionAdded(devices_.size() - 1); |
207 } | 223 } |
208 | 224 |
209 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 225 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
210 const auto& name_it = device_id_to_name_map_.find(device_id); | 226 const auto& name_it = device_id_to_name_map_.find(device_id); |
211 if (name_it == device_id_to_name_map_.end()) | 227 if (name_it == device_id_to_name_map_.end()) |
212 return; | 228 return; |
213 | 229 |
214 size_t index = 0; | 230 auto device_it = |
215 for (const auto& saved_device_id : device_ids_) { | 231 std::find_if(devices_.begin(), devices_.end(), |
216 if (saved_device_id != device_id) { | 232 [&device_id](const BluetoothDeviceInfo& device) { |
217 ++index; | 233 return device.id == device_id; |
218 continue; | 234 }); |
219 } | |
220 | 235 |
221 device_ids_.erase(device_ids_.begin() + index); | 236 if (device_it != devices_.end()) { |
| 237 devices_.erase(device_it); |
222 | 238 |
223 const auto& it = device_name_counts_.find(name_it->second); | 239 const auto& it = device_name_counts_.find(name_it->second); |
224 DCHECK(it != device_name_counts_.end()); | 240 DCHECK(it != device_name_counts_.end()); |
225 DCHECK_GT(it->second, 0); | 241 DCHECK_GT(it->second, 0); |
226 | 242 |
227 if (--(it->second) == 0) | 243 if (--(it->second) == 0) |
228 device_name_counts_.erase(it); | 244 device_name_counts_.erase(it); |
229 | 245 |
230 device_id_to_name_map_.erase(name_it); | 246 device_id_to_name_map_.erase(name_it); |
231 | 247 |
232 if (view()) | 248 if (view()) |
233 view()->OnOptionRemoved(index); | 249 view()->OnOptionRemoved(device_it - devices_.begin()); |
234 return; | |
235 } | 250 } |
236 } | 251 } |
237 | 252 |
238 void BluetoothChooserController::ResetEventHandler() { | 253 void BluetoothChooserController::ResetEventHandler() { |
239 event_handler_.Reset(); | 254 event_handler_.Reset(); |
240 } | 255 } |
241 | 256 |
242 void BluetoothChooserController::ClearAllDevices() { | 257 void BluetoothChooserController::ClearAllDevices() { |
243 device_ids_.clear(); | 258 devices_.clear(); |
244 device_id_to_name_map_.clear(); | 259 device_id_to_name_map_.clear(); |
245 device_name_counts_.clear(); | 260 device_name_counts_.clear(); |
246 } | 261 } |
OLD | NEW |