blob: b877713b4d22a5174c3b57b5e3910d690acd21e5 [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
9#include <string>
10
[email protected]022bf892012-05-07 20:10:0611#include "base/callback.h"
[email protected]fc44a4bef2011-12-16 01:18:4212#include "base/memory/scoped_ptr.h"
[email protected]58edca52012-02-16 21:51:3813#include "chrome/browser/extensions/api/api_resource.h"
[email protected]64c820732012-01-05 20:50:3414#include "net/base/io_buffer.h"
[email protected]fc44a4bef2011-12-16 01:18:4215
16namespace net {
[email protected]398dca82012-04-25 17:54:5317class AddressList;
18class IPEndPoint;
[email protected]d47c11f2012-01-26 18:15:0419class Socket;
[email protected]fc44a4bef2011-12-16 01:18:4220}
21
22namespace extensions {
23
[email protected]022bf892012-05-07 20:10:0624typedef base::Callback<void(int)> CompletionCallback;
25typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
26 ReadCompletionCallback;
27typedef base::Callback<
28 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
29 RecvFromCompletionCallback;
30
[email protected]fc44a4bef2011-12-16 01:18:4231// A Socket wraps a low-level socket and includes housekeeping information that
32// we need to manage it in the context of an extension.
[email protected]58edca52012-02-16 21:51:3833class Socket : public APIResource {
[email protected]fc44a4bef2011-12-16 01:18:4234 public:
[email protected]fc44a4bef2011-12-16 01:18:4235 virtual ~Socket();
36
[email protected]d47c11f2012-01-26 18:15:0437 // Returns net::OK if successful, or an error code otherwise.
[email protected]022bf892012-05-07 20:10:0638 virtual void Connect(const std::string& address,
39 int port,
40 const CompletionCallback& callback) = 0;
[email protected]d47c11f2012-01-26 18:15:0441 virtual void Disconnect() = 0;
[email protected]fc44a4bef2011-12-16 01:18:4242
[email protected]398dca82012-04-25 17:54:5343 virtual int Bind(const std::string& address, int port) = 0;
44
[email protected]097fef762012-04-17 02:07:4745 // Returns the number of bytes read into the buffer, or a negative number if
46 // 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]fc44a4bef2011-12-16 01:18:4250 // Returns the number of bytes successfully written, or a negative error
51 // code. Note that ERR_IO_PENDING means that the operation blocked, in which
[email protected]097fef762012-04-17 02:07:4752 // case |event_notifier| (supplied at socket creation) will eventually be
53 // called with the final result (again, either a nonnegative number of bytes
54 // written, or a negative error).
[email protected]022bf892012-05-07 20:10:0655 virtual void Write(scoped_refptr<net::IOBuffer> io_buffer,
[email protected]398dca82012-04-25 17:54:5356 int byte_count,
[email protected]022bf892012-05-07 20:10:0657 const CompletionCallback& callback) = 0;
[email protected]398dca82012-04-25 17:54:5358
[email protected]022bf892012-05-07 20:10:0659 virtual void RecvFrom(int count,
60 const RecvFromCompletionCallback& callback) = 0;
61
62 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
63 int byte_count,
64 const std::string& address,
65 int port,
66 const CompletionCallback& callback) = 0;
[email protected]d47c11f2012-01-26 18:15:0467
[email protected]398dca82012-04-25 17:54:5368 static bool StringAndPortToAddressList(const std::string& ip_address_str,
69 int port,
70 net::AddressList* address_list);
71 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
72 int port,
73 net::IPEndPoint* ip_end_point);
74 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
75 std::string* ip_address_str,
76 int* port);
77
[email protected]d47c11f2012-01-26 18:15:0478 protected:
[email protected]398dca82012-04-25 17:54:5379 explicit Socket(APIResourceEventNotifier* event_notifier);
[email protected]58edca52012-02-16 21:51:3880 const std::string address_;
81 int port_;
[email protected]fc44a4bef2011-12-16 01:18:4282 bool is_connected_;
[email protected]fc44a4bef2011-12-16 01:18:4283};
84
85} // namespace extensions
86
87#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_