Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 Browser* GetBrowser() { | 24 Browser* GetBrowser() { |
| 25 chrome::ScopedTabbedBrowserDisplayer browser_displayer( | 25 chrome::ScopedTabbedBrowserDisplayer browser_displayer( |
| 26 ProfileManager::GetActiveUserProfile()); | 26 ProfileManager::GetActiveUserProfile()); |
| 27 DCHECK(browser_displayer.browser()); | 27 DCHECK(browser_displayer.browser()); |
| 28 return browser_displayer.browser(); | 28 return browser_displayer.browser(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Anything worse than or equal to this will show 0 bars. | |
| 32 const int kMinRSSI = -100; | |
| 33 // Anything better than or equal to this will show the maximum bars. | |
| 34 const int kMaxRSSI = -55; | |
| 35 // Number of RSSI levels used in the signal strength icon. | |
| 36 const int kNumLevels = 5; | |
| 37 | |
| 38 // Received Signal Strength Indicator (RSSI) is a measurement of the power | |
| 39 // present in a received radio signal. | |
| 40 int CalculateSignalLevel(int8_t rssi) { | |
|
Jeffrey Yasskin
2016/08/19 17:46:54
Write a test that this returns the right levels.
ortuno
2016/08/19 17:58:36
Could you actually move this to BluetoothDeviceCho
juncai
2016/08/19 22:59:17
Done.
juncai
2016/08/19 22:59:17
Done.
| |
| 41 if (rssi <= kMinRSSI) | |
| 42 return 0; | |
| 43 | |
| 44 if (rssi >= kMaxRSSI) | |
| 45 return kNumLevels - 1; | |
| 46 | |
| 47 double input_range = kMaxRSSI - kMinRSSI; | |
| 48 double output_range = kNumLevels - 1; | |
| 49 return static_cast<int>((rssi - kMinRSSI) * output_range / input_range); | |
| 50 } | |
| 51 | |
| 31 } // namespace | 52 } // namespace |
| 32 | 53 |
| 33 BluetoothChooserController::BluetoothChooserController( | 54 BluetoothChooserController::BluetoothChooserController( |
| 34 content::RenderFrameHost* owner, | 55 content::RenderFrameHost* owner, |
| 35 const content::BluetoothChooser::EventHandler& event_handler) | 56 const content::BluetoothChooser::EventHandler& event_handler) |
| 36 : ChooserController(owner, | 57 : ChooserController(owner, |
| 37 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, | 58 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, |
| 38 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 59 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
| 39 event_handler_(event_handler), | 60 event_handler_(event_handler), |
| 40 no_devices_text_(l10n_util::GetStringUTF16( | 61 no_devices_text_(l10n_util::GetStringUTF16( |
| 41 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} | 62 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} |
| 42 | 63 |
| 43 BluetoothChooserController::~BluetoothChooserController() {} | 64 BluetoothChooserController::~BluetoothChooserController() {} |
| 44 | 65 |
| 66 bool BluetoothChooserController::ShouldShowIconBeforeText() const { | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 45 base::string16 BluetoothChooserController::GetNoOptionsText() const { | 70 base::string16 BluetoothChooserController::GetNoOptionsText() const { |
| 46 return no_devices_text_; | 71 return no_devices_text_; |
| 47 } | 72 } |
| 48 | 73 |
| 49 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 74 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
| 50 return l10n_util::GetStringUTF16( | 75 return l10n_util::GetStringUTF16( |
| 51 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 76 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
| 52 } | 77 } |
| 53 | 78 |
| 54 size_t BluetoothChooserController::NumOptions() const { | 79 size_t BluetoothChooserController::NumOptions() const { |
| 55 return device_ids_.size(); | 80 return devices_.size(); |
| 81 } | |
| 82 | |
| 83 int BluetoothChooserController::GetSignalStrengthLevel(size_t index) const { | |
| 84 return devices_[index].signal_strength_level; | |
| 56 } | 85 } |
| 57 | 86 |
| 58 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 87 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
| 59 DCHECK_LT(index, device_ids_.size()); | 88 DCHECK_LT(index, devices_.size()); |
| 60 const std::string& device_id = device_ids_[index]; | 89 const std::string& device_id = devices_[index].id; |
| 61 const auto& device_name_it = device_id_to_name_map_.find(device_id); | 90 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
| 62 DCHECK(device_name_it != device_id_to_name_map_.end()); | 91 DCHECK(device_name_it != device_id_to_name_map_.end()); |
| 63 const auto& it = device_name_counts_.find(device_name_it->second); | 92 const auto& it = device_name_counts_.find(device_name_it->second); |
| 64 DCHECK(it != device_name_counts_.end()); | 93 DCHECK(it != device_name_counts_.end()); |
| 65 return it->second == 1 | 94 return it->second == 1 |
| 66 ? device_name_it->second | 95 ? device_name_it->second |
| 67 : l10n_util::GetStringFUTF16( | 96 : l10n_util::GetStringFUTF16( |
| 68 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, | 97 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
| 69 device_name_it->second, base::UTF8ToUTF16(device_id)); | 98 device_name_it->second, base::UTF8ToUTF16(device_id)); |
| 70 } | 99 } |
| 71 | 100 |
| 72 void BluetoothChooserController::RefreshOptions() { | 101 void BluetoothChooserController::RefreshOptions() { |
| 73 if (event_handler_.is_null()) | 102 if (event_handler_.is_null()) |
| 74 return; | 103 return; |
| 75 ClearAllDevices(); | 104 ClearAllDevices(); |
| 76 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | 105 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); |
| 77 } | 106 } |
| 78 | 107 |
| 79 base::string16 BluetoothChooserController::GetStatus() const { | 108 base::string16 BluetoothChooserController::GetStatus() const { |
| 80 return status_text_; | 109 return status_text_; |
| 81 } | 110 } |
| 82 | 111 |
| 83 void BluetoothChooserController::Select(size_t index) { | 112 void BluetoothChooserController::Select(size_t index) { |
| 84 if (event_handler_.is_null()) { | 113 if (event_handler_.is_null()) { |
| 85 content::RecordRequestDeviceOutcome( | 114 content::RecordRequestDeviceOutcome( |
| 86 content::UMARequestDeviceOutcome:: | 115 content::UMARequestDeviceOutcome:: |
| 87 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); | 116 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); |
| 88 return; | 117 return; |
| 89 } | 118 } |
| 90 DCHECK_LT(index, device_ids_.size()); | 119 DCHECK_LT(index, devices_.size()); |
| 91 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 120 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
| 92 device_ids_[index]); | 121 devices_[index].id); |
| 93 } | 122 } |
| 94 | 123 |
| 95 void BluetoothChooserController::Cancel() { | 124 void BluetoothChooserController::Cancel() { |
| 96 if (event_handler_.is_null()) | 125 if (event_handler_.is_null()) |
| 97 return; | 126 return; |
| 98 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 127 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
| 99 std::string()); | 128 std::string()); |
| 100 } | 129 } |
| 101 | 130 |
| 102 void BluetoothChooserController::Close() { | 131 void BluetoothChooserController::Close() { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 } | 194 } |
| 166 } | 195 } |
| 167 | 196 |
| 168 void BluetoothChooserController::AddOrUpdateDevice( | 197 void BluetoothChooserController::AddOrUpdateDevice( |
| 169 const std::string& device_id, | 198 const std::string& device_id, |
| 170 bool should_update_name, | 199 bool should_update_name, |
| 171 const base::string16& device_name, | 200 const base::string16& device_name, |
| 172 bool is_gatt_connected, | 201 bool is_gatt_connected, |
| 173 bool is_paired, | 202 bool is_paired, |
| 174 const int8_t* rssi) { | 203 const int8_t* rssi) { |
| 204 int level = rssi ? CalculateSignalLevel(*rssi) : -1; | |
|
Jeffrey Yasskin
2016/08/19 17:46:54
Name this either signal_level or signal_strength_l
juncai
2016/08/19 22:59:17
Done.
| |
| 175 auto name_it = device_id_to_name_map_.find(device_id); | 205 auto name_it = device_id_to_name_map_.find(device_id); |
| 176 if (name_it != device_id_to_name_map_.end()) { | 206 if (name_it != device_id_to_name_map_.end()) { |
| 177 if (should_update_name) { | 207 if (should_update_name) { |
| 178 base::string16 previous_device_name = name_it->second; | 208 base::string16 previous_device_name = name_it->second; |
| 179 name_it->second = device_name; | 209 name_it->second = device_name; |
| 180 | 210 |
| 181 const auto& it = device_name_counts_.find(previous_device_name); | 211 const auto& it = device_name_counts_.find(previous_device_name); |
| 182 DCHECK(it != device_name_counts_.end()); | 212 DCHECK(it != device_name_counts_.end()); |
| 183 DCHECK_GT(it->second, 0); | 213 DCHECK_GT(it->second, 0); |
| 184 | 214 |
| 185 if (--(it->second) == 0) | 215 if (--(it->second) == 0) |
| 186 device_name_counts_.erase(it); | 216 device_name_counts_.erase(it); |
| 187 | 217 |
| 188 ++device_name_counts_[device_name]; | 218 ++device_name_counts_[device_name]; |
| 189 } | 219 } |
| 190 | 220 |
| 191 size_t index = std::distance( | 221 size_t index; |
| 192 device_ids_.begin(), | 222 for (index = 0; index < devices_.size(); ++index) { |
|
Jeffrey Yasskin
2016/08/19 17:46:54
You could use
auto device_it = std::find_if(device
juncai
2016/08/19 22:59:17
Done.
| |
| 193 std::find(device_ids_.begin(), device_ids_.end(), device_id)); | 223 if (devices_[index].id == device_id) |
| 194 DCHECK_NE(device_ids_.size(), index); | 224 break; |
| 225 } | |
| 226 | |
| 227 DCHECK_NE(devices_.size(), index); | |
| 195 // http://crbug.com/543466 Update connection and paired status | 228 // http://crbug.com/543466 Update connection and paired status |
| 196 // http://crbug.com/629689 Update RSSI. | 229 devices_[index].signal_strength_level = level; |
| 197 if (view()) | 230 if (view()) |
| 198 view()->OnOptionUpdated(index); | 231 view()->OnOptionUpdated(index); |
| 199 return; | 232 return; |
| 200 } | 233 } |
| 201 | 234 |
| 202 device_ids_.push_back(device_id); | 235 devices_.push_back({device_id, level}); |
| 203 device_id_to_name_map_.insert({device_id, device_name}); | 236 device_id_to_name_map_.insert({device_id, device_name}); |
| 204 ++device_name_counts_[device_name]; | 237 ++device_name_counts_[device_name]; |
| 205 if (view()) | 238 if (view()) |
| 206 view()->OnOptionAdded(device_ids_.size() - 1); | 239 view()->OnOptionAdded(devices_.size() - 1); |
| 207 } | 240 } |
| 208 | 241 |
| 209 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 242 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
| 210 const auto& name_it = device_id_to_name_map_.find(device_id); | 243 const auto& name_it = device_id_to_name_map_.find(device_id); |
| 211 if (name_it == device_id_to_name_map_.end()) | 244 if (name_it == device_id_to_name_map_.end()) |
| 212 return; | 245 return; |
| 213 | 246 |
| 214 size_t index = 0; | 247 size_t index = 0; |
| 215 for (const auto& saved_device_id : device_ids_) { | 248 for (const auto& saved_device_info : devices_) { |
| 216 if (saved_device_id != device_id) { | 249 if (saved_device_info.id != device_id) { |
| 217 ++index; | 250 ++index; |
| 218 continue; | 251 continue; |
| 219 } | 252 } |
| 220 | 253 |
| 221 device_ids_.erase(device_ids_.begin() + index); | 254 devices_.erase(devices_.begin() + index); |
| 222 | 255 |
| 223 const auto& it = device_name_counts_.find(name_it->second); | 256 const auto& it = device_name_counts_.find(name_it->second); |
| 224 DCHECK(it != device_name_counts_.end()); | 257 DCHECK(it != device_name_counts_.end()); |
| 225 DCHECK_GT(it->second, 0); | 258 DCHECK_GT(it->second, 0); |
| 226 | 259 |
| 227 if (--(it->second) == 0) | 260 if (--(it->second) == 0) |
| 228 device_name_counts_.erase(it); | 261 device_name_counts_.erase(it); |
| 229 | 262 |
| 230 device_id_to_name_map_.erase(name_it); | 263 device_id_to_name_map_.erase(name_it); |
| 231 | 264 |
| 232 if (view()) | 265 if (view()) |
| 233 view()->OnOptionRemoved(index); | 266 view()->OnOptionRemoved(index); |
| 234 return; | 267 return; |
| 235 } | 268 } |
| 236 } | 269 } |
| 237 | 270 |
| 238 void BluetoothChooserController::ResetEventHandler() { | 271 void BluetoothChooserController::ResetEventHandler() { |
| 239 event_handler_.Reset(); | 272 event_handler_.Reset(); |
| 240 } | 273 } |
| 241 | 274 |
| 242 void BluetoothChooserController::ClearAllDevices() { | 275 void BluetoothChooserController::ClearAllDevices() { |
| 243 device_ids_.clear(); | 276 devices_.clear(); |
| 244 device_id_to_name_map_.clear(); | 277 device_id_to_name_map_.clear(); |
| 245 device_name_counts_.clear(); | 278 device_name_counts_.clear(); |
| 246 } | 279 } |
| OLD | NEW |