Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Side by Side Diff: device/bluetooth/bluetooth_adapter_chromeos.cc

Issue 982593002: Fix BluetoothAdapterProfileChromeOS lifecycle management (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address nits, remove test debug logging Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) {
1019 profiles_[uuid] = profile.release();
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, 1036 if (profiles_.find(uuid) == profiles_.end()) {
990 error_callback); 1037 error_callback.Run("Cannot find profile!");
991 } 1038 return;
1039 }
992 1040
993 bool BluetoothAdapterChromeOS::SetProfileDelegate(
994 const BluetoothUUID& uuid,
995 const dbus::ObjectPath& device_path,
996 BluetoothProfileServiceProvider::Delegate* delegate,
997 const ProfileRegisteredCallback& success_callback,
998 const ErrorCompletionCallback& error_callback) {
999 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { 1041 if (profiles_[uuid]->SetDelegate(device_path, delegate)) {
1000 success_callback.Run(profiles_[uuid]); 1042 success_callback.Run(profiles_[uuid]);
1001 return true; 1043 return;
1002 } 1044 }
1003 // Already set 1045 // Already set
1004 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); 1046 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists);
1005 return false;
1006 } 1047 }
1007 1048
1008 void BluetoothAdapterChromeOS::OnRegisterProfileError( 1049 void BluetoothAdapterChromeOS::OnRegisterProfileError(
1009 const BluetoothUUID& uuid, 1050 const BluetoothUUID& uuid,
1010 const ErrorCompletionCallback& error_callback,
1011 const std::string& error_name, 1051 const std::string& error_name,
1012 const std::string& error_message) { 1052 const std::string& error_message) {
1013 LOG(WARNING) << object_path_.value() 1053 VLOG(2) << object_path_.value() << ": Failed to register profile: "
1014 << ": Failed to register profile: " << error_name << ": " 1054 << error_name << ": " << error_message;
1015 << error_message; 1055 if (profile_queues_.find(uuid) == profile_queues_.end())
1016 error_callback.Run(error_message); 1056 return;
1017 ReleaseProfile(uuid); 1057
1058 for (auto& it : *profile_queues_[uuid])
1059 it.second.Run(error_message);
1060
1061 delete profile_queues_[uuid];
1062 profile_queues_.erase(uuid);
1018 } 1063 }
1019 1064
1020 void BluetoothAdapterChromeOS::OnSetDiscoverable( 1065 void BluetoothAdapterChromeOS::OnSetDiscoverable(
1021 const base::Closure& callback, 1066 const base::Closure& callback,
1022 const ErrorCallback& error_callback, 1067 const ErrorCallback& error_callback,
1023 bool success) { 1068 bool success) {
1024 if (!IsPresent()) { 1069 if (!IsPresent()) {
1025 error_callback.Run(); 1070 error_callback.Run();
1026 return; 1071 return;
1027 } 1072 }
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 1269
1225 // If the queued request resulted in a pending call, then let it 1270 // If the queued request resulted in a pending call, then let it
1226 // asynchonously process the remaining queued requests once the pending 1271 // asynchonously process the remaining queued requests once the pending
1227 // call returns. 1272 // call returns.
1228 if (discovery_request_pending_) 1273 if (discovery_request_pending_)
1229 return; 1274 return;
1230 } 1275 }
1231 } 1276 }
1232 1277
1233 } // namespace chromeos 1278 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_chromeos.h ('k') | device/bluetooth/bluetooth_adapter_profile_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698