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