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_SOCKET_H_
      6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_
      7 
      8 #include <queue>
      9 #include <string>
     10 #include <utility>
     11 
     12 #include "base/callback.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "extensions/browser/api/api_resource.h"
     15 #include "extensions/browser/api/api_resource_manager.h"
     16 #include "net/base/completion_callback.h"
     17 #include "net/base/io_buffer.h"
     18 #include "net/base/ip_endpoint.h"
     19 #include "net/socket/tcp_client_socket.h"
     20 
     21 namespace net {
     22 class AddressList;
     23 class IPEndPoint;
     24 class Socket;
     25 }
     26 
     27 namespace extensions {
     28 
     29 typedef base::Callback<void(int)> CompletionCallback;
     30 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
     31     ReadCompletionCallback;
     32 typedef base::Callback<
     33     void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
     34     RecvFromCompletionCallback;
     35 typedef base::Callback<void(int, net::TCPClientSocket*)>
     36     AcceptCompletionCallback;
     37 
     38 // A Socket wraps a low-level socket and includes housekeeping information that
     39 // we need to manage it in the context of an extension.
     40 class Socket : public ApiResource {
     41  public:
     42   enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS };
     43 
     44   virtual ~Socket();
     45 
     46   // The hostname of the remote host that this socket is connected to.  This
     47   // may be the empty string if the client does not intend to ever upgrade the
     48   // socket to TLS, and thusly has not invoked set_hostname().
     49   const std::string& hostname() const { return hostname_; }
     50 
     51   // Set the hostname of the remote host that this socket is connected to.
     52   // Note: This may be an IP literal. In the case of IDNs, this should be a
     53   // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be
     54   // unbracketed.
     55   void set_hostname(const std::string& hostname) { hostname_ = hostname; }
     56 
     57   // Note: |address| contains the resolved IP address, not the hostname of
     58   // the remote endpoint. In order to upgrade this socket to TLS, callers
     59   // must also supply the hostname of the endpoint via set_hostname().
     60   virtual void Connect(const std::string& address,
     61                        int port,
     62                        const CompletionCallback& callback) = 0;
     63   virtual void Disconnect() = 0;
     64   virtual int Bind(const std::string& address, int port) = 0;
     65 
     66   // The |callback| will be called with the number of bytes read into the
     67   // buffer, or a negative number if an error occurred.
     68   virtual void Read(int count, const ReadCompletionCallback& callback) = 0;
     69 
     70   // The |callback| will be called with |byte_count| or a negative number if an
     71   // error occurred.
     72   void Write(scoped_refptr<net::IOBuffer> io_buffer,
     73              int byte_count,
     74              const CompletionCallback& callback);
     75 
     76   virtual void RecvFrom(int count,
     77                         const RecvFromCompletionCallback& callback) = 0;
     78   virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
     79                       int byte_count,
     80                       const std::string& address,
     81                       int port,
     82                       const CompletionCallback& callback) = 0;
     83 
     84   virtual bool SetKeepAlive(bool enable, int delay);
     85   virtual bool SetNoDelay(bool no_delay);
     86   virtual int Listen(const std::string& address,
     87                      int port,
     88                      int backlog,
     89                      std::string* error_msg);
     90   virtual void Accept(const AcceptCompletionCallback& callback);
     91 
     92   virtual bool IsConnected() = 0;
     93 
     94   virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
     95   virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
     96 
     97   virtual SocketType GetSocketType() const = 0;
     98 
     99   static bool StringAndPortToAddressList(const std::string& ip_address_str,
    100                                          int port,
    101                                          net::AddressList* address_list);
    102   static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
    103                                         int port,
    104                                         net::IPEndPoint* ip_end_point);
    105   static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
    106                                         std::string* ip_address_str,
    107                                         int* port);
    108 
    109  protected:
    110   explicit Socket(const std::string& owner_extension_id_);
    111 
    112   void WriteData();
    113   virtual int WriteImpl(net::IOBuffer* io_buffer,
    114                         int io_buffer_size,
    115                         const net::CompletionCallback& callback) = 0;
    116   virtual void OnWriteComplete(int result);
    117 
    118   std::string hostname_;
    119   bool is_connected_;
    120 
    121  private:
    122   friend class ApiResourceManager<Socket>;
    123   static const char* service_name() { return "SocketManager"; }
    124 
    125   struct WriteRequest {
    126     WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
    127                  int byte_count,
    128                  const CompletionCallback& callback);
    129     ~WriteRequest();
    130     scoped_refptr<net::IOBuffer> io_buffer;
    131     int byte_count;
    132     CompletionCallback callback;
    133     int bytes_written;
    134   };
    135   std::queue<WriteRequest> write_queue_;
    136   scoped_refptr<net::IOBuffer> io_buffer_write_;
    137 };
    138 
    139 }  //  namespace extensions
    140 
    141 #endif  // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_
    142