Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / proxy / tcp_socket_resource_base.h
blob3274fb9518e91e8f6bee72a65653ce17fa7a9b74
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_
8 #include <queue>
9 #include <string>
10 #include <vector>
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"
21 namespace ppapi {
23 class PPB_X509Certificate_Fields;
24 class PPB_X509Certificate_Private_Shared;
25 class SocketOptionData;
27 namespace proxy {
29 class PPAPI_PROXY_EXPORT TCPSocketResourceBase : public PluginResource {
30 public:
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;
50 protected:
51 // C-tor used for new sockets.
52 TCPSocketResourceBase(Connection connection,
53 PP_Instance instance,
54 TCPSocketVersion version);
56 // C-tor used for already accepted sockets.
57 TCPSocketResourceBase(Connection connection,
58 PP_Instance instance,
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(
67 int pending_host_id,
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,
74 uint16_t port,
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,
81 uint16_t server_port,
82 scoped_refptr<TrackedCallback> callback);
83 PP_Resource GetServerCertificateImpl();
84 PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate,
85 PP_Bool trusted);
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);
95 void CloseImpl();
96 int32_t SetOptionImpl(PP_TCPSocket_Option name,
97 const PP_Var& value,
98 bool check_connect_state,
99 scoped_refptr<TrackedCallback> callback);
101 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback);
103 // IPC message handlers.
104 void OnPluginMsgBindReply(const ResourceMessageReplyParams& params,
105 const PP_NetAddress_Private& local_addr);
106 void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params,
107 const PP_NetAddress_Private& local_addr,
108 const PP_NetAddress_Private& remote_addr);
109 void OnPluginMsgSSLHandshakeReply(
110 const ResourceMessageReplyParams& params,
111 const PPB_X509Certificate_Fields& certificate_fields);
112 void OnPluginMsgReadReply(const ResourceMessageReplyParams& params,
113 const std::string& data);
114 void OnPluginMsgWriteReply(const ResourceMessageReplyParams& params);
115 void OnPluginMsgListenReply(const ResourceMessageReplyParams& params);
116 void OnPluginMsgAcceptReply(const ResourceMessageReplyParams& params,
117 int pending_host_id,
118 const PP_NetAddress_Private& local_addr,
119 const PP_NetAddress_Private& remote_addr);
120 void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams& params);
122 scoped_refptr<TrackedCallback> bind_callback_;
123 scoped_refptr<TrackedCallback> connect_callback_;
124 scoped_refptr<TrackedCallback> ssl_handshake_callback_;
125 scoped_refptr<TrackedCallback> read_callback_;
126 scoped_refptr<TrackedCallback> write_callback_;
127 scoped_refptr<TrackedCallback> listen_callback_;
128 scoped_refptr<TrackedCallback> accept_callback_;
129 std::queue<scoped_refptr<TrackedCallback> > set_option_callbacks_;
131 TCPSocketState state_;
132 char* read_buffer_;
133 int32_t bytes_to_read_;
135 PP_NetAddress_Private local_addr_;
136 PP_NetAddress_Private remote_addr_;
138 scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_;
140 std::vector<std::vector<char> > trusted_certificates_;
141 std::vector<std::vector<char> > untrusted_certificates_;
143 PP_Resource* accepted_tcp_socket_;
145 private:
146 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result);
148 TCPSocketVersion version_;
150 DISALLOW_COPY_AND_ASSIGN(TCPSocketResourceBase);
153 } // namespace proxy
154 } // namespace ppapi
156 #endif // PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_