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" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 } | 65 } |
| 66 | 66 |
| 67 namespace chromeos { | 67 namespace chromeos { |
| 68 | 68 |
| 69 // static | 69 // static |
| 70 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { | 70 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { |
| 71 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); | 71 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); |
| 72 return adapter->weak_ptr_factory_.GetWeakPtr(); | 72 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void BluetoothAdapterChromeOS::Shutdown() { | |
| 76 if (is_shutdown_) | |
| 77 return; | |
| 78 is_shutdown_ = true; | |
|
armansito
2015/01/14 20:49:04
Here I would explicitly set present as false or at
scheib
2015/01/14 23:07:27
Done: reworked to call RemoveAdapter().
| |
| 79 DCHECK(DBusThreadManager::IsInitialized()) | |
| 80 << "Call BluetoothAdapterFactory::Shutdown() before " | |
| 81 "DBusThreadManager::Shutdown()."; | |
| 82 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); | |
| 83 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); | |
| 84 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); | |
| 85 | |
| 86 VLOG(1) << "Unregistering pairing agent"; | |
| 87 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent( | |
| 88 dbus::ObjectPath(kAgentPath), base::Bind(&base::DoNothing), | |
| 89 base::Bind(&OnUnregisterAgentError)); | |
| 90 | |
| 91 agent_.reset(); | |
| 92 STLDeleteValues(&devices_); | |
| 93 } | |
| 94 | |
| 75 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() | 95 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() |
| 76 : num_discovery_sessions_(0), | 96 : is_shutdown_(false), |
| 97 num_discovery_sessions_(0), | |
| 77 discovery_request_pending_(false), | 98 discovery_request_pending_(false), |
| 78 weak_ptr_factory_(this) { | 99 weak_ptr_factory_(this) { |
| 79 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 100 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 80 socket_thread_ = device::BluetoothSocketThread::Get(); | 101 socket_thread_ = device::BluetoothSocketThread::Get(); |
| 81 | 102 |
| 82 DBusThreadManager::Get()->GetBluetoothAdapterClient()->AddObserver(this); | 103 DBusThreadManager::Get()->GetBluetoothAdapterClient()->AddObserver(this); |
| 83 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | 104 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); |
| 84 DBusThreadManager::Get()->GetBluetoothInputClient()->AddObserver(this); | 105 DBusThreadManager::Get()->GetBluetoothInputClient()->AddObserver(this); |
| 85 | 106 |
| 86 // Register the pairing agent. | 107 // Register the pairing agent. |
| 87 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 108 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| 88 agent_.reset(BluetoothAgentServiceProvider::Create( | 109 agent_.reset(BluetoothAgentServiceProvider::Create( |
| 89 system_bus, dbus::ObjectPath(kAgentPath), this)); | 110 system_bus, dbus::ObjectPath(kAgentPath), this)); |
| 90 DCHECK(agent_.get()); | 111 DCHECK(agent_.get()); |
| 91 | 112 |
| 92 std::vector<dbus::ObjectPath> object_paths = | 113 std::vector<dbus::ObjectPath> object_paths = |
| 93 DBusThreadManager::Get()->GetBluetoothAdapterClient()->GetAdapters(); | 114 DBusThreadManager::Get()->GetBluetoothAdapterClient()->GetAdapters(); |
| 94 | 115 |
| 95 if (!object_paths.empty()) { | 116 if (!object_paths.empty()) { |
| 96 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; | 117 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; |
| 97 SetAdapter(object_paths[0]); | 118 SetAdapter(object_paths[0]); |
| 98 } | 119 } |
| 99 } | 120 } |
| 100 | 121 |
| 101 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { | 122 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { |
| 102 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); | 123 Shutdown(); |
| 103 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); | |
| 104 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); | |
| 105 | |
| 106 VLOG(1) << "Unregistering pairing agent"; | |
| 107 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | |
| 108 UnregisterAgent( | |
| 109 dbus::ObjectPath(kAgentPath), | |
| 110 base::Bind(&base::DoNothing), | |
| 111 base::Bind(&OnUnregisterAgentError)); | |
| 112 } | 124 } |
| 113 | 125 |
| 114 void BluetoothAdapterChromeOS::DeleteOnCorrectThread() const { | 126 void BluetoothAdapterChromeOS::DeleteOnCorrectThread() const { |
| 115 if (ui_task_runner_->RunsTasksOnCurrentThread() || | 127 if (ui_task_runner_->RunsTasksOnCurrentThread() || |
| 116 !ui_task_runner_->DeleteSoon(FROM_HERE, this)) | 128 !ui_task_runner_->DeleteSoon(FROM_HERE, this)) |
| 117 delete this; | 129 delete this; |
| 118 } | 130 } |
| 119 | 131 |
| 120 void BluetoothAdapterChromeOS::AddObserver( | 132 void BluetoothAdapterChromeOS::AddObserver( |
| 121 BluetoothAdapter::Observer* observer) { | 133 BluetoothAdapter::Observer* observer) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 weak_ptr_factory_.GetWeakPtr(), | 180 weak_ptr_factory_.GetWeakPtr(), |
| 169 callback, | 181 callback, |
| 170 error_callback)); | 182 error_callback)); |
| 171 } | 183 } |
| 172 | 184 |
| 173 bool BluetoothAdapterChromeOS::IsInitialized() const { | 185 bool BluetoothAdapterChromeOS::IsInitialized() const { |
| 174 return true; | 186 return true; |
| 175 } | 187 } |
| 176 | 188 |
| 177 bool BluetoothAdapterChromeOS::IsPresent() const { | 189 bool BluetoothAdapterChromeOS::IsPresent() const { |
| 178 return !object_path_.value().empty(); | 190 return !is_shutdown_ && !object_path_.value().empty(); |
| 179 } | 191 } |
| 180 | 192 |
| 181 bool BluetoothAdapterChromeOS::IsPowered() const { | 193 bool BluetoothAdapterChromeOS::IsPowered() const { |
| 182 if (!IsPresent()) | 194 if (!IsPresent()) |
| 183 return false; | 195 return false; |
| 184 | 196 |
| 185 BluetoothAdapterClient::Properties* properties = | 197 BluetoothAdapterClient::Properties* properties = |
| 186 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 198 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 187 GetProperties(object_path_); | 199 GetProperties(object_path_); |
| 188 | 200 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 GetProperties(object_path_); | 257 GetProperties(object_path_); |
| 246 | 258 |
| 247 return properties->discovering.value(); | 259 return properties->discovering.value(); |
| 248 } | 260 } |
| 249 | 261 |
| 250 void BluetoothAdapterChromeOS::CreateRfcommService( | 262 void BluetoothAdapterChromeOS::CreateRfcommService( |
| 251 const BluetoothUUID& uuid, | 263 const BluetoothUUID& uuid, |
| 252 const ServiceOptions& options, | 264 const ServiceOptions& options, |
| 253 const CreateServiceCallback& callback, | 265 const CreateServiceCallback& callback, |
| 254 const CreateServiceErrorCallback& error_callback) { | 266 const CreateServiceErrorCallback& error_callback) { |
| 255 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " | 267 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " |
|
armansito
2015/01/14 19:52:43
Add check for IsPresent in all public API function
scheib
2015/01/14 23:07:27
Added a check for dbus_is_shutdown_ added.
A test
| |
| 256 << uuid.canonical_value(); | 268 << uuid.canonical_value(); |
| 257 scoped_refptr<BluetoothSocketChromeOS> socket = | 269 scoped_refptr<BluetoothSocketChromeOS> socket = |
| 258 BluetoothSocketChromeOS::CreateBluetoothSocket( | 270 BluetoothSocketChromeOS::CreateBluetoothSocket( |
| 259 ui_task_runner_, socket_thread_); | 271 ui_task_runner_, socket_thread_); |
| 260 socket->Listen(this, | 272 socket->Listen(this, |
| 261 BluetoothSocketChromeOS::kRfcomm, | 273 BluetoothSocketChromeOS::kRfcomm, |
| 262 uuid, | 274 uuid, |
| 263 options, | 275 options, |
| 264 base::Bind(callback, socket), | 276 base::Bind(callback, socket), |
| 265 error_callback); | 277 error_callback); |
| (...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1068 | 1080 |
| 1069 // If the queued request resulted in a pending call, then let it | 1081 // If the queued request resulted in a pending call, then let it |
| 1070 // asynchonously process the remaining queued requests once the pending | 1082 // asynchonously process the remaining queued requests once the pending |
| 1071 // call returns. | 1083 // call returns. |
| 1072 if (discovery_request_pending_) | 1084 if (discovery_request_pending_) |
| 1073 return; | 1085 return; |
| 1074 } | 1086 } |
| 1075 } | 1087 } |
| 1076 | 1088 |
| 1077 } // namespace chromeos | 1089 } // namespace chromeos |
| OLD | NEW |