Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 5 | /* |
| 6 | * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 7 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
| 8 | * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. |
| 9 | * (http://www.torchmobile.com/) |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 21 | * its contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 25 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 28 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 36 | #include "content/browser/renderer_host/navigation_controller_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 38 | #include <algorithm> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 39 | #include <utility> |
| 40 | |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 41 | #include "base/command_line.h" |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 42 | #include "base/containers/adapters.h" |
Lei Zhang | 302bc3fa | 2025-06-04 20:10:34 | [diff] [blame] | 43 | #include "base/debug/crash_logging.h" |
Rakina Zata Amni | 627360d | 2022-02-24 00:53:40 | [diff] [blame] | 44 | #include "base/debug/dump_without_crashing.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 45 | #include "base/functional/bind.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | #include "base/logging.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 47 | #include "base/metrics/histogram_macros.h" |
Kevin McNee | 37a10979 | 2025-04-14 21:16:38 | [diff] [blame] | 48 | #include "base/not_fatal_until.h" |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 49 | #include "base/numerics/safe_conversions.h" |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 50 | #include "base/strings/escape.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 51 | #include "base/strings/string_util.h" |
Daniel Cheng | 17390fd | 2025-06-07 06:38:26 | [diff] [blame] | 52 | #include "base/strings/string_view_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 53 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 54 | #include "base/time/time.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 55 | #include "base/trace_event/optional_trace_event.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 56 | #include "base/trace_event/trace_event.h" |
Daniel Cheng | abb00686 | 2022-09-09 22:39:08 | [diff] [blame] | 57 | #include "base/types/optional_util.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 58 | #include "build/build_config.h" |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 59 | #include "cc/base/switches.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 60 | #include "content/browser/bad_message.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 61 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 62 | #include "content/browser/browser_context_impl.h" |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 63 | #include "content/browser/browser_url_handler_impl.h" |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 64 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 65 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 66 | #include "content/browser/preloading/prerender/prerender_host.h" |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 67 | #include "content/browser/process_lock.h" |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 68 | #include "content/browser/renderer_host/back_forward_cache_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 69 | #include "content/browser/renderer_host/debug_urls.h" |
| 70 | #include "content/browser/renderer_host/frame_tree.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 71 | #include "content/browser/renderer_host/frame_tree_node.h" |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 72 | #include "content/browser/renderer_host/navigation_controller_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 73 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 74 | #include "content/browser/renderer_host/navigation_entry_restore_context_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 75 | #include "content/browser/renderer_host/navigation_request.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 76 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h" |
| 77 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_manager.h" |
Khushal Sagar | 94ea2dc | 2024-08-06 17:15:26 | [diff] [blame] | 78 | #include "content/browser/renderer_host/navigation_transitions/navigation_transition_config.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 79 | #include "content/browser/renderer_host/navigator.h" |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 80 | #include "content/browser/renderer_host/page_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 81 | #include "content/browser/renderer_host/render_frame_host_delegate.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 82 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 83 | #include "content/browser/renderer_host/system_entropy_utils.h" |
Sharon Yang | d70a539 | 2021-10-26 23:06:32 | [diff] [blame] | 84 | #include "content/browser/site_info.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 85 | #include "content/browser/site_instance_impl.h" |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 86 | #include "content/common/content_constants_internal.h" |
Rakina Zata Amni | 84a859df | 2024-06-07 15:56:32 | [diff] [blame] | 87 | #include "content/common/content_navigation_policy.h" |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 88 | #include "content/common/navigation_params_utils.h" |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 89 | #include "content/common/trace_utils.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 90 | #include "content/public/browser/browser_context.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 91 | #include "content/public/browser/content_browser_client.h" |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 92 | #include "content/public/browser/disallow_activation_reason.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 93 | #include "content/public/browser/invalidate_type.h" |
[email protected] | 5b96836f | 2011-12-22 07:39:00 | [diff] [blame] | 94 | #include "content/public/browser/navigation_details.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 95 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 96 | #include "content/public/browser/render_widget_host.h" |
| 97 | #include "content/public/browser/render_widget_host_view.h" |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 98 | #include "content/public/browser/replaced_navigation_entry_data.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 99 | #include "content/public/browser/storage_partition.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 100 | #include "content/public/common/content_client.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 101 | #include "content/public/common/content_constants.h" |
toyoshim | 86e34ec | 2016-02-25 08:56:10 | [diff] [blame] | 102 | #include "content/public/common/content_features.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 103 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 104 | #include "content/public/common/url_utils.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 105 | #include "media/base/mime_util.h" |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 106 | #include "net/base/net_errors.h" |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 107 | #include "net/http/http_status_code.h" |
Shivani Sharma | 9332910 | 2019-01-24 19:44:18 | [diff] [blame] | 108 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 109 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Yue Ru Sun | 12880493 | 2020-09-30 22:19:17 | [diff] [blame] | 110 | #include "services/metrics/public/cpp/ukm_source_id.h" |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 111 | #include "services/network/public/mojom/fetch_api.mojom.h" |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 112 | #include "services/network/public/mojom/url_response_head.mojom-shared.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 113 | #include "skia/ext/platform_canvas.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 114 | #include "third_party/blink/public/common/blob/blob_utils.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 115 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 116 | #include "third_party/blink/public/common/history/session_history_constants.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 117 | #include "third_party/blink/public/common/mime_util/mime_util.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 118 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 119 | #include "third_party/blink/public/common/page_state/page_state_serialization.h" |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 120 | #include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h" |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 121 | #include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h" |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 122 | #include "third_party/blink/public/mojom/runtime_feature_state/runtime_feature.mojom.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 123 | #include "url/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 125 | namespace content { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 126 | namespace { |
| 127 | |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 128 | // Feature to skip a redundant NavigationRequest creation for bfcache |
| 129 | // activations, per https://crbug.com/417251428. |
Charlie Reis | 3696ab6 | 2025-05-28 07:02:30 | [diff] [blame] | 130 | // TODO(crbug.com/420275259): Diagnose crashes and enable by default. |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 131 | BASE_FEATURE(kSkipExtraBfcacheNavigationRequest, |
| 132 | "SkipExtraBfcacheNavigationRequest", |
Charlie Reis | 3696ab6 | 2025-05-28 07:02:30 | [diff] [blame] | 133 | base::FEATURE_DISABLED_BY_DEFAULT); |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 134 | |
Diana Qu | 21d0c92 | 2025-06-25 17:19:08 | [diff] [blame] | 135 | // Enables a CHECK in RendererDidNavigate to ensure that session |
| 136 | // history navigations commit in the expected SiteInstance when the |
| 137 | // document sequence number matches. Helps detect navigation process |
| 138 | // mismatches and potential security issues. |
| 139 | BASE_FEATURE(kCheckSiteInstanceOnHistoryNavigation, |
| 140 | "CheckSiteInstanceOnHistoryNavigation", |
| 141 | base::FEATURE_ENABLED_BY_DEFAULT); |
| 142 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 143 | // Invoked when entries have been pruned, or removed. For example, if the |
| 144 | // current entries are [google, digg, yahoo], with the current entry google, |
| 145 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 146 | void NotifyPrunedEntries(NavigationControllerImpl* nav_controller, |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 147 | int index, |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 148 | int count) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 149 | PrunedDetails details; |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 150 | details.index = index; |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 151 | details.count = count; |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 152 | nav_controller->delegate()->NotifyNavigationListPruned(details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 155 | // Configure all the NavigationEntries in entries for restore. This resets |
| 156 | // the transition type to reload and makes sure the content state isn't empty. |
| 157 | void ConfigureEntriesForRestore( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 158 | std::vector<std::unique_ptr<NavigationEntryImpl>>* entries, |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 159 | RestoreType type) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 160 | for (auto& entry : *entries) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 161 | // Use a transition type of reload so that we don't incorrectly increase |
| 162 | // the typed count. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 163 | entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 164 | entry->set_restore_type(type); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 168 | // Determines whether or not we should be carrying over a user agent override |
| 169 | // between two NavigationEntries. |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 170 | bool ShouldKeepOverride(NavigationEntry* last_entry) { |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 171 | return last_entry && last_entry->GetIsOverridingUserAgent(); |
| 172 | } |
| 173 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 174 | // Determines whether to override user agent for a navigation. |
| 175 | bool ShouldOverrideUserAgent( |
| 176 | NavigationController::UserAgentOverrideOption override_user_agent, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 177 | NavigationEntry* last_committed_entry) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 178 | switch (override_user_agent) { |
| 179 | case NavigationController::UA_OVERRIDE_INHERIT: |
| 180 | return ShouldKeepOverride(last_committed_entry); |
| 181 | case NavigationController::UA_OVERRIDE_TRUE: |
| 182 | return true; |
| 183 | case NavigationController::UA_OVERRIDE_FALSE: |
| 184 | return false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 185 | } |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 186 | NOTREACHED(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 187 | } |
| 188 | |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 189 | // Returns true if this navigation should be treated as a reload. For e.g. |
Liang Zhao | 0351d76 | 2024-11-13 00:10:07 | [diff] [blame] | 190 | // clicking on a link which results in a navigation to the last committed URL |
| 191 | // (but wasn't converted to do a replacement navigation in the renderer), etc. |
| 192 | // This intentionally excludes navigating to the last committed URL via the |
| 193 | // address bar, so that the current scroll position is not restored. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 194 | // |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|, |
| 195 | // |base_url_for_data_url|, |transition_type| correspond to the new navigation |
| 196 | // (i.e. the pending NavigationEntry). |last_committed_entry| is the last |
| 197 | // navigation that committed. |
| 198 | bool ShouldTreatNavigationAsReload(FrameTreeNode* node, |
| 199 | const GURL& url, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 200 | const GURL& virtual_url, |
| 201 | const GURL& base_url_for_data_url, |
| 202 | ui::PageTransition transition_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 203 | bool is_post, |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 204 | bool should_replace_current_entry, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 205 | NavigationEntryImpl* last_committed_entry) { |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 206 | // Navigations intended to do a replacement shouldn't be converted to do a |
| 207 | // reload. |
| 208 | if (should_replace_current_entry) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 209 | return false; |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 210 | // Only convert to reload if at least one navigation committed. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 211 | if (last_committed_entry->IsInitialEntry()) |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 212 | return false; |
| 213 | |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 214 | // Skip navigations initiated by external applications. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 215 | if (transition_type & ui::PAGE_TRANSITION_FROM_API) |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 216 | return false; |
| 217 | |
Liang Zhao | 0351d76 | 2024-11-13 00:10:07 | [diff] [blame] | 218 | // Same URL navigations from the address bar should only be treated as reloads |
| 219 | // if PAGE_TRANSITION_RELOAD is set (not for PAGE_TRANSITION_TYPED or |
| 220 | // PAGE_TRANSITION_LINK). In non-address-bar cases, PAGE_TRANSITION_TYPED |
| 221 | // and PAGE_TRANSITION_LINK can be treated as reloads. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 222 | bool transition_type_can_be_converted = false; |
Liang Zhao | 0351d76 | 2024-11-13 00:10:07 | [diff] [blame] | 223 | if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) { |
| 224 | if (ui::PageTransitionCoreTypeIs(transition_type, |
| 225 | ui::PAGE_TRANSITION_RELOAD)) { |
| 226 | transition_type_can_be_converted = true; |
| 227 | } |
| 228 | } else { |
| 229 | if (ui::PageTransitionCoreTypeIs(transition_type, |
| 230 | ui::PAGE_TRANSITION_TYPED) || |
| 231 | ui::PageTransitionCoreTypeIs(transition_type, |
| 232 | ui::PAGE_TRANSITION_LINK)) { |
| 233 | transition_type_can_be_converted = true; |
| 234 | } |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 235 | } |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 236 | if (!transition_type_can_be_converted) |
| 237 | return false; |
| 238 | |
| 239 | // This check is required for cases like view-source:, etc. Here the URL of |
| 240 | // the navigation entry would contain the url of the page, while the virtual |
| 241 | // URL contains the full URL including the view-source prefix. |
| 242 | if (virtual_url != last_committed_entry->GetVirtualURL()) |
| 243 | return false; |
| 244 | |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 245 | // Check that the URLs match. |
| 246 | FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node); |
| 247 | // If there's no frame entry then by definition the URLs don't match. |
| 248 | if (!frame_entry) |
| 249 | return false; |
| 250 | |
| 251 | if (url != frame_entry->url()) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 252 | return false; |
| 253 | |
| 254 | // This check is required for Android WebView loadDataWithBaseURL. Apps |
| 255 | // can pass in anything in the base URL and we need to ensure that these |
| 256 | // match before classifying it as a reload. |
| 257 | if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) { |
| 258 | if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL()) |
| 259 | return false; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 260 | } |
| 261 | |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 262 | // Skip entries with SSL errors. |
| 263 | if (last_committed_entry->ssl_error()) |
| 264 | return false; |
| 265 | |
| 266 | // Don't convert to a reload when the last navigation was a POST or the new |
| 267 | // navigation is a POST. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 268 | if (frame_entry->get_has_post_data() || is_post) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 269 | return false; |
| 270 | |
| 271 | return true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 272 | } |
| 273 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 274 | std::optional<url::Origin> GetCommittedOriginForFrameEntry( |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 275 | const mojom::DidCommitProvisionalLoadParams& params, |
| 276 | NavigationRequest* request) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 277 | // Error pages commit in an opaque origin, yet have the real URL that resulted |
| 278 | // in an error as the |params.url|. Since successful reload of an error page |
| 279 | // should commit in the correct origin, setting the opaque origin on the |
| 280 | // FrameNavigationEntry will be incorrect. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 281 | if (request && request->DidEncounterError()) |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 282 | return std::nullopt; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 283 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 284 | return std::make_optional(params.origin); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 285 | } |
| 286 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 287 | bool IsValidURLForNavigation(FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 288 | const GURL& virtual_url, |
| 289 | const GURL& dest_url) { |
| 290 | // Don't attempt to navigate if the virtual URL is non-empty and invalid. |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 291 | if (node->IsOutermostMainFrame() && !virtual_url.is_valid() && |
| 292 | !virtual_url.is_empty()) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 293 | LOG(WARNING) << "Refusing to load for invalid virtual URL: " |
| 294 | << virtual_url.possibly_invalid_spec(); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | // Don't attempt to navigate to non-empty invalid URLs. |
| 299 | if (!dest_url.is_valid() && !dest_url.is_empty()) { |
| 300 | LOG(WARNING) << "Refusing to load invalid URL: " |
| 301 | << dest_url.possibly_invalid_spec(); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // The renderer will reject IPC messages with URLs longer than |
| 306 | // this limit, so don't attempt to navigate with a longer URL. |
| 307 | if (dest_url.spec().size() > url::kMaxURLChars) { |
| 308 | LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars |
| 309 | << " characters."; |
| 310 | return false; |
| 311 | } |
| 312 | |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 313 | // Reject renderer debug URLs because they should have been handled before |
| 314 | // we get to this point. This check handles renderer debug URLs |
| 315 | // that are inside a view-source: URL (e.g. view-source:chrome://kill) and |
| 316 | // provides defense-in-depth if a renderer debug URL manages to get here via |
| 317 | // some other path. We want to reject the navigation here so it doesn't |
| 318 | // violate assumptions in downstream code. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 319 | if (blink::IsRendererDebugURL(dest_url)) { |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 320 | LOG(WARNING) << "Refusing to load renderer debug URL: " |
| 321 | << dest_url.possibly_invalid_spec(); |
| 322 | return false; |
| 323 | } |
| 324 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 325 | // Guests only support navigations to known-safe schemes. This check already |
| 326 | // exists in the extensions layer, where it also dispatches proper events to |
| 327 | // the guest's embedder (see WebViewGuest::LoadURLWithParams). This check is |
| 328 | // for defense-in-depth to ensure that no other places in the codebase |
| 329 | // accidentally navigate guests to schemes such as WebUI, which is not |
| 330 | // supported. See https://crbug.com/1444221. |
| 331 | if (node->current_frame_host()->GetSiteInstance()->IsGuest()) { |
| 332 | auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance(); |
| 333 | if (!cpsp->IsWebSafeScheme(dest_url.scheme()) && |
| 334 | !dest_url.SchemeIs(url::kAboutScheme)) { |
| 335 | LOG(WARNING) << "Refusing to load unsafe URL in a guest: " |
| 336 | << dest_url.possibly_invalid_spec(); |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 344 | // See replaced_navigation_entry_data.h for details: this information is meant |
| 345 | // to ensure |*output_entry| keeps track of its original URL (landing page in |
| 346 | // case of server redirects) as it gets replaced (e.g. history.replaceState()), |
| 347 | // without overwriting it later, for main frames. |
| 348 | void CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 349 | NavigationEntryImpl* replaced_entry, |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 350 | NavigationEntryImpl* output_entry) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 351 | if (output_entry->GetReplacedEntryData().has_value() || |
| 352 | replaced_entry->IsInitialEntry()) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 353 | return; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 354 | } |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 355 | |
| 356 | ReplacedNavigationEntryData data; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 357 | data.first_committed_url = replaced_entry->GetURL(); |
| 358 | data.first_timestamp = replaced_entry->GetTimestamp(); |
| 359 | data.first_transition_type = replaced_entry->GetTransitionType(); |
Charlie Reis | b55438f | 2019-01-08 01:54:29 | [diff] [blame] | 360 | output_entry->set_replaced_entry_data(data); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 361 | } |
| 362 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 363 | blink::mojom::NavigationType GetNavigationType( |
| 364 | const GURL& old_url, |
| 365 | const GURL& new_url, |
| 366 | ReloadType reload_type, |
| 367 | NavigationEntryImpl* entry, |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 368 | FrameNavigationEntry* frame_entry, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 369 | bool has_pending_cross_document_commit, |
| 370 | bool is_currently_error_page, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 371 | bool is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 372 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 373 | bool is_unfenced_top_navigation) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 374 | // Reload navigations |
| 375 | switch (reload_type) { |
| 376 | case ReloadType::NORMAL: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 377 | return blink::mojom::NavigationType::RELOAD; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 378 | case ReloadType::BYPASSING_CACHE: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 379 | return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 380 | case ReloadType::NONE: |
| 381 | break; // Fall through to rest of function. |
| 382 | } |
| 383 | |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 384 | if (entry->IsRestored()) { |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 385 | return entry->GetHasPostData() |
| 386 | ? blink::mojom::NavigationType::RESTORE_WITH_POST |
| 387 | : blink::mojom::NavigationType::RESTORE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 388 | } |
| 389 | |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 390 | const bool can_be_same_document = |
| 391 | // A pending cross-document commit means this navigation will not occur in |
| 392 | // the current document, as that document would end up being replaced in |
| 393 | // the meantime. |
| 394 | !has_pending_cross_document_commit && |
| 395 | // If the current document is an error page, we should always treat it as |
| 396 | // a different-document navigation so that we'll attempt to load the |
| 397 | // document we're navigating to (and not stay in the current error page). |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 398 | !is_currently_error_page && |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 399 | // If the navigation is an embedder-initiated navigation of a fenced |
| 400 | // frame root (i.e. enters a fenced frame tree from outside), |
| 401 | // same-document navigations should be disabled because we don't want to |
| 402 | // allow information to be joined across multiple embedder-initiated |
| 403 | // fenced frame navigations (which may contain separate cross-site data). |
| 404 | !is_embedder_initiated_fenced_frame_navigation && |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 405 | // If the navigation is to _unfencedTop (i.e. escapes a fenced frame), |
| 406 | // same-document navigations should be disabled because we want to force |
| 407 | // the creation of a new browsing context group. |
| 408 | !is_unfenced_top_navigation; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 409 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 410 | // History navigations. |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 411 | if (frame_entry && frame_entry->page_state().IsValid()) { |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 412 | return can_be_same_document && is_same_document_history_load |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 413 | ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT |
| 414 | : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 415 | } |
| 416 | DCHECK(!is_same_document_history_load); |
| 417 | |
| 418 | // A same-document fragment-navigation happens when the only part of the url |
| 419 | // that is modified is after the '#' character. |
| 420 | // |
| 421 | // When modifying this condition, please take a look at: |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 422 | // FrameLoader::ShouldPerformFragmentNavigation(). |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 423 | // |
| 424 | // Note: this check is only valid for navigations that are not history |
| 425 | // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a |
| 426 | // history navigation from 'A#foo' to 'A#bar' is not a same-document |
| 427 | // navigation, but a different-document one. This is why history navigation |
| 428 | // are classified before this check. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 429 | bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) && |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 430 | frame_entry && frame_entry->method() == "GET"; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 431 | |
| 432 | // The one case where we do the wrong thing here and incorrectly choose |
| 433 | // SAME_DOCUMENT is if the navigation is browser-initiated but the document in |
| 434 | // the renderer is a frameset. All frameset navigations should be |
| 435 | // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated |
| 436 | // navigation would do the right thing, as it would send it to the browser and |
| 437 | // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get |
| 438 | // into this method). But since we can't tell that case here for browser- |
| 439 | // initiated navigations, we have to get the renderer involved. In that case |
| 440 | // the navigation would be restarted due to the renderer spending a reply of |
| 441 | // mojom::CommitResult::RestartCrossDocument. |
| 442 | |
| 443 | return can_be_same_document && is_same_doc |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 444 | ? blink::mojom::NavigationType::SAME_DOCUMENT |
| 445 | : blink::mojom::NavigationType::DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 446 | } |
| 447 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 448 | // Adjusts the original input URL if needed, to get the URL to actually load and |
| 449 | // the virtual URL, which may differ. |
| 450 | void RewriteUrlForNavigation(const GURL& original_url, |
| 451 | BrowserContext* browser_context, |
| 452 | GURL* url_to_load, |
| 453 | GURL* virtual_url, |
| 454 | bool* reverse_on_redirect) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 455 | // Allow the browser URL handler to rewrite the URL. This will, for example, |
| 456 | // remove "view-source:" from the beginning of the URL to get the URL that |
| 457 | // will actually be loaded. This real URL won't be shown to the user, just |
| 458 | // used internally. |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 459 | *url_to_load = *virtual_url = original_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 460 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 461 | url_to_load, browser_context, reverse_on_redirect); |
| 462 | } |
| 463 | |
| 464 | #if DCHECK_IS_ON() |
| 465 | // Helper sanity check function used in debug mode. |
| 466 | void ValidateRequestMatchesEntry(NavigationRequest* request, |
| 467 | NavigationEntryImpl* entry) { |
| 468 | if (request->frame_tree_node()->IsMainFrame()) { |
| 469 | DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated()); |
| 470 | DCHECK(ui::PageTransitionTypeIncludingQualifiersIs( |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 471 | ui::PageTransitionFromInt(request->common_params().transition), |
| 472 | entry->GetTransitionType())); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 473 | } |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 474 | DCHECK_EQ(request->commit_params().should_clear_history_list, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 475 | entry->should_clear_history_list()); |
| 476 | DCHECK_EQ(request->common_params().has_user_gesture, |
| 477 | entry->has_user_gesture()); |
| 478 | DCHECK_EQ(request->common_params().base_url_for_data_url, |
| 479 | entry->GetBaseURLForDataURL()); |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 480 | DCHECK_EQ(request->commit_params().can_load_local_resources, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 481 | entry->GetCanLoadLocalResources()); |
| 482 | DCHECK_EQ(request->common_params().started_from_context_menu, |
| 483 | entry->has_started_from_context_menu()); |
| 484 | |
| 485 | FrameNavigationEntry* frame_entry = |
| 486 | entry->GetFrameEntry(request->frame_tree_node()); |
| 487 | if (!frame_entry) { |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 488 | // TODO(https://crbug.com/40467594): This case should be unreachable once |
| 489 | // all subframes have FrameNavigationEntries associated with them. |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 493 | DCHECK_EQ(request->common_params().method, frame_entry->method()); |
| 494 | |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 495 | size_t redirect_size = request->commit_params().redirects.size(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 496 | if (redirect_size == frame_entry->redirect_chain().size()) { |
| 497 | for (size_t i = 0; i < redirect_size; ++i) { |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 498 | DCHECK_EQ(request->commit_params().redirects[i], |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 499 | frame_entry->redirect_chain()[i]); |
| 500 | } |
| 501 | } else { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 502 | NOTREACHED(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | #endif // DCHECK_IS_ON() |
| 506 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 507 | // Returns whether the session history NavigationRequests in |navigations| |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 508 | // would stay within the subtree of |sandboxed_initiator_rfh|. |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 509 | bool DoesSandboxNavigationStayWithinSubtree( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 510 | RenderFrameHostImpl* sandboxed_initiator_rfh, |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 511 | const std::vector<std::unique_ptr<NavigationRequest>>& navigations) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 512 | DCHECK(sandboxed_initiator_rfh); |
| 513 | DCHECK(sandboxed_initiator_rfh->IsSandboxed( |
| 514 | network::mojom::WebSandboxFlags::kTopNavigation)); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 515 | for (auto& item : navigations) { |
| 516 | bool within_subtree = false; |
| 517 | // Check whether this NavigationRequest affects a frame within the |
| 518 | // sandboxed frame's subtree by walking up the tree looking for the |
| 519 | // sandboxed frame. |
| 520 | for (auto* frame = item->frame_tree_node(); frame; |
Alexander Timin | 381e7e18 | 2020-04-28 19:04:03 | [diff] [blame] | 521 | frame = FrameTreeNode::From(frame->parent())) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 522 | if (frame == sandboxed_initiator_rfh->frame_tree_node()) { |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 523 | within_subtree = true; |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | if (!within_subtree) |
| 528 | return false; |
| 529 | } |
| 530 | return true; |
| 531 | } |
| 532 | |
William Liu | 12275494 | 2024-01-18 22:34:39 | [diff] [blame] | 533 | // Used for "Navigation.SessionHistoryCount" histogram. |
| 534 | enum class HistoryNavTypeForHistogram { |
| 535 | // A same-doc or cross-doc navigation of the main frame. We can only have one |
| 536 | // main frame request per `NavigationController::GoToIndex()`. |
| 537 | kMainFrame = 0, |
| 538 | // `NavigationController::GoToIndex()` creates one main frame request and |
| 539 | // one or more subframe requests. |
| 540 | kMainFrameAndSubframe, |
| 541 | // `NavigationController::GoToIndex()` creates one or more subframe requests. |
| 542 | kSubframe, |
| 543 | // Used for histogram boundary. |
| 544 | kCount |
| 545 | }; |
| 546 | |
| 547 | void CountRequests( |
| 548 | const std::vector<std::unique_ptr<NavigationRequest>>& requests, |
| 549 | int& mutable_main_frame_cnt, |
| 550 | int& mutable_subframe_cnt) { |
| 551 | for (const auto& req : requests) { |
| 552 | if (req->IsInPrimaryMainFrame()) { |
| 553 | // We can only have one main frame navigation at a time. |
| 554 | CHECK_EQ(mutable_main_frame_cnt, 0); |
| 555 | ++mutable_main_frame_cnt; |
| 556 | } else if (req->GetNavigatingFrameType() == FrameType::kSubframe) { |
| 557 | ++mutable_subframe_cnt; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // Record the number of different types of navigations as histograms. See |
| 563 | // `HistoryNavTypeForHistogram` for the types. |
| 564 | void CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions( |
| 565 | const std::vector<std::unique_ptr<NavigationRequest>>& cross_doc_requests, |
| 566 | const std::vector<std::unique_ptr<NavigationRequest>>& same_doc_requests) { |
| 567 | // We must have one request. |
| 568 | CHECK(!cross_doc_requests.empty() || !same_doc_requests.empty()); |
| 569 | |
| 570 | int main_frame_cnt = 0; |
| 571 | int subframe_cnt = 0; |
| 572 | CountRequests(cross_doc_requests, main_frame_cnt, subframe_cnt); |
| 573 | CountRequests(same_doc_requests, main_frame_cnt, subframe_cnt); |
| 574 | |
| 575 | std::optional<HistoryNavTypeForHistogram> history_nav_type; |
| 576 | if (main_frame_cnt > 0) { |
| 577 | if (subframe_cnt == 0) { |
| 578 | history_nav_type = HistoryNavTypeForHistogram::kMainFrame; |
| 579 | } else { |
| 580 | history_nav_type = HistoryNavTypeForHistogram::kMainFrameAndSubframe; |
| 581 | } |
| 582 | } else if (subframe_cnt > 0) { |
| 583 | history_nav_type = HistoryNavTypeForHistogram::kSubframe; |
| 584 | } |
| 585 | if (history_nav_type.has_value()) { |
| 586 | UMA_HISTOGRAM_ENUMERATION("Navigation.BrowserInitiatedSessionHistoryCount", |
| 587 | history_nav_type.value(), |
| 588 | HistoryNavTypeForHistogram::kCount); |
| 589 | } |
| 590 | } |
| 591 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 592 | } // namespace |
| 593 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 594 | // NavigationControllerImpl::PendingEntryRef------------------------------------ |
| 595 | |
| 596 | NavigationControllerImpl::PendingEntryRef::PendingEntryRef( |
| 597 | base::WeakPtr<NavigationControllerImpl> controller) |
| 598 | : controller_(controller) {} |
| 599 | |
| 600 | NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() { |
| 601 | if (!controller_) // Can be null with interstitials. |
| 602 | return; |
| 603 | |
| 604 | controller_->PendingEntryRefDeleted(this); |
| 605 | } |
| 606 | |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 607 | // NavigationControllerImpl::ScopedPendingEntryReentrancyGuard------------------ |
| 608 | |
| 609 | NavigationControllerImpl::ScopedPendingEntryReentrancyGuard:: |
| 610 | ScopedPendingEntryReentrancyGuard( |
| 611 | base::SafeRef<NavigationControllerImpl> controller) |
| 612 | : controller_(controller) { |
| 613 | // Navigations that involve pending entries do not support re-entrancy. |
| 614 | // Encountering this CHECK failure means the caller is attempting to start |
| 615 | // another navigation while a navigation to an existing pending entry is on |
| 616 | // the stack. In most cases, the new navigation should be done from a posted |
| 617 | // task instead. See http://crbug.com/40353566 for context. |
| 618 | CHECK(!controller->in_navigate_to_pending_entry_); |
| 619 | |
| 620 | controller->in_navigate_to_pending_entry_ = true; |
| 621 | controller->CheckPotentialNavigationReentrancy(); |
| 622 | |
| 623 | // It must not be possible to delete the pending NavigationEntry while |
| 624 | // navigating to it. Grab a reference to delay potential deletion until |
| 625 | // this scoped object is deleted at the end of the relevant function call. |
| 626 | pending_entry_ref_ = controller->ReferencePendingEntry(); |
| 627 | } |
| 628 | |
| 629 | NavigationControllerImpl::ScopedPendingEntryReentrancyGuard:: |
| 630 | ~ScopedPendingEntryReentrancyGuard() { |
| 631 | controller_->in_navigate_to_pending_entry_ = false; |
| 632 | } |
| 633 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 634 | // NavigationControllerImpl ---------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 635 | |
[email protected] | 23a918b | 2014-07-15 09:51:36 | [diff] [blame] | 636 | const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1); |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 637 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 638 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 639 | size_t NavigationControllerImpl::max_entry_count_for_testing_ = |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 640 | kMaxEntryCountForTestingNotSet; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 641 | |
[email protected] | e6fec47 | 2013-05-14 05:29:02 | [diff] [blame] | 642 | // Should Reload check for post data? The default is true, but is set to false |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 643 | // when testing. |
| 644 | static bool g_check_for_repost = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 645 | |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 646 | // static |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 647 | std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry( |
| 648 | const GURL& url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 649 | Referrer referrer, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 650 | std::optional<url::Origin> initiator_origin, |
| 651 | std::optional<GURL> initiator_base_url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 652 | ui::PageTransition transition, |
| 653 | bool is_renderer_initiated, |
| 654 | const std::string& extra_headers, |
| 655 | BrowserContext* browser_context, |
| 656 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { |
| 657 | return NavigationControllerImpl::CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 658 | url, referrer, std::move(initiator_origin), std::move(initiator_base_url), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 659 | std::nullopt /* source_process_site_url */, transition, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 660 | is_renderer_initiated, extra_headers, browser_context, |
| 661 | std::move(blob_url_loader_factory), true /* rewrite_virtual_urls */); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // static |
| 665 | std::unique_ptr<NavigationEntryImpl> |
| 666 | NavigationControllerImpl::CreateNavigationEntry( |
| 667 | const GURL& url, |
| 668 | Referrer referrer, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 669 | std::optional<url::Origin> initiator_origin, |
| 670 | std::optional<GURL> initiator_base_url, |
| 671 | std::optional<GURL> source_process_site_url, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 672 | ui::PageTransition transition, |
| 673 | bool is_renderer_initiated, |
| 674 | const std::string& extra_headers, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 675 | BrowserContext* browser_context, |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 676 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
| 677 | bool rewrite_virtual_urls) { |
| 678 | GURL url_to_load = url; |
| 679 | GURL virtual_url = url; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 680 | bool reverse_on_redirect = false; |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 681 | if (rewrite_virtual_urls) { |
| 682 | RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url, |
| 683 | &reverse_on_redirect); |
| 684 | } |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 685 | // Let the NTP override the navigation params and pretend that this is a |
| 686 | // browser-initiated, bookmark-like navigation. |
| 687 | GetContentClient()->browser()->OverrideNavigationParams( |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 688 | source_process_site_url, &transition, &is_renderer_initiated, &referrer, |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 689 | &initiator_origin); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 690 | |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 691 | auto entry = std::make_unique<NavigationEntryImpl>( |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 692 | nullptr, // The site instance for tabs is sent on navigation |
| 693 | // (WebContents::GetSiteInstance). |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 694 | url_to_load, referrer, initiator_origin, initiator_base_url, |
| 695 | std::u16string(), transition, is_renderer_initiated, |
| 696 | blob_url_loader_factory, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 697 | /* is_initial_entry = */ false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 698 | entry->SetVirtualURL(virtual_url); |
| 699 | entry->set_user_typed_url(virtual_url); |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 700 | entry->set_update_virtual_url_with_url(reverse_on_redirect); |
| 701 | entry->set_extra_headers(extra_headers); |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 702 | return entry; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 703 | } |
| 704 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 705 | // static |
| 706 | void NavigationController::DisablePromptOnRepost() { |
| 707 | g_check_for_repost = false; |
| 708 | } |
| 709 | |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 710 | base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime( |
| 711 | base::Time t) { |
| 712 | // If |t| is between the water marks, we're in a run of duplicates |
| 713 | // or just getting out of it, so increase the high-water mark to get |
| 714 | // a time that probably hasn't been used before and return it. |
| 715 | if (low_water_mark_ <= t && t <= high_water_mark_) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 716 | high_water_mark_ += base::Microseconds(1); |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 717 | return high_water_mark_; |
| 718 | } |
| 719 | |
| 720 | // Otherwise, we're clear of the last duplicate run, so reset the |
| 721 | // water marks. |
| 722 | low_water_mark_ = high_water_mark_ = t; |
| 723 | return t; |
| 724 | } |
| 725 | |
ckitagawa | 0faa5e4 | 2020-06-17 17:30:54 | [diff] [blame] | 726 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 727 | ScopedShowRepostDialogForTesting() |
| 728 | : was_disallowed_(g_check_for_repost) { |
| 729 | g_check_for_repost = true; |
| 730 | } |
| 731 | |
| 732 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 733 | ~ScopedShowRepostDialogForTesting() { |
| 734 | g_check_for_repost = was_disallowed_; |
| 735 | } |
| 736 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 737 | NavigationControllerImpl::RemovedEntriesTracker::RemovedEntriesTracker( |
| 738 | base::SafeRef<NavigationControllerImpl> controller) |
| 739 | : controller_(controller) { |
| 740 | for (FrameTreeNode* frame_tree_node : controller_->frame_tree().Nodes()) { |
| 741 | names_to_nodes_.insert({frame_tree_node->unique_name(), frame_tree_node}); |
| 742 | frame_tree_node_id_to_keys_.insert( |
| 743 | {frame_tree_node->frame_tree_node_id(), std::set<std::string>()}); |
| 744 | if (auto* frame_entry = frame_tree_node->current_frame_host() |
| 745 | ->last_committed_frame_entry()) { |
| 746 | frame_tree_node_id_to_doc_seq_nos_.insert( |
| 747 | {frame_tree_node->frame_tree_node_id(), |
| 748 | frame_entry->document_sequence_number()}); |
| 749 | } |
| 750 | } |
| 751 | PopulateKeySet(Direction::kBack); |
| 752 | PopulateKeySet(Direction::kForward); |
| 753 | } |
| 754 | |
| 755 | void NavigationControllerImpl::RemovedEntriesTracker::PopulateKeySet( |
| 756 | Direction direction) { |
| 757 | // Keep track of which FrameTreeNodes may still have relevant API keys in |
| 758 | // additional FrameNavigationEntries. |
| 759 | std::set<FrameTreeNode*> nodes_to_process; |
| 760 | for (FrameTreeNode* node : controller_->frame_tree().Nodes()) { |
| 761 | nodes_to_process.insert(node); |
| 762 | } |
| 763 | |
| 764 | const int offset = direction == Direction::kForward ? 1 : -1; |
| 765 | const int start = direction == Direction::kForward |
| 766 | ? controller_->GetLastCommittedEntryIndex() |
| 767 | : controller_->GetLastCommittedEntryIndex() - 1; |
| 768 | for (int i = start; |
| 769 | i >= 0 && i < controller_->GetEntryCount() && !nodes_to_process.empty(); |
| 770 | i += offset) { |
| 771 | std::set<FrameTreeNode*> nodes_to_continue_processing; |
| 772 | |
| 773 | NavigationEntryImpl* entry = controller_->GetEntryAtIndex(i); |
| 774 | entry->ForEachFrameEntry([this, &nodes_to_process, |
| 775 | &nodes_to_continue_processing, |
| 776 | &entry](FrameNavigationEntry* frame_entry) { |
| 777 | // Find the |node| that matches |frame_entry|, if any. |
| 778 | FrameTreeNode* node = nullptr; |
| 779 | if (frame_entry == entry->root_node()->frame_entry) { |
| 780 | node = controller_->frame_tree().root(); |
| 781 | } else { |
| 782 | auto it = names_to_nodes_.find(frame_entry->frame_unique_name()); |
| 783 | if (it == names_to_nodes_.end()) |
| 784 | return; |
| 785 | node = it->second; |
| 786 | } |
| 787 | |
| 788 | // Skip this node if a previous step determine there are no longer |
| 789 | // relevant navigation API keys in this direction. |
| 790 | if (nodes_to_process.find(node) == nodes_to_process.end()) |
| 791 | return; |
| 792 | |
| 793 | // Stop processing |node| if we reach a point where it's cross-origin. |
| 794 | // See also PopulateSingleNavigationApiHistoryEntryVector(). |
| 795 | url::Origin frame_entry_origin = |
| 796 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 797 | frame_entry->url(), |
| 798 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 799 | if (!node->current_origin().IsSameOriginWith(frame_entry_origin)) |
| 800 | return; |
| 801 | |
| 802 | frame_tree_node_id_to_keys_[node->frame_tree_node_id()].insert( |
| 803 | frame_entry->navigation_api_key()); |
| 804 | // Mark |node| as needing more processing for the next entry. |
| 805 | nodes_to_continue_processing.insert(node); |
| 806 | |
| 807 | // Check whether |node| went cross-document. If so, its children are |
| 808 | // no longer the same conceptual iframe as the current one, and |
| 809 | // should no longer be processed. This check is intentionally done |
| 810 | // after processing the current |node|, which may still have relevant |
| 811 | // discarded API keys. |
| 812 | if (frame_entry->document_sequence_number() != |
| 813 | frame_tree_node_id_to_doc_seq_nos_[node->frame_tree_node_id()]) { |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 814 | for (auto* descendant : node->frame_tree().SubtreeNodes(node)) |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 815 | nodes_to_process.erase(descendant); |
| 816 | } |
| 817 | }); |
| 818 | |
| 819 | nodes_to_process.swap(nodes_to_continue_processing); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | NavigationControllerImpl::RemovedEntriesTracker::~RemovedEntriesTracker() { |
| 824 | std::set<std::string> all_keys; |
| 825 | // Find all remaining navigation API keys after some entries have been |
| 826 | // removed. |
| 827 | for (auto& entry : controller_->entries_) { |
| 828 | entry->ForEachFrameEntry([&all_keys](FrameNavigationEntry* frame_entry) { |
| 829 | all_keys.insert(frame_entry->navigation_api_key()); |
| 830 | }); |
| 831 | } |
| 832 | |
| 833 | // Notify each frame in the renderer of any disposed navigation API keys. |
| 834 | for (auto& id_to_keys : frame_tree_node_id_to_keys_) { |
| 835 | std::vector<std::string> disposed_keys; |
| 836 | for (const auto& key : id_to_keys.second) { |
| 837 | if (all_keys.find(key) == all_keys.end()) |
| 838 | disposed_keys.push_back(key); |
| 839 | } |
| 840 | if (disposed_keys.empty()) |
| 841 | continue; |
| 842 | |
| 843 | FrameTreeNode* node = controller_->frame_tree().FindByID(id_to_keys.first); |
| 844 | auto& frame = node->current_frame_host()->GetAssociatedLocalFrame(); |
| 845 | frame->NotifyNavigationApiOfDisposedEntries(disposed_keys); |
| 846 | } |
| 847 | } |
| 848 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 849 | NavigationControllerImpl::NavigationControllerImpl( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 850 | BrowserContext* browser_context, |
| 851 | FrameTree& frame_tree, |
| 852 | NavigationControllerDelegate* delegate) |
| 853 | : frame_tree_(frame_tree), |
| 854 | browser_context_(browser_context), |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 855 | delegate_(delegate), |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 856 | ssl_manager_(this), |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 857 | get_timestamp_callback_(base::BindRepeating(&base::Time::Now)), |
| 858 | back_forward_cache_(browser_context) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 859 | DCHECK(browser_context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 860 | } |
| 861 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 862 | NavigationControllerImpl::~NavigationControllerImpl() { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 863 | // The NavigationControllerImpl might be called inside its delegate |
| 864 | // destructor. Calling it is not valid anymore. |
| 865 | delegate_ = nullptr; |
| 866 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 867 | } |
| 868 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 869 | BrowserContext* NavigationControllerImpl::GetBrowserContext() { |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 870 | return browser_context_; |
| 871 | } |
| 872 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 873 | void NavigationControllerImpl::Restore( |
[email protected] | 5e36967 | 2009-11-03 23:48:30 | [diff] [blame] | 874 | int selected_navigation, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 875 | RestoreType type, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 876 | std::vector<std::unique_ptr<NavigationEntry>>* entries) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 877 | // Note that it's possible for `entries_` to contain multiple entries at this |
| 878 | // point, as Restore() might be called on a NavigationController that is |
| 879 | // already used (e.g. due to WebView's restoreState() API), not only for fresh |
| 880 | // NavigationControllers. These entries are not cleared to preserve legacy |
| 881 | // behavior and also because `pending_entry_` might point to one of the |
| 882 | // pre-existing entries. An exception for this is when `entries_` contains |
| 883 | // only the initial NavigationEntry, which must be removed. |
| 884 | |
| 885 | // Do not proceed if selected_navigation will be out of bounds for the updated |
| 886 | // entries_ list, since it will be assigned to last_committed_entry_index_ and |
| 887 | // used to index entries_. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 888 | // TODO(crbug.com/40816356): Consider also returning early if entries |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 889 | // is empty, since there should be no work to do (rather than marking the |
| 890 | // existing entries as needing reload). Also consider returning early if the |
| 891 | // selected index is -1, which represents a no-committed-entry state. |
| 892 | if (selected_navigation < -1 || |
| 893 | selected_navigation >= |
| 894 | base::checked_cast<int>(entries->size() + entries_.size())) { |
| 895 | return; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 896 | } |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 897 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 898 | // There will always be at least one entry (new NavigationControllers will |
| 899 | // have the initial NavigationEntry). |
| 900 | if (selected_navigation == -1) |
| 901 | selected_navigation = 0; |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 902 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 903 | if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) { |
| 904 | // If we are on the initial NavigationEntry, it must be the only entry in |
| 905 | // the list. Since it's impossible to do a history navigation to the |
| 906 | // initial NavigationEntry, `pending_entry_index_` must be -1 (but |
| 907 | // `pending_entry` might be set for a new non-history navigation). |
| 908 | // Note that we should not clear `entries_` if `entries` is empty (when |
| 909 | // InitialNavigationEntry mode is enabled), since that would leave us |
| 910 | // without any NavigationEntry. |
| 911 | CHECK_EQ(1, GetEntryCount()); |
| 912 | CHECK_EQ(-1, pending_entry_index_); |
| 913 | entries_.clear(); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 914 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 915 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 916 | needs_reload_ = true; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 917 | needs_reload_type_ = NeedsReloadType::kRestoreSession; |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 918 | entries_.reserve(entries->size()); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 919 | for (auto& entry : *entries) { |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 920 | if (entry->GetURL().is_empty()) { |
| 921 | // We're trying to restore an entry with an empty URL (e.g. from |
Rakina Zata Amni | 2729a51 | 2022-03-16 05:54:01 | [diff] [blame] | 922 | // persisting the initial NavigationEntry [which is no longer possible but |
| 923 | // some old persisted sessions might still contain it] or when the |
| 924 | // serializer failed to write the URL because it's too long). Trying to |
| 925 | // restore and navigate to an entry with an empty URL will result in |
| 926 | // crashes, so change the URL to about:blank. See also |
| 927 | // https://crbug.com/1240138 and https://crbug.com/1299335. |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 928 | entry->SetURL(GURL(url::kAboutBlankURL)); |
| 929 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 930 | entries_.push_back( |
| 931 | NavigationEntryImpl::FromNavigationEntry(std::move(entry))); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 932 | } |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 933 | |
| 934 | // At this point, the |entries| is full of empty scoped_ptrs, so it can be |
| 935 | // cleared out safely. |
| 936 | entries->clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 937 | |
| 938 | // And finish the restore. |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 939 | FinishRestore(selected_navigation, type); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 940 | } |
| 941 | |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 942 | void NavigationControllerImpl::Reload(ReloadType reload_type, |
| 943 | bool check_for_repost) { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 944 | base::TimeTicks actual_navigation_start = base::TimeTicks::Now(); |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 945 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type", |
| 946 | (int)reload_type); |
| 947 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check", |
| 948 | check_for_repost); |
liaoyuke | 9168fba | 2017-03-10 19:20:28 | [diff] [blame] | 949 | DCHECK_NE(ReloadType::NONE, reload_type); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 950 | NavigationEntryImpl* entry = nullptr; |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 951 | int current_index = -1; |
| 952 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 953 | if (entry_replaced_by_post_commit_error_) { |
| 954 | // If there is an entry that was replaced by a currently active post-commit |
| 955 | // error navigation, this can't be the initial navigation. |
| 956 | DCHECK(!IsInitialNavigation()); |
| 957 | // If the current entry is a post commit error, we reload the entry it |
| 958 | // replaced instead. We leave the error entry in place until a commit |
| 959 | // replaces it, but the pending entry points to the original entry in the |
| 960 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 961 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 962 | entry = entry_replaced_by_post_commit_error_.get(); |
| 963 | current_index = GetCurrentEntryIndex(); |
| 964 | } else if (IsInitialNavigation() && pending_entry_) { |
| 965 | // If we are reloading the initial navigation, just use the current |
| 966 | // pending entry. Otherwise look up the current entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 967 | entry = pending_entry_; |
| 968 | // The pending entry might be in entries_ (e.g., after a Clone), so we |
| 969 | // should also update the current_index. |
| 970 | current_index = pending_entry_index_; |
| 971 | } else { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 972 | DiscardNonCommittedEntries(); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 973 | current_index = GetCurrentEntryIndex(); |
| 974 | if (current_index != -1) { |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 975 | entry = GetEntryAtIndex(current_index); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 976 | } |
[email protected] | 979a4bc | 2013-04-24 01:27:15 | [diff] [blame] | 977 | } |
[email protected] | 241db35 | 2013-04-22 18:04:05 | [diff] [blame] | 978 | |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 979 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 980 | // CanReload method. |
| 981 | if (!entry) |
| 982 | return; |
| 983 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 984 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 985 | // We should never navigate to an existing initial NavigationEntry that is |
| 986 | // the initial NavigationEntry for the initial empty document that hasn't |
| 987 | // been overridden by the synchronous about:blank commit, to preserve |
| 988 | // legacy behavior where trying to reload when the main frame is on the |
| 989 | // initial empty document won't result in a navigation. See also |
| 990 | // https://crbug.com/1277414. |
| 991 | return; |
| 992 | } |
| 993 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 994 | if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) { |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 995 | // The user is asking to reload a page with POST data. Prompt to make sure |
[email protected] | b5bb35f | 2009-02-05 20:17:07 | [diff] [blame] | 996 | // they really want to do this. If they do, the dialog will call us back |
| 997 | // with check_for_repost = false. |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 998 | delegate_->NotifyBeforeFormRepostWarningShow(); |
[email protected] | 965bb09 | 2010-04-09 11:59:02 | [diff] [blame] | 999 | |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 1000 | pending_reload_ = reload_type; |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 1001 | delegate_->ActivateAndShowRepostFormWarningDialog(); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1002 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1003 | } |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1004 | |
Wang Hui | a25efabc | 2022-09-24 17:27:22 | [diff] [blame] | 1005 | // Set ReloadType for |entry|. |
| 1006 | entry->set_reload_type(reload_type); |
| 1007 | |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1008 | if (!IsInitialNavigation()) |
| 1009 | DiscardNonCommittedEntries(); |
| 1010 | |
| 1011 | pending_entry_ = entry; |
| 1012 | pending_entry_index_ = current_index; |
| 1013 | pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 1014 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1015 | // location.reload() goes through BeginNavigation, so all reloads triggered |
| 1016 | // via this codepath are browser initiated. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1017 | NavigateToExistingPendingEntry( |
| 1018 | reload_type, |
| 1019 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1020 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1021 | /*navigation_api_key=*/nullptr, actual_navigation_start); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1022 | } |
| 1023 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1024 | void NavigationControllerImpl::CancelPendingReload() { |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1025 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 1026 | } |
| 1027 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1028 | void NavigationControllerImpl::ContinuePendingReload() { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1029 | // If the pending reload type has been cleared by another navigation |
| 1030 | // committing, then do not proceed to reload after a form repost dialog. |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1031 | if (pending_reload_ == ReloadType::NONE) { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1032 | return; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 1033 | } |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1034 | Reload(pending_reload_, false); |
| 1035 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 1036 | } |
| 1037 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1038 | bool NavigationControllerImpl::IsInitialNavigation() { |
[email protected] | 27ba81c | 2012-08-21 17:04:09 | [diff] [blame] | 1039 | return is_initial_navigation_; |
[email protected] | c70f9b8 | 2010-04-21 07:31:11 | [diff] [blame] | 1040 | } |
| 1041 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1042 | bool NavigationControllerImpl::IsInitialBlankNavigation() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 1043 | // Check that we're on the initial NavigationEntry and that this is not a |
| 1044 | // cloned tab. |
| 1045 | return IsInitialNavigation() && GetEntryCount() == 1 && |
| 1046 | GetLastCommittedEntry()->IsInitialEntry() && |
| 1047 | GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored; |
creis | 10a4ab7 | 2015-10-13 17:22:40 | [diff] [blame] | 1048 | } |
| 1049 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1050 | NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID( |
| 1051 | int nav_entry_id) const { |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 1052 | int index = GetEntryIndexWithUniqueID(nav_entry_id); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1053 | return (index != -1) ? entries_[index].get() : nullptr; |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 1054 | } |
| 1055 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 1056 | NavigationEntryImpl* |
| 1057 | NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending( |
| 1058 | int nav_entry_id) const { |
| 1059 | NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id); |
| 1060 | if (entry) |
| 1061 | return entry; |
| 1062 | return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 1063 | ? pending_entry_.get() |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 1064 | : nullptr; |
| 1065 | } |
| 1066 | |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1067 | void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation( |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1068 | const url::Origin& origin) { |
| 1069 | for (int i = 0; i < GetEntryCount(); i++) { |
| 1070 | auto* entry = GetEntryAtIndex(i); |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1071 | entry->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1072 | } |
| 1073 | if (entry_replaced_by_post_commit_error_) { |
| 1074 | // It's possible we could come back to this entry if the error |
| 1075 | // page/interstitial goes away. |
| 1076 | entry_replaced_by_post_commit_error_ |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1077 | ->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1078 | } |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1079 | } |
| 1080 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1081 | void NavigationControllerImpl::SetPendingEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 1082 | std::unique_ptr<NavigationEntryImpl> entry) { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 1083 | DiscardNonCommittedEntries(); |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1084 | pending_entry_ = entry.release(); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1085 | DCHECK_EQ(-1, pending_entry_index_); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1086 | } |
| 1087 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1088 | NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1089 | if (pending_entry_) |
| 1090 | return pending_entry_; |
| 1091 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1092 | } |
| 1093 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1094 | NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() { |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1095 | // The pending entry is safe to return for new (non-history), browser- |
| 1096 | // initiated navigations. Most renderer-initiated navigations should not |
| 1097 | // show the pending entry, to prevent URL spoof attacks. |
| 1098 | // |
| 1099 | // We make an exception for renderer-initiated navigations in new tabs, as |
| 1100 | // long as no other page has tried to access the initial empty document in |
| 1101 | // the new tab. If another page modifies this blank page, a URL spoof is |
| 1102 | // possible, so we must stop showing the pending entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1103 | bool safe_to_show_pending = |
| 1104 | pending_entry_ && |
| 1105 | // Require a new navigation. |
avi | 0dca04d | 2015-01-26 20:21:09 | [diff] [blame] | 1106 | pending_entry_index_ == -1 && |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1107 | // Require either browser-initiated or an unmodified new tab. |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 1108 | (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab()); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1109 | |
| 1110 | // Also allow showing the pending entry for history navigations in a new tab, |
| 1111 | // such as Ctrl+Back. In this case, no existing page is visible and no one |
| 1112 | // can script the new tab before it commits. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1113 | if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 && |
| 1114 | IsInitialNavigation() && !pending_entry_->is_renderer_initiated()) |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1115 | safe_to_show_pending = true; |
| 1116 | |
| 1117 | if (safe_to_show_pending) |
[email protected] | 867e1f9 | 2011-08-30 19:01:19 | [diff] [blame] | 1118 | return pending_entry_; |
| 1119 | return GetLastCommittedEntry(); |
| 1120 | } |
| 1121 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1122 | int NavigationControllerImpl::GetCurrentEntryIndex() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1123 | if (pending_entry_index_ != -1) |
| 1124 | return pending_entry_index_; |
| 1125 | return last_committed_entry_index_; |
| 1126 | } |
| 1127 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1128 | NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 1129 | CHECK_NE(last_committed_entry_index_, -1); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1130 | return entries_[last_committed_entry_index_].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1131 | } |
| 1132 | |
Elad Alon | 32044f53 | 2025-03-04 22:16:03 | [diff] [blame] | 1133 | const NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() |
| 1134 | const { |
| 1135 | CHECK_NE(last_committed_entry_index_, -1); |
| 1136 | return entries_[last_committed_entry_index_].get(); |
| 1137 | } |
| 1138 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1139 | bool NavigationControllerImpl::CanViewSource() { |
Takashi Toyoshima | 6c58bbd | 2023-05-19 09:41:35 | [diff] [blame] | 1140 | const std::string& mime_type = frame_tree_->root() |
| 1141 | ->current_frame_host() |
| 1142 | ->GetPage() |
| 1143 | .GetContentsMimeType(); |
Kinuko Yasuda | 74702f9 | 2017-07-31 03:27:53 | [diff] [blame] | 1144 | bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) && |
| 1145 | !media::IsSupportedMediaMimeType(mime_type); |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 1146 | NavigationEntry* visible_entry = GetVisibleEntry(); |
| 1147 | return visible_entry && !visible_entry->IsViewSourceMode() && |
Carlos IL | d51e770 | 2020-05-07 18:51:39 | [diff] [blame] | 1148 | is_viewable_mime_type; |
[email protected] | 3168228 | 2010-01-15 18:05:16 | [diff] [blame] | 1149 | } |
| 1150 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1151 | int NavigationControllerImpl::GetLastCommittedEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1152 | // The last committed entry index must always be less than the number of |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1153 | // entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1154 | DCHECK_LT(last_committed_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1155 | return last_committed_entry_index_; |
| 1156 | } |
| 1157 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1158 | int NavigationControllerImpl::GetEntryCount() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1159 | DCHECK_GE(entries_.size(), 1u); |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 1160 | DCHECK_LE(entries_.size(), max_entry_count()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1161 | return static_cast<int>(entries_.size()); |
| 1162 | } |
| 1163 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1164 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) { |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1165 | if (index < 0 || index >= GetEntryCount()) |
| 1166 | return nullptr; |
| 1167 | |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1168 | return entries_[index].get(); |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 1169 | } |
| 1170 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1171 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) { |
avi | 057ce149 | 2015-06-29 15:59:47 | [diff] [blame] | 1172 | return GetEntryAtIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1173 | } |
| 1174 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1175 | int NavigationControllerImpl::GetIndexForOffset(int offset) { |
[email protected] | 7bc2b03 | 2012-12-19 22:45:46 | [diff] [blame] | 1176 | return GetCurrentEntryIndex() + offset; |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1177 | } |
| 1178 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1179 | std::optional<int> NavigationControllerImpl::GetIndexForGoBack() { |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1180 | for (int index = GetIndexForOffset(-1); index >= 0; index--) { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1181 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1182 | return index; |
| 1183 | } |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1184 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1185 | return std::nullopt; |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | bool NavigationControllerImpl::CanGoBack() { |
| 1189 | return GetIndexForGoBack().has_value(); |
| 1190 | } |
| 1191 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1192 | std::optional<int> NavigationControllerImpl::GetIndexForGoForward() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1193 | for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) { |
| 1194 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1195 | return index; |
| 1196 | } |
| 1197 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1198 | return std::nullopt; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1199 | } |
| 1200 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1201 | bool NavigationControllerImpl::CanGoForward() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1202 | return GetIndexForGoForward().has_value(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1203 | } |
| 1204 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1205 | bool NavigationControllerImpl::CanGoToOffset(int offset) { |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1206 | int index = GetIndexForOffset(offset); |
| 1207 | return index >= 0 && index < GetEntryCount(); |
| 1208 | } |
| 1209 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1210 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1211 | bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1212 | if (offset == 0) |
| 1213 | return true; |
| 1214 | int increment = offset > 0 ? 1 : -1; |
| 1215 | int non_skippable_entries = 0; |
| 1216 | for (int index = GetIndexForOffset(increment); |
| 1217 | index >= 0 && index < GetEntryCount(); index += increment) { |
| 1218 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1219 | non_skippable_entries++; |
| 1220 | |
| 1221 | if (non_skippable_entries == std::abs(offset)) |
| 1222 | return true; |
| 1223 | } |
| 1224 | return false; |
| 1225 | } |
| 1226 | #endif |
| 1227 | |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1228 | NavigationController::WeakNavigationHandleVector |
| 1229 | NavigationControllerImpl::GoBack() { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1230 | const std::optional<int> target_index = GetIndexForGoBack(); |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1231 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1232 | CHECK(target_index.has_value()); |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 1233 | |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1234 | return GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1235 | } |
| 1236 | |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1237 | NavigationController::WeakNavigationHandleVector |
| 1238 | NavigationControllerImpl::GoForward() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 1239 | // Note that at least one entry (the last one) will be non-skippable since |
| 1240 | // entries are marked skippable only when they add another entry because of |
| 1241 | // redirect or pushState. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1242 | const std::optional<int> target_index = GetIndexForGoForward(); |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1243 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1244 | CHECK(target_index.has_value()); |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1245 | |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1246 | return GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1247 | } |
| 1248 | |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1249 | NavigationController::WeakNavigationHandleVector |
| 1250 | NavigationControllerImpl::GoToIndex(int index) { |
| 1251 | std::vector<base::WeakPtr<NavigationRequest>> requests = |
| 1252 | GoToIndex(index, /*initiator_rfh=*/nullptr, |
| 1253 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1254 | /*navigation_api_key=*/nullptr, |
| 1255 | /*actual_navigation_start=*/base::TimeTicks::Now()); |
David Bokan | b8e0df0 | 2025-05-14 22:03:48 | [diff] [blame] | 1256 | std::vector<base::WeakPtr<NavigationHandle>> handles; |
| 1257 | std::ranges::move(requests, std::back_inserter(handles)); |
| 1258 | return handles; |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 1259 | } |
| 1260 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1261 | std::vector<base::WeakPtr<NavigationRequest>> |
| 1262 | NavigationControllerImpl::GoToIndex( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1263 | int index, |
| 1264 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1265 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1266 | soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1267 | const std::string* navigation_api_key, |
| 1268 | base::TimeTicks actual_navigation_start) { |
sunjian | 30574a6 | 2017-03-21 21:39:44 | [diff] [blame] | 1269 | TRACE_EVENT0("browser,navigation,benchmark", |
| 1270 | "NavigationControllerImpl::GoToIndex"); |
Peter Boström | f68fe04 | 2023-06-07 18:27:50 | [diff] [blame] | 1271 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index); |
| 1272 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_size", |
| 1273 | entries_.size()); |
| 1274 | // Indices should have have been validated by the caller. |
| 1275 | CHECK_GE(index, 0); |
| 1276 | CHECK_LT(static_cast<size_t>(index), entries_.size()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1277 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1278 | if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 1279 | // We should never navigate to an existing initial NavigationEntry that is |
| 1280 | // the initial NavigationEntry for the initial empty document that hasn't |
| 1281 | // been overridden by the synchronous about:blank commit, to preserve |
| 1282 | // legacy behavior where trying to reload when the main frame is on the |
| 1283 | // initial empty document won't result in a navigation. See also |
| 1284 | // https://crbug.com/1277414. |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1285 | return {}; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1286 | } |
| 1287 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1288 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1289 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1290 | DCHECK_EQ(nullptr, pending_entry_); |
| 1291 | DCHECK_EQ(-1, pending_entry_index_); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1292 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1293 | pending_entry_ = entries_[index].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1294 | pending_entry_index_ = index; |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1295 | pending_entry_->SetTransitionType(ui::PageTransitionFromInt( |
| 1296 | pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK)); |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1297 | return NavigateToExistingPendingEntry( |
| 1298 | ReloadType::NONE, initiator_rfh, soft_navigation_heuristics_task_id, |
| 1299 | navigation_api_key, actual_navigation_start); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1300 | } |
| 1301 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1302 | void NavigationControllerImpl::GoToOffset(int offset) { |
toyoshim | 3af4d50 | 2016-03-30 12:38:12 | [diff] [blame] | 1303 | // Note: This is actually reached in unit tests. |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1304 | if (!CanGoToOffset(offset)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1305 | return; |
| 1306 | |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1307 | GoToIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1308 | } |
| 1309 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1310 | void NavigationControllerImpl::GoToOffsetFromRenderer( |
| 1311 | int offset, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1312 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1313 | std::optional<blink::scheduler::TaskAttributionId> |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1314 | soft_navigation_heuristics_task_id, |
| 1315 | base::TimeTicks actual_navigation_start) { |
Nate Chapin | bb74c5a5 | 2023-07-21 23:13:35 | [diff] [blame] | 1316 | // Note: This is actually reached in unit tests. |
| 1317 | if (!CanGoToOffset(offset)) |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1318 | return; |
| 1319 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1320 | GoToIndex(GetIndexForOffset(offset), initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1321 | soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1322 | /*navigation_api_key=*/nullptr, actual_navigation_start); |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1323 | } |
| 1324 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1325 | std::vector<base::WeakPtr<NavigationRequest>> |
| 1326 | NavigationControllerImpl::GoToIndexAndReturnAllRequests(int index) { |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 1327 | return GoToIndex(index, /*initiator_rfh=*/nullptr, |
| 1328 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1329 | /*navigation_api_key=*/nullptr, |
| 1330 | /*actual_navigation_start=*/base::TimeTicks::Now()); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 1331 | } |
| 1332 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1333 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1334 | void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) { |
| 1335 | // Note: This is actually reached in unit tests. |
| 1336 | if (!CanGoToOffsetWithSkipping(offset)) |
| 1337 | return; |
| 1338 | |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 1339 | if (offset == 0) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1340 | GoToIndex(GetIndexForOffset(offset)); |
| 1341 | return; |
| 1342 | } |
| 1343 | int increment = offset > 0 ? 1 : -1; |
| 1344 | // Find the offset without counting skippable entries. |
| 1345 | int target_index = GetIndexForOffset(increment); |
| 1346 | int non_skippable_entries = 0; |
| 1347 | for (int index = target_index; index >= 0 && index < GetEntryCount(); |
| 1348 | index += increment) { |
| 1349 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1350 | non_skippable_entries++; |
| 1351 | |
| 1352 | if (non_skippable_entries == std::abs(offset)) { |
| 1353 | target_index = index; |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | GoToIndex(target_index); |
| 1359 | } |
| 1360 | #endif |
| 1361 | |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1362 | bool NavigationControllerImpl::RemoveEntryAtIndex(int index) { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1363 | if (index == last_committed_entry_index_ || index == pending_entry_index_) |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1364 | return false; |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 1365 | |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 1366 | RemoveEntryAtIndexInternal(index); |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1367 | return true; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1368 | } |
| 1369 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1370 | void NavigationControllerImpl::PruneForwardEntries() { |
| 1371 | DiscardNonCommittedEntries(); |
| 1372 | int remove_start_index = last_committed_entry_index_ + 1; |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1373 | int num_removed = static_cast<int>(entries_.size()) - remove_start_index; |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1374 | if (num_removed <= 0) |
| 1375 | return; |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 1376 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1377 | entries_.erase(entries_.begin() + remove_start_index, entries_.end()); |
| 1378 | NotifyPrunedEntries(this, remove_start_index /* start index */, |
| 1379 | num_removed /* count */); |
| 1380 | } |
| 1381 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1382 | void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry, |
| 1383 | const GURL& new_url) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1384 | GURL new_virtual_url(new_url); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 1385 | if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite( |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 1386 | &new_virtual_url, entry->GetVirtualURL(), browser_context_)) { |
| 1387 | entry->SetVirtualURL(new_virtual_url); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1391 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL( |
| 1392 | const GURL& url, |
| 1393 | const Referrer& referrer, |
| 1394 | ui::PageTransition transition, |
| 1395 | const std::string& extra_headers) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1396 | LoadURLParams params(url); |
| 1397 | params.referrer = referrer; |
| 1398 | params.transition_type = transition; |
| 1399 | params.extra_headers = extra_headers; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1400 | return LoadURLWithParams(params); |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1401 | } |
| 1402 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1403 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams( |
| 1404 | const LoadURLParams& params) { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1405 | // For now, treat this as the actual navigation start time, even though a fair |
| 1406 | // amount of work is done in the browser process between the various ways to |
| 1407 | // start a navigation and reaching here (e.g., sending OpenURLParams through |
| 1408 | // the embedder and converting them to LoadURLParams). Note that this may be |
| 1409 | // used for some renderer-initiated navigations that go through |
| 1410 | // RenderFrameHostImpl::OpenURL as well. |
| 1411 | // TODO(crbug.com/385170155): Consider whether tracking this earlier work with |
| 1412 | // an earlier start time is worthwhile for metrics. |
| 1413 | base::TimeTicks actual_navigation_start = base::TimeTicks::Now(); |
| 1414 | |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1415 | if (params.is_renderer_initiated) |
| 1416 | DCHECK(params.initiator_origin.has_value()); |
| 1417 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 1418 | TRACE_EVENT1("browser,navigation", |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1419 | "NavigationControllerImpl::LoadURLWithParams", "url", |
| 1420 | params.url.possibly_invalid_spec()); |
Emmanuel Arias Soto | 9e15965 | 2025-05-16 07:53:55 | [diff] [blame] | 1421 | |
| 1422 | if (IsDebugURL(params.url)) { |
| 1423 | // Browser-debug URLs won't go through NavigationThrottles so we have to |
| 1424 | // check them explicitly. See crbug.com/40605746. |
| 1425 | ContentBrowserClient* client = GetContentClient()->browser(); |
| 1426 | if (client->ShouldBlockRendererDebugURL( |
| 1427 | params.url, browser_context_, |
| 1428 | GetTargetFrameTreeNodeForNavigation(params) |
| 1429 | ->current_frame_host())) { |
| 1430 | DiscardPendingEntry(false); |
| 1431 | return nullptr; |
| 1432 | } |
| 1433 | HandleDebugURL(params.url, params.transition_type, |
| 1434 | client->IsExplicitNavigation(params.transition_type)); |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1435 | // If Telemetry is running, allow the URL load to proceed as if it's |
| 1436 | // unhandled, otherwise Telemetry can't tell if Navigation completed. |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 1437 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
Peter Kasting | 6e6af3c | 2024-10-15 00:13:18 | [diff] [blame] | 1438 | switches::kEnableGpuBenchmarking)) { |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1439 | return nullptr; |
Peter Kasting | 6e6af3c | 2024-10-15 00:13:18 | [diff] [blame] | 1440 | } |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1441 | } |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 1442 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1443 | // Checks based on params.load_type. |
| 1444 | switch (params.load_type) { |
| 1445 | case LOAD_TYPE_DEFAULT: |
lukasza | 477a5a2 | 2016-06-16 18:28:43 | [diff] [blame] | 1446 | case LOAD_TYPE_HTTP_POST: |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 1447 | #if BUILDFLAG(IS_ANDROID) |
| 1448 | case LOAD_TYPE_PDF_ANDROID: |
| 1449 | #endif |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1450 | break; |
| 1451 | case LOAD_TYPE_DATA: |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 1452 | if (!params.url.SchemeIs(url::kDataScheme)) { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 1453 | NOTREACHED() << "Data load must use data scheme."; |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1454 | } |
| 1455 | break; |
Lukasz Anforowicz | bb0cfd5e | 2017-12-14 22:39:46 | [diff] [blame] | 1456 | } |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1457 | |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1458 | // The user initiated a load, we don't need to reload anymore. |
| 1459 | needs_reload_ = false; |
| 1460 | |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 1461 | return NavigateWithoutEntry(params, actual_navigation_start); |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1462 | } |
| 1463 | |
Charlie Reis | 4c53a96 | 2023-06-21 23:17:53 | [diff] [blame] | 1464 | void NavigationControllerImpl::LoadOriginalRequestURL() { |
| 1465 | // If the original request URL is not valid, matches the current URL, or |
| 1466 | // involves POST data, then simply reload. The POST check avoids issues with |
| 1467 | // sending data to the wrong page. |
| 1468 | const GURL& last_committed_url = GetLastCommittedEntry()->GetURL(); |
| 1469 | const GURL& original_request_url = |
| 1470 | GetLastCommittedEntry()->GetOriginalRequestURL(); |
| 1471 | if (!original_request_url.is_valid() || |
| 1472 | original_request_url == last_committed_url || |
| 1473 | GetLastCommittedEntry()->GetHasPostData()) { |
| 1474 | Reload(ReloadType::NORMAL, true); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | // Otherwise, attempt to load the original request URL without any of the |
| 1479 | // other data from the current NavigationEntry, replacing the current entry. |
| 1480 | // Loading the original URL is useful in cases such as modifying the user |
| 1481 | // agent. |
| 1482 | std::unique_ptr<NavigationController::LoadURLParams> load_params = |
| 1483 | std::make_unique<NavigationController::LoadURLParams>( |
| 1484 | original_request_url); |
| 1485 | load_params->should_replace_current_entry = true; |
| 1486 | load_params->transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 1487 | LoadURLWithParams(*load_params.get()); |
| 1488 | } |
| 1489 | |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1490 | bool NavigationControllerImpl::PendingEntryMatchesRequest( |
| 1491 | NavigationRequest* request) const { |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1492 | return pending_entry_ && |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1493 | pending_entry_->GetUniqueID() == request->nav_entry_id(); |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1494 | } |
| 1495 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1496 | bool NavigationControllerImpl::RendererDidNavigate( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1497 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1498 | const mojom::DidCommitProvisionalLoadParams& params, |
peary2 | 1b0f797b | 2016-09-28 17:28:33 | [diff] [blame] | 1499 | LoadCommittedDetails* details, |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1500 | bool is_same_document_navigation, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1501 | bool was_on_initial_empty_document, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1502 | bool previous_document_had_history_intervention_activation, |
Camille Lamy | 10aafcd3 | 2018-12-05 15:48:13 | [diff] [blame] | 1503 | NavigationRequest* navigation_request) { |
| 1504 | DCHECK(navigation_request); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1505 | |
| 1506 | // Note: validation checks and renderer kills due to invalid commit messages |
| 1507 | // must happen before getting here, in |
| 1508 | // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some |
| 1509 | // effects of the navigation have already occurred. |
| 1510 | |
[email protected] | cd2e1574 | 2013-03-08 04:08:31 | [diff] [blame] | 1511 | is_initial_navigation_ = false; |
| 1512 | |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1513 | // Any pending request to repost a form submission is no longer valid, since a |
| 1514 | // different NavigationEntry is committing. |
| 1515 | pending_reload_ = ReloadType::NONE; |
| 1516 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1517 | // Save the previous state before we clobber it. |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1518 | bool overriding_user_agent_changed = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1519 | if (entry_replaced_by_post_commit_error_) { |
| 1520 | // Same document navigation events with a post-commit error should already |
| 1521 | // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before |
| 1522 | // reaching here. |
| 1523 | CHECK(!is_same_document_navigation); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1524 | |
Xinghui Lu | b9e86a35 | 2024-05-17 16:29:42 | [diff] [blame] | 1525 | if (pending_entry_) { |
| 1526 | // Before `entry_replaced_by_post_commit_error_` is moved back, make sure |
| 1527 | // `pending_entry_` isn't pointing to the last committed entry. |
| 1528 | // Instead, all reload approaches (e.g., in `Reload` and |
| 1529 | // `LoadIfNecessary`) should attempt to load the |
| 1530 | // `entry_replaced_by_post_commit_error_` instead of the post commit error |
| 1531 | // entry itself. |
| 1532 | CHECK_NE(pending_entry_, entries_[last_committed_entry_index_].get()) |
| 1533 | << "Incorrectly reloading the post commit error page entry."; |
| 1534 | } |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 1535 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1536 | // Any commit while a post-commit error page is showing should put the |
| 1537 | // original entry back, replacing the error page's entry. This includes |
| 1538 | // reloads, where the original entry was used as the pending entry and |
| 1539 | // should now be at the correct index at commit time. |
| 1540 | entries_[last_committed_entry_index_] = |
| 1541 | std::move(entry_replaced_by_post_commit_error_); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1542 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1543 | details->previous_main_frame_url = GetLastCommittedEntry()->GetURL(); |
| 1544 | details->previous_entry_index = GetLastCommittedEntryIndex(); |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1545 | // Must honor user agent overrides in the |navigation_request|, such as |
| 1546 | // from things like RequestDesktopSiteWebContentsObserverAndroid. As a |
| 1547 | // result, besides comparing |pending_entry_|'s user agent against |
| 1548 | // LastCommittedEntry's, also need to compare |navigation_request|'s user |
| 1549 | // agent against LastCommittedEntry's. |
| 1550 | if (navigation_request->is_overriding_user_agent() != |
Shu Yang | 127e41a | 2023-12-19 01:26:54 | [diff] [blame] | 1551 | GetLastCommittedEntry()->GetIsOverridingUserAgent() || |
| 1552 | (PendingEntryMatchesRequest(navigation_request) && |
| 1553 | pending_entry_->GetIsOverridingUserAgent() != |
| 1554 | GetLastCommittedEntry()->GetIsOverridingUserAgent())) { |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1555 | overriding_user_agent_changed = true; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1556 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1557 | |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1558 | bool is_main_frame_navigation = !rfh->GetParent(); |
| 1559 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1560 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1561 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1562 | // For primary frame tree navigations, choose an appropriate |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1563 | // BackForwardCacheMetrics to be associated with the new navigation's |
| 1564 | // NavigationEntry, by either creating a new object or reusing the previous |
| 1565 | // entry's one. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1566 | scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics; |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 1567 | if (navigation_request->frame_tree_node()->frame_tree().is_primary()) { |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1568 | back_forward_cache_metrics = BackForwardCacheMetrics:: |
| 1569 | CreateOrReuseBackForwardCacheMetricsForNavigation( |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1570 | GetLastCommittedEntry(), is_main_frame_navigation, |
| 1571 | params.document_sequence_number); |
| 1572 | } |
Yuzu Saijo | 29f96ca9 | 2022-12-08 04:54:12 | [diff] [blame] | 1573 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1574 | // Notify the last active entry that we have navigated away. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1575 | if (is_main_frame_navigation && !is_same_document_navigation) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1576 | if (auto* metrics = GetLastCommittedEntry()->back_forward_cache_metrics()) { |
| 1577 | metrics->MainFrameDidNavigateAwayFromDocument(); |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1581 | // Use CommonNavigationParam's `should_replace_current_entry` to determine |
| 1582 | // whether the current NavigationEntry should be replaced. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1583 | // (See below for a case where we might override that.) |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1584 | details->did_replace_entry = |
| 1585 | navigation_request->common_params().should_replace_current_entry; |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1586 | |
fdegans | 9caf66a | 2015-07-30 21:10:42 | [diff] [blame] | 1587 | // If there is a pending entry at this point, it should have a SiteInstance, |
Charlie Reis | c4155af | 2022-10-19 15:33:11 | [diff] [blame] | 1588 | // except for restored entries. This should be true even if the current commit |
| 1589 | // is not related to the pending entry. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1590 | bool was_restored = false; |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1591 | DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() || |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 1592 | pending_entry_->IsRestored()); |
Charlie Reis | c4155af | 2022-10-19 15:33:11 | [diff] [blame] | 1593 | |
| 1594 | // Only make changes based on the pending entry if the NavigationRequest |
| 1595 | // matches it. Otherwise, the pending entry may be for a different request |
| 1596 | // (e.g., if a slow history navigation is pending while an auto-subframe |
| 1597 | // commit occurs). |
| 1598 | if (PendingEntryMatchesRequest(navigation_request)) { |
| 1599 | // It is no longer necessary to consider the pending entry as restored. |
| 1600 | if (pending_entry_->IsRestored()) { |
| 1601 | pending_entry_->set_restore_type(RestoreType::kNotRestored); |
| 1602 | was_restored = true; |
| 1603 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1604 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1605 | // If the SiteInstance has changed from the matching pending entry, this |
| 1606 | // must be treated as a new navigation with replacement. Set the replacement |
| 1607 | // bit here and ClassifyNavigation will identify this case and return |
| 1608 | // NEW_ENTRY. |
| 1609 | if (!rfh->GetParent() && pending_entry_->site_instance() && |
| 1610 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
| 1611 | DCHECK_NE(-1, pending_entry_index_); |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1612 | // TODO(nasko,creis,rakina): Move this to happen before committing the |
| 1613 | // navigation. This is a bit complicated because we don't currently |
| 1614 | // set `should_replace_current_entry` for reload/history navigations. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1615 | details->did_replace_entry = true; |
| 1616 | } |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1617 | } |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 1618 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1619 | // Do navigation-type specific actions. These will make and commit an entry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1620 | NavigationType navigation_type = |
| 1621 | ClassifyNavigation(rfh, params, navigation_request); |
| 1622 | navigation_request->set_navigation_type(navigation_type); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1623 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1624 | if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node())) { |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1625 | // Ensure that this navigation does not add a navigation entry, since |
| 1626 | // ShouldMaintainTrivialSessionHistory() means we should not add an entry |
| 1627 | // beyond the last committed one. Therefore, `should_replace_current_entry` |
| 1628 | // should be set, which replaces the current entry, or this should be a |
| 1629 | // reload, which does not create a new entry. |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1630 | DCHECK(navigation_request->common_params().should_replace_current_entry || |
Dominic Farolino | e0f8d7c | 2023-08-16 15:38:33 | [diff] [blame] | 1631 | navigation_request->GetReloadType() != ReloadType::NONE); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1632 | } |
| 1633 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1634 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1635 | if (rfh->GetParent()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1636 | // This is a subframe navigation on the initial empty document, which used |
| 1637 | // to not have a NavigationEntry to attach to. Now it can attach to the |
| 1638 | // initial NavigationEntry, and we must ensure that its NavigationEntry |
| 1639 | // will keep the "initial NavigationEntry" status and won't append a new |
| 1640 | // NavigationEntry (it should always do replacement instead). |
| 1641 | // See also https://crbug.com/1277414. |
| 1642 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1643 | // Subframe navigation on initial NavigationEntry must not append a new |
| 1644 | // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This |
| 1645 | // means every subframe navigation that happens while we're on the initial |
| 1646 | // NavigationEntry will always reuse the existing NavigationEntry and |
| 1647 | // just update the corresponding FrameNavigationEntry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1648 | DCHECK_EQ(navigation_type, NAVIGATION_TYPE_AUTO_SUBFRAME); |
| 1649 | } else if (navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1650 | // This is a navigation that modifies the initial NavigationEntry, either |
| 1651 | // for a replacement or a reload. The initial NavigationEntry should |
| 1652 | // retain its "initial NavigationEntry" status in this case. |
| 1653 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1654 | } else if (navigation_request->is_synchronous_renderer_commit() && |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1655 | !navigation_request->IsSameDocument() && !rfh->GetParent()) { |
| 1656 | DCHECK(navigation_request->common_params().should_replace_current_entry); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1657 | // This is a synchronous about:blank navigation on the main frame, which |
| 1658 | // used to not create a NavigationEntry when we have no NavigationEntry on |
| 1659 | // FrameTree creation. We now have the initial NavigationEntry and are on |
| 1660 | // the initial NavigationEntry. To preserve old behavior, we should still |
| 1661 | // keep the "initial" status for the new NavigationEntry that we will |
| 1662 | // create for this navigation, so that subframe navigations under the |
| 1663 | // synchronously committed about:blank document will never append new |
| 1664 | // NavigationEntry, and instead will just reuse the initial |
| 1665 | // NavigationEntry and modify the corresponding FrameNavigationEntries. |
| 1666 | // See also https://crbug.com/1277414. |
| 1667 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | DCHECK(!details->should_stay_as_initial_entry || |
| 1671 | GetLastCommittedEntry()->IsInitialEntry()); |
[email protected] | 4bf3522c | 2010-08-19 21:00:20 | [diff] [blame] | 1672 | |
eugenebut | ee08663a | 2017-04-27 17:43:12 | [diff] [blame] | 1673 | // is_same_document must be computed before the entry gets committed. |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1674 | details->is_same_document = is_same_document_navigation; |
[email protected] | b9d4dfdc | 2013-08-08 00:25:12 | [diff] [blame] | 1675 | |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1676 | details->is_prerender_activation = |
| 1677 | navigation_request->IsPrerenderedPageActivation(); |
Robert Lin | 540dbd1 | 2022-04-28 22:07:24 | [diff] [blame] | 1678 | details->is_in_active_page = navigation_request->GetRenderFrameHost() |
| 1679 | ->GetOutermostMainFrame() |
| 1680 | ->IsInPrimaryMainFrame(); |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1681 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1682 | // Make sure we do not discard the pending entry for a different ongoing |
| 1683 | // navigation when a same document commit comes in unexpectedly from the |
| 1684 | // renderer. Limit this to a very narrow set of conditions to avoid risks to |
| 1685 | // other navigation types. See https://crbug.com/900036. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 1686 | // TODO(crbug.com/41437754): Handle history.pushState() as well. |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1687 | bool keep_pending_entry = |
| 1688 | is_same_document_navigation && |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1689 | navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY && |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1690 | pending_entry_ && !PendingEntryMatchesRequest(navigation_request); |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1691 | |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1692 | switch (navigation_type) { |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1693 | case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1694 | RendererDidNavigateToNewEntry( |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 1695 | rfh, params, details->is_same_document, details->did_replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1696 | previous_document_had_history_intervention_activation, |
| 1697 | navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1698 | break; |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1699 | case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1700 | RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document, |
| 1701 | was_restored, navigation_request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1702 | keep_pending_entry, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1703 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1704 | case NAVIGATION_TYPE_NEW_SUBFRAME: |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1705 | RendererDidNavigateNewSubframe( |
| 1706 | rfh, params, details->is_same_document, details->did_replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1707 | previous_document_had_history_intervention_activation, |
| 1708 | navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1709 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1710 | case NAVIGATION_TYPE_AUTO_SUBFRAME: |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1711 | if (!RendererDidNavigateAutoSubframe( |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1712 | rfh, params, details->is_same_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1713 | was_on_initial_empty_document, navigation_request, details)) { |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1714 | // We don't send a notification about auto-subframe PageState during |
| 1715 | // UpdateStateForFrame, since it looks like nothing has changed. Send |
| 1716 | // it here at commit time instead. |
| 1717 | NotifyEntryChanged(GetLastCommittedEntry()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1718 | return false; |
creis | 59d5a47cb | 2016-08-24 23:57:19 | [diff] [blame] | 1719 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1720 | break; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1721 | case NAVIGATION_TYPE_UNKNOWN: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 1722 | NOTREACHED(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1723 | } |
| 1724 | |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1725 | // At this point, we know that the navigation has just completed, so |
| 1726 | // record the time. |
| 1727 | // |
| 1728 | // TODO(akalin): Use "sane time" as described in |
Adam Langley | 4463fb83 | 2018-01-28 22:42:26 | [diff] [blame] | 1729 | // https://www.chromium.org/developers/design-documents/sane-time . |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 1730 | base::Time timestamp = |
| 1731 | time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run()); |
| 1732 | DVLOG(1) << "Navigation finished at (smoothed) timestamp " |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 1733 | << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1734 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1735 | // If we aren't keeping the pending entry, there shouldn't be one at this |
| 1736 | // point. Clear it again in case any error cases above forgot to do so. |
| 1737 | // TODO(pbos): Consider a CHECK here that verifies that the pending entry has |
| 1738 | // been cleared instead of protecting against it. |
| 1739 | if (!keep_pending_entry) |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1740 | DiscardNonCommittedEntriesWithCommitDetails(details); |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 1741 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1742 | // All committed entries should have nonempty content state so WebKit doesn't |
| 1743 | // get confused when we go back to them (see the function for details). |
creis | 0cade2e | 2017-02-28 06:37:47 | [diff] [blame] | 1744 | DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState."; |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1745 | NavigationEntryImpl* active_entry = GetLastCommittedEntry(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1746 | active_entry->SetTimestamp(timestamp); |
[email protected] | f49737b3 | 2013-08-28 07:51:44 | [diff] [blame] | 1747 | active_entry->SetHttpStatusCode(params.http_status_code); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1748 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1749 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1750 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1751 | if (back_forward_cache_metrics && |
| 1752 | !active_entry->back_forward_cache_metrics()) { |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1753 | active_entry->set_back_forward_cache_metrics( |
| 1754 | std::move(back_forward_cache_metrics)); |
| 1755 | } |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1756 | |
| 1757 | // `back_forward_cache_metrics()` may return null as we do not record |
| 1758 | // back-forward cache metrics for navigations in non-primary frame trees. |
| 1759 | if (active_entry->back_forward_cache_metrics()) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 1760 | // TODO(crbug.com/40229455): Remove this. |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1761 | // These are both only available from details at this point, so we capture |
| 1762 | // them here. |
| 1763 | SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type", |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1764 | navigation_type); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1765 | SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace", |
| 1766 | details->did_replace_entry); |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1767 | active_entry->back_forward_cache_metrics()->DidCommitNavigation( |
| 1768 | navigation_request, |
Rakina Zata Amni | 84a859df | 2024-06-07 15:56:32 | [diff] [blame] | 1769 | IsBackForwardCacheEnabled() && |
| 1770 | rfh->delegate()->IsBackForwardCacheSupported() && |
| 1771 | back_forward_cache_.IsAllowed(navigation_request->GetURL())); |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1772 | } |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1773 | |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1774 | // Grab the corresponding FrameNavigationEntry for a few updates, but only if |
| 1775 | // the SiteInstance matches (to avoid updating the wrong entry by mistake). |
| 1776 | // A mismatch can occur if the renderer lies or due to a unique name collision |
| 1777 | // after a race with an OOPIF (see https://crbug.com/616820). |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1778 | FrameNavigationEntry* frame_entry = |
| 1779 | active_entry->GetFrameEntry(rfh->frame_tree_node()); |
Diana Qu | 21d0c92 | 2025-06-25 17:19:08 | [diff] [blame] | 1780 | if (base::FeatureList::IsEnabled(kCheckSiteInstanceOnHistoryNavigation) && |
Diana Qu | 1cc5eaf | 2025-06-21 00:04:27 | [diff] [blame] | 1781 | frame_entry && frame_entry->site_instance()) { |
Diana Qu | 2d6c85b | 2025-06-17 01:13:14 | [diff] [blame] | 1782 | int64_t dsn = navigation_request->frame_entry_document_sequence_number(); |
| 1783 | if (dsn != -1 && dsn == frame_entry->document_sequence_number()) { |
| 1784 | // We CHECK that the SiteInstance matches the one stored in the session |
| 1785 | // history's FrameNavigationEntry, if the document sequence number (DSN) |
| 1786 | // also matches. This ensures the navigation is committing in the expected |
| 1787 | // SiteInstance. |
| 1788 | // |
| 1789 | // It's okay for the SiteInstance to differ if a cross-document redirect |
| 1790 | // occurred — in that case, the DSN in NavigationRequest should be cleared |
| 1791 | // (set to -1), and we skip the CHECK. |
| 1792 | CHECK(rfh->GetSiteInstance() == frame_entry->site_instance(), |
| 1793 | base::NotFatalUntil::M141) |
| 1794 | << "Session history navigation committed in a different SiteInstance " |
| 1795 | "than intended. " |
| 1796 | << "FrameNavigationEntry SiteInstance: " |
| 1797 | << frame_entry->site_instance() |
| 1798 | << ", Committed RFH SiteInstance: " << rfh->GetSiteInstance() |
| 1799 | << ", URL: " << params.url; |
| 1800 | } |
| 1801 | } |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1802 | if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance()) |
| 1803 | frame_entry = nullptr; |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1804 | // Make sure we've updated the PageState in one of the helper methods. |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1805 | // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed. |
| 1806 | if (frame_entry) { |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1807 | DCHECK(params.page_state == frame_entry->page_state()); |
Nasko Oskov | bbcfc000 | 2019-11-20 20:03:20 | [diff] [blame] | 1808 | |
| 1809 | // Remember the bindings the renderer process has at this point, so that |
| 1810 | // we do not grant this entry additional bindings if we come back to it. |
| 1811 | frame_entry->SetBindings(rfh->GetEnabledBindings()); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 1812 | } |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1813 | |
[email protected] | 97d8f0d | 2013-10-29 16:49:21 | [diff] [blame] | 1814 | // Once it is committed, we no longer need to track several pieces of state on |
| 1815 | // the entry. |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1816 | active_entry->ResetForCommit(frame_entry); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1817 | |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1818 | // The active entry's SiteInstance should match our SiteInstance. |
[email protected] | a1b9926 | 2013-12-27 21:56:22 | [diff] [blame] | 1819 | // TODO(creis): This check won't pass for subframes until we create entries |
| 1820 | // for subframe navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1821 | if (!rfh->GetParent()) |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1822 | CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance()); |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1823 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1824 | // Now prep the rest of the details for the notification and broadcast. |
[email protected] | 0f38dc455 | 2011-02-25 11:24:00 | [diff] [blame] | 1825 | details->entry = active_entry; |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1826 | details->is_main_frame = !rfh->GetParent(); |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame] | 1827 | details->http_status_code = params.http_status_code; |
estark | a5635c4 | 2015-07-14 00:06:53 | [diff] [blame] | 1828 | |
arthursonzogni | 7ddc654 | 2021-04-09 09:16:50 | [diff] [blame] | 1829 | active_entry->SetIsOverridingUserAgent( |
| 1830 | navigation_request->is_overriding_user_agent()); |
Scott Violet | c36f746 | 2020-05-06 23:13:03 | [diff] [blame] | 1831 | |
[email protected] | 93f230e0 | 2011-06-01 14:40:00 | [diff] [blame] | 1832 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1833 | |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1834 | if (overriding_user_agent_changed) |
| 1835 | delegate_->UpdateOverridingUserAgent(); |
| 1836 | |
creis | 03b4800 | 2015-11-04 00:54:56 | [diff] [blame] | 1837 | // Update the nav_entry_id for each RenderFrameHost in the tree, so that each |
| 1838 | // one knows the latest NavigationEntry it is showing (whether it has |
| 1839 | // committed anything in this navigation or not). This allows things like |
| 1840 | // state and title updates from RenderFrames to apply to the latest relevant |
| 1841 | // NavigationEntry. |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1842 | int nav_entry_id = active_entry->GetUniqueID(); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1843 | for (FrameTreeNode* node : frame_tree_->Nodes()) |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1844 | node->current_frame_host()->set_nav_entry_id(nav_entry_id); |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1845 | |
| 1846 | if (navigation_request->IsPrerenderedPageActivation()) { |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 1847 | BroadcastHistoryIndexAndLength(); |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 1848 | // TODO(crbug.com/40187392): Broadcasting happens after the prerendered page |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1849 | // is activated. As a result, a "prerenderingchange" event listener sees the |
| 1850 | // history.length which is not updated yet. We should guarantee that |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 1851 | // history's length and index should be updated before a |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1852 | // "prerenderingchange" event listener runs. One possible approach is to use |
| 1853 | // the same IPC which "prerenderingchange" uses, and propagate history's |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 1854 | // length and index together with that. |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1855 | } |
| 1856 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1857 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1858 | } |
| 1859 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1860 | NavigationType NavigationControllerImpl::ClassifyNavigation( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1861 | RenderFrameHostImpl* rfh, |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1862 | const mojom::DidCommitProvisionalLoadParams& params, |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1863 | NavigationRequest* navigation_request) { |
Piotr Tworek | bad5128 | 2020-09-30 19:17:59 | [diff] [blame] | 1864 | TraceReturnReason<tracing_category::kNavigation> trace_return( |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1865 | "ClassifyNavigation"); |
| 1866 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1867 | if (params.did_create_new_entry) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1868 | // A new entry. We may or may not have a corresponding pending entry, and |
| 1869 | // this may or may not be the main frame. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1870 | if (!rfh->GetParent()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1871 | trace_return.set_return_reason("new entry, no parent, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1872 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1873 | } |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1874 | // Valid subframe navigation. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1875 | trace_return.set_return_reason("new entry, new subframe"); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1876 | return NAVIGATION_TYPE_NEW_SUBFRAME; |
| 1877 | } |
| 1878 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1879 | // We only clear the session history in tests when navigating to a new entry. |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1880 | DCHECK(!params.history_list_was_cleared); |
| 1881 | |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1882 | if (rfh->GetParent()) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1883 | // All manual subframes would be did_create_new_entry and handled above, so |
| 1884 | // we know this is auto. |
Rakina Zata Amni | acd4df66 | 2022-11-15 06:49:08 | [diff] [blame] | 1885 | trace_return.set_return_reason("subframe, last commmited, auto subframe"); |
| 1886 | return NAVIGATION_TYPE_AUTO_SUBFRAME; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1887 | } |
| 1888 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1889 | const int nav_entry_id = navigation_request->commit_params().nav_entry_id; |
| 1890 | if (nav_entry_id == 0) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1891 | // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't |
| 1892 | // create a new page. |
| 1893 | |
Hayato Ito | 2ae4944 | 2021-07-02 02:59:25 | [diff] [blame] | 1894 | // This main frame navigation is not a history navigation (since |
| 1895 | // nav_entry_id is 0), but didn't create a new entry. So this must be a |
| 1896 | // reload or a replacement navigation, which will modify the existing entry. |
| 1897 | // |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1898 | // TODO(nasko): With error page isolation, reloading an existing session |
| 1899 | // history entry can result in change of SiteInstance. Check for such a case |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1900 | // here and classify it as NEW_ENTRY, as such navigations should be treated |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1901 | // as new with replacement. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1902 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1903 | "nav entry 0, last committed, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1904 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1905 | } |
| 1906 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1907 | if (PendingEntryMatchesRequest(navigation_request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1908 | // If the SiteInstance of the |pending_entry_| does not match the |
| 1909 | // SiteInstance that got committed, treat this as a new navigation with |
| 1910 | // replacement. This can happen if back/forward/reload encounters a server |
| 1911 | // redirect to a different site or an isolated error page gets successfully |
| 1912 | // reloaded into a different SiteInstance. |
| 1913 | if (pending_entry_->site_instance() && |
| 1914 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1915 | trace_return.set_return_reason("pending matching nav entry, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1916 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1917 | } |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1918 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1919 | if (pending_entry_index_ == -1) { |
| 1920 | // In this case, we have a pending entry for a load of a new URL but Blink |
| 1921 | // didn't do a new navigation (params.did_create_new_entry). First check |
| 1922 | // to make sure Blink didn't treat a new cross-process navigation as |
| 1923 | // inert, and thus set params.did_create_new_entry to false. In that case, |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1924 | // we must treat it as NEW rather than the converted reload case below, |
| 1925 | // since the new SiteInstance doesn't match the last committed entry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1926 | if (GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1927 | trace_return.set_return_reason("new pending, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1928 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | // Otherwise, this happens when you press enter in the URL bar to reload. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1932 | // We will create a pending entry, but NavigateWithoutEntry will convert |
| 1933 | // it to a reload since it's the same page and not create a new entry for |
| 1934 | // it. (The user doesn't want to have a new back/forward entry when they |
| 1935 | // do this.) Therefore we want to just ignore the pending entry and go |
| 1936 | // back to where we were (the "existing entry"). |
| 1937 | trace_return.set_return_reason("new pending, existing (same) entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1938 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1939 | } |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1940 | } |
| 1941 | |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 1942 | if (navigation_request->commit_params().intended_as_new_entry) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1943 | // This was intended to be a navigation to a new entry but the pending entry |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1944 | // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may |
| 1945 | // or may not have a pending entry. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1946 | trace_return.set_return_reason("intended as new entry, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1947 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1948 | } |
| 1949 | |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 1950 | if (navigation_request->DidEncounterError() && |
| 1951 | failed_pending_entry_id_ != 0 && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1952 | nav_entry_id == failed_pending_entry_id_) { |
Charlie Reis | b5f39cf | 2024-11-12 16:55:01 | [diff] [blame] | 1953 | // If the renderer was going to a pending entry that got cleared because of |
| 1954 | // an error, then the error page will replace the existing entry. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1955 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1956 | "unreachable, matching pending, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1957 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1958 | } |
| 1959 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1960 | // Now we know that the notification is for an existing entry; find it. |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1961 | int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1962 | trace_return.traced_value()->SetInteger("existing_entry_index", |
| 1963 | existing_entry_index); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1964 | if (existing_entry_index == -1) { |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 1965 | // The renderer has committed a navigation to an entry that no longer |
| 1966 | // exists. Because the renderer is showing that page, resurrect that entry. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1967 | trace_return.set_return_reason("existing entry -1, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1968 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1969 | } |
| 1970 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1971 | // Since we weeded out "new" navigations above, we know this is an existing |
| 1972 | // (back/forward) navigation. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1973 | trace_return.set_return_reason("default return, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1974 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1975 | } |
| 1976 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1977 | void NavigationControllerImpl::UpdateNavigationEntryDetails( |
| 1978 | NavigationEntryImpl* entry, |
| 1979 | RenderFrameHostImpl* rfh, |
| 1980 | const mojom::DidCommitProvisionalLoadParams& params, |
| 1981 | NavigationRequest* request, |
| 1982 | NavigationEntryImpl::UpdatePolicy update_policy, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1983 | bool is_new_entry, |
| 1984 | LoadCommittedDetails* commit_details) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1985 | // Update the FrameNavigationEntry. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1986 | std::vector<GURL> redirects; |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1987 | entry->AddOrUpdateFrameEntry( |
| 1988 | rfh->frame_tree_node(), update_policy, params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1989 | params.document_sequence_number, params.navigation_api_key, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1990 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1991 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1992 | Referrer(*params.referrer), |
| 1993 | request ? request->common_params().initiator_origin : params.origin, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1994 | request ? request->common_params().initiator_base_url : std::nullopt, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1995 | request ? request->GetRedirectChain() : redirects, params.page_state, |
| 1996 | params.method, params.post_id, nullptr /* blob_url_loader_factory */, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1997 | ComputePolicyContainerPoliciesForFrameEntry( |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1998 | rfh, request && request->IsSameDocument(), |
Antonio Sartori | b8addf6 | 2024-09-16 07:59:21 | [diff] [blame] | 1999 | request ? request->DidEncounterError() : false, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2000 | request ? request->common_params().url : params.url)); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2001 | |
| 2002 | if (rfh->GetParent()) { |
| 2003 | // Only modify the NavigationEntry for main frame navigations. |
| 2004 | return; |
| 2005 | } |
| 2006 | if (entry->update_virtual_url_with_url()) |
| 2007 | UpdateVirtualURLToURL(entry, params.url); |
| 2008 | // Don't use the page type from the pending entry. Some interstitial page |
| 2009 | // may have set the type to interstitial. Once we commit, however, the page |
| 2010 | // type must always be normal or error. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2011 | entry->set_page_type((request && request->DidEncounterError()) |
| 2012 | ? PAGE_TYPE_ERROR |
| 2013 | : PAGE_TYPE_NORMAL); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 2014 | if (commit_details && commit_details->should_stay_as_initial_entry) { |
| 2015 | // Retain the "initial NavigationEntry" status. |
| 2016 | if (request->IsSameDocument()) { |
| 2017 | // If this is for a same-document navigation, the NavigationEntry must be |
| 2018 | // reused and should already be marked as the initial NavigationEntry. |
| 2019 | DCHECK(entry->IsInitialEntry()); |
| 2020 | } else { |
| 2021 | // If this is for a cross-document navigation, it can be caused by a |
| 2022 | // renderer-initiated reload, or the synchronous about:blank commit. Mark |
| 2023 | // "for synchronous about:blank" in the latter case, and also when it is |
| 2024 | // reloading a "for synchronous about:blank" entry. Otherwise, the entry |
| 2025 | // is not for a synchronous about:blank commit. |
| 2026 | NavigationEntryImpl::InitialNavigationEntryState new_state = |
| 2027 | NavigationEntryImpl::InitialNavigationEntryState:: |
| 2028 | kInitialNotForSynchronousAboutBlank; |
| 2029 | if (entry->IsInitialEntryForSynchronousAboutBlank() || |
| 2030 | request->is_synchronous_renderer_commit()) { |
| 2031 | new_state = NavigationEntryImpl::InitialNavigationEntryState:: |
| 2032 | kInitialForSynchronousAboutBlank; |
| 2033 | } |
| 2034 | entry->set_initial_navigation_entry_state(new_state); |
| 2035 | } |
| 2036 | } else if (commit_details && !commit_details->should_stay_as_initial_entry) { |
| 2037 | // Remove the "initial NavigationEntry" status. |
| 2038 | entry->set_initial_navigation_entry_state( |
| 2039 | NavigationEntryImpl::InitialNavigationEntryState::kNonInitial); |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2040 | } |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2041 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2042 | if (is_new_entry) { |
| 2043 | // Some properties of the NavigationEntry are only set when the entry is |
| 2044 | // new (we aren't reusing it). |
| 2045 | entry->SetTransitionType(params.transition); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2046 | entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL() |
Peter Kasting | 8104ba8 | 2024-01-31 15:23:40 | [diff] [blame] | 2047 | : GURL()); |
Adithya Srinivasan | 72b0735 | 2023-12-21 15:56:01 | [diff] [blame] | 2048 | DCHECK_EQ(rfh->GetPage().is_overriding_user_agent(), |
| 2049 | params.is_overriding_user_agent); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2050 | entry->SetIsOverridingUserAgent(params.is_overriding_user_agent); |
| 2051 | } else { |
| 2052 | // We are reusing the NavigationEntry. The site instance will normally be |
| 2053 | // the same except for a few cases: |
| 2054 | // 1) session restore, when no site instance will be assigned or |
| 2055 | // 2) redirect, when the site instance is reset. |
| 2056 | DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() || |
| 2057 | entry->site_instance() == rfh->GetSiteInstance()); |
| 2058 | } |
| 2059 | } |
| 2060 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2061 | void NavigationControllerImpl::CreateInitialEntry() { |
| 2062 | DCHECK_EQ(entries_.size(), 0u); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2063 | RenderFrameHostImpl* rfh = frame_tree_->root()->current_frame_host(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2064 | auto params = mojom::DidCommitProvisionalLoadParams::New(); |
| 2065 | // The initial NavigationEntry's URL is the empty URL. This preserves the old |
| 2066 | // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from |
| 2067 | // before we have initial NavigationEntries. |
Peter Kasting | 8104ba8 | 2024-01-31 15:23:40 | [diff] [blame] | 2068 | params->url = GURL(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2069 | params->http_status_code = 0; |
| 2070 | params->url_is_unreachable = false; |
| 2071 | params->method = "GET"; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2072 | params->post_id = -1; |
| 2073 | params->embedding_token = base::UnguessableToken::Create(); |
| 2074 | params->navigation_token = base::UnguessableToken::Create(); |
| 2075 | params->did_create_new_entry = true; |
| 2076 | params->origin = rfh->GetLastCommittedOrigin(); |
| 2077 | params->should_update_history = true; |
| 2078 | params->item_sequence_number = 0; |
| 2079 | params->document_sequence_number = 0; |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2080 | bool is_in_fenced_frame_tree = rfh->IsNestedWithinFencedFrame(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2081 | params->transition = is_in_fenced_frame_tree |
| 2082 | ? ui::PAGE_TRANSITION_AUTO_SUBFRAME |
| 2083 | : ui::PAGE_TRANSITION_LINK; |
| 2084 | params->referrer = blink::mojom::Referrer::New(); |
| 2085 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2086 | auto new_entry = std::make_unique<NavigationEntryImpl>( |
| 2087 | rfh->GetSiteInstance(), params->url, Referrer(*params->referrer), |
W. James MacLean | 78e2f87 | 2023-01-24 17:59:38 | [diff] [blame] | 2088 | rfh->GetLastCommittedOrigin(), rfh->GetInheritedBaseUrl(), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2089 | std::u16string() /* title */, ui::PAGE_TRANSITION_TYPED, |
| 2090 | false /* renderer_initiated */, nullptr /* blob_url_loader_factory */, |
| 2091 | true /* is_initial_entry */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2092 | UpdateNavigationEntryDetails( |
| 2093 | new_entry.get(), rfh, *params, nullptr /* request */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2094 | NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */, |
| 2095 | nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2096 | |
| 2097 | InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */, |
| 2098 | false /* was_post_commit_error */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2099 | is_in_fenced_frame_tree, nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2100 | } |
| 2101 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2102 | void NavigationControllerImpl::RendererDidNavigateToNewEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2103 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2104 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2105 | bool is_same_document, |
clamy | 3bf35e3c | 2016-11-10 15:59:44 | [diff] [blame] | 2106 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2107 | bool previous_document_had_history_intervention_activation, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2108 | NavigationRequest* request, |
| 2109 | LoadCommittedDetails* commit_details) { |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 2110 | std::unique_ptr<NavigationEntryImpl> new_entry; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2111 | const std::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2112 | request->common_params().initiator_origin; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2113 | std::optional<GURL> initiator_base_url; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 2114 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 2115 | initiator_base_url = request->common_params().initiator_base_url; |
| 2116 | } |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 2117 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2118 | // First check if this is an in-page navigation. If so, clone the current |
| 2119 | // entry instead of looking at the pending entry, because the pending entry |
| 2120 | // does not have any subframe history items. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2121 | if (is_same_document) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2122 | FrameNavigationEntry* previous_frame_entry = |
| 2123 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2124 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2125 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2126 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2127 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2128 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2129 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
Rakina Zata Amni | 82fafba | 2021-03-11 07:07:09 | [diff] [blame] | 2130 | request->GetRedirectChain(), params.page_state, params.method, |
| 2131 | params.post_id, nullptr /* blob_url_loader_factory */, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2132 | // We will set the document policies later in this function. |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2133 | nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2134 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2135 | // FrameNavigationEntry. |
| 2136 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2137 | previous_frame_entry->protect_url_in_navigation_api()); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2138 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2139 | new_entry = GetLastCommittedEntry()->CloneAndReplace( |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2140 | frame_entry, true, request->frame_tree_node(), frame_tree_->root()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2141 | if (new_entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2142 | params.url.DeprecatedGetOriginAsURL()) { |
jam | a78746e | 2017-02-22 17:21:57 | [diff] [blame] | 2143 | // TODO(jam): we had one report of this with a URL that was redirecting to |
| 2144 | // only tildes. Until we understand that better, don't copy the cert in |
| 2145 | // this case. |
| 2146 | new_entry->GetSSL() = SSLStatus(); |
| 2147 | } |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2148 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2149 | // It is expected that |frame_entry| is now owned by |new_entry|. This means |
| 2150 | // that |frame_entry| should now have a reference count of exactly 2: one |
| 2151 | // due to the local variable |frame_entry|, and another due to |new_entry| |
| 2152 | // also retaining one. This should never fail, because it's the main frame. |
| 2153 | CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef()); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2154 | } |
| 2155 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2156 | // If this is an activation navigation from a prerendered page, transfer the |
| 2157 | // new entry from an entry already created and stored in the |
| 2158 | // NavigationRequest. |new_entry| will not have been set prior to this as |
| 2159 | // |is_same_document| is mutually exclusive with |
| 2160 | // |IsPrerenderedPageActivation|. |
| 2161 | if (request->IsPrerenderedPageActivation()) { |
| 2162 | DCHECK(!is_same_document); |
| 2163 | DCHECK(!new_entry); |
| 2164 | new_entry = request->TakePrerenderNavigationEntry(); |
| 2165 | DCHECK(new_entry); |
| 2166 | } |
| 2167 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2168 | // Only make a copy of the pending entry if it is appropriate for the new |
| 2169 | // document that just loaded. Verify this by checking if the entry corresponds |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2170 | // to the given NavigationRequest. Additionally, coarsely check that: |
csharrison | 9a9142bc4 | 2016-03-01 17:24:04 | [diff] [blame] | 2171 | // 1. The SiteInstance hasn't been assigned to something else. |
| 2172 | // 2. The pending entry was intended as a new entry, rather than being a |
| 2173 | // history navigation that was interrupted by an unrelated, |
| 2174 | // renderer-initiated navigation. |
| 2175 | // TODO(csharrison): Investigate whether we can remove some of the coarser |
| 2176 | // checks. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2177 | if (!new_entry && PendingEntryMatchesRequest(request) && |
| 2178 | pending_entry_index_ == -1 && |
[email protected] | 6dd86ab | 2013-02-27 00:30:34 | [diff] [blame] | 2179 | (!pending_entry_->site_instance() || |
[email protected] | 27dd82fd | 2014-03-03 22:11:43 | [diff] [blame] | 2180 | pending_entry_->site_instance() == rfh->GetSiteInstance())) { |
creis | ef4a0cb | 2015-03-12 19:14:35 | [diff] [blame] | 2181 | new_entry = pending_entry_->Clone(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2182 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2183 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2184 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2185 | } |
| 2186 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2187 | // For cross-document commits with no matching pending entry, create a new |
| 2188 | // entry. |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2189 | if (!new_entry) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 2190 | new_entry = std::make_unique<NavigationEntryImpl>( |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2191 | rfh->GetSiteInstance(), params.url, Referrer(*params.referrer), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2192 | initiator_origin, initiator_base_url, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2193 | std::u16string(), // title |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2194 | params.transition, request->IsRendererInitiated(), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2195 | nullptr, // blob_url_loader_factory |
| 2196 | false); // is_initial_entry |
[email protected] | f8f93eb | 2012-09-25 03:06:24 | [diff] [blame] | 2197 | |
| 2198 | // Find out whether the new entry needs to update its virtual URL on URL |
| 2199 | // change and set up the entry accordingly. This is needed to correctly |
| 2200 | // update the virtual URL when replaceState is called after a pushState. |
| 2201 | GURL url = params.url; |
| 2202 | bool needs_update = false; |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2203 | // When navigating to a new entry, give the browser URL handler a chance to |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 2204 | // update the virtual URL based on the new URL. For example, this is needed |
| 2205 | // to show chrome://bookmarks/#1 when the bookmarks webui extension changes |
| 2206 | // the URL. |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2207 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 2208 | &url, browser_context_, &needs_update); |
| 2209 | new_entry->set_update_virtual_url_with_url(needs_update); |
| 2210 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2211 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2212 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2213 | } |
| 2214 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 2215 | // TODO(crbug.com/40169536) - determine which parts of the entry need to be |
| 2216 | // set for prerendered contents, if any. This is because |
| 2217 | // prerendering/activation technically won't be creating a new document. |
| 2218 | // Unlike BFCache, prerendering creates a new NavigationEntry rather than |
| 2219 | // using an existing one. |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2220 | if (!request->IsPrerenderedPageActivation()) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2221 | UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request, |
| 2222 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2223 | true /* is_new_entry */, commit_details); |
creis | 8b5cd4c | 2015-06-19 00:11:08 | [diff] [blame] | 2224 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2225 | // history.pushState() is classified as a navigation to a new page, but sets |
| 2226 | // is_same_document to true. In this case, we already have the title and |
| 2227 | // favicon available, so set them immediately. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2228 | if (is_same_document) { |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2229 | new_entry->SetTitle(GetLastCommittedEntry()->GetTitle()); |
| 2230 | new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon(); |
| 2231 | } |
[email protected] | 3a868f21 | 2014-08-09 10:41:19 | [diff] [blame] | 2232 | } |
[email protected] | ff64b3e | 2014-05-31 04:07:33 | [diff] [blame] | 2233 | |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2234 | DCHECK(!params.history_list_was_cleared || !replace_entry); |
| 2235 | // The browser requested to clear the session history when it initiated the |
| 2236 | // navigation. Now we know that the renderer has updated its state accordingly |
| 2237 | // and it is safe to also clear the browser side history. |
| 2238 | if (params.history_list_was_cleared) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2239 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2240 | entries_.clear(); |
| 2241 | last_committed_entry_index_ = -1; |
| 2242 | } |
| 2243 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2244 | // If this is a new navigation with replacement and there is a |
| 2245 | // pending_entry_ which matches the navigation reported by the renderer |
| 2246 | // process, then it should be the one replaced, so update the |
| 2247 | // last_committed_entry_index_ to use it. |
| 2248 | if (replace_entry && pending_entry_index_ != -1 && |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2249 | PendingEntryMatchesRequest(request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2250 | last_committed_entry_index_ = pending_entry_index_; |
| 2251 | } |
| 2252 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2253 | SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2254 | replace_entry, previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2255 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 2256 | |
Yuzu Saijo | a725585f | 2022-11-28 04:14:03 | [diff] [blame] | 2257 | // If this is a history navigation and the old entry has an existing |
| 2258 | // back/forward cache metrics object, keep using the old one so that the |
| 2259 | // reasons logged from the last time the page navigated gets preserved. |
| 2260 | if (BackForwardCacheMetrics::IsCrossDocumentMainFrameHistoryNavigation( |
| 2261 | request)) { |
| 2262 | // Use |request->GetNavigationEntry()| instead of |pending_entry_| here |
| 2263 | // because some tests do not have a pending entry. |
| 2264 | NavigationEntryImpl* entry = |
| 2265 | static_cast<NavigationEntryImpl*>(request->GetNavigationEntry()); |
| 2266 | if (entry && entry->back_forward_cache_metrics()) { |
| 2267 | scoped_refptr<BackForwardCacheMetrics> metrics = |
| 2268 | entry->TakeBackForwardCacheMetrics(); |
| 2269 | new_entry->set_back_forward_cache_metrics(std::move(metrics)); |
| 2270 | } |
| 2271 | } |
| 2272 | |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 2273 | bool was_post_commit_error = |
| 2274 | request->browser_initiated_error_navigation_type() == |
| 2275 | NavigationRequest::BrowserInitiatedErrorNavigationType::kPostCommit; |
| 2276 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2277 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 2278 | was_post_commit_error, rfh->IsNestedWithinFencedFrame(), |
| 2279 | commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2280 | } |
| 2281 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2282 | void NavigationControllerImpl::RendererDidNavigateToExistingEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2283 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2284 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2285 | bool is_same_document, |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2286 | bool was_restored, |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2287 | NavigationRequest* request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2288 | bool keep_pending_entry, |
| 2289 | LoadCommittedDetails* commit_details) { |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 2290 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2291 | << "that a last committed entry exists."; |
| 2292 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2293 | // We should only get here for main frame navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 2294 | DCHECK(!rfh->GetParent()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2295 | |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2296 | NavigationEntryImpl* entry = nullptr; |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 2297 | if (request->commit_params().intended_as_new_entry) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2298 | // We're guaranteed to have a last committed entry if intended_as_new_entry |
| 2299 | // is true. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2300 | entry = GetLastCommittedEntry(); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2301 | |
| 2302 | // If the NavigationRequest matches a new pending entry and is classified as |
| 2303 | // EXISTING_ENTRY, then it is a navigation to the same URL that was |
| 2304 | // converted to a reload, such as a user pressing enter in the omnibox. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2305 | if (pending_entry_index_ == -1 && PendingEntryMatchesRequest(request)) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2306 | // Note: The pending entry will usually have a real ReloadType here, but |
| 2307 | // it can still be ReloadType::NONE in cases that |
| 2308 | // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source). |
| 2309 | |
| 2310 | // If we classified this correctly, the SiteInstance should not have |
| 2311 | // changed. |
| 2312 | CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance()); |
| 2313 | |
| 2314 | // For converted reloads, we assign the entry's unique ID to be that of |
| 2315 | // the new one. Since this is always the result of a user action, we want |
| 2316 | // to dismiss infobars, etc. like a regular user-initiated navigation. |
| 2317 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
| 2318 | |
| 2319 | // The extra headers may have changed due to reloading with different |
| 2320 | // headers. |
| 2321 | entry->set_extra_headers(pending_entry_->extra_headers()); |
| 2322 | } |
| 2323 | // Otherwise, this was intended as a new entry but the pending entry was |
| 2324 | // lost in the meantime and no new entry was created. We are stuck at the |
| 2325 | // last committed entry. |
| 2326 | |
| 2327 | // Even if this is a converted reload from pressing enter in the omnibox, |
| 2328 | // the server could redirect, requiring an update to the SSL status. If this |
| 2329 | // is a same document navigation, though, there's no SSLStatus in the |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2330 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2331 | if (!is_same_document) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2332 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2333 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2334 | } |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2335 | } else if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2336 | // This is a browser-initiated navigation (back/forward/reload). |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2337 | entry = GetEntryWithUniqueID(nav_entry_id); |
jam | d208b90 | 2016-09-01 16:58:16 | [diff] [blame] | 2338 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2339 | if (is_same_document) { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2340 | // There's no SSLStatus in the NavigationRequest for same document |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2341 | // navigations, so normally we leave |entry|'s SSLStatus as is. However if |
| 2342 | // this was a restored same document navigation entry, then it won't have |
| 2343 | // an SSLStatus. So we need to copy over the SSLStatus from the entry that |
| 2344 | // navigated it. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2345 | NavigationEntryImpl* last_entry = GetLastCommittedEntry(); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2346 | if (entry->GetURL().DeprecatedGetOriginAsURL() == |
| 2347 | last_entry->GetURL().DeprecatedGetOriginAsURL() && |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2348 | last_entry->GetSSL().initialized && !entry->GetSSL().initialized && |
| 2349 | was_restored) { |
| 2350 | entry->GetSSL() = last_entry->GetSSL(); |
| 2351 | } |
| 2352 | } else { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2353 | // In rapid back/forward navigations |request| sometimes won't have a cert |
| 2354 | // (http://crbug.com/727892). So we use the request's cert if it exists, |
John Abd-El-Malek | 3f24708 | 2017-12-07 19:02:19 | [diff] [blame] | 2355 | // otherwise we only reuse the existing cert if the origins match. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2356 | if (request->GetSSLInfo().has_value() && |
| 2357 | request->GetSSLInfo()->is_valid()) { |
| 2358 | entry->GetSSL() = SSLStatus(*(request->GetSSLInfo())); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2359 | } else if (entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2360 | request->GetURL().DeprecatedGetOriginAsURL()) { |
John Abd-El-Malek | 3f24708 | 2017-12-07 19:02:19 | [diff] [blame] | 2361 | entry->GetSSL() = SSLStatus(); |
| 2362 | } |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2363 | } |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2364 | } else { |
Feifei Wang | 2ab8ba6c | 2022-04-13 22:19:27 | [diff] [blame] | 2365 | // This is renderer-initiated. The only kinds of renderer-initiated |
Rakina Zata Amni | 557afb9 | 2021-07-17 04:39:57 | [diff] [blame] | 2366 | // navigations that are EXISTING_ENTRY are same-document navigations that |
| 2367 | // result in replacement (e.g. history.replaceState(), location.replace(), |
| 2368 | // forced replacements for trivial session history contexts). For these |
| 2369 | // cases, we reuse the last committed entry. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2370 | entry = GetLastCommittedEntry(); |
jam | 0576b13 | 2016-09-07 05:13:10 | [diff] [blame] | 2371 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 2372 | // TODO(crbug.com/40532777): Set page transition type to |
| 2373 | // PAGE_TRANSITION_LINK to avoid misleading interpretations (e.g. URLs |
| 2374 | // paired with PAGE_TRANSITION_TYPED that haven't actually been typed) as |
| 2375 | // well as to fix the inconsistency with what we report to observers |
| 2376 | // (PAGE_TRANSITION_LINK | PAGE_TRANSITION_CLIENT_REDIRECT). |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2377 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2378 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2379 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2380 | // If this is a same document navigation, then there's no SSLStatus in the |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2381 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2382 | if (!is_same_document) |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2383 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2384 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2385 | } |
| 2386 | DCHECK(entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2387 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2388 | UpdateNavigationEntryDetails(entry, rfh, params, request, |
| 2389 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2390 | false /* is_new_entry */, commit_details); |
creis | 22a7b4c | 2016-04-28 07:20:30 | [diff] [blame] | 2391 | |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2392 | // The redirected to page should not inherit the favicon from the previous |
| 2393 | // page. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2394 | if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document) |
[email protected] | 91a4ff8 | 2012-10-29 20:29:48 | [diff] [blame] | 2395 | entry->GetFavicon() = FaviconStatus(); |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2396 | |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2397 | // Update the last committed index to reflect the committed entry. Do this |
| 2398 | // before calling DiscardNonCommittedEntriesWithCommitDetails, so that the |
| 2399 | // delegate sees the correct committed index when notified of navigation |
| 2400 | // state changes. (Otherwise CanGoBack may incorrectly return true, as in |
| 2401 | // https://crbug.com/1439948.) |
Charlie Reis | fbe5f102 | 2023-10-03 15:21:21 | [diff] [blame] | 2402 | last_committed_entry_index_ = GetIndexOfEntry(entry); |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2403 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2404 | // We should also usually discard the pending entry if it corresponds to a |
| 2405 | // different navigation, since that one is now likely canceled. In rare |
| 2406 | // cases, we leave the pending entry for another navigation in place when we |
| 2407 | // know it is still ongoing, to avoid a flicker in the omnibox (see |
| 2408 | // https://crbug.com/900036). |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2409 | // |
| 2410 | // Note that we need to use the "internal" version since we don't want to |
| 2411 | // actually change any other state, just kill the pointer. |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2412 | if (!keep_pending_entry) |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2413 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2414 | } |
| 2415 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2416 | void NavigationControllerImpl::RendererDidNavigateNewSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2417 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2418 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2419 | bool is_same_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2420 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2421 | bool previous_document_had_history_intervention_activation, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2422 | NavigationRequest* request, |
| 2423 | LoadCommittedDetails* commit_details) { |
avi | 25f5f9e | 2015-07-17 20:08:26 | [diff] [blame] | 2424 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2425 | ui::PAGE_TRANSITION_MANUAL_SUBFRAME)); |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2426 | // The NEW_SUBFRAME path should never result in an initial NavigationEntry. |
| 2427 | DCHECK(!commit_details->should_stay_as_initial_entry); |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 2428 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2429 | // Manual subframe navigations just get the current entry cloned so the user |
| 2430 | // can go back or forward to it. The actual subframe information will be |
| 2431 | // stored in the page state for each of those entries. This happens out of |
| 2432 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 2433 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2434 | << "that a last committed entry exists."; |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2435 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2436 | // The DCHECK below documents the fact that we don't know of any situation |
| 2437 | // where |replace_entry| is true for subframe navigations. This simplifies |
| 2438 | // reasoning about the replacement struct for subframes (see |
| 2439 | // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()). |
| 2440 | DCHECK(!replace_entry); |
| 2441 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2442 | // This FrameNavigationEntry might not end up being used in the |
| 2443 | // CloneAndReplace() call below, if a spot can't be found for it in the tree. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2444 | const std::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2445 | request->common_params().initiator_origin; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2446 | std::optional<GURL> initiator_base_url; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 2447 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 2448 | initiator_base_url = request->common_params().initiator_base_url; |
| 2449 | } |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2450 | std::unique_ptr<PolicyContainerPolicies> policy_container_policies = |
| 2451 | ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document, |
Antonio Sartori | b8addf6 | 2024-09-16 07:59:21 | [diff] [blame] | 2452 | request->DidEncounterError(), |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2453 | request->GetURL()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2454 | bool protect_url_in_navigation_api = false; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2455 | if (is_same_document) { |
| 2456 | FrameNavigationEntry* previous_frame_entry = |
| 2457 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2458 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2459 | // FrameNavigationEntry. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2460 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2461 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2462 | previous_frame_entry->protect_url_in_navigation_api(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2463 | } else { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2464 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2465 | policy_container_policies && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2466 | ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2467 | policy_container_policies->referrer_policy); |
| 2468 | } |
| 2469 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2470 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2471 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2472 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2473 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2474 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2475 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
| 2476 | request->GetRedirectChain(), params.page_state, params.method, |
| 2477 | params.post_id, nullptr /* blob_url_loader_factory */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2478 | std::move(policy_container_policies), protect_url_in_navigation_api); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2479 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2480 | std::unique_ptr<NavigationEntryImpl> new_entry = |
| 2481 | GetLastCommittedEntry()->CloneAndReplace( |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2482 | std::move(frame_entry), is_same_document, rfh->frame_tree_node(), |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2483 | frame_tree_->root()); |
creis | e062d54 | 2015-08-25 02:01:55 | [diff] [blame] | 2484 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2485 | SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2486 | replace_entry, previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2487 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2488 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2489 | // TODO(creis): Update this to add the frame_entry if we can't find the one |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2490 | // to replace, which can happen due to a unique name change. See |
| 2491 | // https://crbug.com/607205. For now, the call to CloneAndReplace() will |
| 2492 | // delete the |frame_entry| when the function exits if it doesn't get used. |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2493 | |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2494 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, false, |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2495 | rfh->IsNestedWithinFencedFrame(), commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2496 | } |
| 2497 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2498 | bool NavigationControllerImpl::RendererDidNavigateAutoSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2499 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2500 | const mojom::DidCommitProvisionalLoadParams& params, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2501 | bool is_same_document, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2502 | bool was_on_initial_empty_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2503 | NavigationRequest* request, |
| 2504 | LoadCommittedDetails* commit_details) { |
avi | 9f07a0c | 2015-02-18 22:51:29 | [diff] [blame] | 2505 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2506 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
| 2507 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2508 | // We're guaranteed to have a previously committed entry, and we now need to |
| 2509 | // handle navigation inside of a subframe in it without creating a new entry. |
| 2510 | DCHECK(GetLastCommittedEntry()); |
| 2511 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2512 | // For newly created subframes, we don't need to send a commit notification. |
| 2513 | // This is only necessary for history navigations in subframes. |
| 2514 | bool send_commit_notification = false; |
| 2515 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2516 | // If |nav_entry_id| is non-zero and matches an existing entry, this |
| 2517 | // is a history navigation. Update the last committed index accordingly. If |
| 2518 | // we don't recognize the |nav_entry_id|, it might be a recently |
| 2519 | // pruned entry. We'll handle it below. |
| 2520 | if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
| 2521 | int entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2522 | if (entry_index != -1 && entry_index != last_committed_entry_index_) { |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2523 | // Make sure that a subframe commit isn't changing the main frame's |
| 2524 | // origin. Otherwise the renderer process may be confused, leading to a |
| 2525 | // URL spoof. We can't check the path since that may change |
| 2526 | // (https://crbug.com/373041). |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2527 | // TODO(creis): For now, restrict this check to HTTP(S) origins, because |
| 2528 | // about:blank, file, and unique origins are more subtle to get right. |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 2529 | // We should use checks similar to RenderFrameHostImpl's |
| 2530 | // CanCommitUrlAndOrigin on the main frame during subframe commits. |
| 2531 | // See https://crbug.com/1209092. |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2532 | const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL(); |
| 2533 | const GURL& current_top_url = GetLastCommittedEntry()->GetURL(); |
| 2534 | if (current_top_url.SchemeIsHTTPOrHTTPS() && |
| 2535 | dest_top_url.SchemeIsHTTPOrHTTPS() && |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2536 | current_top_url.DeprecatedGetOriginAsURL() != |
| 2537 | dest_top_url.DeprecatedGetOriginAsURL()) { |
Chris Bookholt | 10f4b733 | 2022-02-14 18:25:44 | [diff] [blame] | 2538 | bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(), |
creis | fb6eeb6 | 2016-05-10 19:01:51 | [diff] [blame] | 2539 | bad_message::NC_AUTO_SUBFRAME); |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2540 | } |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2541 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2542 | // We only need to discard the pending entry in this history navigation |
| 2543 | // case. For newly created subframes, there was no pending entry. |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2544 | last_committed_entry_index_ = entry_index; |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2545 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2546 | |
| 2547 | // History navigations should send a commit notification. |
| 2548 | send_commit_notification = true; |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2549 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2550 | } |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 2551 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2552 | // This may be a "new auto" case where we add a new FrameNavigationEntry, or |
| 2553 | // it may be a "history auto" case where we update an existing one. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2554 | // We may want to update |last_committed|'s FrameNavigationEntry (if one |
| 2555 | // exists), or we may want to clobber it and create a new one. We update in |
| 2556 | // cases where the corresponding FrameNavigationEntry is conceptually similar |
| 2557 | // to the document described by the commit params: same-document |
| 2558 | // navigations, history traversal to an existing entry, and reloads (including |
| 2559 | // a "soft reload" where we navigate to the same url without flagging it as a |
| 2560 | // reload). But in the case of a different document that is not logically |
| 2561 | // related to the committed FrameNavigationEntry's document (cross-document, |
| 2562 | // not same url, not a reload, not a history traversal), we replace rather |
| 2563 | // than update. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2564 | // |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2565 | // In the case where we update, the FrameNavigationEntry will potentially be |
| 2566 | // shared across multiple NavigationEntries, and any updates will be reflected |
| 2567 | // in all of those NavigationEntries. In the replace case, any existing |
| 2568 | // sharing with other NavigationEntries will stop. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2569 | // |
| 2570 | // When navigating away from the initial empty document, we also update rather |
| 2571 | // than replace. Either update or replace will overwrite the initial empty |
| 2572 | // document state for |last_committed|, but if the FrameNavigationEntry for |
| 2573 | // the initial empty document is shared across multiple NavigationEntries (due |
| 2574 | // to a navigation in another frame), we want to make sure we overwrite the |
| 2575 | // initial empty document state everywhere this FrameNavigationEntry is used, |
| 2576 | // which is accompished by updating the existing FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2577 | NavigationEntryImpl* last_committed = GetLastCommittedEntry(); |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2578 | FrameNavigationEntry* last_committed_frame_entry = |
| 2579 | last_committed->GetFrameEntry(rfh->frame_tree_node()); |
| 2580 | NavigationEntryImpl::UpdatePolicy update_policy = |
| 2581 | NavigationEntryImpl::UpdatePolicy::kUpdate; |
| 2582 | if (request->common_params().navigation_type == |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 2583 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT && |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2584 | last_committed_frame_entry && |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2585 | last_committed_frame_entry->url() != params.url && |
| 2586 | !was_on_initial_empty_document) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2587 | update_policy = NavigationEntryImpl::UpdatePolicy::kReplace; |
| 2588 | } |
| 2589 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2590 | UpdateNavigationEntryDetails(last_committed, rfh, params, request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2591 | update_policy, false /* is_new_entry */, |
| 2592 | commit_details); |
creis | 625a0c7d | 2015-03-24 23:17:12 | [diff] [blame] | 2593 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2594 | return send_commit_notification; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2595 | } |
| 2596 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2597 | int NavigationControllerImpl::GetIndexOfEntry( |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 2598 | const NavigationEntryImpl* entry) const { |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 2599 | for (size_t i = 0; i < entries_.size(); ++i) { |
| 2600 | if (entries_[i].get() == entry) |
| 2601 | return i; |
| 2602 | } |
| 2603 | return -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2604 | } |
| 2605 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2606 | void NavigationControllerImpl::CopyStateFrom(NavigationController* temp, |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2607 | bool needs_reload) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2608 | NavigationControllerImpl* source = |
| 2609 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2610 | // Verify that we look new. |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 2611 | DCHECK_EQ(1, GetEntryCount()); |
| 2612 | DCHECK(GetLastCommittedEntry()->IsInitialEntry()); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 2613 | DCHECK(!GetPendingEntry()); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2614 | entries_.clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2615 | |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2616 | needs_reload_ = needs_reload; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 2617 | needs_reload_type_ = NeedsReloadType::kCopyStateFrom; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2618 | InsertEntriesFrom(source, source->GetEntryCount()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2619 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2620 | for (auto& it : source->session_storage_namespace_map_) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2621 | SessionStorageNamespaceImpl* source_namespace = |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2622 | static_cast<SessionStorageNamespaceImpl*>(it.second.get()); |
| 2623 | session_storage_namespace_map_[it.first] = source_namespace->Clone(); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2624 | } |
[email protected] | 4e6419c | 2010-01-15 04:50:34 | [diff] [blame] | 2625 | |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 2626 | FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2627 | } |
| 2628 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2629 | bool NavigationControllerImpl::CanPruneAllButLastCommitted() { |
Adithya Srinivasan | e8094c8 | 2024-06-26 22:45:02 | [diff] [blame] | 2630 | // If there is no last committed entry, we cannot prune. |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2631 | if (last_committed_entry_index_ == -1) |
| 2632 | return false; |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2633 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2634 | // We cannot prune if there is a pending entry at an existing entry index. |
| 2635 | // It may not commit, so we have to keep the last committed entry, and thus |
| 2636 | // there is no sensible place to keep the pending entry. It is ok to have |
| 2637 | // a new pending entry, which can optionally commit as a new navigation. |
| 2638 | if (pending_entry_index_ != -1) |
| 2639 | return false; |
| 2640 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2641 | return true; |
| 2642 | } |
| 2643 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2644 | void NavigationControllerImpl::PruneAllButLastCommitted() { |
| 2645 | PruneAllButLastCommittedInternal(); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2646 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2647 | DCHECK_EQ(0, last_committed_entry_index_); |
| 2648 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2649 | |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 2650 | BroadcastHistoryIndexAndLength(); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2651 | } |
| 2652 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2653 | void NavigationControllerImpl::PruneAllButLastCommittedInternal() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2654 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2655 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2656 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 2657 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
| 2658 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2659 | // Erase all entries but the last committed entry. There may still be a |
| 2660 | // new pending entry after this. |
| 2661 | entries_.erase(entries_.begin(), |
| 2662 | entries_.begin() + last_committed_entry_index_); |
| 2663 | entries_.erase(entries_.begin() + 1, entries_.end()); |
| 2664 | last_committed_entry_index_ = 0; |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2665 | } |
| 2666 | |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2667 | void NavigationControllerImpl::DeleteNavigationEntries( |
| 2668 | const DeletionPredicate& deletionPredicate) { |
| 2669 | // It is up to callers to check the invariants before calling this. |
| 2670 | CHECK(CanPruneAllButLastCommitted()); |
| 2671 | std::vector<int> delete_indices; |
| 2672 | for (size_t i = 0; i < entries_.size(); i++) { |
| 2673 | if (i != static_cast<size_t>(last_committed_entry_index_) && |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2674 | deletionPredicate.Run(entries_[i].get())) { |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2675 | delete_indices.push_back(i); |
| 2676 | } |
| 2677 | } |
| 2678 | if (delete_indices.empty()) |
| 2679 | return; |
| 2680 | |
| 2681 | if (delete_indices.size() == GetEntryCount() - 1U) { |
| 2682 | PruneAllButLastCommitted(); |
| 2683 | } else { |
| 2684 | // Do the deletion in reverse to preserve indices. |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 2685 | for (const auto& index : base::Reversed(delete_indices)) { |
| 2686 | RemoveEntryAtIndex(index); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2687 | } |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 2688 | BroadcastHistoryIndexAndLength(); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2689 | } |
| 2690 | delegate()->NotifyNavigationEntriesDeleted(); |
| 2691 | } |
| 2692 | |
Carlos Caballero | 35ce710c | 2019-09-19 10:59:45 | [diff] [blame] | 2693 | BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() { |
| 2694 | return back_forward_cache_; |
| 2695 | } |
| 2696 | |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2697 | NavigationEntryScreenshotCache* |
| 2698 | NavigationControllerImpl::GetNavigationEntryScreenshotCache() { |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 2699 | CHECK(frame_tree_->is_primary()); |
Khushal Sagar | 94ea2dc | 2024-08-06 17:15:26 | [diff] [blame] | 2700 | if (!nav_entry_screenshot_cache_ && |
| 2701 | NavigationTransitionConfig::AreBackForwardTransitionsEnabled()) { |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2702 | nav_entry_screenshot_cache_ = |
| 2703 | std::make_unique<NavigationEntryScreenshotCache>( |
| 2704 | BrowserContextImpl::From(browser_context_) |
| 2705 | ->GetNavigationEntryScreenshotManager() |
| 2706 | ->GetSafeRef(), |
| 2707 | this); |
| 2708 | } |
| 2709 | return nav_entry_screenshot_cache_.get(); |
| 2710 | } |
| 2711 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2712 | void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) { |
| 2713 | // It is not safe to call DiscardPendingEntry while NavigateToEntry is in |
| 2714 | // progress, since this will cause a use-after-free. (We only allow this |
| 2715 | // when the tab is being destroyed for shutdown, since it won't return to |
| 2716 | // NavigateToEntry in that case.) http://crbug.com/347742. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2717 | CHECK(!in_navigate_to_pending_entry_ || frame_tree_->IsBeingDestroyed()); |
Minoru Chikamune | 13ab4de | 2025-05-14 21:54:07 | [diff] [blame] | 2718 | // If `was_failure` is true, it means that the pending entry was discarded by |
| 2719 | // a `PendingEntryRefDeleted` call within `OnRequestFailedInternal`, in |
| 2720 | // response to a navigation request failure. This case is not at risk for |
| 2721 | // re-entrancy when `can_be_in_navigate_to_pending_entry_` is true, because |
| 2722 | // that code also creates another `PendingEntryRef` that would prevent the |
| 2723 | // `DiscardPendingEntry` call if the PostTask were skipped. See |
| 2724 | // https://crbug.com/411855273. |
| 2725 | if (!was_failure && can_be_in_navigate_to_pending_entry_ && |
Minoru Chikamune | 646eba4 | 2025-04-14 01:25:03 | [diff] [blame] | 2726 | !frame_tree_->IsBeingDestroyed()) { |
| 2727 | CheckPotentialNavigationReentrancy(); |
| 2728 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2729 | |
| 2730 | if (was_failure && pending_entry_) { |
| 2731 | failed_pending_entry_id_ = pending_entry_->GetUniqueID(); |
| 2732 | } else { |
| 2733 | failed_pending_entry_id_ = 0; |
| 2734 | } |
| 2735 | |
| 2736 | if (pending_entry_) { |
| 2737 | if (pending_entry_index_ == -1) |
Paul Semel | 7e51469e | 2022-07-12 12:16:33 | [diff] [blame] | 2738 | pending_entry_.ClearAndDelete(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2739 | pending_entry_index_ = -1; |
| 2740 | pending_entry_ = nullptr; |
| 2741 | } |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 2742 | |
| 2743 | // Ensure any refs to the current pending entry are ignored if they get |
| 2744 | // deleted, by clearing the set of known refs. All future pending entries will |
| 2745 | // only be affected by new refs. |
| 2746 | pending_entry_refs_.clear(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) { |
| 2750 | if (pending_entry_) |
| 2751 | pending_entry_->set_ssl_error(error); |
| 2752 | } |
| 2753 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 2754 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2755 | // static |
| 2756 | bool NavigationControllerImpl::ValidateDataURLAsString( |
| 2757 | const scoped_refptr<const base::RefCountedString>& data_url_as_string) { |
| 2758 | if (!data_url_as_string) |
| 2759 | return false; |
| 2760 | |
| 2761 | if (data_url_as_string->size() > kMaxLengthOfDataURLString) |
| 2762 | return false; |
| 2763 | |
| 2764 | // The number of characters that is enough for validating a data: URI. |
| 2765 | // From the GURL's POV, the only important part here is scheme, it doesn't |
| 2766 | // check the actual content. Thus we can take only the prefix of the url, to |
| 2767 | // avoid unneeded copying of a potentially long string. |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 2768 | constexpr size_t kDataUriPrefixMaxLen = 64; |
| 2769 | const size_t len = std::min(data_url_as_string->size(), kDataUriPrefixMaxLen); |
| 2770 | GURL data_url(base::as_string_view(*data_url_as_string).substr(0u, len)); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2771 | if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme)) |
| 2772 | return false; |
| 2773 | |
| 2774 | return true; |
| 2775 | } |
| 2776 | #endif |
| 2777 | |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2778 | void NavigationControllerImpl::NotifyUserActivation() { |
| 2779 | // When a user activation occurs, ensure that all adjacent entries for the |
| 2780 | // same document clear their skippable bit, so that the history manipulation |
| 2781 | // intervention does not apply to them. |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2782 | const bool can_go_back = CanGoBack(); |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 2783 | SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false); |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2784 | // If the value of CanGoBack changes as a result of making some entries |
| 2785 | // non-skippable, then we must let the delegate know to update its UI state. |
| 2786 | // See https://crbug.com/1477784. |
| 2787 | if (!can_go_back && CanGoBack()) { |
| 2788 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
| 2789 | } |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2790 | } |
| 2791 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2792 | bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe( |
| 2793 | RenderFrameHostImpl* render_frame_host, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2794 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client, |
| 2795 | blink::LocalFrameToken initiator_frame_token, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 2796 | int initiator_process_id, |
| 2797 | base::TimeTicks actual_navigation_start) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2798 | NavigationEntryImpl* entry = |
| 2799 | GetEntryWithUniqueID(render_frame_host->nav_entry_id()); |
| 2800 | if (!entry) |
| 2801 | return false; |
| 2802 | |
| 2803 | FrameNavigationEntry* frame_entry = |
| 2804 | entry->GetFrameEntry(render_frame_host->frame_tree_node()); |
| 2805 | if (!frame_entry) |
| 2806 | return false; |
| 2807 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2808 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2809 | render_frame_host->frame_tree_node(), entry, frame_entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2810 | ReloadType::NONE, false /* is_same_document_history_load */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2811 | true /* is_history_navigation_in_new_child */, initiator_frame_token, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 2812 | initiator_process_id, actual_navigation_start); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2813 | |
| 2814 | if (!request) |
| 2815 | return false; |
| 2816 | |
arthursonzogni | f046d4a | 2019-12-12 19:08:10 | [diff] [blame] | 2817 | request->SetNavigationClient(std::move(*navigation_client)); |
Arthur Hemery | 06173ce | 2019-05-29 12:11:41 | [diff] [blame] | 2818 | |
Rakina Zata Amni | 1c83b08 | 2023-02-08 01:09:00 | [diff] [blame] | 2819 | SCOPED_CRASH_KEY_STRING256( |
| 2820 | "Bug1400009", "req_url", |
| 2821 | request->GetURL().GetWithEmptyPath().possibly_invalid_spec()); |
| 2822 | SCOPED_CRASH_KEY_NUMBER( |
| 2823 | "Bug1400009", "nav_entry_si", |
| 2824 | entry->site_instance() ? ((int)entry->site_instance()->GetId()) : -1); |
| 2825 | SCOPED_CRASH_KEY_NUMBER("Bug1400009", "fne_si", |
| 2826 | frame_entry->site_instance() |
| 2827 | ? ((int)frame_entry->site_instance()->GetId()) |
| 2828 | : -1); |
| 2829 | bool has_sig = |
| 2830 | (frame_entry->site_instance() && frame_entry->site_instance()->group()); |
| 2831 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_exists", has_sig); |
| 2832 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_has_rvh", |
| 2833 | has_sig ? (!!frame_tree_->GetRenderViewHost( |
| 2834 | frame_entry->site_instance()->group())) |
| 2835 | : false); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2836 | render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request), |
| 2837 | ReloadType::NONE); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2838 | |
| 2839 | return true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2840 | } |
| 2841 | |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2842 | bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 2843 | base::TimeTicks actual_navigation_start = base::TimeTicks::Now(); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2844 | NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex()); |
| 2845 | if (!entry) |
| 2846 | return false; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 2847 | |
| 2848 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 2849 | // We should never navigate to an existing initial NavigationEntry that is |
| 2850 | // the initial NavigationEntry for the initial empty document that hasn't |
| 2851 | // been overridden by the synchronous about:blank commit, to preserve |
| 2852 | // legacy behavior where trying to reload when the main frame is on the |
| 2853 | // initial empty document won't result in a navigation. See also |
| 2854 | // https://crbug.com/1277414. |
| 2855 | return false; |
| 2856 | } |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2857 | FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node); |
| 2858 | if (!frame_entry) |
| 2859 | return false; |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2860 | ReloadType reload_type = ReloadType::NORMAL; |
| 2861 | entry->set_reload_type(reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2862 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2863 | frame_tree_node, entry, frame_entry, reload_type, |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2864 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2865 | false /* is_history_navigation_in_new_child */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2866 | std::nullopt /* initiator_frame_token */, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 2867 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */, |
| 2868 | actual_navigation_start); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2869 | if (!request) |
| 2870 | return false; |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2871 | frame_tree_node->navigator().Navigate(std::move(request), reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2872 | return true; |
| 2873 | } |
| 2874 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2875 | void NavigationControllerImpl::NavigateFromFrameProxy( |
| 2876 | RenderFrameHostImpl* render_frame_host, |
| 2877 | const GURL& url, |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2878 | const blink::LocalFrameToken* initiator_frame_token, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2879 | int initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2880 | const std::optional<url::Origin>& initiator_origin, |
| 2881 | const std::optional<GURL>& initiator_base_url, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2882 | bool is_renderer_initiated, |
| 2883 | SiteInstance* source_site_instance, |
| 2884 | const Referrer& referrer, |
| 2885 | ui::PageTransition page_transition, |
| 2886 | bool should_replace_current_entry, |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 2887 | blink::NavigationDownloadPolicy download_policy, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2888 | const std::string& method, |
| 2889 | scoped_refptr<network::ResourceRequestBody> post_body, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 2890 | const std::string& extra_headers, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2891 | network::mojom::SourceLocationPtr source_location, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2892 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 2893 | bool is_form_submission, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2894 | const std::optional<blink::Impression>& impression, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 2895 | blink::mojom::NavigationInitiatorActivationAndAdStatus |
| 2896 | initiator_activation_and_ad_status, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 2897 | base::TimeTicks actual_navigation_start_time, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 2898 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 2899 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 2900 | bool is_unfenced_top_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 2901 | bool force_new_browsing_instance, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2902 | bool is_container_initiated, |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 2903 | bool has_rel_opener, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 2904 | net::StorageAccessApiStatus storage_access_api_status, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2905 | std::optional<std::u16string> embedder_shared_storage_context) { |
Lukasz Anforowicz | 63f3b943 | 2019-05-30 05:42:58 | [diff] [blame] | 2906 | if (is_renderer_initiated) |
| 2907 | DCHECK(initiator_origin.has_value()); |
| 2908 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2909 | FrameTreeNode* node = render_frame_host->frame_tree_node(); |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2910 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2911 | // Don't allow an entry replacement if there is no entry to replace. |
| 2912 | // http://crbug.com/457149 |
| 2913 | if (GetEntryCount() == 0) |
| 2914 | should_replace_current_entry = false; |
| 2915 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2916 | // Create a NavigationEntry for the transfer, without making it the pending |
| 2917 | // entry. Subframe transfers should have a clone of the last committed entry |
| 2918 | // with a FrameNavigationEntry for the target frame. Main frame transfers |
| 2919 | // should have a new NavigationEntry. |
| 2920 | // TODO(creis): Make this unnecessary by creating (and validating) the params |
| 2921 | // directly, passing them to the destination RenderFrameHost. See |
| 2922 | // https://crbug.com/536906. |
| 2923 | std::unique_ptr<NavigationEntryImpl> entry; |
Harkiran Bolaria | e1b5158b | 2021-09-16 19:03:26 | [diff] [blame] | 2924 | if (!render_frame_host->is_main_frame()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2925 | // Subframe case: create FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2926 | DCHECK(GetLastCommittedEntry()); |
| 2927 | entry = GetLastCommittedEntry()->Clone(); |
| 2928 | entry->set_extra_headers(extra_headers); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2929 | // TODO(arthursonzogni): What about |is_renderer_initiated|? |
| 2930 | // Renderer-initiated navigation that target a remote frame are currently |
| 2931 | // classified as browser-initiated when this one has already navigated. |
| 2932 | // See https://crbug.com/722251. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2933 | // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter |
| 2934 | // to CreateNavigationRequestFromLoadParams(), so while kReplace might |
| 2935 | // remove child FrameNavigationEntries (e.g., if this is a cross-process |
| 2936 | // same-document navigation), they will still be present in the |
| 2937 | // committed NavigationEntry that will be referenced to construct the new |
| 2938 | // FrameNavigationEntry tree when this navigation commits. |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2939 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2940 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2941 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2942 | std::nullopt /* commit_origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2943 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2944 | blob_url_loader_factory, nullptr /* policy_container_policies */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2945 | } else { |
| 2946 | // Main frame case. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 2947 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 2948 | // to adjust the original input url if needed. For inner frames such as |
| 2949 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 2950 | // input urls by users. |
| 2951 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2952 | std::optional<GURL> source_process_site_url = std::nullopt; |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2953 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 2954 | source_process_site_url = |
| 2955 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 2956 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2957 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2958 | url, referrer, initiator_origin, initiator_base_url, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2959 | source_process_site_url, page_transition, is_renderer_initiated, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2960 | extra_headers, browser_context_, blob_url_loader_factory, |
| 2961 | rewrite_virtual_urls)); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2962 | entry->root_node()->frame_entry->set_source_site_instance( |
| 2963 | static_cast<SiteInstanceImpl*>(source_site_instance)); |
| 2964 | entry->root_node()->frame_entry->set_method(method); |
| 2965 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2966 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2967 | bool override_user_agent = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2968 | if (GetLastCommittedEntry()->GetIsOverridingUserAgent()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2969 | entry->SetIsOverridingUserAgent(true); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2970 | override_user_agent = true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2971 | } |
| 2972 | // TODO(creis): Set user gesture and intent received timestamp on Android. |
| 2973 | |
| 2974 | // We may not have successfully added the FrameNavigationEntry to |entry| |
| 2975 | // above (per https://crbug.com/608402), in which case we create it from |
| 2976 | // scratch. This works because we do not depend on |frame_entry| being inside |
| 2977 | // |entry| during NavigateToEntry. This will go away when we shortcut this |
| 2978 | // further in https://crbug.com/536906. |
| 2979 | scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node)); |
| 2980 | if (!frame_entry) { |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2981 | frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2982 | node->unique_name(), -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2983 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2984 | std::nullopt /* origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2985 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2986 | blob_url_loader_factory, nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2987 | false /* protect_url_in_navigation_api */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2988 | } |
| 2989 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2990 | LoadURLParams params(url); |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2991 | params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token); |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2992 | params.initiator_process_id = initiator_process_id; |
Nasko Oskov | 93e7c55c | 2018-12-19 01:59:29 | [diff] [blame] | 2993 | params.initiator_origin = initiator_origin; |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2994 | params.initiator_base_url = initiator_base_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2995 | params.source_site_instance = source_site_instance; |
| 2996 | params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT; |
| 2997 | params.transition_type = page_transition; |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2998 | params.frame_tree_node_id = node->frame_tree_node_id(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2999 | params.referrer = referrer; |
| 3000 | /* params.redirect_chain: skip */ |
| 3001 | params.extra_headers = extra_headers; |
| 3002 | params.is_renderer_initiated = is_renderer_initiated; |
| 3003 | params.override_user_agent = UA_OVERRIDE_INHERIT; |
| 3004 | /* params.base_url_for_data_url: skip */ |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 3005 | /* params.virtual_url_for_special_cases: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3006 | /* params.data_url_as_string: skip */ |
| 3007 | params.post_data = post_body; |
| 3008 | params.can_load_local_resources = false; |
Kevin McNee | e60e76b | 2019-11-27 20:01:58 | [diff] [blame] | 3009 | /* params.should_replace_current_entry: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3010 | /* params.frame_name: skip */ |
| 3011 | // TODO(clamy): See if user gesture should be propagated to this function. |
| 3012 | params.has_user_gesture = false; |
| 3013 | params.should_clear_history_list = false; |
| 3014 | params.started_from_context_menu = false; |
| 3015 | /* params.navigation_ui_data: skip */ |
| 3016 | /* params.input_start: skip */ |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 3017 | params.was_activated = blink::mojom::WasActivatedOption::kUnknown; |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3018 | /* params.reload_type: skip */ |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 3019 | params.impression = impression; |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 3020 | params.download_policy = std::move(download_policy); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 3021 | params.is_form_submission = is_form_submission; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 3022 | params.initiator_activation_and_ad_status = |
| 3023 | initiator_activation_and_ad_status; |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 3024 | params.has_rel_opener = has_rel_opener; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3025 | |
| 3026 | std::unique_ptr<NavigationRequest> request = |
| 3027 | CreateNavigationRequestFromLoadParams( |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 3028 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3029 | false /* has_user_gesture */, std::move(source_location), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3030 | ReloadType::NONE, entry.get(), frame_entry.get(), |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 3031 | actual_navigation_start_time, navigation_start_time, |
| 3032 | is_embedder_initiated_fenced_frame_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 3033 | is_unfenced_top_navigation, is_container_initiated, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 3034 | storage_access_api_status, embedder_shared_storage_context); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3035 | |
| 3036 | if (!request) |
| 3037 | return; |
| 3038 | |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 3039 | // Force the navigation to take place in a new browsing instance. |
| 3040 | // This is used by _unfencedTop in fenced frames to ensure that navigations |
| 3041 | // leaving the fenced context create a new browsing instance. |
| 3042 | if (force_new_browsing_instance) { |
| 3043 | request->coop_status().ForceBrowsingInstanceSwap(); |
| 3044 | } |
| 3045 | |
Arthur Hemery | 94874276 | 2019-09-18 10:06:24 | [diff] [blame] | 3046 | // At this stage we are proceeding with this navigation. If this was renderer |
| 3047 | // initiated with user gesture, we need to make sure we clear up potential |
| 3048 | // remains of a cancelled browser initiated navigation to avoid URL spoofs. |
| 3049 | DiscardNonCommittedEntries(); |
| 3050 | |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3051 | node->navigator().Navigate(std::move(request), ReloadType::NONE); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 3052 | } |
| 3053 | |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3054 | void NavigationControllerImpl::SetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3055 | const StoragePartitionConfig& partition_config, |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 3056 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3057 | if (!session_storage_namespace) |
| 3058 | return; |
| 3059 | |
| 3060 | // We can't overwrite an existing SessionStorage without violating spec. |
| 3061 | // Attempts to do so may give a tab access to another tab's session storage |
| 3062 | // so die hard on an error. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 3063 | bool successful_insert = |
| 3064 | session_storage_namespace_map_ |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3065 | .insert(std::make_pair(partition_config, |
Aaron Colwell | f3b316e | 2021-03-11 20:17:05 | [diff] [blame] | 3066 | static_cast<SessionStorageNamespaceImpl*>( |
| 3067 | session_storage_namespace))) |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3068 | .second; |
| 3069 | CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace"; |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3070 | } |
| 3071 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3072 | bool NavigationControllerImpl::IsUnmodifiedBlankTab() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3073 | return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() && |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3074 | !frame_tree_->has_accessed_initial_main_document(); |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 3075 | } |
| 3076 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 3077 | SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3078 | const StoragePartitionConfig& partition_config) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3079 | StoragePartition* partition = |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 3080 | browser_context_->GetStoragePartition(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 3081 | DOMStorageContextWrapper* context_wrapper = |
| 3082 | static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext()); |
| 3083 | |
| 3084 | SessionStorageNamespaceMap::const_iterator it = |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3085 | session_storage_namespace_map_.find(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 3086 | if (it != session_storage_namespace_map_.end()) { |
| 3087 | // Ensure that this namespace actually belongs to this partition. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 3088 | DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get()) |
| 3089 | ->IsFromContext(context_wrapper)); |
Aaron Colwell | b731a0ae | 2021-03-19 19:14:47 | [diff] [blame] | 3090 | |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 3091 | return it->second.get(); |
| 3092 | } |
| 3093 | |
| 3094 | // Create one if no one has accessed session storage for this partition yet. |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 3095 | scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace = |
| 3096 | SessionStorageNamespaceImpl::Create(context_wrapper); |
| 3097 | SessionStorageNamespaceImpl* session_storage_namespace_ptr = |
| 3098 | session_storage_namespace.get(); |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3099 | session_storage_namespace_map_[partition_config] = |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 3100 | std::move(session_storage_namespace); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3101 | |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 3102 | return session_storage_namespace_ptr; |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | SessionStorageNamespace* |
| 3106 | NavigationControllerImpl::GetDefaultSessionStorageNamespace() { |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 3107 | return GetSessionStorageNamespace( |
| 3108 | StoragePartitionConfig::CreateDefault(GetBrowserContext())); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | const SessionStorageNamespaceMap& |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3112 | NavigationControllerImpl::GetSessionStorageNamespaceMap() { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 3113 | return session_storage_namespace_map_; |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 3114 | } |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3115 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3116 | bool NavigationControllerImpl::NeedsReload() { |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 3117 | return needs_reload_; |
| 3118 | } |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 3119 | |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 3120 | void NavigationControllerImpl::SetNeedsReload() { |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 3121 | SetNeedsReload(NeedsReloadType::kRequestedByClient); |
| 3122 | } |
| 3123 | |
| 3124 | void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) { |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 3125 | needs_reload_ = true; |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 3126 | needs_reload_type_ = type; |
jaekyun | c8cefa8 | 2015-01-09 20:14:54 | [diff] [blame] | 3127 | |
| 3128 | if (last_committed_entry_index_ != -1) { |
| 3129 | entries_[last_committed_entry_index_]->SetTransitionType( |
| 3130 | ui::PAGE_TRANSITION_RELOAD); |
| 3131 | } |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 3132 | } |
| 3133 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3134 | void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) { |
Kevin McNee | 0516477 | 2019-09-03 17:24:57 | [diff] [blame] | 3135 | DCHECK_LT(index, GetEntryCount()); |
| 3136 | DCHECK_NE(index, last_committed_entry_index_); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3137 | DiscardNonCommittedEntries(); |
| 3138 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 3139 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3140 | entries_.erase(entries_.begin() + index); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 3141 | if (last_committed_entry_index_ > index) |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3142 | last_committed_entry_index_--; |
| 3143 | } |
| 3144 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3145 | NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3146 | // If there is no pending_entry_, there should be no pending_entry_index_. |
| 3147 | DCHECK(pending_entry_ || pending_entry_index_ == -1); |
| 3148 | |
| 3149 | // If there is a pending_entry_index_, then pending_entry_ must be the entry |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3150 | // at that index. An exception is while a reload of a post commit error page |
| 3151 | // is ongoing; in that case pending entry will point to the entry replaced |
| 3152 | // by the error. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3153 | DCHECK(pending_entry_index_ == -1 || |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3154 | pending_entry_ == GetEntryAtIndex(pending_entry_index_) || |
| 3155 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3156 | |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 3157 | return pending_entry_; |
| 3158 | } |
| 3159 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3160 | int NavigationControllerImpl::GetPendingEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3161 | // The pending entry index must always be less than the number of entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3162 | DCHECK_LT(pending_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 3163 | return pending_entry_index_; |
| 3164 | } |
| 3165 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 3166 | void NavigationControllerImpl::InsertOrReplaceEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 3167 | std::unique_ptr<NavigationEntryImpl> entry, |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3168 | bool replace, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 3169 | bool was_post_commit_error, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 3170 | bool in_fenced_frame_tree, |
| 3171 | LoadCommittedDetails* commit_details) { |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 3172 | // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME` |
| 3173 | // set because: |
| 3174 | // 1) They don't influence the history of the outer page. |
| 3175 | // 2) They are always replace only navigation (there is always only one entry |
| 3176 | // in their history stack). |
| 3177 | // 3) Are not top level navigations and appear similar to iframes. |
| 3178 | // Navigations of the fenced frame might create a new NavigationEntry, which |
| 3179 | // will call this function. Non fenced frame navigations will never have |
| 3180 | // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call |
| 3181 | // InsertOrReplaceEntry. |
| 3182 | DCHECK_EQ(in_fenced_frame_tree, |
| 3183 | ui::PageTransitionCoreTypeIs(entry->GetTransitionType(), |
| 3184 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3185 | |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 3186 | // If the pending_entry_index_ is -1, the navigation was to a new page, and we |
| 3187 | // need to keep continuity with the pending entry, so copy the pending entry's |
| 3188 | // unique ID to the committed entry. If the pending_entry_index_ isn't -1, |
| 3189 | // then the renderer navigated on its own, independent of the pending entry, |
| 3190 | // so don't copy anything. |
| 3191 | if (pending_entry_ && pending_entry_index_ == -1) |
| 3192 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3193 | |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 3194 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3195 | |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3196 | // When replacing, don't prune the forward history. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3197 | if (replace || was_post_commit_error) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 3198 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3199 | entries_[last_committed_entry_index_].get(), entry.get()); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3200 | // If the new entry is a post-commit error page, we store the current last |
| 3201 | // committed entry to the side so that we can put it back when navigating |
| 3202 | // away from the error. |
| 3203 | if (was_post_commit_error) { |
| 3204 | DCHECK(!entry_replaced_by_post_commit_error_); |
| 3205 | entry_replaced_by_post_commit_error_ = |
| 3206 | std::move(entries_[last_committed_entry_index_]); |
| 3207 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3208 | entries_[last_committed_entry_index_] = std::move(entry); |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3209 | return; |
| 3210 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3211 | |
creis | 37979a6 | 2015-08-04 19:48:18 | [diff] [blame] | 3212 | // We shouldn't see replace == true when there's no committed entries. |
| 3213 | DCHECK(!replace); |
| 3214 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 3215 | PruneForwardEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3216 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3217 | PruneOldestSkippableEntryIfFull(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3218 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3219 | entries_.push_back(std::move(entry)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3220 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3221 | } |
| 3222 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3223 | void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 3224 | if (entries_.size() < max_entry_count()) |
| 3225 | return; |
| 3226 | |
| 3227 | DCHECK_EQ(max_entry_count(), entries_.size()); |
| 3228 | DCHECK_GT(last_committed_entry_index_, 0); |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3229 | CHECK_EQ(pending_entry_index_, -1); |
| 3230 | |
| 3231 | int index = 0; |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 3232 | // Retrieve the oldest skippable entry. |
| 3233 | for (; index < GetEntryCount(); index++) { |
| 3234 | if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 3235 | break; |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | // If there is no skippable entry or if it is the last committed entry then |
| 3239 | // fall back to pruning the oldest entry. It is not safe to prune the last |
| 3240 | // committed entry. |
| 3241 | if (index == GetEntryCount() || index == last_committed_entry_index_) |
| 3242 | index = 0; |
| 3243 | |
| 3244 | bool should_succeed = RemoveEntryAtIndex(index); |
| 3245 | DCHECK_EQ(true, should_succeed); |
| 3246 | |
| 3247 | NotifyPrunedEntries(this, index, 1); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 3248 | } |
| 3249 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3250 | std::vector<base::WeakPtr<NavigationRequest>> |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3251 | NavigationControllerImpl::NavigateToExistingPendingEntry( |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3252 | ReloadType reload_type, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3253 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3254 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3255 | soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3256 | const std::string* navigation_api_key, |
| 3257 | base::TimeTicks actual_navigation_start) { |
Alexander Timin | 3a92df7 | 2019-09-20 11:59:50 | [diff] [blame] | 3258 | TRACE_EVENT0("navigation", |
| 3259 | "NavigationControllerImpl::NavigateToExistingPendingEntry"); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3260 | DCHECK(pending_entry_); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3261 | DCHECK(IsInitialNavigation() || pending_entry_index_ != -1); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3262 | if (pending_entry_index_ != -1) { |
| 3263 | // The pending entry may not be in entries_ if a post-commit error page is |
| 3264 | // showing. |
| 3265 | DCHECK(pending_entry_ == entries_[pending_entry_index_].get() || |
| 3266 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
| 3267 | } |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3268 | DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL())); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3269 | bool is_forced_reload = needs_reload_; |
[email protected] | 72097fd0 | 2010-01-21 23:36:01 | [diff] [blame] | 3270 | needs_reload_ = false; |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3271 | FrameTreeNode* root = frame_tree_->root(); |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3272 | int nav_entry_id = pending_entry_->GetUniqueID(); |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3273 | // Only pass down the soft_navigation_heuristics_task_id when the initiator is |
| 3274 | // the same as the top level frame being navigated. |
| 3275 | if (root->current_frame_host() != initiator_rfh) { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3276 | soft_navigation_heuristics_task_id = std::nullopt; |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3277 | } |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3278 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3279 | // If we were navigating to a slow-to-commit page, and the user performs |
| 3280 | // a session history navigation to the last committed page, RenderViewHost |
| 3281 | // will force the throbber to start, but WebKit will essentially ignore the |
| 3282 | // navigation, and won't send a message to stop the throbber. To prevent this |
| 3283 | // from happening, we drop the navigation here and stop the slow-to-commit |
| 3284 | // page from loading (which would normally happen during the navigation). |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3285 | if (pending_entry_index_ == last_committed_entry_index_ && |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 3286 | !pending_entry_->IsRestored() && |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3287 | pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) { |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3288 | frame_tree_->StopLoading(); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 3289 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3290 | DiscardNonCommittedEntries(); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3291 | return {}; |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3292 | } |
| 3293 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3294 | std::optional<blink::LocalFrameToken> initiator_frame_token; |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3295 | int initiator_process_id = ChildProcessHost::kInvalidUniqueID; |
| 3296 | if (initiator_rfh) { |
| 3297 | initiator_frame_token = initiator_rfh->GetFrameToken(); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 3298 | initiator_process_id = initiator_rfh->GetProcess()->GetDeprecatedID(); |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3299 | DCHECK(initiator_frame_token); |
| 3300 | } |
| 3301 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 3302 | // Compare FrameNavigationEntries to see which frames in the tree need to be |
| 3303 | // navigated. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3304 | std::vector<std::unique_ptr<NavigationRequest>> same_document_loads; |
| 3305 | std::vector<std::unique_ptr<NavigationRequest>> different_document_loads; |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3306 | FindFramesToNavigate(root, reload_type, initiator_frame_token, |
| 3307 | initiator_process_id, soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3308 | actual_navigation_start, &same_document_loads, |
| 3309 | &different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3310 | |
| 3311 | if (same_document_loads.empty() && different_document_loads.empty()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3312 | // We were unable to match any frames to navigate. This can happen if a |
| 3313 | // history navigation targets a subframe that no longer exists |
| 3314 | // (https://crbug.com/705550). In this case, we need to update the current |
| 3315 | // history entry to the pending one but keep the main document loaded. We |
| 3316 | // also need to ensure that observers are informed about the updated |
| 3317 | // current history entry (e.g., for greying out back/forward buttons), and |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 3318 | // that renderer processes update their history indices. The easiest way |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3319 | // to do all that is to schedule a "redundant" same-document navigation in |
| 3320 | // the main frame. |
| 3321 | // |
| 3322 | // Note that we don't want to remove this history entry, as it might still |
| 3323 | // be valid later, since a frame that it's targeting may be recreated. |
| 3324 | // |
| 3325 | // TODO(alexmos, creis): This behavior isn't ideal, as the user would |
| 3326 | // need to repeat history navigations until finding the one that works. |
| 3327 | // Consider changing this behavior to keep looking for the first valid |
| 3328 | // history entry that finds frames to navigate. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3329 | std::unique_ptr<NavigationRequest> navigation_request = |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3330 | CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3331 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3332 | ReloadType::NONE /* reload_type */, |
| 3333 | true /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3334 | false /* is_history_navigation_in_new_child */, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3335 | initiator_frame_token, initiator_process_id, |
| 3336 | actual_navigation_start); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3337 | if (!navigation_request) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3338 | // If this navigation cannot start, delete the pending NavigationEntry. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3339 | DiscardPendingEntry(false); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3340 | return {}; |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3341 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3342 | same_document_loads.push_back(std::move(navigation_request)); |
| 3343 | |
| 3344 | // Sanity check that we never take this branch for any kinds of reloads, |
| 3345 | // as those should've queued a different-document load in the main frame. |
| 3346 | DCHECK(!is_forced_reload); |
| 3347 | DCHECK_EQ(reload_type, ReloadType::NONE); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3348 | } |
| 3349 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3350 | // If the initiator is top-navigation sandboxed, then track whether this |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3351 | // navigation affects any frame outside the frame's subtree. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3352 | if (initiator_rfh && initiator_rfh->IsSandboxed( |
| 3353 | network::mojom::WebSandboxFlags::kTopNavigation)) { |
| 3354 | bool navigates_inside_tree = DoesSandboxNavigationStayWithinSubtree( |
| 3355 | initiator_rfh, same_document_loads) && |
| 3356 | DoesSandboxNavigationStayWithinSubtree( |
| 3357 | initiator_rfh, different_document_loads); |
Dave Tapuska | 716ed3af | 2019-09-23 18:45:50 | [diff] [blame] | 3358 | // Count the navigations as web use counters so we can determine |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3359 | // the number of pages that trigger this. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3360 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 3361 | initiator_rfh, |
| 3362 | navigates_inside_tree |
| 3363 | ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree |
| 3364 | : blink::mojom::WebFeature:: |
| 3365 | kSandboxBackForwardAffectsFramesOutsideSubtree); |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3366 | |
| 3367 | // If the navigation occurred outside the tree discard it because |
| 3368 | // the sandboxed frame didn't have permission to navigate outside |
| 3369 | // its tree. If it is possible that the navigation is both inside and |
| 3370 | // outside the frame tree and we discard it entirely because we don't |
| 3371 | // want to end up in a history state that didn't exist before. |
Dominic Farolino | 05744004 | 2022-01-19 18:18:14 | [diff] [blame] | 3372 | if (!navigates_inside_tree) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3373 | // If a |navigation_api_key| was provided, this navigation originated from |
| 3374 | // the navigation API. Notify the renderer that the navigation was |
| 3375 | // cancelled so the navigation API can fire an error event and reject the |
| 3376 | // relevant promise. |
| 3377 | if (navigation_api_key) { |
| 3378 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 3379 | *navigation_api_key, |
| 3380 | blink::mojom::TraverseCancelledReason::kSandboxViolation); |
| 3381 | } |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3382 | DiscardPendingEntry(false); |
Rakina Zata Amni | 58681c6 | 2024-06-25 06:32:13 | [diff] [blame] | 3383 | |
| 3384 | for (auto& unused_request : same_document_loads) { |
| 3385 | unused_request->set_navigation_discard_reason( |
| 3386 | NavigationDiscardReason::kNeverStarted); |
| 3387 | } |
| 3388 | for (auto& unused_request : different_document_loads) { |
| 3389 | unused_request->set_navigation_discard_reason( |
| 3390 | NavigationDiscardReason::kNeverStarted); |
| 3391 | } |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3392 | return {}; |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3393 | } |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3394 | } |
| 3395 | |
Nate Chapin | 6c43c02 | 2023-02-13 23:32:42 | [diff] [blame] | 3396 | // If it is possible that this traverse may involve a same-document navigation |
| 3397 | // in the initiator and there is a Navigation API key involved, then we may |
| 3398 | // need to notify the initiator if it fails. (The early returns above either |
| 3399 | // do not involve these cases or already notify the initiator.) |
| 3400 | // The event only needs to fire for the initiator, and only if the initiator |
| 3401 | // itself is performing a same-document navigation (because the event will not |
| 3402 | // fire if it navigates cross-document). |
| 3403 | if (navigation_api_key) { |
| 3404 | for (auto& item : same_document_loads) { |
| 3405 | if (item->frame_tree_node() == initiator_rfh->frame_tree_node()) { |
| 3406 | item->set_pending_navigation_api_key(*navigation_api_key); |
| 3407 | break; |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 | |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3412 | // BackForwardCache: |
| 3413 | // Navigate immediately if the document is in the BackForwardCache. |
Mingyu Lei | 7584b6b | 2023-04-13 03:02:56 | [diff] [blame] | 3414 | if (back_forward_cache_.GetOrEvictEntry(nav_entry_id).has_value()) { |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3415 | TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest"); |
Charlie Reis | 3696ab6 | 2025-05-28 07:02:30 | [diff] [blame] | 3416 | // TODO(crbug.com/420275259): Diagnose failures and upgrade to a CHECK. |
| 3417 | DCHECK_EQ(reload_type, ReloadType::NONE); |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3418 | base::WeakPtr<NavigationRequest> request; |
Rakina Zata Amni | 58681c6 | 2024-06-25 06:32:13 | [diff] [blame] | 3419 | |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3420 | // Skip a redundant NavigationRequest creation, per |
| 3421 | // https://crbug.com/417251428. |
| 3422 | if (base::FeatureList::IsEnabled(kSkipExtraBfcacheNavigationRequest)) { |
| 3423 | // If the BackForwardCache can handle this request, it must be for a main |
| 3424 | // frame, cross-document, non-reload request. This means there is only one |
| 3425 | // item in `different_document_loads` and no `same_document_loads`. |
| 3426 | CHECK_EQ(different_document_loads.size(), 1u); |
| 3427 | CHECK(same_document_loads.empty()); |
| 3428 | request = different_document_loads.at(0)->GetWeakPtr(); |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3429 | |
| 3430 | // Ensure that no re-entrant calls or discards of the pending entry occur |
| 3431 | // while calling `Navigator::Navigate` for a pending entry. |
| 3432 | ScopedPendingEntryReentrancyGuard reentrancy_guard( |
| 3433 | weak_factory_.GetSafeRef()); |
| 3434 | |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3435 | root->navigator().Navigate(std::move(different_document_loads.at(0)), |
| 3436 | ReloadType::NONE); |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3437 | |
| 3438 | // `reentrancy_guard` deleted here. |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3439 | } else { |
| 3440 | // The legacy approach creates a new NavigationRequest for the entry and |
| 3441 | // discards any previously created NavigationRequests, even though the new |
| 3442 | // request is identical to the sole existing request. |
| 3443 | // TODO(crbug.com/417251428): Remove this path once we measure the impact. |
| 3444 | auto navigation_request = CreateNavigationRequestFromEntry( |
| 3445 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
| 3446 | ReloadType::NONE, false /* is_same_document_history_load */, |
| 3447 | false /* is_history_navigation_in_new_child */, initiator_frame_token, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3448 | initiator_process_id, actual_navigation_start); |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3449 | request = navigation_request->GetWeakPtr(); |
| 3450 | root->navigator().Navigate(std::move(navigation_request), |
| 3451 | ReloadType::NONE); |
| 3452 | |
| 3453 | for (auto& unused_request : same_document_loads) { |
| 3454 | unused_request->set_navigation_discard_reason( |
| 3455 | NavigationDiscardReason::kNeverStarted); |
| 3456 | } |
| 3457 | for (auto& unused_request : different_document_loads) { |
| 3458 | unused_request->set_navigation_discard_reason( |
| 3459 | NavigationDiscardReason::kNeverStarted); |
| 3460 | } |
Rakina Zata Amni | 58681c6 | 2024-06-25 06:32:13 | [diff] [blame] | 3461 | } |
| 3462 | |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3463 | std::vector<base::WeakPtr<NavigationRequest>> bf_cache_requests; |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3464 | if (request) { |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3465 | bf_cache_requests.push_back(std::move(request)); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3466 | } |
Charlie Reis | 655c8f0 | 2025-05-15 01:16:17 | [diff] [blame] | 3467 | return bf_cache_requests; |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | // History navigation might try to reuse a specific BrowsingInstance, already |
| 3471 | // used by a page in the cache. To avoid having two different main frames that |
| 3472 | // live in the same BrowsingInstance, evict the all pages with this |
| 3473 | // BrowsingInstance from the cache. |
| 3474 | // |
| 3475 | // For example, take the following scenario: |
| 3476 | // |
| 3477 | // A1 = Some page on a.com |
| 3478 | // A2 = Some other page on a.com |
| 3479 | // B3 = An uncacheable page on b.com |
| 3480 | // |
| 3481 | // Then the following navigations occur: |
| 3482 | // A1->A2->B3->A1 |
| 3483 | // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't |
| 3484 | // take its place) and A1 will be created in the same BrowsingInstance (and |
| 3485 | // SiteInstance), as A2. |
| 3486 | // |
| 3487 | // If we didn't do anything, both A1 and A2 would remain alive in the same |
| 3488 | // BrowsingInstance/SiteInstance, which is unsupported by |
| 3489 | // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict |
| 3490 | // A2 from the cache. |
Rakina Zata Amni | 2cde21d | 2024-09-27 04:20:48 | [diff] [blame] | 3491 | SCOPED_CRASH_KEY_NUMBER("rvh_double", "pending_entry_si", |
| 3492 | pending_entry_->site_instance() |
| 3493 | ? pending_entry_->site_instance()->GetId().value() |
| 3494 | : -1); |
| 3495 | SCOPED_CRASH_KEY_NUMBER( |
| 3496 | "rvh_double", "pending_entry_bi", |
| 3497 | pending_entry_->site_instance() |
| 3498 | ? pending_entry_->site_instance()->GetBrowsingInstanceId().value() |
| 3499 | : -1); |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3500 | if (pending_entry_->site_instance()) { |
| 3501 | back_forward_cache_.EvictFramesInRelatedSiteInstances( |
| 3502 | pending_entry_->site_instance()); |
| 3503 | } |
| 3504 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3505 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored", |
| 3506 | pending_entry_ && pending_entry_->IsRestored()); |
| 3507 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id", |
| 3508 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3509 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index", |
| 3510 | pending_entry_index_); |
| 3511 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index", |
| 3512 | last_committed_entry_index_); |
| 3513 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size()); |
| 3514 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial", |
| 3515 | pending_entry_ && pending_entry_->IsInitialEntry()); |
| 3516 | SCOPED_CRASH_KEY_BOOL( |
| 3517 | "nav_reentrancy", "pending_entry_initial2", |
| 3518 | pending_entry_ && |
| 3519 | pending_entry_->IsInitialEntryNotForSynchronousAboutBlank()); |
| 3520 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav", |
| 3521 | IsInitialNavigation()); |
| 3522 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav", |
| 3523 | IsInitialBlankNavigation()); |
| 3524 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload); |
| 3525 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type", |
| 3526 | (int)pending_reload_); |
| 3527 | |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3528 | // Ensure that no re-entrant calls or discards of the pending entry occur |
| 3529 | // while calling `Navigator::Navigate` for a pending entry. |
| 3530 | ScopedPendingEntryReentrancyGuard reentrancy_guard( |
| 3531 | weak_factory_.GetSafeRef()); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3532 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3533 | // If the navigation-reentrancy is caused by calling |
| 3534 | // NavigateToExistingPendingEntry twice, this will note the previous call's |
| 3535 | // pending entry's ID. |
| 3536 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id", |
| 3537 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3538 | |
Nate Chapin | 154b14b | 2023-02-17 18:28:16 | [diff] [blame] | 3539 | // If there is a main-frame same-document history navigation, we may defer |
| 3540 | // the subframe history navigations in order to give JS in the main frame the |
| 3541 | // opportunity to cancel the entire traverse via the navigate event. In that |
| 3542 | // case, we need to stash the main frame request's navigation token on the |
| 3543 | // subframes, so they can look up the main frame request and defer themselves |
| 3544 | // until it completes. |
| 3545 | if (!same_document_loads.empty() && |
| 3546 | same_document_loads.at(0)->frame_tree_node()->IsMainFrame()) { |
| 3547 | NavigationRequest* main_frame_request = same_document_loads.at(0).get(); |
| 3548 | // The token will only be returned in cases where deferring the navigation |
| 3549 | // is necessary. |
| 3550 | if (auto main_frame_same_document_token = |
| 3551 | main_frame_request->GetNavigationTokenForDeferringSubframes()) { |
| 3552 | for (auto& item : same_document_loads) { |
| 3553 | if (item.get() != main_frame_request) { |
| 3554 | item->set_main_frame_same_document_history_token( |
| 3555 | main_frame_same_document_token); |
| 3556 | } |
| 3557 | } |
| 3558 | for (auto& item : different_document_loads) { |
| 3559 | item->set_main_frame_same_document_history_token( |
| 3560 | main_frame_same_document_token); |
| 3561 | } |
| 3562 | } |
| 3563 | } |
| 3564 | |
William Liu | 12275494 | 2024-01-18 22:34:39 | [diff] [blame] | 3565 | if (!initiator_rfh) { |
| 3566 | // A browser-initiated navigation won't have a `initiator_rfh`. |
| 3567 | CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions( |
| 3568 | different_document_loads, same_document_loads); |
| 3569 | } |
| 3570 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3571 | std::vector<base::WeakPtr<NavigationRequest>> all_requests; |
| 3572 | all_requests.reserve(same_document_loads.size() + |
| 3573 | different_document_loads.size()); |
| 3574 | bool seen_primary_main_frame_request = false; |
| 3575 | |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3576 | // Send all the same document frame loads before the different document loads. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3577 | for (auto& item : same_document_loads) { |
| 3578 | FrameTreeNode* frame = item->frame_tree_node(); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3579 | // The request could be destroyed before `navigator().Navigate()` returns. |
| 3580 | base::WeakPtr<NavigationRequest> request = item->GetWeakPtr(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3581 | frame->navigator().Navigate(std::move(item), reload_type); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3582 | if (request) { |
| 3583 | // Can only have one primary main frame request. |
| 3584 | CHECK(!seen_primary_main_frame_request || |
| 3585 | !request->IsInPrimaryMainFrame()); |
| 3586 | seen_primary_main_frame_request = request->IsInPrimaryMainFrame(); |
| 3587 | all_requests.push_back(std::move(request)); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3588 | } |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3589 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3590 | for (auto& item : different_document_loads) { |
| 3591 | FrameTreeNode* frame = item->frame_tree_node(); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3592 | base::WeakPtr<NavigationRequest> request = item->GetWeakPtr(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3593 | frame->navigator().Navigate(std::move(item), reload_type); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3594 | if (request) { |
| 3595 | // Can only have one primary main frame request. |
| 3596 | CHECK(!seen_primary_main_frame_request || |
| 3597 | !request->IsInPrimaryMainFrame()); |
| 3598 | seen_primary_main_frame_request = request->IsInPrimaryMainFrame(); |
| 3599 | all_requests.push_back(std::move(request)); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3600 | } |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3601 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3602 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3603 | return all_requests; |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3604 | // `reentrancy_guard` deleted here. |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3605 | } |
| 3606 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3607 | NavigationControllerImpl::HistoryNavigationAction |
| 3608 | NavigationControllerImpl::DetermineActionForHistoryNavigation( |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3609 | FrameTreeNode* frame, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3610 | ReloadType reload_type) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3611 | RenderFrameHostImpl* render_frame_host = frame->current_frame_host(); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3612 | // Only active and prerendered documents are allowed to navigate in their |
| 3613 | // frame. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3614 | if (render_frame_host->lifecycle_state() != |
Sreeja Kamishetty | 299329ad | 2021-03-25 14:06:01 | [diff] [blame] | 3615 | RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) { |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3616 | // - If the document is in pending deletion, the browser already committed |
| 3617 | // to destroying this RenderFrameHost. See https://crbug.com/930278. |
| 3618 | // - If the document is in back-forward cache, it's not allowed to navigate |
| 3619 | // as it should remain frozen. Ignore the request and evict the document |
| 3620 | // from back-forward cache. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3621 | // |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3622 | // If the document is inactive, there's no need to recurse into subframes, |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3623 | // which should all be inactive as well. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3624 | if (frame->current_frame_host()->IsInactiveAndDisallowActivation( |
| 3625 | DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3626 | return HistoryNavigationAction::kStopLooking; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3627 | } |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3628 | } |
arthursonzogni | 03f7615 | 2019-02-12 10:35:20 | [diff] [blame] | 3629 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3630 | // Reloads should result in a different-document load. Note that reloads may |
| 3631 | // also happen via the |needs_reload_| mechanism where the reload_type is |
| 3632 | // NONE, so detect this by comparing whether we're going to the same |
| 3633 | // entry that we're currently on. Similarly to above, only main frames |
| 3634 | // should reach this. Note that subframes support reloads, but that's done |
| 3635 | // via a different path that doesn't involve FindFramesToNavigate (see |
| 3636 | // RenderFrameHost::Reload()). |
| 3637 | if (reload_type != ReloadType::NONE || |
| 3638 | pending_entry_index_ == last_committed_entry_index_) { |
| 3639 | DCHECK(frame->IsMainFrame()); |
| 3640 | return HistoryNavigationAction::kDifferentDocument; |
| 3641 | } |
| 3642 | |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3643 | // If there is no new FrameNavigationEntry for the frame, ignore the |
| 3644 | // load. For example, this may happen when going back to an entry before a |
| 3645 | // frame was created. Suppose we commit a same-document navigation that also |
| 3646 | // results in adding a new subframe somewhere in the tree. If we go back, |
| 3647 | // the new subframe will be missing a FrameNavigationEntry in the previous |
| 3648 | // NavigationEntry, but we shouldn't delete or change what's loaded in |
| 3649 | // it. |
| 3650 | // |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3651 | // Note that in this case, there is no need to keep looking for navigations |
| 3652 | // in subframes, which would be missing FrameNavigationEntries as well. |
| 3653 | // |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3654 | // It's important to check this before checking |old_item| below, since both |
| 3655 | // might be null, and in that case we still shouldn't change what's loaded in |
| 3656 | // this frame. Note that scheduling any loads assumes that |new_item| is |
| 3657 | // non-null. See https://crbug.com/1088354. |
| 3658 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3659 | if (!new_item) |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3660 | return HistoryNavigationAction::kStopLooking; |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3661 | |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3662 | // Use the RenderFrameHost's last committed FrameNavigationEntry to identify |
| 3663 | // which history item it is currently on, since this may be different than the |
| 3664 | // FrameNavigationEntry for the frame in the last committed NavigationEntry |
| 3665 | // (e.g., if a history navigation is targeting multiple frames and only some |
| 3666 | // have committed so far). |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3667 | FrameNavigationEntry* old_item = |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3668 | frame->current_frame_host()->last_committed_frame_entry(); |
| 3669 | if (!old_item) { |
| 3670 | // In cases where the RenderFrameHost does not have a FrameNavigationEntry, |
| 3671 | // fall back to the last committed NavigationEntry's record for this frame. |
| 3672 | // This may happen in cases like the initial state of the RenderFrameHost. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 3673 | // TODO(crbug.com/40217743): Ensure the RenderFrameHost always has an |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3674 | // accurate FrameNavigationEntry and eliminate this case. |
| 3675 | old_item = GetLastCommittedEntry()->GetFrameEntry(frame); |
| 3676 | } |
| 3677 | // If neither approach finds a FrameNavigationEntry, schedule a |
| 3678 | // different-document load. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 3679 | // TODO(crbug.com/40467594): Remove this case. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3680 | if (!old_item) |
| 3681 | return HistoryNavigationAction::kDifferentDocument; |
| 3682 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3683 | // If the new item is not in the same SiteInstance, schedule a |
| 3684 | // different-document load. Newly restored items may not have a SiteInstance |
| 3685 | // yet, in which case it will be assigned on first commit. |
| 3686 | if (new_item->site_instance() && |
| 3687 | new_item->site_instance() != old_item->site_instance()) |
| 3688 | return HistoryNavigationAction::kDifferentDocument; |
| 3689 | |
Charlie Reis | 71e4ae3 | 2025-01-29 21:37:57 | [diff] [blame] | 3690 | // If the origins of the new and old items are both present but don't match, |
| 3691 | // schedule a different document load even if the document sequence numbers |
| 3692 | // somehow match. |
| 3693 | // TODO(crbug.com/40051596): Also handle session restore cases that lack a |
| 3694 | // committed origin on `new_item`, and update the Blink DSN computation to |
| 3695 | // avoid a cross-origin DSN match when possible. |
| 3696 | if (new_item->committed_origin().has_value() && |
| 3697 | old_item->committed_origin().has_value() && |
| 3698 | !new_item->committed_origin()->IsSameOriginWith( |
| 3699 | old_item->committed_origin().value())) { |
| 3700 | return HistoryNavigationAction::kDifferentDocument; |
| 3701 | } |
| 3702 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3703 | // Schedule a different-document load if the current RenderFrameHost is not |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3704 | // live. This case can happen for Ctrl+Back or after a renderer crash. Note |
| 3705 | // that we do this even if the history navigation would not be modifying this |
| 3706 | // frame were it live. |
| 3707 | if (!frame->current_frame_host()->IsRenderFrameLive()) |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3708 | return HistoryNavigationAction::kDifferentDocument; |
| 3709 | |
| 3710 | if (new_item->item_sequence_number() != old_item->item_sequence_number()) { |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3711 | // Starting a navigation after a crash early-promotes the speculative |
| 3712 | // RenderFrameHost. Then we have a RenderFrameHost with no document in it |
| 3713 | // committed yet, so we can not possibly perform a same-document history |
| 3714 | // navigation. The frame would need to be reloaded with a cross-document |
| 3715 | // navigation. |
| 3716 | if (!frame->current_frame_host()->has_committed_any_navigation()) |
| 3717 | return HistoryNavigationAction::kDifferentDocument; |
| 3718 | |
creis | 5413169 | 2016-08-12 18:32:25 | [diff] [blame] | 3719 | // Same document loads happen if the previous item has the same document |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3720 | // sequence number but different item sequence number. |
| 3721 | if (new_item->document_sequence_number() == |
| 3722 | old_item->document_sequence_number()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3723 | return HistoryNavigationAction::kSameDocument; |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3724 | } |
avi | b48cb31 | 2016-05-05 21:35:00 | [diff] [blame] | 3725 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3726 | // Otherwise, if both item and document sequence numbers differ, this |
| 3727 | // should be a different document load. |
| 3728 | return HistoryNavigationAction::kDifferentDocument; |
| 3729 | } |
| 3730 | |
| 3731 | // If the item sequence numbers match, there is no need to navigate this |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3732 | // frame. Keep looking for navigations in this frame's children. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3733 | DCHECK_EQ(new_item->document_sequence_number(), |
| 3734 | old_item->document_sequence_number()); |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3735 | return HistoryNavigationAction::kKeepLooking; |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | void NavigationControllerImpl::FindFramesToNavigate( |
| 3739 | FrameTreeNode* frame, |
| 3740 | ReloadType reload_type, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3741 | const std::optional<blink::LocalFrameToken>& initiator_frame_token, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3742 | int initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3743 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3744 | soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3745 | base::TimeTicks actual_navigation_start, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3746 | std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads, |
| 3747 | std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) { |
| 3748 | DCHECK(pending_entry_); |
| 3749 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3750 | |
| 3751 | auto action = DetermineActionForHistoryNavigation(frame, reload_type); |
| 3752 | |
| 3753 | if (action == HistoryNavigationAction::kSameDocument) { |
| 3754 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3755 | CreateNavigationRequestFromEntry( |
| 3756 | frame, pending_entry_, new_item, reload_type, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3757 | /*is_same_document_history_load=*/true, |
| 3758 | /*is_history_navigation_in_new_child_frame=*/false, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3759 | initiator_frame_token, initiator_process_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3760 | actual_navigation_start, soft_navigation_heuristics_task_id); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3761 | if (navigation_request) { |
| 3762 | // Only add the request if was properly created. It's possible for the |
| 3763 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3764 | same_document_loads->push_back(std::move(navigation_request)); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3765 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3766 | } else if (action == HistoryNavigationAction::kDifferentDocument) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3767 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3768 | CreateNavigationRequestFromEntry( |
| 3769 | frame, pending_entry_, new_item, reload_type, |
| 3770 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3771 | false /* is_history_navigation_in_new_child */, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3772 | initiator_frame_token, initiator_process_id, |
| 3773 | actual_navigation_start); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3774 | if (navigation_request) { |
| 3775 | // Only add the request if was properly created. It's possible for the |
| 3776 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3777 | different_document_loads->push_back(std::move(navigation_request)); |
| 3778 | } |
| 3779 | // For a different document, the subframes will be destroyed, so there's |
| 3780 | // no need to consider them. |
| 3781 | return; |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3782 | } else if (action == HistoryNavigationAction::kStopLooking) { |
| 3783 | return; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3784 | } |
| 3785 | |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3786 | // Do not pass down the soft_navigation_heuristics_task_id to child frames, as |
| 3787 | // we currently only support soft navigation heuristics for the top level |
| 3788 | // frame. |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3789 | for (size_t i = 0; i < frame->child_count(); i++) { |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3790 | FindFramesToNavigate(frame->child_at(i), reload_type, initiator_frame_token, |
| 3791 | initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3792 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3793 | actual_navigation_start, same_document_loads, |
| 3794 | different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3798 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry( |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3799 | const LoadURLParams& params, |
| 3800 | base::TimeTicks actual_navigation_start) { |
Emmanuel Arias Soto | 9e15965 | 2025-05-16 07:53:55 | [diff] [blame] | 3801 | FrameTreeNode* node = GetTargetFrameTreeNodeForNavigation(params); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3802 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3803 | // Compute overrides to the LoadURLParams for |override_user_agent|, |
| 3804 | // |should_replace_current_entry| and |has_user_gesture| that will be used |
| 3805 | // both in the creation of the NavigationEntry and the NavigationRequest. |
| 3806 | // Ideally, the LoadURLParams themselves would be updated, but since they are |
| 3807 | // passed as a const reference, this is not possible. |
| 3808 | // TODO(clamy): When we only create a NavigationRequest, move this to |
| 3809 | // CreateNavigationRequestFromLoadURLParams. |
| 3810 | bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent, |
| 3811 | GetLastCommittedEntry()); |
| 3812 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3813 | // An entry replacement must happen if the current browsing context should |
| 3814 | // maintain a trivial session history. |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 3815 | bool should_replace_current_entry = |
| 3816 | (params.should_replace_current_entry || |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3817 | ShouldMaintainTrivialSessionHistory(node)); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3818 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3819 | // Javascript URLs should not create NavigationEntries. All other navigations |
| 3820 | // do, including navigations to chrome renderer debug URLs. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3821 | if (!params.url.SchemeIs(url::kJavaScriptScheme)) { |
Scott Violet | 5ae6c42e | 2020-10-28 02:47:37 | [diff] [blame] | 3822 | std::unique_ptr<NavigationEntryImpl> entry = |
| 3823 | CreateNavigationEntryFromLoadParams(node, params, override_user_agent, |
| 3824 | should_replace_current_entry, |
| 3825 | params.has_user_gesture); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3826 | DiscardPendingEntry(false); |
| 3827 | SetPendingEntry(std::move(entry)); |
| 3828 | } |
| 3829 | |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3830 | // Renderer-debug URLs are sent to the current renderer process immediately |
| 3831 | // for processing and don't need to create a NavigationRequest. Note: this |
| 3832 | // includes navigations to JavaScript URLs, which are considered |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3833 | // renderer-debug URLs. |
| 3834 | // Note: we intentionally leave the pending entry in place for renderer debug |
| 3835 | // URLs, unlike the cases below where we clear it if the navigation doesn't |
| 3836 | // proceed. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3837 | if (blink::IsRendererDebugURL(params.url)) { |
Emmanuel Arias Soto | 9e15965 | 2025-05-16 07:53:55 | [diff] [blame] | 3838 | // Renderer-debug URLs won't go through NavigationThrottles so we have to |
| 3839 | // check them explicitly. See crbug.com/40605746. |
Aaron Colwell | e1908d98 | 2020-06-26 22:08:15 | [diff] [blame] | 3840 | if (GetContentClient()->browser()->ShouldBlockRendererDebugURL( |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3841 | params.url, browser_context_, node->current_frame_host())) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3842 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3843 | return nullptr; |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3844 | } |
| 3845 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3846 | HandleRendererDebugURL(node, params.url); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3847 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3848 | } |
| 3849 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3850 | DCHECK(pending_entry_); |
| 3851 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3852 | // Convert navigations to the current URL to a reload. |
| 3853 | // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of |
| 3854 | // relying on the frame tree node id from LoadURLParams. Unfortunately, |
| 3855 | // DevTools sometimes issues navigations to main frames that they do not |
| 3856 | // expect to see treated as reload, and it only works because they pass a |
| 3857 | // FrameTreeNode id in their LoadURLParams. Change this once they no longer do |
| 3858 | // that. See https://crbug.com/850926. |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3859 | ReloadType reload_type = params.reload_type; |
| 3860 | if (reload_type == ReloadType::NONE && |
| 3861 | ShouldTreatNavigationAsReload( |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 3862 | node, params.url, pending_entry_->GetVirtualURL(), |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3863 | params.base_url_for_data_url, params.transition_type, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3864 | params.load_type == |
| 3865 | NavigationController::LOAD_TYPE_HTTP_POST /* is_post */, |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3866 | should_replace_current_entry, GetLastCommittedEntry())) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3867 | reload_type = ReloadType::NORMAL; |
Alexander Timin | b70f6738 | 2020-12-10 00:03:47 | [diff] [blame] | 3868 | pending_entry_->set_reload_type(reload_type); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3869 | |
| 3870 | // If this is a reload of an existing FrameNavigationEntry and we had a |
| 3871 | // policy container for it, then we should copy it into the pending entry, |
| 3872 | // so that it is copied to the navigation request in |
| 3873 | // CreateNavigationRequestFromLoadParams later. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3874 | FrameNavigationEntry* previous_frame_entry = |
| 3875 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 3876 | if (previous_frame_entry && |
| 3877 | previous_frame_entry->policy_container_policies()) { |
| 3878 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
| 3879 | previous_frame_entry->policy_container_policies()->ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | // If this navigation is an "Enter-in-omnibox" with the initial about:blank |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 3884 | // document, then we should copy the document polices from RenderFrameHost's |
| 3885 | // PolicyContainerHost. The NavigationRequest will create a new |
| 3886 | // PolicyContainerHost with the document policies from the |pending_entry_|, |
| 3887 | // and that PolicyContainerHost will be put in the final RenderFrameHost for |
| 3888 | // the navigation. This way, we ensure that we keep enforcing the right |
| 3889 | // policies on the initial empty document after the reload. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3890 | if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3891 | if (node->current_frame_host() && |
| 3892 | node->current_frame_host()->policy_container_host()) { |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3893 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 3894 | node->current_frame_host() |
| 3895 | ->policy_container_host() |
| 3896 | ->policies() |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 3897 | .ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3898 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | // navigation_ui_data should only be present for main frame navigations. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 3902 | DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3903 | |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3904 | // This will be used to set the Navigation Timing API navigationStart |
| 3905 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 3906 | // "Open link in new tab"). If the navigation must wait on the current |
| 3907 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 3908 | // will be updated when the BeforeUnload ack is received. |
| 3909 | const auto navigation_start_time = base::TimeTicks::Now(); |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3910 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3911 | std::unique_ptr<NavigationRequest> request = |
| 3912 | CreateNavigationRequestFromLoadParams( |
| 3913 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3914 | params.has_user_gesture, network::mojom::SourceLocation::New(), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3915 | reload_type, pending_entry_, pending_entry_->GetFrameEntry(node), |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 3916 | actual_navigation_start, navigation_start_time); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3917 | |
| 3918 | // If the navigation couldn't start, return immediately and discard the |
| 3919 | // pending NavigationEntry. |
| 3920 | if (!request) { |
| 3921 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3922 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3923 | } |
| 3924 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3925 | #if DCHECK_IS_ON() |
| 3926 | // Safety check that NavigationRequest and NavigationEntry match. |
| 3927 | ValidateRequestMatchesEntry(request.get(), pending_entry_); |
| 3928 | #endif |
| 3929 | |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3930 | // Ensure that no re-entrant calls or discards of the pending entry occur |
| 3931 | // while calling `Navigator::Navigate` for a pending entry. |
| 3932 | ScopedPendingEntryReentrancyGuard reentrancy_guard( |
| 3933 | weak_factory_.GetSafeRef()); |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3934 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3935 | base::WeakPtr<NavigationHandle> created_navigation_handle( |
| 3936 | request->GetWeakPtr()); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3937 | node->navigator().Navigate(std::move(request), reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3938 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3939 | return created_navigation_handle; |
Charlie Reis | d3e4fef | 2025-05-20 02:04:17 | [diff] [blame] | 3940 | // `reentrancy_guard` deleted here. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3941 | } |
| 3942 | |
Emmanuel Arias Soto | 9e15965 | 2025-05-16 07:53:55 | [diff] [blame] | 3943 | FrameTreeNode* NavigationControllerImpl::GetTargetFrameTreeNodeForNavigation( |
| 3944 | const LoadURLParams& params) { |
| 3945 | FrameTreeNode* node = nullptr; |
| 3946 | if (params.frame_tree_node_id || !params.frame_name.empty()) { |
| 3947 | node = params.frame_tree_node_id |
| 3948 | ? frame_tree_->FindByID(params.frame_tree_node_id) |
| 3949 | : frame_tree_->FindByName(params.frame_name); |
| 3950 | DCHECK(!node || &node->frame_tree() == &frame_tree()); |
| 3951 | if (!node && params.frame_tree_node_id) { |
| 3952 | // If the specified FrameTreeNode exists in another FrameTree, the caller |
| 3953 | // is using the wrong NavigationController. |
| 3954 | CHECK(!FrameTreeNode::GloballyFindByID(params.frame_tree_node_id), |
| 3955 | base::NotFatalUntil::M140); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | // If no FrameTreeNode was specified, navigate the main frame. |
| 3960 | if (!node) { |
| 3961 | node = frame_tree_->root(); |
| 3962 | } |
| 3963 | return node; |
| 3964 | } |
| 3965 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3966 | void NavigationControllerImpl::HandleRendererDebugURL( |
| 3967 | FrameTreeNode* frame_tree_node, |
| 3968 | const GURL& url) { |
| 3969 | if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3970 | // Any renderer-side debug URLs or javascript: URLs should be ignored if |
| 3971 | // the renderer process is not live, unless it is the initial navigation |
| 3972 | // of the tab. |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3973 | if (!IsInitialNavigation()) { |
| 3974 | DiscardNonCommittedEntries(); |
| 3975 | return; |
| 3976 | } |
Fergal Daly | ecd3b020 | 2020-06-25 01:57:37 | [diff] [blame] | 3977 | // The current frame is always a main frame. If IsInitialNavigation() is |
| 3978 | // true then there have been no navigations and any frames of this tab must |
| 3979 | // be in the same renderer process. If that has crashed then the only frame |
| 3980 | // that can be revived is the main frame. |
| 3981 | frame_tree_node->render_manager() |
| 3982 | ->InitializeMainRenderFrameForImmediateUse(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3983 | } |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3984 | |
| 3985 | // Several tests expect a load of Chrome Debug URLs to send a DidStopLoading |
| 3986 | // notification, so set is loading to true here to properly surface it when |
| 3987 | // the renderer process is done handling the URL. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 3988 | // TODO(crbug.com/40199456): Remove the test dependency on this behavior. |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3989 | if (!url.SchemeIs(url::kJavaScriptScheme)) { |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3990 | frame_tree_node->current_frame_host()->SetIsLoadingForRendererDebugURL(); |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3991 | } |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3992 | frame_tree_node->current_frame_host()->HandleRendererDebugURL(url); |
| 3993 | } |
| 3994 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3995 | std::unique_ptr<NavigationEntryImpl> |
| 3996 | NavigationControllerImpl::CreateNavigationEntryFromLoadParams( |
| 3997 | FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3998 | const LoadURLParams& params, |
| 3999 | bool override_user_agent, |
| 4000 | bool should_replace_current_entry, |
| 4001 | bool has_user_gesture) { |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 4002 | // Browser initiated navigations might not have a blob_url_loader_factory set |
| 4003 | // in params even if the navigation is to a blob URL. If that happens, lookup |
| 4004 | // the correct url loader factory to use here. |
| 4005 | auto blob_url_loader_factory = params.blob_url_loader_factory; |
Kinuko Yasuda | 7d925ea2 | 2019-08-01 10:08:48 | [diff] [blame] | 4006 | if (!blob_url_loader_factory && params.url.SchemeIsBlob()) { |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 4007 | // Resolve the blob URL in the storage partition associated with the target |
| 4008 | // frame. This is the storage partition the URL will be loaded in, and only |
| 4009 | // URLs that can be resolved by it should be able to access its data. |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 4010 | blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl( |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 4011 | node->current_frame_host()->GetStoragePartition(), params.url); |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 4012 | } |
| 4013 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4014 | std::unique_ptr<NavigationEntryImpl> entry; |
Tommy C. Li | 03eee77a | 2019-02-05 02:07:44 | [diff] [blame] | 4015 | // extra_headers in params are \n separated; navigation entries want \r\n. |
| 4016 | std::string extra_headers_crlf; |
| 4017 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4018 | |
| 4019 | // For subframes, create a pending entry with a corresponding frame entry. |
| 4020 | if (!node->IsMainFrame()) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4021 | entry = GetLastCommittedEntry()->Clone(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4022 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4023 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4024 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get()), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4025 | params.url, std::nullopt, params.referrer, params.initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 4026 | params.initiator_base_url, params.redirect_chain, blink::PageState(), |
| 4027 | "GET", -1, blob_url_loader_factory, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4028 | // If in NavigateWithoutEntry we later determine that this navigation is |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 4029 | // a conversion of a new navigation into a reload, we will set the right |
| 4030 | // document policies there. |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4031 | nullptr /* policy_container_policies */); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4032 | } else { |
| 4033 | // Otherwise, create a pending entry for the main frame. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 4034 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 4035 | // to adjust the original input url if needed. For inner frames such as |
| 4036 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 4037 | // input urls by users. |
| 4038 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 4039 | scoped_refptr<SiteInstance> source_site_instance = |
| 4040 | params.source_site_instance; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4041 | std::optional<GURL> source_process_site_url = std::nullopt; |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 4042 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 4043 | source_process_site_url = |
| 4044 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 4045 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4046 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 4047 | params.url, params.referrer, params.initiator_origin, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 4048 | params.initiator_base_url, source_process_site_url, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 4049 | params.transition_type, params.is_renderer_initiated, |
| 4050 | extra_headers_crlf, browser_context_, blob_url_loader_factory, |
| 4051 | rewrite_virtual_urls)); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4052 | entry->set_source_site_instance( |
| 4053 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get())); |
| 4054 | entry->SetRedirectChain(params.redirect_chain); |
| 4055 | } |
| 4056 | |
| 4057 | // Set the FTN ID (only used in non-site-per-process, for tests). |
| 4058 | entry->set_frame_tree_node_id(node->frame_tree_node_id()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4059 | entry->set_should_clear_history_list(params.should_clear_history_list); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4060 | entry->SetIsOverridingUserAgent(override_user_agent); |
| 4061 | entry->set_has_user_gesture(has_user_gesture); |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 4062 | entry->set_reload_type(params.reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4063 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4064 | switch (params.load_type) { |
| 4065 | case LOAD_TYPE_DEFAULT: |
| 4066 | break; |
| 4067 | case LOAD_TYPE_HTTP_POST: |
| 4068 | entry->SetHasPostData(true); |
| 4069 | entry->SetPostData(params.post_data); |
| 4070 | break; |
| 4071 | case LOAD_TYPE_DATA: |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 4072 | // LoadDataWithBaseURL is a special case that needs to assign both a base |
| 4073 | // URL and a virtual URL, while loading the actual content from a data |
| 4074 | // URL. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4075 | entry->SetBaseURLForDataURL(params.base_url_for_data_url); |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 4076 | entry->SetVirtualURL(params.virtual_url_for_special_cases); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 4077 | #if BUILDFLAG(IS_ANDROID) |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4078 | entry->SetDataURLAsString(params.data_url_as_string); |
| 4079 | #endif |
| 4080 | entry->SetCanLoadLocalResources(params.can_load_local_resources); |
| 4081 | break; |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 4082 | #if BUILDFLAG(IS_ANDROID) |
| 4083 | case LOAD_TYPE_PDF_ANDROID: |
| 4084 | // Android PDF URLs show the actual PDF URL as a virtual URL, while an |
| 4085 | // internal URL is used for the navigation URL. |
| 4086 | entry->SetVirtualURL(params.virtual_url_for_special_cases); |
| 4087 | break; |
| 4088 | #endif |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | // TODO(clamy): NavigationEntry is meant for information that will be kept |
| 4092 | // after the navigation ended and therefore is not appropriate for |
| 4093 | // started_from_context_menu. Move started_from_context_menu to |
| 4094 | // NavigationUIData. |
| 4095 | entry->set_started_from_context_menu(params.started_from_context_menu); |
| 4096 | |
| 4097 | return entry; |
| 4098 | } |
| 4099 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4100 | std::unique_ptr<NavigationRequest> |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4101 | NavigationControllerImpl::CreateNavigationRequestFromLoadParams( |
| 4102 | FrameTreeNode* node, |
| 4103 | const LoadURLParams& params, |
| 4104 | bool override_user_agent, |
| 4105 | bool should_replace_current_entry, |
| 4106 | bool has_user_gesture, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 4107 | network::mojom::SourceLocationPtr source_location, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4108 | ReloadType reload_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4109 | NavigationEntryImpl* entry, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 4110 | FrameNavigationEntry* frame_entry, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 4111 | base::TimeTicks actual_navigation_start_time, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 4112 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4113 | bool is_embedder_initiated_fenced_frame_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 4114 | bool is_unfenced_top_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 4115 | bool is_container_initiated, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 4116 | net::StorageAccessApiStatus storage_access_api_status, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4117 | std::optional<std::u16string> embedder_shared_storage_context) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4118 | DCHECK_EQ(-1, GetIndexOfEntry(entry)); |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4119 | |
| 4120 | // TODO(https://crbug.com/40467594): Add a CHECK(frame_entry) once all |
| 4121 | // subframes have FrameNavigationEntries associated with them. Until then, |
| 4122 | // there may be cases where a subframe navigation is missing `frame_entry` |
| 4123 | // (e.g., see https://crbug.com/358084015). |
| 4124 | |
Nasko Oskov | 3c2f9e25 | 2019-01-10 17:45:53 | [diff] [blame] | 4125 | // All renderer-initiated navigations must have an initiator_origin. |
| 4126 | DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value()); |
Camille Lamy | ff7c482 | 2018-11-07 15:42:51 | [diff] [blame] | 4127 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4128 | GURL url_to_load; |
| 4129 | GURL virtual_url; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 4130 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4131 | // For main frames, rewrite the URL if necessary and compute the virtual URL |
| 4132 | // that should be shown in the address bar. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 4133 | if (node->IsOutermostMainFrame()) { |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 4134 | bool ignored_reverse_on_redirect = false; |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4135 | RewriteUrlForNavigation(params.url, browser_context_, &url_to_load, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 4136 | &virtual_url, &ignored_reverse_on_redirect); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4137 | |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 4138 | // Both LoadDataWithBaseURL and Android PDF navigations are special cases |
| 4139 | // that need to define a virtual URL to display, which differs from the |
| 4140 | // navigation URL. |
| 4141 | if (params.load_type == LOAD_TYPE_DATA) { |
| 4142 | virtual_url = params.virtual_url_for_special_cases; |
| 4143 | } |
| 4144 | #if BUILDFLAG(IS_ANDROID) |
| 4145 | if (params.load_type == LOAD_TYPE_PDF_ANDROID) { |
| 4146 | virtual_url = params.virtual_url_for_special_cases; |
| 4147 | } |
| 4148 | #endif |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4149 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4150 | if (virtual_url.is_empty()) |
| 4151 | virtual_url = url_to_load; |
| 4152 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4153 | CHECK(virtual_url == entry->GetVirtualURL()); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 4154 | |
Aran Gilman | 249eb12 | 2019-12-02 23:32:46 | [diff] [blame] | 4155 | // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic |
| 4156 | // behavior: it is possible for two calls to RewriteUrlForNavigation to |
| 4157 | // return different results, leading to a different URL in the |
| 4158 | // NavigationRequest and FrameEntry. This will be fixed once we remove the |
| 4159 | // pending NavigationEntry, as we'll only make one call to |
| 4160 | // RewriteUrlForNavigation. |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4161 | if (frame_entry) { |
| 4162 | VLOG_IF(1, (url_to_load != frame_entry->url())) |
| 4163 | << "NavigationRequest and FrameEntry have different URLs: " |
| 4164 | << url_to_load << " vs " << frame_entry->url(); |
| 4165 | } |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 4166 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4167 | // TODO(clamy): In order to remove the pending NavigationEntry, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 4168 | // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4169 | // NavigationRequest. |
| 4170 | } else { |
| 4171 | url_to_load = params.url; |
| 4172 | virtual_url = params.url; |
Camille Lamy | f664f762 | 2019-01-07 19:28:24 | [diff] [blame] | 4173 | CHECK(!frame_entry || url_to_load == frame_entry->url()); |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 4174 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4175 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 4176 | if (node->render_manager()->is_attaching_inner_delegate()) { |
| 4177 | // Avoid starting any new navigations since this node is now preparing for |
| 4178 | // attaching an inner delegate. |
| 4179 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 4180 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4181 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 4182 | if (!IsValidURLForNavigation(node, virtual_url, url_to_load)) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4183 | return nullptr; |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 4184 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4185 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4186 | // Look for a pending commit that is to another document in this |
| 4187 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 4188 | // current URL by the time this navigation commits. |
| 4189 | bool has_pending_cross_document_commit = |
| 4190 | node->render_manager()->HasPendingCommitForCrossDocumentNavigation(); |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 4191 | bool is_currently_error_page = node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4192 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4193 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4194 | /*old_url=*/node->current_url(), |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4195 | /*new_url=*/url_to_load, reload_type, entry, frame_entry, |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4196 | has_pending_cross_document_commit, is_currently_error_page, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 4197 | /*is_same_document_history_load=*/false, |
| 4198 | is_embedder_initiated_fenced_frame_navigation, |
| 4199 | is_unfenced_top_navigation); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4200 | |
| 4201 | // Create the NavigationParams based on |params|. |
| 4202 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 4203 | bool is_view_source_mode = entry->IsViewSourceMode(); |
| 4204 | DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme)); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4205 | |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 4206 | blink::NavigationDownloadPolicy download_policy = params.download_policy; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4207 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 4208 | // Update |download_policy| if the virtual URL is view-source. |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4209 | if (is_view_source_mode) |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 4210 | download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4211 | |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4212 | std::string page_state_data = |
| 4213 | frame_entry ? frame_entry->page_state().ToEncodedData() : std::string(); |
| 4214 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4215 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 4216 | blink::mojom::CommonNavigationParams::New( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 4217 | url_to_load, params.initiator_origin, params.initiator_base_url, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4218 | blink::mojom::Referrer::New(params.referrer.url, |
| 4219 | params.referrer.policy), |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 4220 | params.transition_type, navigation_type, download_policy, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4221 | should_replace_current_entry, params.base_url_for_data_url, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 4222 | actual_navigation_start_time, navigation_start_time, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4223 | params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET", |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 4224 | params.post_data, std::move(source_location), |
arthursonzogni | af7c62c5 | 2020-02-12 10:49:41 | [diff] [blame] | 4225 | params.started_from_context_menu, has_user_gesture, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 4226 | false /* has_text_fragment_token */, |
| 4227 | network::mojom::CSPDisposition::CHECK, std::vector<int>(), |
| 4228 | params.href_translate, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4229 | false /* is_history_navigation_in_new_child_frame */, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 4230 | params.input_start, network::mojom::RequestDestination::kEmpty); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4231 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4232 | blink::mojom::CommitNavigationParamsPtr commit_params = |
| 4233 | blink::mojom::CommitNavigationParams::New( |
Diana Qu | ae845bf | 2025-04-03 20:12:37 | [diff] [blame] | 4234 | url::Origin(), |
Ari Chivukula | c753352 | 2024-05-14 21:52:34 | [diff] [blame] | 4235 | // The correct storage key will be computed before committing the |
| 4236 | // navigation. |
| 4237 | blink::StorageKey(), override_user_agent, params.redirect_chain, |
Lucas Furukawa Gadani | 81e294b | 2019-08-29 16:26:32 | [diff] [blame] | 4238 | std::vector<network::mojom::URLResponseHeadPtr>(), |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4239 | std::vector<net::RedirectInfo>(), params.post_content_type, |
| 4240 | common_params->url, common_params->method, |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4241 | params.can_load_local_resources, page_state_data, |
| 4242 | entry->GetUniqueID(), entry->GetSubframeUniqueNames(node), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4243 | /*intended_as_new_entry=*/true, |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4244 | /*pending_history_list_index=*/-1, |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4245 | params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(), |
| 4246 | params.should_clear_history_list ? 0 : GetEntryCount(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4247 | /*was_discarded=*/false, is_view_source_mode, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4248 | params.should_clear_history_list, |
| 4249 | blink::mojom::NavigationTiming::New(), |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 4250 | blink::mojom::WasActivatedOption::kUnknown, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4251 | /*navigation_token=*/base::UnguessableToken::Create(), |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 4252 | std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(), |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 4253 | #if BUILDFLAG(IS_ANDROID) |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4254 | /*data_url_as_string=*/std::string(), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4255 | #endif |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4256 | /*is_browser_initiated=*/!params.is_renderer_initiated, |
Khushal Sagar | 71837ad8 | 2024-08-09 18:03:15 | [diff] [blame] | 4257 | /*has_ua_visual_transition*/ false, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4258 | /*document_ukm_source_id=*/ukm::kInvalidSourceId, |
Jiewei Qian | 0406fc0 | 2020-03-09 06:02:07 | [diff] [blame] | 4259 | node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4260 | /*force_enabled_origin_trials=*/std::vector<std::string>(), |
| 4261 | /*origin_agent_cluster=*/false, |
| 4262 | /*origin_agent_cluster_left_as_default=*/true, |
| 4263 | /*enabled_client_hints=*/ |
| 4264 | std::vector<network::mojom::WebClientHintsType>(), |
Liam Brady | 38b8456 | 2024-03-07 22:11:26 | [diff] [blame] | 4265 | /*is_cross_site_cross_browsing_context_group=*/false, |
| 4266 | /*should_have_sticky_user_activation=*/false, |
| 4267 | /*old_page_info=*/nullptr, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4268 | /*http_response_code=*/-1, |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4269 | blink::mojom::NavigationApiHistoryEntryArrays::New(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4270 | /*early_hints_preloaded_resources=*/std::vector<GURL>(), |
Clark DuVall | 8ee487a2 | 2021-11-10 02:25:58 | [diff] [blame] | 4271 | // This timestamp will be populated when the commit IPC is sent. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4272 | /*commit_sent=*/base::TimeTicks(), /*srcdoc_value=*/std::string(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4273 | /*should_load_data_url=*/false, |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 4274 | /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4275 | /*reduced_accept_language=*/std::string(), |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 4276 | /*navigation_delivery_type=*/ |
Khushal Sagar | 7b26135c6 | 2022-11-08 20:25:42 | [diff] [blame] | 4277 | network::mojom::NavigationDeliveryType::kDefault, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4278 | /*view_transition_state=*/std::nullopt, |
| 4279 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
sbingler | a07ae73 | 2022-12-02 20:49:05 | [diff] [blame] | 4280 | /*modified_runtime_features=*/ |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 4281 | base::flat_map<::blink::mojom::RuntimeFeature, bool>(), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4282 | /*fenced_frame_properties=*/std::nullopt, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 4283 | /*not_restored_reasons=*/nullptr, |
Chris Fredrickson | 9ffdf5b | 2024-07-09 20:05:09 | [diff] [blame] | 4284 | /*load_with_storage_access=*/ |
| 4285 | net::StorageAccessApiStatus::kNone, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4286 | /*browsing_context_group_info=*/std::nullopt, |
Nan Lin | da394ba | 2023-11-03 21:17:37 | [diff] [blame] | 4287 | /*lcpp_hint=*/nullptr, blink::CreateDefaultRendererContentSettings(), |
Kyra | 800d1d01 | 2024-03-27 17:05:48 | [diff] [blame] | 4288 | /*cookie_deprecation_label=*/std::nullopt, |
Khushal Sagar | 81f8981 | 2024-06-11 13:40:31 | [diff] [blame] | 4289 | /*visited_link_salt=*/std::nullopt, |
Thomas Nguyen | 42fa4c2 | 2024-11-06 14:29:16 | [diff] [blame] | 4290 | /*local_surface_id=*/std::nullopt, |
Aldo Culquicondor | 17b73f7 | 2025-01-29 21:02:17 | [diff] [blame] | 4291 | node->current_frame_host()->GetCachedPermissionStatuses(), |
Charlie Reis | cdb79834 | 2025-02-14 22:04:46 | [diff] [blame] | 4292 | /*should_skip_screentshot=*/false, |
Alex Moshchuk | d0d759c2 | 2025-05-09 18:18:35 | [diff] [blame] | 4293 | /*force_new_document_sequence_number=*/false, |
| 4294 | /*navigation_metrics_token=*/base::UnguessableToken::Create()); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 4295 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4296 | if (ValidateDataURLAsString(params.data_url_as_string)) { |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 4297 | commit_params->data_url_as_string = params.data_url_as_string->as_string(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4298 | } |
| 4299 | #endif |
| 4300 | |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4301 | commit_params->was_activated = params.was_activated; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4302 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4303 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4304 | node, params.suggested_system_entropy); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4305 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4306 | // extra_headers in params are \n separated; NavigationRequests want \r\n. |
| 4307 | std::string extra_headers_crlf; |
| 4308 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4309 | |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 4310 | auto navigation_request = NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4311 | node, std::move(common_params), std::move(commit_params), |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 4312 | !params.is_renderer_initiated, params.was_opener_suppressed, |
Hiroshige Hayashizaki | f07ad781 | 2023-05-10 02:26:09 | [diff] [blame] | 4313 | params.initiator_frame_token, params.initiator_process_id, |
| 4314 | extra_headers_crlf, frame_entry, entry, params.is_form_submission, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 4315 | params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4316 | params.impression, params.initiator_activation_and_ad_status, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 4317 | params.is_pdf, is_embedder_initiated_fenced_frame_navigation, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 4318 | is_container_initiated, params.has_rel_opener, storage_access_api_status, |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 4319 | embedder_shared_storage_context); |
Liam Brady | edb866e | 2025-01-22 22:28:13 | [diff] [blame] | 4320 | |
| 4321 | if (!navigation_request) { |
| 4322 | return nullptr; |
| 4323 | } |
| 4324 | |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4325 | navigation_request->set_from_download_cross_origin_redirect( |
| 4326 | params.from_download_cross_origin_redirect); |
W. James MacLean | 00568d7 | 2022-02-24 19:36:55 | [diff] [blame] | 4327 | navigation_request->set_force_new_browsing_instance( |
| 4328 | params.force_new_browsing_instance); |
Steve Kobes | 8d111ba | 2024-11-12 15:23:51 | [diff] [blame] | 4329 | navigation_request->set_force_new_compositor(params.force_new_compositor); |
Mustafa Emre Acer | 1001b3d | 2024-07-09 23:10:43 | [diff] [blame] | 4330 | if (params.force_no_https_upgrade) { |
| 4331 | navigation_request->set_force_no_https_upgrade(); |
| 4332 | } |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4333 | return navigation_request; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | std::unique_ptr<NavigationRequest> |
| 4337 | NavigationControllerImpl::CreateNavigationRequestFromEntry( |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4338 | FrameTreeNode* frame_tree_node, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4339 | NavigationEntryImpl* entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4340 | FrameNavigationEntry* frame_entry, |
| 4341 | ReloadType reload_type, |
| 4342 | bool is_same_document_history_load, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4343 | bool is_history_navigation_in_new_child_frame, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4344 | const std::optional<blink::LocalFrameToken>& initiator_frame_token, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4345 | int initiator_process_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4346 | base::TimeTicks actual_navigation_start, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4347 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4348 | soft_navigation_heuristics_task_id) { |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 4349 | DCHECK(frame_entry); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4350 | GURL dest_url = frame_entry->url(); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4351 | // We should never navigate to an existing initial NavigationEntry that is the |
| 4352 | // initial NavigationEntry for the initial empty document that hasn't been |
| 4353 | // overridden by the synchronous about:blank commit, to preserve previous |
| 4354 | // behavior where trying to reload when the main frame is on the initial empty |
| 4355 | // document won't result in a navigation. |
| 4356 | // See also https://crbug.com/1277414. |
| 4357 | DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank()); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 4358 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4359 | Referrer dest_referrer = frame_entry->referrer(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4360 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 4361 | if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) { |
| 4362 | // Avoid starting any new navigations since this node is now preparing for |
| 4363 | // attaching an inner delegate. |
| 4364 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 4365 | } |
| 4366 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 4367 | if (!IsValidURLForNavigation(frame_tree_node, entry->GetVirtualURL(), |
| 4368 | dest_url)) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4369 | return nullptr; |
| 4370 | } |
| 4371 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4372 | // This will be used to set the Navigation Timing API navigationStart |
| 4373 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 4374 | // "Open link in new tab"). If the navigation must wait on the current |
| 4375 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 4376 | // will be updated when the BeforeUnload ack is received. |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4377 | base::TimeTicks navigation_start = base::TimeTicks::Now(); |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4378 | const auto navigation_start_system_entropy = |
| 4379 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4380 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4381 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4382 | // Look for a pending commit that is to another document in this |
| 4383 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 4384 | // current URL by the time this navigation commits. |
| 4385 | bool has_pending_cross_document_commit = |
| 4386 | frame_tree_node->render_manager() |
| 4387 | ->HasPendingCommitForCrossDocumentNavigation(); |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4388 | bool is_currently_error_page = |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 4389 | frame_tree_node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4390 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4391 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4392 | /*old_url=*/frame_tree_node->current_url(), |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4393 | /*new_url=*/dest_url, reload_type, entry, frame_entry, |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4394 | has_pending_cross_document_commit, is_currently_error_page, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4395 | is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 4396 | /*is_embedder_initiated_fenced_frame_navigation=*/false, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4397 | /*is_unfenced_top_navigation=*/false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4398 | |
| 4399 | // A form submission may happen here if the navigation is a |
| 4400 | // back/forward/reload navigation that does a form resubmission. |
| 4401 | scoped_refptr<network::ResourceRequestBody> request_body; |
| 4402 | std::string post_content_type; |
Alison Gale | d94ce4f | 2024-04-22 15:20:39 | [diff] [blame] | 4403 | // TODO(crbug.com/41440869) Store |is_form_submission| in the history |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4404 | // entry. This way, it could be directly retrieved here. Right now, it is only |
| 4405 | // partially recovered when request.method == "POST" and request.body exists. |
| 4406 | bool is_form_submission = false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4407 | if (frame_entry->method() == "POST") { |
| 4408 | request_body = frame_entry->GetPostData(&post_content_type); |
| 4409 | // Might have a LF at end. |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 4410 | post_content_type = std::string( |
| 4411 | base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4412 | is_form_submission = !!request_body; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | // Create the NavigationParams based on |entry| and |frame_entry|. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4416 | blink::mojom::CommonNavigationParamsPtr common_params = |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4417 | entry->ConstructCommonNavigationParams( |
| 4418 | *frame_entry, request_body, dest_url, |
| 4419 | blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy), |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4420 | navigation_type, actual_navigation_start, navigation_start, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 4421 | /*input_start=*/base::TimeTicks()); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4422 | common_params->is_history_navigation_in_new_child_frame = |
Arthur Hemery | bee4a75 | 2019-05-29 10:50:55 | [diff] [blame] | 4423 | is_history_navigation_in_new_child_frame; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4424 | |
| 4425 | // TODO(clamy): |intended_as_new_entry| below should always be false once |
| 4426 | // Reload no longer leads to this being called for a pending NavigationEntry |
| 4427 | // of index -1. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4428 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4429 | entry->ConstructCommitNavigationParams( |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4430 | *frame_entry, common_params->url, common_params->method, |
| 4431 | entry->GetSubframeUniqueNames(frame_tree_node), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4432 | GetPendingEntryIndex() == -1 /* intended_as_new_entry */, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 4433 | GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(), |
Liam Brady | d2a41e15 | 2022-07-19 13:58:48 | [diff] [blame] | 4434 | frame_tree_node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4435 | frame_tree_node->AncestorOrSelfHasCSPEE(), |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4436 | navigation_start_system_entropy, soft_navigation_heuristics_task_id); |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4437 | commit_params->post_content_type = post_content_type; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4438 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4439 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4440 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
Thomas Nguyen | 42fa4c2 | 2024-11-06 14:29:16 | [diff] [blame] | 4441 | commit_params->initial_permission_statuses = |
| 4442 | frame_tree_node->current_frame_host()->GetCachedPermissionStatuses(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4443 | |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4444 | if (common_params->url.IsAboutSrcdoc()) { |
| 4445 | // TODO(wjmaclean): initialize this in NavigationRequest's constructor |
| 4446 | // instead. |
W. James MacLean | 81b8d01f | 2022-01-25 20:50:59 | [diff] [blame] | 4447 | commit_params->srcdoc_value = frame_tree_node->srcdoc_value(); |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4448 | } |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4449 | const bool is_browser_initiated = !initiator_frame_token; |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 4450 | return NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4451 | frame_tree_node, std::move(common_params), std::move(commit_params), |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4452 | is_browser_initiated, false /* was_opener_suppressed */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4453 | initiator_frame_token, initiator_process_id, entry->extra_headers(), |
| 4454 | frame_entry, entry, is_form_submission, nullptr /* navigation_ui_data */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4455 | std::nullopt /* impression */, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4456 | blink::mojom::NavigationInitiatorActivationAndAdStatus:: |
| 4457 | kDidNotStartWithTransientActivation, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 4458 | false /* is_pdf */); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4459 | } |
| 4460 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4461 | void NavigationControllerImpl::NotifyNavigationEntryCommitted( |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4462 | LoadCommittedDetails* details) { |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 4463 | details->entry = GetLastCommittedEntry(); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4464 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 4465 | // We need to notify the ssl_manager_ before the WebContents so the |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4466 | // location bar will have up-to-date information about the security style |
| 4467 | // when it wants to draw. See http://crbug.com/11157 |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 4468 | ssl_manager_.DidCommitProvisionalLoad(*details); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4469 | |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4470 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 4471 | delegate_->NotifyNavigationEntryCommitted(*details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4472 | } |
| 4473 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4474 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4475 | size_t NavigationControllerImpl::max_entry_count() { |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4476 | if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4477 | return max_entry_count_for_testing_; |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 4478 | return blink::kMaxSessionHistoryEntries; |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4479 | } |
| 4480 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4481 | void NavigationControllerImpl::SetActive(bool is_active) { |
[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 4482 | if (is_active && needs_reload_) |
| 4483 | LoadIfNecessary(); |
Khushal Sagar | ace0e734 | 2024-08-07 21:34:21 | [diff] [blame] | 4484 | |
| 4485 | if (frame_tree_->is_primary(); |
| 4486 | auto* cache = GetNavigationEntryScreenshotCache()) { |
| 4487 | cache->SetVisible(is_active); |
| 4488 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4489 | } |
| 4490 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4491 | void NavigationControllerImpl::LoadIfNecessary() { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4492 | base::TimeTicks actual_navigation_start = base::TimeTicks::Now(); |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 4493 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending", |
| 4494 | !!pending_entry_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4495 | if (!needs_reload_) |
| 4496 | return; |
| 4497 | |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 4498 | UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType", |
| 4499 | needs_reload_type_); |
| 4500 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4501 | // Calling Reload() results in ignoring state, and not loading. |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 4502 | // Explicitly use NavigateToExistingPendingEntry so that the renderer uses the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4503 | // cached state. |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 4504 | if (entry_replaced_by_post_commit_error_) { |
| 4505 | // If the current entry is a post commit error, we reload the entry it |
| 4506 | // replaced instead. We leave the error entry in place until a commit |
| 4507 | // replaces it, but the pending entry points to the original entry in the |
| 4508 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 4509 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 4510 | // Note that this handling is similar to |
| 4511 | // `NavigationControllerImpl::Reload()`. |
| 4512 | pending_entry_ = entry_replaced_by_post_commit_error_.get(); |
| 4513 | pending_entry_index_ = GetCurrentEntryIndex(); |
| 4514 | NavigateToExistingPendingEntry( |
| 4515 | ReloadType::NONE, |
| 4516 | /*initiator_rfh=*/nullptr, |
| 4517 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4518 | /*navigation_api_key=*/nullptr, actual_navigation_start); |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 4519 | } else if (pending_entry_) { |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4520 | NavigateToExistingPendingEntry( |
| 4521 | ReloadType::NONE, |
| 4522 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4523 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4524 | /*navigation_api_key=*/nullptr, actual_navigation_start); |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4525 | } else if (!GetLastCommittedEntry() |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4526 | ->IsInitialEntryNotForSynchronousAboutBlank()) { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4527 | pending_entry_ = entries_[last_committed_entry_index_].get(); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4528 | pending_entry_index_ = last_committed_entry_index_; |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4529 | NavigateToExistingPendingEntry( |
| 4530 | ReloadType::NONE, |
| 4531 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4532 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 4533 | /*navigation_api_key=*/nullptr, actual_navigation_start); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4534 | } else { |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4535 | // We should never navigate to an existing initial NavigationEntry that is |
| 4536 | // the initial NavigationEntry for the initial empty document that hasn't |
| 4537 | // been overridden by the synchronous about:blank commit, to preserve |
| 4538 | // legacy behavior where trying to reload when the main frame is on the |
| 4539 | // initial empty document won't result in a navigation. See also |
| 4540 | // https://crbug.com/1277414. If there is something to reload, the |
| 4541 | // successful reload will clear the |needs_reload_| flag. Otherwise, just do |
| 4542 | // it here. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4543 | needs_reload_ = false; |
| 4544 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4545 | } |
| 4546 | |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4547 | base::WeakPtr<NavigationHandle> |
| 4548 | NavigationControllerImpl::LoadPostCommitErrorPage( |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4549 | RenderFrameHost* render_frame_host, |
| 4550 | const GURL& url, |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 4551 | const std::string& error_page_html) { |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4552 | RenderFrameHostImpl* rfhi = |
| 4553 | static_cast<RenderFrameHostImpl*>(render_frame_host); |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 4554 | std::unique_ptr<NavigationRequest> navigation_request = |
| 4555 | CreateNavigationRequestForErrorPage(rfhi, url, error_page_html, |
| 4556 | /*is_post_commit_error_page=*/true); |
| 4557 | if (!navigation_request) { |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4558 | return nullptr; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 4559 | } |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 4560 | |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4561 | FrameTreeNode* node = rfhi->frame_tree_node(); |
Charlie Reis | 09952ee | 2022-12-08 16:35:07 | [diff] [blame] | 4562 | node->TakeNavigationRequest(std::move(navigation_request)); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4563 | DCHECK(node->navigation_request()); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4564 | |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 4565 | // TODO(crbug.com/406729265): LoadPostCommitErrorPage() does not initiate a |
| 4566 | // navigation via Navigator::Navigate(). We should fix that, so that |
| 4567 | // post-commit error page navigations don't bypass other important checks in |
| 4568 | // this function. |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4569 | // Calling BeginNavigation may destroy the NavigationRequest. |
| 4570 | base::WeakPtr<NavigationRequest> created_navigation_request( |
| 4571 | node->navigation_request()->GetWeakPtr()); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4572 | node->navigation_request()->BeginNavigation(); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4573 | return created_navigation_request; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4574 | } |
| 4575 | |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 4576 | void NavigationControllerImpl::NavigateFrameToErrorPage( |
| 4577 | RenderFrameHostImpl* render_frame_host_impl, |
| 4578 | const GURL& url, |
| 4579 | const std::string& error_page_html) { |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 4580 | std::unique_ptr<NavigationRequest> navigation_request = |
| 4581 | CreateNavigationRequestForErrorPage(render_frame_host_impl, url, |
| 4582 | error_page_html, |
| 4583 | /*is_post_commit_error_page=*/false); |
| 4584 | if (!navigation_request) { |
| 4585 | return; |
| 4586 | } |
| 4587 | |
| 4588 | FrameTreeNode* node = render_frame_host_impl->frame_tree_node(); |
| 4589 | node->navigator().Navigate(std::move(navigation_request), ReloadType::NONE); |
| 4590 | } |
| 4591 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4592 | void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4593 | EntryChangedDetails det; |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 4594 | det.changed_entry = entry; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4595 | det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry)); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 4596 | delegate_->NotifyNavigationEntryChanged(det); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4597 | } |
| 4598 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4599 | void NavigationControllerImpl::FinishRestore(int selected_index, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4600 | RestoreType type) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4601 | // TODO(crbug.com/40816356): Don't allow an index of -1, which would |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4602 | // represent a no-committed-entry state. |
| 4603 | DCHECK(selected_index >= -1 && selected_index < GetEntryCount()); |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4604 | ConfigureEntriesForRestore(&entries_, type); |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4605 | // TODO(crbug.com/40816356): This will be pointing to the wrong entry |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4606 | // if `entries_` contains pre-existing entries from the NavigationController |
| 4607 | // before restore, which would not be removed and will be at the front of the |
| 4608 | // entries list, causing the index to be off by the amount of pre-existing |
| 4609 | // entries in the list. Fix this to point to the correct entry. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4610 | last_committed_entry_index_ = selected_index; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4611 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 4612 | |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 4613 | void NavigationControllerImpl::DiscardNonCommittedEntries() { |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 4614 | DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */); |
| 4615 | } |
| 4616 | |
| 4617 | void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails( |
| 4618 | LoadCommittedDetails* commit_details) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4619 | // Avoid sending a notification if there is nothing to discard. |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4620 | // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help |
| 4621 | // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570. |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 4622 | if (!pending_entry_ && failed_pending_entry_id_ == 0) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4623 | return; |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4624 | } |
avi | 45a7253 | 2015-04-07 21:01:45 | [diff] [blame] | 4625 | DiscardPendingEntry(false); |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 4626 | |
| 4627 | if (!delegate_) |
| 4628 | return; |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4629 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | b12eb22 | 2013-09-10 00:11:48 | [diff] [blame] | 4630 | } |
| 4631 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 4632 | int NavigationControllerImpl::GetEntryIndexWithUniqueID( |
| 4633 | int nav_entry_id) const { |
| 4634 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
| 4635 | if (entries_[i]->GetUniqueID() == nav_entry_id) |
| 4636 | return i; |
| 4637 | } |
| 4638 | return -1; |
| 4639 | } |
| 4640 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4641 | void NavigationControllerImpl::InsertEntriesFrom( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4642 | NavigationControllerImpl* source, |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4643 | int max_index) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4644 | DCHECK_LE(max_index, source->GetEntryCount()); |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4645 | NavigationEntryRestoreContextImpl context; |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4646 | for (int i = 0; i < max_index; i++) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4647 | // Normally, cloning a NavigationEntryImpl results in sharing |
| 4648 | // FrameNavigationEntries between the original and the clone. However, when |
| 4649 | // cloning from a different NavigationControllerImpl, we want to fork the |
| 4650 | // FrameNavigationEntries. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4651 | entries_.insert(entries_.begin() + i, |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4652 | source->entries_[i]->CloneWithoutSharing(&context)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4653 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4654 | DCHECK_GE(entries_.size(), 1u); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4655 | DCHECK(pending_entry_index_ == -1 || |
| 4656 | pending_entry_ == GetEntryAtIndex(pending_entry_index_)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4657 | } |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4658 | |
| 4659 | void NavigationControllerImpl::SetGetTimestampCallbackForTest( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 4660 | const base::RepeatingCallback<base::Time()>& get_timestamp_callback) { |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4661 | get_timestamp_callback_ = get_timestamp_callback; |
| 4662 | } |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4663 | |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4664 | // History manipulation intervention: |
| 4665 | void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4666 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4667 | bool previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4668 | bool is_renderer_initiated, |
| 4669 | ukm::SourceId previous_page_load_ukm_source_id) { |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4670 | // Note that for a subframe, |
| 4671 | // previous_document_had_history_intervention_activation is true if the |
Shivani Sharma | 712d5d7 | 2019-04-16 21:56:45 | [diff] [blame] | 4672 | // gesture happened in any subframe (propagated to main frame) or in the main |
| 4673 | // frame itself. |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4674 | if (replace_entry || previous_document_had_history_intervention_activation || |
shivanigithub | cceeacf | 2020-03-06 20:00:27 | [diff] [blame] | 4675 | !is_renderer_initiated) { |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4676 | return; |
| 4677 | } |
| 4678 | if (last_committed_entry_index_ == -1) |
| 4679 | return; |
| 4680 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4681 | SetSkippableForSameDocumentEntries(last_committed_entry_index_, true); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4682 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4683 | // Log UKM with the URL we are navigating away from. |
| 4684 | ukm::builders::HistoryManipulationIntervention( |
| 4685 | previous_page_load_ukm_source_id) |
| 4686 | .Record(ukm::UkmRecorder::Get()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4687 | } |
| 4688 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4689 | void NavigationControllerImpl::SetSkippableForSameDocumentEntries( |
| 4690 | int reference_index, |
| 4691 | bool skippable) { |
| 4692 | auto* reference_entry = GetEntryAtIndex(reference_index); |
| 4693 | reference_entry->set_should_skip_on_back_forward_ui(skippable); |
| 4694 | |
| 4695 | int64_t document_sequence_number = |
| 4696 | reference_entry->root_node()->frame_entry->document_sequence_number(); |
| 4697 | for (int index = 0; index < GetEntryCount(); index++) { |
| 4698 | auto* entry = GetEntryAtIndex(index); |
| 4699 | if (entry->root_node()->frame_entry->document_sequence_number() == |
| 4700 | document_sequence_number) { |
| 4701 | entry->set_should_skip_on_back_forward_ui(skippable); |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4706 | std::unique_ptr<NavigationControllerImpl::PendingEntryRef> |
| 4707 | NavigationControllerImpl::ReferencePendingEntry() { |
| 4708 | DCHECK(pending_entry_); |
| 4709 | auto pending_entry_ref = |
| 4710 | std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr()); |
| 4711 | pending_entry_refs_.insert(pending_entry_ref.get()); |
| 4712 | return pending_entry_ref; |
| 4713 | } |
| 4714 | |
| 4715 | void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) { |
| 4716 | // Ignore refs that don't correspond to the current pending entry. |
| 4717 | auto it = pending_entry_refs_.find(ref); |
| 4718 | if (it == pending_entry_refs_.end()) |
| 4719 | return; |
| 4720 | pending_entry_refs_.erase(it); |
| 4721 | |
| 4722 | if (!pending_entry_refs_.empty()) |
| 4723 | return; |
| 4724 | |
| 4725 | // The pending entry may be deleted before the last PendingEntryRef. |
| 4726 | if (!pending_entry_) |
| 4727 | return; |
| 4728 | |
| 4729 | // We usually clear the pending entry when the matching NavigationRequest |
| 4730 | // fails, so that an arbitrary URL isn't left visible above a committed page. |
| 4731 | // |
| 4732 | // However, we do preserve the pending entry in some cases, such as on the |
| 4733 | // initial navigation of an unmodified blank tab. We also allow the delegate |
| 4734 | // to say when it's safe to leave aborted URLs in the omnibox, to let the |
| 4735 | // user edit the URL and try again. This may be useful in cases that the |
| 4736 | // committed page cannot be attacker-controlled. In these cases, we still |
| 4737 | // allow the view to clear the pending entry and typed URL if the user |
| 4738 | // requests (e.g., hitting Escape with focus in the address bar). |
| 4739 | // |
| 4740 | // Do not leave the pending entry visible if it has an invalid URL, since this |
| 4741 | // might be formatted in an unexpected or unsafe way. |
| 4742 | // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824. |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4743 | bool should_preserve_entry = |
| 4744 | (pending_entry_ == GetVisibleEntry()) && |
| 4745 | pending_entry_->GetURL().is_valid() && |
| 4746 | (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs()); |
| 4747 | if (should_preserve_entry) |
| 4748 | return; |
| 4749 | |
| 4750 | DiscardPendingEntry(true); |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4751 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4752 | } |
| 4753 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4754 | std::unique_ptr<PolicyContainerPolicies> |
| 4755 | NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry( |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4756 | RenderFrameHostImpl* rfh, |
| 4757 | bool is_same_document, |
Antonio Sartori | b8addf6 | 2024-09-16 07:59:21 | [diff] [blame] | 4758 | bool navigation_encountered_error, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4759 | const GURL& url) { |
Antonio Sartori | b8addf6 | 2024-09-16 07:59:21 | [diff] [blame] | 4760 | if (navigation_encountered_error) { |
| 4761 | // We should never reload the policy container of an error page from |
| 4762 | // history, see https://crbug.com/364773822. |
| 4763 | return nullptr; |
| 4764 | } |
| 4765 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4766 | if (is_same_document) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4767 | DCHECK(GetLastCommittedEntry()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4768 | FrameNavigationEntry* previous_frame_entry = |
| 4769 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
| 4770 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4771 | // TODO(crbug.com/40467594): Remove this nullptr check when we can |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4772 | // ensure we always have a FrameNavigationEntry here. |
| 4773 | if (!previous_frame_entry) |
| 4774 | return nullptr; |
| 4775 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4776 | const PolicyContainerPolicies* previous_policies = |
| 4777 | previous_frame_entry->policy_container_policies(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4778 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4779 | if (!previous_policies) |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4780 | return nullptr; |
| 4781 | |
| 4782 | // Make a copy of the policy container for the new FrameNavigationEntry. |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4783 | return previous_policies->ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4784 | } |
| 4785 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4786 | return rfh->policy_container_host()->policies().ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4787 | } |
| 4788 | |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4789 | void NavigationControllerImpl::BroadcastHistoryIndexAndLength() { |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4790 | OPTIONAL_TRACE_EVENT2( |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4791 | "content", "NavigationControllerImpl::BroadcastHistoryIndexAndLength", |
| 4792 | "history_index", GetLastCommittedEntryIndex(), "history_length", |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4793 | GetEntryCount()); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4794 | |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4795 | int history_index = GetLastCommittedEntryIndex(); |
Kevin McNee | 7705fe8 | 2024-11-07 18:56:31 | [diff] [blame] | 4796 | int history_length = GetEntryCount(); |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4797 | auto callback = [history_index, history_length](RenderViewHostImpl* rvh) { |
Kevin McNee | 7705fe8 | 2024-11-07 18:56:31 | [diff] [blame] | 4798 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) { |
Charlie Reis | 99b2eba2 | 2025-01-31 19:18:57 | [diff] [blame] | 4799 | broadcast->SetHistoryIndexAndLength(history_index, history_length); |
Kevin McNee | 7705fe8 | 2024-11-07 18:56:31 | [diff] [blame] | 4800 | } |
| 4801 | }; |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4802 | frame_tree_->root()->render_manager()->ExecutePageBroadcastMethod(callback); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | void NavigationControllerImpl::DidAccessInitialMainDocument() { |
| 4806 | // We may have left a failed browser-initiated navigation in the address bar |
| 4807 | // to let the user edit it and try again. Clear it now that content might |
| 4808 | // show up underneath it. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4809 | if (!frame_tree_->IsLoadingIncludingInnerFrameTrees() && GetPendingEntry()) |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4810 | DiscardPendingEntry(false); |
| 4811 | |
| 4812 | // Update the URL display. |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4813 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | void NavigationControllerImpl::UpdateStateForFrame( |
| 4817 | RenderFrameHostImpl* rfhi, |
| 4818 | const blink::PageState& page_state) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4819 | OPTIONAL_TRACE_EVENT1("content", |
| 4820 | "NavigationControllerImpl::UpdateStateForFrame", |
| 4821 | "render_frame_host", rfhi); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4822 | // The state update affects the last NavigationEntry associated with the given |
| 4823 | // |render_frame_host|. This may not be the last committed NavigationEntry (as |
| 4824 | // in the case of an UpdateState from a frame being swapped out). We track |
| 4825 | // which entry this is in the RenderFrameHost's nav_entry_id. |
| 4826 | NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id()); |
| 4827 | if (!entry) |
| 4828 | return; |
| 4829 | |
| 4830 | FrameNavigationEntry* frame_entry = |
| 4831 | entry->GetFrameEntry(rfhi->frame_tree_node()); |
| 4832 | if (!frame_entry) |
| 4833 | return; |
| 4834 | |
| 4835 | // The SiteInstance might not match if we do a cross-process navigation with |
| 4836 | // replacement (e.g., auto-subframe), in which case the swap out of the old |
| 4837 | // RenderFrameHost runs in the background after the old FrameNavigationEntry |
| 4838 | // has already been replaced and destroyed. |
| 4839 | if (frame_entry->site_instance() != rfhi->GetSiteInstance()) |
| 4840 | return; |
| 4841 | |
| 4842 | if (page_state == frame_entry->page_state()) |
| 4843 | return; // Nothing to update. |
| 4844 | |
| 4845 | DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState."; |
| 4846 | |
| 4847 | // The document_sequence_number and item_sequence_number recorded in the |
| 4848 | // FrameNavigationEntry should not differ from the one coming with the update, |
| 4849 | // since it must come from the same document. Do not update it if a difference |
| 4850 | // is detected, as this indicates that |frame_entry| is not the correct one. |
| 4851 | blink::ExplodedPageState exploded_state; |
| 4852 | if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state)) |
| 4853 | return; |
| 4854 | |
| 4855 | if (exploded_state.top.document_sequence_number != |
| 4856 | frame_entry->document_sequence_number() || |
| 4857 | exploded_state.top.item_sequence_number != |
| 4858 | frame_entry->item_sequence_number()) { |
| 4859 | return; |
| 4860 | } |
| 4861 | |
| 4862 | frame_entry->SetPageState(page_state); |
| 4863 | NotifyEntryChanged(entry); |
| 4864 | } |
| 4865 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4866 | namespace { |
| 4867 | |
| 4868 | // The caller is responsible for ensuring the entry is same-origin to the |
| 4869 | // origin to be committed. |
| 4870 | blink::mojom::NavigationApiHistoryEntryPtr ToNavigationApiHistoryEntry( |
| 4871 | FrameNavigationEntry* frame_entry, |
| 4872 | int64_t pending_document_sequence_number) { |
| 4873 | blink::ExplodedPageState exploded_state; |
| 4874 | if (!blink::DecodePageState(frame_entry->page_state().ToEncodedData(), |
| 4875 | &exploded_state)) { |
| 4876 | return nullptr; |
| 4877 | } |
| 4878 | blink::ExplodedFrameState frame_state = exploded_state.top; |
| 4879 | |
| 4880 | // If the document represented by this FNE hid its full url from appearing in |
| 4881 | // a referrer via a "no-referrer" or "origin" referrer policy, censor the url |
| 4882 | // in the navigation API as well (unless we're navigating to that document). |
| 4883 | std::u16string url; |
| 4884 | if (pending_document_sequence_number == |
| 4885 | frame_entry->document_sequence_number() || |
| 4886 | !frame_entry->protect_url_in_navigation_api()) { |
| 4887 | url = frame_state.url_string.value_or(std::u16string()); |
| 4888 | } |
| 4889 | |
| 4890 | return blink::mojom::NavigationApiHistoryEntry::New( |
| 4891 | frame_state.navigation_api_key.value_or(std::u16string()), |
| 4892 | frame_state.navigation_api_id.value_or(std::u16string()), url, |
| 4893 | frame_state.item_sequence_number, frame_state.document_sequence_number, |
| 4894 | frame_state.navigation_api_state); |
| 4895 | } |
| 4896 | |
| 4897 | } // namespace |
| 4898 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4899 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> |
| 4900 | NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4901 | Direction direction, |
| 4902 | int entry_index, |
| 4903 | const url::Origin& pending_origin, |
| 4904 | FrameTreeNode* node, |
| 4905 | SiteInstance* site_instance, |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4906 | int64_t pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4907 | int64_t pending_document_sequence_number, |
| 4908 | int& last_index_checked) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4909 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4910 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4911 | // Don't process the initial entry. |
| 4912 | DCHECK_EQ(GetEntryCount(), 1); |
| 4913 | return entries; |
| 4914 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4915 | int offset = direction == Direction::kForward ? 1 : -1; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4916 | int64_t previous_item_sequence_number = pending_item_sequence_number; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4917 | for (int i = entry_index + offset; i >= 0 && i < GetEntryCount(); |
| 4918 | i += offset) { |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4919 | last_index_checked = i; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4920 | FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node); |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4921 | if (!frame_entry) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4922 | break; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4923 | // An entry should only appear in the navigation API entries if it is for |
| 4924 | // the same origin as the document being committed. Check the committed |
| 4925 | // origin, or if that is not available (during restore), check against the |
| 4926 | // FNE's url. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4927 | // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry() |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4928 | // once we can be sure that entries with the same ISN will never be |
| 4929 | // cross-origin. |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4930 | url::Origin frame_entry_origin = |
| 4931 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4932 | frame_entry->url(), |
| 4933 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 4934 | if (!pending_origin.IsSameOriginWith(frame_entry_origin)) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4935 | break; |
| 4936 | if (previous_item_sequence_number == frame_entry->item_sequence_number()) |
| 4937 | continue; |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4938 | if (blink::mojom::NavigationApiHistoryEntryPtr entry = |
| 4939 | ToNavigationApiHistoryEntry(frame_entry, |
| 4940 | pending_document_sequence_number)) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4941 | DCHECK(entry->url.empty() || |
| 4942 | pending_origin.CanBeDerivedFrom(GURL(entry->url))); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4943 | entries.push_back(std::move(entry)); |
| 4944 | previous_item_sequence_number = frame_entry->item_sequence_number(); |
| 4945 | } |
| 4946 | } |
| 4947 | // If |entries| was constructed by iterating backwards from |
| 4948 | // |entry_index|, it's latest-at-the-front, but the renderer will want it |
| 4949 | // earliest-at-the-front. Reverse it. |
| 4950 | if (direction == Direction::kBack) |
| 4951 | std::reverse(entries.begin(), entries.end()); |
| 4952 | return entries; |
| 4953 | } |
| 4954 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4955 | blink::mojom::NavigationApiHistoryEntryArraysPtr |
| 4956 | NavigationControllerImpl::GetNavigationApiHistoryEntryVectors( |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4957 | FrameTreeNode* node, |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4958 | NavigationRequest* request) { |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4959 | url::Origin pending_origin = request |
Lukasz Anforowicz | 435e68d | 2022-11-09 21:47:44 | [diff] [blame] | 4960 | ? request->GetOriginToCommit().value() |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4961 | : url::Origin::Create(node->current_url()); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4962 | |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4963 | scoped_refptr<SiteInstance> site_instance = |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4964 | node->current_frame_host()->GetSiteInstance(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4965 | |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4966 | // NOTE: |entry_index| is the index where this entry will commit if no |
| 4967 | // modifications are made between now and DidCommitNavigation. This is used to |
| 4968 | // walk |entries_| and determine which entries should be exposed by the |
| 4969 | // navigation API. It is important to calculate this correctly, because blink |
| 4970 | // will cancel a same-document history commit if it's not present in the |
| 4971 | // entries blink knows about. |
| 4972 | int entry_index = GetLastCommittedEntryIndex(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4973 | int64_t pending_item_sequence_number = 0; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4974 | int64_t pending_document_sequence_number = 0; |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4975 | bool will_create_new_entry = false; |
| 4976 | if (GetPendingEntryIndex() != -1) { |
| 4977 | entry_index = GetPendingEntryIndex(); |
| 4978 | if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) { |
| 4979 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
| 4980 | pending_document_sequence_number = |
| 4981 | frame_entry->document_sequence_number(); |
| 4982 | } |
| 4983 | } else if (request && |
| 4984 | !NavigationTypeUtils::IsReload( |
| 4985 | request->common_params().navigation_type) && |
| 4986 | !NavigationTypeUtils::IsHistory( |
| 4987 | request->common_params().navigation_type) && |
| 4988 | !request->common_params().should_replace_current_entry && |
| 4989 | !request->common_params() |
| 4990 | .is_history_navigation_in_new_child_frame) { |
| 4991 | will_create_new_entry = true; |
| 4992 | entry_index = GetLastCommittedEntryIndex() + 1; |
| 4993 | // Don't set pending_item_sequence_number or |
| 4994 | // pending_document_sequence_number in this case - a new unique isn/dsn will |
| 4995 | // be calculated in the renderer later. |
| 4996 | } else if (GetLastCommittedEntryIndex() != -1) { |
| 4997 | entry_index = GetLastCommittedEntryIndex(); |
| 4998 | if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) { |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4999 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 5000 | pending_document_sequence_number = |
| 5001 | frame_entry->document_sequence_number(); |
| 5002 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5003 | } |
| 5004 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 5005 | auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New(); |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 5006 | if (entry_index == -1) { |
| 5007 | // TODO(rakina): Exit early when there is no last committed entry. |
| 5008 | // Remove when InitialNavigationEntry ships. |
| 5009 | return entry_arrays; |
| 5010 | } |
| 5011 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 5012 | int backmost_index = entry_index; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 5013 | entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5014 | Direction::kBack, entry_index, pending_origin, node, site_instance.get(), |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 5015 | pending_item_sequence_number, pending_document_sequence_number, |
| 5016 | backmost_index); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5017 | |
| 5018 | // Don't populate forward entries if they will be truncated by a new entry. |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 5019 | int forwardmost_index = entry_index; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5020 | if (!will_create_new_entry) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 5021 | entry_arrays->forward_entries = |
| 5022 | PopulateSingleNavigationApiHistoryEntryVector( |
| 5023 | Direction::kForward, entry_index, pending_origin, node, |
| 5024 | site_instance.get(), pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 5025 | pending_document_sequence_number, forwardmost_index); |
| 5026 | } |
| 5027 | |
| 5028 | // If the previous entry is within the block of contiguous entries being |
| 5029 | // provided, then report it as the `previous_entry`. |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 5030 | FrameNavigationEntry* previous_entry = nullptr; |
| 5031 | if (frame_tree_->is_prerendering()) { |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 5032 | FrameTreeNodeId initiator_id = PrerenderHost::GetFromFrameTreeNode(*node) |
| 5033 | .initiator_frame_tree_node_id(); |
| 5034 | if (initiator_id) { |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 5035 | auto* initiator_node = FrameTreeNode::GloballyFindByID(initiator_id); |
| 5036 | previous_entry = initiator_node->frame_tree() |
| 5037 | .controller() |
| 5038 | .GetLastCommittedEntry() |
| 5039 | ->GetFrameEntry(initiator_node); |
| 5040 | } |
| 5041 | } else if (GetLastCommittedEntryIndex() != -1 && |
| 5042 | GetLastCommittedEntryIndex() >= backmost_index && |
| 5043 | GetLastCommittedEntryIndex() <= forwardmost_index) { |
| 5044 | previous_entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 5045 | } |
| 5046 | if (previous_entry) { |
| 5047 | url::Origin previous_entry_origin = |
| 5048 | previous_entry->committed_origin().value_or(url::Origin::Resolve( |
| 5049 | previous_entry->url(), |
| 5050 | previous_entry->initiator_origin().value_or(url::Origin()))); |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 5051 | // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry() |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 5052 | // once we can be sure that entries with the same ISN will never be |
| 5053 | // cross-origin. |
| 5054 | if (pending_origin.IsSameOriginWith(previous_entry_origin)) { |
| 5055 | entry_arrays->previous_entry = ToNavigationApiHistoryEntry( |
| 5056 | previous_entry, pending_document_sequence_number); |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 5057 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5058 | } |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 5059 | |
Nate Chapin | 4e657a47 | 2022-02-06 03:38:37 | [diff] [blame] | 5060 | return entry_arrays; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 5061 | } |
| 5062 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5063 | NavigationControllerImpl::HistoryNavigationAction |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5064 | NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5065 | FrameNavigationEntry* current_entry, |
| 5066 | FrameNavigationEntry* target_entry, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5067 | const std::string& navigation_api_key) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5068 | if (!target_entry || !target_entry->committed_origin()) |
| 5069 | return HistoryNavigationAction::kStopLooking; |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5070 | if (!current_entry->committed_origin()->IsSameOriginWith( |
| 5071 | *target_entry->committed_origin())) { |
| 5072 | return HistoryNavigationAction::kStopLooking; |
| 5073 | } |
| 5074 | |
| 5075 | // NOTE: We don't actually care between kSameDocument and |
| 5076 | // kDifferentDocument, so always use kDifferentDocument by convention. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5077 | if (target_entry->navigation_api_key() == navigation_api_key) |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5078 | return HistoryNavigationAction::kDifferentDocument; |
| 5079 | return HistoryNavigationAction::kKeepLooking; |
| 5080 | } |
| 5081 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5082 | void NavigationControllerImpl::NavigateToNavigationApiKey( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 5083 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 5084 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 5085 | soft_navigation_heuristics_task_id, |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 5086 | const std::string& key, |
| 5087 | base::TimeTicks actual_navigation_start) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 5088 | FrameTreeNode* node = initiator_rfh->frame_tree_node(); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5089 | FrameNavigationEntry* current_entry = |
| 5090 | GetLastCommittedEntry()->GetFrameEntry(node); |
Nate Chapin | 0dbc93af | 2025-03-28 17:02:52 | [diff] [blame] | 5091 | if (!current_entry || !current_entry->committed_origin()) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5092 | return; |
Nate Chapin | 0dbc93af | 2025-03-28 17:02:52 | [diff] [blame] | 5093 | } |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5094 | |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 5095 | // TODO(crbug.com/40878000): Make sure that the right task ID is passed |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 5096 | // when `navigation.traverseTo()` is called. |
| 5097 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5098 | // We want to find the nearest matching entry that is contiguously |
| 5099 | // same-instance and same-origin. Check back first, then forward. |
| 5100 | // TODO(japhet): Link spec here once it exists. |
| 5101 | for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5102 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5103 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 5104 | if (result == HistoryNavigationAction::kStopLooking) |
| 5105 | break; |
| 5106 | if (result != HistoryNavigationAction::kKeepLooking) { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 5107 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key, |
| 5108 | actual_navigation_start); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5109 | return; |
| 5110 | } |
| 5111 | } |
| 5112 | for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5113 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5114 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 5115 | if (result == HistoryNavigationAction::kStopLooking) |
| 5116 | break; |
| 5117 | if (result != HistoryNavigationAction::kKeepLooking) { |
Charlie Reis | f4d51f40 | 2025-05-23 18:00:49 | [diff] [blame] | 5118 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key, |
| 5119 | actual_navigation_start); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5120 | return; |
| 5121 | } |
| 5122 | } |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 5123 | |
| 5124 | // If we fall through to here, a matching NavigationEntry couldn't be found. |
| 5125 | // Notify the renderer that the navigation was cancelled. |
| 5126 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 5127 | key, blink::mojom::TraverseCancelledReason::kNotFound); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 5128 | } |
| 5129 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 5130 | bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 5131 | network::mojom::ReferrerPolicy referrer_policy) { |
| 5132 | return referrer_policy == network::mojom::ReferrerPolicy::kNever || |
| 5133 | referrer_policy == network::mojom::ReferrerPolicy::kOrigin; |
| 5134 | } |
| 5135 | |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 5136 | bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory( |
| 5137 | const FrameTreeNode* frame_tree_node) const { |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 5138 | // TODO(crbug.com/40176906): We may have to add portals in addition to |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 5139 | // prerender and fenced frames. This should be kept in sync with |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 5140 | // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory. |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 5141 | // The preview mode appears as prerendered page in renderers, and |
| 5142 | // GetDocument()->IsPrerendering() covers the case together. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 5143 | return frame_tree_->is_prerendering() || |
Takashi Toyoshima | 8dfc05c | 2024-01-29 21:03:51 | [diff] [blame] | 5144 | frame_tree_->page_delegate()->IsPageInPreviewMode() || |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 5145 | frame_tree_node->IsInFencedFrameTree(); |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 5146 | } |
| 5147 | |
Julie Jeongeun Kim | 0e24224 | 2022-11-30 10:45:09 | [diff] [blame] | 5148 | void NavigationControllerImpl::DidChangeReferrerPolicy( |
| 5149 | FrameTreeNode* node, |
| 5150 | network::mojom::ReferrerPolicy referrer_policy) { |
| 5151 | FrameNavigationEntry* entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 5152 | if (!entry) |
| 5153 | return; |
| 5154 | |
| 5155 | // The FrameNavigationEntry may want to change whether to protect its url |
| 5156 | // in the navigation API when the referrer policy changes. |
| 5157 | entry->set_protect_url_in_navigation_api( |
| 5158 | ShouldProtectUrlInNavigationApi(referrer_policy)); |
| 5159 | } |
| 5160 | |
Minoru Chikamune | 646eba4 | 2025-04-14 01:25:03 | [diff] [blame] | 5161 | void NavigationControllerImpl::CheckPotentialNavigationReentrancy() { |
Minoru Chikamune | 13ab4de | 2025-05-14 21:54:07 | [diff] [blame] | 5162 | if (can_be_in_navigate_to_pending_entry_) { |
Minoru Chikamune | 646eba4 | 2025-04-14 01:25:03 | [diff] [blame] | 5163 | // This DumpWithoutCrashing is an investigation code for |
| 5164 | // https://crbug.com/396998476. |
| 5165 | base::debug::DumpWithoutCrashing(); |
| 5166 | } |
| 5167 | } |
| 5168 | |
Andrew Verge | 754c70a | 2025-04-17 17:19:19 | [diff] [blame] | 5169 | std::unique_ptr<NavigationRequest> |
| 5170 | NavigationControllerImpl::CreateNavigationRequestForErrorPage( |
| 5171 | RenderFrameHostImpl* render_frame_host_impl, |
| 5172 | const GURL& url, |
| 5173 | const std::string& error_page_html, |
| 5174 | bool is_post_commit_error_page) { |
| 5175 | // Only active documents can load error pages: |
| 5176 | // - If the document is in pending deletion, the browser already committed to |
| 5177 | // destroying this RenderFrameHost so ignore loading the error page. |
| 5178 | // - If the document is in back-forward cache, it's not allowed to navigate as |
| 5179 | // it should remain frozen. Ignore the request and evict the document from |
| 5180 | // back-forward cache. |
| 5181 | // - If the document is prerendering, it can navigate but when loading error |
| 5182 | // pages, cancel prerendering. |
| 5183 | if (render_frame_host_impl->IsInactiveAndDisallowActivation( |
| 5184 | is_post_commit_error_page |
| 5185 | ? DisallowActivationReasonId::kLoadPostCommitErrorPage |
| 5186 | : DisallowActivationReasonId::kBrowserInitiatedErrorPage)) { |
| 5187 | return nullptr; |
| 5188 | } |
| 5189 | |
| 5190 | FrameTreeNode* node = render_frame_host_impl->frame_tree_node(); |
| 5191 | |
| 5192 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 5193 | blink::CreateCommonNavigationParams(); |
| 5194 | // |url| might be empty, such as when this method is called before |
| 5195 | // the frame actually committed (e.g. iframe with "src" set to a |
| 5196 | // slow-responding URL). We should rewrite the URL to about:blank in this |
| 5197 | // case, as the renderer will only think a page is an error page if it has a |
| 5198 | // non-empty unreachable URL. |
| 5199 | common_params->url = url.is_empty() ? GURL("about:blank") : url; |
| 5200 | |
| 5201 | // Loading an error page via LoadPostCommitErrorPage() performs a temporary |
| 5202 | // replacement of the entire NavigationEntry and swaps it back in later. |
| 5203 | // All other error page types should permanently replace the current entry. |
| 5204 | if (!is_post_commit_error_page) { |
| 5205 | common_params->should_replace_current_entry = true; |
| 5206 | } |
| 5207 | |
| 5208 | blink::mojom::CommitNavigationParamsPtr commit_params = |
| 5209 | blink::CreateCommitNavigationParams(); |
| 5210 | commit_params->original_url = common_params->url; |
| 5211 | |
| 5212 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 5213 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 5214 | node, blink::mojom::SystemEntropy::kNormal); |
| 5215 | |
| 5216 | // TODO(arthursonzogni): Consider providing the minimal capabilities to the |
| 5217 | // error pages. |
| 5218 | commit_params->frame_policy = node->pending_frame_policy(); |
| 5219 | |
| 5220 | std::unique_ptr<NavigationRequest> navigation_request = |
| 5221 | NavigationRequest::CreateBrowserInitiated( |
| 5222 | node, std::move(common_params), std::move(commit_params), |
| 5223 | false /* was_opener_suppressed */, "" /* extra_headers */, |
| 5224 | nullptr /* frame_entry */, nullptr /* entry */, |
| 5225 | false /* is_form_submission */, nullptr /* navigation_ui_data */, |
| 5226 | std::nullopt /* impression */, false /* is_pdf */); |
| 5227 | if (is_post_commit_error_page) { |
| 5228 | navigation_request->set_browser_initiated_error_navigation_type( |
| 5229 | NavigationRequest::BrowserInitiatedErrorNavigationType::kPostCommit); |
| 5230 | } else { |
| 5231 | navigation_request->set_browser_initiated_error_navigation_type( |
| 5232 | NavigationRequest::BrowserInitiatedErrorNavigationType::kRegular); |
| 5233 | } |
| 5234 | navigation_request->set_net_error(net::ERR_BLOCKED_BY_CLIENT); |
| 5235 | navigation_request->set_error_page_html(error_page_html); |
| 5236 | return navigation_request; |
| 5237 | } |
| 5238 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 5239 | } // namespace content |