blob: 27e2685fe505d91410a9ef07c4bfc338a6abb1d3 [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]fc44a4bef2011-12-16 01:18:4218
19namespace net {
[email protected]398dca82012-04-25 17:54:5320class AddressList;
21class IPEndPoint;
[email protected]d47c11f2012-01-26 18:15:0422class Socket;
[email protected]fc44a4bef2011-12-16 01:18:4223}
24
25namespace extensions {
26
[email protected]022bf892012-05-07 20:10:0627typedef base::Callback<void(int)> CompletionCallback;
28typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
29 ReadCompletionCallback;
30typedef base::Callback<
31 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
32 RecvFromCompletionCallback;
33
[email protected]fc44a4bef2011-12-16 01:18:4234// A Socket wraps a low-level socket and includes housekeeping information that
35// we need to manage it in the context of an extension.
[email protected]931186e02012-07-20 01:22:0636class Socket : public ApiResource {
[email protected]fc44a4bef2011-12-16 01:18:4237 public:
[email protected]1d8b79a2012-08-16 20:22:5438 enum SocketType {
39 TYPE_TCP,
40 TYPE_UDP,
41 };
42
[email protected]fc44a4bef2011-12-16 01:18:4243 virtual ~Socket();
[email protected]022bf892012-05-07 20:10:0644 virtual void Connect(const std::string& address,
45 int port,
46 const CompletionCallback& callback) = 0;
[email protected]d47c11f2012-01-26 18:15:0447 virtual void Disconnect() = 0;
[email protected]398dca82012-04-25 17:54:5348 virtual int Bind(const std::string& address, int port) = 0;
49
[email protected]1a52af52012-05-18 19:28:2550 // The |callback| will be called with the number of bytes read into the
51 // buffer, or a negative number if an error occurred.
[email protected]022bf892012-05-07 20:10:0652 virtual void Read(int count,
53 const ReadCompletionCallback& callback) = 0;
[email protected]64c820732012-01-05 20:50:3454
[email protected]1a52af52012-05-18 19:28:2555 // The |callback| will be called with |byte_count| or a negative number if an
56 // error occurred.
57 void Write(scoped_refptr<net::IOBuffer> io_buffer,
58 int byte_count,
59 const CompletionCallback& callback);
[email protected]398dca82012-04-25 17:54:5360
[email protected]022bf892012-05-07 20:10:0661 virtual void RecvFrom(int count,
62 const RecvFromCompletionCallback& callback) = 0;
[email protected]022bf892012-05-07 20:10:0663 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
64 int byte_count,
65 const std::string& address,
66 int port,
67 const CompletionCallback& callback) = 0;
[email protected]820b984a2012-05-29 23:15:0668
69 virtual bool SetKeepAlive(bool enable, int delay);
70 virtual bool SetNoDelay(bool no_delay);
71
[email protected]356127d2012-08-07 00:29:5372 bool IsConnected();
[email protected]356127d2012-08-07 00:29:5373
74 virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
75 virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
76
[email protected]1d8b79a2012-08-16 20:22:5477 virtual SocketType GetSocketType() const = 0;
78
[email protected]398dca82012-04-25 17:54:5379 static bool StringAndPortToAddressList(const std::string& ip_address_str,
80 int port,
81 net::AddressList* address_list);
82 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
83 int port,
84 net::IPEndPoint* ip_end_point);
85 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
86 std::string* ip_address_str,
87 int* port);
88
[email protected]d47c11f2012-01-26 18:15:0489 protected:
[email protected]09426852012-09-11 22:39:0790 Socket(const std::string& owner_extension_id_,
91 ApiResourceEventNotifier* event_notifier);
[email protected]1a52af52012-05-18 19:28:2592
93 void WriteData();
94 virtual int WriteImpl(net::IOBuffer* io_buffer,
95 int io_buffer_size,
96 const net::CompletionCallback& callback) = 0;
97 virtual void OnWriteComplete(int result);
98
[email protected]58edca52012-02-16 21:51:3899 const std::string address_;
100 int port_;
[email protected]fc44a4bef2011-12-16 01:18:42101 bool is_connected_;
[email protected]1a52af52012-05-18 19:28:25102
103 private:
104 struct WriteRequest {
105 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
106 int byte_count,
[email protected]23827ec2012-08-10 22:08:08107 const CompletionCallback& callback);
108 ~WriteRequest();
[email protected]1a52af52012-05-18 19:28:25109 scoped_refptr<net::IOBuffer> io_buffer;
110 int byte_count;
111 CompletionCallback callback;
112 int bytes_written;
113 };
114 std::queue<WriteRequest> write_queue_;
115 scoped_refptr<net::IOBuffer> io_buffer_write_;
[email protected]fc44a4bef2011-12-16 01:18:42116};
117
118} // namespace extensions
119
120#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_