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. 4 5#ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ 6#define CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ 7 8#include <string> 9 10#include "chrome/browser/extensions/api/socket/socket.h" 11 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" 16 17namespace net { 18class Socket; 19} 20 21namespace extensions { 22 23class 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); 29 30 virtual ~TCPSocket(); 31 32 virtual void Connect(const std::string& address, 33 int port, 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, 42 int byte_count, 43 const std::string& address, 44 int port, 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; 51 52 virtual bool IsConnected() OVERRIDE; 53 54 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE; 55 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE; 56 virtual Socket::SocketType GetSocketType() const OVERRIDE; 57 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); 65 66 protected: 67 virtual int WriteImpl(net::IOBuffer* io_buffer, 68 int io_buffer_size, 69 const net::CompletionCallback& callback) OVERRIDE; 70 71 private: 72 void RefreshConnectionStatus(); 73 void OnConnectComplete(int result); 74 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, 75 int result); 76 void OnAccept(int result); 77 78 TCPSocket(net::TCPServerSocket* tcp_server_socket, 79 const std::string& owner_extension_id); 80 81 scoped_ptr<net::TCPClientSocket> socket_; 82 scoped_ptr<net::TCPServerSocket> server_socket_; 83 84 enum SocketMode { 85 UNKNOWN = 0, 86 CLIENT, 87 SERVER, 88 }; 89 SocketMode socket_mode_; 90 91 CompletionCallback connect_callback_; 92 93 ReadCompletionCallback read_callback_; 94 95 scoped_ptr<net::StreamSocket> accept_socket_; 96 AcceptCompletionCallback accept_callback_; 97}; 98 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. 102class ResumableTCPSocket : public TCPSocket { 103 public: 104 explicit ResumableTCPSocket(const std::string& owner_extension_id); 105 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, 106 const std::string& owner_extension_id, 107 bool is_connected); 108 109 // Overriden from ApiResource 110 virtual bool IsPersistent() const OVERRIDE; 111 112 const std::string& name() const { return name_; } 113 void set_name(const std::string& name) { name_ = name; } 114 115 bool persistent() const { return persistent_; } 116 void set_persistent(bool persistent) { persistent_ = persistent; } 117 118 int buffer_size() const { return buffer_size_; } 119 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } 120 121 bool paused() const { return paused_; } 122 void set_paused(bool paused) { paused_ = paused; } 123 124 private: 125 friend class ApiResourceManager<ResumableTCPSocket>; 126 static const char* service_name() { 127 return "ResumableTCPSocketManager"; 128 } 129 130 // Application-defined string - see sockets_tcp.idl. 131 std::string name_; 132 // Flag indicating whether the socket is left open when the application is 133 // suspended - see sockets_tcp.idl. 134 bool persistent_; 135 // The size of the buffer used to receive data - see sockets_tcp.idl. 136 int buffer_size_; 137 // Flag indicating whether a connected socket blocks its peer from sending 138 // more data - see sockets_tcp.idl. 139 bool paused_; 140}; 141 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. 145class ResumableTCPServerSocket : public TCPSocket { 146 public: 147 explicit ResumableTCPServerSocket(const std::string& owner_extension_id); 148 149 // Overriden from ApiResource 150 virtual bool IsPersistent() const OVERRIDE; 151 152 const std::string& name() const { return name_; } 153 void set_name(const std::string& name) { name_ = name; } 154 155 bool persistent() const { return persistent_; } 156 void set_persistent(bool persistent) { persistent_ = persistent; } 157 158 bool paused() const { return paused_; } 159 void set_paused(bool paused) { paused_ = paused; } 160 161 private: 162 friend class ApiResourceManager<ResumableTCPServerSocket>; 163 static const char* service_name() { 164 return "ResumableTCPServerSocketManager"; 165 } 166 167 // Application-defined string - see sockets_tcp_server.idl. 168 std::string name_; 169 // Flag indicating whether the socket is left open when the application is 170 // suspended - see sockets_tcp_server.idl. 171 bool persistent_; 172 // Flag indicating whether a connected socket blocks its peer from sending 173 // more data - see sockets_tcp_server.idl. 174 bool paused_; 175}; 176 177} // namespace extensions 178 179#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ 180