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