| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "device/bluetooth/bluetooth_adapter_chromeos.h" | 5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 | 618 |
| 619 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { | 619 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { |
| 620 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 620 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 621 AdapterDiscoverableChanged(this, discoverable)); | 621 AdapterDiscoverableChanged(this, discoverable)); |
| 622 } | 622 } |
| 623 | 623 |
| 624 void BluetoothAdapterChromeOS::DiscoveringChanged( | 624 void BluetoothAdapterChromeOS::DiscoveringChanged( |
| 625 bool discovering) { | 625 bool discovering) { |
| 626 // If the adapter stopped discovery due to a reason other than a request by | 626 // If the adapter stopped discovery due to a reason other than a request by |
| 627 // us, reset the count to 0. | 627 // us, reset the count to 0. |
| 628 VLOG(1) << "Discovering changed: " << discovering; |
| 628 if (!discovering && !discovery_request_pending_ | 629 if (!discovering && !discovery_request_pending_ |
| 629 && num_discovery_sessions_ > 0) { | 630 && num_discovery_sessions_ > 0) { |
| 631 VLOG(1) << "Marking sessions as inactive."; |
| 630 num_discovery_sessions_ = 0; | 632 num_discovery_sessions_ = 0; |
| 631 MarkDiscoverySessionsAsInactive(); | 633 MarkDiscoverySessionsAsInactive(); |
| 632 } | 634 } |
| 633 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 635 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 634 AdapterDiscoveringChanged(this, discovering)); | 636 AdapterDiscoveringChanged(this, discovering)); |
| 635 } | 637 } |
| 636 | 638 |
| 637 void BluetoothAdapterChromeOS::PresentChanged(bool present) { | 639 void BluetoothAdapterChromeOS::PresentChanged(bool present) { |
| 638 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 640 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 639 AdapterPresentChanged(this, present)); | 641 AdapterPresentChanged(this, present)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 } | 675 } |
| 674 | 676 |
| 675 void BluetoothAdapterChromeOS::AddDiscoverySession( | 677 void BluetoothAdapterChromeOS::AddDiscoverySession( |
| 676 const base::Closure& callback, | 678 const base::Closure& callback, |
| 677 const ErrorCallback& error_callback) { | 679 const ErrorCallback& error_callback) { |
| 678 VLOG(1) << __func__; | 680 VLOG(1) << __func__; |
| 679 if (discovery_request_pending_) { | 681 if (discovery_request_pending_) { |
| 680 // The pending request is either to stop a previous session or to start a | 682 // The pending request is either to stop a previous session or to start a |
| 681 // new one. Either way, queue this one. | 683 // new one. Either way, queue this one. |
| 682 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); | 684 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); |
| 683 VLOG(1) << "Pending request to initiate device discovery. Queueing request " | 685 VLOG(1) << "Pending request to start/stop device discovery. Queueing " |
| 684 << "to start a new discovery session."; | 686 << "request to start a new discovery session."; |
| 685 discovery_request_queue_.push(std::make_pair(callback, error_callback)); | 687 discovery_request_queue_.push(std::make_pair(callback, error_callback)); |
| 686 return; | 688 return; |
| 687 } | 689 } |
| 688 | 690 |
| 689 // The adapter is already discovering. | 691 // The adapter is already discovering. |
| 690 if (num_discovery_sessions_ > 0) { | 692 if (num_discovery_sessions_ > 0) { |
| 691 DCHECK(IsDiscovering()); | 693 DCHECK(IsDiscovering()); |
| 692 DCHECK(!discovery_request_pending_); | 694 DCHECK(!discovery_request_pending_); |
| 693 num_discovery_sessions_++; | 695 num_discovery_sessions_++; |
| 694 callback.Run(); | 696 callback.Run(); |
| 695 return; | 697 return; |
| 696 } | 698 } |
| 697 | 699 |
| 698 // There are no active discovery sessions. | 700 // There are no active discovery sessions. |
| 699 DCHECK(num_discovery_sessions_ == 0); | 701 DCHECK(num_discovery_sessions_ == 0); |
| 700 | 702 |
| 701 // This is the first request to start device discovery. | 703 // This is the first request to start device discovery. |
| 702 discovery_request_pending_ = true; | 704 discovery_request_pending_ = true; |
| 703 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 705 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 704 StartDiscovery( | 706 StartDiscovery( |
| 705 object_path_, | 707 object_path_, |
| 706 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | 708 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, |
| 707 weak_ptr_factory_.GetWeakPtr(), | 709 weak_ptr_factory_.GetWeakPtr(), |
| 708 callback), | 710 callback), |
| 709 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | 711 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, |
| 710 weak_ptr_factory_.GetWeakPtr(), | 712 weak_ptr_factory_.GetWeakPtr(), |
| 713 callback, |
| 711 error_callback)); | 714 error_callback)); |
| 712 } | 715 } |
| 713 | 716 |
| 714 void BluetoothAdapterChromeOS::RemoveDiscoverySession( | 717 void BluetoothAdapterChromeOS::RemoveDiscoverySession( |
| 715 const base::Closure& callback, | 718 const base::Closure& callback, |
| 716 const ErrorCallback& error_callback) { | 719 const ErrorCallback& error_callback) { |
| 717 VLOG(1) << __func__; | 720 VLOG(1) << __func__; |
| 718 // There are active sessions other than the one currently being removed. | 721 // There are active sessions other than the one currently being removed. |
| 719 if (num_discovery_sessions_ > 1) { | 722 if (num_discovery_sessions_ > 1) { |
| 720 DCHECK(IsDiscovering()); | 723 DCHECK(IsDiscovering()); |
| 721 DCHECK(!discovery_request_pending_); | 724 DCHECK(!discovery_request_pending_); |
| 722 num_discovery_sessions_--; | 725 num_discovery_sessions_--; |
| 723 callback.Run(); | 726 callback.Run(); |
| 724 return; | 727 return; |
| 725 } | 728 } |
| 726 | 729 |
| 727 // If there is a pending request to BlueZ, then queue this request. | 730 // If there is a pending request to BlueZ, then queue this request. |
| 728 if (discovery_request_pending_) { | 731 if (discovery_request_pending_) { |
| 729 VLOG(1) << "Pending request to initiate device discovery. Queueing request " | 732 VLOG(1) << "Pending request to start/stop device discovery. Queueing " |
| 730 << "to stop discovery session."; | 733 << "request to stop discovery session."; |
| 731 error_callback.Run(); | 734 error_callback.Run(); |
| 732 return; | 735 return; |
| 733 } | 736 } |
| 734 | 737 |
| 735 // There are no active sessions. Return error. | 738 // There are no active sessions. Return error. |
| 736 if (num_discovery_sessions_ == 0) { | 739 if (num_discovery_sessions_ == 0) { |
| 737 // TODO(armansito): This should never happen once we have the | 740 // TODO(armansito): This should never happen once we have the |
| 738 // DiscoverySession API. Replace this case with an assert once it's | 741 // DiscoverySession API. Replace this case with an assert once it's |
| 739 // the deprecated methods have been removed. (See crbug.com/3445008). | 742 // the deprecated methods have been removed. (See crbug.com/3445008). |
| 740 VLOG(1) << "No active discovery sessions. Returning error."; | 743 VLOG(1) << "No active discovery sessions. Returning error."; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 752 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | 755 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, |
| 753 weak_ptr_factory_.GetWeakPtr(), | 756 weak_ptr_factory_.GetWeakPtr(), |
| 754 callback), | 757 callback), |
| 755 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | 758 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, |
| 756 weak_ptr_factory_.GetWeakPtr(), | 759 weak_ptr_factory_.GetWeakPtr(), |
| 757 error_callback)); | 760 error_callback)); |
| 758 } | 761 } |
| 759 | 762 |
| 760 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { | 763 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
| 761 // Report success on the original request and increment the count. | 764 // Report success on the original request and increment the count. |
| 765 VLOG(1) << __func__; |
| 762 DCHECK(discovery_request_pending_); | 766 DCHECK(discovery_request_pending_); |
| 763 DCHECK(num_discovery_sessions_ == 0); | 767 DCHECK(num_discovery_sessions_ == 0); |
| 764 discovery_request_pending_ = false; | 768 discovery_request_pending_ = false; |
| 765 num_discovery_sessions_++; | 769 num_discovery_sessions_++; |
| 766 callback.Run(); | 770 callback.Run(); |
| 767 | 771 |
| 768 // Try to add a new discovery session for each queued request. | 772 // Try to add a new discovery session for each queued request. |
| 769 ProcessQueuedDiscoveryRequests(); | 773 ProcessQueuedDiscoveryRequests(); |
| 770 } | 774 } |
| 771 | 775 |
| 772 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | 776 void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
| 777 const base::Closure& callback, |
| 773 const ErrorCallback& error_callback, | 778 const ErrorCallback& error_callback, |
| 774 const std::string& error_name, | 779 const std::string& error_name, |
| 775 const std::string& error_message) { | 780 const std::string& error_message) { |
| 776 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | 781 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
| 777 << error_name << ": " << error_message; | 782 << error_name << ": " << error_message; |
| 778 | 783 |
| 779 // Failed to start discovery. This can only happen if the count is at 0. | 784 // Failed to start discovery. This can only happen if the count is at 0. |
| 780 DCHECK(num_discovery_sessions_ == 0); | 785 DCHECK(num_discovery_sessions_ == 0); |
| 781 DCHECK(discovery_request_pending_); | 786 DCHECK(discovery_request_pending_); |
| 782 discovery_request_pending_ = false; | 787 discovery_request_pending_ = false; |
| 783 error_callback.Run(); | 788 |
| 789 // Discovery request may fail if discovery was previously initiated by Chrome, |
| 790 // but the session were invalidated due to the discovery state unexpectedly |
| 791 // changing to false and then back to true. In this case, report success. |
| 792 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { |
| 793 VLOG(1) << "Discovery previously initiated. Reporting success."; |
| 794 num_discovery_sessions_++; |
| 795 callback.Run(); |
| 796 } else { |
| 797 error_callback.Run(); |
| 798 } |
| 784 | 799 |
| 785 // Try to add a new discovery session for each queued request. | 800 // Try to add a new discovery session for each queued request. |
| 786 ProcessQueuedDiscoveryRequests(); | 801 ProcessQueuedDiscoveryRequests(); |
| 787 } | 802 } |
| 788 | 803 |
| 789 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | 804 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
| 790 // Report success on the original request and decrement the count. | 805 // Report success on the original request and decrement the count. |
| 806 VLOG(1) << __func__; |
| 791 DCHECK(discovery_request_pending_); | 807 DCHECK(discovery_request_pending_); |
| 792 DCHECK(num_discovery_sessions_ == 1); | 808 DCHECK(num_discovery_sessions_ == 1); |
| 793 discovery_request_pending_ = false; | 809 discovery_request_pending_ = false; |
| 794 num_discovery_sessions_--; | 810 num_discovery_sessions_--; |
| 795 callback.Run(); | 811 callback.Run(); |
| 796 | 812 |
| 797 // Try to add a new discovery session for each queued request. | 813 // Try to add a new discovery session for each queued request. |
| 798 ProcessQueuedDiscoveryRequests(); | 814 ProcessQueuedDiscoveryRequests(); |
| 799 } | 815 } |
| 800 | 816 |
| 801 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | 817 void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
| 802 const ErrorCallback& error_callback, | 818 const ErrorCallback& error_callback, |
| 803 const std::string& error_name, | 819 const std::string& error_name, |
| 804 const std::string& error_message) { | 820 const std::string& error_message) { |
| 805 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | 821 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
| 806 << error_name << ": " << error_message; | 822 << error_name << ": " << error_message; |
| 807 | 823 |
| 808 // Failed to stop discovery. This can only happen if the count is at 1. | 824 // Failed to stop discovery. This can only happen if the count is at 1. |
| 809 DCHECK(discovery_request_pending_); | 825 DCHECK(discovery_request_pending_); |
| 810 DCHECK(num_discovery_sessions_ == 1); | 826 DCHECK(num_discovery_sessions_ == 1); |
| 811 discovery_request_pending_ = false; | 827 discovery_request_pending_ = false; |
| 812 error_callback.Run(); | 828 error_callback.Run(); |
| 813 | 829 |
| 814 // Try to add a new discovery session for each queued request. | 830 // Try to add a new discovery session for each queued request. |
| 815 ProcessQueuedDiscoveryRequests(); | 831 ProcessQueuedDiscoveryRequests(); |
| 816 } | 832 } |
| 817 | 833 |
| 818 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | 834 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { |
| 819 while (!discovery_request_queue_.empty()) { | 835 while (!discovery_request_queue_.empty()) { |
| 836 VLOG(1) << "Process queued discovery request."; |
| 820 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); | 837 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); |
| 821 discovery_request_queue_.pop(); | 838 discovery_request_queue_.pop(); |
| 822 AddDiscoverySession(callbacks.first, callbacks.second); | 839 AddDiscoverySession(callbacks.first, callbacks.second); |
| 823 | 840 |
| 824 // If the queued request resulted in a pending call, then let it | 841 // If the queued request resulted in a pending call, then let it |
| 825 // asynchonously process the remaining queued requests once the pending | 842 // asynchonously process the remaining queued requests once the pending |
| 826 // call returns. | 843 // call returns. |
| 827 if (discovery_request_pending_) | 844 if (discovery_request_pending_) |
| 828 return; | 845 return; |
| 829 } | 846 } |
| 830 } | 847 } |
| 831 | 848 |
| 832 } // namespace chromeos | 849 } // namespace chromeos |
| OLD | NEW |