Chromium Code Reviews| 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 const std::string& hostname() const { return hostname_; } | |
| 47 | |
| 48 // The hostname of the remote host that this socket is connected to. Note: | |
| 49 // This may contain an IP literal. In the case of IDNs, this will contain | |
| 50 // a series of U-LABELs (UTF-8), not A-LABELs. IP literals for IPv6 will | |
| 51 // be unbracketed. | |
|
Ryan Sleevi
2014/07/15 00:18:45
This doc-comment documents what hostname_ stores,
lally
2014/07/16 16:19:44
Done.
| |
| 52 void set_hostname(const std::string& hostname) { hostname_ = hostname; } | |
| 53 | |
| 54 // Note: |address| contains the resolved IP address, not the hostname of | |
| 55 // the remote endpoint. In order to upgrade this socket to TLS, callers | |
| 56 // must also supply the hostname of the endpoint via set_hostname(). | |
| 45 virtual void Connect(const std::string& address, | 57 virtual void Connect(const std::string& address, |
| 46 int port, | 58 int port, |
| 47 const CompletionCallback& callback) = 0; | 59 const CompletionCallback& callback) = 0; |
| 48 virtual void Disconnect() = 0; | 60 virtual void Disconnect() = 0; |
| 49 virtual int Bind(const std::string& address, int port) = 0; | 61 virtual int Bind(const std::string& address, int port) = 0; |
| 50 | 62 |
| 51 // The |callback| will be called with the number of bytes read into the | 63 // The |callback| will be called with the number of bytes read into the |
| 52 // buffer, or a negative number if an error occurred. | 64 // buffer, or a negative number if an error occurred. |
| 53 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; | 65 virtual void Read(int count, const ReadCompletionCallback& callback) = 0; |
| 54 | 66 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 | 105 |
| 94 protected: | 106 protected: |
| 95 explicit Socket(const std::string& owner_extension_id_); | 107 explicit Socket(const std::string& owner_extension_id_); |
| 96 | 108 |
| 97 void WriteData(); | 109 void WriteData(); |
| 98 virtual int WriteImpl(net::IOBuffer* io_buffer, | 110 virtual int WriteImpl(net::IOBuffer* io_buffer, |
| 99 int io_buffer_size, | 111 int io_buffer_size, |
| 100 const net::CompletionCallback& callback) = 0; | 112 const net::CompletionCallback& callback) = 0; |
| 101 virtual void OnWriteComplete(int result); | 113 virtual void OnWriteComplete(int result); |
| 102 | 114 |
| 103 const std::string address_; | 115 std::string hostname_; |
| 104 bool is_connected_; | 116 bool is_connected_; |
| 105 | 117 |
| 106 private: | 118 private: |
| 107 friend class ApiResourceManager<Socket>; | 119 friend class ApiResourceManager<Socket>; |
| 108 static const char* service_name() { return "SocketManager"; } | 120 static const char* service_name() { return "SocketManager"; } |
| 109 | 121 |
| 110 struct WriteRequest { | 122 struct WriteRequest { |
| 111 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, | 123 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer, |
| 112 int byte_count, | 124 int byte_count, |
| 113 const CompletionCallback& callback); | 125 const CompletionCallback& callback); |
| 114 ~WriteRequest(); | 126 ~WriteRequest(); |
| 115 scoped_refptr<net::IOBuffer> io_buffer; | 127 scoped_refptr<net::IOBuffer> io_buffer; |
| 116 int byte_count; | 128 int byte_count; |
| 117 CompletionCallback callback; | 129 CompletionCallback callback; |
| 118 int bytes_written; | 130 int bytes_written; |
| 119 }; | 131 }; |
| 120 std::queue<WriteRequest> write_queue_; | 132 std::queue<WriteRequest> write_queue_; |
| 121 scoped_refptr<net::IOBuffer> io_buffer_write_; | 133 scoped_refptr<net::IOBuffer> io_buffer_write_; |
| 122 }; | 134 }; |
| 123 | 135 |
| 124 } // namespace extensions | 136 } // namespace extensions |
| 125 | 137 |
| 126 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ | 138 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_H_ |
| OLD | NEW |