| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/extensions/api/api_resource.h" | 12 #include "chrome/browser/extensions/api/api_resource.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 class AddressList; |
| 17 class IPEndPoint; |
| 16 class Socket; | 18 class Socket; |
| 17 } | 19 } |
| 18 | 20 |
| 19 namespace extensions { | 21 namespace extensions { |
| 20 | 22 |
| 21 // A Socket wraps a low-level socket and includes housekeeping information that | 23 // A Socket wraps a low-level socket and includes housekeeping information that |
| 22 // we need to manage it in the context of an extension. | 24 // we need to manage it in the context of an extension. |
| 23 class Socket : public APIResource { | 25 class Socket : public APIResource { |
| 24 public: | 26 public: |
| 25 virtual ~Socket(); | 27 virtual ~Socket(); |
| 26 | 28 |
| 27 // Returns true iff the socket was able to properly initialize itself. | 29 // Returns true iff the socket was able to properly initialize itself. |
| 28 virtual bool IsValid() = 0; | 30 virtual bool IsValid() = 0; |
| 29 | 31 |
| 30 // Returns net::OK if successful, or an error code otherwise. | 32 // Returns net::OK if successful, or an error code otherwise. |
| 31 virtual int Connect() = 0; | 33 virtual int Connect(const std::string& address, int port) = 0; |
| 32 virtual void Disconnect() = 0; | 34 virtual void Disconnect() = 0; |
| 33 | 35 |
| 36 virtual int Bind(const std::string& address, int port) = 0; |
| 37 |
| 34 // Returns the number of bytes read into the buffer, or a negative number if | 38 // Returns the number of bytes read into the buffer, or a negative number if |
| 35 // an error occurred. | 39 // an error occurred. |
| 36 virtual int Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_size); | 40 virtual int Read(scoped_refptr<net::IOBuffer> io_buffer, |
| 41 int io_buffer_size) = 0; |
| 37 | 42 |
| 38 // Returns the number of bytes successfully written, or a negative error | 43 // Returns the number of bytes successfully written, or a negative error |
| 39 // code. Note that ERR_IO_PENDING means that the operation blocked, in which | 44 // code. Note that ERR_IO_PENDING means that the operation blocked, in which |
| 40 // case |event_notifier| (supplied at socket creation) will eventually be | 45 // case |event_notifier| (supplied at socket creation) will eventually be |
| 41 // called with the final result (again, either a nonnegative number of bytes | 46 // called with the final result (again, either a nonnegative number of bytes |
| 42 // written, or a negative error). | 47 // written, or a negative error). |
| 43 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int bytes); | 48 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, |
| 49 int byte_count) = 0; |
| 44 | 50 |
| 45 virtual void OnDataRead(scoped_refptr<net::IOBuffer> io_buffer, int result); | 51 virtual int RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, |
| 52 int io_buffer_size, |
| 53 net::IPEndPoint *address) = 0; |
| 54 virtual int SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 55 int byte_count, |
| 56 const std::string& address, |
| 57 int port) = 0; |
| 58 |
| 59 virtual void OnDataRead(scoped_refptr<net::IOBuffer> io_buffer, |
| 60 net::IPEndPoint *address, |
| 61 int result); |
| 46 virtual void OnWriteComplete(int result); | 62 virtual void OnWriteComplete(int result); |
| 47 | 63 |
| 64 static bool StringAndPortToAddressList(const std::string& ip_address_str, |
| 65 int port, |
| 66 net::AddressList* address_list); |
| 67 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
| 68 int port, |
| 69 net::IPEndPoint* ip_end_point); |
| 70 static bool IPEndPointToStringAndPort(const net::IPEndPoint& address, |
| 71 std::string* ip_address_str, |
| 72 int* port); |
| 48 protected: | 73 protected: |
| 49 Socket(const std::string& address, int port, | 74 Socket(APIResourceEventNotifier* event_notifier); |
| 50 APIResourceEventNotifier* event_notifier); | |
| 51 virtual net::Socket* socket() = 0; | |
| 52 | 75 |
| 53 const std::string address_; | 76 const std::string address_; |
| 54 int port_; | 77 int port_; |
| 55 bool is_connected_; | 78 bool is_connected_; |
| 56 }; | 79 }; |
| 57 | 80 |
| 58 } // namespace extensions | 81 } // namespace extensions |
| 59 | 82 |
| 60 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 83 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
| OLD | NEW |