proxying_websocket.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2020 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. #ifndef ELECTRON_SHELL_BROWSER_NET_PROXYING_WEBSOCKET_H_
  5. #define ELECTRON_SHELL_BROWSER_NET_PROXYING_WEBSOCKET_H_
  6. #include <optional>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "content/public/browser/content_browser_client.h"
  12. #include "extensions/browser/api/web_request/web_request_info.h"
  13. #include "mojo/public/cpp/bindings/pending_receiver.h"
  14. #include "mojo/public/cpp/bindings/receiver.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "services/network/public/cpp/resource_request.h"
  17. #include "services/network/public/mojom/network_context.mojom.h"
  18. #include "services/network/public/mojom/websocket.mojom.h"
  19. #include "shell/browser/net/web_request_api_interface.h"
  20. #include "url/gurl.h"
  21. #include "url/origin.h"
  22. namespace electron {
  23. // A ProxyingWebSocket proxies a WebSocket connection and dispatches
  24. // WebRequest API events.
  25. //
  26. // The code is referenced from the
  27. // extensions::WebRequestProxyingWebSocket class.
  28. class ProxyingWebSocket : public network::mojom::WebSocketHandshakeClient,
  29. public network::mojom::WebSocketAuthenticationHandler,
  30. public network::mojom::TrustedHeaderClient {
  31. public:
  32. using WebSocketFactory = content::ContentBrowserClient::WebSocketFactory;
  33. // AuthRequiredResponse indicates how an OnAuthRequired call is handled.
  34. enum class AuthRequiredResponse {
  35. // No credentials were provided.
  36. kNoAction,
  37. // AuthCredentials is filled in with a username and password, which should
  38. // be used in a response to the provided auth challenge.
  39. kSetAuth,
  40. // The request should be canceled.
  41. kCancelAuth,
  42. // The action will be decided asynchronously. |callback| will be invoked
  43. // when the decision is made, and one of the other AuthRequiredResponse
  44. // values will be passed in with the same semantics as described above.
  45. kIoPending,
  46. };
  47. ProxyingWebSocket(
  48. WebRequestAPI* web_request_api,
  49. WebSocketFactory factory,
  50. const network::ResourceRequest& request,
  51. mojo::PendingRemote<network::mojom::WebSocketHandshakeClient>
  52. handshake_client,
  53. bool has_extra_headers,
  54. int process_id,
  55. int render_frame_id,
  56. content::BrowserContext* browser_context,
  57. uint64_t* request_id_generator);
  58. ~ProxyingWebSocket() override;
  59. // disable copy
  60. ProxyingWebSocket(const ProxyingWebSocket&) = delete;
  61. ProxyingWebSocket& operator=(const ProxyingWebSocket&) = delete;
  62. void Start();
  63. // network::mojom::WebSocketHandshakeClient methods:
  64. void OnOpeningHandshakeStarted(
  65. network::mojom::WebSocketHandshakeRequestPtr request) override;
  66. void OnFailure(const std::string& message,
  67. int32_t net_error,
  68. int32_t response_code) override;
  69. void OnConnectionEstablished(
  70. mojo::PendingRemote<network::mojom::WebSocket> websocket,
  71. mojo::PendingReceiver<network::mojom::WebSocketClient> client_receiver,
  72. network::mojom::WebSocketHandshakeResponsePtr response,
  73. mojo::ScopedDataPipeConsumerHandle readable,
  74. mojo::ScopedDataPipeProducerHandle writable) override;
  75. // network::mojom::WebSocketAuthenticationHandler method:
  76. void OnAuthRequired(const net::AuthChallengeInfo& auth_info,
  77. const scoped_refptr<net::HttpResponseHeaders>& headers,
  78. const net::IPEndPoint& remote_endpoint,
  79. OnAuthRequiredCallback callback) override;
  80. // network::mojom::TrustedHeaderClient methods:
  81. void OnBeforeSendHeaders(const net::HttpRequestHeaders& headers,
  82. OnBeforeSendHeadersCallback callback) override;
  83. void OnHeadersReceived(const std::string& headers,
  84. const net::IPEndPoint& endpoint,
  85. OnHeadersReceivedCallback callback) override;
  86. static void StartProxying(
  87. WebRequestAPI* web_request_api,
  88. WebSocketFactory factory,
  89. const GURL& url,
  90. const net::SiteForCookies& site_for_cookies,
  91. const std::optional<std::string>& user_agent,
  92. mojo::PendingRemote<network::mojom::WebSocketHandshakeClient>
  93. handshake_client,
  94. bool has_extra_headers,
  95. int process_id,
  96. int render_frame_id,
  97. const url::Origin& origin,
  98. content::BrowserContext* browser_context,
  99. uint64_t* request_id_generator);
  100. WebRequestAPI* web_request_api() { return web_request_api_; }
  101. private:
  102. void OnBeforeRequestComplete(int error_code);
  103. void OnBeforeSendHeadersComplete(const std::set<std::string>& removed_headers,
  104. const std::set<std::string>& set_headers,
  105. int error_code);
  106. void ContinueToStartRequest(int error_code);
  107. void OnHeadersReceivedComplete(int error_code);
  108. void ContinueToHeadersReceived();
  109. void OnAuthRequiredComplete(AuthRequiredResponse rv);
  110. void OnHeadersReceivedCompleteForAuth(const net::AuthChallengeInfo& auth_info,
  111. int rv);
  112. void ContinueToCompleted();
  113. void PauseIncomingMethodCallProcessing();
  114. void ResumeIncomingMethodCallProcessing();
  115. void OnError(int error_code);
  116. // This is used for detecting errors on mojo connection with the network
  117. // service.
  118. void OnMojoConnectionErrorWithCustomReason(uint32_t custom_reason,
  119. const std::string& description);
  120. // This is used for detecting errors on mojo connection with original client
  121. // (i.e., renderer).
  122. void OnMojoConnectionError();
  123. // Passed from api::WebRequest.
  124. raw_ptr<WebRequestAPI> web_request_api_;
  125. // Saved to feed the api::WebRequest.
  126. network::ResourceRequest request_;
  127. WebSocketFactory factory_;
  128. mojo::Remote<network::mojom::WebSocketHandshakeClient>
  129. forwarding_handshake_client_;
  130. mojo::Receiver<network::mojom::WebSocketHandshakeClient>
  131. receiver_as_handshake_client_{this};
  132. mojo::Receiver<network::mojom::WebSocketAuthenticationHandler>
  133. receiver_as_auth_handler_{this};
  134. mojo::Receiver<network::mojom::TrustedHeaderClient>
  135. receiver_as_header_client_{this};
  136. net::HttpRequestHeaders request_headers_;
  137. network::mojom::URLResponseHeadPtr response_;
  138. net::AuthCredentials auth_credentials_;
  139. OnAuthRequiredCallback auth_required_callback_;
  140. scoped_refptr<net::HttpResponseHeaders> override_headers_;
  141. std::vector<network::mojom::HttpHeaderPtr> additional_headers_;
  142. OnBeforeSendHeadersCallback on_before_send_headers_callback_;
  143. OnHeadersReceivedCallback on_headers_received_callback_;
  144. GURL redirect_url_;
  145. bool is_done_ = false;
  146. bool has_extra_headers_;
  147. mojo::PendingRemote<network::mojom::WebSocket> websocket_;
  148. mojo::PendingReceiver<network::mojom::WebSocketClient> client_receiver_;
  149. network::mojom::WebSocketHandshakeResponsePtr handshake_response_ = nullptr;
  150. mojo::ScopedDataPipeConsumerHandle readable_;
  151. mojo::ScopedDataPipeProducerHandle writable_;
  152. extensions::WebRequestInfo info_;
  153. base::WeakPtrFactory<ProxyingWebSocket> weak_factory_{this};
  154. };
  155. } // namespace electron
  156. #endif // ELECTRON_SHELL_BROWSER_NET_PROXYING_WEBSOCKET_H_