Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / api / socket / tcp_socket.h
blob40dd5fd7fd9163ce1e1de75937c689e7b2e61445
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_
8 #include <string>
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"
17 namespace net {
18 class Socket;
21 namespace extensions {
23 class TCPSocket : public Socket {
24 public:
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,
39 int byte_count,
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,
45 uint16 port,
46 int backlog,
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
57 // ClientStream().
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;
77 protected:
78 int WriteImpl(net::IOBuffer* io_buffer,
79 int io_buffer_size,
80 const net::CompletionCallback& callback) override;
82 private:
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 {
109 public:
110 explicit ResumableTCPSocket(const std::string& owner_extension_id);
111 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket,
112 const std::string& owner_extension_id,
113 bool is_connected);
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; }
130 private:
131 friend class ApiResourceManager<ResumableTCPSocket>;
132 static const char* service_name() { return "ResumableTCPSocketManager"; }
134 // Application-defined string - see sockets_tcp.idl.
135 std::string name_;
136 // Flag indicating whether the socket is left open when the application is
137 // suspended - see sockets_tcp.idl.
138 bool persistent_;
139 // The size of the buffer used to receive data - see sockets_tcp.idl.
140 int buffer_size_;
141 // Flag indicating whether a connected socket blocks its peer from sending
142 // more data - see sockets_tcp.idl.
143 bool paused_;
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 {
150 public:
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; }
165 private:
166 friend class ApiResourceManager<ResumableTCPServerSocket>;
167 static const char* service_name() {
168 return "ResumableTCPServerSocketManager";
171 // Application-defined string - see sockets_tcp_server.idl.
172 std::string name_;
173 // Flag indicating whether the socket is left open when the application is
174 // suspended - see sockets_tcp_server.idl.
175 bool persistent_;
176 // Flag indicating whether a connected socket blocks its peer from sending
177 // more data - see sockets_tcp_server.idl.
178 bool paused_;
181 } // namespace extensions
183 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_