Home | History | Annotate | Download | only in socket
      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.
      4 
      5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
      6 #define EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
      7 
      8 #include <string>
      9 
     10 #include "extensions/browser/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 
     17 namespace net {
     18 class Socket;
     19 }
     20 
     21 namespace extensions {
     22 
     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);
     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, const ReadCompletionCallback& callback) OVERRIDE;
     38   virtual void RecvFrom(int count,
     39                         const RecvFromCompletionCallback& callback) OVERRIDE;
     40   virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
     41                       int byte_count,
     42                       const std::string& address,
     43                       int port,
     44                       const CompletionCallback& callback) OVERRIDE;
     45   virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE;
     46   virtual bool SetNoDelay(bool no_delay) OVERRIDE;
     47   virtual int Listen(const std::string& address,
     48                      int port,
     49                      int backlog,
     50                      std::string* error_msg) OVERRIDE;
     51   virtual void Accept(const AcceptCompletionCallback& callback) OVERRIDE;
     52 
     53   virtual bool IsConnected() OVERRIDE;
     54 
     55   virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE;
     56   virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE;
     57 
     58   // Like Disconnect(), only Release() doesn't delete the underlying stream
     59   // or attempt to close it. Useful when giving away ownership with
     60   // ClientStream().
     61   virtual void Release();
     62 
     63   virtual Socket::SocketType GetSocketType() const OVERRIDE;
     64 
     65   static TCPSocket* CreateSocketForTesting(
     66       net::TCPClientSocket* tcp_client_socket,
     67       const std::string& owner_extension_id,
     68       bool is_connected = false);
     69   static TCPSocket* CreateServerSocketForTesting(
     70       net::TCPServerSocket* tcp_server_socket,
     71       const std::string& owner_extension_id);
     72 
     73   // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection
     74   // wasn't set up via Connect() (vs Listen()/Accept()).
     75   net::TCPClientSocket* ClientStream();
     76 
     77   // Whether a Read() has been issued, that hasn't come back yet.
     78   bool HasPendingRead() const;
     79 
     80  protected:
     81   virtual int WriteImpl(net::IOBuffer* io_buffer,
     82                         int io_buffer_size,
     83                         const net::CompletionCallback& callback) OVERRIDE;
     84 
     85  private:
     86   void RefreshConnectionStatus();
     87   void OnConnectComplete(int result);
     88   void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result);
     89   void OnAccept(int result);
     90 
     91   TCPSocket(net::TCPServerSocket* tcp_server_socket,
     92             const std::string& owner_extension_id);
     93 
     94   scoped_ptr<net::TCPClientSocket> socket_;
     95   scoped_ptr<net::TCPServerSocket> server_socket_;
     96 
     97   enum SocketMode { UNKNOWN = 0, CLIENT, SERVER, };
     98   SocketMode socket_mode_;
     99 
    100   CompletionCallback connect_callback_;
    101 
    102   ReadCompletionCallback read_callback_;
    103 
    104   scoped_ptr<net::StreamSocket> accept_socket_;
    105   AcceptCompletionCallback accept_callback_;
    106 };
    107 
    108 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
    109 // socket objects with additional properties related to the behavior defined in
    110 // the "sockets.tcp" namespace.
    111 class ResumableTCPSocket : public TCPSocket {
    112  public:
    113   explicit ResumableTCPSocket(const std::string& owner_extension_id);
    114   explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket,
    115                               const std::string& owner_extension_id,
    116                               bool is_connected);
    117 
    118   // Overriden from ApiResource
    119   virtual bool IsPersistent() const OVERRIDE;
    120 
    121   const std::string& name() const { return name_; }
    122   void set_name(const std::string& name) { name_ = name; }
    123 
    124   bool persistent() const { return persistent_; }
    125   void set_persistent(bool persistent) { persistent_ = persistent; }
    126 
    127   int buffer_size() const { return buffer_size_; }
    128   void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
    129 
    130   bool paused() const { return paused_; }
    131   void set_paused(bool paused) { paused_ = paused; }
    132 
    133  private:
    134   friend class ApiResourceManager<ResumableTCPSocket>;
    135   static const char* service_name() { return "ResumableTCPSocketManager"; }
    136 
    137   // Application-defined string - see sockets_tcp.idl.
    138   std::string name_;
    139   // Flag indicating whether the socket is left open when the application is
    140   // suspended - see sockets_tcp.idl.
    141   bool persistent_;
    142   // The size of the buffer used to receive data - see sockets_tcp.idl.
    143   int buffer_size_;
    144   // Flag indicating whether a connected socket blocks its peer from sending
    145   // more data - see sockets_tcp.idl.
    146   bool paused_;
    147 };
    148 
    149 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
    150 // regular socket objects with additional properties related to the behavior
    151 // defined in the "sockets.tcpServer" namespace.
    152 class ResumableTCPServerSocket : public TCPSocket {
    153  public:
    154   explicit ResumableTCPServerSocket(const std::string& owner_extension_id);
    155 
    156   // Overriden from ApiResource
    157   virtual bool IsPersistent() const OVERRIDE;
    158 
    159   const std::string& name() const { return name_; }
    160   void set_name(const std::string& name) { name_ = name; }
    161 
    162   bool persistent() const { return persistent_; }
    163   void set_persistent(bool persistent) { persistent_ = persistent; }
    164 
    165   bool paused() const { return paused_; }
    166   void set_paused(bool paused) { paused_ = paused; }
    167 
    168  private:
    169   friend class ApiResourceManager<ResumableTCPServerSocket>;
    170   static const char* service_name() {
    171     return "ResumableTCPServerSocketManager";
    172   }
    173 
    174   // Application-defined string - see sockets_tcp_server.idl.
    175   std::string name_;
    176   // Flag indicating whether the socket is left open when the application is
    177   // suspended - see sockets_tcp_server.idl.
    178   bool persistent_;
    179   // Flag indicating whether a connected socket blocks its peer from sending
    180   // more data - see sockets_tcp_server.idl.
    181   bool paused_;
    182 };
    183 
    184 }  //  namespace extensions
    185 
    186 #endif  // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
    187