• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   virtual ~TCPSocket();
31 
32   virtual void Connect(const std::string& address,
33                        int port,
34                        const CompletionCallback& callback) OVERRIDE;
35   virtual void Disconnect() OVERRIDE;
36   virtual int Bind(const std::string& address, int port) OVERRIDE;
37   virtual void Read(int count, const ReadCompletionCallback& callback) OVERRIDE;
38   virtual void RecvFrom(int count,
39                         const RecvFromCompletionCallback& callback) OVERRIDE;
40   virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
41                       int byte_count,
42                       const std::string& address,
43                       int port,
44                       const CompletionCallback& callback) OVERRIDE;
45   virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE;
46   virtual bool SetNoDelay(bool no_delay) OVERRIDE;
47   virtual int Listen(const std::string& address,
48                      int port,
49                      int backlog,
50                      std::string* error_msg) OVERRIDE;
51   virtual void Accept(const AcceptCompletionCallback& callback) OVERRIDE;
52 
53   virtual bool IsConnected() OVERRIDE;
54 
55   virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE;
56   virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE;
57   virtual Socket::SocketType GetSocketType() const OVERRIDE;
58 
59   static TCPSocket* CreateSocketForTesting(
60       net::TCPClientSocket* tcp_client_socket,
61       const std::string& owner_extension_id,
62       bool is_connected = false);
63   static TCPSocket* CreateServerSocketForTesting(
64       net::TCPServerSocket* tcp_server_socket,
65       const std::string& owner_extension_id);
66 
67  protected:
68   virtual int WriteImpl(net::IOBuffer* io_buffer,
69                         int io_buffer_size,
70                         const net::CompletionCallback& callback) OVERRIDE;
71 
72  private:
73   void RefreshConnectionStatus();
74   void OnConnectComplete(int result);
75   void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result);
76   void OnAccept(int result);
77 
78   TCPSocket(net::TCPServerSocket* tcp_server_socket,
79             const std::string& owner_extension_id);
80 
81   scoped_ptr<net::TCPClientSocket> socket_;
82   scoped_ptr<net::TCPServerSocket> server_socket_;
83 
84   enum SocketMode { UNKNOWN = 0, CLIENT, SERVER, };
85   SocketMode socket_mode_;
86 
87   CompletionCallback connect_callback_;
88 
89   ReadCompletionCallback read_callback_;
90 
91   scoped_ptr<net::StreamSocket> accept_socket_;
92   AcceptCompletionCallback accept_callback_;
93 };
94 
95 // TCP Socket instances from the "sockets.tcp" namespace. These are regular
96 // socket objects with additional properties related to the behavior defined in
97 // the "sockets.tcp" namespace.
98 class ResumableTCPSocket : public TCPSocket {
99  public:
100   explicit ResumableTCPSocket(const std::string& owner_extension_id);
101   explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket,
102                               const std::string& owner_extension_id,
103                               bool is_connected);
104 
105   // Overriden from ApiResource
106   virtual bool IsPersistent() const OVERRIDE;
107 
name()108   const std::string& name() const { return name_; }
set_name(const std::string & name)109   void set_name(const std::string& name) { name_ = name; }
110 
persistent()111   bool persistent() const { return persistent_; }
set_persistent(bool persistent)112   void set_persistent(bool persistent) { persistent_ = persistent; }
113 
buffer_size()114   int buffer_size() const { return buffer_size_; }
set_buffer_size(int buffer_size)115   void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
116 
paused()117   bool paused() const { return paused_; }
set_paused(bool paused)118   void set_paused(bool paused) { paused_ = paused; }
119 
120  private:
121   friend class ApiResourceManager<ResumableTCPSocket>;
service_name()122   static const char* service_name() { return "ResumableTCPSocketManager"; }
123 
124   // Application-defined string - see sockets_tcp.idl.
125   std::string name_;
126   // Flag indicating whether the socket is left open when the application is
127   // suspended - see sockets_tcp.idl.
128   bool persistent_;
129   // The size of the buffer used to receive data - see sockets_tcp.idl.
130   int buffer_size_;
131   // Flag indicating whether a connected socket blocks its peer from sending
132   // more data - see sockets_tcp.idl.
133   bool paused_;
134 };
135 
136 // TCP Socket instances from the "sockets.tcpServer" namespace. These are
137 // regular socket objects with additional properties related to the behavior
138 // defined in the "sockets.tcpServer" namespace.
139 class ResumableTCPServerSocket : public TCPSocket {
140  public:
141   explicit ResumableTCPServerSocket(const std::string& owner_extension_id);
142 
143   // Overriden from ApiResource
144   virtual bool IsPersistent() const OVERRIDE;
145 
name()146   const std::string& name() const { return name_; }
set_name(const std::string & name)147   void set_name(const std::string& name) { name_ = name; }
148 
persistent()149   bool persistent() const { return persistent_; }
set_persistent(bool persistent)150   void set_persistent(bool persistent) { persistent_ = persistent; }
151 
paused()152   bool paused() const { return paused_; }
set_paused(bool paused)153   void set_paused(bool paused) { paused_ = paused; }
154 
155  private:
156   friend class ApiResourceManager<ResumableTCPServerSocket>;
service_name()157   static const char* service_name() {
158     return "ResumableTCPServerSocketManager";
159   }
160 
161   // Application-defined string - see sockets_tcp_server.idl.
162   std::string name_;
163   // Flag indicating whether the socket is left open when the application is
164   // suspended - see sockets_tcp_server.idl.
165   bool persistent_;
166   // Flag indicating whether a connected socket blocks its peer from sending
167   // more data - see sockets_tcp_server.idl.
168   bool paused_;
169 };
170 
171 }  //  namespace extensions
172 
173 #endif  // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_
174