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