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

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

Issue 933823003: bluetooth: Modify DCHECK to conditionals in several BluetoothAdapterChromeOS methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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"
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 const device::BluetoothAdapter::AcquiredCallback& callback, 309 const device::BluetoothAdapter::AcquiredCallback& callback,
310 const BluetoothAudioSink::ErrorCallback& error_callback) { 310 const BluetoothAudioSink::ErrorCallback& error_callback) {
311 VLOG(1) << "Registering audio sink"; 311 VLOG(1) << "Registering audio sink";
312 if (!this->IsPresent()) { 312 if (!this->IsPresent()) {
313 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); 313 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER);
314 return; 314 return;
315 } 315 }
316 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink( 316 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink(
317 new BluetoothAudioSinkChromeOS(this)); 317 new BluetoothAudioSinkChromeOS(this));
318 audio_sink->Register( 318 audio_sink->Register(
319 options, 319 options, base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink,
320 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, 320 weak_ptr_factory_.GetWeakPtr(), callback,
321 weak_ptr_factory_.GetWeakPtr(), callback, audio_sink), 321 error_callback, audio_sink),
322 error_callback); 322 error_callback);
323 } 323 }
324 324
325 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( 325 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal(
326 BluetoothDevice::PairingDelegate* pairing_delegate) { 326 BluetoothDevice::PairingDelegate* pairing_delegate) {
327 DCHECK(IsPresent());
328 // Before removing a pairing delegate make sure that there aren't any devices 327 // Before removing a pairing delegate make sure that there aren't any devices
329 // currently using it; if there are, clear the pairing context which will 328 // currently using it; if there are, clear the pairing context which will
330 // make any responses no-ops. 329 // make any responses no-ops.
331 for (DevicesMap::iterator iter = devices_.begin(); 330 for (DevicesMap::iterator iter = devices_.begin();
332 iter != devices_.end(); ++iter) { 331 iter != devices_.end(); ++iter) {
333 BluetoothDeviceChromeOS* device_chromeos = 332 BluetoothDeviceChromeOS* device_chromeos =
334 static_cast<BluetoothDeviceChromeOS*>(iter->second); 333 static_cast<BluetoothDeviceChromeOS*>(iter->second);
335 334
336 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); 335 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing();
337 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) 336 if (pairing && pairing->GetPairingDelegate() == pairing_delegate)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 473
475 // Properties structure can be removed, which triggers a change in the 474 // Properties structure can be removed, which triggers a change in the
476 // BluetoothDevice::IsConnectable() property, as does a change in the 475 // BluetoothDevice::IsConnectable() property, as does a change in the
477 // actual reconnect_mode property. 476 // actual reconnect_mode property.
478 if (!properties || 477 if (!properties ||
479 property_name == properties->reconnect_mode.name()) 478 property_name == properties->reconnect_mode.name())
480 NotifyDeviceChanged(device_chromeos); 479 NotifyDeviceChanged(device_chromeos);
481 } 480 }
482 481
483 void BluetoothAdapterChromeOS::Released() { 482 void BluetoothAdapterChromeOS::Released() {
484 DCHECK(IsPresent()); 483 VLOG(1) << "Release";
484 if (!IsPresent())
485 return;
485 DCHECK(agent_.get()); 486 DCHECK(agent_.get());
486 VLOG(1) << "Release";
487 487
488 // Called after we unregister the pairing agent, e.g. when changing I/O 488 // Called after we unregister the pairing agent, e.g. when changing I/O
489 // capabilities. Nothing much to be done right now. 489 // capabilities. Nothing much to be done right now.
490 } 490 }
491 491
492 void BluetoothAdapterChromeOS::RequestPinCode( 492 void BluetoothAdapterChromeOS::RequestPinCode(
493 const dbus::ObjectPath& device_path, 493 const dbus::ObjectPath& device_path,
494 const PinCodeCallback& callback) { 494 const PinCodeCallback& callback) {
495 DCHECK(IsPresent()); 495 DCHECK(IsPresent());
496 DCHECK(agent_.get()); 496 DCHECK(agent_.get());
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 callback.Run(REJECTED); 617 callback.Run(REJECTED);
618 } 618 }
619 619
620 void BluetoothAdapterChromeOS::Cancel() { 620 void BluetoothAdapterChromeOS::Cancel() {
621 DCHECK(IsPresent()); 621 DCHECK(IsPresent());
622 DCHECK(agent_.get()); 622 DCHECK(agent_.get());
623 VLOG(1) << "Cancel"; 623 VLOG(1) << "Cancel";
624 } 624 }
625 625
626 void BluetoothAdapterChromeOS::OnRegisterAgent() { 626 void BluetoothAdapterChromeOS::OnRegisterAgent() {
627 DCHECK(IsPresent());
628 VLOG(1) << "Pairing agent registered, requesting to be made default"; 627 VLOG(1) << "Pairing agent registered, requesting to be made default";
629 628
630 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> 629 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->
631 RequestDefaultAgent( 630 RequestDefaultAgent(
632 dbus::ObjectPath(kAgentPath), 631 dbus::ObjectPath(kAgentPath),
633 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, 632 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent,
634 weak_ptr_factory_.GetWeakPtr()), 633 weak_ptr_factory_.GetWeakPtr()),
635 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, 634 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError,
636 weak_ptr_factory_.GetWeakPtr())); 635 weak_ptr_factory_.GetWeakPtr()));
637 } 636 }
638 637
639 void BluetoothAdapterChromeOS::OnRegisterAgentError( 638 void BluetoothAdapterChromeOS::OnRegisterAgentError(
640 const std::string& error_name, 639 const std::string& error_name,
641 const std::string& error_message) { 640 const std::string& error_message) {
642 DCHECK(IsPresent());
643 // Our agent being already registered isn't an error. 641 // Our agent being already registered isn't an error.
644 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) 642 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists)
645 return; 643 return;
646 644
647 LOG(WARNING) << ": Failed to register pairing agent: " 645 LOG(WARNING) << ": Failed to register pairing agent: "
648 << error_name << ": " << error_message; 646 << error_name << ": " << error_message;
649 } 647 }
650 648
651 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { 649 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() {
652 DCHECK(IsPresent());
653 VLOG(1) << "Pairing agent now default"; 650 VLOG(1) << "Pairing agent now default";
654 } 651 }
655 652
656 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( 653 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError(
657 const std::string& error_name, 654 const std::string& error_name,
658 const std::string& error_message) { 655 const std::string& error_message) {
659 DCHECK(IsPresent());
660 LOG(WARNING) << ": Failed to make pairing agent default: " 656 LOG(WARNING) << ": Failed to make pairing agent default: "
661 << error_name << ": " << error_message; 657 << error_name << ": " << error_message;
662 } 658 }
663 659
664 void BluetoothAdapterChromeOS::OnRegisterAudioSink( 660 void BluetoothAdapterChromeOS::OnRegisterAudioSink(
665 const device::BluetoothAdapter::AcquiredCallback& callback, 661 const device::BluetoothAdapter::AcquiredCallback& callback,
662 const device::BluetoothAudioSink::ErrorCallback& error_callback,
666 scoped_refptr<BluetoothAudioSink> audio_sink) { 663 scoped_refptr<BluetoothAudioSink> audio_sink) {
664 if (!IsPresent()) {
665 VLOG(1) << "Failed to register audio sink, adapter not present";
666 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER);
667 return;
668 }
667 DCHECK(audio_sink.get()); 669 DCHECK(audio_sink.get());
668 callback.Run(audio_sink); 670 callback.Run(audio_sink);
669 } 671 }
670 672
671 BluetoothDeviceChromeOS* 673 BluetoothDeviceChromeOS*
672 BluetoothAdapterChromeOS::GetDeviceWithPath( 674 BluetoothAdapterChromeOS::GetDeviceWithPath(
673 const dbus::ObjectPath& object_path) { 675 const dbus::ObjectPath& object_path) {
674 if (!IsPresent()) 676 if (!IsPresent())
675 return NULL; 677 return NULL;
676 678
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 << ": Failed to register profile: " << error_name << ": " 1014 << ": Failed to register profile: " << error_name << ": "
1013 << error_message; 1015 << error_message;
1014 error_callback.Run(error_message); 1016 error_callback.Run(error_message);
1015 ReleaseProfile(uuid); 1017 ReleaseProfile(uuid);
1016 } 1018 }
1017 1019
1018 void BluetoothAdapterChromeOS::OnSetDiscoverable( 1020 void BluetoothAdapterChromeOS::OnSetDiscoverable(
1019 const base::Closure& callback, 1021 const base::Closure& callback,
1020 const ErrorCallback& error_callback, 1022 const ErrorCallback& error_callback,
1021 bool success) { 1023 bool success) {
1022 DCHECK(IsPresent()); 1024 if (!IsPresent()) {
1025 error_callback.Run();
1026 return;
1027 }
1028
1023 // Set the discoverable_timeout property to zero so the adapter remains 1029 // Set the discoverable_timeout property to zero so the adapter remains
1024 // discoverable forever. 1030 // discoverable forever.
1025 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> 1031 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
1026 GetProperties(object_path_)->discoverable_timeout.Set( 1032 GetProperties(object_path_)->discoverable_timeout.Set(
1027 0, 1033 0,
1028 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, 1034 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted,
1029 weak_ptr_factory_.GetWeakPtr(), 1035 weak_ptr_factory_.GetWeakPtr(),
1030 callback, 1036 callback,
1031 error_callback)); 1037 error_callback));
1032 } 1038 }
1033 1039
1034 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( 1040 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted(
1035 const base::Closure& callback, 1041 const base::Closure& callback,
1036 const ErrorCallback& error_callback, 1042 const ErrorCallback& error_callback,
1037 bool success) { 1043 bool success) {
1038 DCHECK(IsPresent()); 1044 if (IsPresent() && success)
1039 if (success)
1040 callback.Run(); 1045 callback.Run();
1041 else 1046 else
1042 error_callback.Run(); 1047 error_callback.Run();
1043 } 1048 }
1044 1049
1045 void BluetoothAdapterChromeOS::AddDiscoverySession( 1050 void BluetoothAdapterChromeOS::AddDiscoverySession(
1046 const base::Closure& callback, 1051 const base::Closure& callback,
1047 const ErrorCallback& error_callback) { 1052 const ErrorCallback& error_callback) {
1048 if (!IsPresent()) { 1053 if (!IsPresent()) {
1049 error_callback.Run(); 1054 error_callback.Run();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 callback), 1087 callback),
1083 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1088 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError,
1084 weak_ptr_factory_.GetWeakPtr(), 1089 weak_ptr_factory_.GetWeakPtr(),
1085 callback, 1090 callback,
1086 error_callback)); 1091 error_callback));
1087 } 1092 }
1088 1093
1089 void BluetoothAdapterChromeOS::RemoveDiscoverySession( 1094 void BluetoothAdapterChromeOS::RemoveDiscoverySession(
1090 const base::Closure& callback, 1095 const base::Closure& callback,
1091 const ErrorCallback& error_callback) { 1096 const ErrorCallback& error_callback) {
1092 DCHECK(IsPresent()); 1097 if (!IsPresent()) {
1098 error_callback.Run();
1099 return;
1100 }
1101
1093 VLOG(1) << __func__; 1102 VLOG(1) << __func__;
1094 // There are active sessions other than the one currently being removed. 1103 // There are active sessions other than the one currently being removed.
1095 if (num_discovery_sessions_ > 1) { 1104 if (num_discovery_sessions_ > 1) {
1096 DCHECK(IsDiscovering()); 1105 DCHECK(IsDiscovering());
1097 DCHECK(!discovery_request_pending_); 1106 DCHECK(!discovery_request_pending_);
1098 num_discovery_sessions_--; 1107 num_discovery_sessions_--;
1099 callback.Run(); 1108 callback.Run();
1100 return; 1109 return;
1101 } 1110 }
1102 1111
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 1154
1146 // Try to add a new discovery session for each queued request. 1155 // Try to add a new discovery session for each queued request.
1147 ProcessQueuedDiscoveryRequests(); 1156 ProcessQueuedDiscoveryRequests();
1148 } 1157 }
1149 1158
1150 void BluetoothAdapterChromeOS::OnStartDiscoveryError( 1159 void BluetoothAdapterChromeOS::OnStartDiscoveryError(
1151 const base::Closure& callback, 1160 const base::Closure& callback,
1152 const ErrorCallback& error_callback, 1161 const ErrorCallback& error_callback,
1153 const std::string& error_name, 1162 const std::string& error_name,
1154 const std::string& error_message) { 1163 const std::string& error_message) {
1155 DCHECK(IsPresent()); 1164 if (!IsPresent()) {
1165 error_callback.Run();
1166 return;
1167 }
1156 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " 1168 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: "
1157 << error_name << ": " << error_message; 1169 << error_name << ": " << error_message;
1158 1170
1159 // Failed to start discovery. This can only happen if the count is at 0. 1171 // Failed to start discovery. This can only happen if the count is at 0.
1160 DCHECK_EQ(num_discovery_sessions_, 0); 1172 DCHECK_EQ(num_discovery_sessions_, 0);
1161 DCHECK(discovery_request_pending_); 1173 DCHECK(discovery_request_pending_);
1162 discovery_request_pending_ = false; 1174 discovery_request_pending_ = false;
1163 1175
1164 // Discovery request may fail if discovery was previously initiated by Chrome, 1176 // Discovery request may fail if discovery was previously initiated by Chrome,
1165 // but the session were invalidated due to the discovery state unexpectedly 1177 // but the session were invalidated due to the discovery state unexpectedly
(...skipping 21 matching lines...) Expand all
1187 callback.Run(); 1199 callback.Run();
1188 1200
1189 // Try to add a new discovery session for each queued request. 1201 // Try to add a new discovery session for each queued request.
1190 ProcessQueuedDiscoveryRequests(); 1202 ProcessQueuedDiscoveryRequests();
1191 } 1203 }
1192 1204
1193 void BluetoothAdapterChromeOS::OnStopDiscoveryError( 1205 void BluetoothAdapterChromeOS::OnStopDiscoveryError(
1194 const ErrorCallback& error_callback, 1206 const ErrorCallback& error_callback,
1195 const std::string& error_name, 1207 const std::string& error_name,
1196 const std::string& error_message) { 1208 const std::string& error_message) {
1197 DCHECK(IsPresent());
1198 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " 1209 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: "
1199 << error_name << ": " << error_message; 1210 << error_name << ": " << error_message;
1211 if (IsPresent()) {
1212 // Failed to stop discovery. This can only happen if the count is at 1.
1213 DCHECK(discovery_request_pending_);
1214 DCHECK_EQ(num_discovery_sessions_, 1);
1215 }
1200 1216
1201 // Failed to stop discovery. This can only happen if the count is at 1. 1217 // Both when IsPresent and after Shutdown, run the error callbacks and empty
1202 DCHECK(discovery_request_pending_); 1218 // the queue.
armansito 2015/02/19 21:40:49 Sorry for the push back, can you add a unit test f
scheib 2015/02/20 00:41:17 Done. I've added 4 more unit tests to verify that
1203 DCHECK_EQ(num_discovery_sessions_, 1); 1219
1204 discovery_request_pending_ = false; 1220 discovery_request_pending_ = false;
1205 error_callback.Run(); 1221 error_callback.Run();
1206 1222
1207 // Try to add a new discovery session for each queued request. 1223 // Try to add a new discovery session for each queued request.
1208 ProcessQueuedDiscoveryRequests(); 1224 ProcessQueuedDiscoveryRequests();
1209 } 1225 }
1210 1226
1211 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { 1227 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() {
1212 while (!discovery_request_queue_.empty()) { 1228 while (!discovery_request_queue_.empty()) {
1213 VLOG(1) << "Process queued discovery request."; 1229 VLOG(1) << "Process queued discovery request.";
1214 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); 1230 DiscoveryCallbackPair callbacks = discovery_request_queue_.front();
1215 discovery_request_queue_.pop(); 1231 discovery_request_queue_.pop();
1216 AddDiscoverySession(callbacks.first, callbacks.second); 1232 AddDiscoverySession(callbacks.first, callbacks.second);
1217 1233
1218 // If the queued request resulted in a pending call, then let it 1234 // If the queued request resulted in a pending call, then let it
1219 // asynchonously process the remaining queued requests once the pending 1235 // asynchonously process the remaining queued requests once the pending
1220 // call returns. 1236 // call returns.
1221 if (discovery_request_pending_) 1237 if (discovery_request_pending_)
1222 return; 1238 return;
1223 } 1239 }
1224 } 1240 }
1225 1241
1226 } // namespace chromeos 1242 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698