1 // Copyright 2013 The Chromium Authors 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 8 #include <stdint.h> 9 10 #include <memory> 11 #include <string> 12 #include <vector> 13 14 #include "base/containers/span.h" 15 #include "base/functional/callback_forward.h" 16 #include "base/memory/scoped_refptr.h" 17 #include "net/base/net_export.h" 18 #include "third_party/abseil-cpp/absl/types/optional.h" 19 20 class GURL; 21 22 namespace net { 23 24 class AuthChallengeInfo; 25 class AuthCredentials; 26 class IPEndPoint; 27 class HttpResponseHeaders; 28 class SSLInfo; 29 class URLRequest; 30 struct WebSocketHandshakeRequestInfo; 31 struct WebSocketHandshakeResponseInfo; 32 33 // Interface for events sent from the network layer to the content layer. These 34 // events will generally be sent as-is to the renderer process. 35 class NET_EXPORT WebSocketEventInterface { 36 public: 37 typedef int WebSocketMessageType; 38 39 WebSocketEventInterface(const WebSocketEventInterface&) = delete; 40 WebSocketEventInterface& operator=(const WebSocketEventInterface&) = delete; 41 42 virtual ~WebSocketEventInterface() = default; 43 44 // Called when a URLRequest is created for handshaking. 45 virtual void OnCreateURLRequest(URLRequest* request) = 0; 46 47 // Called in response to an AddChannelRequest. This means that a response has 48 // been received from the remote server. 49 virtual void OnAddChannelResponse( 50 std::unique_ptr<WebSocketHandshakeResponseInfo> response, 51 const std::string& selected_subprotocol, 52 const std::string& extensions) = 0; 53 54 // Called when a data frame has been received from the remote host and needs 55 // to be forwarded to the renderer process. 56 // |payload| stays valid as long as both 57 // - the associated WebSocketChannel is valid. 58 // - no further ReadFrames() is called on the associated WebSocketChannel. 59 virtual void OnDataFrame(bool fin, 60 WebSocketMessageType type, 61 base::span<const char> payload) = 0; 62 63 // Returns true if data pipe is full and waiting the renderer process read 64 // out. The network service should not read more from network until that. 65 virtual bool HasPendingDataFrames() = 0; 66 67 // Called once for each call to SendFrame() once the frame has been passed to 68 // the OS. 69 virtual void OnSendDataFrameDone() = 0; 70 71 // Called when the remote server has Started the WebSocket Closing 72 // Handshake. The client should not attempt to send any more messages after 73 // receiving this message. It will be followed by OnDropChannel() when the 74 // closing handshake is complete. 75 virtual void OnClosingHandshake() = 0; 76 77 // Called when the channel has been dropped, either due to a network close, a 78 // network error, or a protocol error. This may or may not be preceeded by a 79 // call to OnClosingHandshake(). 80 // 81 // Warning: Both the |code| and |reason| are passed through to Javascript, so 82 // callers must take care not to provide details that could be useful to 83 // attackers attempting to use WebSockets to probe networks. 84 // 85 // |was_clean| should be true if the closing handshake completed successfully. 86 // 87 // The channel should not be used again after OnDropChannel() has been 88 // called. 89 // 90 // This function deletes the Channel. 91 virtual void OnDropChannel(bool was_clean, 92 uint16_t code, 93 const std::string& reason) = 0; 94 95 // Called when the browser fails the channel, as specified in the spec. 96 // 97 // The channel should not be used again after OnFailChannel() has been 98 // called. 99 // 100 // |message| is a human readable string describing the failure. (It may be 101 // empty.) |net_error| contains the network error code for the failure, which 102 // may be |OK| if the failure was at a higher level. |response_code| contains 103 // the HTTP status code that caused the failure, or |absl::nullopt| if the 104 // attempt didn't get that far. 105 // 106 // This function deletes the Channel. 107 virtual void OnFailChannel(const std::string& message, 108 int net_error, 109 absl::optional<int> response_code) = 0; 110 111 // Called when the browser starts the WebSocket Opening Handshake. 112 virtual void OnStartOpeningHandshake( 113 std::unique_ptr<WebSocketHandshakeRequestInfo> request) = 0; 114 115 // Callbacks to be used in response to a call to OnSSLCertificateError. Very 116 // similar to content::SSLErrorHandler::Delegate (which we can't use directly 117 // due to layering constraints). 118 class NET_EXPORT SSLErrorCallbacks { 119 public: 120 virtual ~SSLErrorCallbacks() = default; 121 122 // Cancels the SSL response in response to the error. 123 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) = 0; 124 125 // Continue with the SSL connection despite the error. 126 virtual void ContinueSSLRequest() = 0; 127 }; 128 129 // Called on SSL Certificate Error during the SSL handshake. Should result in 130 // a call to either ssl_error_callbacks->ContinueSSLRequest() or 131 // ssl_error_callbacks->CancelSSLRequest(). Normally the implementation of 132 // this method will delegate to content::SSLManager::OnSSLCertificateError to 133 // make the actual decision. The callbacks must not be called after the 134 // WebSocketChannel has been destroyed. 135 virtual void OnSSLCertificateError( 136 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, 137 const GURL& url, 138 int net_error, 139 const SSLInfo& ssl_info, 140 bool fatal) = 0; 141 142 // Called when authentication is required. Returns a net error. The opening 143 // handshake is blocked when this function returns ERR_IO_PENDING. 144 // In that case calling |callback| resumes the handshake. |callback| can be 145 // called during the opening handshake. An implementation can rewrite 146 // |*credentials| (in the sync case) or provide new credentials (in the 147 // async case). 148 // Providing null credentials (nullopt in the sync case and nullptr in the 149 // async case) cancels authentication. Otherwise the new credentials are set 150 // and the opening handshake will be retried with the credentials. 151 virtual int OnAuthRequired( 152 const AuthChallengeInfo& auth_info, 153 scoped_refptr<HttpResponseHeaders> response_headers, 154 const IPEndPoint& socket_address, 155 base::OnceCallback<void(const AuthCredentials*)> callback, 156 absl::optional<AuthCredentials>* credentials) = 0; 157 158 protected: 159 WebSocketEventInterface() = default; 160 }; 161 162 } // namespace net 163 164 #endif // NET_WEBSOCKETS_WEBSOCKET_EVENT_INTERFACE_H_ 165