| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/bluetooth_adapter_client.h" | 5 #include "chromeos/dbus/bluetooth_adapter_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "chromeos/dbus/bluetooth_device_client.h" | 14 #include "chromeos/dbus/bluetooth_device_client.h" |
| 15 #include "chromeos/dbus/bluetooth_manager_client.h" | 15 #include "chromeos/dbus/bluetooth_manager_client.h" |
| 16 #include "chromeos/dbus/bluetooth_property.h" | 16 #include "chromeos/dbus/bluetooth_property.h" |
| 17 #include "chromeos/dbus/fake_old_bluetooth_adapter_client.h" |
| 17 #include "dbus/bus.h" | 18 #include "dbus/bus.h" |
| 18 #include "dbus/message.h" | 19 #include "dbus/message.h" |
| 19 #include "dbus/object_path.h" | 20 #include "dbus/object_path.h" |
| 20 #include "dbus/object_proxy.h" | 21 #include "dbus/object_proxy.h" |
| 21 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // The |CreatePairedDevice| DBus call needs a longer timeout than the default | 26 // The |CreatePairedDevice| DBus call needs a longer timeout than the default |
| 26 // in order to allow BlueZ to timeout this call first. See crosbug.com/37387. | 27 // in order to allow BlueZ to timeout this call first. See crosbug.com/37387. |
| (...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 | 729 |
| 729 // Weak pointer factory for generating 'this' pointers that might live longer | 730 // Weak pointer factory for generating 'this' pointers that might live longer |
| 730 // than we do. | 731 // than we do. |
| 731 // Note: This should remain the last member so it'll be destroyed and | 732 // Note: This should remain the last member so it'll be destroyed and |
| 732 // invalidate its weak pointers before any other members are destroyed. | 733 // invalidate its weak pointers before any other members are destroyed. |
| 733 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_; | 734 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_; |
| 734 | 735 |
| 735 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); | 736 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); |
| 736 }; | 737 }; |
| 737 | 738 |
| 738 // The BluetoothAdapterClient implementation used on Linux desktop, which does | |
| 739 // nothing. | |
| 740 class BluetoothAdapterClientStubImpl : public BluetoothAdapterClient { | |
| 741 public: | |
| 742 struct Properties : public BluetoothAdapterClient::Properties { | |
| 743 explicit Properties(const PropertyChangedCallback& callback) | |
| 744 : BluetoothAdapterClient::Properties(NULL, callback) { | |
| 745 } | |
| 746 | |
| 747 virtual ~Properties() { | |
| 748 } | |
| 749 | |
| 750 virtual void Get(dbus::PropertyBase* property, | |
| 751 dbus::PropertySet::GetCallback callback) OVERRIDE { | |
| 752 VLOG(1) << "Get " << property->name(); | |
| 753 callback.Run(false); | |
| 754 } | |
| 755 | |
| 756 virtual void GetAll() OVERRIDE { | |
| 757 VLOG(1) << "GetAll"; | |
| 758 } | |
| 759 | |
| 760 virtual void Set(dbus::PropertyBase *property, | |
| 761 dbus::PropertySet::SetCallback callback) OVERRIDE { | |
| 762 VLOG(1) << "Set " << property->name(); | |
| 763 if (property->name() == "Powered") { | |
| 764 property->ReplaceValueWithSetValue(); | |
| 765 callback.Run(true); | |
| 766 } else { | |
| 767 callback.Run(false); | |
| 768 } | |
| 769 } | |
| 770 }; | |
| 771 | |
| 772 BluetoothAdapterClientStubImpl() { | |
| 773 properties_.reset(new Properties(base::Bind( | |
| 774 &BluetoothAdapterClientStubImpl::OnPropertyChanged, | |
| 775 base::Unretained(this)))); | |
| 776 | |
| 777 properties_->address.ReplaceValue("hci0"); | |
| 778 properties_->name.ReplaceValue("Fake Adapter"); | |
| 779 properties_->pairable.ReplaceValue(true); | |
| 780 | |
| 781 std::vector<dbus::ObjectPath> devices; | |
| 782 devices.push_back(dbus::ObjectPath("/fake/hci0/dev0")); | |
| 783 properties_->devices.ReplaceValue(devices); | |
| 784 } | |
| 785 | |
| 786 // BluetoothAdapterClient override. | |
| 787 virtual void AddObserver(Observer* observer) OVERRIDE { | |
| 788 observers_.AddObserver(observer); | |
| 789 } | |
| 790 | |
| 791 // BluetoothAdapterClient override. | |
| 792 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
| 793 observers_.RemoveObserver(observer); | |
| 794 } | |
| 795 | |
| 796 // BluetoothAdapterClient override. | |
| 797 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
| 798 OVERRIDE { | |
| 799 VLOG(1) << "GetProperties: " << object_path.value(); | |
| 800 if (object_path.value() == "/fake/hci0") | |
| 801 return properties_.get(); | |
| 802 else | |
| 803 return NULL; | |
| 804 } | |
| 805 | |
| 806 // BluetoothAdapterClient override. | |
| 807 virtual void RequestSession(const dbus::ObjectPath& object_path, | |
| 808 const AdapterCallback& callback) OVERRIDE { | |
| 809 VLOG(1) << "RequestSession: " << object_path.value(); | |
| 810 callback.Run(object_path, false); | |
| 811 } | |
| 812 | |
| 813 // BluetoothAdapterClient override. | |
| 814 virtual void ReleaseSession(const dbus::ObjectPath& object_path, | |
| 815 const AdapterCallback& callback) OVERRIDE { | |
| 816 VLOG(1) << "ReleaseSession: " << object_path.value(); | |
| 817 callback.Run(object_path, false); | |
| 818 } | |
| 819 | |
| 820 // BluetoothAdapterClient override. | |
| 821 virtual void StartDiscovery(const dbus::ObjectPath& object_path, | |
| 822 const AdapterCallback& callback) OVERRIDE { | |
| 823 VLOG(1) << "StartDiscovery: " << object_path.value(); | |
| 824 callback.Run(object_path, false); | |
| 825 } | |
| 826 | |
| 827 // BluetoothAdapterClient override. | |
| 828 virtual void StopDiscovery(const dbus::ObjectPath& object_path, | |
| 829 const AdapterCallback& callback) OVERRIDE { | |
| 830 VLOG(1) << "StopDiscovery: " << object_path.value(); | |
| 831 callback.Run(object_path, false); | |
| 832 } | |
| 833 | |
| 834 // BluetoothAdapterClient override. | |
| 835 virtual void FindDevice(const dbus::ObjectPath& object_path, | |
| 836 const std::string& address, | |
| 837 const DeviceCallback& callback) OVERRIDE { | |
| 838 VLOG(1) << "FindDevice: " << object_path.value() << " " << address; | |
| 839 callback.Run(dbus::ObjectPath(), false); | |
| 840 } | |
| 841 | |
| 842 // BluetoothAdapterClient override. | |
| 843 virtual void CreateDevice(const dbus::ObjectPath& object_path, | |
| 844 const std::string& address, | |
| 845 const CreateDeviceCallback& callback, | |
| 846 const CreateDeviceErrorCallback& error_callback) | |
| 847 OVERRIDE { | |
| 848 VLOG(1) << "CreateDevice: " << object_path.value() << " " << address; | |
| 849 error_callback.Run("", ""); | |
| 850 } | |
| 851 | |
| 852 // BluetoothAdapterClient override. | |
| 853 virtual void CreatePairedDevice( | |
| 854 const dbus::ObjectPath& object_path, const std::string& address, | |
| 855 const dbus::ObjectPath& agent_path, const std::string& capability, | |
| 856 const CreateDeviceCallback& callback, | |
| 857 const CreateDeviceErrorCallback& error_callback) OVERRIDE { | |
| 858 VLOG(1) << "CreatePairedDevice: " << object_path.value() << " " << address | |
| 859 << " " << agent_path.value() << " " << capability; | |
| 860 error_callback.Run("", ""); | |
| 861 } | |
| 862 | |
| 863 // BluetoothAdapterClient override. | |
| 864 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path, | |
| 865 const std::string& address, | |
| 866 const AdapterCallback& callback) OVERRIDE { | |
| 867 VLOG(1) << "CancelDeviceCreation: " << object_path.value() | |
| 868 << " " << address; | |
| 869 callback.Run(object_path, false); | |
| 870 } | |
| 871 | |
| 872 // BluetoothAdapterClient override. | |
| 873 virtual void RemoveDevice(const dbus::ObjectPath& object_path, | |
| 874 const dbus::ObjectPath& device_path, | |
| 875 const AdapterCallback& callback) OVERRIDE { | |
| 876 VLOG(1) << "RemoveDevice: " << object_path.value() | |
| 877 << " " << device_path.value(); | |
| 878 callback.Run(object_path, false); | |
| 879 } | |
| 880 | |
| 881 // BluetoothAdapterClient override. | |
| 882 virtual void RegisterAgent(const dbus::ObjectPath& object_path, | |
| 883 const dbus::ObjectPath& agent_path, | |
| 884 const std::string& capability, | |
| 885 const AdapterCallback& callback) OVERRIDE { | |
| 886 VLOG(1) << "RegisterAgent: " << object_path.value() | |
| 887 << " " << agent_path.value(); | |
| 888 callback.Run(object_path, false); | |
| 889 } | |
| 890 | |
| 891 // BluetoothAdapterClient override. | |
| 892 virtual void UnregisterAgent(const dbus::ObjectPath& object_path, | |
| 893 const dbus::ObjectPath& agent_path, | |
| 894 const AdapterCallback& callback) OVERRIDE { | |
| 895 VLOG(1) << "UnregisterAgent: " << object_path.value() | |
| 896 << " " << agent_path.value(); | |
| 897 callback.Run(object_path, false); | |
| 898 } | |
| 899 | |
| 900 private: | |
| 901 void OnPropertyChanged(const std::string& property_name) { | |
| 902 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
| 903 AdapterPropertyChanged(dbus::ObjectPath("/fake/hci0"), | |
| 904 property_name)); | |
| 905 } | |
| 906 | |
| 907 // List of observers interested in event notifications from us. | |
| 908 ObserverList<Observer> observers_; | |
| 909 | |
| 910 // Static properties we return. | |
| 911 scoped_ptr<Properties> properties_; | |
| 912 }; | |
| 913 | |
| 914 BluetoothAdapterClient::BluetoothAdapterClient() { | 739 BluetoothAdapterClient::BluetoothAdapterClient() { |
| 915 } | 740 } |
| 916 | 741 |
| 917 BluetoothAdapterClient::~BluetoothAdapterClient() { | 742 BluetoothAdapterClient::~BluetoothAdapterClient() { |
| 918 } | 743 } |
| 919 | 744 |
| 920 BluetoothAdapterClient* BluetoothAdapterClient::Create( | 745 BluetoothAdapterClient* BluetoothAdapterClient::Create( |
| 921 DBusClientImplementationType type, | 746 DBusClientImplementationType type, |
| 922 dbus::Bus* bus, | 747 dbus::Bus* bus, |
| 923 BluetoothManagerClient* manager_client) { | 748 BluetoothManagerClient* manager_client) { |
| 924 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 749 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 925 return new BluetoothAdapterClientImpl(bus, manager_client); | 750 return new BluetoothAdapterClientImpl(bus, manager_client); |
| 926 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 751 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 927 return new BluetoothAdapterClientStubImpl(); | 752 return new FakeOldBluetoothAdapterClient(); |
| 928 } | 753 } |
| 929 | 754 |
| 930 } // namespace chromeos | 755 } // namespace chromeos |
| OLD | NEW |