Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / socket / tcp_socket.h
1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
7
8 #include <string>
9
10 #include "extensions/browser/api/socket/socket.h"
11
12 // This looks like it should be forward-declarable, but it does some tricky
13 // moves that make it easier to just include it.
14 #include "net/socket/tcp_client_socket.h"
15 #include "net/socket/tcp_server_socket.h"
16
17 namespace net {
18 class Socket;
19 }
20
21 namespace extensions {
22
23 class TCPSocket : public Socket {
24  public:
25   explicit TCPSocket(const std::string& owner_extension_id);
26   TCPSocket(net::TCPClientSocket* tcp_client_socket,
27             const std::string& owner_extension_id,
28             bool is_connected = false);
29
30   ~TCPSocket() override;
31
32   void Connect(const std::string& address,
33                int port,
34                const CompletionCallback& callback) override;
35   void Disconnect() override;
36   int Bind(const std::string& address, int port) override;
37   void Read(int count, const ReadCompletionCallback& callback) override;
38   void RecvFrom(int count, const RecvFromCompletionCallback& callback) override;
39   void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
40               int byte_count,
41               const std::string& address,
42               int port,
43               const CompletionCallback& callback) override;
44   bool SetKeepAlive(bool enable, int delay) override;
45   bool SetNoDelay(bool no_delay) override;
46   int Listen(const std::string& address,
47              int port,
48              int backlog,
49              std::string* error_msg) override;
50   void Accept(const AcceptCompletionCallback& callback) override;
51
52   bool IsConnected() override;
53
54   bool GetPeerAddress(net::IPEndPoint* address) override;
55   bool GetLocalAddress(net::IPEndPoint* address) override;
56
57   // Like Disconnect(), only Release() doesn't delete the underlying stream
58   // or attempt to close it. Useful when giving away ownership with
59   // ClientStream().
60   virtual void Release();
61
62   Socket::SocketType GetSocketType() const override;
63
64   static TCPSocket* CreateSocketForTesting(
65       net::TCPClientSocket* tcp_client_socket,
66       const std::string& owner_extension_id,
67       bool is_connected = false);
68   static TCPSocket* CreateServerSocketForTesting(
69       net::TCPServerSocket* tcp_server_socket,
70       const std::string& owner_extension_id);
71
72   // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection
73   // wasn't set up via Connect() (vs Listen()/Accept()).
74   net::TCPClientSocket* ClientStream();
75
76   // Whether a Read() has been issued, that hasn't come back yet.
77   bool HasPendingRead() const;
78
79  protected:
80   int WriteImpl(net::IOBuffer* io_buffer,
81                 int io_buffer_size,
82                 const net::CompletionCallback& callback) override;
83
84  private:
85   void RefreshConnectionStatus();
86   void OnConnectComplete(int result);
87   void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result);
88   void OnAccept(int result);
89
90   TCPSocket(net::TCPServerSocket* tcp_server_socket,
91             const std::string& owner_extension_id);
92
93   scoped_ptr<net::TCPClientSocket> socket_;
94   scoped_ptr<net::TCPServerSocket> server_socket_;
95
96   enum SocketMode { UNKNOWN = 0, CLIENT, SERVER, };
97   SocketMode socket_mode_;
98
99   CompletionCallback connect_callback_;
100
101   ReadCompletionCallback read_callback_;
102
103   scoped_ptr<net::StreamSocket> accept_socket_;
104   AcceptCompletionCallback accept_callback_;
105 };
106
107 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
108 // socket objects with additional properties related to the behavior defined in
109 // the "sockets.tcp" namespace.
110 class ResumableTCPSocket : public TCPSocket {
111  public:
112   explicit ResumableTCPSocket(const std::string& owner_extension_id);
113   explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket,
114                               const std::string& owner_extension_id,
115                               bool is_connected);
116
117   // Overriden from ApiResource
118   bool IsPersistent() const override;
119
120   const std::string& name() const { return name_; }
121   void set_name(const std::string& name) { name_ = name; }
122
123   bool persistent() const { return persistent_; }
124   void set_persistent(bool persistent) { persistent_ = persistent; }
125
126   int buffer_size() const { return buffer_size_; }
127   void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
128
129   bool paused() const { return paused_; }
130   void set_paused(bool paused) { paused_ = paused; }
131
132  private:
133   friend class ApiResourceManager<ResumableTCPSocket>;
134   static const char* service_name() { return "ResumableTCPSocketManager"; }
135
136   // Application-defined string - see sockets_tcp.idl.
137   std::string name_;
138   // Flag indicating whether the socket is left open when the application is
139   // suspended - see sockets_tcp.idl.
140   bool persistent_;
141   // The size of the buffer used to receive data - see sockets_tcp.idl.
142   int buffer_size_;
143   // Flag indicating whether a connected socket blocks its peer from sending
144   // more data - see sockets_tcp.idl.
145   bool paused_;
146 };
147
148 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
149 // regular socket objects with additional properties related to the behavior
150 // defined in the "sockets.tcpServer" namespace.
151 class ResumableTCPServerSocket : public TCPSocket {
152  public:
153   explicit ResumableTCPServerSocket(const std::string& owner_extension_id);
154
155   // Overriden from ApiResource
156   bool IsPersistent() const override;
157
158   const std::string& name() const { return name_; }
159   void set_name(const std::string& name) { name_ = name; }
160
161   bool persistent() const { return persistent_; }
162   void set_persistent(bool persistent) { persistent_ = persistent; }
163
164   bool paused() const { return paused_; }
165   void set_paused(bool paused) { paused_ = paused; }
166
167  private:
168   friend class ApiResourceManager<ResumableTCPServerSocket>;
169   static const char* service_name() {
170     return "ResumableTCPServerSocketManager";
171   }
172
173   // Application-defined string - see sockets_tcp_server.idl.
174   std::string name_;
175   // Flag indicating whether the socket is left open when the application is
176   // suspended - see sockets_tcp_server.idl.
177   bool persistent_;
178   // Flag indicating whether a connected socket blocks its peer from sending
179   // more data - see sockets_tcp_server.idl.
180   bool paused_;
181 };
182
183 }  //  namespace extensions
184
185 #endif  // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_