| 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/bluez/bluetooth_socket_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_socket_bluez.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 const char kSocketNotListening[] = "Socket is not listening."; | 53 const char kSocketNotListening[] = "Socket is not listening."; |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 namespace bluez { | 57 namespace bluez { |
| 58 | 58 |
| 59 // static | 59 // static |
| 60 scoped_refptr<BluetoothSocketBlueZ> BluetoothSocketBlueZ::CreateBluetoothSocket( | 60 scoped_refptr<BluetoothSocketBlueZ> BluetoothSocketBlueZ::CreateBluetoothSocket( |
| 61 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 61 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 62 scoped_refptr<BluetoothSocketThread> socket_thread) { | 62 scoped_refptr<BluetoothSocketThread> socket_thread) { |
| 63 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 63 DCHECK(ui_task_runner->RunsTasksInCurrentSequence()); |
| 64 | 64 |
| 65 return make_scoped_refptr( | 65 return make_scoped_refptr( |
| 66 new BluetoothSocketBlueZ(ui_task_runner, socket_thread)); | 66 new BluetoothSocketBlueZ(ui_task_runner, socket_thread)); |
| 67 } | 67 } |
| 68 | 68 |
| 69 BluetoothSocketBlueZ::AcceptRequest::AcceptRequest() {} | 69 BluetoothSocketBlueZ::AcceptRequest::AcceptRequest() {} |
| 70 | 70 |
| 71 BluetoothSocketBlueZ::AcceptRequest::~AcceptRequest() {} | 71 BluetoothSocketBlueZ::AcceptRequest::~AcceptRequest() {} |
| 72 | 72 |
| 73 BluetoothSocketBlueZ::ConnectionRequest::ConnectionRequest() | 73 BluetoothSocketBlueZ::ConnectionRequest::ConnectionRequest() |
| (...skipping 14 matching lines...) Expand all Loading... |
| 88 adapter_ = nullptr; | 88 adapter_ = nullptr; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void BluetoothSocketBlueZ::Connect( | 92 void BluetoothSocketBlueZ::Connect( |
| 93 const BluetoothDeviceBlueZ* device, | 93 const BluetoothDeviceBlueZ* device, |
| 94 const BluetoothUUID& uuid, | 94 const BluetoothUUID& uuid, |
| 95 SecurityLevel security_level, | 95 SecurityLevel security_level, |
| 96 const base::Closure& success_callback, | 96 const base::Closure& success_callback, |
| 97 const ErrorCompletionCallback& error_callback) { | 97 const ErrorCompletionCallback& error_callback) { |
| 98 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 98 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 99 DCHECK(!profile_); | 99 DCHECK(!profile_); |
| 100 | 100 |
| 101 if (!uuid.IsValid()) { | 101 if (!uuid.IsValid()) { |
| 102 error_callback.Run(kInvalidUUID); | 102 error_callback.Run(kInvalidUUID); |
| 103 return; | 103 return; |
| 104 } | 104 } |
| 105 | 105 |
| 106 device_address_ = device->GetAddress(); | 106 device_address_ = device->GetAddress(); |
| 107 device_path_ = device->object_path(); | 107 device_path_ = device->object_path(); |
| 108 uuid_ = uuid; | 108 uuid_ = uuid; |
| 109 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); | 109 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); |
| 110 if (security_level == SECURITY_LEVEL_LOW) | 110 if (security_level == SECURITY_LEVEL_LOW) |
| 111 options_->require_authentication.reset(new bool(false)); | 111 options_->require_authentication.reset(new bool(false)); |
| 112 | 112 |
| 113 adapter_ = device->adapter(); | 113 adapter_ = device->adapter(); |
| 114 | 114 |
| 115 RegisterProfile(device->adapter(), success_callback, error_callback); | 115 RegisterProfile(device->adapter(), success_callback, error_callback); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void BluetoothSocketBlueZ::Listen( | 118 void BluetoothSocketBlueZ::Listen( |
| 119 scoped_refptr<BluetoothAdapter> adapter, | 119 scoped_refptr<BluetoothAdapter> adapter, |
| 120 SocketType socket_type, | 120 SocketType socket_type, |
| 121 const BluetoothUUID& uuid, | 121 const BluetoothUUID& uuid, |
| 122 const BluetoothAdapter::ServiceOptions& service_options, | 122 const BluetoothAdapter::ServiceOptions& service_options, |
| 123 const base::Closure& success_callback, | 123 const base::Closure& success_callback, |
| 124 const ErrorCompletionCallback& error_callback) { | 124 const ErrorCompletionCallback& error_callback) { |
| 125 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 125 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 126 DCHECK(!profile_); | 126 DCHECK(!profile_); |
| 127 | 127 |
| 128 if (!uuid.IsValid()) { | 128 if (!uuid.IsValid()) { |
| 129 error_callback.Run(kInvalidUUID); | 129 error_callback.Run(kInvalidUUID); |
| 130 return; | 130 return; |
| 131 } | 131 } |
| 132 | 132 |
| 133 adapter_ = adapter; | 133 adapter_ = adapter; |
| 134 adapter_->AddObserver(this); | 134 adapter_->AddObserver(this); |
| 135 | 135 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 149 break; | 149 break; |
| 150 default: | 150 default: |
| 151 NOTREACHED(); | 151 NOTREACHED(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 RegisterProfile(static_cast<BluetoothAdapterBlueZ*>(adapter.get()), | 154 RegisterProfile(static_cast<BluetoothAdapterBlueZ*>(adapter.get()), |
| 155 success_callback, error_callback); | 155 success_callback, error_callback); |
| 156 } | 156 } |
| 157 | 157 |
| 158 void BluetoothSocketBlueZ::Close() { | 158 void BluetoothSocketBlueZ::Close() { |
| 159 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 159 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 160 | 160 |
| 161 if (profile_) | 161 if (profile_) |
| 162 UnregisterProfile(); | 162 UnregisterProfile(); |
| 163 | 163 |
| 164 // In the case below, where an asynchronous task gets posted on the socket | 164 // In the case below, where an asynchronous task gets posted on the socket |
| 165 // thread in BluetoothSocketNet::Close, a reference will be held to this | 165 // thread in BluetoothSocketNet::Close, a reference will be held to this |
| 166 // socket by the callback. This may cause the BluetoothAdapter to outlive | 166 // socket by the callback. This may cause the BluetoothAdapter to outlive |
| 167 // BluezDBusManager during shutdown if that callback executes too late. | 167 // BluezDBusManager during shutdown if that callback executes too late. |
| 168 if (adapter_.get()) { | 168 if (adapter_.get()) { |
| 169 adapter_->RemoveObserver(this); | 169 adapter_->RemoveObserver(this); |
| 170 adapter_ = nullptr; | 170 adapter_ = nullptr; |
| 171 } | 171 } |
| 172 | 172 |
| 173 if (!device_path_.value().empty()) { | 173 if (!device_path_.value().empty()) { |
| 174 BluetoothSocketNet::Close(); | 174 BluetoothSocketNet::Close(); |
| 175 } else { | 175 } else { |
| 176 DoCloseListening(); | 176 DoCloseListening(); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 void BluetoothSocketBlueZ::Disconnect(const base::Closure& callback) { | 180 void BluetoothSocketBlueZ::Disconnect(const base::Closure& callback) { |
| 181 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 181 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 182 | 182 |
| 183 if (profile_) | 183 if (profile_) |
| 184 UnregisterProfile(); | 184 UnregisterProfile(); |
| 185 | 185 |
| 186 if (!device_path_.value().empty()) { | 186 if (!device_path_.value().empty()) { |
| 187 BluetoothSocketNet::Disconnect(callback); | 187 BluetoothSocketNet::Disconnect(callback); |
| 188 } else { | 188 } else { |
| 189 DoCloseListening(); | 189 DoCloseListening(); |
| 190 callback.Run(); | 190 callback.Run(); |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 | 193 |
| 194 void BluetoothSocketBlueZ::Accept( | 194 void BluetoothSocketBlueZ::Accept( |
| 195 const AcceptCompletionCallback& success_callback, | 195 const AcceptCompletionCallback& success_callback, |
| 196 const ErrorCompletionCallback& error_callback) { | 196 const ErrorCompletionCallback& error_callback) { |
| 197 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 197 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 198 | 198 |
| 199 if (!device_path_.value().empty()) { | 199 if (!device_path_.value().empty()) { |
| 200 error_callback.Run(kSocketNotListening); | 200 error_callback.Run(kSocketNotListening); |
| 201 return; | 201 return; |
| 202 } | 202 } |
| 203 | 203 |
| 204 // Only one pending accept at a time | 204 // Only one pending accept at a time |
| 205 if (accept_request_.get()) { | 205 if (accept_request_.get()) { |
| 206 error_callback.Run(net::ErrorToString(net::ERR_IO_PENDING)); | 206 error_callback.Run(net::ErrorToString(net::ERR_IO_PENDING)); |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 accept_request_.reset(new AcceptRequest); | 210 accept_request_.reset(new AcceptRequest); |
| 211 accept_request_->success_callback = success_callback; | 211 accept_request_->success_callback = success_callback; |
| 212 accept_request_->error_callback = error_callback; | 212 accept_request_->error_callback = error_callback; |
| 213 | 213 |
| 214 if (connection_request_queue_.size() >= 1) { | 214 if (connection_request_queue_.size() >= 1) { |
| 215 AcceptConnectionRequest(); | 215 AcceptConnectionRequest(); |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 | 218 |
| 219 void BluetoothSocketBlueZ::RegisterProfile( | 219 void BluetoothSocketBlueZ::RegisterProfile( |
| 220 BluetoothAdapterBlueZ* adapter, | 220 BluetoothAdapterBlueZ* adapter, |
| 221 const base::Closure& success_callback, | 221 const base::Closure& success_callback, |
| 222 const ErrorCompletionCallback& error_callback) { | 222 const ErrorCompletionCallback& error_callback) { |
| 223 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 223 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 224 DCHECK(!profile_); | 224 DCHECK(!profile_); |
| 225 DCHECK(adapter); | 225 DCHECK(adapter); |
| 226 | 226 |
| 227 // If the adapter is not present, this is a listening socket and the | 227 // If the adapter is not present, this is a listening socket and the |
| 228 // adapter isn't running yet. Report success and carry on; | 228 // adapter isn't running yet. Report success and carry on; |
| 229 // the profile will be registered when the daemon becomes available. | 229 // the profile will be registered when the daemon becomes available. |
| 230 if (!adapter->IsPresent()) { | 230 if (!adapter->IsPresent()) { |
| 231 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 231 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
| 232 << ": Delaying profile registration."; | 232 << ": Delaying profile registration."; |
| 233 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, success_callback); | 233 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, success_callback); |
| 234 return; | 234 return; |
| 235 } | 235 } |
| 236 | 236 |
| 237 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 237 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
| 238 << ": Acquiring profile."; | 238 << ": Acquiring profile."; |
| 239 | 239 |
| 240 adapter->UseProfile(uuid_, device_path_, *options_, this, | 240 adapter->UseProfile(uuid_, device_path_, *options_, this, |
| 241 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfile, this, | 241 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfile, this, |
| 242 success_callback, error_callback), | 242 success_callback, error_callback), |
| 243 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfileError, | 243 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfileError, |
| 244 this, error_callback)); | 244 this, error_callback)); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void BluetoothSocketBlueZ::OnRegisterProfile( | 247 void BluetoothSocketBlueZ::OnRegisterProfile( |
| 248 const base::Closure& success_callback, | 248 const base::Closure& success_callback, |
| 249 const ErrorCompletionCallback& error_callback, | 249 const ErrorCompletionCallback& error_callback, |
| 250 BluetoothAdapterProfileBlueZ* profile) { | 250 BluetoothAdapterProfileBlueZ* profile) { |
| 251 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 251 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 252 DCHECK(!profile_); | 252 DCHECK(!profile_); |
| 253 | 253 |
| 254 profile_ = profile; | 254 profile_ = profile; |
| 255 | 255 |
| 256 if (device_path_.value().empty()) { | 256 if (device_path_.value().empty()) { |
| 257 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; | 257 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; |
| 258 success_callback.Run(); | 258 success_callback.Run(); |
| 259 return; | 259 return; |
| 260 } | 260 } |
| 261 | 261 |
| 262 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " | 262 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " |
| 263 << device_path_.value(); | 263 << device_path_.value(); |
| 264 | 264 |
| 265 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | 265 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( |
| 266 device_path_, uuid_.canonical_value(), | 266 device_path_, uuid_.canonical_value(), |
| 267 base::Bind(&BluetoothSocketBlueZ::OnConnectProfile, this, | 267 base::Bind(&BluetoothSocketBlueZ::OnConnectProfile, this, |
| 268 success_callback), | 268 success_callback), |
| 269 base::Bind(&BluetoothSocketBlueZ::OnConnectProfileError, this, | 269 base::Bind(&BluetoothSocketBlueZ::OnConnectProfileError, this, |
| 270 error_callback)); | 270 error_callback)); |
| 271 } | 271 } |
| 272 | 272 |
| 273 void BluetoothSocketBlueZ::OnRegisterProfileError( | 273 void BluetoothSocketBlueZ::OnRegisterProfileError( |
| 274 const ErrorCompletionCallback& error_callback, | 274 const ErrorCompletionCallback& error_callback, |
| 275 const std::string& error_message) { | 275 const std::string& error_message) { |
| 276 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 276 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 277 | 277 |
| 278 LOG(WARNING) << uuid_.canonical_value() | 278 LOG(WARNING) << uuid_.canonical_value() |
| 279 << ": Failed to register profile: " << error_message; | 279 << ": Failed to register profile: " << error_message; |
| 280 error_callback.Run(error_message); | 280 error_callback.Run(error_message); |
| 281 } | 281 } |
| 282 | 282 |
| 283 void BluetoothSocketBlueZ::OnConnectProfile( | 283 void BluetoothSocketBlueZ::OnConnectProfile( |
| 284 const base::Closure& success_callback) { | 284 const base::Closure& success_callback) { |
| 285 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 285 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 286 DCHECK(profile_); | 286 DCHECK(profile_); |
| 287 | 287 |
| 288 VLOG(1) << profile_->object_path().value() << ": Profile connected."; | 288 VLOG(1) << profile_->object_path().value() << ": Profile connected."; |
| 289 UnregisterProfile(); | 289 UnregisterProfile(); |
| 290 success_callback.Run(); | 290 success_callback.Run(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 void BluetoothSocketBlueZ::OnConnectProfileError( | 293 void BluetoothSocketBlueZ::OnConnectProfileError( |
| 294 const ErrorCompletionCallback& error_callback, | 294 const ErrorCompletionCallback& error_callback, |
| 295 const std::string& error_name, | 295 const std::string& error_name, |
| 296 const std::string& error_message) { | 296 const std::string& error_message) { |
| 297 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 297 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 298 DCHECK(profile_); | 298 DCHECK(profile_); |
| 299 | 299 |
| 300 LOG(WARNING) << profile_->object_path().value() | 300 LOG(WARNING) << profile_->object_path().value() |
| 301 << ": Failed to connect profile: " << error_name << ": " | 301 << ": Failed to connect profile: " << error_name << ": " |
| 302 << error_message; | 302 << error_message; |
| 303 UnregisterProfile(); | 303 UnregisterProfile(); |
| 304 error_callback.Run(error_message); | 304 error_callback.Run(error_message); |
| 305 } | 305 } |
| 306 | 306 |
| 307 void BluetoothSocketBlueZ::AdapterPresentChanged(BluetoothAdapter* adapter, | 307 void BluetoothSocketBlueZ::AdapterPresentChanged(BluetoothAdapter* adapter, |
| 308 bool present) { | 308 bool present) { |
| 309 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 309 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 310 | 310 |
| 311 if (!present) { | 311 if (!present) { |
| 312 // Adapter removed, we can't use the profile anymore. | 312 // Adapter removed, we can't use the profile anymore. |
| 313 UnregisterProfile(); | 313 UnregisterProfile(); |
| 314 return; | 314 return; |
| 315 } | 315 } |
| 316 | 316 |
| 317 DCHECK(!profile_); | 317 DCHECK(!profile_); |
| 318 | 318 |
| 319 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 319 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
| 320 << ": Acquiring profile."; | 320 << ": Acquiring profile."; |
| 321 | 321 |
| 322 static_cast<BluetoothAdapterBlueZ*>(adapter)->UseProfile( | 322 static_cast<BluetoothAdapterBlueZ*>(adapter)->UseProfile( |
| 323 uuid_, device_path_, *options_, this, | 323 uuid_, device_path_, *options_, this, |
| 324 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfile, this), | 324 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfile, this), |
| 325 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfileError, this)); | 325 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfileError, this)); |
| 326 } | 326 } |
| 327 | 327 |
| 328 void BluetoothSocketBlueZ::OnInternalRegisterProfile( | 328 void BluetoothSocketBlueZ::OnInternalRegisterProfile( |
| 329 BluetoothAdapterProfileBlueZ* profile) { | 329 BluetoothAdapterProfileBlueZ* profile) { |
| 330 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 330 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 331 DCHECK(!profile_); | 331 DCHECK(!profile_); |
| 332 | 332 |
| 333 profile_ = profile; | 333 profile_ = profile; |
| 334 | 334 |
| 335 VLOG(1) << uuid_.canonical_value() << ": Profile re-registered"; | 335 VLOG(1) << uuid_.canonical_value() << ": Profile re-registered"; |
| 336 } | 336 } |
| 337 | 337 |
| 338 void BluetoothSocketBlueZ::OnInternalRegisterProfileError( | 338 void BluetoothSocketBlueZ::OnInternalRegisterProfileError( |
| 339 const std::string& error_message) { | 339 const std::string& error_message) { |
| 340 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 340 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 341 | 341 |
| 342 LOG(WARNING) << "Failed to re-register profile: " << error_message; | 342 LOG(WARNING) << "Failed to re-register profile: " << error_message; |
| 343 } | 343 } |
| 344 | 344 |
| 345 void BluetoothSocketBlueZ::Released() { | 345 void BluetoothSocketBlueZ::Released() { |
| 346 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 346 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 347 DCHECK(profile_); | 347 DCHECK(profile_); |
| 348 | 348 |
| 349 VLOG(1) << profile_->object_path().value() << ": Release"; | 349 VLOG(1) << profile_->object_path().value() << ": Release"; |
| 350 } | 350 } |
| 351 | 351 |
| 352 void BluetoothSocketBlueZ::NewConnection( | 352 void BluetoothSocketBlueZ::NewConnection( |
| 353 const dbus::ObjectPath& device_path, | 353 const dbus::ObjectPath& device_path, |
| 354 base::ScopedFD fd, | 354 base::ScopedFD fd, |
| 355 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 355 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
| 356 const ConfirmationCallback& callback) { | 356 const ConfirmationCallback& callback) { |
| 357 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 357 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 358 | 358 |
| 359 VLOG(1) << uuid_.canonical_value() | 359 VLOG(1) << uuid_.canonical_value() |
| 360 << ": New connection from device: " << device_path.value(); | 360 << ": New connection from device: " << device_path.value(); |
| 361 | 361 |
| 362 if (!device_path_.value().empty()) { | 362 if (!device_path_.value().empty()) { |
| 363 DCHECK(device_path_ == device_path); | 363 DCHECK(device_path_ == device_path); |
| 364 | 364 |
| 365 socket_thread()->task_runner()->PostTask( | 365 socket_thread()->task_runner()->PostTask( |
| 366 FROM_HERE, | 366 FROM_HERE, |
| 367 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, this, device_path_, | 367 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, this, device_path_, |
| 368 base::Passed(&fd), options, callback)); | 368 base::Passed(&fd), options, callback)); |
| 369 } else { | 369 } else { |
| 370 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); | 370 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); |
| 371 request->device_path = device_path; | 371 request->device_path = device_path; |
| 372 request->fd = std::move(fd); | 372 request->fd = std::move(fd); |
| 373 request->options = options; | 373 request->options = options; |
| 374 request->callback = callback; | 374 request->callback = callback; |
| 375 | 375 |
| 376 connection_request_queue_.push(request); | 376 connection_request_queue_.push(request); |
| 377 VLOG(1) << uuid_.canonical_value() << ": Connection is now pending."; | 377 VLOG(1) << uuid_.canonical_value() << ": Connection is now pending."; |
| 378 if (accept_request_) { | 378 if (accept_request_) { |
| 379 AcceptConnectionRequest(); | 379 AcceptConnectionRequest(); |
| 380 } | 380 } |
| 381 } | 381 } |
| 382 } | 382 } |
| 383 | 383 |
| 384 void BluetoothSocketBlueZ::RequestDisconnection( | 384 void BluetoothSocketBlueZ::RequestDisconnection( |
| 385 const dbus::ObjectPath& device_path, | 385 const dbus::ObjectPath& device_path, |
| 386 const ConfirmationCallback& callback) { | 386 const ConfirmationCallback& callback) { |
| 387 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 387 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 388 DCHECK(profile_); | 388 DCHECK(profile_); |
| 389 | 389 |
| 390 VLOG(1) << profile_->object_path().value() << ": Request disconnection"; | 390 VLOG(1) << profile_->object_path().value() << ": Request disconnection"; |
| 391 callback.Run(SUCCESS); | 391 callback.Run(SUCCESS); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void BluetoothSocketBlueZ::Cancel() { | 394 void BluetoothSocketBlueZ::Cancel() { |
| 395 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 395 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 396 DCHECK(profile_); | 396 DCHECK(profile_); |
| 397 | 397 |
| 398 VLOG(1) << profile_->object_path().value() << ": Cancel"; | 398 VLOG(1) << profile_->object_path().value() << ": Cancel"; |
| 399 | 399 |
| 400 if (connection_request_queue_.empty()) | 400 if (connection_request_queue_.empty()) |
| 401 return; | 401 return; |
| 402 | 402 |
| 403 // If the front request is being accepted mark it as cancelled, otherwise | 403 // If the front request is being accepted mark it as cancelled, otherwise |
| 404 // just pop it from the queue. | 404 // just pop it from the queue. |
| 405 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 405 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 406 if (!request->accepting) { | 406 if (!request->accepting) { |
| 407 request->cancelled = true; | 407 request->cancelled = true; |
| 408 } else { | 408 } else { |
| 409 connection_request_queue_.pop(); | 409 connection_request_queue_.pop(); |
| 410 } | 410 } |
| 411 } | 411 } |
| 412 | 412 |
| 413 void BluetoothSocketBlueZ::AcceptConnectionRequest() { | 413 void BluetoothSocketBlueZ::AcceptConnectionRequest() { |
| 414 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 414 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 415 DCHECK(accept_request_.get()); | 415 DCHECK(accept_request_.get()); |
| 416 DCHECK(connection_request_queue_.size() >= 1); | 416 DCHECK(connection_request_queue_.size() >= 1); |
| 417 DCHECK(profile_); | 417 DCHECK(profile_); |
| 418 | 418 |
| 419 VLOG(1) << profile_->object_path().value() | 419 VLOG(1) << profile_->object_path().value() |
| 420 << ": Accepting pending connection."; | 420 << ": Accepting pending connection."; |
| 421 | 421 |
| 422 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 422 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 423 request->accepting = true; | 423 request->accepting = true; |
| 424 | 424 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 442 request->options, | 442 request->options, |
| 443 base::Bind(&BluetoothSocketBlueZ::OnNewConnection, this, | 443 base::Bind(&BluetoothSocketBlueZ::OnNewConnection, this, |
| 444 client_socket, request->callback))); | 444 client_socket, request->callback))); |
| 445 } | 445 } |
| 446 | 446 |
| 447 void BluetoothSocketBlueZ::DoNewConnection( | 447 void BluetoothSocketBlueZ::DoNewConnection( |
| 448 const dbus::ObjectPath& device_path, | 448 const dbus::ObjectPath& device_path, |
| 449 base::ScopedFD fd, | 449 base::ScopedFD fd, |
| 450 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 450 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
| 451 const ConfirmationCallback& callback) { | 451 const ConfirmationCallback& callback) { |
| 452 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 452 DCHECK(socket_thread()->task_runner()->RunsTasksInCurrentSequence()); |
| 453 base::ThreadRestrictions::AssertIOAllowed(); | 453 base::ThreadRestrictions::AssertIOAllowed(); |
| 454 | 454 |
| 455 if (!fd.is_valid()) { | 455 if (!fd.is_valid()) { |
| 456 LOG(WARNING) << uuid_.canonical_value() << " :" << fd.get() | 456 LOG(WARNING) << uuid_.canonical_value() << " :" << fd.get() |
| 457 << ": Invalid file descriptor received from Bluetooth Daemon."; | 457 << ": Invalid file descriptor received from Bluetooth Daemon."; |
| 458 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); | 458 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
| 459 return; | 459 return; |
| 460 } | 460 } |
| 461 | 461 |
| 462 if (tcp_socket()) { | 462 if (tcp_socket()) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 477 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); | 477 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
| 478 return; | 478 return; |
| 479 } | 479 } |
| 480 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); | 480 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
| 481 } | 481 } |
| 482 | 482 |
| 483 void BluetoothSocketBlueZ::OnNewConnection( | 483 void BluetoothSocketBlueZ::OnNewConnection( |
| 484 scoped_refptr<BluetoothSocket> socket, | 484 scoped_refptr<BluetoothSocket> socket, |
| 485 const ConfirmationCallback& callback, | 485 const ConfirmationCallback& callback, |
| 486 Status status) { | 486 Status status) { |
| 487 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 487 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 488 DCHECK(accept_request_.get()); | 488 DCHECK(accept_request_.get()); |
| 489 DCHECK(connection_request_queue_.size() >= 1); | 489 DCHECK(connection_request_queue_.size() >= 1); |
| 490 | 490 |
| 491 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 491 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 492 if (status == SUCCESS && !request->cancelled) { | 492 if (status == SUCCESS && !request->cancelled) { |
| 493 BluetoothDeviceBlueZ* device = | 493 BluetoothDeviceBlueZ* device = |
| 494 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) | 494 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
| 495 ->GetDeviceWithPath(request->device_path); | 495 ->GetDeviceWithPath(request->device_path); |
| 496 DCHECK(device); | 496 DCHECK(device); |
| 497 | 497 |
| 498 accept_request_->success_callback.Run(device, socket); | 498 accept_request_->success_callback.Run(device, socket); |
| 499 } else { | 499 } else { |
| 500 accept_request_->error_callback.Run(kAcceptFailed); | 500 accept_request_->error_callback.Run(kAcceptFailed); |
| 501 } | 501 } |
| 502 | 502 |
| 503 accept_request_.reset(nullptr); | 503 accept_request_.reset(nullptr); |
| 504 connection_request_queue_.pop(); | 504 connection_request_queue_.pop(); |
| 505 | 505 |
| 506 callback.Run(status); | 506 callback.Run(status); |
| 507 } | 507 } |
| 508 | 508 |
| 509 void BluetoothSocketBlueZ::DoCloseListening() { | 509 void BluetoothSocketBlueZ::DoCloseListening() { |
| 510 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 510 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 511 | 511 |
| 512 if (accept_request_) { | 512 if (accept_request_) { |
| 513 accept_request_->error_callback.Run( | 513 accept_request_->error_callback.Run( |
| 514 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); | 514 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); |
| 515 accept_request_.reset(nullptr); | 515 accept_request_.reset(nullptr); |
| 516 } | 516 } |
| 517 | 517 |
| 518 while (connection_request_queue_.size() > 0) { | 518 while (connection_request_queue_.size() > 0) { |
| 519 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 519 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 520 request->callback.Run(REJECTED); | 520 request->callback.Run(REJECTED); |
| 521 connection_request_queue_.pop(); | 521 connection_request_queue_.pop(); |
| 522 } | 522 } |
| 523 } | 523 } |
| 524 | 524 |
| 525 void BluetoothSocketBlueZ::UnregisterProfile() { | 525 void BluetoothSocketBlueZ::UnregisterProfile() { |
| 526 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 526 DCHECK(ui_task_runner()->RunsTasksInCurrentSequence()); |
| 527 DCHECK(profile_); | 527 DCHECK(profile_); |
| 528 | 528 |
| 529 VLOG(1) << profile_->object_path().value() << ": Release profile"; | 529 VLOG(1) << profile_->object_path().value() << ": Release profile"; |
| 530 | 530 |
| 531 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) | 531 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
| 532 ->ReleaseProfile(device_path_, profile_); | 532 ->ReleaseProfile(device_path_, profile_); |
| 533 profile_ = nullptr; | 533 profile_ = nullptr; |
| 534 } | 534 } |
| 535 | 535 |
| 536 } // namespace bluez | 536 } // namespace bluez |
| OLD | NEW |