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.
4
5#ifndef EXTENSIONS_BROWSER_API_SOCKETS_TCP_SOCKETS_TCP_API_H_
6#define EXTENSIONS_BROWSER_API_SOCKETS_TCP_SOCKETS_TCP_API_H_
7
8#include "extensions/browser/api/socket/socket_api.h"
9#include "extensions/common/api/sockets_tcp.h"
10
11namespace extensions {
12class ResumableTCPSocket;
13class TLSSocket;
14}
15
16namespace extensions {
17namespace core_api {
18
19class TCPSocketEventDispatcher;
20
21class TCPSocketAsyncApiFunction : public SocketAsyncApiFunction {
22 protected:
23  virtual ~TCPSocketAsyncApiFunction();
24
25  virtual scoped_ptr<SocketResourceManagerInterface>
26      CreateSocketResourceManager() OVERRIDE;
27
28  ResumableTCPSocket* GetTcpSocket(int socket_id);
29};
30
31class TCPSocketExtensionWithDnsLookupFunction
32    : public SocketExtensionWithDnsLookupFunction {
33 protected:
34  virtual ~TCPSocketExtensionWithDnsLookupFunction();
35
36  virtual scoped_ptr<SocketResourceManagerInterface>
37      CreateSocketResourceManager() OVERRIDE;
38
39  ResumableTCPSocket* GetTcpSocket(int socket_id);
40};
41
42class SocketsTcpCreateFunction : public TCPSocketAsyncApiFunction {
43 public:
44  DECLARE_EXTENSION_FUNCTION("sockets.tcp.create", SOCKETS_TCP_CREATE)
45
46  SocketsTcpCreateFunction();
47
48 protected:
49  virtual ~SocketsTcpCreateFunction();
50
51  // AsyncApiFunction:
52  virtual bool Prepare() OVERRIDE;
53  virtual void Work() OVERRIDE;
54
55 private:
56  FRIEND_TEST_ALL_PREFIXES(SocketsTcpUnitTest, Create);
57  scoped_ptr<sockets_tcp::Create::Params> params_;
58};
59
60class SocketsTcpUpdateFunction : public TCPSocketAsyncApiFunction {
61 public:
62  DECLARE_EXTENSION_FUNCTION("sockets.tcp.update", SOCKETS_TCP_UPDATE)
63
64  SocketsTcpUpdateFunction();
65
66 protected:
67  virtual ~SocketsTcpUpdateFunction();
68
69  // AsyncApiFunction:
70  virtual bool Prepare() OVERRIDE;
71  virtual void Work() OVERRIDE;
72
73 private:
74  scoped_ptr<sockets_tcp::Update::Params> params_;
75};
76
77class SocketsTcpSetPausedFunction : public TCPSocketAsyncApiFunction {
78 public:
79  DECLARE_EXTENSION_FUNCTION("sockets.tcp.setPaused", SOCKETS_TCP_SETPAUSED)
80
81  SocketsTcpSetPausedFunction();
82
83 protected:
84  virtual ~SocketsTcpSetPausedFunction();
85
86  // AsyncApiFunction
87  virtual bool Prepare() OVERRIDE;
88  virtual void Work() OVERRIDE;
89
90 private:
91  scoped_ptr<sockets_tcp::SetPaused::Params> params_;
92  TCPSocketEventDispatcher* socket_event_dispatcher_;
93};
94
95class SocketsTcpSetKeepAliveFunction : public TCPSocketAsyncApiFunction {
96 public:
97  DECLARE_EXTENSION_FUNCTION("sockets.tcp.setKeepAlive",
98                             SOCKETS_TCP_SETKEEPALIVE)
99
100  SocketsTcpSetKeepAliveFunction();
101
102 protected:
103  virtual ~SocketsTcpSetKeepAliveFunction();
104
105  // AsyncApiFunction
106  virtual bool Prepare() OVERRIDE;
107  virtual void Work() OVERRIDE;
108
109 private:
110  scoped_ptr<sockets_tcp::SetKeepAlive::Params> params_;
111};
112
113class SocketsTcpSetNoDelayFunction : public TCPSocketAsyncApiFunction {
114 public:
115  DECLARE_EXTENSION_FUNCTION("sockets.tcp.setNoDelay", SOCKETS_TCP_SETNODELAY)
116
117  SocketsTcpSetNoDelayFunction();
118
119 protected:
120  virtual ~SocketsTcpSetNoDelayFunction();
121
122  // AsyncApiFunction
123  virtual bool Prepare() OVERRIDE;
124  virtual void Work() OVERRIDE;
125
126 private:
127  scoped_ptr<sockets_tcp::SetNoDelay::Params> params_;
128};
129
130class SocketsTcpConnectFunction
131    : public TCPSocketExtensionWithDnsLookupFunction {
132 public:
133  DECLARE_EXTENSION_FUNCTION("sockets.tcp.connect", SOCKETS_TCP_CONNECT)
134
135  SocketsTcpConnectFunction();
136
137 protected:
138  virtual ~SocketsTcpConnectFunction();
139
140  // AsyncApiFunction:
141  virtual bool Prepare() OVERRIDE;
142  virtual void AsyncWorkStart() OVERRIDE;
143
144  // SocketExtensionWithDnsLookupFunction:
145  virtual void AfterDnsLookup(int lookup_result) OVERRIDE;
146
147 private:
148  void StartConnect();
149  void OnCompleted(int net_result);
150
151  scoped_ptr<sockets_tcp::Connect::Params> params_;
152  TCPSocketEventDispatcher* socket_event_dispatcher_;
153};
154
155class SocketsTcpDisconnectFunction : public TCPSocketAsyncApiFunction {
156 public:
157  DECLARE_EXTENSION_FUNCTION("sockets.tcp.disconnect", SOCKETS_TCP_DISCONNECT)
158
159  SocketsTcpDisconnectFunction();
160
161 protected:
162  virtual ~SocketsTcpDisconnectFunction();
163
164  // AsyncApiFunction:
165  virtual bool Prepare() OVERRIDE;
166  virtual void Work() OVERRIDE;
167
168 private:
169  scoped_ptr<sockets_tcp::Disconnect::Params> params_;
170};
171
172class SocketsTcpSendFunction : public TCPSocketAsyncApiFunction {
173 public:
174  DECLARE_EXTENSION_FUNCTION("sockets.tcp.send", SOCKETS_TCP_SEND)
175
176  SocketsTcpSendFunction();
177
178 protected:
179  virtual ~SocketsTcpSendFunction();
180
181  // AsyncApiFunction:
182  virtual bool Prepare() OVERRIDE;
183  virtual void AsyncWorkStart() OVERRIDE;
184
185 private:
186  void OnCompleted(int net_result);
187  void SetSendResult(int net_result, int bytes_sent);
188
189  scoped_ptr<sockets_tcp::Send::Params> params_;
190  scoped_refptr<net::IOBuffer> io_buffer_;
191  size_t io_buffer_size_;
192};
193
194class SocketsTcpCloseFunction : public TCPSocketAsyncApiFunction {
195 public:
196  DECLARE_EXTENSION_FUNCTION("sockets.tcp.close", SOCKETS_TCP_CLOSE)
197
198  SocketsTcpCloseFunction();
199
200 protected:
201  virtual ~SocketsTcpCloseFunction();
202
203  // AsyncApiFunction:
204  virtual bool Prepare() OVERRIDE;
205  virtual void Work() OVERRIDE;
206
207 private:
208  scoped_ptr<sockets_tcp::Close::Params> params_;
209};
210
211class SocketsTcpGetInfoFunction : public TCPSocketAsyncApiFunction {
212 public:
213  DECLARE_EXTENSION_FUNCTION("sockets.tcp.getInfo", SOCKETS_TCP_GETINFO)
214
215  SocketsTcpGetInfoFunction();
216
217 protected:
218  virtual ~SocketsTcpGetInfoFunction();
219
220  // AsyncApiFunction:
221  virtual bool Prepare() OVERRIDE;
222  virtual void Work() OVERRIDE;
223
224 private:
225  scoped_ptr<sockets_tcp::GetInfo::Params> params_;
226};
227
228class SocketsTcpGetSocketsFunction : public TCPSocketAsyncApiFunction {
229 public:
230  DECLARE_EXTENSION_FUNCTION("sockets.tcp.getSockets", SOCKETS_TCP_GETSOCKETS)
231
232  SocketsTcpGetSocketsFunction();
233
234 protected:
235  virtual ~SocketsTcpGetSocketsFunction();
236
237  // AsyncApiFunction:
238  virtual bool Prepare() OVERRIDE;
239  virtual void Work() OVERRIDE;
240};
241
242class SocketsTcpSecureFunction : public TCPSocketAsyncApiFunction {
243 public:
244  DECLARE_EXTENSION_FUNCTION("sockets.tcp.secure", SOCKETS_TCP_SECURE);
245
246  SocketsTcpSecureFunction();
247
248 protected:
249  virtual ~SocketsTcpSecureFunction();
250  virtual bool Prepare() OVERRIDE;
251  virtual void AsyncWorkStart() OVERRIDE;
252
253 private:
254  virtual void TlsConnectDone(scoped_ptr<extensions::TLSSocket> sock,
255                              int result);
256
257  bool paused_;
258  bool persistent_;
259  scoped_ptr<sockets_tcp::Secure::Params> params_;
260  scoped_refptr<net::URLRequestContextGetter> url_request_getter_;
261
262  DISALLOW_COPY_AND_ASSIGN(SocketsTcpSecureFunction);
263};
264
265}  // namespace core_api
266}  // namespace extensions
267
268#endif  // EXTENSIONS_BROWSER_API_SOCKETS_TCP_SOCKETS_TCP_API_H_
269