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