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