Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / socket / socket.h
blobb548d23e2acf72658d77cff185d3177b8d309bdb
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_
8 #include <queue>
9 #include <string>
10 #include <utility>
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"
23 #endif // OS_CHROMEOS
25 namespace net {
26 class AddressList;
27 class IPEndPoint;
28 class Socket;
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,
38 const std::string&,
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 {
46 public:
47 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS };
49 ~Socket() override;
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
59 // unbracketed.
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>
65 firewall_hole) {
66 firewall_hole_ = firewall_hole.Pass();
68 #endif // OS_CHROMEOS
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
83 // error occurred.
84 void Write(scoped_refptr<net::IOBuffer> io_buffer,
85 int byte_count,
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,
91 int byte_count,
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,
98 uint16 port,
99 int backlog,
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,
111 uint16 port,
112 net::IPEndPoint* ip_end_point);
113 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
114 std::string* ip_address_str,
115 uint16* port);
117 protected:
118 explicit Socket(const std::string& owner_extension_id_);
120 void WriteData();
121 virtual int WriteImpl(net::IOBuffer* io_buffer,
122 int io_buffer_size,
123 const net::CompletionCallback& callback) = 0;
124 virtual void OnWriteComplete(int result);
126 std::string hostname_;
127 bool is_connected_;
129 private:
130 friend class ApiResourceManager<Socket>;
131 static const char* service_name() { return "SocketManager"; }
133 struct WriteRequest {
134 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
135 int byte_count,
136 const CompletionCallback& callback);
137 ~WriteRequest();
138 scoped_refptr<net::IOBuffer> io_buffer;
139 int byte_count;
140 CompletionCallback callback;
141 int bytes_written;
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>
149 firewall_hole_;
150 #endif // OS_CHROMEOS
153 } // namespace extensions
155 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_