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