Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_tcp.h
1 // Copyright (c) 2011 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 CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
7
8 #include <queue>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "content/browser/renderer_host/p2p/socket_host.h"
16 #include "content/common/p2p_socket_type.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/ip_endpoint.h"
19
20 namespace net {
21 class DrainableIOBuffer;
22 class GrowableIOBuffer;
23 class StreamSocket;
24 class URLRequestContextGetter;
25 }  // namespace net
26
27 namespace content {
28
29 class CONTENT_EXPORT P2PSocketHostTcpBase : public P2PSocketHost {
30  public:
31   P2PSocketHostTcpBase(IPC::Sender* message_sender,
32                        int socket_id,
33                        P2PSocketType type,
34                        net::URLRequestContextGetter* url_context);
35   ~P2PSocketHostTcpBase() override;
36
37   bool InitAccepted(const net::IPEndPoint& remote_address,
38                     net::StreamSocket* socket);
39
40   // P2PSocketHost overrides.
41   bool Init(const net::IPEndPoint& local_address,
42             const P2PHostAndIPEndPoint& remote_address) override;
43   void Send(const net::IPEndPoint& to,
44             const std::vector<char>& data,
45             const rtc::PacketOptions& options,
46             uint64 packet_id) override;
47   P2PSocketHost* AcceptIncomingTcpConnection(
48       const net::IPEndPoint& remote_address,
49       int id) override;
50   bool SetOption(P2PSocketOption option, int value) override;
51
52  protected:
53   // Derived classes will provide the implementation.
54   virtual int ProcessInput(char* input, int input_len) = 0;
55   virtual void DoSend(const net::IPEndPoint& to,
56                       const std::vector<char>& data,
57                       const rtc::PacketOptions& options) = 0;
58
59   void WriteOrQueue(scoped_refptr<net::DrainableIOBuffer>& buffer);
60   void OnPacket(const std::vector<char>& data);
61   void OnError();
62
63  private:
64   friend class P2PSocketHostTcpTestBase;
65   friend class P2PSocketHostTcpServerTest;
66
67   // SSL/TLS connection functions.
68   void StartTls();
69   void ProcessTlsSslConnectDone(int status);
70
71   void DidCompleteRead(int result);
72   void DoRead();
73
74   void DoWrite();
75   void HandleWriteResult(int result);
76
77   // Callbacks for Connect(), Read() and Write().
78   void OnConnected(int result);
79   void OnRead(int result);
80   void OnWritten(int result);
81
82   // Helper method to send socket create message and start read.
83   void OnOpen();
84   bool DoSendSocketCreateMsg();
85
86   P2PHostAndIPEndPoint remote_address_;
87
88   scoped_ptr<net::StreamSocket> socket_;
89   scoped_refptr<net::GrowableIOBuffer> read_buffer_;
90   std::queue<scoped_refptr<net::DrainableIOBuffer> > write_queue_;
91   scoped_refptr<net::DrainableIOBuffer> write_buffer_;
92
93   bool write_pending_;
94
95   bool connected_;
96   P2PSocketType type_;
97   scoped_refptr<net::URLRequestContextGetter> url_context_;
98
99   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpBase);
100 };
101
102 class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHostTcpBase {
103  public:
104   P2PSocketHostTcp(IPC::Sender* message_sender,
105                    int socket_id,
106                    P2PSocketType type,
107                    net::URLRequestContextGetter* url_context);
108   ~P2PSocketHostTcp() override;
109
110  protected:
111   int ProcessInput(char* input, int input_len) override;
112   void DoSend(const net::IPEndPoint& to,
113               const std::vector<char>& data,
114               const rtc::PacketOptions& options) override;
115
116  private:
117   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp);
118 };
119
120 // P2PSocketHostStunTcp class provides the framing of STUN messages when used
121 // with TURN. These messages will not have length at front of the packet and
122 // are padded to multiple of 4 bytes.
123 // Formatting of messages is defined in RFC5766.
124 class CONTENT_EXPORT P2PSocketHostStunTcp : public P2PSocketHostTcpBase {
125  public:
126   P2PSocketHostStunTcp(IPC::Sender* message_sender,
127                        int socket_id,
128                        P2PSocketType type,
129                        net::URLRequestContextGetter* url_context);
130
131   ~P2PSocketHostStunTcp() override;
132
133  protected:
134   int ProcessInput(char* input, int input_len) override;
135   void DoSend(const net::IPEndPoint& to,
136               const std::vector<char>& data,
137               const rtc::PacketOptions& options) override;
138
139  private:
140   int GetExpectedPacketSize(const char* data, int len, int* pad_bytes);
141
142   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostStunTcp);
143 };
144
145
146 }  // namespace content
147
148 #endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_