OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
| 11 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "chrome/browser/extensions/api/api_resource.h" | 13 #include "chrome/browser/extensions/api/api_resource.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 class AddressList; | 17 class AddressList; |
17 class IPEndPoint; | 18 class IPEndPoint; |
18 class Socket; | 19 class Socket; |
19 } | 20 } |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
| 24 typedef base::Callback<void(int)> CompletionCallback; |
| 25 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
| 26 ReadCompletionCallback; |
| 27 typedef base::Callback< |
| 28 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
| 29 RecvFromCompletionCallback; |
| 30 |
23 // A Socket wraps a low-level socket and includes housekeeping information that | 31 // A Socket wraps a low-level socket and includes housekeeping information that |
24 // we need to manage it in the context of an extension. | 32 // we need to manage it in the context of an extension. |
25 class Socket : public APIResource { | 33 class Socket : public APIResource { |
26 public: | 34 public: |
27 virtual ~Socket(); | 35 virtual ~Socket(); |
28 | 36 |
29 // Returns net::OK if successful, or an error code otherwise. | 37 // Returns net::OK if successful, or an error code otherwise. |
30 virtual int Connect(const std::string& address, int port) = 0; | 38 virtual void Connect(const std::string& address, |
| 39 int port, |
| 40 const CompletionCallback& callback) = 0; |
31 virtual void Disconnect() = 0; | 41 virtual void Disconnect() = 0; |
32 | 42 |
33 virtual int Bind(const std::string& address, int port) = 0; | 43 virtual int Bind(const std::string& address, int port) = 0; |
34 | 44 |
35 // Returns the number of bytes read into the buffer, or a negative number if | 45 // Returns the number of bytes read into the buffer, or a negative number if |
36 // an error occurred. | 46 // an error occurred. |
37 virtual int Read(scoped_refptr<net::IOBuffer> io_buffer, | 47 virtual void Read(int count, |
38 int io_buffer_size) = 0; | 48 const ReadCompletionCallback& callback) = 0; |
39 | 49 |
40 // Returns the number of bytes successfully written, or a negative error | 50 // 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 | 51 // code. Note that ERR_IO_PENDING means that the operation blocked, in which |
42 // case |event_notifier| (supplied at socket creation) will eventually be | 52 // case |event_notifier| (supplied at socket creation) will eventually be |
43 // called with the final result (again, either a nonnegative number of bytes | 53 // called with the final result (again, either a nonnegative number of bytes |
44 // written, or a negative error). | 54 // written, or a negative error). |
45 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, | 55 virtual void Write(scoped_refptr<net::IOBuffer> io_buffer, |
46 int byte_count) = 0; | 56 int byte_count, |
| 57 const CompletionCallback& callback) = 0; |
47 | 58 |
48 virtual int RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, | 59 virtual void RecvFrom(int count, |
49 int io_buffer_size, | 60 const RecvFromCompletionCallback& callback) = 0; |
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 | 61 |
56 virtual void OnDataRead(scoped_refptr<net::IOBuffer> io_buffer, | 62 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
57 net::IPEndPoint *address, | 63 int byte_count, |
58 int result); | 64 const std::string& address, |
59 virtual void OnWriteComplete(int result); | 65 int port, |
| 66 const CompletionCallback& callback) = 0; |
60 | 67 |
61 static bool StringAndPortToAddressList(const std::string& ip_address_str, | 68 static bool StringAndPortToAddressList(const std::string& ip_address_str, |
62 int port, | 69 int port, |
63 net::AddressList* address_list); | 70 net::AddressList* address_list); |
64 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, | 71 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
65 int port, | 72 int port, |
66 net::IPEndPoint* ip_end_point); | 73 net::IPEndPoint* ip_end_point); |
67 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, | 74 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
68 std::string* ip_address_str, | 75 std::string* ip_address_str, |
69 int* port); | 76 int* port); |
70 | 77 |
71 protected: | 78 protected: |
72 explicit Socket(APIResourceEventNotifier* event_notifier); | 79 explicit Socket(APIResourceEventNotifier* event_notifier); |
73 | |
74 const std::string address_; | 80 const std::string address_; |
75 int port_; | 81 int port_; |
76 bool is_connected_; | 82 bool is_connected_; |
77 }; | 83 }; |
78 | 84 |
79 } // namespace extensions | 85 } // namespace extensions |
80 | 86 |
81 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 87 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
OLD | NEW |