blob: c6ee7e34195dad7c1a0fe8fdb80c30806d54fb22 [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_
7#pragma once
8
[email protected]1a52af52012-05-18 19:28:259#include <queue>
[email protected]fc44a4bef2011-12-16 01:18:4210#include <string>
[email protected]1a52af52012-05-18 19:28:2511#include <utility>
[email protected]fc44a4bef2011-12-16 01:18:4212
[email protected]022bf892012-05-07 20:10:0613#include "base/callback.h"
[email protected]78566202012-05-14 20:46:5114#include "base/memory/ref_counted.h"
[email protected]58edca52012-02-16 21:51:3815#include "chrome/browser/extensions/api/api_resource.h"
[email protected]1a52af52012-05-18 19:28:2516#include "net/base/completion_callback.h"
[email protected]64c820732012-01-05 20:50:3417#include "net/base/io_buffer.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]58edca52012-02-16 21:51:3836class Socket : public APIResource {
[email protected]fc44a4bef2011-12-16 01:18:4237 public:
[email protected]fc44a4bef2011-12-16 01:18:4238 virtual ~Socket();
[email protected]022bf892012-05-07 20:10:0639 virtual void Connect(const std::string& address,
40 int port,
41 const CompletionCallback& callback) = 0;
[email protected]d47c11f2012-01-26 18:15:0442 virtual void Disconnect() = 0;
[email protected]398dca82012-04-25 17:54:5343 virtual int Bind(const std::string& address, int port) = 0;
44
[email protected]1a52af52012-05-18 19:28:2545 // The |callback| will be called with the number of bytes read into the
46 // buffer, or a negative number if an error occurred.
[email protected]022bf892012-05-07 20:10:0647 virtual void Read(int count,
48 const ReadCompletionCallback& callback) = 0;
[email protected]64c820732012-01-05 20:50:3449
[email protected]1a52af52012-05-18 19:28:2550 // The |callback| will be called with |byte_count| or a negative number if an
51 // error occurred.
52 void Write(scoped_refptr<net::IOBuffer> io_buffer,
53 int byte_count,
54 const CompletionCallback& callback);
[email protected]398dca82012-04-25 17:54:5355
[email protected]022bf892012-05-07 20:10:0656 virtual void RecvFrom(int count,
57 const RecvFromCompletionCallback& callback) = 0;
[email protected]022bf892012-05-07 20:10:0658 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
59 int byte_count,
60 const std::string& address,
61 int port,
62 const CompletionCallback& callback) = 0;
[email protected]398dca82012-04-25 17:54:5363 static bool StringAndPortToAddressList(const std::string& ip_address_str,
64 int port,
65 net::AddressList* address_list);
66 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
67 int port,
68 net::IPEndPoint* ip_end_point);
69 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
70 std::string* ip_address_str,
71 int* port);
72
[email protected]d47c11f2012-01-26 18:15:0473 protected:
[email protected]398dca82012-04-25 17:54:5374 explicit Socket(APIResourceEventNotifier* event_notifier);
[email protected]1a52af52012-05-18 19:28:2575
76 void WriteData();
77 virtual int WriteImpl(net::IOBuffer* io_buffer,
78 int io_buffer_size,
79 const net::CompletionCallback& callback) = 0;
80 virtual void OnWriteComplete(int result);
81
[email protected]58edca52012-02-16 21:51:3882 const std::string address_;
83 int port_;
[email protected]fc44a4bef2011-12-16 01:18:4284 bool is_connected_;
[email protected]1a52af52012-05-18 19:28:2585
86 private:
87 struct WriteRequest {
88 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
89 int byte_count,
90 const CompletionCallback& callback)
91 : io_buffer(io_buffer),
92 byte_count(byte_count),
93 callback(callback),
94 bytes_written(0) { }
95 ~WriteRequest() { }
96 scoped_refptr<net::IOBuffer> io_buffer;
97 int byte_count;
98 CompletionCallback callback;
99 int bytes_written;
100 };
101 std::queue<WriteRequest> write_queue_;
102 scoped_refptr<net::IOBuffer> io_buffer_write_;
[email protected]fc44a4bef2011-12-16 01:18:42103};
104
105} // namespace extensions
106
107#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_