Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_socket_win.h
1 // Copyright (c) 2012 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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_
7
8 #include <WinSock2.h>
9
10 #include <string>
11
12 #include "base/memory/ref_counted.h"
13 #include "device/bluetooth/bluetooth_service_record_win.h"
14 #include "device/bluetooth/bluetooth_socket.h"
15 #include "device/bluetooth/bluetooth_socket_net.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/socket/tcp_socket.h"
18
19 namespace device {
20
21 class BluetoothServiceRecord;
22
23 // The BluetoothSocketWin class implements BluetoothSocket for the Microsoft
24 // Windows platform.
25 class BluetoothSocketWin : public BluetoothSocketNet {
26  public:
27   typedef base::Callback<void(scoped_refptr<BluetoothSocketWin>,
28                               const net::IPEndPoint&)> OnNewConnectionCallback;
29
30   static scoped_refptr<BluetoothSocketWin> CreateBluetoothSocket(
31       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
32       scoped_refptr<BluetoothSocketThread> socket_thread,
33       net::NetLog* net_log,
34       const net::NetLog::Source& source);
35
36   // Starts a service with the given uuid, name and rfcomm_channel.
37   // |success_callback| is invoked when the underlying socket is created
38   // and the service is published successfully. Otherwise, |error_callback| is
39   // called with an error message. |new_connection_callback| is invoked when
40   // an incoming connection is accepted by the underlying socket.
41   void StartService(
42       const BluetoothUUID& uuid,
43       const std::string& name,
44       int rfcomm_channel,
45       const base::Closure& success_callback,
46       const ErrorCompletionCallback& error_callback,
47       const OnNewConnectionCallback& new_connection_callback);
48
49   // connection has been established successfully. If an error occurs, calls
50   // |error_callback| with a system error message.
51   void Connect(const BluetoothServiceRecord& service_record,
52                const base::Closure& success_callback,
53                const ErrorCompletionCallback& error_callback);
54
55   // BluetoothSocketNet:
56   void ResetData();
57
58  protected:
59   virtual ~BluetoothSocketWin();
60
61  private:
62   struct ServiceRegData;
63
64   BluetoothSocketWin(scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
65                      scoped_refptr<BluetoothSocketThread> socket_thread,
66                      net::NetLog* net_log,
67                      const net::NetLog::Source& source);
68
69
70   void DoConnect(const base::Closure& success_callback,
71                  const ErrorCompletionCallback& error_callback);
72   void DoStartService(const BluetoothUUID& uuid,
73       const std::string& name,
74       int rfcomm_channel,
75       const base::Closure& success_callback,
76       const ErrorCompletionCallback& error_callback,
77       const OnNewConnectionCallback& new_connection_callback);
78   void DoAccept();
79   void OnAcceptOnSocketThread(int accept_result);
80   void OnAcceptOnUI(scoped_ptr<net::TCPSocket> accept_socket,
81                     const net::IPEndPoint& peer_address);
82
83   std::string device_address_;
84   bool supports_rfcomm_;
85   uint8 rfcomm_channel_;
86   BTH_ADDR bth_addr_;
87
88   scoped_ptr<ServiceRegData> service_reg_data_;
89   scoped_ptr<net::TCPSocket> accept_socket_;
90   net::IPEndPoint accept_address_;
91   OnNewConnectionCallback on_new_connection_callback_;
92
93   DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin);
94 };
95
96 }  // namespace device
97
98 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_