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 net::AddressList
& address
,
33 const CompletionCallback
& callback
) override
;
34 void Disconnect() override
;
35 int Bind(const std::string
& address
, uint16 port
) override
;
36 void Read(int count
, const ReadCompletionCallback
& callback
) override
;
37 void RecvFrom(int count
, const RecvFromCompletionCallback
& callback
) override
;
38 void SendTo(scoped_refptr
<net::IOBuffer
> io_buffer
,
40 const net::IPEndPoint
& address
,
41 const CompletionCallback
& callback
) override
;
42 bool SetKeepAlive(bool enable
, int delay
) override
;
43 bool SetNoDelay(bool no_delay
) override
;
44 int Listen(const std::string
& address
,
47 std::string
* error_msg
) override
;
48 void Accept(const AcceptCompletionCallback
& callback
) override
;
50 bool IsConnected() override
;
52 bool GetPeerAddress(net::IPEndPoint
* address
) override
;
53 bool GetLocalAddress(net::IPEndPoint
* address
) override
;
55 // Like Disconnect(), only Release() doesn't delete the underlying stream
56 // or attempt to close it. Useful when giving away ownership with
58 virtual void Release();
60 Socket::SocketType
GetSocketType() const override
;
62 static TCPSocket
* CreateSocketForTesting(
63 net::TCPClientSocket
* tcp_client_socket
,
64 const std::string
& owner_extension_id
,
65 bool is_connected
= false);
66 static TCPSocket
* CreateServerSocketForTesting(
67 net::TCPServerSocket
* tcp_server_socket
,
68 const std::string
& owner_extension_id
);
70 // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection
71 // wasn't set up via Connect() (vs Listen()/Accept()).
72 net::TCPClientSocket
* ClientStream();
74 // Whether a Read() has been issued, that hasn't come back yet.
75 bool HasPendingRead() const;
78 int WriteImpl(net::IOBuffer
* io_buffer
,
80 const net::CompletionCallback
& callback
) override
;
83 void RefreshConnectionStatus();
84 void OnConnectComplete(int result
);
85 void OnReadComplete(scoped_refptr
<net::IOBuffer
> io_buffer
, int result
);
86 void OnAccept(int result
);
88 TCPSocket(net::TCPServerSocket
* tcp_server_socket
,
89 const std::string
& owner_extension_id
);
91 scoped_ptr
<net::TCPClientSocket
> socket_
;
92 scoped_ptr
<net::TCPServerSocket
> server_socket_
;
94 enum SocketMode
{ UNKNOWN
= 0, CLIENT
, SERVER
, };
95 SocketMode socket_mode_
;
97 CompletionCallback connect_callback_
;
99 ReadCompletionCallback read_callback_
;
101 scoped_ptr
<net::StreamSocket
> accept_socket_
;
102 AcceptCompletionCallback accept_callback_
;
105 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
106 // socket objects with additional properties related to the behavior defined in
107 // the "sockets.tcp" namespace.
108 class ResumableTCPSocket
: public TCPSocket
{
110 explicit ResumableTCPSocket(const std::string
& owner_extension_id
);
111 explicit ResumableTCPSocket(net::TCPClientSocket
* tcp_client_socket
,
112 const std::string
& owner_extension_id
,
115 // Overriden from ApiResource
116 bool IsPersistent() const override
;
118 const std::string
& name() const { return name_
; }
119 void set_name(const std::string
& name
) { name_
= name
; }
121 bool persistent() const { return persistent_
; }
122 void set_persistent(bool persistent
) { persistent_
= persistent
; }
124 int buffer_size() const { return buffer_size_
; }
125 void set_buffer_size(int buffer_size
) { buffer_size_
= buffer_size
; }
127 bool paused() const { return paused_
; }
128 void set_paused(bool paused
) { paused_
= paused
; }
131 friend class ApiResourceManager
<ResumableTCPSocket
>;
132 static const char* service_name() { return "ResumableTCPSocketManager"; }
134 // Application-defined string - see sockets_tcp.idl.
136 // Flag indicating whether the socket is left open when the application is
137 // suspended - see sockets_tcp.idl.
139 // The size of the buffer used to receive data - see sockets_tcp.idl.
141 // Flag indicating whether a connected socket blocks its peer from sending
142 // more data - see sockets_tcp.idl.
146 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
147 // regular socket objects with additional properties related to the behavior
148 // defined in the "sockets.tcpServer" namespace.
149 class ResumableTCPServerSocket
: public TCPSocket
{
151 explicit ResumableTCPServerSocket(const std::string
& owner_extension_id
);
153 // Overriden from ApiResource
154 bool IsPersistent() const override
;
156 const std::string
& name() const { return name_
; }
157 void set_name(const std::string
& name
) { name_
= name
; }
159 bool persistent() const { return persistent_
; }
160 void set_persistent(bool persistent
) { persistent_
= persistent
; }
162 bool paused() const { return paused_
; }
163 void set_paused(bool paused
) { paused_
= paused
; }
166 friend class ApiResourceManager
<ResumableTCPServerSocket
>;
167 static const char* service_name() {
168 return "ResumableTCPServerSocketManager";
171 // Application-defined string - see sockets_tcp_server.idl.
173 // Flag indicating whether the socket is left open when the application is
174 // suspended - see sockets_tcp_server.idl.
176 // Flag indicating whether a connected socket blocks its peer from sending
177 // more data - see sockets_tcp_server.idl.
181 } // namespace extensions
183 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_