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/location.h" | 10 #include "base/location.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 void BluetoothAdapterChromeOS::Shutdown() { | 78 void BluetoothAdapterChromeOS::Shutdown() { |
79 if (dbus_is_shutdown_) | 79 if (dbus_is_shutdown_) |
80 return; | 80 return; |
81 DCHECK(DBusThreadManager::IsInitialized()) | 81 DCHECK(DBusThreadManager::IsInitialized()) |
82 << "Call BluetoothAdapterFactory::Shutdown() before " | 82 << "Call BluetoothAdapterFactory::Shutdown() before " |
83 "DBusThreadManager::Shutdown()."; | 83 "DBusThreadManager::Shutdown()."; |
84 | 84 |
85 if (IsPresent()) | 85 if (IsPresent()) |
86 RemoveAdapter(); // Also deletes devices_. | 86 RemoveAdapter(); // Also deletes devices_. |
87 DCHECK(devices_.empty()); | 87 DCHECK(devices_.empty()); |
88 // profiles_ should be empty because all BluetoothSockets have been signaled | |
89 // that this adapter is disappearing. | |
90 DCHECK(profiles_.empty()); | |
91 | |
92 for (auto& it : profile_queues_) | |
93 delete it.second; | |
94 profile_queues_.clear(); | |
95 | 88 |
96 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); | 89 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); |
97 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); | 90 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); |
98 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); | 91 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); |
99 | 92 |
100 VLOG(1) << "Unregistering pairing agent"; | 93 VLOG(1) << "Unregistering pairing agent"; |
101 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent( | 94 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent( |
102 dbus::ObjectPath(kAgentPath), base::Bind(&base::DoNothing), | 95 dbus::ObjectPath(kAgentPath), base::Bind(&base::DoNothing), |
103 base::Bind(&OnUnregisterAgentError)); | 96 base::Bind(&OnUnregisterAgentError)); |
104 | 97 |
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 | 950 |
958 void BluetoothAdapterChromeOS::UseProfile( | 951 void BluetoothAdapterChromeOS::UseProfile( |
959 const BluetoothUUID& uuid, | 952 const BluetoothUUID& uuid, |
960 const dbus::ObjectPath& device_path, | 953 const dbus::ObjectPath& device_path, |
961 const BluetoothProfileManagerClient::Options& options, | 954 const BluetoothProfileManagerClient::Options& options, |
962 BluetoothProfileServiceProvider::Delegate* delegate, | 955 BluetoothProfileServiceProvider::Delegate* delegate, |
963 const ProfileRegisteredCallback& success_callback, | 956 const ProfileRegisteredCallback& success_callback, |
964 const ErrorCompletionCallback& error_callback) { | 957 const ErrorCompletionCallback& error_callback) { |
965 DCHECK(delegate); | 958 DCHECK(delegate); |
966 | 959 |
967 if (!IsPresent()) { | |
968 VLOG(2) << "Adapter not present, erroring out"; | |
969 error_callback.Run("Adapter not present"); | |
970 return; | |
971 } | |
972 | |
973 if (profiles_.find(uuid) != profiles_.end()) { | 960 if (profiles_.find(uuid) != profiles_.end()) { |
974 // TODO(jamuraa) check that the options are the same and error when they are | 961 // TODO(jamuraa) check that the options are the same and error when they are |
975 // not. | 962 // not. |
976 SetProfileDelegate(uuid, device_path, delegate, success_callback, | 963 SetProfileDelegate(uuid, device_path, delegate, success_callback, |
977 error_callback); | 964 error_callback); |
978 return; | 965 return; |
979 } | 966 } |
980 | 967 |
981 if (profile_queues_.find(uuid) == profile_queues_.end()) { | 968 profiles_[uuid] = BluetoothAdapterProfileChromeOS::Register( |
982 BluetoothAdapterProfileChromeOS::Register( | 969 this, uuid, options, |
983 uuid, options, | 970 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid, |
984 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid), | |
985 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, | |
986 uuid)); | |
987 | |
988 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>(); | |
989 } | |
990 | |
991 profile_queues_[uuid]->push_back(std::make_pair( | |
992 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate, this, uuid, | |
993 device_path, delegate, success_callback, error_callback), | 971 device_path, delegate, success_callback, error_callback), |
994 error_callback)); | 972 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, uuid, |
| 973 error_callback)); |
995 } | 974 } |
996 | 975 |
997 void BluetoothAdapterChromeOS::ReleaseProfile( | 976 void BluetoothAdapterChromeOS::ReleaseProfile(const BluetoothUUID& uuid) { |
998 const dbus::ObjectPath& device_path, | |
999 BluetoothAdapterProfileChromeOS* profile) { | |
1000 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value() | |
1001 << " from " << device_path.value(); | |
1002 profile->RemoveDelegate( | |
1003 device_path, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile, | |
1004 weak_ptr_factory_.GetWeakPtr(), profile->uuid())); | |
1005 } | |
1006 | |
1007 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID& uuid) { | |
1008 VLOG(2) << "Remove Profile: " << uuid.canonical_value(); | |
1009 | |
1010 if (profiles_.find(uuid) != profiles_.end()) { | 977 if (profiles_.find(uuid) != profiles_.end()) { |
1011 delete profiles_[uuid]; | 978 delete profiles_[uuid]; |
1012 profiles_.erase(uuid); | 979 profiles_.erase(uuid); |
1013 } | 980 } |
1014 } | 981 } |
1015 | 982 |
1016 void BluetoothAdapterChromeOS::OnRegisterProfile( | 983 void BluetoothAdapterChromeOS::OnRegisterProfile( |
1017 const BluetoothUUID& uuid, | 984 const BluetoothUUID& uuid, |
1018 BluetoothAdapterProfileChromeOS* profile) { | |
1019 profiles_[uuid] = profile; | |
1020 | |
1021 if (profile_queues_.find(uuid) == profile_queues_.end()) | |
1022 return; | |
1023 | |
1024 for (auto& it : *profile_queues_[uuid]) | |
1025 it.first.Run(); | |
1026 delete profile_queues_[uuid]; | |
1027 profile_queues_.erase(uuid); | |
1028 } | |
1029 | |
1030 void BluetoothAdapterChromeOS::SetProfileDelegate( | |
1031 const BluetoothUUID& uuid, | |
1032 const dbus::ObjectPath& device_path, | 985 const dbus::ObjectPath& device_path, |
1033 BluetoothProfileServiceProvider::Delegate* delegate, | 986 BluetoothProfileServiceProvider::Delegate* delegate, |
1034 const ProfileRegisteredCallback& success_callback, | 987 const ProfileRegisteredCallback& success_callback, |
1035 const ErrorCompletionCallback& error_callback) { | 988 const ErrorCompletionCallback& error_callback) { |
| 989 SetProfileDelegate(uuid, device_path, delegate, success_callback, |
| 990 error_callback); |
| 991 } |
1036 | 992 |
1037 if (profiles_.find(uuid) == profiles_.end()) { | 993 bool BluetoothAdapterChromeOS::SetProfileDelegate( |
1038 error_callback.Run("Cannot find profile!"); | 994 const BluetoothUUID& uuid, |
1039 return; | 995 const dbus::ObjectPath& device_path, |
1040 } | 996 BluetoothProfileServiceProvider::Delegate* delegate, |
1041 | 997 const ProfileRegisteredCallback& success_callback, |
| 998 const ErrorCompletionCallback& error_callback) { |
1042 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { | 999 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { |
1043 success_callback.Run(profiles_[uuid]); | 1000 success_callback.Run(profiles_[uuid]); |
1044 return; | 1001 return true; |
1045 } | 1002 } |
1046 // Already set | 1003 // Already set |
1047 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); | 1004 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); |
| 1005 return false; |
1048 } | 1006 } |
1049 | 1007 |
1050 void BluetoothAdapterChromeOS::OnRegisterProfileError( | 1008 void BluetoothAdapterChromeOS::OnRegisterProfileError( |
1051 const BluetoothUUID& uuid, | 1009 const BluetoothUUID& uuid, |
| 1010 const ErrorCompletionCallback& error_callback, |
1052 const std::string& error_name, | 1011 const std::string& error_name, |
1053 const std::string& error_message) { | 1012 const std::string& error_message) { |
1054 VLOG(2) << object_path_.value() << ": Failed to register profile: " | 1013 LOG(WARNING) << object_path_.value() |
1055 << error_name << ": " << error_message; | 1014 << ": Failed to register profile: " << error_name << ": " |
1056 if (profile_queues_.find(uuid) == profile_queues_.end()) | 1015 << error_message; |
1057 return; | 1016 error_callback.Run(error_message); |
1058 | 1017 ReleaseProfile(uuid); |
1059 for (auto& it : *profile_queues_[uuid]) | |
1060 it.second.Run(error_message); | |
1061 | |
1062 delete profile_queues_[uuid]; | |
1063 profile_queues_.erase(uuid); | |
1064 } | 1018 } |
1065 | 1019 |
1066 void BluetoothAdapterChromeOS::OnSetDiscoverable( | 1020 void BluetoothAdapterChromeOS::OnSetDiscoverable( |
1067 const base::Closure& callback, | 1021 const base::Closure& callback, |
1068 const ErrorCallback& error_callback, | 1022 const ErrorCallback& error_callback, |
1069 bool success) { | 1023 bool success) { |
1070 if (!IsPresent()) { | 1024 if (!IsPresent()) { |
1071 error_callback.Run(); | 1025 error_callback.Run(); |
1072 return; | 1026 return; |
1073 } | 1027 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1270 | 1224 |
1271 // If the queued request resulted in a pending call, then let it | 1225 // If the queued request resulted in a pending call, then let it |
1272 // asynchonously process the remaining queued requests once the pending | 1226 // asynchonously process the remaining queued requests once the pending |
1273 // call returns. | 1227 // call returns. |
1274 if (discovery_request_pending_) | 1228 if (discovery_request_pending_) |
1275 return; | 1229 return; |
1276 } | 1230 } |
1277 } | 1231 } |
1278 | 1232 |
1279 } // namespace chromeos | 1233 } // namespace chromeos |
OLD | NEW |