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