Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / socket / tls_socket.h
blob0c4d87ea86edfb3a7c91d8a453eafd53596338e4
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_
8 #include <string>
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"
15 namespace net {
16 class Socket;
17 class CertVerifier;
18 class TransportSecurityState;
21 namespace extensions {
23 class TLSSocket;
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;
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.
47 // Fails.
48 void Connect(const net::AddressList& address,
49 const CompletionCallback& callback) override;
50 // Forwards.
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
59 // invoked.
60 bool SetKeepAlive(bool enable, int delay) override;
62 // Fails. This should have been called on the TCP socket before secure() was
63 // invoked.
64 bool SetNoDelay(bool no_delay) override;
66 // Fails. TLSSocket is only a client.
67 int Listen(const std::string& address,
68 uint16 port,
69 int backlog,
70 std::string* error_msg) override;
72 // Fails. TLSSocket is only a client.
73 void Accept(const AcceptCompletionCallback& callback) override;
75 // Forwards.
76 bool IsConnected() override;
78 // Forwards.
79 bool GetPeerAddress(net::IPEndPoint* address) override;
80 // Forwards.
81 bool GetLocalAddress(net::IPEndPoint* address) override;
83 // Returns TYPE_TLS.
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|
89 // may be NULL.
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(
95 Socket* socket,
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 api::socket::SecureOptions* options,
101 const SecureCallback& callback);
103 private:
104 int WriteImpl(net::IOBuffer* io_buffer,
105 int io_buffer_size,
106 const net::CompletionCallback& callback) override;
108 void OnReadComplete(const scoped_refptr<net::IOBuffer>& io_buffer,
109 int result);
111 scoped_ptr<net::StreamSocket> tls_socket_;
112 ReadCompletionCallback read_callback_;
115 } // namespace extensions
117 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_