1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/extensions/api/api_resource.h"
15 #include "chrome/browser/extensions/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<
33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
34 RecvFromCompletionCallback;
35 typedef base::Callback<
36 void(int, net::TCPClientSocket*)> AcceptCompletionCallback;
38 // A Socket wraps a low-level socket and includes housekeeping information that
39 // we need to manage it in the context of an extension.
40 class Socket : public ApiResource {
48 virtual void Connect(const std::string& address,
50 const CompletionCallback& callback) = 0;
51 virtual void Disconnect() = 0;
52 virtual int Bind(const std::string& address, int port) = 0;
54 // The |callback| will be called with the number of bytes read into the
55 // buffer, or a negative number if an error occurred.
56 virtual void Read(int count,
57 const ReadCompletionCallback& callback) = 0;
59 // The |callback| will be called with |byte_count| or a negative number if an
61 void Write(scoped_refptr<net::IOBuffer> io_buffer,
63 const CompletionCallback& callback);
65 virtual void RecvFrom(int count,
66 const RecvFromCompletionCallback& callback) = 0;
67 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
69 const std::string& address,
71 const CompletionCallback& callback) = 0;
73 virtual bool SetKeepAlive(bool enable, int delay);
74 virtual bool SetNoDelay(bool no_delay);
75 virtual int Listen(const std::string& address, int port, int backlog,
76 std::string* error_msg);
77 virtual void Accept(const AcceptCompletionCallback &callback);
79 virtual bool IsConnected() = 0;
81 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
82 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
84 virtual SocketType GetSocketType() const = 0;
86 static bool StringAndPortToAddressList(const std::string& ip_address_str,
88 net::AddressList* address_list);
89 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
91 net::IPEndPoint* ip_end_point);
92 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
93 std::string* ip_address_str,
97 explicit Socket(const std::string& owner_extension_id_);
100 virtual int WriteImpl(net::IOBuffer* io_buffer,
102 const net::CompletionCallback& callback) = 0;
103 virtual void OnWriteComplete(int result);
105 const std::string address_;
109 friend class ApiResourceManager<Socket>;
110 static const char* service_name() {
111 return "SocketManager";
114 struct WriteRequest {
115 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
117 const CompletionCallback& callback);
119 scoped_refptr<net::IOBuffer> io_buffer;
121 CompletionCallback callback;
124 std::queue<WriteRequest> write_queue_;
125 scoped_refptr<net::IOBuffer> io_buffer_write_;
128 } // namespace extensions
130 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_