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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/threading/thread_checker.h"
16 #include "device/bluetooth/bluetooth_service_record_win.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18 #include "net/base/net_log.h"
19 #include "net/socket/tcp_socket.h"
23 class IOBufferWithSize
;
28 class BluetoothServiceRecord
;
29 class BluetoothSocketThreadWin
;
31 // This class is an implementation of BluetoothSocket class for the Windows
32 // platform. All public methods (including the factory method) must be called
33 // on the UI thread, while underlying socket operations are performed on a
35 class BluetoothSocketWin
: public BluetoothSocket
{
37 static scoped_refptr
<BluetoothSocketWin
> CreateBluetoothSocket(
38 const BluetoothServiceRecord
& service_record
,
39 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
40 scoped_refptr
<BluetoothSocketThreadWin
> socket_thread
,
42 const net::NetLog::Source
& source
);
44 // Connect to the peer device and calls |success_callback| when the
45 // connection has been established successfully. If an error occurs, calls
46 // |error_callback| with a system error message.
47 void Connect(const base::Closure
& success_callback
,
48 const ErrorCompletionCallback
& error_callback
);
50 // Overriden from BluetoothSocket:
51 virtual void Close() OVERRIDE
;
53 virtual void Disconnect(const base::Closure
& callback
) OVERRIDE
;
55 virtual void Receive(int count
,
56 const ReceiveCompletionCallback
& success_callback
,
57 const ReceiveErrorCompletionCallback
& error_callback
)
59 virtual void Send(scoped_refptr
<net::IOBuffer
> buffer
,
61 const SendCompletionCallback
& success_callback
,
62 const ErrorCompletionCallback
& error_callback
) OVERRIDE
;
65 virtual ~BluetoothSocketWin();
69 scoped_refptr
<net::IOBuffer
> buffer
;
71 SendCompletionCallback success_callback
;
72 ErrorCompletionCallback error_callback
;
75 BluetoothSocketWin(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
76 scoped_refptr
<BluetoothSocketThreadWin
> socket_thread
,
78 const net::NetLog::Source
& source
);
81 void DoConnect(const base::Closure
& success_callback
,
82 const ErrorCompletionCallback
& error_callback
);
83 void DoDisconnect(const base::Closure
& callback
);
84 void DoReceive(int count
,
85 const ReceiveCompletionCallback
& success_callback
,
86 const ReceiveErrorCompletionCallback
& error_callback
);
87 void DoSend(scoped_refptr
<net::IOBuffer
> buffer
,
89 const SendCompletionCallback
& success_callback
,
90 const ErrorCompletionCallback
& error_callback
);
92 void PostSuccess(const base::Closure
& callback
);
93 void PostErrorCompletion(const ErrorCompletionCallback
& callback
,
94 const std::string
& error
);
95 void PostReceiveCompletion(const ReceiveCompletionCallback
& callback
,
97 scoped_refptr
<net::IOBuffer
> io_buffer
);
98 void PostReceiveErrorCompletion(
99 const ReceiveErrorCompletionCallback
& callback
,
101 const std::string
& error_message
);
102 void PostSendCompletion(const SendCompletionCallback
& callback
,
105 void SendFrontWriteRequest();
106 void OnSocketWriteComplete(const SendCompletionCallback
& success_callback
,
107 const ErrorCompletionCallback
& error_callback
,
109 void OnSocketReadComplete(
110 const ReceiveCompletionCallback
& success_callback
,
111 const ReceiveErrorCompletionCallback
& error_callback
,
114 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
115 scoped_refptr
<BluetoothSocketThreadWin
> socket_thread_
;
116 net::NetLog
* net_log_
;
117 const net::NetLog::Source source_
;
118 std::string device_address_
;
119 bool supports_rfcomm_
;
120 uint8 rfcomm_channel_
;
122 scoped_ptr
<net::TCPSocket
> tcp_socket_
;
123 // Queue of pending writes. The buffer at the front of the queue is the one
125 std::queue
<linked_ptr
<WriteRequest
> > write_queue_
;
126 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
128 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketWin
);
131 } // namespace device
133 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_