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.
5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_
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"
27 namespace extensions {
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;
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 {
42 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS };
46 // The hostname of the remote host that this socket is connected to. This
47 // may be the empty string if the client does not intend to ever upgrade the
48 // socket to TLS, and thusly has not invoked set_hostname().
49 const std::string& hostname() const { return hostname_; }
51 // Set the hostname of the remote host that this socket is connected to.
52 // Note: This may be an IP literal. In the case of IDNs, this should be a
53 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be
55 void set_hostname(const std::string& hostname) { hostname_ = hostname; }
57 // Note: |address| contains the resolved IP address, not the hostname of
58 // the remote endpoint. In order to upgrade this socket to TLS, callers
59 // must also supply the hostname of the endpoint via set_hostname().
60 virtual void Connect(const std::string& address,
62 const CompletionCallback& callback) = 0;
63 virtual void Disconnect() = 0;
64 virtual int Bind(const std::string& address, int port) = 0;
66 // The |callback| will be called with the number of bytes read into the
67 // buffer, or a negative number if an error occurred.
68 virtual void Read(int count, const ReadCompletionCallback& callback) = 0;
70 // The |callback| will be called with |byte_count| or a negative number if an
72 void Write(scoped_refptr<net::IOBuffer> io_buffer,
74 const CompletionCallback& callback);
76 virtual void RecvFrom(int count,
77 const RecvFromCompletionCallback& callback) = 0;
78 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
80 const std::string& address,
82 const CompletionCallback& callback) = 0;
84 virtual bool SetKeepAlive(bool enable, int delay);
85 virtual bool SetNoDelay(bool no_delay);
86 virtual int Listen(const std::string& address,
89 std::string* error_msg);
90 virtual void Accept(const AcceptCompletionCallback& callback);
92 virtual bool IsConnected() = 0;
94 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
95 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
97 virtual SocketType GetSocketType() const = 0;
99 static bool StringAndPortToAddressList(const std::string& ip_address_str,
101 net::AddressList* address_list);
102 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
104 net::IPEndPoint* ip_end_point);
105 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
106 std::string* ip_address_str,
110 explicit Socket(const std::string& owner_extension_id_);
113 virtual int WriteImpl(net::IOBuffer* io_buffer,
115 const net::CompletionCallback& callback) = 0;
116 virtual void OnWriteComplete(int result);
118 std::string hostname_;
122 friend class ApiResourceManager<Socket>;
123 static const char* service_name() { return "SocketManager"; }
125 struct WriteRequest {
126 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
128 const CompletionCallback& callback);
130 scoped_refptr<net::IOBuffer> io_buffer;
132 CompletionCallback callback;
135 std::queue<WriteRequest> write_queue_;
136 scoped_refptr<net::IOBuffer> io_buffer_write_;
139 } // namespace extensions
141 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_