service_resolver_64: Correctly check all the bytes of the service code.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_socket_net.h
blob74117767388e5be3b374ac73165b9c1700fc58f5
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_
8 #include <queue>
9 #include <string>
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"
20 namespace net {
21 class IOBuffer;
22 class IOBufferWithSize;
23 } // namespace net
25 namespace device {
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 {
32 public:
33 // 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,
40 int buffer_size,
41 const SendCompletionCallback& success_callback,
42 const ErrorCompletionCallback& error_callback) override;
44 protected:
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);
71 private:
72 struct WriteRequest {
73 WriteRequest();
74 ~WriteRequest();
76 scoped_refptr<net::IOBuffer> buffer;
77 int buffer_size;
78 SendCompletionCallback success_callback;
79 ErrorCompletionCallback error_callback;
82 void DoClose();
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,
90 int read_result);
91 void DoSend(scoped_refptr<net::IOBuffer> buffer,
92 int buffer_size,
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,
98 int send_result);
100 void PostReceiveCompletion(const ReceiveCompletionCallback& callback,
101 int io_buffer_size,
102 scoped_refptr<net::IOBuffer> io_buffer);
103 void PostReceiveErrorCompletion(
104 const ReceiveErrorCompletionCallback& callback,
105 ErrorReason reason,
106 const std::string& error_message);
107 void PostSendCompletion(const SendCompletionCallback& callback,
108 int bytes_written);
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_