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.
5 #ifndef PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_
6 #define PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "ppapi/c/ppb_tcp_socket.h"
15 #include "ppapi/c/private/ppb_net_address_private.h"
16 #include "ppapi/proxy/plugin_resource.h"
17 #include "ppapi/proxy/ppapi_proxy_export.h"
18 #include "ppapi/shared_impl/ppb_tcp_socket_shared.h"
19 #include "ppapi/shared_impl/tracked_callback.h"
23 class PPB_X509Certificate_Fields
;
24 class PPB_X509Certificate_Private_Shared
;
25 class SocketOptionData
;
29 class PPAPI_PROXY_EXPORT TCPSocketResourceBase
: public PluginResource
{
31 // TODO(yzshen): Move these constants to ppb_tcp_socket_shared.
32 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read
33 // message is allowed to request.
34 static const int32_t kMaxReadSize
;
35 // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write
36 // message is allowed to carry.
37 static const int32_t kMaxWriteSize
;
39 // The maximum number that we allow for setting
40 // PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input
41 // argument sanity check, it doesn't mean the browser guarantees to support
42 // such a buffer size.
43 static const int32_t kMaxSendBufferSize
;
44 // The maximum number that we allow for setting
45 // PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input
46 // argument sanity check, it doesn't mean the browser guarantees to support
47 // such a buffer size.
48 static const int32_t kMaxReceiveBufferSize
;
51 // C-tor used for new sockets.
52 TCPSocketResourceBase(Connection connection
,
54 TCPSocketVersion version
);
56 // C-tor used for already accepted sockets.
57 TCPSocketResourceBase(Connection connection
,
59 TCPSocketVersion version
,
60 const PP_NetAddress_Private
& local_addr
,
61 const PP_NetAddress_Private
& remote_addr
);
63 virtual ~TCPSocketResourceBase();
65 // Implemented by subclasses to create resources for accepted sockets.
66 virtual PP_Resource
CreateAcceptedSocket(
68 const PP_NetAddress_Private
& local_addr
,
69 const PP_NetAddress_Private
& remote_addr
) = 0;
71 int32_t BindImpl(const PP_NetAddress_Private
* addr
,
72 scoped_refptr
<TrackedCallback
> callback
);
73 int32_t ConnectImpl(const char* host
,
75 scoped_refptr
<TrackedCallback
> callback
);
76 int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private
* addr
,
77 scoped_refptr
<TrackedCallback
> callback
);
78 PP_Bool
GetLocalAddressImpl(PP_NetAddress_Private
* local_addr
);
79 PP_Bool
GetRemoteAddressImpl(PP_NetAddress_Private
* remote_addr
);
80 int32_t SSLHandshakeImpl(const char* server_name
,
82 scoped_refptr
<TrackedCallback
> callback
);
83 PP_Resource
GetServerCertificateImpl();
84 PP_Bool
AddChainBuildingCertificateImpl(PP_Resource certificate
,
86 int32_t ReadImpl(char* buffer
,
87 int32_t bytes_to_read
,
88 scoped_refptr
<TrackedCallback
> callback
);
89 int32_t WriteImpl(const char* buffer
,
90 int32_t bytes_to_write
,
91 scoped_refptr
<TrackedCallback
> callback
);
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
);
96 int32_t SetOptionImpl(PP_TCPSocket_Option name
,
98 scoped_refptr
<TrackedCallback
> callback
);
100 void PostAbortIfNecessary(scoped_refptr
<TrackedCallback
>* callback
);
102 // IPC message handlers.
103 void OnPluginMsgBindReply(const ResourceMessageReplyParams
& params
,
104 const PP_NetAddress_Private
& local_addr
);
105 void OnPluginMsgConnectReply(const ResourceMessageReplyParams
& params
,
106 const PP_NetAddress_Private
& local_addr
,
107 const PP_NetAddress_Private
& remote_addr
);
108 void OnPluginMsgSSLHandshakeReply(
109 const ResourceMessageReplyParams
& params
,
110 const PPB_X509Certificate_Fields
& certificate_fields
);
111 void OnPluginMsgReadReply(const ResourceMessageReplyParams
& params
,
112 const std::string
& data
);
113 void OnPluginMsgWriteReply(const ResourceMessageReplyParams
& params
);
114 void OnPluginMsgListenReply(const ResourceMessageReplyParams
& params
);
115 void OnPluginMsgAcceptReply(const ResourceMessageReplyParams
& params
,
117 const PP_NetAddress_Private
& local_addr
,
118 const PP_NetAddress_Private
& remote_addr
);
119 void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams
& params
);
121 scoped_refptr
<TrackedCallback
> bind_callback_
;
122 scoped_refptr
<TrackedCallback
> connect_callback_
;
123 scoped_refptr
<TrackedCallback
> ssl_handshake_callback_
;
124 scoped_refptr
<TrackedCallback
> read_callback_
;
125 scoped_refptr
<TrackedCallback
> write_callback_
;
126 scoped_refptr
<TrackedCallback
> listen_callback_
;
127 scoped_refptr
<TrackedCallback
> accept_callback_
;
128 std::queue
<scoped_refptr
<TrackedCallback
> > set_option_callbacks_
;
130 TCPSocketState state_
;
132 int32_t bytes_to_read_
;
134 PP_NetAddress_Private local_addr_
;
135 PP_NetAddress_Private remote_addr_
;
137 scoped_refptr
<PPB_X509Certificate_Private_Shared
> server_certificate_
;
139 std::vector
<std::vector
<char> > trusted_certificates_
;
140 std::vector
<std::vector
<char> > untrusted_certificates_
;
142 PP_Resource
* accepted_tcp_socket_
;
145 void RunCallback(scoped_refptr
<TrackedCallback
> callback
, int32_t pp_result
);
147 TCPSocketVersion version_
;
149 DISALLOW_COPY_AND_ASSIGN(TCPSocketResourceBase
);
155 #endif // PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_