proxying_websocket.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "extensions/browser/api/web_request/web_request_api.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 "net/base/network_delegate.h"
  18. #include "services/network/public/cpp/resource_request.h"
  19. #include "services/network/public/cpp/resource_response.h"
  20. #include "services/network/public/mojom/network_context.mojom.h"
  21. #include "services/network/public/mojom/websocket.mojom.h"
  22. #include "shell/browser/net/web_request_api_interface.h"
  23. #include "url/gurl.h"
  24. #include "url/origin.h"
  25. namespace electron {
  26. // A ProxyingWebSocket proxies a WebSocket connection and dispatches
  27. // WebRequest API events.
  28. //
  29. // The code is referenced from the
  30. // extensions::WebRequestProxyingWebSocket class.
  31. class ProxyingWebSocket : public network::mojom::WebSocketHandshakeClient,
  32. public network::mojom::AuthenticationHandler,
  33. public network::mojom::TrustedHeaderClient {
  34. public:
  35. using WebSocketFactory = content::ContentBrowserClient::WebSocketFactory;
  36. ProxyingWebSocket(
  37. WebRequestAPI* web_request_api,
  38. WebSocketFactory factory,
  39. const network::ResourceRequest& request,
  40. mojo::PendingRemote<network::mojom::WebSocketHandshakeClient>
  41. handshake_client,
  42. bool has_extra_headers,
  43. int process_id,
  44. int render_frame_id,
  45. content::BrowserContext* browser_context,
  46. uint64_t* request_id_generator);
  47. ~ProxyingWebSocket() override;
  48. void Start();
  49. // network::mojom::WebSocketHandshakeClient methods:
  50. void OnOpeningHandshakeStarted(
  51. network::mojom::WebSocketHandshakeRequestPtr request) override;
  52. void OnResponseReceived(
  53. network::mojom::WebSocketHandshakeResponsePtr response) override;
  54. void OnConnectionEstablished(
  55. mojo::PendingRemote<network::mojom::WebSocket> websocket,
  56. mojo::PendingReceiver<network::mojom::WebSocketClient> client_receiver,
  57. const std::string& selected_protocol,
  58. const std::string& extensions,
  59. mojo::ScopedDataPipeConsumerHandle readable) override;
  60. // network::mojom::AuthenticationHandler method:
  61. void OnAuthRequired(const net::AuthChallengeInfo& auth_info,
  62. const scoped_refptr<net::HttpResponseHeaders>& headers,
  63. const net::IPEndPoint& remote_endpoint,
  64. OnAuthRequiredCallback callback) override;
  65. // network::mojom::TrustedHeaderClient methods:
  66. void OnBeforeSendHeaders(const net::HttpRequestHeaders& headers,
  67. OnBeforeSendHeadersCallback callback) override;
  68. void OnHeadersReceived(const std::string& headers,
  69. OnHeadersReceivedCallback callback) override;
  70. static void StartProxying(
  71. WebRequestAPI* web_request_api,
  72. WebSocketFactory factory,
  73. const GURL& url,
  74. const GURL& site_for_cookies,
  75. const base::Optional<std::string>& user_agent,
  76. mojo::PendingRemote<network::mojom::WebSocketHandshakeClient>
  77. handshake_client,
  78. bool has_extra_headers,
  79. int process_id,
  80. int render_frame_id,
  81. const url::Origin& origin,
  82. content::BrowserContext* browser_context,
  83. uint64_t* request_id_generator);
  84. WebRequestAPI* web_request_api() { return web_request_api_; }
  85. private:
  86. void OnBeforeRequestComplete(int error_code);
  87. void OnBeforeSendHeadersComplete(const std::set<std::string>& removed_headers,
  88. const std::set<std::string>& set_headers,
  89. int error_code);
  90. void ContinueToStartRequest(int error_code);
  91. void OnHeadersReceivedComplete(int error_code);
  92. void ContinueToHeadersReceived();
  93. void OnAuthRequiredComplete(net::NetworkDelegate::AuthRequiredResponse rv);
  94. void OnHeadersReceivedCompleteForAuth(const net::AuthChallengeInfo& auth_info,
  95. int rv);
  96. void PauseIncomingMethodCallProcessing();
  97. void ResumeIncomingMethodCallProcessing();
  98. void OnError(int result);
  99. // This is used for detecting errors on mojo connection with the network
  100. // service.
  101. void OnMojoConnectionErrorWithCustomReason(uint32_t custom_reason,
  102. const std::string& description);
  103. // This is used for detecting errors on mojo connection with original client
  104. // (i.e., renderer).
  105. void OnMojoConnectionError();
  106. // Passed from api::WebRequest.
  107. WebRequestAPI* web_request_api_;
  108. // Saved to feed the api::WebRequest.
  109. network::ResourceRequest request_;
  110. WebSocketFactory factory_;
  111. mojo::Remote<network::mojom::WebSocketHandshakeClient>
  112. forwarding_handshake_client_;
  113. mojo::Receiver<network::mojom::WebSocketHandshakeClient>
  114. receiver_as_handshake_client_{this};
  115. mojo::Receiver<network::mojom::AuthenticationHandler>
  116. receiver_as_auth_handler_{this};
  117. mojo::Receiver<network::mojom::TrustedHeaderClient>
  118. receiver_as_header_client_{this};
  119. net::HttpRequestHeaders request_headers_;
  120. network::ResourceResponseHead response_;
  121. net::AuthCredentials auth_credentials_;
  122. OnAuthRequiredCallback auth_required_callback_;
  123. scoped_refptr<net::HttpResponseHeaders> override_headers_;
  124. std::vector<network::mojom::HttpHeaderPtr> additional_headers_;
  125. OnBeforeSendHeadersCallback on_before_send_headers_callback_;
  126. OnHeadersReceivedCallback on_headers_received_callback_;
  127. GURL redirect_url_;
  128. bool is_done_ = false;
  129. bool waiting_for_header_client_headers_received_ = false;
  130. bool has_extra_headers_;
  131. extensions::WebRequestInfo info_;
  132. // Notifies the proxy that the browser context has been shutdown.
  133. std::unique_ptr<KeyedServiceShutdownNotifier::Subscription>
  134. shutdown_notifier_;
  135. base::WeakPtrFactory<ProxyingWebSocket> weak_factory_{this};
  136. DISALLOW_COPY_AND_ASSIGN(ProxyingWebSocket);
  137. };
  138. } // namespace electron
  139. #endif // SHELL_BROWSER_NET_PROXYING_WEBSOCKET_H_