| 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" |
| 11 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" | 11 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 13 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "content/browser/bluetooth/bluetooth_metrics.h" | 16 #include "content/browser/bluetooth/bluetooth_metrics.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 Browser* GetBrowser() { | 22 Browser* GetBrowser() { |
| 23 chrome::ScopedTabbedBrowserDisplayer browser_displayer( | 23 chrome::ScopedTabbedBrowserDisplayer browser_displayer( |
| 24 ProfileManager::GetActiveUserProfile()); | 24 ProfileManager::GetActiveUserProfile()); |
| 25 DCHECK(browser_displayer.browser()); | 25 DCHECK(browser_displayer.browser()); |
| 26 return browser_displayer.browser(); | 26 return browser_displayer.browser(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Anything worse than or equal to this will show 0 bars. |
| 30 const int kMinRSSI = -100; |
| 31 // Anything better than or equal to this will show the maximum bars. |
| 32 const int kMaxRSSI = -55; |
| 33 // Number of RSSI levels used in the signal strength icon. |
| 34 const int kNumLevels = 5; |
| 35 |
| 36 // Received Signal Strength Indicator (RSSI) is a measurement of the power |
| 37 // present in a received radio signal. |
| 38 int CalculateSignalLevel(int8_t rssi) { |
| 39 if (rssi <= kMinRSSI) |
| 40 return 0; |
| 41 |
| 42 if (rssi >= kMaxRSSI) |
| 43 return kNumLevels - 1; |
| 44 |
| 45 double input_range = kMaxRSSI - kMinRSSI; |
| 46 double output_range = kNumLevels - 1; |
| 47 return static_cast<int>((rssi - kMinRSSI) * output_range / input_range); |
| 48 } |
| 49 |
| 29 } // namespace | 50 } // namespace |
| 30 | 51 |
| 31 BluetoothChooserController::BluetoothChooserController( | 52 BluetoothChooserController::BluetoothChooserController( |
| 32 content::RenderFrameHost* owner, | 53 content::RenderFrameHost* owner, |
| 33 const content::BluetoothChooser::EventHandler& event_handler) | 54 const content::BluetoothChooser::EventHandler& event_handler) |
| 34 : ChooserController(owner, | 55 : ChooserController(owner, |
| 35 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, | 56 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, |
| 36 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 57 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
| 37 event_handler_(event_handler), | 58 event_handler_(event_handler), |
| 38 no_devices_text_(l10n_util::GetStringUTF16( | 59 no_devices_text_(l10n_util::GetStringUTF16( |
| 39 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} | 60 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} |
| 40 | 61 |
| 41 BluetoothChooserController::~BluetoothChooserController() {} | 62 BluetoothChooserController::~BluetoothChooserController() {} |
| 42 | 63 |
| 64 bool BluetoothChooserController::ShouldShowIconBeforeText() const { |
| 65 return true; |
| 66 } |
| 67 |
| 43 base::string16 BluetoothChooserController::GetNoOptionsText() const { | 68 base::string16 BluetoothChooserController::GetNoOptionsText() const { |
| 44 return no_devices_text_; | 69 return no_devices_text_; |
| 45 } | 70 } |
| 46 | 71 |
| 47 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 72 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
| 48 return l10n_util::GetStringUTF16( | 73 return l10n_util::GetStringUTF16( |
| 49 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 74 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
| 50 } | 75 } |
| 51 | 76 |
| 52 size_t BluetoothChooserController::NumOptions() const { | 77 size_t BluetoothChooserController::NumOptions() const { |
| 53 return device_ids_.size(); | 78 return devices_.size(); |
| 79 } |
| 80 |
| 81 int BluetoothChooserController::GetSignalStrengthLevel(size_t index) const { |
| 82 return devices_[index].signal_strength_level; |
| 54 } | 83 } |
| 55 | 84 |
| 56 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 85 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
| 57 DCHECK_LT(index, device_ids_.size()); | 86 DCHECK_LT(index, devices_.size()); |
| 58 const std::string& device_id = device_ids_[index]; | 87 const std::string& device_id = devices_[index].id; |
| 59 const auto& device_name_it = device_id_to_name_map_.find(device_id); | 88 const auto& device_name_it = device_id_to_name_map_.find(device_id); |
| 60 DCHECK(device_name_it != device_id_to_name_map_.end()); | 89 DCHECK(device_name_it != device_id_to_name_map_.end()); |
| 61 const auto& it = device_name_map_.find(device_name_it->second); | 90 const auto& it = device_name_map_.find(device_name_it->second); |
| 62 DCHECK(it != device_name_map_.end()); | 91 DCHECK(it != device_name_map_.end()); |
| 63 return it->second == 1 | 92 return it->second == 1 |
| 64 ? device_name_it->second | 93 ? device_name_it->second |
| 65 : l10n_util::GetStringFUTF16( | 94 : l10n_util::GetStringFUTF16( |
| 66 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, | 95 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
| 67 device_name_it->second, base::UTF8ToUTF16(device_id)); | 96 device_name_it->second, base::UTF8ToUTF16(device_id)); |
| 68 } | 97 } |
| 69 | 98 |
| 70 void BluetoothChooserController::RefreshOptions() { | 99 void BluetoothChooserController::RefreshOptions() { |
| 71 if (event_handler_.is_null()) | 100 if (event_handler_.is_null()) |
| 72 return; | 101 return; |
| 73 ClearAllDevices(); | 102 ClearAllDevices(); |
| 74 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | 103 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); |
| 75 } | 104 } |
| 76 | 105 |
| 77 base::string16 BluetoothChooserController::GetStatus() const { | 106 base::string16 BluetoothChooserController::GetStatus() const { |
| 78 return status_text_; | 107 return status_text_; |
| 79 } | 108 } |
| 80 | 109 |
| 81 void BluetoothChooserController::Select(size_t index) { | 110 void BluetoothChooserController::Select(size_t index) { |
| 82 if (event_handler_.is_null()) { | 111 if (event_handler_.is_null()) { |
| 83 content::RecordRequestDeviceOutcome( | 112 content::RecordRequestDeviceOutcome( |
| 84 content::UMARequestDeviceOutcome:: | 113 content::UMARequestDeviceOutcome:: |
| 85 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); | 114 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); |
| 86 return; | 115 return; |
| 87 } | 116 } |
| 88 DCHECK_LT(index, device_ids_.size()); | 117 DCHECK_LT(index, devices_.size()); |
| 89 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 118 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
| 90 device_ids_[index]); | 119 devices_[index].id); |
| 91 } | 120 } |
| 92 | 121 |
| 93 void BluetoothChooserController::Cancel() { | 122 void BluetoothChooserController::Cancel() { |
| 94 if (event_handler_.is_null()) | 123 if (event_handler_.is_null()) |
| 95 return; | 124 return; |
| 96 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 125 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
| 97 std::string()); | 126 std::string()); |
| 98 } | 127 } |
| 99 | 128 |
| 100 void BluetoothChooserController::Close() { | 129 void BluetoothChooserController::Close() { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 const int8_t* rssi) { | 201 const int8_t* rssi) { |
| 173 auto result = device_id_to_name_map_.insert({device_id, device_name}); | 202 auto result = device_id_to_name_map_.insert({device_id, device_name}); |
| 174 if (!result.second) { | 203 if (!result.second) { |
| 175 // TODO(ortuno): Update device's information. | 204 // TODO(ortuno): Update device's information. |
| 176 // https://crbug.com/634366 Update name | 205 // https://crbug.com/634366 Update name |
| 177 // http://crbug.com/543466 Update connection and paired status | 206 // http://crbug.com/543466 Update connection and paired status |
| 178 // http://crbug.com/629689 Update RSSI. | 207 // http://crbug.com/629689 Update RSSI. |
| 179 return; | 208 return; |
| 180 } | 209 } |
| 181 | 210 |
| 182 device_ids_.push_back(device_id); | 211 int level = rssi ? CalculateSignalLevel(*rssi) : -1; |
| 212 devices_.push_back({device_id, level}); |
| 183 ++device_name_map_[device_name]; | 213 ++device_name_map_[device_name]; |
| 184 if (view()) | 214 if (view()) |
| 185 view()->OnOptionAdded(device_ids_.size() - 1); | 215 view()->OnOptionAdded(devices_.size() - 1); |
| 186 } | 216 } |
| 187 | 217 |
| 188 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 218 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
| 189 const auto& name_it = device_id_to_name_map_.find(device_id); | 219 const auto& name_it = device_id_to_name_map_.find(device_id); |
| 190 if (name_it == device_id_to_name_map_.end()) | 220 if (name_it == device_id_to_name_map_.end()) |
| 191 return; | 221 return; |
| 192 | 222 |
| 193 size_t index = 0; | 223 size_t index = 0; |
| 194 for (const auto& saved_device_id : device_ids_) { | 224 for (const auto& saved_device_info : devices_) { |
| 195 if (saved_device_id != device_id) { | 225 if (saved_device_info.id != device_id) { |
| 196 ++index; | 226 ++index; |
| 197 continue; | 227 continue; |
| 198 } | 228 } |
| 199 | 229 |
| 200 device_ids_.erase(device_ids_.begin() + index); | 230 devices_.erase(devices_.begin() + index); |
| 201 | 231 |
| 202 const auto& it = device_name_map_.find(name_it->second); | 232 const auto& it = device_name_map_.find(name_it->second); |
| 203 DCHECK(it != device_name_map_.end()); | 233 DCHECK(it != device_name_map_.end()); |
| 204 DCHECK_GT(it->second, 0); | 234 DCHECK_GT(it->second, 0); |
| 205 | 235 |
| 206 if (--(it->second) == 0) | 236 if (--(it->second) == 0) |
| 207 device_name_map_.erase(it); | 237 device_name_map_.erase(it); |
| 208 | 238 |
| 209 device_id_to_name_map_.erase(name_it); | 239 device_id_to_name_map_.erase(name_it); |
| 210 | 240 |
| 211 if (view()) | 241 if (view()) |
| 212 view()->OnOptionRemoved(index); | 242 view()->OnOptionRemoved(index); |
| 213 return; | 243 return; |
| 214 } | 244 } |
| 215 } | 245 } |
| 216 | 246 |
| 217 void BluetoothChooserController::ResetEventHandler() { | 247 void BluetoothChooserController::ResetEventHandler() { |
| 218 event_handler_.Reset(); | 248 event_handler_.Reset(); |
| 219 } | 249 } |
| 220 | 250 |
| 221 void BluetoothChooserController::ClearAllDevices() { | 251 void BluetoothChooserController::ClearAllDevices() { |
| 222 device_ids_.clear(); | 252 devices_.clear(); |
| 223 device_id_to_name_map_.clear(); | 253 device_id_to_name_map_.clear(); |
| 224 device_name_map_.clear(); | 254 device_name_map_.clear(); |
| 225 } | 255 } |
| OLD | NEW |