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 | #include "content/browser/loader/navigation_url_loader.h" |
| 6 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
scottmg | 6998521 | 2017-04-12 16:47:28 | [diff] [blame] | 9 | #include "base/command_line.h" |
Minoru Chikamune | 99de26ca | 2024-02-29 06:45:21 | [diff] [blame] | 10 | #include "base/trace_event/trace_event.h" |
Lowell Manners | 0c04d63 | 2019-08-05 10:52:02 | [diff] [blame] | 11 | #include "content/browser/loader/cached_navigation_url_loader.h" |
| 12 | #include "content/browser/loader/navigation_loader_interceptor.h" |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 13 | #include "content/browser/loader/navigation_url_loader_factory.h" |
arthursonzogni | 1fd60e6 | 2018-05-09 15:57:32 | [diff] [blame] | 14 | #include "content/browser/loader/navigation_url_loader_impl.h" |
danakj | 22c9ee2 | 2020-09-16 16:37:28 | [diff] [blame] | 15 | #include "content/browser/renderer_host/navigation_request_info.h" |
Tsuyoshi Horo | 0e20a6e | 2019-06-04 09:48:30 | [diff] [blame] | 16 | #include "content/browser/web_package/prefetched_signed_exchange_cache.h" |
clamy | 1e5574e9 | 2016-09-29 16:48:44 | [diff] [blame] | 17 | #include "content/public/browser/navigation_ui_data.h" |
Yutaka Hirano | d8789f9 | 2018-01-30 09:59:51 | [diff] [blame] | 18 | #include "services/network/public/cpp/features.h" |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 19 | |
| 20 | namespace content { |
| 21 | |
Daniel Bratell | 89560a0 | 2017-10-13 10:20:54 | [diff] [blame] | 22 | static NavigationURLLoaderFactory* g_loader_factory = nullptr; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 23 | |
Takashi Toyoshima | 258a615 | 2021-10-06 23:07:47 | [diff] [blame] | 24 | // static |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 25 | std::unique_ptr<NavigationURLLoader> NavigationURLLoader::Create( |
Clark DuVall | fffa41e | 2019-06-25 20:27:19 | [diff] [blame] | 26 | BrowserContext* browser_context, |
jam | 0f396ee | 2017-03-01 01:34:11 | [diff] [blame] | 27 | StoragePartition* storage_partition, |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 28 | std::unique_ptr<NavigationRequestInfo> request_info, |
clamy | 1e5574e9 | 2016-09-29 16:48:44 | [diff] [blame] | 29 | std::unique_ptr<NavigationUIData> navigation_ui_data, |
Zhuoyu Qian | 1811ccf | 2020-01-06 06:46:54 | [diff] [blame] | 30 | ServiceWorkerMainResourceHandle* service_worker_handle, |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 31 | scoped_refptr<PrefetchedSignedExchangeCache> |
| 32 | prefetched_signed_exchange_cache, |
Takashi Toyoshima | 5af383aa | 2019-07-22 15:24:22 | [diff] [blame] | 33 | NavigationURLLoaderDelegate* delegate, |
Hiroki Nakagawa | 5034e196 | 2020-11-12 09:11:30 | [diff] [blame] | 34 | LoaderType loader_type, |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 35 | mojo::PendingRemote<network::mojom::CookieAccessObserver> cookie_observer, |
Steven Valdez | a06f680c | 2023-03-21 19:00:10 | [diff] [blame] | 36 | mojo::PendingRemote<network::mojom::TrustTokenAccessObserver> |
| 37 | trust_token_observer, |
Tsuyoshi Horo | bcd0b9f6 | 2023-06-28 11:35:32 | [diff] [blame] | 38 | mojo::PendingRemote<network::mojom::SharedDictionaryAccessObserver> |
| 39 | shared_dictionary_observer, |
Dave Tapuska | 03d6f50 | 2021-03-05 23:06:28 | [diff] [blame] | 40 | mojo::PendingRemote<network::mojom::URLLoaderNetworkServiceObserver> |
| 41 | url_loader_network_observer, |
Dave Tapuska | 2a6046a | 2021-03-01 22:42:52 | [diff] [blame] | 42 | mojo::PendingRemote<network::mojom::DevToolsObserver> devtools_observer, |
Daniel Rubery | 6420d7430 | 2024-11-19 01:42:40 | [diff] [blame] | 43 | mojo::PendingRemote<network::mojom::DeviceBoundSessionAccessObserver> |
| 44 | device_bound_session_observer, |
Takashi Toyoshima | 2e8d6101 | 2021-06-07 10:31:27 | [diff] [blame] | 45 | network::mojom::URLResponseHeadPtr cached_response_head, |
Takashi Toyoshima | 5af383aa | 2019-07-22 15:24:22 | [diff] [blame] | 46 | std::vector<std::unique_ptr<NavigationLoaderInterceptor>> |
| 47 | initial_interceptors) { |
Minoru Chikamune | 99de26ca | 2024-02-29 06:45:21 | [diff] [blame] | 48 | TRACE_EVENT0("navigation", "NavigationURLLoader::Create"); |
Hiroki Nakagawa | 6c5f39bd | 2021-07-13 00:22:36 | [diff] [blame] | 49 | // Prioritize CachedNavigationURLLoader over `g_loader_factory` even for tests |
| 50 | // as prerendered page activation needs to run synchronously and |
| 51 | // CachedNavigationURLLoader serves a fake response synchronously. |
| 52 | if (loader_type == LoaderType::kNoopForPrerender) { |
| 53 | DCHECK(cached_response_head); |
| 54 | return CachedNavigationURLLoader::Create(loader_type, |
| 55 | std::move(request_info), delegate, |
| 56 | std::move(cached_response_head)); |
| 57 | } |
| 58 | |
Daniel Bratell | 89560a0 | 2017-10-13 10:20:54 | [diff] [blame] | 59 | if (g_loader_factory) { |
| 60 | return g_loader_factory->CreateLoader( |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 61 | storage_partition, std::move(request_info), |
arthursonzogni | 7ce00b8f | 2019-08-22 09:37:01 | [diff] [blame] | 62 | std::move(navigation_ui_data), service_worker_handle, delegate, |
Hiroki Nakagawa | 5034e196 | 2020-11-12 09:11:30 | [diff] [blame] | 63 | loader_type); |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 64 | } |
arthursonzogni | 7ce00b8f | 2019-08-22 09:37:01 | [diff] [blame] | 65 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 66 | // TODO(crbug.com/40188852): Merge this into the kNoopForPrerender path |
Hiroki Nakagawa | 6c5f39bd | 2021-07-13 00:22:36 | [diff] [blame] | 67 | // above. |
| 68 | if (loader_type == LoaderType::kNoopForBackForwardCache) { |
Takashi Toyoshima | 2e8d6101 | 2021-06-07 10:31:27 | [diff] [blame] | 69 | DCHECK(cached_response_head); |
Hiroki Nakagawa | 6c5f39bd | 2021-07-13 00:22:36 | [diff] [blame] | 70 | return CachedNavigationURLLoader::Create(loader_type, |
| 71 | std::move(request_info), delegate, |
Takashi Toyoshima | 2e8d6101 | 2021-06-07 10:31:27 | [diff] [blame] | 72 | std::move(cached_response_head)); |
| 73 | } |
arthursonzogni | 7ce00b8f | 2019-08-22 09:37:01 | [diff] [blame] | 74 | |
arthursonzogni | 1fd60e6 | 2018-05-09 15:57:32 | [diff] [blame] | 75 | return std::make_unique<NavigationURLLoaderImpl>( |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 76 | browser_context, storage_partition, std::move(request_info), |
Adrienne Walker | 5b98d1ee | 2021-10-19 16:57:41 | [diff] [blame] | 77 | std::move(navigation_ui_data), service_worker_handle, |
Tsuyoshi Horo | d5eb761 | 2019-05-09 08:59:46 | [diff] [blame] | 78 | std::move(prefetched_signed_exchange_cache), delegate, |
Steven Valdez | a06f680c | 2023-03-21 19:00:10 | [diff] [blame] | 79 | std::move(cookie_observer), std::move(trust_token_observer), |
Tsuyoshi Horo | bcd0b9f6 | 2023-06-28 11:35:32 | [diff] [blame] | 80 | std::move(shared_dictionary_observer), |
Steven Valdez | a06f680c | 2023-03-21 19:00:10 | [diff] [blame] | 81 | std::move(url_loader_network_observer), std::move(devtools_observer), |
Daniel Rubery | 6420d7430 | 2024-11-19 01:42:40 | [diff] [blame] | 82 | std::move(device_bound_session_observer), |
Steven Valdez | a06f680c | 2023-03-21 19:00:10 | [diff] [blame] | 83 | std::move(initial_interceptors)); |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 84 | } |
| 85 | |
Takashi Toyoshima | 258a615 | 2021-10-06 23:07:47 | [diff] [blame] | 86 | // static |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 87 | void NavigationURLLoader::SetFactoryForTesting( |
| 88 | NavigationURLLoaderFactory* factory) { |
Daniel Bratell | 89560a0 | 2017-10-13 10:20:54 | [diff] [blame] | 89 | DCHECK(g_loader_factory == nullptr || factory == nullptr); |
| 90 | g_loader_factory = factory; |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 91 | } |
| 92 | |
sisidovski | c01b1a3 | 2023-10-12 06:42:24 | [diff] [blame] | 93 | // static |
| 94 | uint32_t NavigationURLLoader::GetURLLoaderOptions( |
| 95 | bool is_outermost_main_frame) { |
| 96 | uint32_t options = network::mojom::kURLLoadOptionNone; |
| 97 | |
| 98 | // Ensure that Mime sniffing works. |
| 99 | options |= network::mojom::kURLLoadOptionSniffMimeType; |
| 100 | |
| 101 | if (is_outermost_main_frame) { |
| 102 | // SSLInfo is not needed on subframe or fenced frame responses because users |
| 103 | // can inspect only the certificate for the main frame when using the info |
| 104 | // bubble. |
| 105 | options |= network::mojom::kURLLoadOptionSendSSLInfoWithResponse; |
| 106 | } |
| 107 | |
| 108 | // When there's a certificate error for a frame load (regardless of whether |
| 109 | // the error caused the connection to fail), SSLInfo is useful for adjusting |
| 110 | // security UI accordingly. |
| 111 | options |= network::mojom::kURLLoadOptionSendSSLInfoForCertificateError; |
| 112 | |
| 113 | return options; |
| 114 | } |
davidben | 6b77cd7 | 2014-10-29 21:13:45 | [diff] [blame] | 115 | } // namespace content |