| 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/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/bluetooth/bluetooth_blocklist.h" | 18 #include "content/browser/bluetooth/bluetooth_blocklist.h" |
| 19 #include "content/browser/bluetooth/bluetooth_metrics.h" | 19 #include "content/browser/bluetooth/bluetooth_metrics.h" |
| 20 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" | 20 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/content_browser_client.h" | 22 #include "content/public/browser/content_browser_client.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_delegate.h" | 25 #include "content/public/browser/web_contents_delegate.h" |
| 26 #include "device/bluetooth/bluetooth_adapter.h" | 26 #include "device/bluetooth/bluetooth_adapter.h" |
| 27 #include "device/bluetooth/bluetooth_common.h" | 27 #include "device/bluetooth/bluetooth_common.h" |
| 28 #include "device/bluetooth/bluetooth_discovery_session.h" | 28 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 29 | 29 |
| 30 #include "base/logging.h" |
| 31 |
| 30 using device::BluetoothUUID; | 32 using device::BluetoothUUID; |
| 31 using UUIDSet = device::BluetoothDevice::UUIDSet; | 33 using UUIDSet = device::BluetoothDevice::UUIDSet; |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| 35 // Signal Strength Display Notes: | 37 // Signal Strength Display Notes: |
| 36 // | 38 // |
| 37 // RSSI values are displayed by the chooser to empower a user to differentiate | 39 // RSSI values are displayed by the chooser to empower a user to differentiate |
| 38 // between multiple devices with the same name, comparing devices with different | 40 // between multiple devices with the same name, comparing devices with different |
| 39 // names is a secondary goal. It is important that a user is able to move closer | 41 // names is a secondary goal. It is important that a user is able to move closer |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // TODO(jyasskin): Add a way for tests to control the dialog | 284 // TODO(jyasskin): Add a way for tests to control the dialog |
| 283 // directly, and change this to a reasonable discovery timeout. | 285 // directly, and change this to a reasonable discovery timeout. |
| 284 base::TimeDelta::FromSeconds( | 286 base::TimeDelta::FromSeconds( |
| 285 use_test_scan_duration_ ? kTestScanDuration : kScanDuration), | 287 use_test_scan_duration_ ? kTestScanDuration : kScanDuration), |
| 286 base::Bind(&BluetoothDeviceChooserController::StopDeviceDiscovery, | 288 base::Bind(&BluetoothDeviceChooserController::StopDeviceDiscovery, |
| 287 // base::Timer guarantees it won't call back after its | 289 // base::Timer guarantees it won't call back after its |
| 288 // destructor starts. | 290 // destructor starts. |
| 289 base::Unretained(this)), | 291 base::Unretained(this)), |
| 290 /*is_repeating=*/false), | 292 /*is_repeating=*/false), |
| 291 weak_ptr_factory_(this) { | 293 weak_ptr_factory_(this) { |
| 294 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::BluetoothDeviceChoose
rController"; |
| 292 CHECK(adapter_); | 295 CHECK(adapter_); |
| 293 } | 296 } |
| 294 | 297 |
| 295 BluetoothDeviceChooserController::~BluetoothDeviceChooserController() { | 298 BluetoothDeviceChooserController::~BluetoothDeviceChooserController() { |
| 296 if (scanning_start_time_) { | 299 if (scanning_start_time_) { |
| 297 RecordScanningDuration(base::TimeTicks::Now() - | 300 RecordScanningDuration(base::TimeTicks::Now() - |
| 298 scanning_start_time_.value()); | 301 scanning_start_time_.value()); |
| 299 } | 302 } |
| 300 | 303 |
| 301 if (chooser_) { | 304 if (chooser_) { |
| 302 DCHECK(!error_callback_.is_null()); | 305 DCHECK(!error_callback_.is_null()); |
| 303 error_callback_.Run(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 306 error_callback_.Run(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 304 } | 307 } |
| 305 } | 308 } |
| 306 | 309 |
| 307 void BluetoothDeviceChooserController::GetDevice( | 310 void BluetoothDeviceChooserController::GetDevice( |
| 308 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | 311 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 309 const SuccessCallback& success_callback, | 312 const SuccessCallback& success_callback, |
| 310 const ErrorCallback& error_callback) { | 313 const ErrorCallback& error_callback) { |
| 314 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::GetDevice"; |
| 311 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 315 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 312 | 316 |
| 313 // GetDevice should only be called once. | 317 // GetDevice should only be called once. |
| 314 DCHECK(success_callback_.is_null()); | 318 DCHECK(success_callback_.is_null()); |
| 315 DCHECK(error_callback_.is_null()); | 319 DCHECK(error_callback_.is_null()); |
| 316 | 320 |
| 317 success_callback_ = success_callback; | 321 success_callback_ = success_callback; |
| 318 error_callback_ = error_callback; | 322 error_callback_ = error_callback; |
| 319 | 323 |
| 320 // The renderer should never send invalid options. | 324 // The renderer should never send invalid options. |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, | 463 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, |
| 460 "" /* device_address */); | 464 "" /* device_address */); |
| 461 } | 465 } |
| 462 } | 466 } |
| 463 | 467 |
| 464 if (!powered) { | 468 if (!powered) { |
| 465 discovery_session_timer_.Stop(); | 469 discovery_session_timer_.Stop(); |
| 466 } | 470 } |
| 467 } | 471 } |
| 468 | 472 |
| 473 void BluetoothDeviceChooserController::AdapterDiscoveringChanged( |
| 474 bool discovering) { |
| 475 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::AdapterDiscoveringCha
nged"; |
| 476 if (!discovering && IsDiscoverySessionActive()) |
| 477 StopDeviceDiscovery(); |
| 478 } |
| 479 |
| 469 int BluetoothDeviceChooserController::CalculateSignalStrengthLevel( | 480 int BluetoothDeviceChooserController::CalculateSignalStrengthLevel( |
| 470 int8_t rssi) { | 481 int8_t rssi) { |
| 471 RecordRSSISignalStrength(rssi); | 482 RecordRSSISignalStrength(rssi); |
| 472 | 483 |
| 473 if (rssi < k20thPercentileRSSI) { | 484 if (rssi < k20thPercentileRSSI) { |
| 474 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_0); | 485 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_0); |
| 475 return 0; | 486 return 0; |
| 476 } else if (rssi < k40thPercentileRSSI) { | 487 } else if (rssi < k40thPercentileRSSI) { |
| 477 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_1); | 488 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_1); |
| 478 return 1; | 489 return 1; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 494 | 505 |
| 495 void BluetoothDeviceChooserController::PopulateConnectedDevices() { | 506 void BluetoothDeviceChooserController::PopulateConnectedDevices() { |
| 496 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 507 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| 497 if (device->IsGattConnected()) { | 508 if (device->IsGattConnected()) { |
| 498 AddFilteredDevice(*device); | 509 AddFilteredDevice(*device); |
| 499 } | 510 } |
| 500 } | 511 } |
| 501 } | 512 } |
| 502 | 513 |
| 503 void BluetoothDeviceChooserController::StartDeviceDiscovery() { | 514 void BluetoothDeviceChooserController::StartDeviceDiscovery() { |
| 515 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> start"; |
| 504 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 516 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 505 | 517 |
| 506 if (discovery_session_.get() && discovery_session_->IsActive()) { | 518 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> #1"; |
| 519 if (IsDiscoverySessionActive()) { |
| 507 // Already running; just increase the timeout. | 520 // Already running; just increase the timeout. |
| 508 discovery_session_timer_.Reset(); | 521 discovery_session_timer_.Reset(); |
| 509 return; | 522 return; |
| 510 } | 523 } |
| 511 | 524 |
| 525 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> #2"; |
| 512 scanning_start_time_ = base::TimeTicks::Now(); | 526 scanning_start_time_ = base::TimeTicks::Now(); |
| 513 | 527 |
| 514 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::DISCOVERING); | 528 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> #2-1"; |
| 529 if (chooser_) |
| 530 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::DISCOVERING); |
| 531 else |
| 532 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscover
y >> #2-2"; |
| 533 |
| 534 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> #3"; |
| 535 |
| 536 if (adapter_) { |
| 537 if (!options_) { |
| 538 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscov
ery >> #3-1 return"; |
| 539 return; |
| 540 } |
| 541 |
| 515 adapter_->StartDiscoverySessionWithFilter( | 542 adapter_->StartDiscoverySessionWithFilter( |
| 516 ComputeScanFilter(options_->filters), | 543 ComputeScanFilter(options_->filters), |
| 517 base::Bind( | 544 base::Bind( |
| 518 &BluetoothDeviceChooserController::OnStartDiscoverySessionSuccess, | 545 &BluetoothDeviceChooserController::OnStartDiscoverySessionSuccess, |
| 519 weak_ptr_factory_.GetWeakPtr()), | 546 weak_ptr_factory_.GetWeakPtr()), |
| 520 base::Bind( | 547 base::Bind( |
| 521 &BluetoothDeviceChooserController::OnStartDiscoverySessionFailed, | 548 &BluetoothDeviceChooserController::OnStartDiscoverySessionFailed, |
| 522 weak_ptr_factory_.GetWeakPtr())); | 549 weak_ptr_factory_.GetWeakPtr())); |
| 550 } |
| 551 |
| 552 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscovery
>> end"; |
| 523 } | 553 } |
| 524 | 554 |
| 525 void BluetoothDeviceChooserController::StopDeviceDiscovery() { | 555 void BluetoothDeviceChooserController::StopDeviceDiscovery() { |
| 526 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 556 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 527 | 557 |
| 528 if (scanning_start_time_) { | 558 if (scanning_start_time_) { |
| 529 RecordScanningDuration(base::TimeTicks::Now() - | 559 RecordScanningDuration(base::TimeTicks::Now() - |
| 530 scanning_start_time_.value()); | 560 scanning_start_time_.value()); |
| 531 scanning_start_time_.reset(); | 561 scanning_start_time_.reset(); |
| 532 } | 562 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 } | 648 } |
| 619 | 649 |
| 620 void BluetoothDeviceChooserController::PostErrorCallback( | 650 void BluetoothDeviceChooserController::PostErrorCallback( |
| 621 blink::mojom::WebBluetoothResult error) { | 651 blink::mojom::WebBluetoothResult error) { |
| 622 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 652 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 623 FROM_HERE, base::Bind(error_callback_, error))) { | 653 FROM_HERE, base::Bind(error_callback_, error))) { |
| 624 LOG(WARNING) << "No TaskRunner."; | 654 LOG(WARNING) << "No TaskRunner."; |
| 625 } | 655 } |
| 626 } | 656 } |
| 627 | 657 |
| 658 bool BluetoothDeviceChooserController::IsDiscoverySessionActive() { |
| 659 if (discovery_session_ && discovery_session_->IsActive()) |
| 660 return true; |
| 661 |
| 662 return false; |
| 663 } |
| 664 |
| 665 void BluetoothDeviceChooserController::StartDeviceDiscoveryForTesting() { |
| 666 LOG(ERROR) << "[DJKim] BluetoothDeviceChooserController::StartDeviceDiscoveryF
orTesting"; |
| 667 StartDeviceDiscovery(); |
| 668 } |
| 669 |
| 628 } // namespace content | 670 } // namespace content |
| OLD | NEW |