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 |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { | 230 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { |
231 // Nothing goes wrong if the discovery session fails to stop, and we don't | 231 // Nothing goes wrong if the discovery session fails to stop, and we don't |
232 // need to wait for it before letting the user's script proceed, so we ignore | 232 // need to wait for it before letting the user's script proceed, so we ignore |
233 // the results here. | 233 // the results here. |
234 discovery_session->Stop(base::Bind(&base::DoNothing), | 234 discovery_session->Stop(base::Bind(&base::DoNothing), |
235 base::Bind(&base::DoNothing)); | 235 base::Bind(&base::DoNothing)); |
236 } | 236 } |
237 | 237 |
238 UMARequestDeviceOutcome OutcomeFromChooserEvent(BluetoothChooser::Event event) { | 238 UMARequestDeviceOutcome OutcomeFromChooserEvent(BluetoothChooser::Event event) { |
239 switch (event) { | 239 switch (event) { |
| 240 case BluetoothChooser::Event::ADAPTER_DISCOVERING_CHANGED: |
| 241 return UMARequestDeviceOutcome::BLUETOOTH_CHOOSER_CANCELLED; |
240 case BluetoothChooser::Event::DENIED_PERMISSION: | 242 case BluetoothChooser::Event::DENIED_PERMISSION: |
241 return UMARequestDeviceOutcome::BLUETOOTH_CHOOSER_DENIED_PERMISSION; | 243 return UMARequestDeviceOutcome::BLUETOOTH_CHOOSER_DENIED_PERMISSION; |
242 case BluetoothChooser::Event::CANCELLED: | 244 case BluetoothChooser::Event::CANCELLED: |
243 return UMARequestDeviceOutcome::BLUETOOTH_CHOOSER_CANCELLED; | 245 return UMARequestDeviceOutcome::BLUETOOTH_CHOOSER_CANCELLED; |
244 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: | 246 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: |
245 return UMARequestDeviceOutcome::BLUETOOTH_OVERVIEW_HELP_LINK_PRESSED; | 247 return UMARequestDeviceOutcome::BLUETOOTH_OVERVIEW_HELP_LINK_PRESSED; |
246 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: | 248 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: |
247 return UMARequestDeviceOutcome::ADAPTER_OFF_HELP_LINK_PRESSED; | 249 return UMARequestDeviceOutcome::ADAPTER_OFF_HELP_LINK_PRESSED; |
248 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: | 250 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: |
249 return UMARequestDeviceOutcome::NEED_LOCATION_HELP_LINK_PRESSED; | 251 return UMARequestDeviceOutcome::NEED_LOCATION_HELP_LINK_PRESSED; |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, | 459 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, |
458 "" /* device_address */); | 460 "" /* device_address */); |
459 } | 461 } |
460 } | 462 } |
461 | 463 |
462 if (!powered) { | 464 if (!powered) { |
463 discovery_session_timer_.Stop(); | 465 discovery_session_timer_.Stop(); |
464 } | 466 } |
465 } | 467 } |
466 | 468 |
| 469 void BluetoothDeviceChooserController::AdapterDiscoveringChanged( |
| 470 bool discovering) { |
| 471 if (!discovering && IsDiscoverySessionActive()) |
| 472 StopDeviceDiscovery(); |
| 473 } |
| 474 |
467 int BluetoothDeviceChooserController::CalculateSignalStrengthLevel( | 475 int BluetoothDeviceChooserController::CalculateSignalStrengthLevel( |
468 int8_t rssi) { | 476 int8_t rssi) { |
469 RecordRSSISignalStrength(rssi); | 477 RecordRSSISignalStrength(rssi); |
470 | 478 |
471 if (rssi < k20thPercentileRSSI) { | 479 if (rssi < k20thPercentileRSSI) { |
472 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_0); | 480 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_0); |
473 return 0; | 481 return 0; |
474 } else if (rssi < k40thPercentileRSSI) { | 482 } else if (rssi < k40thPercentileRSSI) { |
475 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_1); | 483 RecordRSSISignalStrengthLevel(content::UMARSSISignalStrengthLevel::LEVEL_1); |
476 return 1; | 484 return 1; |
(...skipping 17 matching lines...) Expand all Loading... |
494 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 502 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
495 if (device->IsGattConnected()) { | 503 if (device->IsGattConnected()) { |
496 AddFilteredDevice(*device); | 504 AddFilteredDevice(*device); |
497 } | 505 } |
498 } | 506 } |
499 } | 507 } |
500 | 508 |
501 void BluetoothDeviceChooserController::StartDeviceDiscovery() { | 509 void BluetoothDeviceChooserController::StartDeviceDiscovery() { |
502 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 510 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
503 | 511 |
504 if (discovery_session_.get() && discovery_session_->IsActive()) { | 512 if (IsDiscoverySessionActive()) { |
505 // Already running; just increase the timeout. | 513 // Already running; just increase the timeout. |
506 discovery_session_timer_.Reset(); | 514 discovery_session_timer_.Reset(); |
507 return; | 515 return; |
508 } | 516 } |
509 | 517 |
510 scanning_start_time_ = base::TimeTicks::Now(); | 518 scanning_start_time_ = base::TimeTicks::Now(); |
511 | 519 |
512 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::DISCOVERING); | 520 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::DISCOVERING); |
513 adapter_->StartDiscoverySessionWithFilter( | 521 adapter_->StartDiscoverySessionWithFilter( |
514 ComputeScanFilter(options_->filters), | 522 ComputeScanFilter(options_->filters), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 DCHECK(chooser_); | 577 DCHECK(chooser_); |
570 StartDeviceDiscovery(); | 578 StartDeviceDiscovery(); |
571 // No need to close the chooser so we return. | 579 // No need to close the chooser so we return. |
572 return; | 580 return; |
573 case BluetoothChooser::Event::DENIED_PERMISSION: | 581 case BluetoothChooser::Event::DENIED_PERMISSION: |
574 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 582 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
575 PostErrorCallback(blink::mojom::WebBluetoothResult:: | 583 PostErrorCallback(blink::mojom::WebBluetoothResult:: |
576 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); | 584 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); |
577 break; | 585 break; |
578 case BluetoothChooser::Event::CANCELLED: | 586 case BluetoothChooser::Event::CANCELLED: |
| 587 LOG(ERROR) << "[DJKim] " |
| 588 "BluetoothDeviceChooserController::OnBluetoothChooserEvent " |
| 589 ">> CANCELLED"; |
579 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 590 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
580 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 591 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
581 break; | 592 break; |
582 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: | 593 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: |
583 DVLOG(1) << "Overview Help link pressed."; | 594 DVLOG(1) << "Overview Help link pressed."; |
584 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 595 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
585 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 596 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
586 break; | 597 break; |
587 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: | 598 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: |
588 DVLOG(1) << "Adapter Off Help link pressed."; | 599 DVLOG(1) << "Adapter Off Help link pressed."; |
589 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 600 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
590 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 601 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
591 break; | 602 break; |
592 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: | 603 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: |
593 DVLOG(1) << "Need Location Help link pressed."; | 604 DVLOG(1) << "Need Location Help link pressed."; |
594 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 605 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
595 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 606 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
596 break; | 607 break; |
597 case BluetoothChooser::Event::SELECTED: | 608 case BluetoothChooser::Event::SELECTED: |
598 RecordNumOfDevices(options_->accept_all_devices, device_ids_.size()); | 609 RecordNumOfDevices(options_->accept_all_devices, device_ids_.size()); |
599 // RecordRequestDeviceOutcome is called in the callback, because the | 610 // RecordRequestDeviceOutcome is called in the callback, because the |
600 // device may have vanished. | 611 // device may have vanished. |
601 PostSuccessCallback(device_address); | 612 PostSuccessCallback(device_address); |
602 break; | 613 break; |
| 614 case BluetoothChooser::Event::ADAPTER_DISCOVERING_CHANGED: |
| 615 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 616 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 617 break; |
603 } | 618 } |
604 // Close chooser. | 619 // Close chooser. |
605 chooser_.reset(); | 620 chooser_.reset(); |
606 } | 621 } |
607 | 622 |
608 void BluetoothDeviceChooserController::PostSuccessCallback( | 623 void BluetoothDeviceChooserController::PostSuccessCallback( |
609 const std::string& device_address) { | 624 const std::string& device_address) { |
610 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 625 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
611 FROM_HERE, | 626 FROM_HERE, |
612 base::Bind(success_callback_, base::Passed(std::move(options_)), | 627 base::Bind(success_callback_, base::Passed(std::move(options_)), |
613 device_address))) { | 628 device_address))) { |
614 LOG(WARNING) << "No TaskRunner."; | 629 LOG(WARNING) << "No TaskRunner."; |
615 } | 630 } |
616 } | 631 } |
617 | 632 |
618 void BluetoothDeviceChooserController::PostErrorCallback( | 633 void BluetoothDeviceChooserController::PostErrorCallback( |
619 blink::mojom::WebBluetoothResult error) { | 634 blink::mojom::WebBluetoothResult error) { |
620 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 635 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
621 FROM_HERE, base::Bind(error_callback_, error))) { | 636 FROM_HERE, base::Bind(error_callback_, error))) { |
622 LOG(WARNING) << "No TaskRunner."; | 637 LOG(WARNING) << "No TaskRunner."; |
623 } | 638 } |
624 } | 639 } |
625 | 640 |
| 641 bool BluetoothDeviceChooserController::IsDiscoverySessionActive() { |
| 642 if (discovery_session_ && discovery_session_->IsActive()) |
| 643 return true; |
| 644 |
| 645 return false; |
| 646 } |
| 647 |
626 } // namespace content | 648 } // namespace content |
OLD | NEW |