summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/tcp_socket_shared.h
blob: bd5e56b781efdf85b6956c9514dedd597cae6e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_
#define PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_

#include <string>
#include <vector>

#include "base/compiler_specific.h"
#include "ppapi/c/private/ppb_tcp_socket_private.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/tracked_callback.h"

namespace ppapi {

class PPB_X509Certificate_Fields;
class PPB_X509Certificate_Private_Shared;

// This class provides the shared implementation for both PPB_TCPSocket and
// PPB_TCPSocket_Private.
class PPAPI_SHARED_EXPORT TCPSocketShared {
 public:
  // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Read
  // message is allowed to request.
  static const int32_t kMaxReadSize;
  // The maximum number of bytes that each PpapiHostMsg_PPBTCPSocket_Write
  // message is allowed to carry.
  static const int32_t kMaxWriteSize;

  // Notifications on operations completion.
  void OnConnectCompleted(bool succeeded,
                          const PP_NetAddress_Private& local_addr,
                          const PP_NetAddress_Private& remote_addr);
  void OnSSLHandshakeCompleted(
      bool succeeded,
      const PPB_X509Certificate_Fields& certificate_fields);
  void OnReadCompleted(bool succeeded, const std::string& data);
  void OnWriteCompleted(bool succeeded, int32_t bytes_written);
  void OnSetOptionCompleted(bool succeeded);

  // Send functions that need to be implemented differently for the
  // proxied and non-proxied derived classes.
  virtual void SendConnect(const std::string& host, uint16_t port) = 0;
  virtual void SendConnectWithNetAddress(const PP_NetAddress_Private& addr) = 0;
  virtual void SendSSLHandshake(
      const std::string& server_name,
      uint16_t server_port,
      const std::vector<std::vector<char> >& trusted_certs,
      const std::vector<std::vector<char> >& untrusted_certs) = 0;
  virtual void SendRead(int32_t bytes_to_read) = 0;
  virtual void SendWrite(const std::string& buffer) = 0;
  virtual void SendDisconnect() = 0;
  virtual void SendSetBoolOption(PP_TCPSocketOption_Private name,
                                 bool value) = 0;

  virtual Resource* GetOwnerResource() = 0;

 protected:
  enum ConnectionState {
    // Before a connection is successfully established (including a connect
    // request is pending or a previous connect request failed).
    BEFORE_CONNECT,
    // A connection has been successfully established (including a request of
    // initiating SSL is pending).
    CONNECTED,
    // An SSL connection has been successfully established.
    SSL_CONNECTED,
    // The connection has been ended.
    DISCONNECTED
  };

  TCPSocketShared(ResourceObjectType resource_type, uint32 socket_id);
  virtual ~TCPSocketShared();

  int32_t ConnectImpl(const char* host,
                      uint16_t port,
                      scoped_refptr<TrackedCallback> callback);
  int32_t ConnectWithNetAddressImpl(const PP_NetAddress_Private* addr,
                                    scoped_refptr<TrackedCallback> callback);
  PP_Bool GetLocalAddressImpl(PP_NetAddress_Private* local_addr);
  PP_Bool GetRemoteAddressImpl(PP_NetAddress_Private* remote_addr);
  int32_t SSLHandshakeImpl(const char* server_name,
                           uint16_t server_port,
                           scoped_refptr<TrackedCallback> callback);
  PP_Resource GetServerCertificateImpl();
  PP_Bool AddChainBuildingCertificateImpl(PP_Resource certificate,
                                          PP_Bool trusted);
  int32_t ReadImpl(char* buffer,
                   int32_t bytes_to_read,
                   scoped_refptr<TrackedCallback> callback);
  int32_t WriteImpl(const char* buffer,
                    int32_t bytes_to_write,
                    scoped_refptr<TrackedCallback> callback);
  void DisconnectImpl();
  int32_t SetOptionImpl(PP_TCPSocketOption_Private name,
                        const PP_Var& value,
                        scoped_refptr<TrackedCallback> callback);

  void Init(uint32 socket_id);
  bool IsConnected() const;
  void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback);

  ResourceObjectType resource_type_;

  uint32 socket_id_;
  ConnectionState connection_state_;

  scoped_refptr<TrackedCallback> connect_callback_;
  scoped_refptr<TrackedCallback> ssl_handshake_callback_;
  scoped_refptr<TrackedCallback> read_callback_;
  scoped_refptr<TrackedCallback> write_callback_;
  scoped_refptr<TrackedCallback> set_option_callback_;

  char* read_buffer_;
  int32_t bytes_to_read_;

  PP_NetAddress_Private local_addr_;
  PP_NetAddress_Private remote_addr_;

  scoped_refptr<PPB_X509Certificate_Private_Shared> server_certificate_;

  std::vector<std::vector<char> > trusted_certificates_;
  std::vector<std::vector<char> > untrusted_certificates_;

 private:
  DISALLOW_COPY_AND_ASSIGN(TCPSocketShared);
};

}  // namespace ppapi

#endif  // PPAPI_SHARED_IMPL_TCP_SOCKET_SHARED_H_