Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / browser / api / socket / socket.h
blobe2287fe5c895434ec824de0613fce6e6203707ea
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 namespace net {
22 class AddressList;
23 class IPEndPoint;
24 class Socket;
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,
34 const std::string&,
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 {
42 public:
43 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS };
45 ~Socket() override;
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
55 // unbracketed.
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,
62 uint16 port,
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
72 // error occurred.
73 void Write(scoped_refptr<net::IOBuffer> io_buffer,
74 int byte_count,
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,
80 int byte_count,
81 const std::string& address,
82 uint16 port,
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,
88 uint16 port,
89 int backlog,
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,
101 uint16 port,
102 net::AddressList* address_list);
103 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
104 uint16 port,
105 net::IPEndPoint* ip_end_point);
106 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
107 std::string* ip_address_str,
108 uint16* port);
110 protected:
111 explicit Socket(const std::string& owner_extension_id_);
113 void WriteData();
114 virtual int WriteImpl(net::IOBuffer* io_buffer,
115 int io_buffer_size,
116 const net::CompletionCallback& callback) = 0;
117 virtual void OnWriteComplete(int result);
119 std::string hostname_;
120 bool is_connected_;
122 private:
123 friend class ApiResourceManager<Socket>;
124 static const char* service_name() { return "SocketManager"; }
126 struct WriteRequest {
127 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
128 int byte_count,
129 const CompletionCallback& callback);
130 ~WriteRequest();
131 scoped_refptr<net::IOBuffer> io_buffer;
132 int byte_count;
133 CompletionCallback callback;
134 int bytes_written;
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_