proxying_websocket.h 7.1 KB

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