OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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_TLS_SOCKET_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "chrome/browser/extensions/api/socket/socket.h" |
| 11 #include "chrome/browser/extensions/api/socket/socket_api.h" |
| 12 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 13 #include "net/ssl/ssl_config_service.h" |
| 14 |
| 15 namespace net { |
| 16 class Socket; |
| 17 } |
| 18 |
| 19 namespace extensions { |
| 20 class TLSSocket; |
| 21 typedef base::Callback<void(scoped_ptr<TLSSocket>, int)> SecureCallback; |
| 22 |
| 23 // TLS Sockets from the chrome.socket API. A regular TCPSocket is converted |
| 24 // to a TLSSocket via chrome.socket.secure() or chrome.sockets.tcp.secure(). |
| 25 // The inheritance here is for interface API compatibility, not for the |
| 26 // implementation that comes with it. TLSSocket does not use its superclass's |
| 27 // socket state, so all methods are overridden here to prevent any access of |
| 28 // ResumableTCPSocket's socket state. Except for the implementation of a |
| 29 // write queue in Socket::Write() (a super-super class of |
| 30 // ResumableTCPSocket). That implementation only queues and serializes |
| 31 // invocations to WriteImpl(), implemented here, and does not touch any |
| 32 // socket state. |
| 33 class TLSSocket : public ResumableTCPSocket { |
| 34 public: |
| 35 TLSSocket(scoped_ptr<net::StreamSocket> tls_socket, |
| 36 const std::string& owner_extension_id); |
| 37 |
| 38 virtual ~TLSSocket(); |
| 39 |
| 40 // Most of these methods either fail or forward the method call on to the |
| 41 // inner net::StreamSocket. The remaining few do actual TLS work. |
| 42 |
| 43 // Fails. |
| 44 virtual void Connect(const std::string& address, |
| 45 int port, |
| 46 const CompletionCallback& callback) OVERRIDE; |
| 47 // Forwards. |
| 48 virtual void Disconnect() OVERRIDE; |
| 49 |
| 50 // The |callback| will be called with the number of bytes read into the |
| 51 // buffer, or a negative number if an error occurred. Does TLS work. |
| 52 virtual void Read(int count, const ReadCompletionCallback& callback) OVERRIDE; |
| 53 |
| 54 // Fails. This should have been called on the TCP socket before secure() was |
| 55 // invoked. |
| 56 virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE; |
| 57 |
| 58 // Fails. This should have been called on the TCP socket before secure() was |
| 59 // invoked. |
| 60 virtual bool SetNoDelay(bool no_delay) OVERRIDE; |
| 61 |
| 62 // Fails. TLSSocket is only a client. |
| 63 virtual int Listen(const std::string& address, |
| 64 int port, |
| 65 int backlog, |
| 66 std::string* error_msg) OVERRIDE; |
| 67 |
| 68 // Fails. TLSSocket is only a client. |
| 69 virtual void Accept(const AcceptCompletionCallback& callback) OVERRIDE; |
| 70 |
| 71 // Forwards. |
| 72 virtual bool IsConnected() OVERRIDE; |
| 73 |
| 74 // Forwards. |
| 75 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE; |
| 76 // Forwards. |
| 77 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE; |
| 78 |
| 79 // Returns TYPE_TLS. |
| 80 virtual SocketType GetSocketType() const OVERRIDE; |
| 81 |
| 82 // Convert |socket| to a TLS socket. |socket| must be an open TCP client |
| 83 // socket. |socket| must not have a pending read. UpgradeSocketToTLS must |
| 84 // be invoked in the IO thread. |callback| will always be invoked. Note: |
| 85 // |callback| may be synchronously invoked before UpgradeSocketToTLS |
| 86 // returns. |
| 87 static void UpgradeSocketToTLS( |
| 88 Socket* socket, |
| 89 scoped_refptr<net::SSLConfigService> config_service, |
| 90 scoped_refptr<net::URLRequestContextGetter> url_request_getter, |
| 91 const std::string& extension_id, |
| 92 api::socket::SecureOptions* options, |
| 93 const SecureCallback& callback); |
| 94 |
| 95 private: |
| 96 virtual int WriteImpl(net::IOBuffer* io_buffer, |
| 97 int io_buffer_size, |
| 98 const net::CompletionCallback& callback) OVERRIDE; |
| 99 |
| 100 void OnReadComplete(const scoped_refptr<net::IOBuffer>& io_buffer, |
| 101 int result); |
| 102 |
| 103 static void TlsConnectDone(scoped_ptr<net::SSLClientSocket> ssl_socket, |
| 104 const std::string& extension_id, |
| 105 const SecureCallback& callback, |
| 106 int result); |
| 107 |
| 108 scoped_ptr<net::StreamSocket> tls_socket_; |
| 109 ReadCompletionCallback read_callback_; |
| 110 }; |
| 111 |
| 112 } // namespace extensions |
| 113 |
| 114 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_ |
OLD | NEW |