Chromium Code Reviews| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "chromeos/dbus/bluetooth_adapter_client.h" | 17 #include "chromeos/dbus/bluetooth_adapter_client.h" |
| 18 #include "chromeos/dbus/bluetooth_agent_manager_client.h" | 18 #include "chromeos/dbus/bluetooth_agent_manager_client.h" |
| 19 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | 19 #include "chromeos/dbus/bluetooth_agent_service_provider.h" |
| 20 #include "chromeos/dbus/bluetooth_device_client.h" | 20 #include "chromeos/dbus/bluetooth_device_client.h" |
| 21 #include "chromeos/dbus/bluetooth_input_client.h" | 21 #include "chromeos/dbus/bluetooth_input_client.h" |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 23 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" | |
| 23 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 24 #include "device/bluetooth/bluetooth_device_chromeos.h" | 25 #include "device/bluetooth/bluetooth_device_chromeos.h" |
| 25 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | 26 #include "device/bluetooth/bluetooth_pairing_chromeos.h" |
| 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | 27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
| 27 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
| 28 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | 29 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 29 #include "device/bluetooth/bluetooth_socket_chromeos.h" | 30 #include "device/bluetooth/bluetooth_socket_chromeos.h" |
| 30 #include "device/bluetooth/bluetooth_socket_thread.h" | 31 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 31 #include "device/bluetooth/bluetooth_uuid.h" | 32 #include "device/bluetooth/bluetooth_uuid.h" |
| 32 #include "third_party/cros_system_api/dbus/service_constants.h" | 33 #include "third_party/cros_system_api/dbus/service_constants.h" |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 uuid, | 299 uuid, |
| 299 options, | 300 options, |
| 300 base::Bind(callback, socket), | 301 base::Bind(callback, socket), |
| 301 error_callback); | 302 error_callback); |
| 302 } | 303 } |
| 303 | 304 |
| 304 void BluetoothAdapterChromeOS::RegisterAudioSink( | 305 void BluetoothAdapterChromeOS::RegisterAudioSink( |
| 305 const device::BluetoothAudioSink::Options& options, | 306 const device::BluetoothAudioSink::Options& options, |
| 306 const device::BluetoothAdapter::AcquiredCallback& callback, | 307 const device::BluetoothAdapter::AcquiredCallback& callback, |
| 307 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 308 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
| 308 // TODO(mcchou): Create and register a BluetoothAudioSink. Add the | 309 VLOG(1) << "Registering audio sink"; |
| 309 // newly-created audio sink as an observer of the adapter. | 310 BluetoothAudioSinkChromeOS* audio_sink = new BluetoothAudioSinkChromeOS(this); |
| 310 // Add OnRegisterAudioSink(AcquiredCallback& , BluetoothAudioSink*) and pass | 311 audio_sink->Register( |
| 311 // it as an argument to BluetoothAudioSinkChromeOS::Register. | 312 options, |
| 312 error_callback.Run(device::BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM); | 313 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, |
| 314 weak_ptr_factory_.GetWeakPtr(), callback, | |
| 315 scoped_refptr<device::BluetoothAudioSink>(audio_sink)), | |
| 316 error_callback); | |
| 313 } | 317 } |
| 314 | 318 |
| 315 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( | 319 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( |
| 316 BluetoothDevice::PairingDelegate* pairing_delegate) { | 320 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 317 DCHECK(IsPresent()); | 321 DCHECK(IsPresent()); |
| 318 // Before removing a pairing delegate make sure that there aren't any devices | 322 // Before removing a pairing delegate make sure that there aren't any devices |
| 319 // currently using it; if there are, clear the pairing context which will | 323 // currently using it; if there are, clear the pairing context which will |
| 320 // make any responses no-ops. | 324 // make any responses no-ops. |
| 321 for (DevicesMap::iterator iter = devices_.begin(); | 325 for (DevicesMap::iterator iter = devices_.begin(); |
| 322 iter != devices_.end(); ++iter) { | 326 iter != devices_.end(); ++iter) { |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 644 } | 648 } |
| 645 | 649 |
| 646 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | 650 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
| 647 const std::string& error_name, | 651 const std::string& error_name, |
| 648 const std::string& error_message) { | 652 const std::string& error_message) { |
| 649 DCHECK(IsPresent()); | 653 DCHECK(IsPresent()); |
| 650 LOG(WARNING) << ": Failed to make pairing agent default: " | 654 LOG(WARNING) << ": Failed to make pairing agent default: " |
| 651 << error_name << ": " << error_message; | 655 << error_name << ": " << error_message; |
| 652 } | 656 } |
| 653 | 657 |
| 658 void BluetoothAdapterChromeOS::OnRegisterAudioSink( | |
| 659 const device::BluetoothAdapter::AcquiredCallback& callback, | |
| 660 scoped_refptr<device::BluetoothAudioSink> audio_sink) { | |
| 661 DCHECK(audio_sink); | |
|
armansito
2015/01/27 04:38:05
DCHECK(audio_sink.get())?
Miao
2015/01/28 02:05:01
Done.
| |
| 662 callback.Run(audio_sink); | |
| 663 } | |
| 664 | |
| 654 BluetoothDeviceChromeOS* | 665 BluetoothDeviceChromeOS* |
| 655 BluetoothAdapterChromeOS::GetDeviceWithPath( | 666 BluetoothAdapterChromeOS::GetDeviceWithPath( |
| 656 const dbus::ObjectPath& object_path) { | 667 const dbus::ObjectPath& object_path) { |
| 657 if (!IsPresent()) | 668 if (!IsPresent()) |
| 658 return NULL; | 669 return NULL; |
| 659 | 670 |
| 660 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | 671 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
| 661 ++iter) { | 672 ++iter) { |
| 662 BluetoothDeviceChromeOS* device_chromeos = | 673 BluetoothDeviceChromeOS* device_chromeos = |
| 663 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 674 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 977 // The adapter is already discovering. | 988 // The adapter is already discovering. |
| 978 if (num_discovery_sessions_ > 0) { | 989 if (num_discovery_sessions_ > 0) { |
| 979 DCHECK(IsDiscovering()); | 990 DCHECK(IsDiscovering()); |
| 980 DCHECK(!discovery_request_pending_); | 991 DCHECK(!discovery_request_pending_); |
| 981 num_discovery_sessions_++; | 992 num_discovery_sessions_++; |
| 982 callback.Run(); | 993 callback.Run(); |
| 983 return; | 994 return; |
| 984 } | 995 } |
| 985 | 996 |
| 986 // There are no active discovery sessions. | 997 // There are no active discovery sessions. |
| 987 DCHECK(num_discovery_sessions_ == 0); | 998 DCHECK_EQ(num_discovery_sessions_, 0); |
|
armansito
2015/01/27 04:38:05
Is this refactor related to this CL?
Miao
2015/01/28 02:05:01
It is not related to this CL. Lint complained abou
| |
| 988 | 999 |
| 989 // This is the first request to start device discovery. | 1000 // This is the first request to start device discovery. |
| 990 discovery_request_pending_ = true; | 1001 discovery_request_pending_ = true; |
| 991 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 1002 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 992 StartDiscovery( | 1003 StartDiscovery( |
| 993 object_path_, | 1004 object_path_, |
| 994 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | 1005 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, |
| 995 weak_ptr_factory_.GetWeakPtr(), | 1006 weak_ptr_factory_.GetWeakPtr(), |
| 996 callback), | 1007 callback), |
| 997 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | 1008 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1027 // TODO(armansito): This should never happen once we have the | 1038 // TODO(armansito): This should never happen once we have the |
| 1028 // DiscoverySession API. Replace this case with an assert once it's | 1039 // DiscoverySession API. Replace this case with an assert once it's |
| 1029 // the deprecated methods have been removed. (See crbug.com/3445008). | 1040 // the deprecated methods have been removed. (See crbug.com/3445008). |
| 1030 VLOG(1) << "No active discovery sessions. Returning error."; | 1041 VLOG(1) << "No active discovery sessions. Returning error."; |
| 1031 error_callback.Run(); | 1042 error_callback.Run(); |
| 1032 return; | 1043 return; |
| 1033 } | 1044 } |
| 1034 | 1045 |
| 1035 // There is exactly one active discovery session. Request BlueZ to stop | 1046 // There is exactly one active discovery session. Request BlueZ to stop |
| 1036 // discovery. | 1047 // discovery. |
| 1037 DCHECK(num_discovery_sessions_ == 1); | 1048 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1038 discovery_request_pending_ = true; | 1049 discovery_request_pending_ = true; |
| 1039 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 1050 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 1040 StopDiscovery( | 1051 StopDiscovery( |
| 1041 object_path_, | 1052 object_path_, |
| 1042 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | 1053 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, |
| 1043 weak_ptr_factory_.GetWeakPtr(), | 1054 weak_ptr_factory_.GetWeakPtr(), |
| 1044 callback), | 1055 callback), |
| 1045 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | 1056 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, |
| 1046 weak_ptr_factory_.GetWeakPtr(), | 1057 weak_ptr_factory_.GetWeakPtr(), |
| 1047 error_callback)); | 1058 error_callback)); |
| 1048 } | 1059 } |
| 1049 | 1060 |
| 1050 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { | 1061 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
| 1051 DCHECK(IsPresent()); | 1062 DCHECK(IsPresent()); |
| 1052 // Report success on the original request and increment the count. | 1063 // Report success on the original request and increment the count. |
| 1053 VLOG(1) << __func__; | 1064 VLOG(1) << __func__; |
| 1054 DCHECK(discovery_request_pending_); | 1065 DCHECK(discovery_request_pending_); |
| 1055 DCHECK(num_discovery_sessions_ == 0); | 1066 DCHECK_EQ(num_discovery_sessions_, 0); |
| 1056 discovery_request_pending_ = false; | 1067 discovery_request_pending_ = false; |
| 1057 num_discovery_sessions_++; | 1068 num_discovery_sessions_++; |
| 1058 callback.Run(); | 1069 callback.Run(); |
| 1059 | 1070 |
| 1060 // Try to add a new discovery session for each queued request. | 1071 // Try to add a new discovery session for each queued request. |
| 1061 ProcessQueuedDiscoveryRequests(); | 1072 ProcessQueuedDiscoveryRequests(); |
| 1062 } | 1073 } |
| 1063 | 1074 |
| 1064 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | 1075 void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
| 1065 const base::Closure& callback, | 1076 const base::Closure& callback, |
| 1066 const ErrorCallback& error_callback, | 1077 const ErrorCallback& error_callback, |
| 1067 const std::string& error_name, | 1078 const std::string& error_name, |
| 1068 const std::string& error_message) { | 1079 const std::string& error_message) { |
| 1069 DCHECK(IsPresent()); | 1080 DCHECK(IsPresent()); |
| 1070 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | 1081 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
| 1071 << error_name << ": " << error_message; | 1082 << error_name << ": " << error_message; |
| 1072 | 1083 |
| 1073 // Failed to start discovery. This can only happen if the count is at 0. | 1084 // Failed to start discovery. This can only happen if the count is at 0. |
| 1074 DCHECK(num_discovery_sessions_ == 0); | 1085 DCHECK_EQ(num_discovery_sessions_, 0); |
| 1075 DCHECK(discovery_request_pending_); | 1086 DCHECK(discovery_request_pending_); |
| 1076 discovery_request_pending_ = false; | 1087 discovery_request_pending_ = false; |
| 1077 | 1088 |
| 1078 // Discovery request may fail if discovery was previously initiated by Chrome, | 1089 // Discovery request may fail if discovery was previously initiated by Chrome, |
| 1079 // but the session were invalidated due to the discovery state unexpectedly | 1090 // but the session were invalidated due to the discovery state unexpectedly |
| 1080 // changing to false and then back to true. In this case, report success. | 1091 // changing to false and then back to true. In this case, report success. |
| 1081 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { | 1092 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { |
| 1082 VLOG(1) << "Discovery previously initiated. Reporting success."; | 1093 VLOG(1) << "Discovery previously initiated. Reporting success."; |
| 1083 num_discovery_sessions_++; | 1094 num_discovery_sessions_++; |
| 1084 callback.Run(); | 1095 callback.Run(); |
| 1085 } else { | 1096 } else { |
| 1086 error_callback.Run(); | 1097 error_callback.Run(); |
| 1087 } | 1098 } |
| 1088 | 1099 |
| 1089 // Try to add a new discovery session for each queued request. | 1100 // Try to add a new discovery session for each queued request. |
| 1090 ProcessQueuedDiscoveryRequests(); | 1101 ProcessQueuedDiscoveryRequests(); |
| 1091 } | 1102 } |
| 1092 | 1103 |
| 1093 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | 1104 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
| 1094 DCHECK(IsPresent()); | 1105 DCHECK(IsPresent()); |
| 1095 // Report success on the original request and decrement the count. | 1106 // Report success on the original request and decrement the count. |
| 1096 VLOG(1) << __func__; | 1107 VLOG(1) << __func__; |
| 1097 DCHECK(discovery_request_pending_); | 1108 DCHECK(discovery_request_pending_); |
| 1098 DCHECK(num_discovery_sessions_ == 1); | 1109 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1099 discovery_request_pending_ = false; | 1110 discovery_request_pending_ = false; |
| 1100 num_discovery_sessions_--; | 1111 num_discovery_sessions_--; |
| 1101 callback.Run(); | 1112 callback.Run(); |
| 1102 | 1113 |
| 1103 // Try to add a new discovery session for each queued request. | 1114 // Try to add a new discovery session for each queued request. |
| 1104 ProcessQueuedDiscoveryRequests(); | 1115 ProcessQueuedDiscoveryRequests(); |
| 1105 } | 1116 } |
| 1106 | 1117 |
| 1107 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | 1118 void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
| 1108 const ErrorCallback& error_callback, | 1119 const ErrorCallback& error_callback, |
| 1109 const std::string& error_name, | 1120 const std::string& error_name, |
| 1110 const std::string& error_message) { | 1121 const std::string& error_message) { |
| 1111 DCHECK(IsPresent()); | 1122 DCHECK(IsPresent()); |
| 1112 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | 1123 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
| 1113 << error_name << ": " << error_message; | 1124 << error_name << ": " << error_message; |
| 1114 | 1125 |
| 1115 // Failed to stop discovery. This can only happen if the count is at 1. | 1126 // Failed to stop discovery. This can only happen if the count is at 1. |
| 1116 DCHECK(discovery_request_pending_); | 1127 DCHECK(discovery_request_pending_); |
| 1117 DCHECK(num_discovery_sessions_ == 1); | 1128 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1118 discovery_request_pending_ = false; | 1129 discovery_request_pending_ = false; |
| 1119 error_callback.Run(); | 1130 error_callback.Run(); |
| 1120 | 1131 |
| 1121 // Try to add a new discovery session for each queued request. | 1132 // Try to add a new discovery session for each queued request. |
| 1122 ProcessQueuedDiscoveryRequests(); | 1133 ProcessQueuedDiscoveryRequests(); |
| 1123 } | 1134 } |
| 1124 | 1135 |
| 1125 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | 1136 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { |
| 1126 while (!discovery_request_queue_.empty()) { | 1137 while (!discovery_request_queue_.empty()) { |
| 1127 VLOG(1) << "Process queued discovery request."; | 1138 VLOG(1) << "Process queued discovery request."; |
| 1128 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); | 1139 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); |
| 1129 discovery_request_queue_.pop(); | 1140 discovery_request_queue_.pop(); |
| 1130 AddDiscoverySession(callbacks.first, callbacks.second); | 1141 AddDiscoverySession(callbacks.first, callbacks.second); |
| 1131 | 1142 |
| 1132 // If the queued request resulted in a pending call, then let it | 1143 // If the queued request resulted in a pending call, then let it |
| 1133 // asynchonously process the remaining queued requests once the pending | 1144 // asynchonously process the remaining queued requests once the pending |
| 1134 // call returns. | 1145 // call returns. |
| 1135 if (discovery_request_pending_) | 1146 if (discovery_request_pending_) |
| 1136 return; | 1147 return; |
| 1137 } | 1148 } |
| 1138 } | 1149 } |
| 1139 | 1150 |
| 1140 } // namespace chromeos | 1151 } // namespace chromeos |
| OLD | NEW |