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