• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_
6 #define PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_
7 
8 #include <queue>
9 #include <string>
10 #include <vector>
11 
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"
20 
21 namespace ppapi {
22 
23 class PPB_X509Certificate_Fields;
24 class PPB_X509Certificate_Private_Shared;
25 class SocketOptionData;
26 
27 namespace proxy {
28 
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;
38 
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;
49 
50  protected:
51   // C-tor used for new sockets.
52   TCPSocketResourceBase(Connection connection,
53                         PP_Instance instance,
54                         TCPSocketVersion version);
55 
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);
62 
63   virtual ~TCPSocketResourceBase();
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);
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                         scoped_refptr<TrackedCallback> callback);
99 
100   void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback);
101 
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,
116                               int pending_host_id,
117                               const PP_NetAddress_Private& local_addr,
118                               const PP_NetAddress_Private& remote_addr);
119   void OnPluginMsgSetOptionReply(const ResourceMessageReplyParams& params);
120 
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_;
129 
130   TCPSocketState state_;
131   char* read_buffer_;
132   int32_t bytes_to_read_;
133 
134   PP_NetAddress_Private local_addr_;
135   PP_NetAddress_Private remote_addr_;
136 
137   scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_;
138 
139   std::vector<std::vector<char> > trusted_certificates_;
140   std::vector<std::vector<char> > untrusted_certificates_;
141 
142   PP_Resource* accepted_tcp_socket_;
143 
144  private:
145   void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result);
146 
147   TCPSocketVersion version_;
148 
149   DISALLOW_COPY_AND_ASSIGN(TCPSocketResourceBase);
150 };
151 
152 }  // namespace proxy
153 }  // namespace ppapi
154 
155 #endif  // PPAPI_PROXY_TCP_SOCKET_RESOURCE_BASE_H_
156