Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / extensions / browser / api / socket / socket.h
blob7f33b832e66595deb7bf4569de9b0becdf15918a
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 std::string& address,
74 uint16 port,
75 const CompletionCallback& callback) = 0;
76 virtual void Disconnect() = 0;
77 virtual int Bind(const std::string& address, uint16 port) = 0;
79 // The |callback| will be called with the number of bytes read into the
80 // buffer, or a negative number if an error occurred.
81 virtual void Read(int count, const ReadCompletionCallback& callback) = 0;
83 // The |callback| will be called with |byte_count| or a negative number if an
84 // error occurred.
85 void Write(scoped_refptr<net::IOBuffer> io_buffer,
86 int byte_count,
87 const CompletionCallback& callback);
89 virtual void RecvFrom(int count,
90 const RecvFromCompletionCallback& callback) = 0;
91 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
92 int byte_count,
93 const std::string& address,
94 uint16 port,
95 const CompletionCallback& callback) = 0;
97 virtual bool SetKeepAlive(bool enable, int delay);
98 virtual bool SetNoDelay(bool no_delay);
99 virtual int Listen(const std::string& address,
100 uint16 port,
101 int backlog,
102 std::string* error_msg);
103 virtual void Accept(const AcceptCompletionCallback& callback);
105 virtual bool IsConnected() = 0;
107 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
108 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
110 virtual SocketType GetSocketType() const = 0;
112 static bool StringAndPortToAddressList(const std::string& ip_address_str,
113 uint16 port,
114 net::AddressList* address_list);
115 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
116 uint16 port,
117 net::IPEndPoint* ip_end_point);
118 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
119 std::string* ip_address_str,
120 uint16* port);
122 protected:
123 explicit Socket(const std::string& owner_extension_id_);
125 void WriteData();
126 virtual int WriteImpl(net::IOBuffer* io_buffer,
127 int io_buffer_size,
128 const net::CompletionCallback& callback) = 0;
129 virtual void OnWriteComplete(int result);
131 std::string hostname_;
132 bool is_connected_;
134 private:
135 friend class ApiResourceManager<Socket>;
136 static const char* service_name() { return "SocketManager"; }
138 struct WriteRequest {
139 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
140 int byte_count,
141 const CompletionCallback& callback);
142 ~WriteRequest();
143 scoped_refptr<net::IOBuffer> io_buffer;
144 int byte_count;
145 CompletionCallback callback;
146 int bytes_written;
148 std::queue<WriteRequest> write_queue_;
149 scoped_refptr<net::IOBuffer> io_buffer_write_;
151 #if defined(OS_CHROMEOS)
152 // Represents a hole punched in the system firewall for this socket.
153 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread>
154 firewall_hole_;
155 #endif // OS_CHROMEOS
158 } // namespace extensions
160 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_