| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 typedef base::Callback< | 32 typedef base::Callback< |
| 33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> | 33 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> |
| 34 RecvFromCompletionCallback; | 34 RecvFromCompletionCallback; |
| 35 typedef base::Callback<void(int, net::TCPClientSocket*)> | 35 typedef base::Callback<void(int, net::TCPClientSocket*)> |
| 36 AcceptCompletionCallback; | 36 AcceptCompletionCallback; |
| 37 | 37 |
| 38 // A Socket wraps a low-level socket and includes housekeeping information that | 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. | 39 // we need to manage it in the context of an extension. |
| 40 class Socket : public ApiResource { | 40 class Socket : public ApiResource { |
| 41 public: | 41 public: |
| 42 enum SocketType { TYPE_TCP, TYPE_UDP, }; | 42 enum SocketType { TYPE_TCP, TYPE_UDP, TYPE_TLS }; |
| 43 | 43 |
| 44 virtual ~Socket(); | 44 virtual ~Socket(); |
| 45 |
| 46 // The hostname of the remote host that this socket is connected to. This |
| 47 // may be the empty string if the client does not intend to ever upgrade the |
| 48 // socket to TLS, and thusly has not invoked set_hostname(). |
| 49 const std::string& hostname() const { return hostname_; } |
| 50 |
| 51 // Set the hostname of the remote host that this socket is connected to. |
| 52 // Note: This may be an IP literal. In the case of IDNs, this should be a |
| 53 // series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will be |
| 54 // unbracketed. |
| 55 void set_hostname(const std::string& hostname) { hostname_ = hostname; } |
| 56 |
| 57 // Note: |address| contains the resolved IP address, not the hostname of |
| 58 // the remote endpoint. In order to upgrade this socket to TLS, callers |
| 59 // must also supply the hostname of the endpoint via set_hostname(). |
| 45 virtual void Connect(const std::string& address, | 60 virtual void Connect(const std::string& address, |
| 46 int port, | 61 int port, |
| 47 const CompletionCallback& callback) = 0; | 62 const CompletionCallback& callback) = 0; |
| 48 virtual void Disconnect() = 0; | 63 virtual void Disconnect() = 0; |
| 49 virtual int Bind(const std::string& address, int port) = 0; | 64 virtual int Bind(const std::string& address, int port) = 0; |
| 50 | 65 |
| 51 // The |callback| will be called with the number of bytes read into the | 66 // The |callback| will be called with the number of bytes read into the |
| 52 // buffer, or a negative number if an error occurred. | 67 // buffer, or a negative number if an error occurred. |
| 53 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; | 68 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; |
| 54 | 69 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 108 |
| 94 protected: | 109 protected: |
| 95 explicit Socket(const std::string& owner_extension_id_); | 110 explicit Socket(const std::string& owner_extension_id_); |
| 96 | 111 |
| 97 void WriteData(); | 112 void WriteData(); |
| 98 virtual int WriteImpl(net::IOBuffer* io_buffer, | 113 virtual int WriteImpl(net::IOBuffer* io_buffer, |
| 99 int io_buffer_size, | 114 int io_buffer_size, |
| 100 const net::CompletionCallback& callback) = 0; | 115 const net::CompletionCallback& callback) = 0; |
| 101 virtual void OnWriteComplete(int result); | 116 virtual void OnWriteComplete(int result); |
| 102 | 117 |
| 103 const std::string address_; | 118 std::string hostname_; |
| 104 bool is_connected_; | 119 bool is_connected_; |
| 105 | 120 |
| 106 private: | 121 private: |
| 107 friend class ApiResourceManager<Socket>; | 122 friend class ApiResourceManager<Socket>; |
| 108 static const char* service_name() { return "SocketManager"; } | 123 static const char* service_name() { return "SocketManager"; } |
| 109 | 124 |
| 110 struct WriteRequest { | 125 struct WriteRequest { |
| 111 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, | 126 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, |
| 112 int byte_count, | 127 int byte_count, |
| 113 const CompletionCallback& callback); | 128 const CompletionCallback& callback); |
| 114 ~WriteRequest(); | 129 ~WriteRequest(); |
| 115 scoped_refptr<net::IOBuffer> io_buffer; | 130 scoped_refptr<net::IOBuffer> io_buffer; |
| 116 int byte_count; | 131 int byte_count; |
| 117 CompletionCallback callback; | 132 CompletionCallback callback; |
| 118 int bytes_written; | 133 int bytes_written; |
| 119 }; | 134 }; |
| 120 std::queue<WriteRequest> write_queue_; | 135 std::queue<WriteRequest> write_queue_; |
| 121 scoped_refptr<net::IOBuffer> io_buffer_write_; | 136 scoped_refptr<net::IOBuffer> io_buffer_write_; |
| 122 }; | 137 }; |
| 123 | 138 |
| 124 } // namespace extensions | 139 } // namespace extensions |
| 125 | 140 |
| 126 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 141 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| OLD | NEW |