1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// 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
8#include <queue>
9#include <string>
10#include <utility>
11
12#include "base/callback.h"
13#include "base/memory/ref_counted.h"
14#include "chrome/browser/extensions/api/api_resource.h"
15#include "chrome/browser/extensions/api/api_resource_manager.h"
16#include "net/base/completion_callback.h"
17#include "net/base/io_buffer.h"
18#include "net/base/ip_endpoint.h"
19#include "net/socket/tcp_client_socket.h"
20
21namespace net {
22class AddressList;
23class IPEndPoint;
24class Socket;
25}
26
27namespace extensions {
28
29typedef base::Callback<void(int)> CompletionCallback;
30typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
31    ReadCompletionCallback;
32typedef base::Callback<
33  void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
34      RecvFromCompletionCallback;
35typedef base::Callback<
36  void(int, net::TCPClientSocket*)> AcceptCompletionCallback;
37
38// A Socket wraps a low-level socket and includes housekeeping information that
39// we need to manage it in the context of an extension.
40class Socket : public ApiResource {
41 public:
42  enum SocketType {
43    TYPE_TCP,
44    TYPE_UDP,
45  };
46
47  virtual ~Socket();
48  virtual void Connect(const std::string& address,
49                       int port,
50                       const CompletionCallback& callback) = 0;
51  virtual void Disconnect() = 0;
52  virtual int Bind(const std::string& address, int port) = 0;
53
54  // The |callback| will be called with the number of bytes read into the
55  // buffer, or a negative number if an error occurred.
56  virtual void Read(int count,
57                    const ReadCompletionCallback& callback) = 0;
58
59  // The |callback| will be called with |byte_count| or a negative number if an
60  // error occurred.
61  void Write(scoped_refptr<net::IOBuffer> io_buffer,
62             int byte_count,
63             const CompletionCallback& callback);
64
65  virtual void RecvFrom(int count,
66                        const RecvFromCompletionCallback& callback) = 0;
67  virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
68                      int byte_count,
69                      const std::string& address,
70                      int port,
71                      const CompletionCallback& callback) = 0;
72
73  virtual bool SetKeepAlive(bool enable, int delay);
74  virtual bool SetNoDelay(bool no_delay);
75  virtual int Listen(const std::string& address, int port, int backlog,
76                     std::string* error_msg);
77  virtual void Accept(const AcceptCompletionCallback &callback);
78
79  virtual bool IsConnected() = 0;
80
81  virtual bool GetPeerAddress(net::IPEndPoint* address) = 0;
82  virtual bool GetLocalAddress(net::IPEndPoint* address) = 0;
83
84  virtual SocketType GetSocketType() const = 0;
85
86  static bool StringAndPortToAddressList(const std::string& ip_address_str,
87                                         int port,
88                                         net::AddressList* address_list);
89  static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
90                                        int port,
91                                        net::IPEndPoint* ip_end_point);
92  static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
93                                        std::string* ip_address_str,
94                                        int* port);
95
96 protected:
97  explicit Socket(const std::string& owner_extension_id_);
98
99  void WriteData();
100  virtual int WriteImpl(net::IOBuffer* io_buffer,
101                        int io_buffer_size,
102                        const net::CompletionCallback& callback) = 0;
103  virtual void OnWriteComplete(int result);
104
105  const std::string address_;
106  bool is_connected_;
107
108 private:
109  friend class ApiResourceManager<Socket>;
110  static const char* service_name() {
111    return "SocketManager";
112  }
113
114  struct WriteRequest {
115    WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
116                 int byte_count,
117                 const CompletionCallback& callback);
118    ~WriteRequest();
119    scoped_refptr<net::IOBuffer> io_buffer;
120    int byte_count;
121    CompletionCallback callback;
122    int bytes_written;
123  };
124  std::queue<WriteRequest> write_queue_;
125  scoped_refptr<net::IOBuffer> io_buffer_write_;
126};
127
128}  //  namespace extensions
129
130#endif  // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
131