| 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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 blink::mojom::WebBluetoothError::WEB_BLUETOOTH_NOT_SUPPORTED); | 314 blink::mojom::WebBluetoothError::WEB_BLUETOOTH_NOT_SUPPORTED); |
| 315 } | 315 } |
| 316 | 316 |
| 317 if (!chooser_->CanAskForScanningPermission()) { | 317 if (!chooser_->CanAskForScanningPermission()) { |
| 318 VLOG(1) << "Closing immediately because Chooser cannot obtain permission."; | 318 VLOG(1) << "Closing immediately because Chooser cannot obtain permission."; |
| 319 OnBluetoothChooserEvent(BluetoothChooser::Event::DENIED_PERMISSION, | 319 OnBluetoothChooserEvent(BluetoothChooser::Event::DENIED_PERMISSION, |
| 320 "" /* device_address */); | 320 "" /* device_address */); |
| 321 return; | 321 return; |
| 322 } | 322 } |
| 323 | 323 |
| 324 PopulateFoundDevices(); | 324 PopulateConnectedDevices(); |
| 325 if (!chooser_.get()) { | 325 if (!chooser_.get()) { |
| 326 // If the dialog's closing, no need to do any of the rest of this. | 326 // If the dialog's closing, no need to do any of the rest of this. |
| 327 return; | 327 return; |
| 328 } | 328 } |
| 329 | 329 |
| 330 if (!adapter_->IsPowered()) { | 330 if (!adapter_->IsPowered()) { |
| 331 chooser_->SetAdapterPresence( | 331 chooser_->SetAdapterPresence( |
| 332 BluetoothChooser::AdapterPresence::POWERED_OFF); | 332 BluetoothChooser::AdapterPresence::POWERED_OFF); |
| 333 return; | 333 return; |
| 334 } | 334 } |
| 335 | 335 |
| 336 StartDeviceDiscovery(); | 336 StartDeviceDiscovery(); |
| 337 } | 337 } |
| 338 | 338 |
| 339 void BluetoothDeviceChooserController::AddFilteredDevice( | 339 void BluetoothDeviceChooserController::AddFilteredDevice( |
| 340 const device::BluetoothDevice& device) { | 340 const device::BluetoothDevice& device) { |
| 341 if (chooser_.get() && MatchesFilters(device, options_->filters)) { | 341 if (chooser_.get() && MatchesFilters(device, options_->filters)) { |
| 342 VLOG(1) << "Adding device to chooser: " << device.GetAddress(); | |
| 343 chooser_->AddOrUpdateDevice(device.GetAddress(), | 342 chooser_->AddOrUpdateDevice(device.GetAddress(), |
| 344 device.GetNameForDisplay()); | 343 device.GetNameForDisplay()); |
| 345 } | 344 } |
| 346 } | 345 } |
| 347 | 346 |
| 348 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { | 347 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { |
| 349 if (!powered && discovery_session_.get()) { | 348 if (!powered && discovery_session_.get()) { |
| 350 StopDiscoverySession(std::move(discovery_session_)); | 349 StopDiscoverySession(std::move(discovery_session_)); |
| 351 } | 350 } |
| 352 | 351 |
| 353 if (chooser_.get()) { | 352 if (chooser_.get()) { |
| 354 chooser_->SetAdapterPresence( | 353 chooser_->SetAdapterPresence( |
| 355 powered ? BluetoothChooser::AdapterPresence::POWERED_ON | 354 powered ? BluetoothChooser::AdapterPresence::POWERED_ON |
| 356 : BluetoothChooser::AdapterPresence::POWERED_OFF); | 355 : BluetoothChooser::AdapterPresence::POWERED_OFF); |
| 357 if (powered) { | 356 if (powered) { |
| 358 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, | 357 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, |
| 359 "" /* device_address */); | 358 "" /* device_address */); |
| 360 } | 359 } |
| 361 } | 360 } |
| 362 | 361 |
| 363 if (!powered) { | 362 if (!powered) { |
| 364 discovery_session_timer_.Stop(); | 363 discovery_session_timer_.Stop(); |
| 365 } | 364 } |
| 366 } | 365 } |
| 367 | 366 |
| 368 void BluetoothDeviceChooserController::SetTestScanDurationForTesting() { | 367 void BluetoothDeviceChooserController::SetTestScanDurationForTesting() { |
| 369 BluetoothDeviceChooserController::use_test_scan_duration_ = true; | 368 BluetoothDeviceChooserController::use_test_scan_duration_ = true; |
| 370 } | 369 } |
| 371 | 370 |
| 372 void BluetoothDeviceChooserController::PopulateFoundDevices() { | 371 void BluetoothDeviceChooserController::PopulateConnectedDevices() { |
| 373 VLOG(1) << "Populating " << adapter_->GetDevices().size() | |
| 374 << " devices in chooser."; | |
| 375 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 372 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| 376 AddFilteredDevice(*device); | 373 if (device->IsGattConnected()) { |
| 374 AddFilteredDevice(*device); |
| 375 } |
| 377 } | 376 } |
| 378 } | 377 } |
| 379 | 378 |
| 380 void BluetoothDeviceChooserController::StartDeviceDiscovery() { | 379 void BluetoothDeviceChooserController::StartDeviceDiscovery() { |
| 381 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 380 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 382 | 381 |
| 383 if (discovery_session_.get() && discovery_session_->IsActive()) { | 382 if (discovery_session_.get() && discovery_session_->IsActive()) { |
| 384 // Already running; just increase the timeout. | 383 // Already running; just increase the timeout. |
| 385 discovery_session_timer_.Reset(); | 384 discovery_session_timer_.Reset(); |
| 386 return; | 385 return; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 425 |
| 427 void BluetoothDeviceChooserController::OnBluetoothChooserEvent( | 426 void BluetoothDeviceChooserController::OnBluetoothChooserEvent( |
| 428 BluetoothChooser::Event event, | 427 BluetoothChooser::Event event, |
| 429 const std::string& device_address) { | 428 const std::string& device_address) { |
| 430 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 429 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 431 // Shouldn't recieve an event from a closed chooser. | 430 // Shouldn't recieve an event from a closed chooser. |
| 432 DCHECK(chooser_.get()); | 431 DCHECK(chooser_.get()); |
| 433 | 432 |
| 434 switch (event) { | 433 switch (event) { |
| 435 case BluetoothChooser::Event::RESCAN: | 434 case BluetoothChooser::Event::RESCAN: |
| 436 PopulateFoundDevices(); | 435 PopulateConnectedDevices(); |
| 437 DCHECK(chooser_); | 436 DCHECK(chooser_); |
| 438 StartDeviceDiscovery(); | 437 StartDeviceDiscovery(); |
| 439 // No need to close the chooser so we return. | 438 // No need to close the chooser so we return. |
| 440 return; | 439 return; |
| 441 case BluetoothChooser::Event::DENIED_PERMISSION: | 440 case BluetoothChooser::Event::DENIED_PERMISSION: |
| 442 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); | 441 RecordRequestDeviceOutcome(OutcomeFromChooserEvent(event)); |
| 443 PostErrorCallback(blink::mojom::WebBluetoothError:: | 442 PostErrorCallback(blink::mojom::WebBluetoothError:: |
| 444 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); | 443 CHOOSER_NOT_SHOWN_USER_DENIED_PERMISSION_TO_SCAN); |
| 445 break; | 444 break; |
| 446 case BluetoothChooser::Event::CANCELLED: | 445 case BluetoothChooser::Event::CANCELLED: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 | 481 |
| 483 void BluetoothDeviceChooserController::PostErrorCallback( | 482 void BluetoothDeviceChooserController::PostErrorCallback( |
| 484 blink::mojom::WebBluetoothError error) { | 483 blink::mojom::WebBluetoothError error) { |
| 485 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 484 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 486 FROM_HERE, base::Bind(error_callback_, error))) { | 485 FROM_HERE, base::Bind(error_callback_, error))) { |
| 487 LOG(WARNING) << "No TaskRunner."; | 486 LOG(WARNING) << "No TaskRunner."; |
| 488 } | 487 } |
| 489 } | 488 } |
| 490 | 489 |
| 491 } // namespace content | 490 } // namespace content |
| OLD | NEW |