blob: d91a9501dcfbe0b5cf40c0b01b8f91394b16c451 [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
11#include "base/memory/scoped_ptr.h"
[email protected]58edca52012-02-16 21:51:3812#include "chrome/browser/extensions/api/api_resource.h"
[email protected]64c820732012-01-05 20:50:3413#include "net/base/io_buffer.h"
[email protected]fc44a4bef2011-12-16 01:18:4214
15namespace net {
[email protected]398dca82012-04-25 17:54:5316class AddressList;
17class IPEndPoint;
[email protected]d47c11f2012-01-26 18:15:0418class Socket;
[email protected]fc44a4bef2011-12-16 01:18:4219}
20
21namespace extensions {
22
23// A Socket wraps a low-level socket and includes housekeeping information that
24// we need to manage it in the context of an extension.
[email protected]58edca52012-02-16 21:51:3825class Socket : public APIResource {
[email protected]fc44a4bef2011-12-16 01:18:4226 public:
[email protected]fc44a4bef2011-12-16 01:18:4227 virtual ~Socket();
28
[email protected]d47c11f2012-01-26 18:15:0429 // Returns net::OK if successful, or an error code otherwise.
[email protected]398dca82012-04-25 17:54:5330 virtual int Connect(const std::string& address, int port) = 0;
[email protected]d47c11f2012-01-26 18:15:0431 virtual void Disconnect() = 0;
[email protected]fc44a4bef2011-12-16 01:18:4232
[email protected]398dca82012-04-25 17:54:5333 virtual int Bind(const std::string& address, int port) = 0;
34
[email protected]097fef762012-04-17 02:07:4735 // Returns the number of bytes read into the buffer, or a negative number if
36 // an error occurred.
[email protected]398dca82012-04-25 17:54:5337 virtual int Read(scoped_refptr<net::IOBuffer> io_buffer,
38 int io_buffer_size) = 0;
[email protected]64c820732012-01-05 20:50:3439
[email protected]fc44a4bef2011-12-16 01:18:4240 // Returns the number of bytes successfully written, or a negative error
41 // code. Note that ERR_IO_PENDING means that the operation blocked, in which
[email protected]097fef762012-04-17 02:07:4742 // case |event_notifier| (supplied at socket creation) will eventually be
43 // called with the final result (again, either a nonnegative number of bytes
44 // written, or a negative error).
[email protected]398dca82012-04-25 17:54:5345 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer,
46 int byte_count) = 0;
[email protected]fc44a4bef2011-12-16 01:18:4247
[email protected]398dca82012-04-25 17:54:5348 virtual int RecvFrom(scoped_refptr<net::IOBuffer> io_buffer,
49 int io_buffer_size,
50 net::IPEndPoint *address) = 0;
51 virtual int SendTo(scoped_refptr<net::IOBuffer> io_buffer,
52 int byte_count,
53 const std::string& address,
54 int port) = 0;
55
56 virtual void OnDataRead(scoped_refptr<net::IOBuffer> io_buffer,
57 net::IPEndPoint *address,
58 int result);
[email protected]d47c11f2012-01-26 18:15:0459 virtual void OnWriteComplete(int result);
60
[email protected]398dca82012-04-25 17:54:5361 static bool StringAndPortToAddressList(const std::string& ip_address_str,
62 int port,
63 net::AddressList* address_list);
64 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
65 int port,
66 net::IPEndPoint* ip_end_point);
67 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
68 std::string* ip_address_str,
69 int* port);
70
[email protected]d47c11f2012-01-26 18:15:0471 protected:
[email protected]398dca82012-04-25 17:54:5372 explicit Socket(APIResourceEventNotifier* event_notifier);
[email protected]d47c11f2012-01-26 18:15:0473
[email protected]58edca52012-02-16 21:51:3874 const std::string address_;
75 int port_;
[email protected]fc44a4bef2011-12-16 01:18:4276 bool is_connected_;
[email protected]fc44a4bef2011-12-16 01:18:4277};
78
79} // namespace extensions
80
81#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_