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_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <queue> | 10 #include <queue> |
9 #include <string> | 11 #include <string> |
10 #include <utility> | 12 #include <utility> |
11 | 13 |
12 #include "base/callback.h" | 14 #include "base/callback.h" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "build/build_config.h" |
14 #include "extensions/browser/api/api_resource.h" | 17 #include "extensions/browser/api/api_resource.h" |
15 #include "extensions/browser/api/api_resource_manager.h" | 18 #include "extensions/browser/api/api_resource_manager.h" |
16 #include "net/base/completion_callback.h" | 19 #include "net/base/completion_callback.h" |
17 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
18 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
19 #include "net/socket/tcp_client_socket.h" | 22 #include "net/socket/tcp_client_socket.h" |
20 | 23 |
21 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
22 #include "extensions/browser/api/socket/app_firewall_hole_manager.h" | 25 #include "extensions/browser/api/socket/app_firewall_hole_manager.h" |
23 #endif // OS_CHROMEOS | 26 #endif // OS_CHROMEOS |
24 | 27 |
25 namespace net { | 28 namespace net { |
26 class AddressList; | 29 class AddressList; |
27 class IPEndPoint; | 30 class IPEndPoint; |
28 class Socket; | 31 class Socket; |
29 } | 32 } |
30 | 33 |
31 namespace extensions { | 34 namespace extensions { |
32 | 35 |
33 typedef base::Callback<void(int)> CompletionCallback; | 36 typedef base::Callback<void(int)> CompletionCallback; |
34 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> | 37 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
35 ReadCompletionCallback; | 38 ReadCompletionCallback; |
36 typedef base::Callback<void(int, | 39 typedef base::Callback<void(int, |
37 scoped_refptr<net::IOBuffer> io_buffer, | 40 scoped_refptr<net::IOBuffer> io_buffer, |
38 const std::string&, | 41 const std::string&, |
39 uint16)> RecvFromCompletionCallback; | 42 uint16_t)> RecvFromCompletionCallback; |
40 typedef base::Callback<void(int, net::TCPClientSocket*)> | 43 typedef base::Callback<void(int, net::TCPClientSocket*)> |
41 AcceptCompletionCallback; | 44 AcceptCompletionCallback; |
42 | 45 |
43 // A Socket wraps a low-level socket and includes housekeeping information that | 46 // A Socket wraps a low-level socket and includes housekeeping information that |
44 // we need to manage it in the context of an extension. | 47 // we need to manage it in the context of an extension. |
45 class Socket : public ApiResource { | 48 class Socket : public ApiResource { |
46 public: | 49 public: |
47 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS }; | 50 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS }; |
48 | 51 |
49 ~Socket() override; | 52 ~Socket() override; |
(...skipping 16 matching lines...) Expand all Loading... |
66 firewall_hole_ = firewall_hole.Pass(); | 69 firewall_hole_ = firewall_hole.Pass(); |
67 } | 70 } |
68 #endif // OS_CHROMEOS | 71 #endif // OS_CHROMEOS |
69 | 72 |
70 // Note: |address| contains the resolved IP address, not the hostname of | 73 // Note: |address| contains the resolved IP address, not the hostname of |
71 // the remote endpoint. In order to upgrade this socket to TLS, callers | 74 // the remote endpoint. In order to upgrade this socket to TLS, callers |
72 // must also supply the hostname of the endpoint via set_hostname(). | 75 // must also supply the hostname of the endpoint via set_hostname(). |
73 virtual void Connect(const net::AddressList& address, | 76 virtual void Connect(const net::AddressList& address, |
74 const CompletionCallback& callback) = 0; | 77 const CompletionCallback& callback) = 0; |
75 virtual void Disconnect() = 0; | 78 virtual void Disconnect() = 0; |
76 virtual int Bind(const std::string& address, uint16 port) = 0; | 79 virtual int Bind(const std::string& address, uint16_t port) = 0; |
77 | 80 |
78 // The |callback| will be called with the number of bytes read into the | 81 // The |callback| will be called with the number of bytes read into the |
79 // buffer, or a negative number if an error occurred. | 82 // buffer, or a negative number if an error occurred. |
80 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; | 83 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; |
81 | 84 |
82 // The |callback| will be called with |byte_count| or a negative number if an | 85 // The |callback| will be called with |byte_count| or a negative number if an |
83 // error occurred. | 86 // error occurred. |
84 void Write(scoped_refptr<net::IOBuffer> io_buffer, | 87 void Write(scoped_refptr<net::IOBuffer> io_buffer, |
85 int byte_count, | 88 int byte_count, |
86 const CompletionCallback& callback); | 89 const CompletionCallback& callback); |
87 | 90 |
88 virtual void RecvFrom(int count, | 91 virtual void RecvFrom(int count, |
89 const RecvFromCompletionCallback& callback) = 0; | 92 const RecvFromCompletionCallback& callback) = 0; |
90 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 93 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
91 int byte_count, | 94 int byte_count, |
92 const net::IPEndPoint& address, | 95 const net::IPEndPoint& address, |
93 const CompletionCallback& callback) = 0; | 96 const CompletionCallback& callback) = 0; |
94 | 97 |
95 virtual bool SetKeepAlive(bool enable, int delay); | 98 virtual bool SetKeepAlive(bool enable, int delay); |
96 virtual bool SetNoDelay(bool no_delay); | 99 virtual bool SetNoDelay(bool no_delay); |
97 virtual int Listen(const std::string& address, | 100 virtual int Listen(const std::string& address, |
98 uint16 port, | 101 uint16_t port, |
99 int backlog, | 102 int backlog, |
100 std::string* error_msg); | 103 std::string* error_msg); |
101 virtual void Accept(const AcceptCompletionCallback& callback); | 104 virtual void Accept(const AcceptCompletionCallback& callback); |
102 | 105 |
103 virtual bool IsConnected() = 0; | 106 virtual bool IsConnected() = 0; |
104 | 107 |
105 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; | 108 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; |
106 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; | 109 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; |
107 | 110 |
108 virtual SocketType GetSocketType() const = 0; | 111 virtual SocketType GetSocketType() const = 0; |
109 | 112 |
110 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, | 113 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
111 uint16 port, | 114 uint16_t port, |
112 net::IPEndPoint* ip_end_point); | 115 net::IPEndPoint* ip_end_point); |
113 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, | 116 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
114 std::string* ip_address_str, | 117 std::string* ip_address_str, |
115 uint16* port); | 118 uint16_t* port); |
116 | 119 |
117 protected: | 120 protected: |
118 explicit Socket(const std::string& owner_extension_id_); | 121 explicit Socket(const std::string& owner_extension_id_); |
119 | 122 |
120 void WriteData(); | 123 void WriteData(); |
121 virtual int WriteImpl(net::IOBuffer* io_buffer, | 124 virtual int WriteImpl(net::IOBuffer* io_buffer, |
122 int io_buffer_size, | 125 int io_buffer_size, |
123 const net::CompletionCallback& callback) = 0; | 126 const net::CompletionCallback& callback) = 0; |
124 virtual void OnWriteComplete(int result); | 127 virtual void OnWriteComplete(int result); |
125 | 128 |
(...skipping 20 matching lines...) Expand all Loading... |
146 #if defined(OS_CHROMEOS) | 149 #if defined(OS_CHROMEOS) |
147 // Represents a hole punched in the system firewall for this socket. | 150 // Represents a hole punched in the system firewall for this socket. |
148 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread> | 151 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread> |
149 firewall_hole_; | 152 firewall_hole_; |
150 #endif // OS_CHROMEOS | 153 #endif // OS_CHROMEOS |
151 }; | 154 }; |
152 | 155 |
153 } // namespace extensions | 156 } // namespace extensions |
154 | 157 |
155 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 158 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
OLD | NEW |