[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_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame^] | 11 | #include "base/callback.h" |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 58edca5 | 2012-02-16 21:51:38 | [diff] [blame] | 13 | #include "chrome/browser/extensions/api/api_resource.h" |
[email protected] | 64c82073 | 2012-01-05 20:50:34 | [diff] [blame] | 14 | #include "net/base/io_buffer.h" |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 15 | |
| 16 | namespace net { |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 17 | class AddressList; |
| 18 | class IPEndPoint; |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 19 | class Socket; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | namespace extensions { |
| 23 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame^] | 24 | typedef base::Callback<void(int)> CompletionCallback; |
| 25 | typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
| 26 | ReadCompletionCallback; |
| 27 | typedef base::Callback< |
| 28 | void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
| 29 | RecvFromCompletionCallback; |
| 30 | |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 31 | // A Socket wraps a low-level socket and includes housekeeping information that |
| 32 | // we need to manage it in the context of an extension. |
[email protected] | 58edca5 | 2012-02-16 21:51:38 | [diff] [blame] | 33 | class Socket : public APIResource { |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 34 | public: |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 35 | virtual ~Socket(); |
| 36 | |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 37 | // Returns net::OK if successful, or an error code otherwise. |
[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] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 42 | |
[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] | 097fef76 | 2012-04-17 02:07:47 | [diff] [blame] | 45 | // Returns the number of bytes read into the buffer, or a negative number if |
| 46 | // 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] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 50 | // Returns the number of bytes successfully written, or a negative error |
| 51 | // code. Note that ERR_IO_PENDING means that the operation blocked, in which |
[email protected] | 097fef76 | 2012-04-17 02:07:47 | [diff] [blame] | 52 | // case |event_notifier| (supplied at socket creation) will eventually be |
| 53 | // called with the final result (again, either a nonnegative number of bytes |
| 54 | // written, or a negative error). |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame^] | 55 | virtual void Write(scoped_refptr<net::IOBuffer> io_buffer, |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 56 | int byte_count, |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame^] | 57 | const CompletionCallback& callback) = 0; |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 58 | |
[email protected] | 022bf89 | 2012-05-07 20:10:06 | [diff] [blame^] | 59 | virtual void RecvFrom(int count, |
| 60 | const RecvFromCompletionCallback& callback) = 0; |
| 61 | |
| 62 | virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 63 | int byte_count, |
| 64 | const std::string& address, |
| 65 | int port, |
| 66 | const CompletionCallback& callback) = 0; |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 67 | |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 68 | static bool StringAndPortToAddressList(const std::string& ip_address_str, |
| 69 | int port, |
| 70 | net::AddressList* address_list); |
| 71 | static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
| 72 | int port, |
| 73 | net::IPEndPoint* ip_end_point); |
| 74 | static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
| 75 | std::string* ip_address_str, |
| 76 | int* port); |
| 77 | |
[email protected] | d47c11f | 2012-01-26 18:15:04 | [diff] [blame] | 78 | protected: |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 79 | explicit Socket(APIResourceEventNotifier* event_notifier); |
[email protected] | 58edca5 | 2012-02-16 21:51:38 | [diff] [blame] | 80 | const std::string address_; |
| 81 | int port_; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 82 | bool is_connected_; |
[email protected] | fc44a4bef | 2011-12-16 01:18:42 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace extensions |
| 86 | |
| 87 | #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |