[email protected] | 64c82073 | 2012-01-05 20:50:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 7 | |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 8 | #include <queue> |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 9 | #include <string> |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 10 | #include <utility> |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 11 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | 7856620 | 2012-05-14 20:46:51 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 58edca5 | 2012-02-16 21:51:38 | [diff] [blame] | 14 | #include "chrome/browser/extensions/api/api_resource.h" |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 15 | #include "net/base/completion_callback.h" |
[email protected] | 64c82073 | 2012-01-05 20:50:34 | [diff] [blame] | 16 | #include "net/base/io_buffer.h" |
[email protected] | 356127d | 2012-08-07 00:29:53 | [diff] [blame] | 17 | #include "net/base/ip_endpoint.h" |
[email protected] | 1be30b4 | 2012-09-28 01:34:10 | [diff] [blame] | 18 | #include "net/socket/tcp_client_socket.h" |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 19 | |
| 20 | namespace net { |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 21 | class AddressList; |
| 22 | class IPEndPoint; |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 23 | class Socket; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | namespace extensions { |
| 27 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 28 | typedef base::Callback<void(int)> CompletionCallback; |
| 29 | typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
| 30 | ReadCompletionCallback; |
| 31 | typedef base::Callback< |
| 32 | void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
| 33 | RecvFromCompletionCallback; |
[email protected] | 1be30b4 | 2012-09-28 01:34:10 | [diff] [blame] | 34 | typedef base::Callback< |
| 35 | void(int, net::TCPClientSocket*)> AcceptCompletionCallback; |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 36 | |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 37 | // A Socket wraps a low-level socket and includes housekeeping information that |
| 38 | // we need to manage it in the context of an extension. |
[email protected] | 931186e0 | 2012-07-20 01:22:06 | [diff] [blame] | 39 | class Socket : public ApiResource { |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 40 | public: |
[email protected] | 1d8b79a | 2012-08-16 20:22:54 | [diff] [blame] | 41 | enum SocketType { |
| 42 | TYPE_TCP, |
| 43 | TYPE_UDP, |
| 44 | }; |
| 45 | |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 46 | virtual ~Socket(); |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 47 | virtual void Connect(const std::string& address, |
| 48 | int port, |
| 49 | const CompletionCallback& callback) = 0; |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 50 | virtual void Disconnect() = 0; |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 51 | virtual int Bind(const std::string& address, int port) = 0; |
| 52 | |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 53 | // The |callback| will be called with the number of bytes read into the |
| 54 | // buffer, or a negative number if an error occurred. |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 55 | virtual void Read(int count, |
| 56 | const ReadCompletionCallback& callback) = 0; |
[email protected] | 64c82073 | 2012-01-05 20:50:34 | [diff] [blame] | 57 | |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 58 | // The |callback| will be called with |byte_count| or a negative number if an |
| 59 | // error occurred. |
| 60 | void Write(scoped_refptr<net::IOBuffer> io_buffer, |
| 61 | int byte_count, |
| 62 | const CompletionCallback& callback); |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 63 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 64 | virtual void RecvFrom(int count, |
| 65 | const RecvFromCompletionCallback& callback) = 0; |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame] | 66 | virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 67 | int byte_count, |
| 68 | const std::string& address, |
| 69 | int port, |
| 70 | const CompletionCallback& callback) = 0; |
[email protected] | 820b984a | 2012-05-29 23:15:06 | [diff] [blame] | 71 | |
| 72 | virtual bool SetKeepAlive(bool enable, int delay); |
| 73 | virtual bool SetNoDelay(bool no_delay); |
[email protected] | 1be30b4 | 2012-09-28 01:34:10 | [diff] [blame] | 74 | virtual int Listen(const std::string& address, int port, int backlog, |
| 75 | std::string* error_msg); |
| 76 | virtual void Accept(const AcceptCompletionCallback &callback); |
[email protected] | 820b984a | 2012-05-29 23:15:06 | [diff] [blame] | 77 | |
[email protected] | 356127d | 2012-08-07 00:29:53 | [diff] [blame] | 78 | bool IsConnected(); |
[email protected] | 356127d | 2012-08-07 00:29:53 | [diff] [blame] | 79 | |
| 80 | virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; |
| 81 | virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; |
| 82 | |
[email protected] | 1d8b79a | 2012-08-16 20:22:54 | [diff] [blame] | 83 | virtual SocketType GetSocketType() const = 0; |
| 84 | |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 85 | static bool StringAndPortToAddressList(const std::string& ip_address_str, |
| 86 | int port, |
| 87 | net::AddressList* address_list); |
| 88 | static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
| 89 | int port, |
| 90 | net::IPEndPoint* ip_end_point); |
| 91 | static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
| 92 | std::string* ip_address_str, |
| 93 | int* port); |
| 94 | |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 95 | protected: |
[email protected] | 41cacf3 | 2012-10-11 00:06:24 | [diff] [blame] | 96 | explicit Socket(const std::string& owner_extension_id_); |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 97 | |
| 98 | void WriteData(); |
| 99 | virtual int WriteImpl(net::IOBuffer* io_buffer, |
| 100 | int io_buffer_size, |
| 101 | const net::CompletionCallback& callback) = 0; |
| 102 | virtual void OnWriteComplete(int result); |
| 103 | |
[email protected] | 58edca5 | 2012-02-16 21:51:38 | [diff] [blame] | 104 | const std::string address_; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 105 | bool is_connected_; |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | struct WriteRequest { |
| 109 | WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, |
| 110 | int byte_count, |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame] | 111 | const CompletionCallback& callback); |
| 112 | ~WriteRequest(); |
[email protected] | 1a52af5 | 2012-05-18 19:28:25 | [diff] [blame] | 113 | scoped_refptr<net::IOBuffer> io_buffer; |
| 114 | int byte_count; |
| 115 | CompletionCallback callback; |
| 116 | int bytes_written; |
| 117 | }; |
| 118 | std::queue<WriteRequest> write_queue_; |
| 119 | scoped_refptr<net::IOBuffer> io_buffer_write_; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace extensions |
| 123 | |
| 124 | #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |