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 <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 11 matching lines...) Expand all Loading... |
22 class AddressList; | 22 class AddressList; |
23 class IPEndPoint; | 23 class IPEndPoint; |
24 class Socket; | 24 class Socket; |
25 } | 25 } |
26 | 26 |
27 namespace extensions { | 27 namespace extensions { |
28 | 28 |
29 typedef base::Callback<void(int)> CompletionCallback; | 29 typedef base::Callback<void(int)> CompletionCallback; |
30 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> | 30 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
31 ReadCompletionCallback; | 31 ReadCompletionCallback; |
32 typedef base::Callback< | 32 typedef base::Callback<void(int, |
33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> | 33 scoped_refptr<net::IOBuffer> io_buffer, |
34 RecvFromCompletionCallback; | 34 const std::string&, |
| 35 uint16)> RecvFromCompletionCallback; |
35 typedef base::Callback<void(int, net::TCPClientSocket*)> | 36 typedef base::Callback<void(int, net::TCPClientSocket*)> |
36 AcceptCompletionCallback; | 37 AcceptCompletionCallback; |
37 | 38 |
38 // A Socket wraps a low-level socket and includes housekeeping information that | 39 // A Socket wraps a low-level socket and includes housekeeping information that |
39 // we need to manage it in the context of an extension. | 40 // we need to manage it in the context of an extension. |
40 class Socket : public ApiResource { | 41 class Socket : public ApiResource { |
41 public: | 42 public: |
42 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS }; | 43 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS }; |
43 | 44 |
44 ~Socket() override; | 45 ~Socket() override; |
45 | 46 |
46 // The hostname of the remote host that this socket is connected to. This | 47 // The hostname of the remote host that this socket is connected to. This |
47 // may be the empty string if the client does not intend to ever upgrade the | 48 // may be the empty string if the client does not intend to ever upgrade the |
48 // socket to TLS, and thusly has not invoked set_hostname(). | 49 // socket to TLS, and thusly has not invoked set_hostname(). |
49 const std::string& hostname() const { return hostname_; } | 50 const std::string& hostname() const { return hostname_; } |
50 | 51 |
51 // Set the hostname of the remote host that this socket is connected to. | 52 // Set the hostname of the remote host that this socket is connected to. |
52 // Note: This may be an IP literal. In the case of IDNs, this should be a | 53 // Note: This may be an IP literal. In the case of IDNs, this should be a |
53 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be | 54 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be |
54 // unbracketed. | 55 // unbracketed. |
55 void set_hostname(const std::string& hostname) { hostname_ = hostname; } | 56 void set_hostname(const std::string& hostname) { hostname_ = hostname; } |
56 | 57 |
57 // Note: |address| contains the resolved IP address, not the hostname of | 58 // Note: |address| contains the resolved IP address, not the hostname of |
58 // the remote endpoint. In order to upgrade this socket to TLS, callers | 59 // the remote endpoint. In order to upgrade this socket to TLS, callers |
59 // must also supply the hostname of the endpoint via set_hostname(). | 60 // must also supply the hostname of the endpoint via set_hostname(). |
60 virtual void Connect(const std::string& address, | 61 virtual void Connect(const std::string& address, |
61 int port, | 62 uint16 port, |
62 const CompletionCallback& callback) = 0; | 63 const CompletionCallback& callback) = 0; |
63 virtual void Disconnect() = 0; | 64 virtual void Disconnect() = 0; |
64 virtual int Bind(const std::string& address, int port) = 0; | 65 virtual int Bind(const std::string& address, uint16 port) = 0; |
65 | 66 |
66 // The |callback| will be called with the number of bytes read into the | 67 // The |callback| will be called with the number of bytes read into the |
67 // buffer, or a negative number if an error occurred. | 68 // buffer, or a negative number if an error occurred. |
68 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; | 69 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; |
69 | 70 |
70 // The |callback| will be called with |byte_count| or a negative number if an | 71 // The |callback| will be called with |byte_count| or a negative number if an |
71 // error occurred. | 72 // error occurred. |
72 void Write(scoped_refptr<net::IOBuffer> io_buffer, | 73 void Write(scoped_refptr<net::IOBuffer> io_buffer, |
73 int byte_count, | 74 int byte_count, |
74 const CompletionCallback& callback); | 75 const CompletionCallback& callback); |
75 | 76 |
76 virtual void RecvFrom(int count, | 77 virtual void RecvFrom(int count, |
77 const RecvFromCompletionCallback& callback) = 0; | 78 const RecvFromCompletionCallback& callback) = 0; |
78 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 79 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
79 int byte_count, | 80 int byte_count, |
80 const std::string& address, | 81 const std::string& address, |
81 int port, | 82 uint16 port, |
82 const CompletionCallback& callback) = 0; | 83 const CompletionCallback& callback) = 0; |
83 | 84 |
84 virtual bool SetKeepAlive(bool enable, int delay); | 85 virtual bool SetKeepAlive(bool enable, int delay); |
85 virtual bool SetNoDelay(bool no_delay); | 86 virtual bool SetNoDelay(bool no_delay); |
86 virtual int Listen(const std::string& address, | 87 virtual int Listen(const std::string& address, |
87 int port, | 88 uint16 port, |
88 int backlog, | 89 int backlog, |
89 std::string* error_msg); | 90 std::string* error_msg); |
90 virtual void Accept(const AcceptCompletionCallback& callback); | 91 virtual void Accept(const AcceptCompletionCallback& callback); |
91 | 92 |
92 virtual bool IsConnected() = 0; | 93 virtual bool IsConnected() = 0; |
93 | 94 |
94 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; | 95 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; |
95 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; | 96 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; |
96 | 97 |
97 virtual SocketType GetSocketType() const = 0; | 98 virtual SocketType GetSocketType() const = 0; |
98 | 99 |
99 static bool StringAndPortToAddressList(const std::string& ip_address_str, | 100 static bool StringAndPortToAddressList(const std::string& ip_address_str, |
100 int port, | 101 uint16 port, |
101 net::AddressList* address_list); | 102 net::AddressList* address_list); |
102 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, | 103 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
103 int port, | 104 uint16 port, |
104 net::IPEndPoint* ip_end_point); | 105 net::IPEndPoint* ip_end_point); |
105 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, | 106 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
106 std::string* ip_address_str, | 107 std::string* ip_address_str, |
107 int* port); | 108 uint16* port); |
108 | 109 |
109 protected: | 110 protected: |
110 explicit Socket(const std::string& owner_extension_id_); | 111 explicit Socket(const std::string& owner_extension_id_); |
111 | 112 |
112 void WriteData(); | 113 void WriteData(); |
113 virtual int WriteImpl(net::IOBuffer* io_buffer, | 114 virtual int WriteImpl(net::IOBuffer* io_buffer, |
114 int io_buffer_size, | 115 int io_buffer_size, |
115 const net::CompletionCallback& callback) = 0; | 116 const net::CompletionCallback& callback) = 0; |
116 virtual void OnWriteComplete(int result); | 117 virtual void OnWriteComplete(int result); |
117 | 118 |
(...skipping 14 matching lines...) Expand all Loading... |
132 CompletionCallback callback; | 133 CompletionCallback callback; |
133 int bytes_written; | 134 int bytes_written; |
134 }; | 135 }; |
135 std::queue<WriteRequest> write_queue_; | 136 std::queue<WriteRequest> write_queue_; |
136 scoped_refptr<net::IOBuffer> io_buffer_write_; | 137 scoped_refptr<net::IOBuffer> io_buffer_write_; |
137 }; | 138 }; |
138 | 139 |
139 } // namespace extensions | 140 } // namespace extensions |
140 | 141 |
141 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 142 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
OLD | NEW |