OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ |
6 #define EXTENSIONS_BROWSER_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 "extensions/browser/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 ~TCPSocket() override; |
31 | 31 |
32 virtual void Connect(const std::string& address, | 32 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 void Disconnect() override; |
36 virtual int Bind(const std::string& address, int port) override; | 36 int Bind(const std::string& address, int port) override; |
37 virtual void Read(int count, const ReadCompletionCallback& callback) override; | 37 void Read(int count, const ReadCompletionCallback& callback) override; |
38 virtual void RecvFrom(int count, | 38 void RecvFrom(int count, const RecvFromCompletionCallback& callback) override; |
39 const RecvFromCompletionCallback& callback) override; | 39 void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
40 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 40 int byte_count, |
41 int byte_count, | 41 const std::string& address, |
42 const std::string& address, | 42 int port, |
43 int port, | 43 const CompletionCallback& callback) override; |
44 const CompletionCallback& callback) override; | 44 bool SetKeepAlive(bool enable, int delay) override; |
45 virtual bool SetKeepAlive(bool enable, int delay) override; | 45 bool SetNoDelay(bool no_delay) override; |
46 virtual bool SetNoDelay(bool no_delay) override; | 46 int Listen(const std::string& address, |
47 virtual int Listen(const std::string& address, | 47 int port, |
48 int port, | 48 int backlog, |
49 int backlog, | 49 std::string* error_msg) override; |
50 std::string* error_msg) override; | 50 void Accept(const AcceptCompletionCallback& callback) override; |
51 virtual void Accept(const AcceptCompletionCallback& callback) override; | |
52 | 51 |
53 virtual bool IsConnected() override; | 52 bool IsConnected() override; |
54 | 53 |
55 virtual bool GetPeerAddress(net::IPEndPoint* address) override; | 54 bool GetPeerAddress(net::IPEndPoint* address) override; |
56 virtual bool GetLocalAddress(net::IPEndPoint* address) override; | 55 bool GetLocalAddress(net::IPEndPoint* address) override; |
57 | 56 |
58 // Like Disconnect(), only Release() doesn't delete the underlying stream | 57 // Like Disconnect(), only Release() doesn't delete the underlying stream |
59 // or attempt to close it. Useful when giving away ownership with | 58 // or attempt to close it. Useful when giving away ownership with |
60 // ClientStream(). | 59 // ClientStream(). |
61 virtual void Release(); | 60 virtual void Release(); |
62 | 61 |
63 virtual Socket::SocketType GetSocketType() const override; | 62 Socket::SocketType GetSocketType() const override; |
64 | 63 |
65 static TCPSocket* CreateSocketForTesting( | 64 static TCPSocket* CreateSocketForTesting( |
66 net::TCPClientSocket* tcp_client_socket, | 65 net::TCPClientSocket* tcp_client_socket, |
67 const std::string& owner_extension_id, | 66 const std::string& owner_extension_id, |
68 bool is_connected = false); | 67 bool is_connected = false); |
69 static TCPSocket* CreateServerSocketForTesting( | 68 static TCPSocket* CreateServerSocketForTesting( |
70 net::TCPServerSocket* tcp_server_socket, | 69 net::TCPServerSocket* tcp_server_socket, |
71 const std::string& owner_extension_id); | 70 const std::string& owner_extension_id); |
72 | 71 |
73 // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection | 72 // Returns NULL if GetSocketType() isn't TYPE_TCP or if the connection |
74 // wasn't set up via Connect() (vs Listen()/Accept()). | 73 // wasn't set up via Connect() (vs Listen()/Accept()). |
75 net::TCPClientSocket* ClientStream(); | 74 net::TCPClientSocket* ClientStream(); |
76 | 75 |
77 // Whether a Read() has been issued, that hasn't come back yet. | 76 // Whether a Read() has been issued, that hasn't come back yet. |
78 bool HasPendingRead() const; | 77 bool HasPendingRead() const; |
79 | 78 |
80 protected: | 79 protected: |
81 virtual int WriteImpl(net::IOBuffer* io_buffer, | 80 int WriteImpl(net::IOBuffer* io_buffer, |
82 int io_buffer_size, | 81 int io_buffer_size, |
83 const net::CompletionCallback& callback) override; | 82 const net::CompletionCallback& callback) override; |
84 | 83 |
85 private: | 84 private: |
86 void RefreshConnectionStatus(); | 85 void RefreshConnectionStatus(); |
87 void OnConnectComplete(int result); | 86 void OnConnectComplete(int result); |
88 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result); | 87 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result); |
89 void OnAccept(int result); | 88 void OnAccept(int result); |
90 | 89 |
91 TCPSocket(net::TCPServerSocket* tcp_server_socket, | 90 TCPSocket(net::TCPServerSocket* tcp_server_socket, |
92 const std::string& owner_extension_id); | 91 const std::string& owner_extension_id); |
93 | 92 |
(...skipping 15 matching lines...) Expand all Loading... |
109 // socket objects with additional properties related to the behavior defined in | 108 // socket objects with additional properties related to the behavior defined in |
110 // the "sockets.tcp" namespace. | 109 // the "sockets.tcp" namespace. |
111 class ResumableTCPSocket : public TCPSocket { | 110 class ResumableTCPSocket : public TCPSocket { |
112 public: | 111 public: |
113 explicit ResumableTCPSocket(const std::string& owner_extension_id); | 112 explicit ResumableTCPSocket(const std::string& owner_extension_id); |
114 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, | 113 explicit ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, |
115 const std::string& owner_extension_id, | 114 const std::string& owner_extension_id, |
116 bool is_connected); | 115 bool is_connected); |
117 | 116 |
118 // Overriden from ApiResource | 117 // Overriden from ApiResource |
119 virtual bool IsPersistent() const override; | 118 bool IsPersistent() const override; |
120 | 119 |
121 const std::string& name() const { return name_; } | 120 const std::string& name() const { return name_; } |
122 void set_name(const std::string& name) { name_ = name; } | 121 void set_name(const std::string& name) { name_ = name; } |
123 | 122 |
124 bool persistent() const { return persistent_; } | 123 bool persistent() const { return persistent_; } |
125 void set_persistent(bool persistent) { persistent_ = persistent; } | 124 void set_persistent(bool persistent) { persistent_ = persistent; } |
126 | 125 |
127 int buffer_size() const { return buffer_size_; } | 126 int buffer_size() const { return buffer_size_; } |
128 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } | 127 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } |
129 | 128 |
(...skipping 17 matching lines...) Expand all Loading... |
147 }; | 146 }; |
148 | 147 |
149 // TCP Socket instances from the "sockets.tcpServer" namespace. These are | 148 // TCP Socket instances from the "sockets.tcpServer" namespace. These are |
150 // regular socket objects with additional properties related to the behavior | 149 // regular socket objects with additional properties related to the behavior |
151 // defined in the "sockets.tcpServer" namespace. | 150 // defined in the "sockets.tcpServer" namespace. |
152 class ResumableTCPServerSocket : public TCPSocket { | 151 class ResumableTCPServerSocket : public TCPSocket { |
153 public: | 152 public: |
154 explicit ResumableTCPServerSocket(const std::string& owner_extension_id); | 153 explicit ResumableTCPServerSocket(const std::string& owner_extension_id); |
155 | 154 |
156 // Overriden from ApiResource | 155 // Overriden from ApiResource |
157 virtual bool IsPersistent() const override; | 156 bool IsPersistent() const override; |
158 | 157 |
159 const std::string& name() const { return name_; } | 158 const std::string& name() const { return name_; } |
160 void set_name(const std::string& name) { name_ = name; } | 159 void set_name(const std::string& name) { name_ = name; } |
161 | 160 |
162 bool persistent() const { return persistent_; } | 161 bool persistent() const { return persistent_; } |
163 void set_persistent(bool persistent) { persistent_ = persistent; } | 162 void set_persistent(bool persistent) { persistent_ = persistent; } |
164 | 163 |
165 bool paused() const { return paused_; } | 164 bool paused() const { return paused_; } |
166 void set_paused(bool paused) { paused_ = paused; } | 165 void set_paused(bool paused) { paused_ = paused; } |
167 | 166 |
168 private: | 167 private: |
169 friend class ApiResourceManager<ResumableTCPServerSocket>; | 168 friend class ApiResourceManager<ResumableTCPServerSocket>; |
170 static const char* service_name() { | 169 static const char* service_name() { |
171 return "ResumableTCPServerSocketManager"; | 170 return "ResumableTCPServerSocketManager"; |
172 } | 171 } |
173 | 172 |
174 // Application-defined string - see sockets_tcp_server.idl. | 173 // Application-defined string - see sockets_tcp_server.idl. |
175 std::string name_; | 174 std::string name_; |
176 // Flag indicating whether the socket is left open when the application is | 175 // Flag indicating whether the socket is left open when the application is |
177 // suspended - see sockets_tcp_server.idl. | 176 // suspended - see sockets_tcp_server.idl. |
178 bool persistent_; | 177 bool persistent_; |
179 // Flag indicating whether a connected socket blocks its peer from sending | 178 // Flag indicating whether a connected socket blocks its peer from sending |
180 // more data - see sockets_tcp_server.idl. | 179 // more data - see sockets_tcp_server.idl. |
181 bool paused_; | 180 bool paused_; |
182 }; | 181 }; |
183 | 182 |
184 } // namespace extensions | 183 } // namespace extensions |
185 | 184 |
186 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ | 185 #endif // EXTENSIONS_BROWSER_API_SOCKET_TCP_SOCKET_H_ |
OLD | NEW |