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 void Close() override
;
35 void Disconnect(const base::Closure
& callback
) override
;
36 void Receive(int buffer_size
,
37 const ReceiveCompletionCallback
& success_callback
,
38 const ReceiveErrorCompletionCallback
& error_callback
) override
;
39 void Send(scoped_refptr
<net::IOBuffer
> buffer
,
41 const SendCompletionCallback
& success_callback
,
42 const ErrorCompletionCallback
& error_callback
) override
;
45 BluetoothSocketNet(scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
46 scoped_refptr
<BluetoothSocketThread
> socket_thread
);
47 ~BluetoothSocketNet() override
;
49 // Resets locally held data after a socket is closed. Default implementation
50 // does nothing, subclasses may override.
51 virtual void ResetData();
53 // Methods for subclasses to obtain the members.
54 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner() const {
55 return ui_task_runner_
;
58 scoped_refptr
<BluetoothSocketThread
> socket_thread() const {
59 return socket_thread_
;
62 net::TCPSocket
* tcp_socket() { return tcp_socket_
.get(); }
64 void ResetTCPSocket();
65 void SetTCPSocket(scoped_ptr
<net::TCPSocket
> tcp_socket
);
67 void PostSuccess(const base::Closure
& callback
);
68 void PostErrorCompletion(const ErrorCompletionCallback
& callback
,
69 const std::string
& error
);
76 scoped_refptr
<net::IOBuffer
> buffer
;
78 SendCompletionCallback success_callback
;
79 ErrorCompletionCallback error_callback
;
83 void DoDisconnect(const base::Closure
& callback
);
84 void DoReceive(int buffer_size
,
85 const ReceiveCompletionCallback
& success_callback
,
86 const ReceiveErrorCompletionCallback
& error_callback
);
87 void OnSocketReadComplete(
88 const ReceiveCompletionCallback
& success_callback
,
89 const ReceiveErrorCompletionCallback
& error_callback
,
91 void DoSend(scoped_refptr
<net::IOBuffer
> buffer
,
93 const SendCompletionCallback
& success_callback
,
94 const ErrorCompletionCallback
& error_callback
);
95 void SendFrontWriteRequest();
96 void OnSocketWriteComplete(const SendCompletionCallback
& success_callback
,
97 const ErrorCompletionCallback
& error_callback
,
100 void PostReceiveCompletion(const ReceiveCompletionCallback
& callback
,
102 scoped_refptr
<net::IOBuffer
> io_buffer
);
103 void PostReceiveErrorCompletion(
104 const ReceiveErrorCompletionCallback
& callback
,
106 const std::string
& error_message
);
107 void PostSendCompletion(const SendCompletionCallback
& callback
,
110 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
111 scoped_refptr
<BluetoothSocketThread
> socket_thread_
;
113 scoped_ptr
<net::TCPSocket
> tcp_socket_
;
114 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
115 std::queue
<linked_ptr
<WriteRequest
> > write_queue_
;
117 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketNet
);
120 } // namespace device
122 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_