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
<void(int,
33 scoped_refptr
<net::IOBuffer
> io_buffer
,
35 uint16
)> RecvFromCompletionCallback
;
36 typedef base::Callback
<void(int, net::TCPClientSocket
*)>
37 AcceptCompletionCallback
;
39 // A Socket wraps a low-level socket and includes housekeeping information that
40 // we need to manage it in the context of an extension.
41 class Socket
: public ApiResource
{
43 enum SocketType
{ TYPE_TCP
, TYPE_UDP
, TYPE_TLS
};
47 // The hostname of the remote host that this socket is connected to. This
48 // may be the empty string if the client does not intend to ever upgrade the
49 // socket to TLS, and thusly has not invoked set_hostname().
50 const std::string
& hostname() const { return hostname_
; }
52 // Set the hostname of the remote host that this socket is connected to.
53 // Note: This may be an IP literal. In the case of IDNs, this should be a
54 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be
56 void set_hostname(const std::string
& hostname
) { hostname_
= hostname
; }
58 // Note: |address| contains the resolved IP address, not the hostname of
59 // the remote endpoint. In order to upgrade this socket to TLS, callers
60 // must also supply the hostname of the endpoint via set_hostname().
61 virtual void Connect(const std::string
& address
,
63 const CompletionCallback
& callback
) = 0;
64 virtual void Disconnect() = 0;
65 virtual int Bind(const std::string
& address
, uint16 port
) = 0;
67 // The |callback| will be called with the number of bytes read into the
68 // buffer, or a negative number if an error occurred.
69 virtual void Read(int count
, const ReadCompletionCallback
& callback
) = 0;
71 // The |callback| will be called with |byte_count| or a negative number if an
73 void Write(scoped_refptr
<net::IOBuffer
> io_buffer
,
75 const CompletionCallback
& callback
);
77 virtual void RecvFrom(int count
,
78 const RecvFromCompletionCallback
& callback
) = 0;
79 virtual void SendTo(scoped_refptr
<net::IOBuffer
> io_buffer
,
81 const std::string
& address
,
83 const CompletionCallback
& callback
) = 0;
85 virtual bool SetKeepAlive(bool enable
, int delay
);
86 virtual bool SetNoDelay(bool no_delay
);
87 virtual int Listen(const std::string
& address
,
90 std::string
* error_msg
);
91 virtual void Accept(const AcceptCompletionCallback
& callback
);
93 virtual bool IsConnected() = 0;
95 virtual bool GetPeerAddress(net::IPEndPoint
* address
) = 0;
96 virtual bool GetLocalAddress(net::IPEndPoint
* address
) = 0;
98 virtual SocketType
GetSocketType() const = 0;
100 static bool StringAndPortToAddressList(const std::string
& ip_address_str
,
102 net::AddressList
* address_list
);
103 static bool StringAndPortToIPEndPoint(const std::string
& ip_address_str
,
105 net::IPEndPoint
* ip_end_point
);
106 static void IPEndPointToStringAndPort(const net::IPEndPoint
& address
,
107 std::string
* ip_address_str
,
111 explicit Socket(const std::string
& owner_extension_id_
);
114 virtual int WriteImpl(net::IOBuffer
* io_buffer
,
116 const net::CompletionCallback
& callback
) = 0;
117 virtual void OnWriteComplete(int result
);
119 std::string hostname_
;
123 friend class ApiResourceManager
<Socket
>;
124 static const char* service_name() { return "SocketManager"; }
126 struct WriteRequest
{
127 WriteRequest(scoped_refptr
<net::IOBuffer
> io_buffer
,
129 const CompletionCallback
& callback
);
131 scoped_refptr
<net::IOBuffer
> io_buffer
;
133 CompletionCallback callback
;
136 std::queue
<WriteRequest
> write_queue_
;
137 scoped_refptr
<net::IOBuffer
> io_buffer_write_
;
140 } // namespace extensions
142 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_