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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_socket_chromeos.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "dbus/file_descriptor.h"
14 #include "device/bluetooth/bluetooth_socket.h"
15 #include "device/bluetooth/bluetooth_socket_net.h"
16 #include "device/bluetooth/bluetooth_socket_thread.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
22 const char kSocketAlreadyConnected[] = "Socket is already connected.";
29 scoped_refptr<BluetoothSocketChromeOS>
30 BluetoothSocketChromeOS::CreateBluetoothSocket(
31 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
32 scoped_refptr<device::BluetoothSocketThread> socket_thread,
34 const net::NetLog::Source& source) {
35 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
37 return make_scoped_refptr(
38 new BluetoothSocketChromeOS(
39 ui_task_runner, socket_thread, net_log, source));
42 BluetoothSocketChromeOS::BluetoothSocketChromeOS(
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
44 scoped_refptr<device::BluetoothSocketThread> socket_thread,
46 const net::NetLog::Source& source)
47 : BluetoothSocketNet(ui_task_runner, socket_thread, net_log, source) {
50 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() {
53 void BluetoothSocketChromeOS::Connect(
54 scoped_ptr<dbus::FileDescriptor> fd,
55 const base::Closure& success_callback,
56 const ErrorCompletionCallback& error_callback) {
57 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread());
59 socket_thread()->task_runner()->PostTask(
62 &BluetoothSocketChromeOS::DoConnect,
65 base::Bind(&BluetoothSocketChromeOS::PostSuccess,
68 base::Bind(&BluetoothSocketChromeOS::PostErrorCompletion,
73 void BluetoothSocketChromeOS::DoConnect(
74 scoped_ptr<dbus::FileDescriptor> fd,
75 const base::Closure& success_callback,
76 const ErrorCompletionCallback& error_callback) {
77 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread());
78 base::ThreadRestrictions::AssertIOAllowed();
79 DCHECK(fd->is_valid());
82 error_callback.Run(kSocketAlreadyConnected);
88 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the
89 // TCPSocket implementation does not actually require one.
90 int net_result = tcp_socket()->AdoptConnectedSocket(fd->value(),
92 if (net_result != net::OK) {
93 error_callback.Run("Error connecting to socket: " +
94 std::string(net::ErrorToString(net_result)));
99 success_callback.Run();
102 } // namespace chromeos