| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 namespace { | 81 namespace { |
| 82 constexpr size_t kMaxLengthForDeviceName = | 82 constexpr size_t kMaxLengthForDeviceName = |
| 83 29; // max length of device name in filter. | 83 29; // max length of device name in filter. |
| 84 | 84 |
| 85 // The duration of a Bluetooth Scan in seconds. | 85 // The duration of a Bluetooth Scan in seconds. |
| 86 constexpr int kScanDuration = 60; | 86 constexpr int kScanDuration = 60; |
| 87 constexpr int kTestScanDuration = 0; | 87 constexpr int kTestScanDuration = 0; |
| 88 | 88 |
| 89 void LogRequestDeviceOptions( | 89 void LogRequestDeviceOptions( |
| 90 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 90 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 91 VLOG(1) << "requestDevice called with the following filters: "; | 91 DVLOG(1) << "requestDevice called with the following filters: "; |
| 92 VLOG(1) << "acceptAllDevices: " << options->accept_all_devices; | 92 DVLOG(1) << "acceptAllDevices: " << options->accept_all_devices; |
| 93 | 93 |
| 94 if (!options->filters) | 94 if (!options->filters) |
| 95 return; | 95 return; |
| 96 | 96 |
| 97 int i = 0; | 97 int i = 0; |
| 98 for (const auto& filter : options->filters.value()) { | 98 for (const auto& filter : options->filters.value()) { |
| 99 VLOG(1) << "Filter #" << ++i; | 99 DVLOG(1) << "Filter #" << ++i; |
| 100 if (filter->name) | 100 if (filter->name) |
| 101 VLOG(1) << "Name: " << filter->name.value(); | 101 DVLOG(1) << "Name: " << filter->name.value(); |
| 102 | 102 |
| 103 if (filter->name_prefix) | 103 if (filter->name_prefix) |
| 104 VLOG(1) << "Name Prefix: " << filter->name_prefix.value(); | 104 DVLOG(1) << "Name Prefix: " << filter->name_prefix.value(); |
| 105 | 105 |
| 106 if (filter->services) { | 106 if (filter->services) { |
| 107 VLOG(1) << "Services: "; | 107 DVLOG(1) << "Services: "; |
| 108 VLOG(1) << "\t["; | 108 DVLOG(1) << "\t["; |
| 109 for (const auto& service : filter->services.value()) | 109 for (const auto& service : filter->services.value()) |
| 110 VLOG(1) << "\t\t" << service.canonical_value(); | 110 DVLOG(1) << "\t\t" << service.canonical_value(); |
| 111 VLOG(1) << "\t]"; | 111 DVLOG(1) << "\t]"; |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool IsEmptyOrInvalidFilter( | 116 bool IsEmptyOrInvalidFilter( |
| 117 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 117 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
| 118 // At least one member needs to be present. | 118 // At least one member needs to be present. |
| 119 if (!filter->name && !filter->name_prefix && !filter->services) | 119 if (!filter->name && !filter->name_prefix && !filter->services) |
| 120 return true; | 120 return true; |
| 121 | 121 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 if (!embedding_origin.IsSameOriginWith(requesting_origin)) { | 345 if (!embedding_origin.IsSameOriginWith(requesting_origin)) { |
| 346 PostErrorCallback(blink::mojom::WebBluetoothResult:: | 346 PostErrorCallback(blink::mojom::WebBluetoothResult:: |
| 347 REQUEST_DEVICE_FROM_CROSS_ORIGIN_IFRAME); | 347 REQUEST_DEVICE_FROM_CROSS_ORIGIN_IFRAME); |
| 348 return; | 348 return; |
| 349 } | 349 } |
| 350 // The above also excludes unique origins, which are not even same-origin with | 350 // The above also excludes unique origins, which are not even same-origin with |
| 351 // themselves. | 351 // themselves. |
| 352 DCHECK(!requesting_origin.unique()); | 352 DCHECK(!requesting_origin.unique()); |
| 353 | 353 |
| 354 if (!adapter_->IsPresent()) { | 354 if (!adapter_->IsPresent()) { |
| 355 VLOG(1) << "Bluetooth Adapter not present. Can't serve requestDevice."; | 355 DVLOG(1) << "Bluetooth Adapter not present. Can't serve requestDevice."; |
| 356 RecordRequestDeviceOutcome( | 356 RecordRequestDeviceOutcome( |
| 357 UMARequestDeviceOutcome::BLUETOOTH_ADAPTER_NOT_PRESENT); | 357 UMARequestDeviceOutcome::BLUETOOTH_ADAPTER_NOT_PRESENT); |
| 358 PostErrorCallback(blink::mojom::WebBluetoothResult::NO_BLUETOOTH_ADAPTER); | 358 PostErrorCallback(blink::mojom::WebBluetoothResult::NO_BLUETOOTH_ADAPTER); |
| 359 return; | 359 return; |
| 360 } | 360 } |
| 361 | 361 |
| 362 switch (GetContentClient()->browser()->AllowWebBluetooth( | 362 switch (GetContentClient()->browser()->AllowWebBluetooth( |
| 363 web_contents_->GetBrowserContext(), requesting_origin, | 363 web_contents_->GetBrowserContext(), requesting_origin, |
| 364 embedding_origin)) { | 364 embedding_origin)) { |
| 365 case ContentBrowserClient::AllowWebBluetoothResult::BLOCK_POLICY: { | 365 case ContentBrowserClient::AllowWebBluetoothResult::BLOCK_POLICY: { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 395 chooser_event_handler); | 395 chooser_event_handler); |
| 396 } | 396 } |
| 397 | 397 |
| 398 if (!chooser_.get()) { | 398 if (!chooser_.get()) { |
| 399 PostErrorCallback( | 399 PostErrorCallback( |
| 400 blink::mojom::WebBluetoothResult::WEB_BLUETOOTH_NOT_SUPPORTED); | 400 blink::mojom::WebBluetoothResult::WEB_BLUETOOTH_NOT_SUPPORTED); |
| 401 return; | 401 return; |
| 402 } | 402 } |
| 403 | 403 |
| 404 if (!chooser_->CanAskForScanningPermission()) { | 404 if (!chooser_->CanAskForScanningPermission()) { |
| 405 VLOG(1) << "Closing immediately because Chooser cannot obtain permission."; | 405 DVLOG(1) << "Closing immediately because Chooser cannot obtain permission."; |
| 406 OnBluetoothChooserEvent(BluetoothChooser::Event::DENIED_PERMISSION, | 406 OnBluetoothChooserEvent(BluetoothChooser::Event::DENIED_PERMISSION, |
| 407 "" /* device_address */); | 407 "" /* device_address */); |
| 408 return; | 408 return; |
| 409 } | 409 } |
| 410 | 410 |
| 411 device_ids_.clear(); | 411 device_ids_.clear(); |
| 412 PopulateConnectedDevices(); | 412 PopulateConnectedDevices(); |
| 413 if (!chooser_.get()) { | 413 if (!chooser_.get()) { |
| 414 // If the dialog's closing, no need to do any of the rest of this. | 414 // If the dialog's closing, no need to do any of the rest of this. |
| 415 return; | 415 return; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 530 |
| 531 StopDiscoverySession(std::move(discovery_session_)); | 531 StopDiscoverySession(std::move(discovery_session_)); |
| 532 if (chooser_) { | 532 if (chooser_) { |
| 533 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::IDLE); | 533 chooser_->ShowDiscoveryState(BluetoothChooser::DiscoveryState::IDLE); |
| 534 } | 534 } |
| 535 } | 535 } |
| 536 | 536 |
| 537 void BluetoothDeviceChooserController::OnStartDiscoverySessionSuccess( | 537 void BluetoothDeviceChooserController::OnStartDiscoverySessionSuccess( |
| 538 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { | 538 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 539 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 539 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 540 VLOG(1) << "Started discovery session."; | 540 DVLOG(1) << "Started discovery session."; |
| 541 if (chooser_.get()) { | 541 if (chooser_.get()) { |
| 542 discovery_session_ = std::move(discovery_session); | 542 discovery_session_ = std::move(discovery_session); |
| 543 discovery_session_timer_.Reset(); | 543 discovery_session_timer_.Reset(); |
| 544 } else { | 544 } else { |
| 545 StopDiscoverySession(std::move(discovery_session)); | 545 StopDiscoverySession(std::move(discovery_session)); |
| 546 } | 546 } |
| 547 } | 547 } |
| 548 | 548 |
| 549 void BluetoothDeviceChooserController::OnStartDiscoverySessionFailed() { | 549 void BluetoothDeviceChooserController::OnStartDiscoverySessionFailed() { |
| 550 if (chooser_.get()) { | 550 if (chooser_.get()) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 572 case BluetoothChooser::Event::DENIED_PERMISSION: | 572 case BluetoothChooser::Event::DENIED_PERMISSION: |
| 573 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 573 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 574 PostErrorCallback(blink::mojom::WebBluetoothResult:: | 574 PostErrorCallback(blink::mojom::WebBluetoothResult:: |
| 575 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); | 575 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); |
| 576 break; | 576 break; |
| 577 case BluetoothChooser::Event::CANCELLED: | 577 case BluetoothChooser::Event::CANCELLED: |
| 578 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 578 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 579 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 579 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 580 break; | 580 break; |
| 581 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: | 581 case BluetoothChooser::Event::SHOW_OVERVIEW_HELP: |
| 582 VLOG(1) << "Overview Help link pressed."; | 582 DVLOG(1) << "Overview Help link pressed."; |
| 583 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 583 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 584 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 584 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 585 break; | 585 break; |
| 586 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: | 586 case BluetoothChooser::Event::SHOW_ADAPTER_OFF_HELP: |
| 587 VLOG(1) << "Adapter Off Help link pressed."; | 587 DVLOG(1) << "Adapter Off Help link pressed."; |
| 588 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 588 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 589 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 589 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 590 break; | 590 break; |
| 591 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: | 591 case BluetoothChooser::Event::SHOW_NEED_LOCATION_HELP: |
| 592 VLOG(1) << "Need Location Help link pressed."; | 592 DVLOG(1) << "Need Location Help link pressed."; |
| 593 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 593 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 594 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); | 594 PostErrorCallback(blink::mojom::WebBluetoothResult::CHOOSER_CANCELLED); |
| 595 break; | 595 break; |
| 596 case BluetoothChooser::Event::SELECTED: | 596 case BluetoothChooser::Event::SELECTED: |
| 597 RecordNumOfDevices(options_->accept_all_devices, device_ids_.size()); | 597 RecordNumOfDevices(options_->accept_all_devices, device_ids_.size()); |
| 598 // RecordRequestDeviceOutcome is called in the callback, because the | 598 // RecordRequestDeviceOutcome is called in the callback, because the |
| 599 // device may have vanished. | 599 // device may have vanished. |
| 600 PostSuccessCallback(device_address); | 600 PostSuccessCallback(device_address); |
| 601 break; | 601 break; |
| 602 } | 602 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 616 | 616 |
| 617 void BluetoothDeviceChooserController::PostErrorCallback( | 617 void BluetoothDeviceChooserController::PostErrorCallback( |
| 618 blink::mojom::WebBluetoothResult error) { | 618 blink::mojom::WebBluetoothResult error) { |
| 619 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 619 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 620 FROM_HERE, base::Bind(error_callback_, error))) { | 620 FROM_HERE, base::Bind(error_callback_, error))) { |
| 621 LOG(WARNING) << "No TaskRunner."; | 621 LOG(WARNING) << "No TaskRunner."; |
| 622 } | 622 } |
| 623 } | 623 } |
| 624 | 624 |
| 625 } // namespace content | 625 } // namespace content |
| OLD | NEW |