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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_
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"
18 class TransportSecurityState
;
21 namespace extensions
{
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
{
37 typedef base::Callback
<void(scoped_ptr
<TLSSocket
>, int)> SecureCallback
;
39 TLSSocket(scoped_ptr
<net::StreamSocket
> tls_socket
,
40 const std::string
& owner_extension_id
);
42 ~TLSSocket() override
;
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.
48 void Connect(const net::AddressList
& address
,
49 const CompletionCallback
& callback
) override
;
51 void Disconnect() override
;
53 // Attempts to read |count| bytes of decrypted data from the TLS socket,
54 // invoking |callback| with the actual number of bytes read, or a network
55 // error code if an error occurred.
56 void Read(int count
, const ReadCompletionCallback
& callback
) override
;
58 // Fails. This should have been called on the TCP socket before secure() was
60 bool SetKeepAlive(bool enable
, int delay
) override
;
62 // Fails. This should have been called on the TCP socket before secure() was
64 bool SetNoDelay(bool no_delay
) override
;
66 // Fails. TLSSocket is only a client.
67 int Listen(const std::string
& address
,
70 std::string
* error_msg
) override
;
72 // Fails. TLSSocket is only a client.
73 void Accept(const AcceptCompletionCallback
& callback
) override
;
76 bool IsConnected() override
;
79 bool GetPeerAddress(net::IPEndPoint
* address
) override
;
81 bool GetLocalAddress(net::IPEndPoint
* address
) override
;
84 SocketType
GetSocketType() const override
;
86 // Convert |socket| to a TLS socket. |socket| must be an open TCP client
87 // socket. |socket| must not have a pending read. UpgradeSocketToTLS() must
88 // be invoked in the IO thread. |callback| will always be invoked. |options|
90 // Note: |callback| may be synchronously invoked before
91 // UpgradeSocketToTLS() returns. Currently using the older chrome.socket
92 // version of SecureOptions, to avoid having the older API implementation
93 // depend on the newer one.
94 static void UpgradeSocketToTLS(
96 scoped_refptr
<net::SSLConfigService
> config_service
,
97 net::CertVerifier
* cert_verifier
,
98 net::TransportSecurityState
* transport_security_state
,
99 const std::string
& extension_id
,
100 core_api::socket::SecureOptions
* options
,
101 const SecureCallback
& callback
);
104 int WriteImpl(net::IOBuffer
* io_buffer
,
106 const net::CompletionCallback
& callback
) override
;
108 void OnReadComplete(const scoped_refptr
<net::IOBuffer
>& io_buffer
,
111 scoped_ptr
<net::StreamSocket
> tls_socket_
;
112 ReadCompletionCallback read_callback_
;
115 } // namespace extensions
117 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_