proxying_url_loader_factory.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_
  5. #define ELECTRON_SHELL_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_
  6. #include <cstdint>
  7. #include <memory>
  8. #include <optional>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "base/containers/flat_map.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/raw_ref.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "content/public/browser/content_browser_client.h"
  17. #include "extensions/browser/api/web_request/web_request_info.h"
  18. #include "mojo/public/cpp/bindings/receiver.h"
  19. #include "mojo/public/cpp/bindings/receiver_set.h"
  20. #include "mojo/public/cpp/bindings/remote.h"
  21. #include "net/base/completion_once_callback.h"
  22. #include "net/traffic_annotation/network_traffic_annotation.h"
  23. #include "services/network/public/cpp/resource_request.h"
  24. #include "services/network/public/mojom/network_context.mojom.h"
  25. #include "services/network/public/mojom/url_loader.mojom.h"
  26. #include "services/network/public/mojom/url_loader_factory.mojom.h"
  27. #include "services/network/public/mojom/url_response_head.mojom-forward.h"
  28. #include "services/network/url_loader_factory.h"
  29. #include "shell/browser/net/electron_url_loader_factory.h"
  30. #include "shell/browser/net/web_request_api_interface.h"
  31. #include "url/gurl.h"
  32. namespace mojo {
  33. template <typename T>
  34. class PendingReceiver;
  35. template <typename T>
  36. class PendingRemote;
  37. } // namespace mojo
  38. namespace electron {
  39. const uint32_t kBypassCustomProtocolHandlers = 1 << 30;
  40. // This class is responsible for following tasks when NetworkService is enabled:
  41. // 1. handling intercepted protocols;
  42. // 2. implementing webRequest module;
  43. //
  44. // For the task #2, the code is referenced from the
  45. // extensions::WebRequestProxyingURLLoaderFactory class.
  46. class ProxyingURLLoaderFactory
  47. : public network::mojom::URLLoaderFactory,
  48. public network::mojom::TrustedURLLoaderHeaderClient {
  49. public:
  50. class InProgressRequest : public network::mojom::URLLoader,
  51. public network::mojom::URLLoaderClient,
  52. public network::mojom::TrustedHeaderClient {
  53. public:
  54. // For usual requests
  55. InProgressRequest(
  56. ProxyingURLLoaderFactory* factory,
  57. uint64_t web_request_id,
  58. int32_t frame_routing_id,
  59. int32_t network_service_request_id,
  60. uint32_t options,
  61. const network::ResourceRequest& request,
  62. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
  63. mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
  64. mojo::PendingRemote<network::mojom::URLLoaderClient> client);
  65. // For CORS preflights
  66. InProgressRequest(ProxyingURLLoaderFactory* factory,
  67. uint64_t request_id,
  68. int32_t frame_routing_id,
  69. const network::ResourceRequest& request);
  70. ~InProgressRequest() override;
  71. // disable copy
  72. InProgressRequest(const InProgressRequest&) = delete;
  73. InProgressRequest& operator=(const InProgressRequest&) = delete;
  74. void Restart();
  75. // network::mojom::URLLoader:
  76. void FollowRedirect(
  77. const std::vector<std::string>& removed_headers,
  78. const net::HttpRequestHeaders& modified_headers,
  79. const net::HttpRequestHeaders& modified_cors_exempt_headers,
  80. const std::optional<GURL>& new_url) override;
  81. void SetPriority(net::RequestPriority priority,
  82. int32_t intra_priority_value) override;
  83. // network::mojom::URLLoaderClient:
  84. void OnReceiveEarlyHints(
  85. network::mojom::EarlyHintsPtr early_hints) override;
  86. void OnReceiveResponse(
  87. network::mojom::URLResponseHeadPtr head,
  88. mojo::ScopedDataPipeConsumerHandle body,
  89. std::optional<mojo_base::BigBuffer> cached_metadata) override;
  90. void OnReceiveRedirect(const net::RedirectInfo& redirect_info,
  91. network::mojom::URLResponseHeadPtr head) override;
  92. void OnUploadProgress(int64_t current_position,
  93. int64_t total_size,
  94. OnUploadProgressCallback callback) override;
  95. void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
  96. void OnComplete(const network::URLLoaderCompletionStatus& status) override;
  97. void OnLoaderCreated(
  98. mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver);
  99. // network::mojom::TrustedHeaderClient:
  100. void OnBeforeSendHeaders(const net::HttpRequestHeaders& headers,
  101. OnBeforeSendHeadersCallback callback) override;
  102. void OnHeadersReceived(const std::string& headers,
  103. const net::IPEndPoint& endpoint,
  104. OnHeadersReceivedCallback callback) override;
  105. private:
  106. // These two methods combined form the implementation of Restart().
  107. void UpdateRequestInfo();
  108. void RestartInternal();
  109. void ContinueToBeforeSendHeaders(int error_code);
  110. void ContinueToSendHeaders(const std::set<std::string>& removed_headers,
  111. const std::set<std::string>& set_headers,
  112. int error_code);
  113. void ContinueToStartRequest(int error_code);
  114. void ContinueToHandleOverrideHeaders(int error_code);
  115. void ContinueToResponseStarted(int error_code);
  116. void ContinueToBeforeRedirect(const net::RedirectInfo& redirect_info,
  117. int error_code);
  118. void HandleResponseOrRedirectHeaders(
  119. net::CompletionOnceCallback continuation);
  120. void OnRequestError(const network::URLLoaderCompletionStatus& status);
  121. void HandleBeforeRequestRedirect();
  122. raw_ptr<ProxyingURLLoaderFactory> const factory_;
  123. network::ResourceRequest request_;
  124. const std::optional<url::Origin> original_initiator_;
  125. const uint64_t request_id_ = 0;
  126. const int32_t network_service_request_id_ = 0;
  127. const int32_t frame_routing_id_ = MSG_ROUTING_NONE;
  128. const uint32_t options_ = 0;
  129. const net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
  130. mojo::Receiver<network::mojom::URLLoader> proxied_loader_receiver_;
  131. mojo::Remote<network::mojom::URLLoaderClient> target_client_;
  132. std::optional<extensions::WebRequestInfo> info_;
  133. mojo::Receiver<network::mojom::URLLoaderClient> proxied_client_receiver_{
  134. this};
  135. mojo::Remote<network::mojom::URLLoader> target_loader_;
  136. network::mojom::URLResponseHeadPtr current_response_;
  137. mojo::ScopedDataPipeConsumerHandle current_body_;
  138. scoped_refptr<net::HttpResponseHeaders> override_headers_;
  139. GURL redirect_url_;
  140. const bool for_cors_preflight_ = false;
  141. // If |has_any_extra_headers_listeners_| is set to true, the request will be
  142. // sent with the network::mojom::kURLLoadOptionUseHeaderClient option, and
  143. // we expect events to come through the
  144. // network::mojom::TrustedURLLoaderHeaderClient binding on the factory. This
  145. // is only set to true if there is a listener that needs to view or modify
  146. // headers set in the network process.
  147. bool has_any_extra_headers_listeners_ = false;
  148. bool current_request_uses_header_client_ = false;
  149. OnBeforeSendHeadersCallback on_before_send_headers_callback_;
  150. OnHeadersReceivedCallback on_headers_received_callback_;
  151. mojo::Receiver<network::mojom::TrustedHeaderClient> header_client_receiver_{
  152. this};
  153. // If |has_any_extra_headers_listeners_| is set to false and a redirect is
  154. // in progress, this stores the parameters to FollowRedirect that came from
  155. // the client. That way we can combine it with any other changes that
  156. // extensions made to headers in their callbacks.
  157. struct FollowRedirectParams {
  158. FollowRedirectParams();
  159. ~FollowRedirectParams();
  160. std::vector<std::string> removed_headers;
  161. net::HttpRequestHeaders modified_headers;
  162. net::HttpRequestHeaders modified_cors_exempt_headers;
  163. std::optional<GURL> new_url;
  164. // disable copy
  165. FollowRedirectParams(const FollowRedirectParams&) = delete;
  166. FollowRedirectParams& operator=(const FollowRedirectParams&) = delete;
  167. };
  168. std::unique_ptr<FollowRedirectParams> pending_follow_redirect_params_;
  169. std::optional<mojo_base::BigBuffer> current_cached_metadata_;
  170. base::WeakPtrFactory<InProgressRequest> weak_factory_{this};
  171. };
  172. ProxyingURLLoaderFactory(
  173. WebRequestAPI* web_request_api,
  174. const HandlersMap& intercepted_handlers,
  175. int render_process_id,
  176. int frame_routing_id,
  177. uint64_t* request_id_generator,
  178. std::unique_ptr<extensions::ExtensionNavigationUIData> navigation_ui_data,
  179. std::optional<int64_t> navigation_id,
  180. mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader_request,
  181. mojo::PendingRemote<network::mojom::URLLoaderFactory>
  182. target_factory_remote,
  183. mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
  184. header_client_receiver,
  185. content::ContentBrowserClient::URLLoaderFactoryType loader_factory_type);
  186. ~ProxyingURLLoaderFactory() override;
  187. // disable copy
  188. ProxyingURLLoaderFactory(const ProxyingURLLoaderFactory&) = delete;
  189. ProxyingURLLoaderFactory& operator=(const ProxyingURLLoaderFactory&) = delete;
  190. // network::mojom::URLLoaderFactory:
  191. void CreateLoaderAndStart(
  192. mojo::PendingReceiver<network::mojom::URLLoader> loader,
  193. int32_t request_id,
  194. uint32_t options,
  195. const network::ResourceRequest& request,
  196. mojo::PendingRemote<network::mojom::URLLoaderClient> client,
  197. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
  198. override;
  199. void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory>
  200. loader_receiver) override;
  201. // network::mojom::TrustedURLLoaderHeaderClient:
  202. void OnLoaderCreated(
  203. int32_t request_id,
  204. mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver)
  205. override;
  206. void OnLoaderForCorsPreflightCreated(
  207. const network::ResourceRequest& request,
  208. mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver)
  209. override;
  210. WebRequestAPI* web_request_api() { return web_request_api_; }
  211. bool IsForServiceWorkerScript() const;
  212. private:
  213. void OnTargetFactoryError();
  214. void OnProxyBindingError();
  215. void RemoveRequest(int32_t network_service_request_id, uint64_t request_id);
  216. void MaybeDeleteThis();
  217. bool ShouldIgnoreConnectionsLimit(const network::ResourceRequest& request);
  218. // Passed from api::WebRequest.
  219. raw_ptr<WebRequestAPI> web_request_api_;
  220. // This is passed from api::Protocol.
  221. //
  222. // The Protocol instance lives through the lifetime of BrowserContext,
  223. // which is guaranteed to cover the lifetime of URLLoaderFactory, so the
  224. // reference is guaranteed to be valid.
  225. //
  226. // In this way we can avoid using code from api namespace in this file.
  227. const raw_ref<const HandlersMap> intercepted_handlers_;
  228. const int render_process_id_;
  229. const int frame_routing_id_;
  230. raw_ptr<uint64_t> request_id_generator_; // managed by ElectronBrowserClient
  231. std::unique_ptr<extensions::ExtensionNavigationUIData> navigation_ui_data_;
  232. std::optional<int64_t> navigation_id_;
  233. mojo::ReceiverSet<network::mojom::URLLoaderFactory> proxy_receivers_;
  234. mojo::Remote<network::mojom::URLLoaderFactory> target_factory_;
  235. mojo::Receiver<network::mojom::TrustedURLLoaderHeaderClient>
  236. url_loader_header_client_receiver_{this};
  237. const content::ContentBrowserClient::URLLoaderFactoryType
  238. loader_factory_type_;
  239. // Mapping from our own internally generated request ID to an
  240. // InProgressRequest instance.
  241. base::flat_map<uint64_t, std::unique_ptr<InProgressRequest>> requests_;
  242. // A mapping from the network stack's notion of request ID to our own
  243. // internally generated request ID for the same request.
  244. base::flat_map<int32_t, uint64_t> network_request_id_to_web_request_id_;
  245. std::vector<std::string> ignore_connections_limit_domains_;
  246. };
  247. } // namespace electron
  248. #endif // ELECTRON_SHELL_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_