OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/bluetooth/bluetooth_device_chooser_controller.h" | 5 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <unordered_set> | 9 #include <unordered_set> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 15 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
16 #include "content/browser/bluetooth/bluetooth_metrics.h" | 16 #include "content/browser/bluetooth/bluetooth_metrics.h" |
17 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" | 17 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/content_browser_client.h" | 19 #include "content/public/browser/content_browser_client.h" |
20 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/browser/web_contents_delegate.h" | 22 #include "content/public/browser/web_contents_delegate.h" |
23 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
24 #include "device/bluetooth/bluetooth_common.h" | 24 #include "device/bluetooth/bluetooth_common.h" |
25 #include "device/bluetooth/bluetooth_discovery_session.h" | 25 #include "device/bluetooth/bluetooth_discovery_session.h" |
26 | 26 |
27 using device::BluetoothUUID; | 27 using device::BluetoothUUID; |
28 | 28 |
29 namespace { | |
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 image. | |
36 const int kNumSignalStrengthLevels = 5; | |
37 | |
38 } // namespace | |
39 | |
29 namespace content { | 40 namespace content { |
30 | 41 |
31 bool BluetoothDeviceChooserController::use_test_scan_duration_ = false; | 42 bool BluetoothDeviceChooserController::use_test_scan_duration_ = false; |
32 | 43 |
33 namespace { | 44 namespace { |
34 constexpr size_t kMaxLengthForDeviceName = | 45 constexpr size_t kMaxLengthForDeviceName = |
35 29; // max length of device name in filter. | 46 29; // max length of device name in filter. |
36 | 47 |
37 // The duration of a Bluetooth Scan in seconds. | 48 // The duration of a Bluetooth Scan in seconds. |
38 constexpr int kScanDuration = 10; | 49 constexpr int kScanDuration = 10; |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 BluetoothChooser::AdapterPresence::POWERED_OFF); | 344 BluetoothChooser::AdapterPresence::POWERED_OFF); |
334 return; | 345 return; |
335 } | 346 } |
336 | 347 |
337 StartDeviceDiscovery(); | 348 StartDeviceDiscovery(); |
338 } | 349 } |
339 | 350 |
340 void BluetoothDeviceChooserController::AddFilteredDevice( | 351 void BluetoothDeviceChooserController::AddFilteredDevice( |
341 const device::BluetoothDevice& device) { | 352 const device::BluetoothDevice& device) { |
342 if (chooser_.get() && MatchesFilters(device, options_->filters)) { | 353 if (chooser_.get() && MatchesFilters(device, options_->filters)) { |
354 base::Optional<int8_t> rssi = device.GetInquiryRSSI(); | |
343 chooser_->AddOrUpdateDevice( | 355 chooser_->AddOrUpdateDevice( |
344 device.GetAddress(), !!device.GetName() /* should_update_name */, | 356 device.GetAddress(), !!device.GetName() /* should_update_name */, |
345 device.GetNameForDisplay(), | 357 device.GetNameForDisplay(), |
346 // TODO(http://crbug.com/543466): Show connection and paired status. | 358 // TODO(http://crbug.com/543466): Show connection and paired status. |
347 false /* is_gatt_connected */, false /* is_paired */, | 359 false /* is_gatt_connected */, false /* is_paired */, |
348 // TODO(http://crbug.com/629689): Add signal strength indicator. | 360 rssi ? &rssi.value() : nullptr); |
ortuno
2016/08/19 23:07:24
Would it make sense to pass the level here directl
juncai
2016/08/19 23:16:23
That needs to change the BluetoothChooser's interf
juncai
2016/08/19 23:58:42
Actually now I think that doing it in this patch s
Jeffrey Yasskin
2016/08/20 00:03:29
Thanks. That should remove some awkward dependenci
juncai
2016/08/20 01:17:02
Done.
| |
349 nullptr /* rssi */); | |
350 } | 361 } |
351 } | 362 } |
352 | 363 |
353 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { | 364 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { |
354 if (!powered && discovery_session_.get()) { | 365 if (!powered && discovery_session_.get()) { |
355 StopDiscoverySession(std::move(discovery_session_)); | 366 StopDiscoverySession(std::move(discovery_session_)); |
356 } | 367 } |
357 | 368 |
358 if (chooser_.get()) { | 369 if (chooser_.get()) { |
359 chooser_->SetAdapterPresence( | 370 chooser_->SetAdapterPresence( |
360 powered ? BluetoothChooser::AdapterPresence::POWERED_ON | 371 powered ? BluetoothChooser::AdapterPresence::POWERED_ON |
361 : BluetoothChooser::AdapterPresence::POWERED_OFF); | 372 : BluetoothChooser::AdapterPresence::POWERED_OFF); |
362 if (powered) { | 373 if (powered) { |
363 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, | 374 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, |
364 "" /* device_address */); | 375 "" /* device_address */); |
365 } | 376 } |
366 } | 377 } |
367 | 378 |
368 if (!powered) { | 379 if (!powered) { |
369 discovery_session_timer_.Stop(); | 380 discovery_session_timer_.Stop(); |
370 } | 381 } |
371 } | 382 } |
372 | 383 |
384 int BluetoothDeviceChooserController::CalculateSignalStrengthLevel( | |
385 int8_t rssi) { | |
386 if (rssi <= kMinRSSI) | |
387 return 0; | |
388 | |
389 if (rssi >= kMaxRSSI) | |
390 return kNumSignalStrengthLevels - 1; | |
391 | |
392 double input_range = kMaxRSSI - kMinRSSI; | |
393 double output_range = kNumSignalStrengthLevels - 1; | |
394 return static_cast<int>((rssi - kMinRSSI) * output_range / input_range); | |
395 } | |
396 | |
373 void BluetoothDeviceChooserController::SetTestScanDurationForTesting() { | 397 void BluetoothDeviceChooserController::SetTestScanDurationForTesting() { |
374 BluetoothDeviceChooserController::use_test_scan_duration_ = true; | 398 BluetoothDeviceChooserController::use_test_scan_duration_ = true; |
375 } | 399 } |
376 | 400 |
377 void BluetoothDeviceChooserController::PopulateConnectedDevices() { | 401 void BluetoothDeviceChooserController::PopulateConnectedDevices() { |
378 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 402 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
379 if (device->IsGattConnected()) { | 403 if (device->IsGattConnected()) { |
380 AddFilteredDevice(*device); | 404 AddFilteredDevice(*device); |
381 } | 405 } |
382 } | 406 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 | 511 |
488 void BluetoothDeviceChooserController::PostErrorCallback( | 512 void BluetoothDeviceChooserController::PostErrorCallback( |
489 blink::mojom::WebBluetoothError error) { | 513 blink::mojom::WebBluetoothError error) { |
490 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 514 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
491 FROM_HERE, base::Bind(error_callback_, error))) { | 515 FROM_HERE, base::Bind(error_callback_, error))) { |
492 LOG(WARNING) << "No TaskRunner."; | 516 LOG(WARNING) << "No TaskRunner."; |
493 } | 517 } |
494 } | 518 } |
495 | 519 |
496 } // namespace content | 520 } // namespace content |
OLD | NEW |