Index: device/bluetooth/bluetooth_adapter_chromeos.cc |
diff --git a/device/bluetooth/bluetooth_adapter_chromeos.cc b/device/bluetooth/bluetooth_adapter_chromeos.cc |
index 02e7abe26c627ed17a7e411a149610bc79effa5c..401fb5b74924fe662c62fa2bf0d777bc3e4befe0 100644 |
--- a/device/bluetooth/bluetooth_adapter_chromeos.cc |
+++ b/device/bluetooth/bluetooth_adapter_chromeos.cc |
@@ -72,8 +72,33 @@ base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { |
return adapter->weak_ptr_factory_.GetWeakPtr(); |
} |
+void BluetoothAdapterChromeOS::Shutdown() { |
+ if (dbus_is_shutdown_) |
+ return; |
+ DCHECK(DBusThreadManager::IsInitialized()) |
+ << "Call BluetoothAdapterFactory::Shutdown() before " |
+ "DBusThreadManager::Shutdown()."; |
+ |
+ if (IsPresent()) |
+ RemoveAdapter(); // Also deletes devices_. |
+ DCHECK(devices_.empty()); |
+ |
+ DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); |
+ DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); |
+ DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); |
+ |
+ VLOG(1) << "Unregistering pairing agent"; |
+ DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent( |
+ dbus::ObjectPath(kAgentPath), base::Bind(&base::DoNothing), |
+ base::Bind(&OnUnregisterAgentError)); |
+ |
+ agent_.reset(); |
+ dbus_is_shutdown_ = true; |
+} |
+ |
BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() |
- : num_discovery_sessions_(0), |
+ : dbus_is_shutdown_(false), |
+ num_discovery_sessions_(0), |
discovery_request_pending_(false), |
weak_ptr_factory_(this) { |
ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
@@ -99,16 +124,7 @@ BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() |
} |
BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { |
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); |
- DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); |
- DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); |
- |
- VLOG(1) << "Unregistering pairing agent"; |
- DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
- UnregisterAgent( |
- dbus::ObjectPath(kAgentPath), |
- base::Bind(&base::DoNothing), |
- base::Bind(&OnUnregisterAgentError)); |
+ Shutdown(); |
} |
void BluetoothAdapterChromeOS::DeleteOnCorrectThread() const { |
@@ -175,7 +191,7 @@ bool BluetoothAdapterChromeOS::IsInitialized() const { |
} |
bool BluetoothAdapterChromeOS::IsPresent() const { |
- return !object_path_.value().empty(); |
+ return !dbus_is_shutdown_ && !object_path_.value().empty(); |
} |
bool BluetoothAdapterChromeOS::IsPowered() const { |
@@ -252,6 +268,7 @@ void BluetoothAdapterChromeOS::CreateRfcommService( |
const ServiceOptions& options, |
const CreateServiceCallback& callback, |
const CreateServiceErrorCallback& error_callback) { |
+ DCHECK(!dbus_is_shutdown_); |
VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " |
<< uuid.canonical_value(); |
scoped_refptr<BluetoothSocketChromeOS> socket = |
@@ -270,6 +287,7 @@ void BluetoothAdapterChromeOS::CreateL2capService( |
const ServiceOptions& options, |
const CreateServiceCallback& callback, |
const CreateServiceErrorCallback& error_callback) { |
+ DCHECK(!dbus_is_shutdown_); |
VLOG(1) << object_path_.value() << ": Creating L2CAP service: " |
<< uuid.canonical_value(); |
scoped_refptr<BluetoothSocketChromeOS> socket = |
@@ -296,6 +314,7 @@ void BluetoothAdapterChromeOS::RegisterAudioSink( |
void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( |
BluetoothDevice::PairingDelegate* pairing_delegate) { |
+ DCHECK(IsPresent()); |
// Before removing a pairing delegate make sure that there aren't any devices |
// currently using it; if there are, clear the pairing context which will |
// make any responses no-ops. |
@@ -326,6 +345,7 @@ void BluetoothAdapterChromeOS::AdapterRemoved( |
void BluetoothAdapterChromeOS::AdapterPropertyChanged( |
const dbus::ObjectPath& object_path, |
const std::string& property_name) { |
+ DCHECK(IsPresent()); |
if (object_path != object_path_) |
return; |
@@ -343,6 +363,7 @@ void BluetoothAdapterChromeOS::AdapterPropertyChanged( |
void BluetoothAdapterChromeOS::DeviceAdded( |
const dbus::ObjectPath& object_path) { |
+ DCHECK(IsPresent()); |
BluetoothDeviceClient::Properties* properties = |
DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
GetProperties(object_path); |
@@ -364,6 +385,7 @@ void BluetoothAdapterChromeOS::DeviceAdded( |
void BluetoothAdapterChromeOS::DeviceRemoved( |
const dbus::ObjectPath& object_path) { |
+ DCHECK(IsPresent()); |
for (DevicesMap::iterator iter = devices_.begin(); |
iter != devices_.end(); ++iter) { |
BluetoothDeviceChromeOS* device_chromeos = |
@@ -382,6 +404,7 @@ void BluetoothAdapterChromeOS::DeviceRemoved( |
void BluetoothAdapterChromeOS::DevicePropertyChanged( |
const dbus::ObjectPath& object_path, |
const std::string& property_name) { |
+ DCHECK(IsPresent()); |
BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
if (!device_chromeos) |
return; |
@@ -430,6 +453,7 @@ void BluetoothAdapterChromeOS::DevicePropertyChanged( |
void BluetoothAdapterChromeOS::InputPropertyChanged( |
const dbus::ObjectPath& object_path, |
const std::string& property_name) { |
+ DCHECK(IsPresent()); |
BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
if (!device_chromeos) |
return; |
@@ -447,6 +471,7 @@ void BluetoothAdapterChromeOS::InputPropertyChanged( |
} |
void BluetoothAdapterChromeOS::Released() { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << "Release"; |
@@ -457,6 +482,7 @@ void BluetoothAdapterChromeOS::Released() { |
void BluetoothAdapterChromeOS::RequestPinCode( |
const dbus::ObjectPath& device_path, |
const PinCodeCallback& callback) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": RequestPinCode"; |
@@ -472,6 +498,7 @@ void BluetoothAdapterChromeOS::RequestPinCode( |
void BluetoothAdapterChromeOS::DisplayPinCode( |
const dbus::ObjectPath& device_path, |
const std::string& pincode) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; |
@@ -485,6 +512,7 @@ void BluetoothAdapterChromeOS::DisplayPinCode( |
void BluetoothAdapterChromeOS::RequestPasskey( |
const dbus::ObjectPath& device_path, |
const PasskeyCallback& callback) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": RequestPasskey"; |
@@ -501,6 +529,7 @@ void BluetoothAdapterChromeOS::DisplayPasskey( |
const dbus::ObjectPath& device_path, |
uint32 passkey, |
uint16 entered) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey |
<< " (" << entered << " entered)"; |
@@ -519,6 +548,7 @@ void BluetoothAdapterChromeOS::RequestConfirmation( |
const dbus::ObjectPath& device_path, |
uint32 passkey, |
const ConfirmationCallback& callback) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; |
@@ -534,6 +564,7 @@ void BluetoothAdapterChromeOS::RequestConfirmation( |
void BluetoothAdapterChromeOS::RequestAuthorization( |
const dbus::ObjectPath& device_path, |
const ConfirmationCallback& callback) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": RequestAuthorization"; |
@@ -550,6 +581,7 @@ void BluetoothAdapterChromeOS::AuthorizeService( |
const dbus::ObjectPath& device_path, |
const std::string& uuid, |
const ConfirmationCallback& callback) { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; |
@@ -576,11 +608,13 @@ void BluetoothAdapterChromeOS::AuthorizeService( |
} |
void BluetoothAdapterChromeOS::Cancel() { |
+ DCHECK(IsPresent()); |
DCHECK(agent_.get()); |
VLOG(1) << "Cancel"; |
} |
void BluetoothAdapterChromeOS::OnRegisterAgent() { |
+ DCHECK(IsPresent()); |
VLOG(1) << "Pairing agent registered, requesting to be made default"; |
DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
@@ -595,6 +629,7 @@ void BluetoothAdapterChromeOS::OnRegisterAgent() { |
void BluetoothAdapterChromeOS::OnRegisterAgentError( |
const std::string& error_name, |
const std::string& error_message) { |
+ DCHECK(IsPresent()); |
// Our agent being already registered isn't an error. |
if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) |
return; |
@@ -604,12 +639,14 @@ void BluetoothAdapterChromeOS::OnRegisterAgentError( |
} |
void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { |
+ DCHECK(IsPresent()); |
VLOG(1) << "Pairing agent now default"; |
} |
void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
const std::string& error_name, |
const std::string& error_message) { |
+ DCHECK(IsPresent()); |
LOG(WARNING) << ": Failed to make pairing agent default: " |
<< error_name << ": " << error_message; |
} |
@@ -617,8 +654,11 @@ void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
BluetoothDeviceChromeOS* |
BluetoothAdapterChromeOS::GetDeviceWithPath( |
const dbus::ObjectPath& object_path) { |
- for (DevicesMap::iterator iter = devices_.begin(); |
- iter != devices_.end(); ++iter) { |
+ if (!IsPresent()) |
+ return NULL; |
+ |
+ for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
+ ++iter) { |
BluetoothDeviceChromeOS* device_chromeos = |
static_cast<BluetoothDeviceChromeOS*>(iter->second); |
if (device_chromeos->object_path() == object_path) |
@@ -630,6 +670,7 @@ BluetoothAdapterChromeOS::GetDeviceWithPath( |
BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( |
const dbus::ObjectPath& object_path) { |
+ DCHECK(IsPresent()); |
BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
if (!device_chromeos) { |
LOG(WARNING) << "Pairing Agent request for unknown device: " |
@@ -652,6 +693,7 @@ BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( |
void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { |
DCHECK(!IsPresent()); |
+ DCHECK(!dbus_is_shutdown_); |
object_path_ = object_path; |
VLOG(1) << object_path_.value() << ": using adapter."; |
@@ -692,6 +734,7 @@ void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { |
} |
void BluetoothAdapterChromeOS::SetDefaultAdapterName() { |
+ DCHECK(IsPresent()); |
std::string board = base::SysInfo::GetLsbReleaseBoard(); |
std::string alias; |
if (board.substr(0, 6) == "stumpy") { |
@@ -890,6 +933,7 @@ void BluetoothAdapterChromeOS::OnSetDiscoverable( |
const base::Closure& callback, |
const ErrorCallback& error_callback, |
bool success) { |
+ DCHECK(IsPresent()); |
// Set the discoverable_timeout property to zero so the adapter remains |
// discoverable forever. |
DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
@@ -905,6 +949,7 @@ void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( |
const base::Closure& callback, |
const ErrorCallback& error_callback, |
bool success) { |
+ DCHECK(IsPresent()); |
if (success) |
callback.Run(); |
else |
@@ -914,6 +959,10 @@ void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( |
void BluetoothAdapterChromeOS::AddDiscoverySession( |
const base::Closure& callback, |
const ErrorCallback& error_callback) { |
+ if (!IsPresent()) { |
+ error_callback.Run(); |
+ return; |
+ } |
VLOG(1) << __func__; |
if (discovery_request_pending_) { |
// The pending request is either to stop a previous session or to start a |
@@ -954,6 +1003,7 @@ void BluetoothAdapterChromeOS::AddDiscoverySession( |
void BluetoothAdapterChromeOS::RemoveDiscoverySession( |
const base::Closure& callback, |
const ErrorCallback& error_callback) { |
+ DCHECK(IsPresent()); |
VLOG(1) << __func__; |
// There are active sessions other than the one currently being removed. |
if (num_discovery_sessions_ > 1) { |
@@ -998,6 +1048,7 @@ void BluetoothAdapterChromeOS::RemoveDiscoverySession( |
} |
void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
+ DCHECK(IsPresent()); |
// Report success on the original request and increment the count. |
VLOG(1) << __func__; |
DCHECK(discovery_request_pending_); |
@@ -1015,6 +1066,7 @@ void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
const ErrorCallback& error_callback, |
const std::string& error_name, |
const std::string& error_message) { |
+ DCHECK(IsPresent()); |
LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
<< error_name << ": " << error_message; |
@@ -1039,6 +1091,7 @@ void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
} |
void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
+ DCHECK(IsPresent()); |
// Report success on the original request and decrement the count. |
VLOG(1) << __func__; |
DCHECK(discovery_request_pending_); |
@@ -1055,6 +1108,7 @@ void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
const ErrorCallback& error_callback, |
const std::string& error_name, |
const std::string& error_message) { |
+ DCHECK(IsPresent()); |
LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
<< error_name << ": " << error_message; |