1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_
10 #include "chrome/browser/extensions/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);
32 virtual void Connect(const std::string& address,
34 const CompletionCallback& callback) OVERRIDE;
35 virtual void Disconnect() OVERRIDE;
36 virtual int Bind(const std::string& address, int port) OVERRIDE;
37 virtual void Read(int count,
38 const ReadCompletionCallback& callback) OVERRIDE;
39 virtual void RecvFrom(int count,
40 const RecvFromCompletionCallback& callback) OVERRIDE;
41 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
43 const std::string& address,
45 const CompletionCallback& callback) OVERRIDE;
46 virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE;
47 virtual bool SetNoDelay(bool no_delay) OVERRIDE;
48 virtual int Listen(const std::string& address, int port,
49 int backlog, std::string* error_msg) OVERRIDE;
50 virtual void Accept(const AcceptCompletionCallback &callback) OVERRIDE;
52 virtual bool IsConnected() OVERRIDE;
54 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE;
55 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE;
56 virtual Socket::SocketType GetSocketType() const OVERRIDE;
58 static TCPSocket* CreateSocketForTesting(
59 net::TCPClientSocket* tcp_client_socket,
60 const std::string& owner_extension_id,
61 bool is_connected = false);
62 static TCPSocket* CreateServerSocketForTesting(
63 net::TCPServerSocket* tcp_server_socket,
64 const std::string& owner_extension_id);
67 virtual int WriteImpl(net::IOBuffer* io_buffer,
69 const net::CompletionCallback& callback) OVERRIDE;
72 void RefreshConnectionStatus();
73 void OnConnectComplete(int result);
74 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
76 void OnAccept(int result);
78 TCPSocket(net::TCPServerSocket* tcp_server_socket,
79 const std::string& owner_extension_id);
81 scoped_ptr<net::TCPClientSocket> socket_;
82 scoped_ptr<net::TCPServerSocket> server_socket_;
89 SocketMode socket_mode_;
91 CompletionCallback connect_callback_;
93 ReadCompletionCallback read_callback_;
95 scoped_ptr<net::StreamSocket> accept_socket_;
96 AcceptCompletionCallback accept_callback_;
99 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
100 // socket objects with additional properties related to the behavior defined in
101 // the "sockets.tcp" namespace.
102 class ResumableTCPSocket : public TCPSocket {
104 explicit ResumableTCPSocket(const std::string& owner_extension_id);
105 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket,
106 const std::string& owner_extension_id,
109 // Overriden from ApiResource
110 virtual bool IsPersistent() const OVERRIDE;
112 const std::string& name() const { return name_; }
113 void set_name(const std::string& name) { name_ = name; }
115 bool persistent() const { return persistent_; }
116 void set_persistent(bool persistent) { persistent_ = persistent; }
118 int buffer_size() const { return buffer_size_; }
119 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
121 bool paused() const { return paused_; }
122 void set_paused(bool paused) { paused_ = paused; }
125 friend class ApiResourceManager<ResumableTCPSocket>;
126 static const char* service_name() {
127 return "ResumableTCPSocketManager";
130 // Application-defined string - see sockets_tcp.idl.
132 // Flag indicating whether the socket is left open when the application is
133 // suspended - see sockets_tcp.idl.
135 // The size of the buffer used to receive data - see sockets_tcp.idl.
137 // Flag indicating whether a connected socket blocks its peer from sending
138 // more data - see sockets_tcp.idl.
142 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
143 // regular socket objects with additional properties related to the behavior
144 // defined in the "sockets.tcpServer" namespace.
145 class ResumableTCPServerSocket : public TCPSocket {
147 explicit ResumableTCPServerSocket(const std::string& owner_extension_id);
149 // Overriden from ApiResource
150 virtual bool IsPersistent() const OVERRIDE;
152 const std::string& name() const { return name_; }
153 void set_name(const std::string& name) { name_ = name; }
155 bool persistent() const { return persistent_; }
156 void set_persistent(bool persistent) { persistent_ = persistent; }
158 bool paused() const { return paused_; }
159 void set_paused(bool paused) { paused_ = paused; }
162 friend class ApiResourceManager<ResumableTCPServerSocket>;
163 static const char* service_name() {
164 return "ResumableTCPServerSocketManager";
167 // Application-defined string - see sockets_tcp_server.idl.
169 // Flag indicating whether the socket is left open when the application is
170 // suspended - see sockets_tcp_server.idl.
172 // Flag indicating whether a connected socket blocks its peer from sending
173 // more data - see sockets_tcp_server.idl.
177 } // namespace extensions
179 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_