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 EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
10 #include "extensions/browser/api/socket/socket.h"
12 // This looks like it should be forward-declarable, but it does some tricky
13 // moves that make it easier to just include it.
14 #include "net/socket/tcp_client_socket.h"
15 #include "net/socket/tcp_server_socket.h"
21 namespace extensions
{
23 class TCPSocket
: public Socket
{
25 explicit TCPSocket(const std::string
& owner_extension_id
);
26 TCPSocket(net::TCPClientSocket
* tcp_client_socket
,
27 const std::string
& owner_extension_id
,
28 bool is_connected
= false);
30 ~TCPSocket() override
;
32 void Connect(const std::string
& address
,
34 const CompletionCallback
& callback
) override
;
35 void Disconnect() override
;
36 int Bind(const std::string
& address
, uint16 port
) override
;
37 void Read(int count
, const ReadCompletionCallback
& callback
) override
;
38 void RecvFrom(int count
, const RecvFromCompletionCallback
& callback
) override
;
39 void SendTo(scoped_refptr
<net::IOBuffer
> io_buffer
,
41 const std::string
& address
,
43 const CompletionCallback
& callback
) override
;
44 bool SetKeepAlive(bool enable
, int delay
) override
;
45 bool SetNoDelay(bool no_delay
) override
;
46 int Listen(const std::string
& address
,
49 std::string
* error_msg
) override
;
50 void Accept(const AcceptCompletionCallback
& callback
) override
;
52 bool IsConnected() override
;
54 bool GetPeerAddress(net::IPEndPoint
* address
) override
;
55 bool GetLocalAddress(net::IPEndPoint
* address
) override
;
57 // Like Disconnect(), only Release() doesn't delete the underlying stream
58 // or attempt to close it. Useful when giving away ownership with
60 virtual void Release();
62 Socket::SocketType
GetSocketType() const override
;
64 static TCPSocket
* CreateSocketForTesting(
65 net::TCPClientSocket
* tcp_client_socket
,
66 const std::string
& owner_extension_id
,
67 bool is_connected
= false);
68 static TCPSocket
* CreateServerSocketForTesting(
69 net::TCPServerSocket
* tcp_server_socket
,
70 const std::string
& owner_extension_id
);
72 // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection
73 // wasn't set up via Connect() (vs Listen()/Accept()).
74 net::TCPClientSocket
* ClientStream();
76 // Whether a Read() has been issued, that hasn't come back yet.
77 bool HasPendingRead() const;
80 int WriteImpl(net::IOBuffer
* io_buffer
,
82 const net::CompletionCallback
& callback
) override
;
85 void RefreshConnectionStatus();
86 void OnConnectComplete(int result
);
87 void OnReadComplete(scoped_refptr
<net::IOBuffer
> io_buffer
, int result
);
88 void OnAccept(int result
);
90 TCPSocket(net::TCPServerSocket
* tcp_server_socket
,
91 const std::string
& owner_extension_id
);
93 scoped_ptr
<net::TCPClientSocket
> socket_
;
94 scoped_ptr
<net::TCPServerSocket
> server_socket_
;
96 enum SocketMode
{ UNKNOWN
= 0, CLIENT
, SERVER
, };
97 SocketMode socket_mode_
;
99 CompletionCallback connect_callback_
;
101 ReadCompletionCallback read_callback_
;
103 scoped_ptr
<net::StreamSocket
> accept_socket_
;
104 AcceptCompletionCallback accept_callback_
;
107 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
108 // socket objects with additional properties related to the behavior defined in
109 // the "sockets.tcp" namespace.
110 class ResumableTCPSocket
: public TCPSocket
{
112 explicit ResumableTCPSocket(const std::string
& owner_extension_id
);
113 explicit ResumableTCPSocket(net::TCPClientSocket
* tcp_client_socket
,
114 const std::string
& owner_extension_id
,
117 // Overriden from ApiResource
118 bool IsPersistent() const override
;
120 const std::string
& name() const { return name_
; }
121 void set_name(const std::string
& name
) { name_
= name
; }
123 bool persistent() const { return persistent_
; }
124 void set_persistent(bool persistent
) { persistent_
= persistent
; }
126 int buffer_size() const { return buffer_size_
; }
127 void set_buffer_size(int buffer_size
) { buffer_size_
= buffer_size
; }
129 bool paused() const { return paused_
; }
130 void set_paused(bool paused
) { paused_
= paused
; }
133 friend class ApiResourceManager
<ResumableTCPSocket
>;
134 static const char* service_name() { return "ResumableTCPSocketManager"; }
136 // Application-defined string - see sockets_tcp.idl.
138 // Flag indicating whether the socket is left open when the application is
139 // suspended - see sockets_tcp.idl.
141 // The size of the buffer used to receive data - see sockets_tcp.idl.
143 // Flag indicating whether a connected socket blocks its peer from sending
144 // more data - see sockets_tcp.idl.
148 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
149 // regular socket objects with additional properties related to the behavior
150 // defined in the "sockets.tcpServer" namespace.
151 class ResumableTCPServerSocket
: public TCPSocket
{
153 explicit ResumableTCPServerSocket(const std::string
& owner_extension_id
);
155 // Overriden from ApiResource
156 bool IsPersistent() const override
;
158 const std::string
& name() const { return name_
; }
159 void set_name(const std::string
& name
) { name_
= name
; }
161 bool persistent() const { return persistent_
; }
162 void set_persistent(bool persistent
) { persistent_
= persistent
; }
164 bool paused() const { return paused_
; }
165 void set_paused(bool paused
) { paused_
= paused
; }
168 friend class ApiResourceManager
<ResumableTCPServerSocket
>;
169 static const char* service_name() {
170 return "ResumableTCPServerSocketManager";
173 // Application-defined string - see sockets_tcp_server.idl.
175 // Flag indicating whether the socket is left open when the application is
176 // suspended - see sockets_tcp_server.idl.
178 // Flag indicating whether a connected socket blocks its peer from sending
179 // more data - see sockets_tcp_server.idl.
183 } // namespace extensions
185 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_