blob: 39f60c828a78c101c1ad57d584ab5d29135fdf51 [file] [log] [blame]
[email protected]999bcaa2013-07-17 13:42:541// 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
tfarina8a2c66c22015-10-13 19:14:498#include <stdint.h>
Avi Drissman13fc8932015-12-20 04:40:469
danakj9c5cab52016-04-16 00:54:3310#include <memory>
[email protected]999bcaa2013-07-17 13:42:5411#include <string>
12#include <vector>
13
Yutaka Hirano70fa25912018-06-06 05:26:5414#include "base/callback_forward.h"
[email protected]f485985e2013-10-24 13:47:4415#include "base/compiler_specific.h" // for WARN_UNUSED_RESULT
Yutaka Hirano76aacb202019-09-05 16:36:5616#include "base/containers/span.h"
tfarinaea94afc232015-10-20 04:23:3617#include "base/macros.h"
Bence Béky65623972018-03-05 15:31:5618#include "base/memory/scoped_refptr.h"
Yutaka Hirano70fa25912018-06-06 05:26:5419#include "base/optional.h"
[email protected]999bcaa2013-07-17 13:42:5420#include "net/base/net_export.h"
21
[email protected]a62449522014-06-05 11:11:1522class GURL;
23
[email protected]999bcaa2013-07-17 13:42:5424namespace net {
25
Yutaka Hirano70fa25912018-06-06 05:26:5426class AuthChallengeInfo;
27class AuthCredentials;
Tsuyoshi Horo01faed62019-02-20 22:11:3728class IPEndPoint;
Yutaka Hirano70fa25912018-06-06 05:26:5429class HttpResponseHeaders;
[email protected]a62449522014-06-05 11:11:1530class SSLInfo;
yhirano4a593832016-10-24 18:58:2231class URLRequest;
[email protected]cd48ed12014-01-22 14:34:2232struct WebSocketHandshakeRequestInfo;
33struct WebSocketHandshakeResponseInfo;
34
[email protected]999bcaa2013-07-17 13:42:5435// 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.
37class NET_EXPORT WebSocketEventInterface {
38 public:
39 typedef int WebSocketMessageType;
[email protected]f485985e2013-10-24 13:47:4440
[email protected]999bcaa2013-07-17 13:42:5441 virtual ~WebSocketEventInterface() {}
[email protected]6c5d9f62014-01-27 15:05:2142
yhirano4a593832016-10-24 18:58:2243 // Called when a URLRequest is created for handshaking.
44 virtual void OnCreateURLRequest(URLRequest* request) = 0;
45
tyoshinoc06da562015-03-06 06:02:4246 // Called in response to an AddChannelRequest. This means that a response has
47 // been received from the remote server.
Yutaka Hirano4165de92018-04-10 11:46:4948 virtual void OnAddChannelResponse(const std::string& selected_subprotocol,
49 const std::string& extensions) = 0;
[email protected]999bcaa2013-07-17 13:42:5450
51 // Called when a data frame has been received from the remote host and needs
[email protected]f485985e2013-10-24 13:47:4452 // to be forwarded to the renderer process.
Yutaka Hirano76aacb202019-09-05 16:36:5653 // |payload| stays valid as long as both
54 // - the associated WebSocketChannel is valid.
55 // - no further ReadFrames() is called on the associated WebSocketChannel.
Yutaka Hirano4165de92018-04-10 11:46:4956 virtual void OnDataFrame(bool fin,
57 WebSocketMessageType type,
Yutaka Hirano76aacb202019-09-05 16:36:5658 base::span<const char> payload) = 0;
[email protected]999bcaa2013-07-17 13:42:5459
Yoichi Osatofcaa2a22019-08-28 08:22:3660 // 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]999bcaa2013-07-17 13:42:5464 // Called to provide more send quota for this channel to the renderer
Yoichi Osatob088adc2019-06-06 05:52:1965 // process.
66 virtual void OnSendFlowControlQuotaAdded(int64_t quota) = 0;
[email protected]999bcaa2013-07-17 13:42:5467
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]f485985e2013-10-24 13:47:4471 // closing handshake is complete.
Yutaka Hirano4165de92018-04-10 11:46:4972 virtual void OnClosingHandshake() = 0;
[email protected]999bcaa2013-07-17 13:42:5473
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]86ec55502014-02-10 13:16:1682 // |was_clean| should be true if the closing handshake completed successfully.
83 //
[email protected]999bcaa2013-07-17 13:42:5484 // The channel should not be used again after OnDropChannel() has been
85 // called.
86 //
Yutaka Hirano4165de92018-04-10 11:46:4987 // This function deletes the Channel.
88 virtual void OnDropChannel(bool was_clean,
89 uint16_t code,
90 const std::string& reason) = 0;
[email protected]999bcaa2013-07-17 13:42:5491
[email protected]96868202014-01-09 10:38:0492 // 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 Hirano4165de92018-04-10 11:46:4997 // This function deletes the Channel.
98 virtual void OnFailChannel(const std::string& message) = 0;
[email protected]96868202014-01-09 10:38:0499
[email protected]cd48ed12014-01-22 14:34:22100 // Called when the browser starts the WebSocket Opening Handshake.
Yutaka Hirano4165de92018-04-10 11:46:49101 virtual void OnStartOpeningHandshake(
102 std::unique_ptr<WebSocketHandshakeRequestInfo> request) = 0;
[email protected]cd48ed12014-01-22 14:34:22103
104 // Called when the browser finishes the WebSocket Opening Handshake.
Yutaka Hirano4165de92018-04-10 11:46:49105 virtual void OnFinishOpeningHandshake(
106 std::unique_ptr<WebSocketHandshakeResponseInfo> response) = 0;
[email protected]cd48ed12014-01-22 14:34:22107
[email protected]a62449522014-06-05 11:11:15108 // 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 Hirano4165de92018-04-10 11:46:49128 virtual void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33129 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
[email protected]a62449522014-06-05 11:11:15130 const GURL& url,
Emily Starkd9df3d32019-04-29 17:54:57131 int net_error,
[email protected]a62449522014-06-05 11:11:15132 const SSLInfo& ssl_info,
Yutaka Hirano4165de92018-04-10 11:46:49133 bool fatal) = 0;
[email protected]a62449522014-06-05 11:11:15134
Yutaka Hirano70fa25912018-06-06 05:26:54135 // 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 Starkf2c9bbd2019-04-09 17:08:58145 const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54146 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37147 const IPEndPoint& socket_address,
Yutaka Hirano70fa25912018-06-06 05:26:54148 base::OnceCallback<void(const AuthCredentials*)> callback,
149 base::Optional<AuthCredentials>* credentials) = 0;
150
[email protected]999bcaa2013-07-17 13:42:54151 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_