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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 base::string16 BluetoothChooserController::GetNoOptionsText() const { | 42 base::string16 BluetoothChooserController::GetNoOptionsText() const { |
43 return no_devices_text_; | 43 return no_devices_text_; |
44 } | 44 } |
45 | 45 |
46 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 46 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
47 return l10n_util::GetStringUTF16( | 47 return l10n_util::GetStringUTF16( |
48 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 48 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
49 } | 49 } |
50 | 50 |
51 size_t BluetoothChooserController::NumOptions() const { | 51 size_t BluetoothChooserController::NumOptions() const { |
52 return device_names_and_ids_.size(); | 52 return device_ids_.size(); |
53 } | 53 } |
54 | 54 |
55 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 55 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
56 DCHECK_LT(index, device_names_and_ids_.size()); | 56 DCHECK_LT(index, device_ids_.size()); |
57 const base::string16& device_name = device_names_and_ids_[index].first; | 57 const std::string& device_id = device_ids_[index]; |
58 const auto& it = device_name_map_.find(device_name); | 58 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
| 59 DCHECK(device_name_it != device_id_to_name_map_.end()); |
| 60 const auto& it = device_name_map_.find(device_name_it->second); |
59 DCHECK(it != device_name_map_.end()); | 61 DCHECK(it != device_name_map_.end()); |
60 return it->second == 1 | 62 return it->second == 1 |
61 ? device_name | 63 ? device_name_it->second |
62 : l10n_util::GetStringFUTF16( | 64 : l10n_util::GetStringFUTF16( |
63 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, | 65 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
64 base::UTF8ToUTF16(device_names_and_ids_[index].second)); | 66 device_name_it->second, base::UTF8ToUTF16(device_id)); |
65 } | 67 } |
66 | 68 |
67 void BluetoothChooserController::RefreshOptions() { | 69 void BluetoothChooserController::RefreshOptions() { |
68 ClearAllDevices(); | 70 ClearAllDevices(); |
69 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | 71 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); |
70 } | 72 } |
71 | 73 |
72 base::string16 BluetoothChooserController::GetStatus() const { | 74 base::string16 BluetoothChooserController::GetStatus() const { |
73 return status_text_; | 75 return status_text_; |
74 } | 76 } |
75 | 77 |
76 void BluetoothChooserController::Select(size_t index) { | 78 void BluetoothChooserController::Select(size_t index) { |
77 DCHECK_LT(index, device_names_and_ids_.size()); | 79 DCHECK_LT(index, device_ids_.size()); |
78 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 80 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
79 device_names_and_ids_[index].second); | 81 device_ids_[index]); |
80 } | 82 } |
81 | 83 |
82 void BluetoothChooserController::Cancel() { | 84 void BluetoothChooserController::Cancel() { |
83 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 85 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
84 std::string()); | 86 std::string()); |
85 } | 87 } |
86 | 88 |
87 void BluetoothChooserController::Close() { | 89 void BluetoothChooserController::Close() { |
88 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 90 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
89 std::string()); | 91 std::string()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 status_text_ = | 143 status_text_ = |
142 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); | 144 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); |
143 if (view()) { | 145 if (view()) { |
144 view()->OnRefreshStateChanged( | 146 view()->OnRefreshStateChanged( |
145 false /* Refreshing options is complete */); | 147 false /* Refreshing options is complete */); |
146 } | 148 } |
147 break; | 149 break; |
148 } | 150 } |
149 } | 151 } |
150 | 152 |
151 void BluetoothChooserController::AddDevice(const std::string& device_id, | 153 void BluetoothChooserController::AddOrUpdateDevice( |
152 const base::string16& device_name) { | 154 const std::string& device_id, |
153 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); | 155 bool should_update_name, |
| 156 const base::string16& device_name, |
| 157 bool is_gatt_connected, |
| 158 bool is_paired, |
| 159 int8_t* rssi) { |
| 160 auto result = device_id_to_name_map_.insert({device_id, device_name}); |
| 161 if (!result.second) { |
| 162 // TODO(ortuno): Update device's information. |
| 163 // https://crbug.com/634366 Update name |
| 164 // http://crbug.com/543466 Update connection and paired status |
| 165 // http://crbug.com/629689 Update RSSI. |
| 166 return; |
| 167 } |
| 168 |
| 169 device_ids_.push_back(device_id); |
154 ++device_name_map_[device_name]; | 170 ++device_name_map_[device_name]; |
155 if (view()) | 171 if (view()) |
156 view()->OnOptionAdded(device_names_and_ids_.size() - 1); | 172 view()->OnOptionAdded(device_ids_.size() - 1); |
157 } | 173 } |
158 | 174 |
159 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 175 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
160 for (auto it = device_names_and_ids_.begin(); | 176 const auto& name_it = device_id_to_name_map_.find(device_id); |
161 it != device_names_and_ids_.end(); ++it) { | 177 if (name_it == device_id_to_name_map_.end()) |
162 if (it->second == device_id) { | 178 return; |
163 size_t index = it - device_names_and_ids_.begin(); | 179 |
164 DCHECK_GT(device_name_map_[it->first], 0); | 180 size_t index = 0; |
165 if (--device_name_map_[it->first] == 0) | 181 for (const auto& saved_device_id : device_ids_) { |
166 device_name_map_.erase(it->first); | 182 if (saved_device_id != device_id) { |
167 device_names_and_ids_.erase(it); | 183 ++index; |
168 if (view()) | 184 continue; |
169 view()->OnOptionRemoved(index); | |
170 return; | |
171 } | 185 } |
| 186 |
| 187 device_ids_.erase(device_ids_.begin() + index); |
| 188 |
| 189 const auto& it = device_name_map_.find(name_it->second); |
| 190 DCHECK(it != device_name_map_.end()); |
| 191 DCHECK_GT(it->second, 0); |
| 192 |
| 193 if (--(it->second) == 0) |
| 194 device_name_map_.erase(it); |
| 195 |
| 196 device_id_to_name_map_.erase(name_it); |
| 197 |
| 198 if (view()) |
| 199 view()->OnOptionRemoved(index); |
| 200 return; |
172 } | 201 } |
173 } | 202 } |
174 | 203 |
175 void BluetoothChooserController::ClearAllDevices() { | 204 void BluetoothChooserController::ClearAllDevices() { |
176 device_names_and_ids_.clear(); | 205 device_ids_.clear(); |
| 206 device_id_to_name_map_.clear(); |
177 device_name_map_.clear(); | 207 device_name_map_.clear(); |
178 } | 208 } |
OLD | NEW |