OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "chrome/browser/extensions/api/socket/socket.h" | 10 #include "extensions/browser/api/socket/socket.h" |
11 | 11 |
12 // This looks like it should be forward-declarable, but it does some tricky | 12 // This looks like it should be forward-declarable, but it does some tricky |
13 // moves that make it easier to just include it. | 13 // moves that make it easier to just include it. |
14 #include "net/socket/tcp_client_socket.h" | 14 #include "net/socket/tcp_client_socket.h" |
15 #include "net/socket/tcp_server_socket.h" | 15 #include "net/socket/tcp_server_socket.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 class Socket; | 18 class Socket; |
19 } | 19 } |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 class TCPSocket : public Socket { | 23 class TCPSocket : public Socket { |
24 public: | 24 public: |
25 explicit TCPSocket(const std::string& owner_extension_id); | 25 explicit TCPSocket(const std::string& owner_extension_id); |
26 TCPSocket(net::TCPClientSocket* tcp_client_socket, | 26 TCPSocket(net::TCPClientSocket* tcp_client_socket, |
27 const std::string& owner_extension_id, | 27 const std::string& owner_extension_id, |
28 bool is_connected = false); | 28 bool is_connected = false); |
29 | 29 |
30 virtual ~TCPSocket(); | 30 virtual ~TCPSocket(); |
31 | 31 |
32 virtual void Connect(const std::string& address, | 32 virtual void Connect(const std::string& address, |
33 int port, | 33 int port, |
34 const CompletionCallback& callback) OVERRIDE; | 34 const CompletionCallback& callback) OVERRIDE; |
35 virtual void Disconnect() OVERRIDE; | 35 virtual void Disconnect() OVERRIDE; |
36 virtual int Bind(const std::string& address, int port) OVERRIDE; | 36 virtual int Bind(const std::string& address, int port) OVERRIDE; |
37 virtual void Read(int count, | 37 virtual void Read(int count, const ReadCompletionCallback& callback) OVERRIDE; |
38 const ReadCompletionCallback& callback) OVERRIDE; | |
39 virtual void RecvFrom(int count, | 38 virtual void RecvFrom(int count, |
40 const RecvFromCompletionCallback& callback) OVERRIDE; | 39 const RecvFromCompletionCallback& callback) OVERRIDE; |
41 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 40 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
42 int byte_count, | 41 int byte_count, |
43 const std::string& address, | 42 const std::string& address, |
44 int port, | 43 int port, |
45 const CompletionCallback& callback) OVERRIDE; | 44 const CompletionCallback& callback) OVERRIDE; |
46 virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE; | 45 virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE; |
47 virtual bool SetNoDelay(bool no_delay) OVERRIDE; | 46 virtual bool SetNoDelay(bool no_delay) OVERRIDE; |
48 virtual int Listen(const std::string& address, int port, | 47 virtual int Listen(const std::string& address, |
49 int backlog, std::string* error_msg) OVERRIDE; | 48 int port, |
50 virtual void Accept(const AcceptCompletionCallback &callback) OVERRIDE; | 49 int backlog, |
| 50 std::string* error_msg) OVERRIDE; |
| 51 virtual void Accept(const AcceptCompletionCallback& callback) OVERRIDE; |
51 | 52 |
52 virtual bool IsConnected() OVERRIDE; | 53 virtual bool IsConnected() OVERRIDE; |
53 | 54 |
54 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE; | 55 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE; |
55 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE; | 56 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE; |
56 virtual Socket::SocketType GetSocketType() const OVERRIDE; | 57 virtual Socket::SocketType GetSocketType() const OVERRIDE; |
57 | 58 |
58 static TCPSocket* CreateSocketForTesting( | 59 static TCPSocket* CreateSocketForTesting( |
59 net::TCPClientSocket* tcp_client_socket, | 60 net::TCPClientSocket* tcp_client_socket, |
60 const std::string& owner_extension_id, | 61 const std::string& owner_extension_id, |
61 bool is_connected = false); | 62 bool is_connected = false); |
62 static TCPSocket* CreateServerSocketForTesting( | 63 static TCPSocket* CreateServerSocketForTesting( |
63 net::TCPServerSocket* tcp_server_socket, | 64 net::TCPServerSocket* tcp_server_socket, |
64 const std::string& owner_extension_id); | 65 const std::string& owner_extension_id); |
65 | 66 |
66 protected: | 67 protected: |
67 virtual int WriteImpl(net::IOBuffer* io_buffer, | 68 virtual int WriteImpl(net::IOBuffer* io_buffer, |
68 int io_buffer_size, | 69 int io_buffer_size, |
69 const net::CompletionCallback& callback) OVERRIDE; | 70 const net::CompletionCallback& callback) OVERRIDE; |
70 | 71 |
71 private: | 72 private: |
72 void RefreshConnectionStatus(); | 73 void RefreshConnectionStatus(); |
73 void OnConnectComplete(int result); | 74 void OnConnectComplete(int result); |
74 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | 75 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result); |
75 int result); | |
76 void OnAccept(int result); | 76 void OnAccept(int result); |
77 | 77 |
78 TCPSocket(net::TCPServerSocket* tcp_server_socket, | 78 TCPSocket(net::TCPServerSocket* tcp_server_socket, |
79 const std::string& owner_extension_id); | 79 const std::string& owner_extension_id); |
80 | 80 |
81 scoped_ptr<net::TCPClientSocket> socket_; | 81 scoped_ptr<net::TCPClientSocket> socket_; |
82 scoped_ptr<net::TCPServerSocket> server_socket_; | 82 scoped_ptr<net::TCPServerSocket> server_socket_; |
83 | 83 |
84 enum SocketMode { | 84 enum SocketMode { UNKNOWN = 0, CLIENT, SERVER, }; |
85 UNKNOWN = 0, | |
86 CLIENT, | |
87 SERVER, | |
88 }; | |
89 SocketMode socket_mode_; | 85 SocketMode socket_mode_; |
90 | 86 |
91 CompletionCallback connect_callback_; | 87 CompletionCallback connect_callback_; |
92 | 88 |
93 ReadCompletionCallback read_callback_; | 89 ReadCompletionCallback read_callback_; |
94 | 90 |
95 scoped_ptr<net::StreamSocket> accept_socket_; | 91 scoped_ptr<net::StreamSocket> accept_socket_; |
96 AcceptCompletionCallback accept_callback_; | 92 AcceptCompletionCallback accept_callback_; |
97 }; | 93 }; |
98 | 94 |
99 // TCP Socket instances from the "sockets.tcp" namespace. These are regular | 95 // TCP Socket instances from the "sockets.tcp" namespace. These are regular |
100 // socket objects with additional properties related to the behavior defined in | 96 // socket objects with additional properties related to the behavior defined in |
101 // the "sockets.tcp" namespace. | 97 // the "sockets.tcp" namespace. |
102 class ResumableTCPSocket : public TCPSocket { | 98 class ResumableTCPSocket : public TCPSocket { |
103 public: | 99 public: |
104 explicit ResumableTCPSocket(const std::string& owner_extension_id); | 100 explicit ResumableTCPSocket(const std::string& owner_extension_id); |
105 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, | 101 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, |
106 const std::string& owner_extension_id, | 102 const std::string& owner_extension_id, |
107 bool is_connected); | 103 bool is_connected); |
108 | 104 |
109 // Overriden from ApiResource | 105 // Overriden from ApiResource |
110 virtual bool IsPersistent() const OVERRIDE; | 106 virtual bool IsPersistent() const OVERRIDE; |
111 | 107 |
112 const std::string& name() const { return name_; } | 108 const std::string& name() const { return name_; } |
113 void set_name(const std::string& name) { name_ = name; } | 109 void set_name(const std::string& name) { name_ = name; } |
114 | 110 |
115 bool persistent() const { return persistent_; } | 111 bool persistent() const { return persistent_; } |
116 void set_persistent(bool persistent) { persistent_ = persistent; } | 112 void set_persistent(bool persistent) { persistent_ = persistent; } |
117 | 113 |
118 int buffer_size() const { return buffer_size_; } | 114 int buffer_size() const { return buffer_size_; } |
119 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } | 115 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } |
120 | 116 |
121 bool paused() const { return paused_; } | 117 bool paused() const { return paused_; } |
122 void set_paused(bool paused) { paused_ = paused; } | 118 void set_paused(bool paused) { paused_ = paused; } |
123 | 119 |
124 private: | 120 private: |
125 friend class ApiResourceManager<ResumableTCPSocket>; | 121 friend class ApiResourceManager<ResumableTCPSocket>; |
126 static const char* service_name() { | 122 static const char* service_name() { return "ResumableTCPSocketManager"; } |
127 return "ResumableTCPSocketManager"; | |
128 } | |
129 | 123 |
130 // Application-defined string - see sockets_tcp.idl. | 124 // Application-defined string - see sockets_tcp.idl. |
131 std::string name_; | 125 std::string name_; |
132 // Flag indicating whether the socket is left open when the application is | 126 // Flag indicating whether the socket is left open when the application is |
133 // suspended - see sockets_tcp.idl. | 127 // suspended - see sockets_tcp.idl. |
134 bool persistent_; | 128 bool persistent_; |
135 // The size of the buffer used to receive data - see sockets_tcp.idl. | 129 // The size of the buffer used to receive data - see sockets_tcp.idl. |
136 int buffer_size_; | 130 int buffer_size_; |
137 // Flag indicating whether a connected socket blocks its peer from sending | 131 // Flag indicating whether a connected socket blocks its peer from sending |
138 // more data - see sockets_tcp.idl. | 132 // more data - see sockets_tcp.idl. |
(...skipping 30 matching lines...) Expand all Loading... |
169 // Flag indicating whether the socket is left open when the application is | 163 // Flag indicating whether the socket is left open when the application is |
170 // suspended - see sockets_tcp_server.idl. | 164 // suspended - see sockets_tcp_server.idl. |
171 bool persistent_; | 165 bool persistent_; |
172 // Flag indicating whether a connected socket blocks its peer from sending | 166 // Flag indicating whether a connected socket blocks its peer from sending |
173 // more data - see sockets_tcp_server.idl. | 167 // more data - see sockets_tcp_server.idl. |
174 bool paused_; | 168 bool paused_; |
175 }; | 169 }; |
176 | 170 |
177 } // namespace extensions | 171 } // namespace extensions |
178 | 172 |
179 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TCP_SOCKET_H_ | 173 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ |
OLD | NEW |