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