[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 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 NET_WEBSOCKETS_WEBSOCKET_EVENT_INTERFACE_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_EVENT_INTERFACE_H_ |
| 7 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 8 | #include <stdint.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 9 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 14 | #include "base/callback_forward.h" |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 15 | #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 16 | #include "base/macros.h" |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 17 | #include "base/memory/scoped_refptr.h" |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 18 | #include "base/optional.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 19 | #include "net/base/net_export.h" |
| 20 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 21 | class GURL; |
| 22 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 23 | namespace net { |
| 24 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 25 | class AuthChallengeInfo; |
| 26 | class AuthCredentials; |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 27 | class IPEndPoint; |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 28 | class HttpResponseHeaders; |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 29 | class IOBuffer; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 30 | class SSLInfo; |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 31 | class URLRequest; |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 32 | struct WebSocketHandshakeRequestInfo; |
| 33 | struct WebSocketHandshakeResponseInfo; |
| 34 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 35 | // Interface for events sent from the network layer to the content layer. These |
| 36 | // events will generally be sent as-is to the renderer process. |
| 37 | class NET_EXPORT WebSocketEventInterface { |
| 38 | public: |
| 39 | typedef int WebSocketMessageType; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 40 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 41 | virtual ~WebSocketEventInterface() {} |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 42 | |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 43 | // Called when a URLRequest is created for handshaking. |
| 44 | virtual void OnCreateURLRequest(URLRequest* request) = 0; |
| 45 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 46 | // Called in response to an AddChannelRequest. This means that a response has |
| 47 | // been received from the remote server. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 48 | virtual void OnAddChannelResponse(const std::string& selected_subprotocol, |
| 49 | const std::string& extensions) = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 50 | |
| 51 | // Called when a data frame has been received from the remote host and needs |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 52 | // to be forwarded to the renderer process. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 53 | virtual void OnDataFrame(bool fin, |
| 54 | WebSocketMessageType type, |
| 55 | scoped_refptr<IOBuffer> buffer, |
| 56 | size_t buffer_size) = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 57 | |
| 58 | // Called to provide more send quota for this channel to the renderer |
| 59 | // process. Currently the quota units are always bytes of message body |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 60 | // data. In future it might depend on the type of multiplexing in use. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 61 | virtual void OnFlowControl(int64_t quota) = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 62 | |
| 63 | // Called when the remote server has Started the WebSocket Closing |
| 64 | // Handshake. The client should not attempt to send any more messages after |
| 65 | // receiving this message. It will be followed by OnDropChannel() when the |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 66 | // closing handshake is complete. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 67 | virtual void OnClosingHandshake() = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 68 | |
| 69 | // Called when the channel has been dropped, either due to a network close, a |
| 70 | // network error, or a protocol error. This may or may not be preceeded by a |
| 71 | // call to OnClosingHandshake(). |
| 72 | // |
| 73 | // Warning: Both the |code| and |reason| are passed through to Javascript, so |
| 74 | // callers must take care not to provide details that could be useful to |
| 75 | // attackers attempting to use WebSockets to probe networks. |
| 76 | // |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 77 | // |was_clean| should be true if the closing handshake completed successfully. |
| 78 | // |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 79 | // The channel should not be used again after OnDropChannel() has been |
| 80 | // called. |
| 81 | // |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 82 | // This function deletes the Channel. |
| 83 | virtual void OnDropChannel(bool was_clean, |
| 84 | uint16_t code, |
| 85 | const std::string& reason) = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 86 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 87 | // Called when the browser fails the channel, as specified in the spec. |
| 88 | // |
| 89 | // The channel should not be used again after OnFailChannel() has been |
| 90 | // called. |
| 91 | // |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 92 | // This function deletes the Channel. |
| 93 | virtual void OnFailChannel(const std::string& message) = 0; |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 94 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 95 | // Called when the browser starts the WebSocket Opening Handshake. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 96 | virtual void OnStartOpeningHandshake( |
| 97 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) = 0; |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 98 | |
| 99 | // Called when the browser finishes the WebSocket Opening Handshake. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 100 | virtual void OnFinishOpeningHandshake( |
| 101 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) = 0; |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 102 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 103 | // Callbacks to be used in response to a call to OnSSLCertificateError. Very |
| 104 | // similar to content::SSLErrorHandler::Delegate (which we can't use directly |
| 105 | // due to layering constraints). |
| 106 | class NET_EXPORT SSLErrorCallbacks { |
| 107 | public: |
| 108 | virtual ~SSLErrorCallbacks() {} |
| 109 | |
| 110 | // Cancels the SSL response in response to the error. |
| 111 | virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) = 0; |
| 112 | |
| 113 | // Continue with the SSL connection despite the error. |
| 114 | virtual void ContinueSSLRequest() = 0; |
| 115 | }; |
| 116 | |
| 117 | // Called on SSL Certificate Error during the SSL handshake. Should result in |
| 118 | // a call to either ssl_error_callbacks->ContinueSSLRequest() or |
| 119 | // ssl_error_callbacks->CancelSSLRequest(). Normally the implementation of |
| 120 | // this method will delegate to content::SSLManager::OnSSLCertificateError to |
| 121 | // make the actual decision. The callbacks must not be called after the |
| 122 | // WebSocketChannel has been destroyed. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 123 | virtual void OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 124 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 125 | const GURL& url, |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame^] | 126 | int net_error, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 127 | const SSLInfo& ssl_info, |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 128 | bool fatal) = 0; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 129 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 130 | // Called when authentication is required. Returns a net error. The opening |
| 131 | // handshake is blocked when this function returns ERR_IO_PENDING. |
| 132 | // In that case calling |callback| resumes the handshake. |callback| can be |
| 133 | // called during the opening handshake. An implementation can rewrite |
| 134 | // |*credentials| (in the sync case) or provide new credentials (in the |
| 135 | // async case). |
| 136 | // Providing null credentials (nullopt in the sync case and nullptr in the |
| 137 | // async case) cancels authentication. Otherwise the new credentials are set |
| 138 | // and the opening handshake will be retried with the credentials. |
| 139 | virtual int OnAuthRequired( |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 140 | const AuthChallengeInfo& auth_info, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 141 | scoped_refptr<HttpResponseHeaders> response_headers, |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 142 | const IPEndPoint& socket_address, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 143 | base::OnceCallback<void(const AuthCredentials*)> callback, |
| 144 | base::Optional<AuthCredentials>* credentials) = 0; |
| 145 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 146 | protected: |
| 147 | WebSocketEventInterface() {} |
| 148 | |
| 149 | private: |
| 150 | DISALLOW_COPY_AND_ASSIGN(WebSocketEventInterface); |
| 151 | }; |
| 152 | |
| 153 | } // namespace net |
| 154 | |
| 155 | #endif // NET_WEBSOCKETS_WEBSOCKET_EVENT_INTERFACE_H_ |