Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc

Issue 2245603003: Add signal strength indicator icon to WebBluetooth chooser on non-Mac desktops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added signal strength indicator icon to WebBluetooth chooser Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 int CalculateSignalLevel(int rssi) {
msw 2016/08/12 23:08:48 nit: add a comment, explain the rssi acronym (I ha
juncai 2016/08/15 21:53:19 Done.
37 if (rssi <= kMinRSSI) {
38 return 0;
39 } else if (rssi >= kMaxRSSI) {
msw 2016/08/12 23:08:48 nit: no else after return here and below.
juncai 2016/08/15 21:53:19 Done.
40 return kNumLevels - 1;
41 } else {
42 double input_range = kMaxRSSI - kMinRSSI;
43 double output_range = kNumLevels - 1;
44 return static_cast<int>(
45 (static_cast<double>(rssi - kMinRSSI) * output_range / input_range));
msw 2016/08/12 23:08:48 nit: you shouldn't need this static cast, right?
juncai 2016/08/15 21:53:19 Done.
46 }
47 }
48
29 } // namespace 49 } // namespace
30 50
31 BluetoothChooserController::BluetoothChooserController( 51 BluetoothChooserController::BluetoothChooserController(
32 content::RenderFrameHost* owner, 52 content::RenderFrameHost* owner,
33 const content::BluetoothChooser::EventHandler& event_handler) 53 const content::BluetoothChooser::EventHandler& event_handler)
34 : ChooserController(owner, 54 : ChooserController(owner,
35 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, 55 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN,
36 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), 56 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME),
37 event_handler_(event_handler), 57 event_handler_(event_handler),
38 no_devices_text_(l10n_util::GetStringUTF16( 58 no_devices_text_(l10n_util::GetStringUTF16(
39 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} 59 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {}
40 60
41 BluetoothChooserController::~BluetoothChooserController() {} 61 BluetoothChooserController::~BluetoothChooserController() {}
42 62
63 bool BluetoothChooserController::HasIconBeforeText() const {
64 return true;
65 }
66
43 base::string16 BluetoothChooserController::GetNoOptionsText() const { 67 base::string16 BluetoothChooserController::GetNoOptionsText() const {
44 return no_devices_text_; 68 return no_devices_text_;
45 } 69 }
46 70
47 base::string16 BluetoothChooserController::GetOkButtonLabel() const { 71 base::string16 BluetoothChooserController::GetOkButtonLabel() const {
48 return l10n_util::GetStringUTF16( 72 return l10n_util::GetStringUTF16(
49 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); 73 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT);
50 } 74 }
51 75
52 size_t BluetoothChooserController::NumOptions() const { 76 size_t BluetoothChooserController::NumOptions() const {
53 return device_ids_.size(); 77 return devices_info_.size();
78 }
79
80 int BluetoothChooserController::GetSignalStrengthLevel(size_t index) const {
81 return devices_info_[index].signal_level;
54 } 82 }
55 83
56 base::string16 BluetoothChooserController::GetOption(size_t index) const { 84 base::string16 BluetoothChooserController::GetOption(size_t index) const {
57 DCHECK_LT(index, device_ids_.size()); 85 DCHECK_LT(index, devices_info_.size());
58 const std::string& device_id = device_ids_[index]; 86 const std::string& device_id = devices_info_[index].id;
59 const auto& device_name_it = device_id_to_name_map_.find(device_id); 87 const auto& device_name_it = device_id_to_name_map_.find(device_id);
60 DCHECK(device_name_it != device_id_to_name_map_.end()); 88 DCHECK(device_name_it != device_id_to_name_map_.end());
61 const auto& it = device_name_map_.find(device_name_it->second); 89 const auto& it = device_name_map_.find(device_name_it->second);
62 DCHECK(it != device_name_map_.end()); 90 DCHECK(it != device_name_map_.end());
63 return it->second == 1 91 return it->second == 1
64 ? device_name_it->second 92 ? device_name_it->second
65 : l10n_util::GetStringFUTF16( 93 : l10n_util::GetStringFUTF16(
66 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, 94 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID,
67 device_name_it->second, base::UTF8ToUTF16(device_id)); 95 device_name_it->second, base::UTF8ToUTF16(device_id));
68 } 96 }
69 97
70 void BluetoothChooserController::RefreshOptions() { 98 void BluetoothChooserController::RefreshOptions() {
71 if (event_handler_.is_null()) 99 if (event_handler_.is_null())
72 return; 100 return;
73 ClearAllDevices(); 101 ClearAllDevices();
74 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); 102 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string());
75 } 103 }
76 104
77 base::string16 BluetoothChooserController::GetStatus() const { 105 base::string16 BluetoothChooserController::GetStatus() const {
78 return status_text_; 106 return status_text_;
79 } 107 }
80 108
81 void BluetoothChooserController::Select(size_t index) { 109 void BluetoothChooserController::Select(size_t index) {
82 if (event_handler_.is_null()) { 110 if (event_handler_.is_null()) {
83 content::RecordRequestDeviceOutcome( 111 content::RecordRequestDeviceOutcome(
84 content::UMARequestDeviceOutcome:: 112 content::UMARequestDeviceOutcome::
85 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID); 113 BLUETOOTH_CHOOSER_EVENT_HANDLER_INVALID);
86 return; 114 return;
87 } 115 }
88 DCHECK_LT(index, device_ids_.size()); 116 DCHECK_LT(index, devices_info_.size());
89 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, 117 event_handler_.Run(content::BluetoothChooser::Event::SELECTED,
90 device_ids_[index]); 118 devices_info_[index].id);
91 } 119 }
92 120
93 void BluetoothChooserController::Cancel() { 121 void BluetoothChooserController::Cancel() {
94 if (event_handler_.is_null()) 122 if (event_handler_.is_null())
95 return; 123 return;
96 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, 124 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED,
97 std::string()); 125 std::string());
98 } 126 }
99 127
100 void BluetoothChooserController::Close() { 128 void BluetoothChooserController::Close() {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 bool should_update_name, 196 bool should_update_name,
169 const base::string16& device_name, 197 const base::string16& device_name,
170 bool is_gatt_connected, 198 bool is_gatt_connected,
171 bool is_paired, 199 bool is_paired,
172 const int8_t* rssi) { 200 const int8_t* rssi) {
173 auto result = device_id_to_name_map_.insert({device_id, device_name}); 201 auto result = device_id_to_name_map_.insert({device_id, device_name});
174 if (!result.second) { 202 if (!result.second) {
175 // TODO(ortuno): Update device's information. 203 // TODO(ortuno): Update device's information.
176 // https://crbug.com/634366 Update name 204 // https://crbug.com/634366 Update name
177 // http://crbug.com/543466 Update connection and paired status 205 // http://crbug.com/543466 Update connection and paired status
178 // http://crbug.com/629689 Update RSSI. 206 // http://crbug.com/629689 Update RSSI.
msw 2016/08/12 23:08:48 perhaps consider landing your update CL first, the
juncai 2016/08/15 21:53:19 Yes, I'll land the update device name CL first: ht
179 return; 207 return;
180 } 208 }
181 209
182 device_ids_.push_back(device_id); 210 int level = rssi ? CalculateSignalLevel(static_cast<int>(*rssi)) : -1;
msw 2016/08/12 23:08:48 nit: this static cast shouldn't be necessary, righ
juncai 2016/08/15 21:53:19 Done.
211 devices_info_.push_back({device_id, level});
183 ++device_name_map_[device_name]; 212 ++device_name_map_[device_name];
184 if (view()) 213 if (view())
185 view()->OnOptionAdded(device_ids_.size() - 1); 214 view()->OnOptionAdded(devices_info_.size() - 1);
186 } 215 }
187 216
188 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { 217 void BluetoothChooserController::RemoveDevice(const std::string& device_id) {
189 const auto& name_it = device_id_to_name_map_.find(device_id); 218 const auto& name_it = device_id_to_name_map_.find(device_id);
190 if (name_it == device_id_to_name_map_.end()) 219 if (name_it == device_id_to_name_map_.end())
191 return; 220 return;
192 221
193 size_t index = 0; 222 size_t index = 0;
194 for (const auto& saved_device_id : device_ids_) { 223 for (const auto& saved_device_info : devices_info_) {
195 if (saved_device_id != device_id) { 224 if (saved_device_info.id != device_id) {
196 ++index; 225 ++index;
197 continue; 226 continue;
198 } 227 }
199 228
200 device_ids_.erase(device_ids_.begin() + index); 229 devices_info_.erase(devices_info_.begin() + index);
201 230
202 const auto& it = device_name_map_.find(name_it->second); 231 const auto& it = device_name_map_.find(name_it->second);
203 DCHECK(it != device_name_map_.end()); 232 DCHECK(it != device_name_map_.end());
204 DCHECK_GT(it->second, 0); 233 DCHECK_GT(it->second, 0);
205 234
206 if (--(it->second) == 0) 235 if (--(it->second) == 0)
207 device_name_map_.erase(it); 236 device_name_map_.erase(it);
208 237
209 device_id_to_name_map_.erase(name_it); 238 device_id_to_name_map_.erase(name_it);
210 239
211 if (view()) 240 if (view())
212 view()->OnOptionRemoved(index); 241 view()->OnOptionRemoved(index);
213 return; 242 return;
214 } 243 }
215 } 244 }
216 245
217 void BluetoothChooserController::ResetEventHandler() { 246 void BluetoothChooserController::ResetEventHandler() {
218 event_handler_.Reset(); 247 event_handler_.Reset();
219 } 248 }
220 249
221 void BluetoothChooserController::ClearAllDevices() { 250 void BluetoothChooserController::ClearAllDevices() {
222 device_ids_.clear(); 251 devices_info_.clear();
223 device_id_to_name_map_.clear(); 252 device_id_to_name_map_.clear();
224 device_name_map_.clear(); 253 device_name_map_.clear();
225 } 254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698