1 // Copyright 2014 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_NET_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_
11 #include "base/basictypes.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/sequenced_task_runner.h"
16 #include "device/bluetooth/bluetooth_socket.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "net/socket/tcp_socket.h"
22 class IOBufferWithSize
;
27 // This class is a base-class for implementations of BluetoothSocket that can
28 // use net::TCPSocket. All public methods (including the factory method) must
29 // be called on the UI thread, while underlying socket operations are
30 // performed on a separate thread.
31 class BluetoothSocketNet
: public BluetoothSocket
{
34 virtual void Close() OVERRIDE
;
35 virtual void Disconnect(const base::Closure
& callback
) OVERRIDE
;
36 virtual void Receive(int buffer_size
,
37 const ReceiveCompletionCallback
& success_callback
,
38 const ReceiveErrorCompletionCallback
& error_callback
)
40 virtual void Send(scoped_refptr
<net::IOBuffer
> buffer
,
42 const SendCompletionCallback
& success_callback
,
43 const ErrorCompletionCallback
& error_callback
) OVERRIDE
;
46 BluetoothSocketNet(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
47 scoped_refptr
<BluetoothSocketThread
> socket_thread
);
48 virtual ~BluetoothSocketNet();
50 // Resets locally held data after a socket is closed. Default implementation
51 // does nothing, subclasses may override.
52 virtual void ResetData();
54 // Methods for subclasses to obtain the members.
55 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner() const {
56 return ui_task_runner_
;
59 scoped_refptr
<BluetoothSocketThread
> socket_thread() const {
60 return socket_thread_
;
63 net::TCPSocket
* tcp_socket() { return tcp_socket_
.get(); }
65 void ResetTCPSocket();
66 void SetTCPSocket(scoped_ptr
<net::TCPSocket
> tcp_socket
);
68 void PostSuccess(const base::Closure
& callback
);
69 void PostErrorCompletion(const ErrorCompletionCallback
& callback
,
70 const std::string
& error
);
77 scoped_refptr
<net::IOBuffer
> buffer
;
79 SendCompletionCallback success_callback
;
80 ErrorCompletionCallback error_callback
;
84 void DoDisconnect(const base::Closure
& callback
);
85 void DoReceive(int buffer_size
,
86 const ReceiveCompletionCallback
& success_callback
,
87 const ReceiveErrorCompletionCallback
& error_callback
);
88 void OnSocketReadComplete(
89 const ReceiveCompletionCallback
& success_callback
,
90 const ReceiveErrorCompletionCallback
& error_callback
,
92 void DoSend(scoped_refptr
<net::IOBuffer
> buffer
,
94 const SendCompletionCallback
& success_callback
,
95 const ErrorCompletionCallback
& error_callback
);
96 void SendFrontWriteRequest();
97 void OnSocketWriteComplete(const SendCompletionCallback
& success_callback
,
98 const ErrorCompletionCallback
& error_callback
,
101 void PostReceiveCompletion(const ReceiveCompletionCallback
& callback
,
103 scoped_refptr
<net::IOBuffer
> io_buffer
);
104 void PostReceiveErrorCompletion(
105 const ReceiveErrorCompletionCallback
& callback
,
107 const std::string
& error_message
);
108 void PostSendCompletion(const SendCompletionCallback
& callback
,
111 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
112 scoped_refptr
<BluetoothSocketThread
> socket_thread_
;
114 scoped_ptr
<net::TCPSocket
> tcp_socket_
;
115 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
116 std::queue
<linked_ptr
<WriteRequest
> > write_queue_
;
118 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketNet
);
121 } // namespace device
123 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_