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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/net/referrer.h" | 9 #include "chrome/browser/net/referrer.h" |
10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 base::string16 BluetoothChooserController::GetNoOptionsText() const { | 43 base::string16 BluetoothChooserController::GetNoOptionsText() const { |
44 return no_devices_text_; | 44 return no_devices_text_; |
45 } | 45 } |
46 | 46 |
47 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 47 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
48 return l10n_util::GetStringUTF16( | 48 return l10n_util::GetStringUTF16( |
49 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 49 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
50 } | 50 } |
51 | 51 |
52 size_t BluetoothChooserController::NumOptions() const { | 52 size_t BluetoothChooserController::NumOptions() const { |
53 return device_names_and_ids_.size(); | 53 return device_ids_.size(); |
54 } | 54 } |
55 | 55 |
56 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 56 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
57 DCHECK_LT(index, device_names_and_ids_.size()); | 57 DCHECK_LT(index, device_ids_.size()); |
58 const base::string16& device_name = device_names_and_ids_[index].first; | 58 const std::string& device_id = device_ids_[index]; |
59 const auto& it = device_name_map_.find(device_name); | 59 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
| 60 DCHECK(device_name_it != device_id_to_name_map_.end()); |
| 61 const auto& it = device_name_map_.find(device_name_it->second); |
60 DCHECK(it != device_name_map_.end()); | 62 DCHECK(it != device_name_map_.end()); |
61 return it->second == 1 | 63 return it->second == 1 |
62 ? device_name | 64 ? device_name_it->second |
63 : l10n_util::GetStringFUTF16( | 65 : l10n_util::GetStringFUTF16( |
64 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, | 66 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
65 base::UTF8ToUTF16(device_names_and_ids_[index].second)); | 67 device_name_it->second, base::UTF8ToUTF16(device_id)); |
66 } | 68 } |
67 | 69 |
68 void BluetoothChooserController::RefreshOptions() { | 70 void BluetoothChooserController::RefreshOptions() { |
69 if (event_handler_.is_null()) | 71 if (event_handler_.is_null()) |
70 return; | 72 return; |
71 ClearAllDevices(); | 73 ClearAllDevices(); |
72 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | 74 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); |
73 } | 75 } |
74 | 76 |
75 base::string16 BluetoothChooserController::GetStatus() const { | 77 base::string16 BluetoothChooserController::GetStatus() const { |
76 return status_text_; | 78 return status_text_; |
77 } | 79 } |
78 | 80 |
79 void BluetoothChooserController::Select(size_t index) { | 81 void BluetoothChooserController::Select(size_t index) { |
80 if (event_handler_.is_null()) { | 82 if (event_handler_.is_null()) { |
81 content::RecordRequestDeviceOutcome( | 83 content::RecordRequestDeviceOutcome( |
82 content::UMARequestDeviceOutcome:: | 84 content::UMARequestDeviceOutcome:: |
83 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); | 85 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); |
84 return; | 86 return; |
85 } | 87 } |
86 DCHECK_LT(index, device_names_and_ids_.size()); | 88 DCHECK_LT(index, device_ids_.size()); |
87 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 89 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
88 device_names_and_ids_[index].second); | 90 device_ids_[index]); |
89 } | 91 } |
90 | 92 |
91 void BluetoothChooserController::Cancel() { | 93 void BluetoothChooserController::Cancel() { |
92 if (event_handler_.is_null()) | 94 if (event_handler_.is_null()) |
93 return; | 95 return; |
94 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 96 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
95 std::string()); | 97 std::string()); |
96 } | 98 } |
97 | 99 |
98 void BluetoothChooserController::Close() { | 100 void BluetoothChooserController::Close() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 status_text_ = | 156 status_text_ = |
155 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); | 157 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); |
156 if (view()) { | 158 if (view()) { |
157 view()->OnRefreshStateChanged( | 159 view()->OnRefreshStateChanged( |
158 false /* Refreshing options is complete */); | 160 false /* Refreshing options is complete */); |
159 } | 161 } |
160 break; | 162 break; |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 void BluetoothChooserController::AddDevice(const std::string& device_id, | 166 void BluetoothChooserController::AddOrUpdateDevice( |
165 const base::string16& device_name) { | 167 const std::string& device_id, |
166 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); | 168 bool should_update_name, |
| 169 const base::string16& device_name, |
| 170 bool is_gatt_connected, |
| 171 bool is_paired, |
| 172 const int8_t* rssi) { |
| 173 auto result = device_id_to_name_map_.insert({device_id, device_name}); |
| 174 if (!result.second) { |
| 175 // TODO(ortuno): Update device's information. |
| 176 // https://crbug.com/634366 Update name |
| 177 // http://crbug.com/543466 Update connection and paired status |
| 178 // http://crbug.com/629689 Update RSSI. |
| 179 return; |
| 180 } |
| 181 |
| 182 device_ids_.push_back(device_id); |
167 ++device_name_map_[device_name]; | 183 ++device_name_map_[device_name]; |
168 if (view()) | 184 if (view()) |
169 view()->OnOptionAdded(device_names_and_ids_.size() - 1); | 185 view()->OnOptionAdded(device_ids_.size() - 1); |
170 } | 186 } |
171 | 187 |
172 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 188 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
173 for (auto it = device_names_and_ids_.begin(); | 189 const auto& name_it = device_id_to_name_map_.find(device_id); |
174 it != device_names_and_ids_.end(); ++it) { | 190 if (name_it == device_id_to_name_map_.end()) |
175 if (it->second == device_id) { | 191 return; |
176 size_t index = it - device_names_and_ids_.begin(); | 192 |
177 DCHECK_GT(device_name_map_[it->first], 0); | 193 size_t index = 0; |
178 if (--device_name_map_[it->first] == 0) | 194 for (const auto& saved_device_id : device_ids_) { |
179 device_name_map_.erase(it->first); | 195 if (saved_device_id != device_id) { |
180 device_names_and_ids_.erase(it); | 196 ++index; |
181 if (view()) | 197 continue; |
182 view()->OnOptionRemoved(index); | |
183 return; | |
184 } | 198 } |
| 199 |
| 200 device_ids_.erase(device_ids_.begin() + index); |
| 201 |
| 202 const auto& it = device_name_map_.find(name_it->second); |
| 203 DCHECK(it != device_name_map_.end()); |
| 204 DCHECK_GT(it->second, 0); |
| 205 |
| 206 if (--(it->second) == 0) |
| 207 device_name_map_.erase(it); |
| 208 |
| 209 device_id_to_name_map_.erase(name_it); |
| 210 |
| 211 if (view()) |
| 212 view()->OnOptionRemoved(index); |
| 213 return; |
185 } | 214 } |
186 } | 215 } |
187 | 216 |
188 void BluetoothChooserController::ResetEventHandler() { | 217 void BluetoothChooserController::ResetEventHandler() { |
189 event_handler_.Reset(); | 218 event_handler_.Reset(); |
190 } | 219 } |
191 | 220 |
192 void BluetoothChooserController::ClearAllDevices() { | 221 void BluetoothChooserController::ClearAllDevices() { |
193 device_names_and_ids_.clear(); | 222 device_ids_.clear(); |
| 223 device_id_to_name_map_.clear(); |
194 device_name_map_.clear(); | 224 device_name_map_.clear(); |
195 } | 225 } |
OLD | NEW |