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> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
22 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
23 #include "base/task_runner_util.h" | 23 #include "base/task_runner_util.h" |
24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
26 #include "base/threading/worker_pool.h" | 26 #include "base/threading/worker_pool.h" |
27 #include "dbus/bus.h" | 27 #include "dbus/bus.h" |
28 #include "dbus/file_descriptor.h" | |
29 #include "dbus/object_path.h" | 28 #include "dbus/object_path.h" |
30 #include "device/bluetooth/bluetooth_adapter.h" | 29 #include "device/bluetooth/bluetooth_adapter.h" |
31 #include "device/bluetooth/bluetooth_device.h" | 30 #include "device/bluetooth/bluetooth_device.h" |
32 #include "device/bluetooth/bluetooth_socket.h" | 31 #include "device/bluetooth/bluetooth_socket.h" |
33 #include "device/bluetooth/bluetooth_socket_net.h" | 32 #include "device/bluetooth/bluetooth_socket_net.h" |
34 #include "device/bluetooth/bluetooth_socket_thread.h" | 33 #include "device/bluetooth/bluetooth_socket_thread.h" |
35 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 34 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
36 #include "device/bluetooth/bluez/bluetooth_adapter_profile_bluez.h" | 35 #include "device/bluetooth/bluez/bluetooth_adapter_profile_bluez.h" |
37 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" | 36 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" |
38 #include "device/bluetooth/dbus/bluetooth_device_client.h" | 37 #include "device/bluetooth/dbus/bluetooth_device_client.h" |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 | 345 |
347 void BluetoothSocketBlueZ::Released() { | 346 void BluetoothSocketBlueZ::Released() { |
348 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 347 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
349 DCHECK(profile_); | 348 DCHECK(profile_); |
350 | 349 |
351 VLOG(1) << profile_->object_path().value() << ": Release"; | 350 VLOG(1) << profile_->object_path().value() << ": Release"; |
352 } | 351 } |
353 | 352 |
354 void BluetoothSocketBlueZ::NewConnection( | 353 void BluetoothSocketBlueZ::NewConnection( |
355 const dbus::ObjectPath& device_path, | 354 const dbus::ObjectPath& device_path, |
356 std::unique_ptr<dbus::FileDescriptor> fd, | 355 base::ScopedFD fd, |
357 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 356 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
358 const ConfirmationCallback& callback) { | 357 const ConfirmationCallback& callback) { |
359 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 358 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
360 | 359 |
361 VLOG(1) << uuid_.canonical_value() | 360 VLOG(1) << uuid_.canonical_value() |
362 << ": New connection from device: " << device_path.value(); | 361 << ": New connection from device: " << device_path.value(); |
363 | 362 |
364 if (!device_path_.value().empty()) { | 363 if (!device_path_.value().empty()) { |
365 DCHECK(device_path_ == device_path); | 364 DCHECK(device_path_ == device_path); |
366 | 365 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 FROM_HERE, | 440 FROM_HERE, |
442 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, client_socket, | 441 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, client_socket, |
443 request->device_path, base::Passed(&request->fd), | 442 request->device_path, base::Passed(&request->fd), |
444 request->options, | 443 request->options, |
445 base::Bind(&BluetoothSocketBlueZ::OnNewConnection, this, | 444 base::Bind(&BluetoothSocketBlueZ::OnNewConnection, this, |
446 client_socket, request->callback))); | 445 client_socket, request->callback))); |
447 } | 446 } |
448 | 447 |
449 void BluetoothSocketBlueZ::DoNewConnection( | 448 void BluetoothSocketBlueZ::DoNewConnection( |
450 const dbus::ObjectPath& device_path, | 449 const dbus::ObjectPath& device_path, |
451 std::unique_ptr<dbus::FileDescriptor> fd, | 450 base::ScopedFD fd, |
452 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 451 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
453 const ConfirmationCallback& callback) { | 452 const ConfirmationCallback& callback) { |
454 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 453 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
455 base::ThreadRestrictions::AssertIOAllowed(); | 454 base::ThreadRestrictions::AssertIOAllowed(); |
456 fd->CheckValidity(); | |
457 | 455 |
458 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; | 456 if (!fd.is_valid()) { |
459 if (!fd->is_valid()) { | 457 LOG(WARNING) << uuid_.canonical_value() << " :" << fd.get() |
460 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() | |
461 << ": Invalid file descriptor received from Bluetooth Daemon."; | 458 << ": Invalid file descriptor received from Bluetooth Daemon."; |
462 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); | 459 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
463 return; | 460 return; |
464 } | 461 } |
465 | 462 |
466 if (tcp_socket()) { | 463 if (tcp_socket()) { |
467 LOG(WARNING) << uuid_.canonical_value() << ": Already connected"; | 464 LOG(WARNING) << uuid_.canonical_value() << ": Already connected"; |
468 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); | 465 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
469 return; | 466 return; |
470 } | 467 } |
471 | 468 |
472 ResetTCPSocket(); | 469 ResetTCPSocket(); |
473 | 470 |
474 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the | 471 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the |
475 // TCPSocket implementation does not actually require one. | 472 // TCPSocket implementation does not actually require one. |
476 int net_result = | 473 int net_result = |
477 tcp_socket()->AdoptConnectedSocket(fd->value(), net::IPEndPoint()); | 474 tcp_socket()->AdoptConnectedSocket(fd.release(), net::IPEndPoint()); |
478 if (net_result != net::OK) { | 475 if (net_result != net::OK) { |
479 LOG(WARNING) << uuid_.canonical_value() << ": Error adopting socket: " | 476 LOG(WARNING) << uuid_.canonical_value() << ": Error adopting socket: " |
480 << std::string(net::ErrorToString(net_result)); | 477 << std::string(net::ErrorToString(net_result)); |
481 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); | 478 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
482 return; | 479 return; |
483 } | 480 } |
484 | |
485 VLOG(2) << uuid_.canonical_value() | |
486 << ": Taking descriptor, confirming success."; | |
487 fd->TakeValue(); | |
488 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); | 481 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
489 } | 482 } |
490 | 483 |
491 void BluetoothSocketBlueZ::OnNewConnection( | 484 void BluetoothSocketBlueZ::OnNewConnection( |
492 scoped_refptr<BluetoothSocket> socket, | 485 scoped_refptr<BluetoothSocket> socket, |
493 const ConfirmationCallback& callback, | 486 const ConfirmationCallback& callback, |
494 Status status) { | 487 Status status) { |
495 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 488 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
496 DCHECK(accept_request_.get()); | 489 DCHECK(accept_request_.get()); |
497 DCHECK(connection_request_queue_.size() >= 1); | 490 DCHECK(connection_request_queue_.size() >= 1); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 DCHECK(profile_); | 528 DCHECK(profile_); |
536 | 529 |
537 VLOG(1) << profile_->object_path().value() << ": Release profile"; | 530 VLOG(1) << profile_->object_path().value() << ": Release profile"; |
538 | 531 |
539 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) | 532 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
540 ->ReleaseProfile(device_path_, profile_); | 533 ->ReleaseProfile(device_path_, profile_); |
541 profile_ = nullptr; | 534 profile_ = nullptr; |
542 } | 535 } |
543 | 536 |
544 } // namespace bluez | 537 } // namespace bluez |
OLD | NEW |