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

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

Issue 876153002: device/bluetooth:Implement Register() for BluetoothAudioSinkChromeOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed unused include, moved default codec and fixed nits. Created 5 years, 10 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"
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
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 if (!this->IsPresent()) {
310 // Add OnRegisterAudioSink(AcquiredCallback& , BluetoothAudioSink*) and pass 311 error_callback.Run(device::BluetoothAudioSink::ERROR_INVALID_ADAPTER);
311 // it as an argument to BluetoothAudioSinkChromeOS::Register. 312 return;
312 error_callback.Run(device::BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM); 313 }
314 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink(
315 new BluetoothAudioSinkChromeOS(this));
316 audio_sink->Register(
317 options,
318 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink,
319 weak_ptr_factory_.GetWeakPtr(), callback, audio_sink),
320 error_callback);
313 } 321 }
314 322
315 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( 323 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal(
316 BluetoothDevice::PairingDelegate* pairing_delegate) { 324 BluetoothDevice::PairingDelegate* pairing_delegate) {
317 DCHECK(IsPresent()); 325 DCHECK(IsPresent());
318 // Before removing a pairing delegate make sure that there aren't any devices 326 // 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 327 // currently using it; if there are, clear the pairing context which will
320 // make any responses no-ops. 328 // make any responses no-ops.
321 for (DevicesMap::iterator iter = devices_.begin(); 329 for (DevicesMap::iterator iter = devices_.begin();
322 iter != devices_.end(); ++iter) { 330 iter != devices_.end(); ++iter) {
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 } 652 }
645 653
646 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( 654 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError(
647 const std::string& error_name, 655 const std::string& error_name,
648 const std::string& error_message) { 656 const std::string& error_message) {
649 DCHECK(IsPresent()); 657 DCHECK(IsPresent());
650 LOG(WARNING) << ": Failed to make pairing agent default: " 658 LOG(WARNING) << ": Failed to make pairing agent default: "
651 << error_name << ": " << error_message; 659 << error_name << ": " << error_message;
652 } 660 }
653 661
662 void BluetoothAdapterChromeOS::OnRegisterAudioSink(
663 const device::BluetoothAdapter::AcquiredCallback& callback,
664 scoped_refptr<device::BluetoothAudioSink> audio_sink) {
665 DCHECK(audio_sink.get());
666 callback.Run(audio_sink);
667 }
668
654 BluetoothDeviceChromeOS* 669 BluetoothDeviceChromeOS*
655 BluetoothAdapterChromeOS::GetDeviceWithPath( 670 BluetoothAdapterChromeOS::GetDeviceWithPath(
656 const dbus::ObjectPath& object_path) { 671 const dbus::ObjectPath& object_path) {
657 if (!IsPresent()) 672 if (!IsPresent())
658 return NULL; 673 return NULL;
659 674
660 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); 675 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
661 ++iter) { 676 ++iter) {
662 BluetoothDeviceChromeOS* device_chromeos = 677 BluetoothDeviceChromeOS* device_chromeos =
663 static_cast<BluetoothDeviceChromeOS*>(iter->second); 678 static_cast<BluetoothDeviceChromeOS*>(iter->second);
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 // The adapter is already discovering. 992 // The adapter is already discovering.
978 if (num_discovery_sessions_ > 0) { 993 if (num_discovery_sessions_ > 0) {
979 DCHECK(IsDiscovering()); 994 DCHECK(IsDiscovering());
980 DCHECK(!discovery_request_pending_); 995 DCHECK(!discovery_request_pending_);
981 num_discovery_sessions_++; 996 num_discovery_sessions_++;
982 callback.Run(); 997 callback.Run();
983 return; 998 return;
984 } 999 }
985 1000
986 // There are no active discovery sessions. 1001 // There are no active discovery sessions.
987 DCHECK(num_discovery_sessions_ == 0); 1002 DCHECK_EQ(num_discovery_sessions_, 0);
988 1003
989 // This is the first request to start device discovery. 1004 // This is the first request to start device discovery.
990 discovery_request_pending_ = true; 1005 discovery_request_pending_ = true;
991 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> 1006 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
992 StartDiscovery( 1007 StartDiscovery(
993 object_path_, 1008 object_path_,
994 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, 1009 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery,
995 weak_ptr_factory_.GetWeakPtr(), 1010 weak_ptr_factory_.GetWeakPtr(),
996 callback), 1011 callback),
997 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1012 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError,
(...skipping 29 matching lines...) Expand all
1027 // TODO(armansito): This should never happen once we have the 1042 // TODO(armansito): This should never happen once we have the
1028 // DiscoverySession API. Replace this case with an assert once it's 1043 // DiscoverySession API. Replace this case with an assert once it's
1029 // the deprecated methods have been removed. (See crbug.com/3445008). 1044 // the deprecated methods have been removed. (See crbug.com/3445008).
1030 VLOG(1) << "No active discovery sessions. Returning error."; 1045 VLOG(1) << "No active discovery sessions. Returning error.";
1031 error_callback.Run(); 1046 error_callback.Run();
1032 return; 1047 return;
1033 } 1048 }
1034 1049
1035 // There is exactly one active discovery session. Request BlueZ to stop 1050 // There is exactly one active discovery session. Request BlueZ to stop
1036 // discovery. 1051 // discovery.
1037 DCHECK(num_discovery_sessions_ == 1); 1052 DCHECK_EQ(num_discovery_sessions_, 1);
1038 discovery_request_pending_ = true; 1053 discovery_request_pending_ = true;
1039 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> 1054 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
1040 StopDiscovery( 1055 StopDiscovery(
1041 object_path_, 1056 object_path_,
1042 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, 1057 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery,
1043 weak_ptr_factory_.GetWeakPtr(), 1058 weak_ptr_factory_.GetWeakPtr(),
1044 callback), 1059 callback),
1045 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, 1060 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError,
1046 weak_ptr_factory_.GetWeakPtr(), 1061 weak_ptr_factory_.GetWeakPtr(),
1047 error_callback)); 1062 error_callback));
1048 } 1063 }
1049 1064
1050 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { 1065 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) {
1051 DCHECK(IsPresent()); 1066 DCHECK(IsPresent());
1052 // Report success on the original request and increment the count. 1067 // Report success on the original request and increment the count.
1053 VLOG(1) << __func__; 1068 VLOG(1) << __func__;
1054 DCHECK(discovery_request_pending_); 1069 DCHECK(discovery_request_pending_);
1055 DCHECK(num_discovery_sessions_ == 0); 1070 DCHECK_EQ(num_discovery_sessions_, 0);
1056 discovery_request_pending_ = false; 1071 discovery_request_pending_ = false;
1057 num_discovery_sessions_++; 1072 num_discovery_sessions_++;
1058 callback.Run(); 1073 callback.Run();
1059 1074
1060 // Try to add a new discovery session for each queued request. 1075 // Try to add a new discovery session for each queued request.
1061 ProcessQueuedDiscoveryRequests(); 1076 ProcessQueuedDiscoveryRequests();
1062 } 1077 }
1063 1078
1064 void BluetoothAdapterChromeOS::OnStartDiscoveryError( 1079 void BluetoothAdapterChromeOS::OnStartDiscoveryError(
1065 const base::Closure& callback, 1080 const base::Closure& callback,
1066 const ErrorCallback& error_callback, 1081 const ErrorCallback& error_callback,
1067 const std::string& error_name, 1082 const std::string& error_name,
1068 const std::string& error_message) { 1083 const std::string& error_message) {
1069 DCHECK(IsPresent()); 1084 DCHECK(IsPresent());
1070 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " 1085 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: "
1071 << error_name << ": " << error_message; 1086 << error_name << ": " << error_message;
1072 1087
1073 // Failed to start discovery. This can only happen if the count is at 0. 1088 // Failed to start discovery. This can only happen if the count is at 0.
1074 DCHECK(num_discovery_sessions_ == 0); 1089 DCHECK_EQ(num_discovery_sessions_, 0);
1075 DCHECK(discovery_request_pending_); 1090 DCHECK(discovery_request_pending_);
1076 discovery_request_pending_ = false; 1091 discovery_request_pending_ = false;
1077 1092
1078 // Discovery request may fail if discovery was previously initiated by Chrome, 1093 // Discovery request may fail if discovery was previously initiated by Chrome,
1079 // but the session were invalidated due to the discovery state unexpectedly 1094 // 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. 1095 // changing to false and then back to true. In this case, report success.
1081 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { 1096 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) {
1082 VLOG(1) << "Discovery previously initiated. Reporting success."; 1097 VLOG(1) << "Discovery previously initiated. Reporting success.";
1083 num_discovery_sessions_++; 1098 num_discovery_sessions_++;
1084 callback.Run(); 1099 callback.Run();
1085 } else { 1100 } else {
1086 error_callback.Run(); 1101 error_callback.Run();
1087 } 1102 }
1088 1103
1089 // Try to add a new discovery session for each queued request. 1104 // Try to add a new discovery session for each queued request.
1090 ProcessQueuedDiscoveryRequests(); 1105 ProcessQueuedDiscoveryRequests();
1091 } 1106 }
1092 1107
1093 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { 1108 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) {
1094 DCHECK(IsPresent()); 1109 DCHECK(IsPresent());
1095 // Report success on the original request and decrement the count. 1110 // Report success on the original request and decrement the count.
1096 VLOG(1) << __func__; 1111 VLOG(1) << __func__;
1097 DCHECK(discovery_request_pending_); 1112 DCHECK(discovery_request_pending_);
1098 DCHECK(num_discovery_sessions_ == 1); 1113 DCHECK_EQ(num_discovery_sessions_, 1);
1099 discovery_request_pending_ = false; 1114 discovery_request_pending_ = false;
1100 num_discovery_sessions_--; 1115 num_discovery_sessions_--;
1101 callback.Run(); 1116 callback.Run();
1102 1117
1103 // Try to add a new discovery session for each queued request. 1118 // Try to add a new discovery session for each queued request.
1104 ProcessQueuedDiscoveryRequests(); 1119 ProcessQueuedDiscoveryRequests();
1105 } 1120 }
1106 1121
1107 void BluetoothAdapterChromeOS::OnStopDiscoveryError( 1122 void BluetoothAdapterChromeOS::OnStopDiscoveryError(
1108 const ErrorCallback& error_callback, 1123 const ErrorCallback& error_callback,
1109 const std::string& error_name, 1124 const std::string& error_name,
1110 const std::string& error_message) { 1125 const std::string& error_message) {
1111 DCHECK(IsPresent()); 1126 DCHECK(IsPresent());
1112 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " 1127 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: "
1113 << error_name << ": " << error_message; 1128 << error_name << ": " << error_message;
1114 1129
1115 // Failed to stop discovery. This can only happen if the count is at 1. 1130 // Failed to stop discovery. This can only happen if the count is at 1.
1116 DCHECK(discovery_request_pending_); 1131 DCHECK(discovery_request_pending_);
1117 DCHECK(num_discovery_sessions_ == 1); 1132 DCHECK_EQ(num_discovery_sessions_, 1);
1118 discovery_request_pending_ = false; 1133 discovery_request_pending_ = false;
1119 error_callback.Run(); 1134 error_callback.Run();
1120 1135
1121 // Try to add a new discovery session for each queued request. 1136 // Try to add a new discovery session for each queued request.
1122 ProcessQueuedDiscoveryRequests(); 1137 ProcessQueuedDiscoveryRequests();
1123 } 1138 }
1124 1139
1125 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { 1140 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() {
1126 while (!discovery_request_queue_.empty()) { 1141 while (!discovery_request_queue_.empty()) {
1127 VLOG(1) << "Process queued discovery request."; 1142 VLOG(1) << "Process queued discovery request.";
1128 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); 1143 DiscoveryCallbackPair callbacks = discovery_request_queue_.front();
1129 discovery_request_queue_.pop(); 1144 discovery_request_queue_.pop();
1130 AddDiscoverySession(callbacks.first, callbacks.second); 1145 AddDiscoverySession(callbacks.first, callbacks.second);
1131 1146
1132 // If the queued request resulted in a pending call, then let it 1147 // If the queued request resulted in a pending call, then let it
1133 // asynchonously process the remaining queued requests once the pending 1148 // asynchonously process the remaining queued requests once the pending
1134 // call returns. 1149 // call returns.
1135 if (discovery_request_pending_) 1150 if (discovery_request_pending_)
1136 return; 1151 return;
1137 } 1152 }
1138 } 1153 }
1139 1154
1140 } // namespace chromeos 1155 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698