Home | History | Annotate | Download | only in shared_impl
      1 // Copyright 2013 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 PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_
      6 #define PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_
      7 
      8 #include <queue>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "ppapi/c/ppb_tcp_socket.h"
     14 #include "ppapi/c/private/ppb_net_address_private.h"
     15 #include "ppapi/shared_impl/resource.h"
     16 #include "ppapi/shared_impl/tracked_callback.h"
     17 
     18 namespace ppapi {
     19 
     20 class PPB_X509Certificate_Fields;
     21 class PPB_X509Certificate_Private_Shared;
     22 class SocketOptionData;
     23 
     24 // This class provides the shared implementation for both PPB_TCPSocket and
     25 // PPB_TCPSocket_Private.
     26 class PPAPI_SHARED_EXPORT TCPSocketShared {
     27  public:
     28   // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read
     29   // message is allowed to request.
     30   static const int32_t kMaxReadSize;
     31   // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write
     32   // message is allowed to carry.
     33   static const int32_t kMaxWriteSize;
     34 
     35   // The maximum number that we allow for setting
     36   // PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input
     37   // argument sanity check, it doesn't mean the browser guarantees to support
     38   // such a buffer size.
     39   static const int32_t kMaxSendBufferSize;
     40   // The maximum number that we allow for setting
     41   // PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input
     42   // argument sanity check, it doesn't mean the browser guarantees to support
     43   // such a buffer size.
     44   static const int32_t kMaxReceiveBufferSize;
     45 
     46   // Notifications on operations completion.
     47   void OnConnectCompleted(int32_t result,
     48                           const PP_NetAddress_Private& local_addr,
     49                           const PP_NetAddress_Private& remote_addr);
     50   void OnSSLHandshakeCompleted(
     51       bool succeeded,
     52       const PPB_X509Certificate_Fields& certificate_fields);
     53   void OnReadCompleted(int32_t result, const std::string& data);
     54   void OnWriteCompleted(int32_t result);
     55   void OnSetOptionCompleted(int32_t result);
     56 
     57   // Send functions that need to be implemented differently for the
     58   // proxied and non-proxied derived classes.
     59   virtual void SendConnect(const std::string& host, uint16_t port) = 0;
     60   virtual void SendConnectWithNetAddress(const PP_NetAddress_Private& addr) = 0;
     61   virtual void SendSSLHandshake(
     62       const std::string& server_name,
     63       uint16_t server_port,
     64       const std::vector<std::vector<char> >& trusted_certs,
     65       const std::vector<std::vector<char> >& untrusted_certs) = 0;
     66   virtual void SendRead(int32_t bytes_to_read) = 0;
     67   virtual void SendWrite(const std::string& buffer) = 0;
     68   virtual void SendDisconnect() = 0;
     69   virtual void SendSetOption(PP_TCPSocket_Option name,
     70                              const SocketOptionData& value) = 0;
     71 
     72   virtual Resource* GetOwnerResource() = 0;
     73 
     74   // Used to override PP_Error codes received from the browser side.
     75   virtual int32_t OverridePPError(int32_t pp_error);
     76 
     77  protected:
     78   enum ConnectionState {
     79     // Before a connection is successfully established (including a connect
     80     // request is pending or a previous connect request failed).
     81     BEFORE_CONNECT,
     82     // A connection has been successfully established (including a request of
     83     // initiating SSL is pending).
     84     CONNECTED,
     85     // An SSL connection has been successfully established.
     86     SSL_CONNECTED,
     87     // The connection has been ended.
     88     DISCONNECTED
     89   };
     90 
     91   TCPSocketShared(ResourceObjectType resource_type, uint32 socket_id);
     92   virtual ~TCPSocketShared();
     93 
     94   int32_t ConnectImpl(const char* host,
     95                       uint16_t port,
     96                       scoped_refptr<TrackedCallback> callback);
     97   int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr,
     98                                     scoped_refptr<TrackedCallback> callback);
     99   PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr);
    100   PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr);
    101   int32_t SSLHandshakeImpl(const char* server_name,
    102                            uint16_t server_port,
    103                            scoped_refptr<TrackedCallback> callback);
    104   PP_Resource GetServerCertificateImpl();
    105   PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate,
    106                                           PP_Bool trusted);
    107   int32_t ReadImpl(char* buffer,
    108                    int32_t bytes_to_read,
    109                    scoped_refptr<TrackedCallback> callback);
    110   int32_t WriteImpl(const char* buffer,
    111                     int32_t bytes_to_write,
    112                     scoped_refptr<TrackedCallback> callback);
    113   void DisconnectImpl();
    114   int32_t SetOptionImpl(PP_TCPSocket_Option name,
    115                         const PP_Var& value,
    116                         scoped_refptr<TrackedCallback> callback);
    117 
    118   void Init(uint32 socket_id);
    119   bool IsConnected() const;
    120   void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback);
    121 
    122   ResourceObjectType resource_type_;
    123 
    124   uint32 socket_id_;
    125   ConnectionState connection_state_;
    126 
    127   scoped_refptr<TrackedCallback> connect_callback_;
    128   scoped_refptr<TrackedCallback> ssl_handshake_callback_;
    129   scoped_refptr<TrackedCallback> read_callback_;
    130   scoped_refptr<TrackedCallback> write_callback_;
    131   std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_;
    132 
    133   char* read_buffer_;
    134   int32_t bytes_to_read_;
    135 
    136   PP_NetAddress_Private local_addr_;
    137   PP_NetAddress_Private remote_addr_;
    138 
    139   scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_;
    140 
    141   std::vector<std::vector<char> > trusted_certificates_;
    142   std::vector<std::vector<char> > untrusted_certificates_;
    143 
    144  private:
    145   DISALLOW_COPY_AND_ASSIGN(TCPSocketShared);
    146 };
    147 
    148 }  // namespace ppapi
    149 
    150 #endif  // PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_
    151