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 <queue> | |
9 #include <string> | 10 #include <string> |
11 #include <utility> | |
10 | 12 |
11 #include "base/callback.h" | 13 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
13 #include "chrome/browser/extensions/api/api_resource.h" | 15 #include "chrome/browser/extensions/api/api_resource.h" |
16 #include "net/base/completion_callback.h" | |
14 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
15 | 18 |
16 namespace net { | 19 namespace net { |
17 class AddressList; | 20 class AddressList; |
18 class IPEndPoint; | 21 class IPEndPoint; |
19 class Socket; | 22 class Socket; |
20 } | 23 } |
21 | 24 |
22 namespace extensions { | 25 namespace extensions { |
23 | 26 |
24 typedef base::Callback<void(int)> CompletionCallback; | 27 typedef base::Callback<void(int)> CompletionCallback; |
25 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> | 28 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
26 ReadCompletionCallback; | 29 ReadCompletionCallback; |
27 typedef base::Callback< | 30 typedef base::Callback< |
28 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> | 31 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
29 RecvFromCompletionCallback; | 32 RecvFromCompletionCallback; |
30 | 33 |
31 // A Socket wraps a low-level socket and includes housekeeping information that | 34 // A Socket wraps a low-level socket and includes housekeeping information that |
32 // we need to manage it in the context of an extension. | 35 // we need to manage it in the context of an extension. |
33 class Socket : public APIResource { | 36 class Socket : public APIResource { |
34 public: | 37 public: |
35 virtual ~Socket(); | 38 virtual ~Socket(); |
36 | |
37 // Returns net::OK if successful, or an error code otherwise. | |
38 virtual void Connect(const std::string& address, | 39 virtual void Connect(const std::string& address, |
39 int port, | 40 int port, |
40 const CompletionCallback& callback) = 0; | 41 const CompletionCallback& callback) = 0; |
41 virtual void Disconnect() = 0; | 42 virtual void Disconnect() = 0; |
42 | |
43 virtual int Bind(const std::string& address, int port) = 0; | 43 virtual int Bind(const std::string& address, int port) = 0; |
44 | 44 |
45 // Returns the number of bytes read into the buffer, or a negative number if | 45 // The |callback| will be called with the number of bytes read into the |
46 // an error occurred. | 46 // buffer, or a negative number if an error occurred. |
47 virtual void Read(int count, | 47 virtual void Read(int count, |
48 const ReadCompletionCallback& callback) = 0; | 48 const ReadCompletionCallback& callback) = 0; |
49 | 49 |
50 // Returns the number of bytes successfully written, or a negative error | 50 // The |callback| will be called with the number of bytes successfully |
51 // code. Note that ERR_IO_PENDING means that the operation blocked, in which | 51 // written, or a negative number if an error occurred. |
jeremya
2012/05/18 01:32:12
The number of bytes will always be the same as the
Peng
2012/05/18 15:12:19
Done.
| |
52 // case |event_notifier| (supplied at socket creation) will eventually be | 52 void Write(scoped_refptr<net::IOBuffer> io_buffer, |
53 // called with the final result (again, either a nonnegative number of bytes | 53 int byte_count, |
54 // written, or a negative error). | 54 const CompletionCallback& callback); |
55 virtual void Write(scoped_refptr<net::IOBuffer> io_buffer, | |
56 int byte_count, | |
57 const CompletionCallback& callback) = 0; | |
58 | 55 |
59 virtual void RecvFrom(int count, | 56 virtual void RecvFrom(int count, |
60 const RecvFromCompletionCallback& callback) = 0; | 57 const RecvFromCompletionCallback& callback) = 0; |
61 | |
62 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 58 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
63 int byte_count, | 59 int byte_count, |
64 const std::string& address, | 60 const std::string& address, |
65 int port, | 61 int port, |
66 const CompletionCallback& callback) = 0; | 62 const CompletionCallback& callback) = 0; |
67 | |
68 static bool StringAndPortToAddressList(const std::string& ip_address_str, | 63 static bool StringAndPortToAddressList(const std::string& ip_address_str, |
69 int port, | 64 int port, |
70 net::AddressList* address_list); | 65 net::AddressList* address_list); |
71 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, | 66 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, |
72 int port, | 67 int port, |
73 net::IPEndPoint* ip_end_point); | 68 net::IPEndPoint* ip_end_point); |
74 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, | 69 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, |
75 std::string* ip_address_str, | 70 std::string* ip_address_str, |
76 int* port); | 71 int* port); |
77 | 72 |
78 protected: | 73 protected: |
79 explicit Socket(APIResourceEventNotifier* event_notifier); | 74 explicit Socket(APIResourceEventNotifier* event_notifier); |
75 | |
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 | |
80 const std::string address_; | 82 const std::string address_; |
81 int port_; | 83 int port_; |
82 bool is_connected_; | 84 bool is_connected_; |
85 | |
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 scoped_refptr<net::IOBuffer> io_buffer; | |
96 int byte_count; | |
97 CompletionCallback callback; | |
98 int bytes_written; | |
99 }; | |
100 std::queue<WriteRequest> write_queue_; | |
101 scoped_refptr<net::IOBuffer> io_buffer_write_; | |
83 }; | 102 }; |
84 | 103 |
85 } // namespace extensions | 104 } // namespace extensions |
86 | 105 |
87 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ | 106 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ |
OLD | NEW |