Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_socket_chromeos.cc
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.
4
5 #include "device/bluetooth/bluetooth_socket_chromeos.h"
6
7 #include <string>
8
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"
19
20 namespace {
21
22 const char kSocketAlreadyConnected[] = "Socket is already connected.";
23
24 }  // namespace
25
26 namespace chromeos {
27
28 // static
29 scoped_refptr<BluetoothSocketChromeOS>
30 BluetoothSocketChromeOS::CreateBluetoothSocket(
31     scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
32     scoped_refptr<device::BluetoothSocketThread> socket_thread,
33     net::NetLog* net_log,
34     const net::NetLog::Source& source) {
35   DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
36
37   return make_scoped_refptr(
38       new BluetoothSocketChromeOS(
39           ui_task_runner, socket_thread, net_log, source));
40 }
41
42 BluetoothSocketChromeOS::BluetoothSocketChromeOS(
43     scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
44     scoped_refptr<device::BluetoothSocketThread> socket_thread,
45     net::NetLog* net_log,
46     const net::NetLog::Source& source)
47     : BluetoothSocketNet(ui_task_runner, socket_thread, net_log, source) {
48 }
49
50 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() {
51 }
52
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());
58
59   socket_thread()->task_runner()->PostTask(
60       FROM_HERE,
61       base::Bind(
62           &BluetoothSocketChromeOS::DoConnect,
63           this,
64           base::Passed(&fd),
65           base::Bind(&BluetoothSocketChromeOS::PostSuccess,
66                      this,
67                      success_callback),
68           base::Bind(&BluetoothSocketChromeOS::PostErrorCompletion,
69                      this,
70                      error_callback)));
71 }
72
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());
80
81   if (tcp_socket()) {
82     error_callback.Run(kSocketAlreadyConnected);
83     return;
84   }
85
86   ResetTCPSocket();
87
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(),
91                                                       net::IPEndPoint());
92   if (net_result != net::OK) {
93     error_callback.Run("Error connecting to socket: " +
94                        std::string(net::ErrorToString(net_result)));
95     return;
96   }
97
98   fd->TakeValue();
99   success_callback.Run();
100 }
101
102 }  // namespace chromeos