| 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_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ | 5 #ifndef PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ |
| 6 #define PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ | 6 #define PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "ppapi/c/ppb_tcp_socket.h" | 14 #include "ppapi/c/ppb_tcp_socket.h" |
| 15 #include "ppapi/c/private/ppb_net_address_private.h" | 15 #include "ppapi/c/private/ppb_net_address_private.h" |
| 16 #include "ppapi/proxy/plugin_resource.h" | 16 #include "ppapi/proxy/plugin_resource.h" |
| 17 #include "ppapi/proxy/ppapi_proxy_export.h" | 17 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 18 #include "ppapi/shared_impl/ppb_tcp_socket_shared.h" |
| 18 #include "ppapi/shared_impl/tracked_callback.h" | 19 #include "ppapi/shared_impl/tracked_callback.h" |
| 19 | 20 |
| 20 namespace ppapi { | 21 namespace ppapi { |
| 21 | 22 |
| 22 class PPB_X509Certificate_Fields; | 23 class PPB_X509Certificate_Fields; |
| 23 class PPB_X509Certificate_Private_Shared; | 24 class PPB_X509Certificate_Private_Shared; |
| 24 class SocketOptionData; | 25 class SocketOptionData; |
| 25 | 26 |
| 26 namespace proxy { | 27 namespace proxy { |
| 27 | 28 |
| 28 class PPAPI_PROXY_EXPORT TCPSocketResourceBase : public PluginResource { | 29 class PPAPI_PROXY_EXPORT TCPSocketResourceBase : public PluginResource { |
| 29 public: | 30 public: |
| 31 // TODO(yzshen): Move these constants to ppb_tcp_socket_shared. |
| 30 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read | 32 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read |
| 31 // message is allowed to request. | 33 // message is allowed to request. |
| 32 static const int32_t kMaxReadSize; | 34 static const int32_t kMaxReadSize; |
| 33 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write | 35 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write |
| 34 // message is allowed to carry. | 36 // message is allowed to carry. |
| 35 static const int32_t kMaxWriteSize; | 37 static const int32_t kMaxWriteSize; |
| 36 | 38 |
| 37 // The maximum number that we allow for setting | 39 // The maximum number that we allow for setting |
| 38 // PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input | 40 // PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input |
| 39 // argument sanity check, it doesn't mean the browser guarantees to support | 41 // argument sanity check, it doesn't mean the browser guarantees to support |
| 40 // such a buffer size. | 42 // such a buffer size. |
| 41 static const int32_t kMaxSendBufferSize; | 43 static const int32_t kMaxSendBufferSize; |
| 42 // The maximum number that we allow for setting | 44 // The maximum number that we allow for setting |
| 43 // PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input | 45 // PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input |
| 44 // argument sanity check, it doesn't mean the browser guarantees to support | 46 // argument sanity check, it doesn't mean the browser guarantees to support |
| 45 // such a buffer size. | 47 // such a buffer size. |
| 46 static const int32_t kMaxReceiveBufferSize; | 48 static const int32_t kMaxReceiveBufferSize; |
| 47 | 49 |
| 48 protected: | 50 protected: |
| 49 enum ConnectionState { | |
| 50 // Before a connection is successfully established (including a connect | |
| 51 // request is pending or a previous connect request failed). | |
| 52 BEFORE_CONNECT, | |
| 53 // A connection has been successfully established (including a request of | |
| 54 // initiating SSL is pending). | |
| 55 CONNECTED, | |
| 56 // An SSL connection has been successfully established. | |
| 57 SSL_CONNECTED, | |
| 58 // The connection has been ended. | |
| 59 DISCONNECTED | |
| 60 }; | |
| 61 | |
| 62 // C-tor used for new sockets. | 51 // C-tor used for new sockets. |
| 63 TCPSocketResourceBase(Connection connection, | 52 TCPSocketResourceBase(Connection connection, |
| 64 PP_Instance instance, | 53 PP_Instance instance, |
| 65 bool private_api); | 54 TCPSocketVersion version); |
| 66 | 55 |
| 67 // C-tor used for already accepted sockets. | 56 // C-tor used for already accepted sockets. |
| 68 TCPSocketResourceBase(Connection connection, | 57 TCPSocketResourceBase(Connection connection, |
| 69 PP_Instance instance, | 58 PP_Instance instance, |
| 70 bool private_api, | 59 TCPSocketVersion version, |
| 71 const PP_NetAddress_Private& local_addr, | 60 const PP_NetAddress_Private& local_addr, |
| 72 const PP_NetAddress_Private& remote_addr); | 61 const PP_NetAddress_Private& remote_addr); |
| 73 | 62 |
| 74 virtual ~TCPSocketResourceBase(); | 63 virtual ~TCPSocketResourceBase(); |
| 75 | 64 |
| 65 // Implemented by subclasses to create resources for accepted sockets. |
| 66 virtual PP_Resource CreateAcceptedSocket( |
| 67 int pending_host_id, |
| 68 const PP_NetAddress_Private& local_addr, |
| 69 const PP_NetAddress_Private& remote_addr) = 0; |
| 70 |
| 71 int32_t BindImpl(const PP_NetAddress_Private* addr, |
| 72 scoped_refptr<TrackedCallback> callback); |
| 76 int32_t ConnectImpl(const char* host, | 73 int32_t ConnectImpl(const char* host, |
| 77 uint16_t port, | 74 uint16_t port, |
| 78 scoped_refptr<TrackedCallback> callback); | 75 scoped_refptr<TrackedCallback> callback); |
| 79 int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr, | 76 int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr, |
| 80 scoped_refptr<TrackedCallback> callback); | 77 scoped_refptr<TrackedCallback> callback); |
| 81 PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr); | 78 PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr); |
| 82 PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr); | 79 PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr); |
| 83 int32_t SSLHandshakeImpl(const char* server_name, | 80 int32_t SSLHandshakeImpl(const char* server_name, |
| 84 uint16_t server_port, | 81 uint16_t server_port, |
| 85 scoped_refptr<TrackedCallback> callback); | 82 scoped_refptr<TrackedCallback> callback); |
| 86 PP_Resource GetServerCertificateImpl(); | 83 PP_Resource GetServerCertificateImpl(); |
| 87 PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate, | 84 PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate, |
| 88 PP_Bool trusted); | 85 PP_Bool trusted); |
| 89 int32_t ReadImpl(char* buffer, | 86 int32_t ReadImpl(char* buffer, |
| 90 int32_t bytes_to_read, | 87 int32_t bytes_to_read, |
| 91 scoped_refptr<TrackedCallback> callback); | 88 scoped_refptr<TrackedCallback> callback); |
| 92 int32_t WriteImpl(const char* buffer, | 89 int32_t WriteImpl(const char* buffer, |
| 93 int32_t bytes_to_write, | 90 int32_t bytes_to_write, |
| 94 scoped_refptr<TrackedCallback> callback); | 91 scoped_refptr<TrackedCallback> callback); |
| 95 void DisconnectImpl(); | 92 int32_t ListenImpl(int32_t backlog, scoped_refptr<TrackedCallback> callback); |
| 93 int32_t AcceptImpl(PP_Resource* accepted_tcp_socket, |
| 94 scoped_refptr<TrackedCallback> callback); |
| 95 void CloseImpl(); |
| 96 int32_t SetOptionImpl(PP_TCPSocket_Option name, | 96 int32_t SetOptionImpl(PP_TCPSocket_Option name, |
| 97 const PP_Var& value, | 97 const PP_Var& value, |
| 98 scoped_refptr<TrackedCallback> callback); | 98 scoped_refptr<TrackedCallback> callback); |
| 99 | 99 |
| 100 bool IsConnected() const; | |
| 101 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); | 100 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); |
| 102 | 101 |
| 103 // IPC message handlers. | 102 // IPC message handlers. |
| 103 void OnPluginMsgBindReply(const ResourceMessageReplyParams& params, |
| 104 const PP_NetAddress_Private& local_addr); |
| 104 void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params, | 105 void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params, |
| 105 const PP_NetAddress_Private& local_addr, | 106 const PP_NetAddress_Private& local_addr, |
| 106 const PP_NetAddress_Private& remote_addr); | 107 const PP_NetAddress_Private& remote_addr); |
| 107 void OnPluginMsgSSLHandshakeReply( | 108 void OnPluginMsgSSLHandshakeReply( |
| 108 const ResourceMessageReplyParams& params, | 109 const ResourceMessageReplyParams& params, |
| 109 const PPB_X509Certificate_Fields& certificate_fields); | 110 const PPB_X509Certificate_Fields& certificate_fields); |
| 110 void OnPluginMsgReadReply(const ResourceMessageReplyParams& params, | 111 void OnPluginMsgReadReply(const ResourceMessageReplyParams& params, |
| 111 const std::string& data); | 112 const std::string& data); |
| 112 void OnPluginMsgWriteReply(const ResourceMessageReplyParams& params); | 113 void OnPluginMsgWriteReply(const ResourceMessageReplyParams& params); |
| 114 void OnPluginMsgListenReply(const ResourceMessageReplyParams& params); |
| 115 void OnPluginMsgAcceptReply(const ResourceMessageReplyParams& params, |
| 116 int pending_host_id, |
| 117 const PP_NetAddress_Private& local_addr, |
| 118 const PP_NetAddress_Private& remote_addr); |
| 113 void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams& params); | 119 void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams& params); |
| 114 | 120 |
| 121 scoped_refptr<TrackedCallback> bind_callback_; |
| 115 scoped_refptr<TrackedCallback> connect_callback_; | 122 scoped_refptr<TrackedCallback> connect_callback_; |
| 116 scoped_refptr<TrackedCallback> ssl_handshake_callback_; | 123 scoped_refptr<TrackedCallback> ssl_handshake_callback_; |
| 117 scoped_refptr<TrackedCallback> read_callback_; | 124 scoped_refptr<TrackedCallback> read_callback_; |
| 118 scoped_refptr<TrackedCallback> write_callback_; | 125 scoped_refptr<TrackedCallback> write_callback_; |
| 126 scoped_refptr<TrackedCallback> listen_callback_; |
| 127 scoped_refptr<TrackedCallback> accept_callback_; |
| 119 std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_; | 128 std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_; |
| 120 | 129 |
| 121 ConnectionState connection_state_; | 130 TCPSocketState state_; |
| 122 char* read_buffer_; | 131 char* read_buffer_; |
| 123 int32_t bytes_to_read_; | 132 int32_t bytes_to_read_; |
| 124 | 133 |
| 125 PP_NetAddress_Private local_addr_; | 134 PP_NetAddress_Private local_addr_; |
| 126 PP_NetAddress_Private remote_addr_; | 135 PP_NetAddress_Private remote_addr_; |
| 127 | 136 |
| 128 scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_; | 137 scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_; |
| 129 | 138 |
| 130 std::vector<std::vector<char> > trusted_certificates_; | 139 std::vector<std::vector<char> > trusted_certificates_; |
| 131 std::vector<std::vector<char> > untrusted_certificates_; | 140 std::vector<std::vector<char> > untrusted_certificates_; |
| 132 | 141 |
| 142 PP_Resource* accepted_tcp_socket_; |
| 143 |
| 133 private: | 144 private: |
| 134 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result); | 145 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result); |
| 135 | 146 |
| 136 bool private_api_; | 147 TCPSocketVersion version_; |
| 137 | 148 |
| 138 DISALLOW_COPY_AND_ASSIGN(TCPSocketResourceBase); | 149 DISALLOW_COPY_AND_ASSIGN(TCPSocketResourceBase); |
| 139 }; | 150 }; |
| 140 | 151 |
| 141 } // namespace proxy | 152 } // namespace proxy |
| 142 } // namespace ppapi | 153 } // namespace ppapi |
| 143 | 154 |
| 144 #endif // PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ | 155 #endif // PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_ |
| OLD | NEW |