OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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 "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.socket.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 // The |callback| will be called with the number of bytes read into the | |
55 // buffer, or a negative number if an error occurred. Does TLS work. | |
Ryan Sleevi
2014/07/15 00:18:45
"Does TLS work" should be expanded. At first, I th
lally
2014/07/16 16:19:45
Done.
| |
56 virtual void Read(int count, const ReadCompletionCallback& callback) OVERRIDE; | |
57 | |
58 // Fails. This should have been called on the TCP socket before secure() was | |
59 // invoked. | |
60 virtual bool SetKeepAlive(bool enable, int delay) OVERRIDE; | |
61 | |
62 // Fails. This should have been called on the TCP socket before secure() was | |
63 // invoked. | |
64 virtual bool SetNoDelay(bool no_delay) OVERRIDE; | |
65 | |
66 // Fails. TLSSocket is only a client. | |
67 virtual int Listen(const std::string& address, | |
68 int port, | |
69 int backlog, | |
70 std::string* error_msg) OVERRIDE; | |
71 | |
72 // Fails. TLSSocket is only a client. | |
73 virtual void Accept(const AcceptCompletionCallback& callback) OVERRIDE; | |
74 | |
75 // Forwards. | |
76 virtual bool IsConnected() OVERRIDE; | |
77 | |
78 // Forwards. | |
79 virtual bool GetPeerAddress(net::IPEndPoint* address) OVERRIDE; | |
80 // Forwards. | |
81 virtual bool GetLocalAddress(net::IPEndPoint* address) OVERRIDE; | |
82 | |
83 // Returns TYPE_TLS. | |
84 virtual SocketType GetSocketType() const OVERRIDE; | |
85 | |
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. Note: |callback| may be synchronously invoked before | |
Ryan Sleevi
2014/07/15 00:18:45
nit: line break before the Note:
lally
2014/07/16 16:19:45
Done.
| |
90 // UpgradeSocketToTLS() returns. Currently using the older chrome.socket | |
91 // version of SecureOptions, to avoid having the older API implementation | |
92 // depend on the newer one. | |
93 static void UpgradeSocketToTLS( | |
94 Socket* socket, | |
95 scoped_refptr<net::SSLConfigService> config_service, | |
96 net::CertVerifier* cert_verifier, | |
97 net::TransportSecurityState* transport_security_state, | |
98 const std::string& extension_id, | |
99 core_api::socket::SecureOptions* options, | |
100 const SecureCallback& callback); | |
101 | |
102 private: | |
103 virtual int WriteImpl(net::IOBuffer* io_buffer, | |
104 int io_buffer_size, | |
105 const net::CompletionCallback& callback) OVERRIDE; | |
106 | |
107 void OnReadComplete(const scoped_refptr<net::IOBuffer>& io_buffer, | |
108 int result); | |
109 | |
110 static void TlsConnectDone(scoped_ptr<net::SSLClientSocket> ssl_socket, | |
111 const std::string& extension_id, | |
112 const SecureCallback& callback, | |
113 int result); | |
114 | |
115 scoped_ptr<net::StreamSocket> tls_socket_; | |
116 ReadCompletionCallback read_callback_; | |
117 }; | |
118 | |
119 } // namespace extensions | |
120 | |
121 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_TLS_SOCKET_H_ | |
OLD | NEW |