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"
21 #if defined(OS_CHROMEOS)
22 #include "extensions/browser/api/socket/app_firewall_hole_manager.h"
31 namespace extensions
{
33 typedef base::Callback
<void(int)> CompletionCallback
;
34 typedef base::Callback
<void(int, scoped_refptr
<net::IOBuffer
> io_buffer
)>
35 ReadCompletionCallback
;
36 typedef base::Callback
<void(int,
37 scoped_refptr
<net::IOBuffer
> io_buffer
,
39 uint16
)> RecvFromCompletionCallback
;
40 typedef base::Callback
<void(int, net::TCPClientSocket
*)>
41 AcceptCompletionCallback
;
43 // A Socket wraps a low-level socket and includes housekeeping information that
44 // we need to manage it in the context of an extension.
45 class Socket
: public ApiResource
{
47 enum SocketType
{ TYPE_TCP
, TYPE_UDP
, TYPE_TLS
};
51 // The hostname of the remote host that this socket is connected to. This
52 // may be the empty string if the client does not intend to ever upgrade the
53 // socket to TLS, and thusly has not invoked set_hostname().
54 const std::string
& hostname() const { return hostname_
; }
56 // Set the hostname of the remote host that this socket is connected to.
57 // Note: This may be an IP literal. In the case of IDNs, this should be a
58 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be
60 void set_hostname(const std::string
& hostname
) { hostname_
= hostname
; }
62 #if defined(OS_CHROMEOS)
63 void set_firewall_hole(
64 scoped_ptr
<AppFirewallHole
, content::BrowserThread::DeleteOnUIThread
>
66 firewall_hole_
= firewall_hole
.Pass();
70 // Note: |address| contains the resolved IP address, not the hostname of
71 // the remote endpoint. In order to upgrade this socket to TLS, callers
72 // must also supply the hostname of the endpoint via set_hostname().
73 virtual void Connect(const net::AddressList
& address
,
74 const CompletionCallback
& callback
) = 0;
75 virtual void Disconnect() = 0;
76 virtual int Bind(const std::string
& address
, uint16 port
) = 0;
78 // The |callback| will be called with the number of bytes read into the
79 // buffer, or a negative number if an error occurred.
80 virtual void Read(int count
, const ReadCompletionCallback
& callback
) = 0;
82 // The |callback| will be called with |byte_count| or a negative number if an
84 void Write(scoped_refptr
<net::IOBuffer
> io_buffer
,
86 const CompletionCallback
& callback
);
88 virtual void RecvFrom(int count
,
89 const RecvFromCompletionCallback
& callback
) = 0;
90 virtual void SendTo(scoped_refptr
<net::IOBuffer
> io_buffer
,
92 const net::IPEndPoint
& address
,
93 const CompletionCallback
& callback
) = 0;
95 virtual bool SetKeepAlive(bool enable
, int delay
);
96 virtual bool SetNoDelay(bool no_delay
);
97 virtual int Listen(const std::string
& address
,
100 std::string
* error_msg
);
101 virtual void Accept(const AcceptCompletionCallback
& callback
);
103 virtual bool IsConnected() = 0;
105 virtual bool GetPeerAddress(net::IPEndPoint
* address
) = 0;
106 virtual bool GetLocalAddress(net::IPEndPoint
* address
) = 0;
108 virtual SocketType
GetSocketType() const = 0;
110 static bool StringAndPortToIPEndPoint(const std::string
& ip_address_str
,
112 net::IPEndPoint
* ip_end_point
);
113 static void IPEndPointToStringAndPort(const net::IPEndPoint
& address
,
114 std::string
* ip_address_str
,
118 explicit Socket(const std::string
& owner_extension_id_
);
121 virtual int WriteImpl(net::IOBuffer
* io_buffer
,
123 const net::CompletionCallback
& callback
) = 0;
124 virtual void OnWriteComplete(int result
);
126 std::string hostname_
;
130 friend class ApiResourceManager
<Socket
>;
131 static const char* service_name() { return "SocketManager"; }
133 struct WriteRequest
{
134 WriteRequest(scoped_refptr
<net::IOBuffer
> io_buffer
,
136 const CompletionCallback
& callback
);
138 scoped_refptr
<net::IOBuffer
> io_buffer
;
140 CompletionCallback callback
;
143 std::queue
<WriteRequest
> write_queue_
;
144 scoped_refptr
<net::IOBuffer
> io_buffer_write_
;
146 #if defined(OS_CHROMEOS)
147 // Represents a hole punched in the system firewall for this socket.
148 scoped_ptr
<AppFirewallHole
, content::BrowserThread::DeleteOnUIThread
>
150 #endif // OS_CHROMEOS
153 } // namespace extensions
155 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_