| 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 EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "chrome/browser/extensions/api/api_resource.h" | 14 #include "chrome/browser/extensions/api/api_resource.h" |
| 15 #include "chrome/browser/extensions/api/api_resource_manager.h" | 15 #include "chrome/browser/extensions/api/api_resource_manager.h" |
| 16 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 19 #include "net/socket/tcp_client_socket.h" | 19 #include "net/socket/tcp_client_socket.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 class AddressList; | 22 class AddressList; |
| 23 class IPEndPoint; | 23 class IPEndPoint; |
| 24 class Socket; | 24 class Socket; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 | 28 |
| 29 typedef base::Callback<void(int)> CompletionCallback; | 29 typedef base::Callback<void(int)> CompletionCallback; |
| 30 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> | 30 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
| 31 ReadCompletionCallback; | 31 ReadCompletionCallback; |
| 32 typedef base::Callback< | 32 typedef base::Callback< |
| 33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> | 33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
| 34 RecvFromCompletionCallback; | 34 RecvFromCompletionCallback; |
| 35 typedef base::Callback< | 35 typedef base::Callback<void(int, net::TCPClientSocket*)> |
| 36 void(int, net::TCPClientSocket*)> AcceptCompletionCallback; | 36 AcceptCompletionCallback; |
| 37 | 37 |
| 38 // A Socket wraps a low-level socket and includes housekeeping information that | 38 // A Socket wraps a low-level socket and includes housekeeping information that |
| 39 // we need to manage it in the context of an extension. | 39 // we need to manage it in the context of an extension. |
| 40 class Socket : public ApiResource { | 40 class Socket : public ApiResource { |
| 41 public: | 41 public: |
| 42 enum SocketType { | 42 enum SocketType { TYPE_TCP, TYPE_UDP, }; |
| 43 TYPE_TCP, | |
| 44 TYPE_UDP, | |
| 45 }; | |
| 46 | 43 |
| 47 virtual ~Socket(); | 44 virtual ~Socket(); |
| 48 virtual void Connect(const std::string& address, | 45 virtual void Connect(const std::string& address, |
| 49 int port, | 46 int port, |
| 50 const CompletionCallback& callback) = 0; | 47 const CompletionCallback& callback) = 0; |
| 51 virtual void Disconnect() = 0; | 48 virtual void Disconnect() = 0; |
| 52 virtual int Bind(const std::string& address, int port) = 0; | 49 virtual int Bind(const std::string& address, int port) = 0; |
| 53 | 50 |
| 54 // The |callback| will be called with the number of bytes read into the | 51 // The |callback| will be called with the number of bytes read into the |
| 55 // buffer, or a negative number if an error occurred. | 52 // buffer, or a negative number if an error occurred. |
| 56 virtual void Read(int count, | 53 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; |
| 57 const ReadCompletionCallback& callback) = 0; | |
| 58 | 54 |
| 59 // The |callback| will be called with |byte_count| or a negative number if an | 55 // The |callback| will be called with |byte_count| or a negative number if an |
| 60 // error occurred. | 56 // error occurred. |
| 61 void Write(scoped_refptr<net::IOBuffer> io_buffer, | 57 void Write(scoped_refptr<net::IOBuffer> io_buffer, |
| 62 int byte_count, | 58 int byte_count, |
| 63 const CompletionCallback& callback); | 59 const CompletionCallback& callback); |
| 64 | 60 |
| 65 virtual void RecvFrom(int count, | 61 virtual void RecvFrom(int count, |
| 66 const RecvFromCompletionCallback& callback) = 0; | 62 const RecvFromCompletionCallback& callback) = 0; |
| 67 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 63 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 68 int byte_count, | 64 int byte_count, |
| 69 const std::string& address, | 65 const std::string& address, |
| 70 int port, | 66 int port, |
| 71 const CompletionCallback& callback) = 0; | 67 const CompletionCallback& callback) = 0; |
| 72 | 68 |
| 73 virtual bool SetKeepAlive(bool enable, int delay); | 69 virtual bool SetKeepAlive(bool enable, int delay); |
| 74 virtual bool SetNoDelay(bool no_delay); | 70 virtual bool SetNoDelay(bool no_delay); |
| 75 virtual int Listen(const std::string& address, int port, int backlog, | 71 virtual int Listen(const std::string& address, |
| 72 int port, |
| 73 int backlog, |
| 76 std::string* error_msg); | 74 std::string* error_msg); |
| 77 virtual void Accept(const AcceptCompletionCallback &callback); | 75 virtual void Accept(const AcceptCompletionCallback& callback); |
| 78 | 76 |
| 79 virtual bool IsConnected() = 0; | 77 virtual bool IsConnected() = 0; |
| 80 | 78 |
| 81 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; | 79 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0; |
| 82 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; | 80 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0; |
| 83 | 81 |
| 84 virtual SocketType GetSocketType() const = 0; | 82 virtual SocketType GetSocketType() const = 0; |
| 85 | 83 |
| 86 static bool StringAndPortToAddressList(const std::string& ip_address_str, | 84 static bool StringAndPortToAddressList(const std::string& ip_address_str, |
| 87 int port, | 85 int port, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 100 virtual int WriteImpl(net::IOBuffer* io_buffer, | 98 virtual int WriteImpl(net::IOBuffer* io_buffer, |
| 101 int io_buffer_size, | 99 int io_buffer_size, |
| 102 const net::CompletionCallback& callback) = 0; | 100 const net::CompletionCallback& callback) = 0; |
| 103 virtual void OnWriteComplete(int result); | 101 virtual void OnWriteComplete(int result); |
| 104 | 102 |
| 105 const std::string address_; | 103 const std::string address_; |
| 106 bool is_connected_; | 104 bool is_connected_; |
| 107 | 105 |
| 108 private: | 106 private: |
| 109 friend class ApiResourceManager<Socket>; | 107 friend class ApiResourceManager<Socket>; |
| 110 static const char* service_name() { | 108 static const char* service_name() { return "SocketManager"; } |
| 111 return "SocketManager"; | |
| 112 } | |
| 113 | 109 |
| 114 struct WriteRequest { | 110 struct WriteRequest { |
| 115 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, | 111 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, |
| 116 int byte_count, | 112 int byte_count, |
| 117 const CompletionCallback& callback); | 113 const CompletionCallback& callback); |
| 118 ~WriteRequest(); | 114 ~WriteRequest(); |
| 119 scoped_refptr<net::IOBuffer> io_buffer; | 115 scoped_refptr<net::IOBuffer> io_buffer; |
| 120 int byte_count; | 116 int byte_count; |
| 121 CompletionCallback callback; | 117 CompletionCallback callback; |
| 122 int bytes_written; | 118 int bytes_written; |
| 123 }; | 119 }; |
| 124 std::queue<WriteRequest> write_queue_; | 120 std::queue<WriteRequest> write_queue_; |
| 125 scoped_refptr<net::IOBuffer> io_buffer_write_; | 121 scoped_refptr<net::IOBuffer> io_buffer_write_; |
| 126 }; | 122 }; |
| 127 | 123 |
| 128 } // namespace extensions | 124 } // namespace extensions |
| 129 | 125 |
| 130 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 126 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| OLD | NEW |