blob: 0f29d38ec7c3ab1933bd99967365013794a30069 [file] [log] [blame]
[email protected]64c820732012-01-05 20:50:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fc44a4bef2011-12-16 01:18:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
6#define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
[email protected]fc44a4bef2011-12-16 01:18:427
[email protected]1a52af52012-05-18 19:28:258#include <queue>
[email protected]fc44a4bef2011-12-16 01:18:429#include <string>
[email protected]1a52af52012-05-18 19:28:2510#include <utility>
[email protected]fc44a4bef2011-12-16 01:18:4211
[email protected]022bf892012-05-07 20:10:0612#include "base/callback.h"
[email protected]78566202012-05-14 20:46:5113#include "base/memory/ref_counted.h"
[email protected]58edca52012-02-16 21:51:3814#include "chrome/browser/extensions/api/api_resource.h"
[email protected]1a52af52012-05-18 19:28:2515#include "net/base/completion_callback.h"
[email protected]64c820732012-01-05 20:50:3416#include "net/base/io_buffer.h"
[email protected]356127d2012-08-07 00:29:5317#include "net/base/ip_endpoint.h"
[email protected]1be30b42012-09-28 01:34:1018#include "net/socket/tcp_client_socket.h"
[email protected]fc44a4bef2011-12-16 01:18:4219
20namespace net {
[email protected]398dca82012-04-25 17:54:5321class AddressList;
22class IPEndPoint;
[email protected]d47c11f2012-01-26 18:15:0423class Socket;
[email protected]fc44a4bef2011-12-16 01:18:4224}
25
26namespace extensions {
27
[email protected]022bf892012-05-07 20:10:0628typedef base::Callback<void(int)> CompletionCallback;
29typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
30 ReadCompletionCallback;
31typedef base::Callback<
32 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
33 RecvFromCompletionCallback;
[email protected]1be30b42012-09-28 01:34:1034typedef base::Callback<
35 void(int, net::TCPClientSocket*)> AcceptCompletionCallback;
[email protected]022bf892012-05-07 20:10:0636
[email protected]fc44a4bef2011-12-16 01:18:4237// A Socket wraps a low-level socket and includes housekeeping information that
38// we need to manage it in the context of an extension.
[email protected]931186e02012-07-20 01:22:0639class Socket : public ApiResource {
[email protected]fc44a4bef2011-12-16 01:18:4240 public:
[email protected]1d8b79a2012-08-16 20:22:5441 enum SocketType {
42 TYPE_TCP,
43 TYPE_UDP,
44 };
45
[email protected]fc44a4bef2011-12-16 01:18:4246 virtual ~Socket();
[email protected]022bf892012-05-07 20:10:0647 virtual void Connect(const std::string& address,
48 int port,
49 const CompletionCallback& callback) = 0;
[email protected]d47c11f2012-01-26 18:15:0450 virtual void Disconnect() = 0;
[email protected]398dca82012-04-25 17:54:5351 virtual int Bind(const std::string& address, int port) = 0;
52
[email protected]1a52af52012-05-18 19:28:2553 // The |callback| will be called with the number of bytes read into the
54 // buffer, or a negative number if an error occurred.
[email protected]022bf892012-05-07 20:10:0655 virtual void Read(int count,
56 const ReadCompletionCallback& callback) = 0;
[email protected]64c820732012-01-05 20:50:3457
[email protected]1a52af52012-05-18 19:28:2558 // The |callback| will be called with |byte_count| or a negative number if an
59 // error occurred.
60 void Write(scoped_refptr<net::IOBuffer> io_buffer,
61 int byte_count,
62 const CompletionCallback& callback);
[email protected]398dca82012-04-25 17:54:5363
[email protected]022bf892012-05-07 20:10:0664 virtual void RecvFrom(int count,
65 const RecvFromCompletionCallback& callback) = 0;
[email protected]022bf892012-05-07 20:10:0666 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
67 int byte_count,
68 const std::string& address,
69 int port,
70 const CompletionCallback& callback) = 0;
[email protected]820b984a2012-05-29 23:15:0671
72 virtual bool SetKeepAlive(bool enable, int delay);
73 virtual bool SetNoDelay(bool no_delay);
[email protected]1be30b42012-09-28 01:34:1074 virtual int Listen(const std::string& address, int port, int backlog,
75 std::string* error_msg);
76 virtual void Accept(const AcceptCompletionCallback &callback);
[email protected]820b984a2012-05-29 23:15:0677
[email protected]356127d2012-08-07 00:29:5378 bool IsConnected();
[email protected]356127d2012-08-07 00:29:5379
80 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
81 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
82
[email protected]1d8b79a2012-08-16 20:22:5483 virtual SocketType GetSocketType() const = 0;
84
[email protected]398dca82012-04-25 17:54:5385 static bool StringAndPortToAddressList(const std::string& ip_address_str,
86 int port,
87 net::AddressList* address_list);
88 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
89 int port,
90 net::IPEndPoint* ip_end_point);
91 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
92 std::string* ip_address_str,
93 int* port);
94
[email protected]d47c11f2012-01-26 18:15:0495 protected:
[email protected]41cacf32012-10-11 00:06:2496 explicit Socket(const std::string& owner_extension_id_);
[email protected]1a52af52012-05-18 19:28:2597
98 void WriteData();
99 virtual int WriteImpl(net::IOBuffer* io_buffer,
100 int io_buffer_size,
101 const net::CompletionCallback& callback) = 0;
102 virtual void OnWriteComplete(int result);
103
[email protected]58edca52012-02-16 21:51:38104 const std::string address_;
[email protected]fc44a4bef2011-12-16 01:18:42105 bool is_connected_;
[email protected]1a52af52012-05-18 19:28:25106
107 private:
108 struct WriteRequest {
109 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
110 int byte_count,
[email protected]23827ec2012-08-10 22:08:08111 const CompletionCallback& callback);
112 ~WriteRequest();
[email protected]1a52af52012-05-18 19:28:25113 scoped_refptr<net::IOBuffer> io_buffer;
114 int byte_count;
115 CompletionCallback callback;
116 int bytes_written;
117 };
118 std::queue<WriteRequest> write_queue_;
119 scoped_refptr<net::IOBuffer> io_buffer_write_;
[email protected]fc44a4bef2011-12-16 01:18:42120};
121
122} // namespace extensions
123
124#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_