| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_ | 5 #ifndef PPAPI_PROXY_PPB_TCP_SOCKET_BASE_RESOURCE_H_ |
| 6 #define PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_ | 6 #define PPAPI_PROXY_PPB_TCP_SOCKET_BASE_RESOURCE_H_ |
| 7 | 7 |
| 8 #include <queue> | |
| 9 #include <string> | 8 #include <string> |
| 10 #include <vector> | 9 #include <vector> |
| 11 | 10 |
| 12 #include "base/compiler_specific.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "ppapi/c/ppb_tcp_socket.h" | 13 #include "ppapi/c/ppb_tcp_socket.h" |
| 14 #include "ppapi/c/private/ppb_net_address_private.h" | 14 #include "ppapi/c/private/ppb_net_address_private.h" |
| 15 #include "ppapi/shared_impl/resource.h" | 15 #include "ppapi/proxy/plugin_resource.h" |
| 16 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 16 #include "ppapi/shared_impl/tracked_callback.h" | 17 #include "ppapi/shared_impl/tracked_callback.h" |
| 17 | 18 |
| 18 namespace ppapi { | 19 namespace ppapi { |
| 19 | 20 |
| 20 class PPB_X509Certificate_Fields; | 21 class PPB_X509Certificate_Fields; |
| 21 class PPB_X509Certificate_Private_Shared; | 22 class PPB_X509Certificate_Private_Shared; |
| 22 class SocketOptionData; | 23 class SocketOptionData; |
| 23 | 24 |
| 24 // This class provides the shared implementation for both PPB_TCPSocket and | 25 namespace proxy { |
| 25 // PPB_TCPSocket_Private. | 26 |
| 26 class PPAPI_SHARED_EXPORT TCPSocketShared { | 27 class PPAPI_PROXY_EXPORT TCPSocketBaseResource : public PluginResource { |
| 27 public: | 28 public: |
| 28 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read | 29 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read |
| 29 // message is allowed to request. | 30 // message is allowed to request. |
| 30 static const int32_t kMaxReadSize; | 31 static const int32_t kMaxReadSize; |
| 31 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write | 32 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write |
| 32 // message is allowed to carry. | 33 // message is allowed to carry. |
| 33 static const int32_t kMaxWriteSize; | 34 static const int32_t kMaxWriteSize; |
| 34 | 35 |
| 35 // The maximum number that we allow for setting | 36 // The maximum number that we allow for setting |
| 36 // PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input | 37 // 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 // argument sanity check, it doesn't mean the browser guarantees to support |
| 38 // such a buffer size. | 39 // such a buffer size. |
| 39 static const int32_t kMaxSendBufferSize; | 40 static const int32_t kMaxSendBufferSize; |
| 40 // The maximum number that we allow for setting | 41 // The maximum number that we allow for setting |
| 41 // PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input | 42 // 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 // argument sanity check, it doesn't mean the browser guarantees to support |
| 43 // such a buffer size. | 44 // such a buffer size. |
| 44 static const int32_t kMaxReceiveBufferSize; | 45 static const int32_t kMaxReceiveBufferSize; |
| 45 | 46 |
| 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: | 47 protected: |
| 78 enum ConnectionState { | 48 enum ConnectionState { |
| 79 // Before a connection is successfully established (including a connect | 49 // Before a connection is successfully established (including a connect |
| 80 // request is pending or a previous connect request failed). | 50 // request is pending or a previous connect request failed). |
| 81 BEFORE_CONNECT, | 51 BEFORE_CONNECT, |
| 82 // A connection has been successfully established (including a request of | 52 // A connection has been successfully established (including a request of |
| 83 // initiating SSL is pending). | 53 // initiating SSL is pending). |
| 84 CONNECTED, | 54 CONNECTED, |
| 85 // An SSL connection has been successfully established. | 55 // An SSL connection has been successfully established. |
| 86 SSL_CONNECTED, | 56 SSL_CONNECTED, |
| 87 // The connection has been ended. | 57 // The connection has been ended. |
| 88 DISCONNECTED | 58 DISCONNECTED |
| 89 }; | 59 }; |
| 90 | 60 |
| 91 TCPSocketShared(ResourceObjectType resource_type, uint32 socket_id); | 61 TCPSocketBaseResource(Connection connection, |
| 92 virtual ~TCPSocketShared(); | 62 PP_Instance instance, |
| 63 bool private_api); |
| 64 TCPSocketBaseResource(Connection connection, |
| 65 PP_Instance instance, |
| 66 bool private_api, |
| 67 const PP_NetAddress_Private& local_addr, |
| 68 const PP_NetAddress_Private& remote_addr); |
| 69 virtual ~TCPSocketBaseResource(); |
| 93 | 70 |
| 94 int32_t ConnectImpl(const char* host, | 71 int32_t ConnectImpl(const char* host, |
| 95 uint16_t port, | 72 uint16_t port, |
| 96 scoped_refptr<TrackedCallback> callback); | 73 scoped_refptr<TrackedCallback> callback); |
| 97 int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr, | 74 int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr, |
| 98 scoped_refptr<TrackedCallback> callback); | 75 scoped_refptr<TrackedCallback> callback); |
| 99 PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr); | 76 PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr); |
| 100 PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr); | 77 PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr); |
| 101 int32_t SSLHandshakeImpl(const char* server_name, | 78 int32_t SSLHandshakeImpl(const char* server_name, |
| 102 uint16_t server_port, | 79 uint16_t server_port, |
| 103 scoped_refptr<TrackedCallback> callback); | 80 scoped_refptr<TrackedCallback> callback); |
| 104 PP_Resource GetServerCertificateImpl(); | 81 PP_Resource GetServerCertificateImpl(); |
| 105 PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate, | 82 PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate, |
| 106 PP_Bool trusted); | 83 PP_Bool trusted); |
| 107 int32_t ReadImpl(char* buffer, | 84 int32_t ReadImpl(char* buffer, |
| 108 int32_t bytes_to_read, | 85 int32_t bytes_to_read, |
| 109 scoped_refptr<TrackedCallback> callback); | 86 scoped_refptr<TrackedCallback> callback); |
| 110 int32_t WriteImpl(const char* buffer, | 87 int32_t WriteImpl(const char* buffer, |
| 111 int32_t bytes_to_write, | 88 int32_t bytes_to_write, |
| 112 scoped_refptr<TrackedCallback> callback); | 89 scoped_refptr<TrackedCallback> callback); |
| 113 void DisconnectImpl(); | 90 void DisconnectImpl(); |
| 114 int32_t SetOptionImpl(PP_TCPSocket_Option name, | 91 int32_t SetOptionImpl(PP_TCPSocket_Option name, |
| 115 const PP_Var& value, | 92 const PP_Var& value, |
| 116 scoped_refptr<TrackedCallback> callback); | 93 scoped_refptr<TrackedCallback> callback); |
| 117 | 94 |
| 118 void Init(uint32 socket_id); | |
| 119 bool IsConnected() const; | 95 bool IsConnected() const; |
| 120 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); | 96 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); |
| 121 | 97 |
| 122 ResourceObjectType resource_type_; | 98 // IPC message handlers. |
| 123 | 99 void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params, |
| 124 uint32 socket_id_; | 100 const PP_NetAddress_Private& local_addr, |
| 125 ConnectionState connection_state_; | 101 const PP_NetAddress_Private& remote_addr); |
| 102 void OnPluginMsgSSLHandshakeReply( |
| 103 const ResourceMessageReplyParams& params, |
| 104 const PPB_X509Certificate_Fields& certificate_fields); |
| 105 void OnPluginMsgReadReply(const ResourceMessageReplyParams& params, |
| 106 const std::string& data); |
| 107 void OnPluginMsgWriteReply(const ResourceMessageReplyParams& params); |
| 108 void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams& params); |
| 126 | 109 |
| 127 scoped_refptr<TrackedCallback> connect_callback_; | 110 scoped_refptr<TrackedCallback> connect_callback_; |
| 128 scoped_refptr<TrackedCallback> ssl_handshake_callback_; | 111 scoped_refptr<TrackedCallback> ssl_handshake_callback_; |
| 129 scoped_refptr<TrackedCallback> read_callback_; | 112 scoped_refptr<TrackedCallback> read_callback_; |
| 130 scoped_refptr<TrackedCallback> write_callback_; | 113 scoped_refptr<TrackedCallback> write_callback_; |
| 131 std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_; | 114 std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_; |
| 132 | 115 |
| 116 ConnectionState connection_state_; |
| 133 char* read_buffer_; | 117 char* read_buffer_; |
| 134 int32_t bytes_to_read_; | 118 int32_t bytes_to_read_; |
| 135 | 119 |
| 136 PP_NetAddress_Private local_addr_; | 120 PP_NetAddress_Private local_addr_; |
| 137 PP_NetAddress_Private remote_addr_; | 121 PP_NetAddress_Private remote_addr_; |
| 138 | 122 |
| 139 scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_; | 123 scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_; |
| 140 | 124 |
| 141 std::vector<std::vector<char> > trusted_certificates_; | 125 std::vector<std::vector<char> > trusted_certificates_; |
| 142 std::vector<std::vector<char> > untrusted_certificates_; | 126 std::vector<std::vector<char> > untrusted_certificates_; |
| 143 | 127 |
| 144 private: | 128 private: |
| 145 DISALLOW_COPY_AND_ASSIGN(TCPSocketShared); | 129 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result); |
| 130 |
| 131 bool private_api_; |
| 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(TCPSocketBaseResource); |
| 146 }; | 134 }; |
| 147 | 135 |
| 136 } // namespace proxy |
| 148 } // namespace ppapi | 137 } // namespace ppapi |
| 149 | 138 |
| 150 #endif // PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_ | 139 #endif // PPAPI_PROXY_PPB_TCP_SOCKET_BASE_RESOURCE_H_ |
| OLD | NEW |