Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_LOADER_NAVIGATION_URL_LOADER_DELEGATE_H_ |
| 6 | #define CONTENT_BROWSER_LOADER_NAVIGATION_URL_LOADER_DELEGATE_H_ |
| 7 | |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 8 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 9 | #include <optional> |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 10 | |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 11 | #include "content/common/content_export.h" |
Kenichi Ishibashi | f3f8052 | 2021-07-16 02:37:07 | [diff] [blame] | 12 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 13 | #include "services/network/public/mojom/early_hints.mojom-forward.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 14 | #include "services/network/public/mojom/url_loader.mojom.h" |
Kenichi Ishibashi | f3f8052 | 2021-07-16 02:37:07 | [diff] [blame] | 15 | #include "services/network/public/mojom/url_loader_factory.mojom.h" |
Lucas Furukawa Gadani | d661c0d | 2019-12-02 19:58:16 | [diff] [blame] | 16 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 17 | #include "third_party/blink/public/common/navigation/navigation_policy.h" |
Kenichi Ishibashi | f3f8052 | 2021-07-16 02:37:07 | [diff] [blame] | 18 | #include "url/origin.h" |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 19 | |
| 20 | namespace net { |
Brianna Goldstein | d22b064 | 2022-10-11 16:30:50 | [diff] [blame] | 21 | class NetworkAnonymizationKey; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 22 | struct RedirectInfo; |
| 23 | } |
| 24 | |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 25 | namespace network { |
Johannes Henkel | 6a43fef | 2018-05-17 18:41:52 | [diff] [blame] | 26 | struct URLLoaderCompletionStatus; |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 27 | } |
| 28 | |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 29 | namespace content { |
| 30 | |
Kenichi Ishibashi | 7eb8cf6 | 2021-04-07 12:35:05 | [diff] [blame] | 31 | class NavigationEarlyHintsManager; |
Kenichi Ishibashi | 1f67a2d | 2021-09-02 01:36:48 | [diff] [blame] | 32 | struct NavigationEarlyHintsManagerParams; |
yzshen | 4434137 | 2016-11-30 21:44:13 | [diff] [blame] | 33 | struct GlobalRequestID; |
Kinuko Yasuda | 250577c | 2017-10-29 02:51:24 | [diff] [blame] | 34 | struct SubresourceLoaderParams; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 35 | |
arthursonzogni | f34adf09 | 2018-04-24 17:06:13 | [diff] [blame] | 36 | // The delegate interface to NavigationURLLoader. |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 37 | class CONTENT_EXPORT NavigationURLLoaderDelegate { |
| 38 | public: |
Kenichi Ishibashi | cf248fb | 2021-04-07 22:29:35 | [diff] [blame] | 39 | // Conveys information related to Early Hints responses. |
| 40 | struct CONTENT_EXPORT EarlyHints { |
| 41 | EarlyHints(); |
| 42 | ~EarlyHints(); |
| 43 | |
| 44 | EarlyHints(EarlyHints&& other); |
| 45 | EarlyHints& operator=(EarlyHints&& other); |
| 46 | |
| 47 | EarlyHints(const EarlyHints& other) = delete; |
| 48 | EarlyHints& operator=(const EarlyHints& other) = delete; |
| 49 | |
Kenichi Ishibashi | 784dd40 | 2021-10-15 00:19:24 | [diff] [blame] | 50 | // True when at least one preload or preconnect Link header was received |
| 51 | // during a main frame navigation. |
| 52 | bool was_resource_hints_received = false; |
Kenichi Ishibashi | cf248fb | 2021-04-07 22:29:35 | [diff] [blame] | 53 | // Non-null when at least one preload is actually requested. |
| 54 | std::unique_ptr<NavigationEarlyHintsManager> manager; |
| 55 | }; |
| 56 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 57 | NavigationURLLoaderDelegate(const NavigationURLLoaderDelegate&) = delete; |
| 58 | NavigationURLLoaderDelegate& operator=(const NavigationURLLoaderDelegate&) = |
| 59 | delete; |
| 60 | |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 61 | // Called when the request is redirected. Call FollowRedirect to continue |
| 62 | // processing the request. |
Matt Menke | 7f615a1 | 2020-12-11 17:06:44 | [diff] [blame] | 63 | // |
Brianna Goldstein | d22b064 | 2022-10-11 16:30:50 | [diff] [blame] | 64 | // |network_anonymization_key| is the NetworkAnonymizationKey associated with |
| 65 | // the request that was redirected, not the one that will be used if the |
| 66 | // redirect is followed. |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 67 | virtual void OnRequestRedirected( |
| 68 | const net::RedirectInfo& redirect_info, |
Brianna Goldstein | d22b064 | 2022-10-11 16:30:50 | [diff] [blame] | 69 | const net::NetworkAnonymizationKey& network_anonymization_key, |
Lucas Furukawa Gadani | d661c0d | 2019-12-02 19:58:16 | [diff] [blame] | 70 | network::mojom::URLResponseHeadPtr response) = 0; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 71 | |
| 72 | // Called when the request receives its response. No further calls will be |
arthursonzogni | f34adf09 | 2018-04-24 17:06:13 | [diff] [blame] | 73 | // made to the delegate. The response body can be retrieved by implementing an |
| 74 | // URLLoaderClient and binding the |url_loader_client_endpoints|. |
| 75 | // |navigation_data| is passed to the NavigationHandle. |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 76 | // |subresource_loader_params| is used in the network service only for passing |
| 77 | // necessary info to create a custom subresource loader in the renderer |
Adrienne Walker | 14402656 | 2021-11-19 23:13:09 | [diff] [blame] | 78 | // process if the navigated context is controlled by a request interceptor. |
Charlie Harrison | 650e1142 | 2018-12-04 00:37:26 | [diff] [blame] | 79 | // |
| 80 | // |is_download| is true if the request must be downloaded, if it isn't |
| 81 | // disallowed. |
| 82 | // |
Matt Menke | 7f615a1 | 2020-12-11 17:06:44 | [diff] [blame] | 83 | // Invoking this method will delete the URLLoader, so it needs to take all |
| 84 | // arguments by value. |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 85 | virtual void OnResponseStarted( |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 86 | network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints, |
Lucas Furukawa Gadani | d661c0d | 2019-12-02 19:58:16 | [diff] [blame] | 87 | network::mojom::URLResponseHeadPtr response_head, |
Minggang Wang | 47a7822 | 2019-06-28 17:05:46 | [diff] [blame] | 88 | mojo::ScopedDataPipeConsumerHandle response_body, |
Matt Menke | 7f615a1 | 2020-12-11 17:06:44 | [diff] [blame] | 89 | GlobalRequestID request_id, |
yzshen | 4434137 | 2016-11-30 21:44:13 | [diff] [blame] | 90 | bool is_download, |
Brianna Goldstein | d22b064 | 2022-10-11 16:30:50 | [diff] [blame] | 91 | net::NetworkAnonymizationKey network_anonymization_key, |
Hiroshige Hayashizaki | 9e0f286 | 2024-02-09 16:54:00 | [diff] [blame] | 92 | SubresourceLoaderParams subresource_loader_params, |
Kenichi Ishibashi | cf248fb | 2021-04-07 22:29:35 | [diff] [blame] | 93 | EarlyHints early_hints) = 0; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 94 | |
Johannes Henkel | 6a43fef | 2018-05-17 18:41:52 | [diff] [blame] | 95 | // Called if the request fails before receving a response. Specific |
| 96 | // fields which are used: |status.error_code| holds the error code |
| 97 | // for the failure; |status.extended_error_code| holds details if |
| 98 | // available; |status.exists_in_cache| indicates a stale cache |
| 99 | // entry; |status.ssl_info| is available when |status.error_code| is |
| 100 | // a certificate error. |
Carlos IL | 8113338 | 2017-12-06 17:18:45 | [diff] [blame] | 101 | virtual void OnRequestFailed( |
Johannes Henkel | 6a43fef | 2018-05-17 18:41:52 | [diff] [blame] | 102 | const network::URLLoaderCompletionStatus& status) = 0; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 103 | |
Kenichi Ishibashi | 1f67a2d | 2021-09-02 01:36:48 | [diff] [blame] | 104 | // Creates parameters to construct NavigationEarlyHintsManager. Returns |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 105 | // std::nullopt when this delegate cannot create parameters. |
| 106 | virtual std::optional<NavigationEarlyHintsManagerParams> |
Kenichi Ishibashi | 1f67a2d | 2021-09-02 01:36:48 | [diff] [blame] | 107 | CreateNavigationEarlyHintsManagerParams( |
Kenichi Ishibashi | f3f8052 | 2021-07-16 02:37:07 | [diff] [blame] | 108 | const network::mojom::EarlyHints& early_hints) = 0; |
| 109 | |
Hiroshige Hayashizaki | 83cd175 | 2025-07-31 17:46:25 | [diff] [blame] | 110 | // Only for testing purpose (https://crbug.com/434182226). |
| 111 | // In non-test cases, use `OnRequestRedirected()` instead, and this method |
| 112 | // must do nothing and return `false`. |
| 113 | // |
| 114 | // Called when `NavigationURLLoaderImpl::OnReceiveRedirect()` is called. |
| 115 | // When the return value is `true` (which is only allowed in tests), |
| 116 | // `head->parsed_headers` is cleared to enforce and test the async |
| 117 | // `ParseHeaders()` path. |
| 118 | virtual bool ShouldClearParsedHeadersOnTestReceiveRedirect() = 0; |
| 119 | |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 120 | protected: |
| 121 | NavigationURLLoaderDelegate() {} |
| 122 | virtual ~NavigationURLLoaderDelegate() {} |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace content |
| 126 | |
| 127 | #endif // CONTENT_BROWSER_LOADER_NAVIGATION_URL_LOADER_DELEGATE_H_ |