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" |
Rakina Zata Amni | 627360d | 2022-02-24 00:53:40 | [diff] [blame] | 43 | #include "base/debug/dump_without_crashing.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 44 | #include "base/functional/bind.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include "base/logging.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 46 | #include "base/metrics/histogram_macros.h" |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 47 | #include "base/numerics/safe_conversions.h" |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 48 | #include "base/strings/escape.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 49 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 50 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 51 | #include "base/time/time.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 52 | #include "base/trace_event/optional_trace_event.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 53 | #include "base/trace_event/trace_event.h" |
Daniel Cheng | abb00686 | 2022-09-09 22:39:08 | [diff] [blame] | 54 | #include "base/types/optional_util.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 55 | #include "build/build_config.h" |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 56 | #include "cc/base/switches.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 57 | #include "content/browser/bad_message.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 58 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 59 | #include "content/browser/browser_context_impl.h" |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 60 | #include "content/browser/browser_url_handler_impl.h" |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 61 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 62 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 63 | #include "content/browser/preloading/prerender/prerender_host.h" |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 64 | #include "content/browser/process_lock.h" |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 65 | #include "content/browser/renderer_host/back_forward_cache_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 66 | #include "content/browser/renderer_host/debug_urls.h" |
| 67 | #include "content/browser/renderer_host/frame_tree.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 68 | #include "content/browser/renderer_host/frame_tree_node.h" |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 69 | #include "content/browser/renderer_host/navigation_controller_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 70 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 71 | #include "content/browser/renderer_host/navigation_entry_restore_context_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 72 | #include "content/browser/renderer_host/navigation_request.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 73 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h" |
| 74 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_manager.h" |
Khushal Sagar | 94ea2dc | 2024-08-06 17:15:26 | [diff] [blame] | 75 | #include "content/browser/renderer_host/navigation_transitions/navigation_transition_config.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 76 | #include "content/browser/renderer_host/navigator.h" |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 77 | #include "content/browser/renderer_host/page_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 78 | #include "content/browser/renderer_host/render_frame_host_delegate.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 79 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 80 | #include "content/browser/renderer_host/system_entropy_utils.h" |
Sharon Yang | d70a539 | 2021-10-26 23:06:32 | [diff] [blame] | 81 | #include "content/browser/site_info.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 82 | #include "content/browser/site_instance_impl.h" |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 83 | #include "content/common/content_constants_internal.h" |
Rakina Zata Amni | 84a859df | 2024-06-07 15:56:32 | [diff] [blame] | 84 | #include "content/common/content_navigation_policy.h" |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 85 | #include "content/common/navigation_params_utils.h" |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 86 | #include "content/common/trace_utils.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 87 | #include "content/public/browser/browser_context.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 88 | #include "content/public/browser/content_browser_client.h" |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 89 | #include "content/public/browser/disallow_activation_reason.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 90 | #include "content/public/browser/invalidate_type.h" |
[email protected] | 5b96836f | 2011-12-22 07:39:00 | [diff] [blame] | 91 | #include "content/public/browser/navigation_details.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 92 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 93 | #include "content/public/browser/render_widget_host.h" |
| 94 | #include "content/public/browser/render_widget_host_view.h" |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 95 | #include "content/public/browser/replaced_navigation_entry_data.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 96 | #include "content/public/browser/storage_partition.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 97 | #include "content/public/common/content_client.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 98 | #include "content/public/common/content_constants.h" |
toyoshim | 86e34ec | 2016-02-25 08:56:10 | [diff] [blame] | 99 | #include "content/public/common/content_features.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 100 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 101 | #include "content/public/common/url_utils.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 102 | #include "media/base/mime_util.h" |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 103 | #include "net/base/net_errors.h" |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 104 | #include "net/http/http_status_code.h" |
Shivani Sharma | 9332910 | 2019-01-24 19:44:18 | [diff] [blame] | 105 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 106 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Yue Ru Sun | 12880493 | 2020-09-30 22:19:17 | [diff] [blame] | 107 | #include "services/metrics/public/cpp/ukm_source_id.h" |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 108 | #include "services/network/public/mojom/fetch_api.mojom.h" |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 109 | #include "services/network/public/mojom/url_response_head.mojom-shared.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 110 | #include "skia/ext/platform_canvas.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 111 | #include "third_party/blink/public/common/blob/blob_utils.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 112 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 113 | #include "third_party/blink/public/common/history/session_history_constants.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 114 | #include "third_party/blink/public/common/mime_util/mime_util.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 115 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 116 | #include "third_party/blink/public/common/page_state/page_state_serialization.h" |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 117 | #include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h" |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 118 | #include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h" |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 119 | #include "third_party/blink/public/mojom/runtime_feature_state/runtime_feature.mojom.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 120 | #include "url/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 122 | namespace content { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 123 | namespace { |
| 124 | |
| 125 | // Invoked when entries have been pruned, or removed. For example, if the |
| 126 | // current entries are [google, digg, yahoo], with the current entry google, |
| 127 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 128 | void NotifyPrunedEntries(NavigationControllerImpl* nav_controller, |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 129 | int index, |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 130 | int count) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 131 | PrunedDetails details; |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 132 | details.index = index; |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 133 | details.count = count; |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 134 | nav_controller->delegate()->NotifyNavigationListPruned(details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 137 | // Configure all the NavigationEntries in entries for restore. This resets |
| 138 | // the transition type to reload and makes sure the content state isn't empty. |
| 139 | void ConfigureEntriesForRestore( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 140 | std::vector<std::unique_ptr<NavigationEntryImpl>>* entries, |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 141 | RestoreType type) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 142 | for (auto& entry : *entries) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 143 | // Use a transition type of reload so that we don't incorrectly increase |
| 144 | // the typed count. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 145 | entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 146 | entry->set_restore_type(type); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 150 | // Determines whether or not we should be carrying over a user agent override |
| 151 | // between two NavigationEntries. |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 152 | bool ShouldKeepOverride(NavigationEntry* last_entry) { |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 153 | return last_entry && last_entry->GetIsOverridingUserAgent(); |
| 154 | } |
| 155 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 156 | // Determines whether to override user agent for a navigation. |
| 157 | bool ShouldOverrideUserAgent( |
| 158 | NavigationController::UserAgentOverrideOption override_user_agent, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 159 | NavigationEntry* last_committed_entry) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 160 | switch (override_user_agent) { |
| 161 | case NavigationController::UA_OVERRIDE_INHERIT: |
| 162 | return ShouldKeepOverride(last_committed_entry); |
| 163 | case NavigationController::UA_OVERRIDE_TRUE: |
| 164 | return true; |
| 165 | case NavigationController::UA_OVERRIDE_FALSE: |
| 166 | return false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 167 | } |
Peter Boström | 8472105d | 2024-05-15 04:36:02 | [diff] [blame] | 168 | NOTREACHED_IN_MIGRATION(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 172 | // Returns true if this navigation should be treated as a reload. For e.g. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 173 | // navigating to the last committed url via the address bar or clicking on a |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 174 | // link which results in a navigation to the last committed URL (but wasn't |
| 175 | // converted to do a replacement navigation in the renderer), etc. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 176 | // |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|, |
| 177 | // |base_url_for_data_url|, |transition_type| correspond to the new navigation |
| 178 | // (i.e. the pending NavigationEntry). |last_committed_entry| is the last |
| 179 | // navigation that committed. |
| 180 | bool ShouldTreatNavigationAsReload(FrameTreeNode* node, |
| 181 | const GURL& url, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 182 | const GURL& virtual_url, |
| 183 | const GURL& base_url_for_data_url, |
| 184 | ui::PageTransition transition_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 185 | bool is_post, |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 186 | bool should_replace_current_entry, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 187 | NavigationEntryImpl* last_committed_entry) { |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 188 | // Navigations intended to do a replacement shouldn't be converted to do a |
| 189 | // reload. |
| 190 | if (should_replace_current_entry) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 191 | return false; |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 192 | // Only convert to reload if at least one navigation committed. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 193 | if (last_committed_entry->IsInitialEntry()) |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 194 | return false; |
| 195 | |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 196 | // Skip navigations initiated by external applications. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 197 | if (transition_type & ui::PAGE_TRANSITION_FROM_API) |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 198 | return false; |
| 199 | |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 200 | // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR), |
| 201 | // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations |
| 202 | // which should be treated as reloads. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 203 | bool transition_type_can_be_converted = false; |
| 204 | if (ui::PageTransitionCoreTypeIs(transition_type, |
| 205 | ui::PAGE_TRANSITION_RELOAD) && |
| 206 | (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) { |
| 207 | transition_type_can_be_converted = true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 208 | } |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 209 | if (ui::PageTransitionCoreTypeIs(transition_type, |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 210 | ui::PAGE_TRANSITION_TYPED)) { |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 211 | transition_type_can_be_converted = true; |
| 212 | } |
| 213 | if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK)) |
| 214 | transition_type_can_be_converted = true; |
| 215 | if (!transition_type_can_be_converted) |
| 216 | return false; |
| 217 | |
| 218 | // This check is required for cases like view-source:, etc. Here the URL of |
| 219 | // the navigation entry would contain the url of the page, while the virtual |
| 220 | // URL contains the full URL including the view-source prefix. |
| 221 | if (virtual_url != last_committed_entry->GetVirtualURL()) |
| 222 | return false; |
| 223 | |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 224 | // Check that the URLs match. |
| 225 | FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node); |
| 226 | // If there's no frame entry then by definition the URLs don't match. |
| 227 | if (!frame_entry) |
| 228 | return false; |
| 229 | |
| 230 | if (url != frame_entry->url()) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 231 | return false; |
| 232 | |
| 233 | // This check is required for Android WebView loadDataWithBaseURL. Apps |
| 234 | // can pass in anything in the base URL and we need to ensure that these |
| 235 | // match before classifying it as a reload. |
| 236 | if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) { |
| 237 | if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL()) |
| 238 | return false; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 239 | } |
| 240 | |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 241 | // Skip entries with SSL errors. |
| 242 | if (last_committed_entry->ssl_error()) |
| 243 | return false; |
| 244 | |
| 245 | // Don't convert to a reload when the last navigation was a POST or the new |
| 246 | // navigation is a POST. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 247 | if (frame_entry->get_has_post_data() || is_post) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 248 | return false; |
| 249 | |
| 250 | return true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 251 | } |
| 252 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 253 | std::optional<url::Origin> GetCommittedOriginForFrameEntry( |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 254 | const mojom::DidCommitProvisionalLoadParams& params, |
| 255 | NavigationRequest* request) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 256 | // Error pages commit in an opaque origin, yet have the real URL that resulted |
| 257 | // in an error as the |params.url|. Since successful reload of an error page |
| 258 | // should commit in the correct origin, setting the opaque origin on the |
| 259 | // FrameNavigationEntry will be incorrect. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 260 | if (request && request->DidEncounterError()) |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 261 | return std::nullopt; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 262 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 263 | return std::make_optional(params.origin); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 264 | } |
| 265 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 266 | bool IsValidURLForNavigation(FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 267 | const GURL& virtual_url, |
| 268 | const GURL& dest_url) { |
| 269 | // 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] | 270 | if (node->IsOutermostMainFrame() && !virtual_url.is_valid() && |
| 271 | !virtual_url.is_empty()) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 272 | LOG(WARNING) << "Refusing to load for invalid virtual URL: " |
| 273 | << virtual_url.possibly_invalid_spec(); |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | // Don't attempt to navigate to non-empty invalid URLs. |
| 278 | if (!dest_url.is_valid() && !dest_url.is_empty()) { |
| 279 | LOG(WARNING) << "Refusing to load invalid URL: " |
| 280 | << dest_url.possibly_invalid_spec(); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // The renderer will reject IPC messages with URLs longer than |
| 285 | // this limit, so don't attempt to navigate with a longer URL. |
| 286 | if (dest_url.spec().size() > url::kMaxURLChars) { |
| 287 | LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars |
| 288 | << " characters."; |
| 289 | return false; |
| 290 | } |
| 291 | |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 292 | // Reject renderer debug URLs because they should have been handled before |
| 293 | // we get to this point. This check handles renderer debug URLs |
| 294 | // that are inside a view-source: URL (e.g. view-source:chrome://kill) and |
| 295 | // provides defense-in-depth if a renderer debug URL manages to get here via |
| 296 | // some other path. We want to reject the navigation here so it doesn't |
| 297 | // violate assumptions in downstream code. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 298 | if (blink::IsRendererDebugURL(dest_url)) { |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 299 | LOG(WARNING) << "Refusing to load renderer debug URL: " |
| 300 | << dest_url.possibly_invalid_spec(); |
| 301 | return false; |
| 302 | } |
| 303 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 304 | // Guests only support navigations to known-safe schemes. This check already |
| 305 | // exists in the extensions layer, where it also dispatches proper events to |
| 306 | // the guest's embedder (see WebViewGuest::LoadURLWithParams). This check is |
| 307 | // for defense-in-depth to ensure that no other places in the codebase |
| 308 | // accidentally navigate guests to schemes such as WebUI, which is not |
| 309 | // supported. See https://crbug.com/1444221. |
| 310 | if (node->current_frame_host()->GetSiteInstance()->IsGuest()) { |
| 311 | auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance(); |
| 312 | if (!cpsp->IsWebSafeScheme(dest_url.scheme()) && |
| 313 | !dest_url.SchemeIs(url::kAboutScheme)) { |
| 314 | LOG(WARNING) << "Refusing to load unsafe URL in a guest: " |
| 315 | << dest_url.possibly_invalid_spec(); |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 320 | return true; |
| 321 | } |
| 322 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 323 | // See replaced_navigation_entry_data.h for details: this information is meant |
| 324 | // to ensure |*output_entry| keeps track of its original URL (landing page in |
| 325 | // case of server redirects) as it gets replaced (e.g. history.replaceState()), |
| 326 | // without overwriting it later, for main frames. |
| 327 | void CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 328 | NavigationEntryImpl* replaced_entry, |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 329 | NavigationEntryImpl* output_entry) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 330 | if (output_entry->GetReplacedEntryData().has_value() || |
| 331 | replaced_entry->IsInitialEntry()) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 332 | return; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 333 | } |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 334 | |
| 335 | ReplacedNavigationEntryData data; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 336 | data.first_committed_url = replaced_entry->GetURL(); |
| 337 | data.first_timestamp = replaced_entry->GetTimestamp(); |
| 338 | data.first_transition_type = replaced_entry->GetTransitionType(); |
Charlie Reis | b55438f | 2019-01-08 01:54:29 | [diff] [blame] | 339 | output_entry->set_replaced_entry_data(data); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 340 | } |
| 341 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 342 | blink::mojom::NavigationType GetNavigationType( |
| 343 | const GURL& old_url, |
| 344 | const GURL& new_url, |
| 345 | ReloadType reload_type, |
| 346 | NavigationEntryImpl* entry, |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 347 | FrameNavigationEntry* frame_entry, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 348 | bool has_pending_cross_document_commit, |
| 349 | bool is_currently_error_page, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 350 | bool is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 351 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 352 | bool is_unfenced_top_navigation) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 353 | // Reload navigations |
| 354 | switch (reload_type) { |
| 355 | case ReloadType::NORMAL: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 356 | return blink::mojom::NavigationType::RELOAD; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 357 | case ReloadType::BYPASSING_CACHE: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 358 | return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 359 | case ReloadType::NONE: |
| 360 | break; // Fall through to rest of function. |
| 361 | } |
| 362 | |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 363 | if (entry->IsRestored()) { |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 364 | return entry->GetHasPostData() |
| 365 | ? blink::mojom::NavigationType::RESTORE_WITH_POST |
| 366 | : blink::mojom::NavigationType::RESTORE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 367 | } |
| 368 | |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 369 | const bool can_be_same_document = |
| 370 | // A pending cross-document commit means this navigation will not occur in |
| 371 | // the current document, as that document would end up being replaced in |
| 372 | // the meantime. |
| 373 | !has_pending_cross_document_commit && |
| 374 | // If the current document is an error page, we should always treat it as |
| 375 | // a different-document navigation so that we'll attempt to load the |
| 376 | // document we're navigating to (and not stay in the current error page). |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 377 | !is_currently_error_page && |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 378 | // If the navigation is an embedder-initiated navigation of a fenced |
| 379 | // frame root (i.e. enters a fenced frame tree from outside), |
| 380 | // same-document navigations should be disabled because we don't want to |
| 381 | // allow information to be joined across multiple embedder-initiated |
| 382 | // fenced frame navigations (which may contain separate cross-site data). |
| 383 | !is_embedder_initiated_fenced_frame_navigation && |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 384 | // If the navigation is to _unfencedTop (i.e. escapes a fenced frame), |
| 385 | // same-document navigations should be disabled because we want to force |
| 386 | // the creation of a new browsing context group. |
| 387 | !is_unfenced_top_navigation; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 388 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 389 | // History navigations. |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 390 | if (frame_entry && frame_entry->page_state().IsValid()) { |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 391 | return can_be_same_document && is_same_document_history_load |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 392 | ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT |
| 393 | : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 394 | } |
| 395 | DCHECK(!is_same_document_history_load); |
| 396 | |
| 397 | // A same-document fragment-navigation happens when the only part of the url |
| 398 | // that is modified is after the '#' character. |
| 399 | // |
| 400 | // When modifying this condition, please take a look at: |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 401 | // FrameLoader::ShouldPerformFragmentNavigation(). |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 402 | // |
| 403 | // Note: this check is only valid for navigations that are not history |
| 404 | // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a |
| 405 | // history navigation from 'A#foo' to 'A#bar' is not a same-document |
| 406 | // navigation, but a different-document one. This is why history navigation |
| 407 | // are classified before this check. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 408 | bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) && |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 409 | frame_entry && frame_entry->method() == "GET"; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 410 | |
| 411 | // The one case where we do the wrong thing here and incorrectly choose |
| 412 | // SAME_DOCUMENT is if the navigation is browser-initiated but the document in |
| 413 | // the renderer is a frameset. All frameset navigations should be |
| 414 | // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated |
| 415 | // navigation would do the right thing, as it would send it to the browser and |
| 416 | // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get |
| 417 | // into this method). But since we can't tell that case here for browser- |
| 418 | // initiated navigations, we have to get the renderer involved. In that case |
| 419 | // the navigation would be restarted due to the renderer spending a reply of |
| 420 | // mojom::CommitResult::RestartCrossDocument. |
| 421 | |
| 422 | return can_be_same_document && is_same_doc |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 423 | ? blink::mojom::NavigationType::SAME_DOCUMENT |
| 424 | : blink::mojom::NavigationType::DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 425 | } |
| 426 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 427 | // Adjusts the original input URL if needed, to get the URL to actually load and |
| 428 | // the virtual URL, which may differ. |
| 429 | void RewriteUrlForNavigation(const GURL& original_url, |
| 430 | BrowserContext* browser_context, |
| 431 | GURL* url_to_load, |
| 432 | GURL* virtual_url, |
| 433 | bool* reverse_on_redirect) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 434 | // Allow the browser URL handler to rewrite the URL. This will, for example, |
| 435 | // remove "view-source:" from the beginning of the URL to get the URL that |
| 436 | // will actually be loaded. This real URL won't be shown to the user, just |
| 437 | // used internally. |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 438 | *url_to_load = *virtual_url = original_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 439 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 440 | url_to_load, browser_context, reverse_on_redirect); |
| 441 | } |
| 442 | |
| 443 | #if DCHECK_IS_ON() |
| 444 | // Helper sanity check function used in debug mode. |
| 445 | void ValidateRequestMatchesEntry(NavigationRequest* request, |
| 446 | NavigationEntryImpl* entry) { |
| 447 | if (request->frame_tree_node()->IsMainFrame()) { |
| 448 | DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated()); |
| 449 | DCHECK(ui::PageTransitionTypeIncludingQualifiersIs( |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 450 | ui::PageTransitionFromInt(request->common_params().transition), |
| 451 | entry->GetTransitionType())); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 452 | } |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 453 | DCHECK_EQ(request->commit_params().should_clear_history_list, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 454 | entry->should_clear_history_list()); |
| 455 | DCHECK_EQ(request->common_params().has_user_gesture, |
| 456 | entry->has_user_gesture()); |
| 457 | DCHECK_EQ(request->common_params().base_url_for_data_url, |
| 458 | entry->GetBaseURLForDataURL()); |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 459 | DCHECK_EQ(request->commit_params().can_load_local_resources, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 460 | entry->GetCanLoadLocalResources()); |
| 461 | DCHECK_EQ(request->common_params().started_from_context_menu, |
| 462 | entry->has_started_from_context_menu()); |
| 463 | |
| 464 | FrameNavigationEntry* frame_entry = |
| 465 | entry->GetFrameEntry(request->frame_tree_node()); |
| 466 | if (!frame_entry) { |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 467 | // TODO(https://crbug.com/40467594): This case should be unreachable once |
| 468 | // all subframes have FrameNavigationEntries associated with them. |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 472 | DCHECK_EQ(request->common_params().method, frame_entry->method()); |
| 473 | |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 474 | size_t redirect_size = request->commit_params().redirects.size(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 475 | if (redirect_size == frame_entry->redirect_chain().size()) { |
| 476 | for (size_t i = 0; i < redirect_size; ++i) { |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 477 | DCHECK_EQ(request->commit_params().redirects[i], |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 478 | frame_entry->redirect_chain()[i]); |
| 479 | } |
| 480 | } else { |
Peter Boström | 8472105d | 2024-05-15 04:36:02 | [diff] [blame] | 481 | NOTREACHED_IN_MIGRATION(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | #endif // DCHECK_IS_ON() |
| 485 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 486 | // Returns whether the session history NavigationRequests in |navigations| |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 487 | // would stay within the subtree of |sandboxed_initiator_rfh|. |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 488 | bool DoesSandboxNavigationStayWithinSubtree( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 489 | RenderFrameHostImpl* sandboxed_initiator_rfh, |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 490 | const std::vector<std::unique_ptr<NavigationRequest>>& navigations) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 491 | DCHECK(sandboxed_initiator_rfh); |
| 492 | DCHECK(sandboxed_initiator_rfh->IsSandboxed( |
| 493 | network::mojom::WebSandboxFlags::kTopNavigation)); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 494 | for (auto& item : navigations) { |
| 495 | bool within_subtree = false; |
| 496 | // Check whether this NavigationRequest affects a frame within the |
| 497 | // sandboxed frame's subtree by walking up the tree looking for the |
| 498 | // sandboxed frame. |
| 499 | for (auto* frame = item->frame_tree_node(); frame; |
Alexander Timin | 381e7e18 | 2020-04-28 19:04:03 | [diff] [blame] | 500 | frame = FrameTreeNode::From(frame->parent())) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 501 | if (frame == sandboxed_initiator_rfh->frame_tree_node()) { |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 502 | within_subtree = true; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | if (!within_subtree) |
| 507 | return false; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
William Liu | 12275494 | 2024-01-18 22:34:39 | [diff] [blame] | 512 | // Used for "Navigation.SessionHistoryCount" histogram. |
| 513 | enum class HistoryNavTypeForHistogram { |
| 514 | // A same-doc or cross-doc navigation of the main frame. We can only have one |
| 515 | // main frame request per `NavigationController::GoToIndex()`. |
| 516 | kMainFrame = 0, |
| 517 | // `NavigationController::GoToIndex()` creates one main frame request and |
| 518 | // one or more subframe requests. |
| 519 | kMainFrameAndSubframe, |
| 520 | // `NavigationController::GoToIndex()` creates one or more subframe requests. |
| 521 | kSubframe, |
| 522 | // Used for histogram boundary. |
| 523 | kCount |
| 524 | }; |
| 525 | |
| 526 | void CountRequests( |
| 527 | const std::vector<std::unique_ptr<NavigationRequest>>& requests, |
| 528 | int& mutable_main_frame_cnt, |
| 529 | int& mutable_subframe_cnt) { |
| 530 | for (const auto& req : requests) { |
| 531 | if (req->IsInPrimaryMainFrame()) { |
| 532 | // We can only have one main frame navigation at a time. |
| 533 | CHECK_EQ(mutable_main_frame_cnt, 0); |
| 534 | ++mutable_main_frame_cnt; |
| 535 | } else if (req->GetNavigatingFrameType() == FrameType::kSubframe) { |
| 536 | ++mutable_subframe_cnt; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // Record the number of different types of navigations as histograms. See |
| 542 | // `HistoryNavTypeForHistogram` for the types. |
| 543 | void CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions( |
| 544 | const std::vector<std::unique_ptr<NavigationRequest>>& cross_doc_requests, |
| 545 | const std::vector<std::unique_ptr<NavigationRequest>>& same_doc_requests) { |
| 546 | // We must have one request. |
| 547 | CHECK(!cross_doc_requests.empty() || !same_doc_requests.empty()); |
| 548 | |
| 549 | int main_frame_cnt = 0; |
| 550 | int subframe_cnt = 0; |
| 551 | CountRequests(cross_doc_requests, main_frame_cnt, subframe_cnt); |
| 552 | CountRequests(same_doc_requests, main_frame_cnt, subframe_cnt); |
| 553 | |
| 554 | std::optional<HistoryNavTypeForHistogram> history_nav_type; |
| 555 | if (main_frame_cnt > 0) { |
| 556 | if (subframe_cnt == 0) { |
| 557 | history_nav_type = HistoryNavTypeForHistogram::kMainFrame; |
| 558 | } else { |
| 559 | history_nav_type = HistoryNavTypeForHistogram::kMainFrameAndSubframe; |
| 560 | } |
| 561 | } else if (subframe_cnt > 0) { |
| 562 | history_nav_type = HistoryNavTypeForHistogram::kSubframe; |
| 563 | } |
| 564 | if (history_nav_type.has_value()) { |
| 565 | UMA_HISTOGRAM_ENUMERATION("Navigation.BrowserInitiatedSessionHistoryCount", |
| 566 | history_nav_type.value(), |
| 567 | HistoryNavTypeForHistogram::kCount); |
| 568 | } |
| 569 | } |
| 570 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 571 | } // namespace |
| 572 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 573 | // NavigationControllerImpl::PendingEntryRef------------------------------------ |
| 574 | |
| 575 | NavigationControllerImpl::PendingEntryRef::PendingEntryRef( |
| 576 | base::WeakPtr<NavigationControllerImpl> controller) |
| 577 | : controller_(controller) {} |
| 578 | |
| 579 | NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() { |
| 580 | if (!controller_) // Can be null with interstitials. |
| 581 | return; |
| 582 | |
| 583 | controller_->PendingEntryRefDeleted(this); |
| 584 | } |
| 585 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 586 | // NavigationControllerImpl ---------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 587 | |
[email protected] | 23a918b | 2014-07-15 09:51:36 | [diff] [blame] | 588 | const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1); |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 589 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 590 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 591 | size_t NavigationControllerImpl::max_entry_count_for_testing_ = |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 592 | kMaxEntryCountForTestingNotSet; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 593 | |
[email protected] | e6fec47 | 2013-05-14 05:29:02 | [diff] [blame] | 594 | // 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] | 595 | // when testing. |
| 596 | static bool g_check_for_repost = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 598 | // static |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 599 | std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry( |
| 600 | const GURL& url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 601 | Referrer referrer, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 602 | std::optional<url::Origin> initiator_origin, |
| 603 | std::optional<GURL> initiator_base_url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 604 | ui::PageTransition transition, |
| 605 | bool is_renderer_initiated, |
| 606 | const std::string& extra_headers, |
| 607 | BrowserContext* browser_context, |
| 608 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { |
| 609 | return NavigationControllerImpl::CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 610 | url, referrer, std::move(initiator_origin), std::move(initiator_base_url), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 611 | std::nullopt /* source_process_site_url */, transition, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 612 | is_renderer_initiated, extra_headers, browser_context, |
| 613 | std::move(blob_url_loader_factory), true /* rewrite_virtual_urls */); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // static |
| 617 | std::unique_ptr<NavigationEntryImpl> |
| 618 | NavigationControllerImpl::CreateNavigationEntry( |
| 619 | const GURL& url, |
| 620 | Referrer referrer, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 621 | std::optional<url::Origin> initiator_origin, |
| 622 | std::optional<GURL> initiator_base_url, |
| 623 | std::optional<GURL> source_process_site_url, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 624 | ui::PageTransition transition, |
| 625 | bool is_renderer_initiated, |
| 626 | const std::string& extra_headers, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 627 | BrowserContext* browser_context, |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 628 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
| 629 | bool rewrite_virtual_urls) { |
| 630 | GURL url_to_load = url; |
| 631 | GURL virtual_url = url; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 632 | bool reverse_on_redirect = false; |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 633 | if (rewrite_virtual_urls) { |
| 634 | RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url, |
| 635 | &reverse_on_redirect); |
| 636 | } |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 637 | // Let the NTP override the navigation params and pretend that this is a |
| 638 | // browser-initiated, bookmark-like navigation. |
| 639 | GetContentClient()->browser()->OverrideNavigationParams( |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 640 | source_process_site_url, &transition, &is_renderer_initiated, &referrer, |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 641 | &initiator_origin); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 642 | |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 643 | auto entry = std::make_unique<NavigationEntryImpl>( |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 644 | nullptr, // The site instance for tabs is sent on navigation |
| 645 | // (WebContents::GetSiteInstance). |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 646 | url_to_load, referrer, initiator_origin, initiator_base_url, |
| 647 | std::u16string(), transition, is_renderer_initiated, |
| 648 | blob_url_loader_factory, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 649 | /* is_initial_entry = */ false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 650 | entry->SetVirtualURL(virtual_url); |
| 651 | entry->set_user_typed_url(virtual_url); |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 652 | entry->set_update_virtual_url_with_url(reverse_on_redirect); |
| 653 | entry->set_extra_headers(extra_headers); |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 654 | return entry; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 657 | // static |
| 658 | void NavigationController::DisablePromptOnRepost() { |
| 659 | g_check_for_repost = false; |
| 660 | } |
| 661 | |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 662 | base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime( |
| 663 | base::Time t) { |
| 664 | // If |t| is between the water marks, we're in a run of duplicates |
| 665 | // or just getting out of it, so increase the high-water mark to get |
| 666 | // a time that probably hasn't been used before and return it. |
| 667 | if (low_water_mark_ <= t && t <= high_water_mark_) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 668 | high_water_mark_ += base::Microseconds(1); |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 669 | return high_water_mark_; |
| 670 | } |
| 671 | |
| 672 | // Otherwise, we're clear of the last duplicate run, so reset the |
| 673 | // water marks. |
| 674 | low_water_mark_ = high_water_mark_ = t; |
| 675 | return t; |
| 676 | } |
| 677 | |
ckitagawa | 0faa5e4 | 2020-06-17 17:30:54 | [diff] [blame] | 678 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 679 | ScopedShowRepostDialogForTesting() |
| 680 | : was_disallowed_(g_check_for_repost) { |
| 681 | g_check_for_repost = true; |
| 682 | } |
| 683 | |
| 684 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 685 | ~ScopedShowRepostDialogForTesting() { |
| 686 | g_check_for_repost = was_disallowed_; |
| 687 | } |
| 688 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 689 | NavigationControllerImpl::RemovedEntriesTracker::RemovedEntriesTracker( |
| 690 | base::SafeRef<NavigationControllerImpl> controller) |
| 691 | : controller_(controller) { |
| 692 | for (FrameTreeNode* frame_tree_node : controller_->frame_tree().Nodes()) { |
| 693 | names_to_nodes_.insert({frame_tree_node->unique_name(), frame_tree_node}); |
| 694 | frame_tree_node_id_to_keys_.insert( |
| 695 | {frame_tree_node->frame_tree_node_id(), std::set<std::string>()}); |
| 696 | if (auto* frame_entry = frame_tree_node->current_frame_host() |
| 697 | ->last_committed_frame_entry()) { |
| 698 | frame_tree_node_id_to_doc_seq_nos_.insert( |
| 699 | {frame_tree_node->frame_tree_node_id(), |
| 700 | frame_entry->document_sequence_number()}); |
| 701 | } |
| 702 | } |
| 703 | PopulateKeySet(Direction::kBack); |
| 704 | PopulateKeySet(Direction::kForward); |
| 705 | } |
| 706 | |
| 707 | void NavigationControllerImpl::RemovedEntriesTracker::PopulateKeySet( |
| 708 | Direction direction) { |
| 709 | // Keep track of which FrameTreeNodes may still have relevant API keys in |
| 710 | // additional FrameNavigationEntries. |
| 711 | std::set<FrameTreeNode*> nodes_to_process; |
| 712 | for (FrameTreeNode* node : controller_->frame_tree().Nodes()) { |
| 713 | nodes_to_process.insert(node); |
| 714 | } |
| 715 | |
| 716 | const int offset = direction == Direction::kForward ? 1 : -1; |
| 717 | const int start = direction == Direction::kForward |
| 718 | ? controller_->GetLastCommittedEntryIndex() |
| 719 | : controller_->GetLastCommittedEntryIndex() - 1; |
| 720 | for (int i = start; |
| 721 | i >= 0 && i < controller_->GetEntryCount() && !nodes_to_process.empty(); |
| 722 | i += offset) { |
| 723 | std::set<FrameTreeNode*> nodes_to_continue_processing; |
| 724 | |
| 725 | NavigationEntryImpl* entry = controller_->GetEntryAtIndex(i); |
| 726 | entry->ForEachFrameEntry([this, &nodes_to_process, |
| 727 | &nodes_to_continue_processing, |
| 728 | &entry](FrameNavigationEntry* frame_entry) { |
| 729 | // Find the |node| that matches |frame_entry|, if any. |
| 730 | FrameTreeNode* node = nullptr; |
| 731 | if (frame_entry == entry->root_node()->frame_entry) { |
| 732 | node = controller_->frame_tree().root(); |
| 733 | } else { |
| 734 | auto it = names_to_nodes_.find(frame_entry->frame_unique_name()); |
| 735 | if (it == names_to_nodes_.end()) |
| 736 | return; |
| 737 | node = it->second; |
| 738 | } |
| 739 | |
| 740 | // Skip this node if a previous step determine there are no longer |
| 741 | // relevant navigation API keys in this direction. |
| 742 | if (nodes_to_process.find(node) == nodes_to_process.end()) |
| 743 | return; |
| 744 | |
| 745 | // Stop processing |node| if we reach a point where it's cross-origin. |
| 746 | // See also PopulateSingleNavigationApiHistoryEntryVector(). |
| 747 | url::Origin frame_entry_origin = |
| 748 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 749 | frame_entry->url(), |
| 750 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 751 | if (!node->current_origin().IsSameOriginWith(frame_entry_origin)) |
| 752 | return; |
| 753 | |
| 754 | frame_tree_node_id_to_keys_[node->frame_tree_node_id()].insert( |
| 755 | frame_entry->navigation_api_key()); |
| 756 | // Mark |node| as needing more processing for the next entry. |
| 757 | nodes_to_continue_processing.insert(node); |
| 758 | |
| 759 | // Check whether |node| went cross-document. If so, its children are |
| 760 | // no longer the same conceptual iframe as the current one, and |
| 761 | // should no longer be processed. This check is intentionally done |
| 762 | // after processing the current |node|, which may still have relevant |
| 763 | // discarded API keys. |
| 764 | if (frame_entry->document_sequence_number() != |
| 765 | frame_tree_node_id_to_doc_seq_nos_[node->frame_tree_node_id()]) { |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 766 | for (auto* descendant : node->frame_tree().SubtreeNodes(node)) |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 767 | nodes_to_process.erase(descendant); |
| 768 | } |
| 769 | }); |
| 770 | |
| 771 | nodes_to_process.swap(nodes_to_continue_processing); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | NavigationControllerImpl::RemovedEntriesTracker::~RemovedEntriesTracker() { |
| 776 | std::set<std::string> all_keys; |
| 777 | // Find all remaining navigation API keys after some entries have been |
| 778 | // removed. |
| 779 | for (auto& entry : controller_->entries_) { |
| 780 | entry->ForEachFrameEntry([&all_keys](FrameNavigationEntry* frame_entry) { |
| 781 | all_keys.insert(frame_entry->navigation_api_key()); |
| 782 | }); |
| 783 | } |
| 784 | |
| 785 | // Notify each frame in the renderer of any disposed navigation API keys. |
| 786 | for (auto& id_to_keys : frame_tree_node_id_to_keys_) { |
| 787 | std::vector<std::string> disposed_keys; |
| 788 | for (const auto& key : id_to_keys.second) { |
| 789 | if (all_keys.find(key) == all_keys.end()) |
| 790 | disposed_keys.push_back(key); |
| 791 | } |
| 792 | if (disposed_keys.empty()) |
| 793 | continue; |
| 794 | |
| 795 | FrameTreeNode* node = controller_->frame_tree().FindByID(id_to_keys.first); |
| 796 | auto& frame = node->current_frame_host()->GetAssociatedLocalFrame(); |
| 797 | frame->NotifyNavigationApiOfDisposedEntries(disposed_keys); |
| 798 | } |
| 799 | } |
| 800 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 801 | NavigationControllerImpl::NavigationControllerImpl( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 802 | BrowserContext* browser_context, |
| 803 | FrameTree& frame_tree, |
| 804 | NavigationControllerDelegate* delegate) |
| 805 | : frame_tree_(frame_tree), |
| 806 | browser_context_(browser_context), |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 807 | delegate_(delegate), |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 808 | ssl_manager_(this), |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 809 | get_timestamp_callback_(base::BindRepeating(&base::Time::Now)), |
| 810 | back_forward_cache_(browser_context) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 811 | DCHECK(browser_context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | } |
| 813 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 814 | NavigationControllerImpl::~NavigationControllerImpl() { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 815 | // The NavigationControllerImpl might be called inside its delegate |
| 816 | // destructor. Calling it is not valid anymore. |
| 817 | delegate_ = nullptr; |
| 818 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 819 | } |
| 820 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 821 | BrowserContext* NavigationControllerImpl::GetBrowserContext() { |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 822 | return browser_context_; |
| 823 | } |
| 824 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 825 | void NavigationControllerImpl::Restore( |
[email protected] | 5e36967 | 2009-11-03 23:48:30 | [diff] [blame] | 826 | int selected_navigation, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 827 | RestoreType type, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 828 | std::vector<std::unique_ptr<NavigationEntry>>* entries) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 829 | // Note that it's possible for `entries_` to contain multiple entries at this |
| 830 | // point, as Restore() might be called on a NavigationController that is |
| 831 | // already used (e.g. due to WebView's restoreState() API), not only for fresh |
| 832 | // NavigationControllers. These entries are not cleared to preserve legacy |
| 833 | // behavior and also because `pending_entry_` might point to one of the |
| 834 | // pre-existing entries. An exception for this is when `entries_` contains |
| 835 | // only the initial NavigationEntry, which must be removed. |
| 836 | |
| 837 | // Do not proceed if selected_navigation will be out of bounds for the updated |
| 838 | // entries_ list, since it will be assigned to last_committed_entry_index_ and |
| 839 | // used to index entries_. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 840 | // TODO(crbug.com/40816356): Consider also returning early if entries |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 841 | // is empty, since there should be no work to do (rather than marking the |
| 842 | // existing entries as needing reload). Also consider returning early if the |
| 843 | // selected index is -1, which represents a no-committed-entry state. |
| 844 | if (selected_navigation < -1 || |
| 845 | selected_navigation >= |
| 846 | base::checked_cast<int>(entries->size() + entries_.size())) { |
| 847 | return; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 848 | } |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 849 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 850 | // There will always be at least one entry (new NavigationControllers will |
| 851 | // have the initial NavigationEntry). |
| 852 | if (selected_navigation == -1) |
| 853 | selected_navigation = 0; |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 854 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 855 | if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) { |
| 856 | // If we are on the initial NavigationEntry, it must be the only entry in |
| 857 | // the list. Since it's impossible to do a history navigation to the |
| 858 | // initial NavigationEntry, `pending_entry_index_` must be -1 (but |
| 859 | // `pending_entry` might be set for a new non-history navigation). |
| 860 | // Note that we should not clear `entries_` if `entries` is empty (when |
| 861 | // InitialNavigationEntry mode is enabled), since that would leave us |
| 862 | // without any NavigationEntry. |
| 863 | CHECK_EQ(1, GetEntryCount()); |
| 864 | CHECK_EQ(-1, pending_entry_index_); |
| 865 | entries_.clear(); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 866 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 867 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 868 | needs_reload_ = true; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 869 | needs_reload_type_ = NeedsReloadType::kRestoreSession; |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 870 | entries_.reserve(entries->size()); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 871 | for (auto& entry : *entries) { |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 872 | if (entry->GetURL().is_empty()) { |
| 873 | // 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] | 874 | // persisting the initial NavigationEntry [which is no longer possible but |
| 875 | // some old persisted sessions might still contain it] or when the |
| 876 | // serializer failed to write the URL because it's too long). Trying to |
| 877 | // restore and navigate to an entry with an empty URL will result in |
| 878 | // crashes, so change the URL to about:blank. See also |
| 879 | // https://crbug.com/1240138 and https://crbug.com/1299335. |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 880 | entry->SetURL(GURL(url::kAboutBlankURL)); |
| 881 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 882 | entries_.push_back( |
| 883 | NavigationEntryImpl::FromNavigationEntry(std::move(entry))); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 884 | } |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 885 | |
| 886 | // At this point, the |entries| is full of empty scoped_ptrs, so it can be |
| 887 | // cleared out safely. |
| 888 | entries->clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 889 | |
| 890 | // And finish the restore. |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 891 | FinishRestore(selected_navigation, type); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 892 | } |
| 893 | |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 894 | void NavigationControllerImpl::Reload(ReloadType reload_type, |
| 895 | bool check_for_repost) { |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 896 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type", |
| 897 | (int)reload_type); |
| 898 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check", |
| 899 | check_for_repost); |
liaoyuke | 9168fba | 2017-03-10 19:20:28 | [diff] [blame] | 900 | DCHECK_NE(ReloadType::NONE, reload_type); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 901 | NavigationEntryImpl* entry = nullptr; |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 902 | int current_index = -1; |
| 903 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 904 | if (entry_replaced_by_post_commit_error_) { |
| 905 | // If there is an entry that was replaced by a currently active post-commit |
| 906 | // error navigation, this can't be the initial navigation. |
| 907 | DCHECK(!IsInitialNavigation()); |
| 908 | // If the current entry is a post commit error, we reload the entry it |
| 909 | // replaced instead. We leave the error entry in place until a commit |
| 910 | // replaces it, but the pending entry points to the original entry in the |
| 911 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 912 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 913 | entry = entry_replaced_by_post_commit_error_.get(); |
| 914 | current_index = GetCurrentEntryIndex(); |
| 915 | } else if (IsInitialNavigation() && pending_entry_) { |
| 916 | // If we are reloading the initial navigation, just use the current |
| 917 | // pending entry. Otherwise look up the current entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 918 | entry = pending_entry_; |
| 919 | // The pending entry might be in entries_ (e.g., after a Clone), so we |
| 920 | // should also update the current_index. |
| 921 | current_index = pending_entry_index_; |
| 922 | } else { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 923 | DiscardNonCommittedEntries(); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 924 | current_index = GetCurrentEntryIndex(); |
| 925 | if (current_index != -1) { |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 926 | entry = GetEntryAtIndex(current_index); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 927 | } |
[email protected] | 979a4bc | 2013-04-24 01:27:15 | [diff] [blame] | 928 | } |
[email protected] | 241db35 | 2013-04-22 18:04:05 | [diff] [blame] | 929 | |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 930 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 931 | // CanReload method. |
| 932 | if (!entry) |
| 933 | return; |
| 934 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 935 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 936 | // We should never navigate to an existing initial NavigationEntry that is |
| 937 | // the initial NavigationEntry for the initial empty document that hasn't |
| 938 | // been overridden by the synchronous about:blank commit, to preserve |
| 939 | // legacy behavior where trying to reload when the main frame is on the |
| 940 | // initial empty document won't result in a navigation. See also |
| 941 | // https://crbug.com/1277414. |
| 942 | return; |
| 943 | } |
| 944 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 945 | if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) { |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 946 | // 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] | 947 | // they really want to do this. If they do, the dialog will call us back |
| 948 | // with check_for_repost = false. |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 949 | delegate_->NotifyBeforeFormRepostWarningShow(); |
[email protected] | 965bb09 | 2010-04-09 11:59:02 | [diff] [blame] | 950 | |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 951 | pending_reload_ = reload_type; |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 952 | delegate_->ActivateAndShowRepostFormWarningDialog(); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 953 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 954 | } |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 955 | |
Wang Hui | a25efabc | 2022-09-24 17:27:22 | [diff] [blame] | 956 | // Set ReloadType for |entry|. |
| 957 | entry->set_reload_type(reload_type); |
| 958 | |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 959 | if (!IsInitialNavigation()) |
| 960 | DiscardNonCommittedEntries(); |
| 961 | |
| 962 | pending_entry_ = entry; |
| 963 | pending_entry_index_ = current_index; |
| 964 | pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 965 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 966 | // location.reload() goes through BeginNavigation, so all reloads triggered |
| 967 | // via this codepath are browser initiated. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 968 | NavigateToExistingPendingEntry( |
| 969 | reload_type, |
| 970 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 971 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 972 | /*navigation_api_key=*/nullptr); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 973 | } |
| 974 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 975 | void NavigationControllerImpl::CancelPendingReload() { |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 976 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 977 | } |
| 978 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 979 | void NavigationControllerImpl::ContinuePendingReload() { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 980 | // If the pending reload type has been cleared by another navigation |
| 981 | // committing, then do not proceed to reload after a form repost dialog. |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 982 | if (pending_reload_ == ReloadType::NONE) { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 983 | return; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 984 | } |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 985 | Reload(pending_reload_, false); |
| 986 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 987 | } |
| 988 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 989 | bool NavigationControllerImpl::IsInitialNavigation() { |
[email protected] | 27ba81c | 2012-08-21 17:04:09 | [diff] [blame] | 990 | return is_initial_navigation_; |
[email protected] | c70f9b8 | 2010-04-21 07:31:11 | [diff] [blame] | 991 | } |
| 992 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 993 | bool NavigationControllerImpl::IsInitialBlankNavigation() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 994 | // Check that we're on the initial NavigationEntry and that this is not a |
| 995 | // cloned tab. |
| 996 | return IsInitialNavigation() && GetEntryCount() == 1 && |
| 997 | GetLastCommittedEntry()->IsInitialEntry() && |
| 998 | GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored; |
creis | 10a4ab7 | 2015-10-13 17:22:40 | [diff] [blame] | 999 | } |
| 1000 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1001 | NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID( |
| 1002 | int nav_entry_id) const { |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 1003 | int index = GetEntryIndexWithUniqueID(nav_entry_id); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1004 | return (index != -1) ? entries_[index].get() : nullptr; |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 1005 | } |
| 1006 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 1007 | NavigationEntryImpl* |
| 1008 | NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending( |
| 1009 | int nav_entry_id) const { |
| 1010 | NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id); |
| 1011 | if (entry) |
| 1012 | return entry; |
| 1013 | return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 1014 | ? pending_entry_.get() |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 1015 | : nullptr; |
| 1016 | } |
| 1017 | |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1018 | void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation( |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1019 | const url::Origin& origin) { |
| 1020 | for (int i = 0; i < GetEntryCount(); i++) { |
| 1021 | auto* entry = GetEntryAtIndex(i); |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1022 | entry->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1023 | } |
| 1024 | if (entry_replaced_by_post_commit_error_) { |
| 1025 | // It's possible we could come back to this entry if the error |
| 1026 | // page/interstitial goes away. |
| 1027 | entry_replaced_by_post_commit_error_ |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 1028 | ->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1029 | } |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 1030 | } |
| 1031 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1032 | void NavigationControllerImpl::SetPendingEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 1033 | std::unique_ptr<NavigationEntryImpl> entry) { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 1034 | DiscardNonCommittedEntries(); |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1035 | pending_entry_ = entry.release(); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1036 | DCHECK_EQ(-1, pending_entry_index_); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1037 | } |
| 1038 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1039 | NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1040 | if (pending_entry_) |
| 1041 | return pending_entry_; |
| 1042 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1043 | } |
| 1044 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1045 | NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() { |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1046 | // The pending entry is safe to return for new (non-history), browser- |
| 1047 | // initiated navigations. Most renderer-initiated navigations should not |
| 1048 | // show the pending entry, to prevent URL spoof attacks. |
| 1049 | // |
| 1050 | // We make an exception for renderer-initiated navigations in new tabs, as |
| 1051 | // long as no other page has tried to access the initial empty document in |
| 1052 | // the new tab. If another page modifies this blank page, a URL spoof is |
| 1053 | // possible, so we must stop showing the pending entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1054 | bool safe_to_show_pending = |
| 1055 | pending_entry_ && |
| 1056 | // Require a new navigation. |
avi | 0dca04d | 2015-01-26 20:21:09 | [diff] [blame] | 1057 | pending_entry_index_ == -1 && |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1058 | // Require either browser-initiated or an unmodified new tab. |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 1059 | (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab()); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1060 | |
| 1061 | // Also allow showing the pending entry for history navigations in a new tab, |
| 1062 | // such as Ctrl+Back. In this case, no existing page is visible and no one |
| 1063 | // can script the new tab before it commits. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1064 | if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 && |
| 1065 | IsInitialNavigation() && !pending_entry_->is_renderer_initiated()) |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1066 | safe_to_show_pending = true; |
| 1067 | |
| 1068 | if (safe_to_show_pending) |
[email protected] | 867e1f9 | 2011-08-30 19:01:19 | [diff] [blame] | 1069 | return pending_entry_; |
| 1070 | return GetLastCommittedEntry(); |
| 1071 | } |
| 1072 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1073 | int NavigationControllerImpl::GetCurrentEntryIndex() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1074 | if (pending_entry_index_ != -1) |
| 1075 | return pending_entry_index_; |
| 1076 | return last_committed_entry_index_; |
| 1077 | } |
| 1078 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1079 | NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 1080 | CHECK_NE(last_committed_entry_index_, -1); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1081 | return entries_[last_committed_entry_index_].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1082 | } |
| 1083 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1084 | bool NavigationControllerImpl::CanViewSource() { |
Takashi Toyoshima | 6c58bbd | 2023-05-19 09:41:35 | [diff] [blame] | 1085 | const std::string& mime_type = frame_tree_->root() |
| 1086 | ->current_frame_host() |
| 1087 | ->GetPage() |
| 1088 | .GetContentsMimeType(); |
Kinuko Yasuda | 74702f9 | 2017-07-31 03:27:53 | [diff] [blame] | 1089 | bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) && |
| 1090 | !media::IsSupportedMediaMimeType(mime_type); |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 1091 | NavigationEntry* visible_entry = GetVisibleEntry(); |
| 1092 | return visible_entry && !visible_entry->IsViewSourceMode() && |
Carlos IL | d51e770 | 2020-05-07 18:51:39 | [diff] [blame] | 1093 | is_viewable_mime_type; |
[email protected] | 3168228 | 2010-01-15 18:05:16 | [diff] [blame] | 1094 | } |
| 1095 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1096 | int NavigationControllerImpl::GetLastCommittedEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1097 | // 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] | 1098 | // entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1099 | DCHECK_LT(last_committed_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1100 | return last_committed_entry_index_; |
| 1101 | } |
| 1102 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1103 | int NavigationControllerImpl::GetEntryCount() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1104 | DCHECK_GE(entries_.size(), 1u); |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 1105 | DCHECK_LE(entries_.size(), max_entry_count()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1106 | return static_cast<int>(entries_.size()); |
| 1107 | } |
| 1108 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1109 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) { |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1110 | if (index < 0 || index >= GetEntryCount()) |
| 1111 | return nullptr; |
| 1112 | |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1113 | return entries_[index].get(); |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 1114 | } |
| 1115 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1116 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) { |
avi | 057ce149 | 2015-06-29 15:59:47 | [diff] [blame] | 1117 | return GetEntryAtIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1118 | } |
| 1119 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1120 | int NavigationControllerImpl::GetIndexForOffset(int offset) { |
[email protected] | 7bc2b03 | 2012-12-19 22:45:46 | [diff] [blame] | 1121 | return GetCurrentEntryIndex() + offset; |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1122 | } |
| 1123 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1124 | std::optional<int> NavigationControllerImpl::GetIndexForGoBack() { |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1125 | for (int index = GetIndexForOffset(-1); index >= 0; index--) { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1126 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1127 | return index; |
| 1128 | } |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1129 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1130 | return std::nullopt; |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | bool NavigationControllerImpl::CanGoBack() { |
| 1134 | return GetIndexForGoBack().has_value(); |
| 1135 | } |
| 1136 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1137 | std::optional<int> NavigationControllerImpl::GetIndexForGoForward() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1138 | for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) { |
| 1139 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1140 | return index; |
| 1141 | } |
| 1142 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1143 | return std::nullopt; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1144 | } |
| 1145 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1146 | bool NavigationControllerImpl::CanGoForward() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1147 | return GetIndexForGoForward().has_value(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1148 | } |
| 1149 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1150 | bool NavigationControllerImpl::CanGoToOffset(int offset) { |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1151 | int index = GetIndexForOffset(offset); |
| 1152 | return index >= 0 && index < GetEntryCount(); |
| 1153 | } |
| 1154 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1155 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1156 | bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1157 | if (offset == 0) |
| 1158 | return true; |
| 1159 | int increment = offset > 0 ? 1 : -1; |
| 1160 | int non_skippable_entries = 0; |
| 1161 | for (int index = GetIndexForOffset(increment); |
| 1162 | index >= 0 && index < GetEntryCount(); index += increment) { |
| 1163 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1164 | non_skippable_entries++; |
| 1165 | |
| 1166 | if (non_skippable_entries == std::abs(offset)) |
| 1167 | return true; |
| 1168 | } |
| 1169 | return false; |
| 1170 | } |
| 1171 | #endif |
| 1172 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1173 | void NavigationControllerImpl::GoBack() { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1174 | const std::optional<int> target_index = GetIndexForGoBack(); |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1175 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1176 | CHECK(target_index.has_value()); |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 1177 | |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1178 | GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1179 | } |
| 1180 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1181 | void NavigationControllerImpl::GoForward() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 1182 | // Note that at least one entry (the last one) will be non-skippable since |
| 1183 | // entries are marked skippable only when they add another entry because of |
| 1184 | // redirect or pushState. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1185 | const std::optional<int> target_index = GetIndexForGoForward(); |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1186 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1187 | CHECK(target_index.has_value()); |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1188 | |
| 1189 | GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1190 | } |
| 1191 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1192 | void NavigationControllerImpl::GoToIndex(int index) { |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1193 | GoToIndex(index, /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1194 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1195 | /*navigation_api_key=*/nullptr); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 1196 | } |
| 1197 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1198 | std::vector<base::WeakPtr<NavigationRequest>> |
| 1199 | NavigationControllerImpl::GoToIndex( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1200 | int index, |
| 1201 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1202 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1203 | soft_navigation_heuristics_task_id, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1204 | const std::string* navigation_api_key) { |
sunjian | 30574a6 | 2017-03-21 21:39:44 | [diff] [blame] | 1205 | TRACE_EVENT0("browser,navigation,benchmark", |
| 1206 | "NavigationControllerImpl::GoToIndex"); |
Peter Boström | f68fe04 | 2023-06-07 18:27:50 | [diff] [blame] | 1207 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index); |
| 1208 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_size", |
| 1209 | entries_.size()); |
| 1210 | // Indices should have have been validated by the caller. |
| 1211 | CHECK_GE(index, 0); |
| 1212 | CHECK_LT(static_cast<size_t>(index), entries_.size()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1213 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1214 | if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 1215 | // We should never navigate to an existing initial NavigationEntry that is |
| 1216 | // the initial NavigationEntry for the initial empty document that hasn't |
| 1217 | // been overridden by the synchronous about:blank commit, to preserve |
| 1218 | // legacy behavior where trying to reload when the main frame is on the |
| 1219 | // initial empty document won't result in a navigation. See also |
| 1220 | // https://crbug.com/1277414. |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1221 | return {}; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1222 | } |
| 1223 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1224 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1225 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1226 | DCHECK_EQ(nullptr, pending_entry_); |
| 1227 | DCHECK_EQ(-1, pending_entry_index_); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1228 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1229 | pending_entry_ = entries_[index].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1230 | pending_entry_index_ = index; |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1231 | pending_entry_->SetTransitionType(ui::PageTransitionFromInt( |
| 1232 | pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK)); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 1233 | return NavigateToExistingPendingEntry(ReloadType::NONE, initiator_rfh, |
| 1234 | soft_navigation_heuristics_task_id, |
| 1235 | navigation_api_key); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1236 | } |
| 1237 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1238 | void NavigationControllerImpl::GoToOffset(int offset) { |
toyoshim | 3af4d50 | 2016-03-30 12:38:12 | [diff] [blame] | 1239 | // Note: This is actually reached in unit tests. |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1240 | if (!CanGoToOffset(offset)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1241 | return; |
| 1242 | |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1243 | GoToIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1244 | } |
| 1245 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1246 | void NavigationControllerImpl::GoToOffsetFromRenderer( |
| 1247 | int offset, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1248 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1249 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1250 | soft_navigation_heuristics_task_id) { |
Nate Chapin | bb74c5a5 | 2023-07-21 23:13:35 | [diff] [blame] | 1251 | // Note: This is actually reached in unit tests. |
| 1252 | if (!CanGoToOffset(offset)) |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1253 | return; |
| 1254 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1255 | GoToIndex(GetIndexForOffset(offset), initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1256 | soft_navigation_heuristics_task_id, |
| 1257 | /*navigation_api_key=*/nullptr); |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1258 | } |
| 1259 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 1260 | std::vector<base::WeakPtr<NavigationRequest>> |
| 1261 | NavigationControllerImpl::GoToIndexAndReturnAllRequests(int index) { |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 1262 | return GoToIndex(index, /*initiator_rfh=*/nullptr, |
| 1263 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
| 1264 | /*navigation_api_key=*/nullptr); |
| 1265 | } |
| 1266 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1267 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1268 | void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) { |
| 1269 | // Note: This is actually reached in unit tests. |
| 1270 | if (!CanGoToOffsetWithSkipping(offset)) |
| 1271 | return; |
| 1272 | |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 1273 | if (offset == 0) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1274 | GoToIndex(GetIndexForOffset(offset)); |
| 1275 | return; |
| 1276 | } |
| 1277 | int increment = offset > 0 ? 1 : -1; |
| 1278 | // Find the offset without counting skippable entries. |
| 1279 | int target_index = GetIndexForOffset(increment); |
| 1280 | int non_skippable_entries = 0; |
| 1281 | for (int index = target_index; index >= 0 && index < GetEntryCount(); |
| 1282 | index += increment) { |
| 1283 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1284 | non_skippable_entries++; |
| 1285 | |
| 1286 | if (non_skippable_entries == std::abs(offset)) { |
| 1287 | target_index = index; |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | GoToIndex(target_index); |
| 1293 | } |
| 1294 | #endif |
| 1295 | |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1296 | bool NavigationControllerImpl::RemoveEntryAtIndex(int index) { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1297 | if (index == last_committed_entry_index_ || index == pending_entry_index_) |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1298 | return false; |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 1299 | |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 1300 | RemoveEntryAtIndexInternal(index); |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1301 | return true; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1302 | } |
| 1303 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1304 | void NavigationControllerImpl::PruneForwardEntries() { |
| 1305 | DiscardNonCommittedEntries(); |
| 1306 | int remove_start_index = last_committed_entry_index_ + 1; |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1307 | int num_removed = static_cast<int>(entries_.size()) - remove_start_index; |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1308 | if (num_removed <= 0) |
| 1309 | return; |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 1310 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1311 | entries_.erase(entries_.begin() + remove_start_index, entries_.end()); |
| 1312 | NotifyPrunedEntries(this, remove_start_index /* start index */, |
| 1313 | num_removed /* count */); |
| 1314 | } |
| 1315 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1316 | void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry, |
| 1317 | const GURL& new_url) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1318 | GURL new_virtual_url(new_url); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 1319 | if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite( |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 1320 | &new_virtual_url, entry->GetVirtualURL(), browser_context_)) { |
| 1321 | entry->SetVirtualURL(new_virtual_url); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1325 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL( |
| 1326 | const GURL& url, |
| 1327 | const Referrer& referrer, |
| 1328 | ui::PageTransition transition, |
| 1329 | const std::string& extra_headers) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1330 | LoadURLParams params(url); |
| 1331 | params.referrer = referrer; |
| 1332 | params.transition_type = transition; |
| 1333 | params.extra_headers = extra_headers; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1334 | return LoadURLWithParams(params); |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1335 | } |
| 1336 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1337 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams( |
| 1338 | const LoadURLParams& params) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1339 | if (params.is_renderer_initiated) |
| 1340 | DCHECK(params.initiator_origin.has_value()); |
| 1341 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 1342 | TRACE_EVENT1("browser,navigation", |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1343 | "NavigationControllerImpl::LoadURLWithParams", "url", |
| 1344 | params.url.possibly_invalid_spec()); |
Ian Vollick | 9dda052 | 2019-09-11 02:24:29 | [diff] [blame] | 1345 | bool is_explicit_navigation = |
| 1346 | GetContentClient()->browser()->IsExplicitNavigation( |
| 1347 | params.transition_type); |
| 1348 | if (HandleDebugURL(params.url, params.transition_type, |
| 1349 | is_explicit_navigation)) { |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1350 | // If Telemetry is running, allow the URL load to proceed as if it's |
| 1351 | // unhandled, otherwise Telemetry can't tell if Navigation completed. |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 1352 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1353 | cc::switches::kEnableGpuBenchmarking)) |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1354 | return nullptr; |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1355 | } |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 1356 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1357 | // Checks based on params.load_type. |
| 1358 | switch (params.load_type) { |
| 1359 | case LOAD_TYPE_DEFAULT: |
lukasza | 477a5a2 | 2016-06-16 18:28:43 | [diff] [blame] | 1360 | case LOAD_TYPE_HTTP_POST: |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 1361 | #if BUILDFLAG(IS_ANDROID) |
| 1362 | case LOAD_TYPE_PDF_ANDROID: |
| 1363 | #endif |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1364 | break; |
| 1365 | case LOAD_TYPE_DATA: |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 1366 | if (!params.url.SchemeIs(url::kDataScheme)) { |
Peter Boström | 8472105d | 2024-05-15 04:36:02 | [diff] [blame] | 1367 | NOTREACHED_IN_MIGRATION() << "Data load must use data scheme."; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1368 | return nullptr; |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1369 | } |
| 1370 | break; |
Lukasz Anforowicz | bb0cfd5e | 2017-12-14 22:39:46 | [diff] [blame] | 1371 | } |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1372 | |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1373 | // The user initiated a load, we don't need to reload anymore. |
| 1374 | needs_reload_ = false; |
| 1375 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1376 | return NavigateWithoutEntry(params); |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1377 | } |
| 1378 | |
Charlie Reis | 4c53a96 | 2023-06-21 23:17:53 | [diff] [blame] | 1379 | void NavigationControllerImpl::LoadOriginalRequestURL() { |
| 1380 | // If the original request URL is not valid, matches the current URL, or |
| 1381 | // involves POST data, then simply reload. The POST check avoids issues with |
| 1382 | // sending data to the wrong page. |
| 1383 | const GURL& last_committed_url = GetLastCommittedEntry()->GetURL(); |
| 1384 | const GURL& original_request_url = |
| 1385 | GetLastCommittedEntry()->GetOriginalRequestURL(); |
| 1386 | if (!original_request_url.is_valid() || |
| 1387 | original_request_url == last_committed_url || |
| 1388 | GetLastCommittedEntry()->GetHasPostData()) { |
| 1389 | Reload(ReloadType::NORMAL, true); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | // Otherwise, attempt to load the original request URL without any of the |
| 1394 | // other data from the current NavigationEntry, replacing the current entry. |
| 1395 | // Loading the original URL is useful in cases such as modifying the user |
| 1396 | // agent. |
| 1397 | std::unique_ptr<NavigationController::LoadURLParams> load_params = |
| 1398 | std::make_unique<NavigationController::LoadURLParams>( |
| 1399 | original_request_url); |
| 1400 | load_params->should_replace_current_entry = true; |
| 1401 | load_params->transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 1402 | LoadURLWithParams(*load_params.get()); |
| 1403 | } |
| 1404 | |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1405 | bool NavigationControllerImpl::PendingEntryMatchesRequest( |
| 1406 | NavigationRequest* request) const { |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1407 | return pending_entry_ && |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1408 | pending_entry_->GetUniqueID() == request->nav_entry_id(); |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1409 | } |
| 1410 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1411 | bool NavigationControllerImpl::RendererDidNavigate( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1412 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1413 | const mojom::DidCommitProvisionalLoadParams& params, |
peary2 | 1b0f797b | 2016-09-28 17:28:33 | [diff] [blame] | 1414 | LoadCommittedDetails* details, |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1415 | bool is_same_document_navigation, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1416 | bool was_on_initial_empty_document, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1417 | bool previous_document_had_history_intervention_activation, |
Camille Lamy | 10aafcd3 | 2018-12-05 15:48:13 | [diff] [blame] | 1418 | NavigationRequest* navigation_request) { |
| 1419 | DCHECK(navigation_request); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1420 | |
| 1421 | // Note: validation checks and renderer kills due to invalid commit messages |
| 1422 | // must happen before getting here, in |
| 1423 | // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some |
| 1424 | // effects of the navigation have already occurred. |
| 1425 | |
[email protected] | cd2e1574 | 2013-03-08 04:08:31 | [diff] [blame] | 1426 | is_initial_navigation_ = false; |
| 1427 | |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1428 | // Any pending request to repost a form submission is no longer valid, since a |
| 1429 | // different NavigationEntry is committing. |
| 1430 | pending_reload_ = ReloadType::NONE; |
| 1431 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1432 | // Save the previous state before we clobber it. |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1433 | bool overriding_user_agent_changed = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1434 | if (entry_replaced_by_post_commit_error_) { |
| 1435 | // Same document navigation events with a post-commit error should already |
| 1436 | // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before |
| 1437 | // reaching here. |
| 1438 | CHECK(!is_same_document_navigation); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1439 | |
Xinghui Lu | b9e86a35 | 2024-05-17 16:29:42 | [diff] [blame] | 1440 | if (pending_entry_) { |
| 1441 | // Before `entry_replaced_by_post_commit_error_` is moved back, make sure |
| 1442 | // `pending_entry_` isn't pointing to the last committed entry. |
| 1443 | // Instead, all reload approaches (e.g., in `Reload` and |
| 1444 | // `LoadIfNecessary`) should attempt to load the |
| 1445 | // `entry_replaced_by_post_commit_error_` instead of the post commit error |
| 1446 | // entry itself. |
| 1447 | CHECK_NE(pending_entry_, entries_[last_committed_entry_index_].get()) |
| 1448 | << "Incorrectly reloading the post commit error page entry."; |
| 1449 | } |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 1450 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1451 | // Any commit while a post-commit error page is showing should put the |
| 1452 | // original entry back, replacing the error page's entry. This includes |
| 1453 | // reloads, where the original entry was used as the pending entry and |
| 1454 | // should now be at the correct index at commit time. |
| 1455 | entries_[last_committed_entry_index_] = |
| 1456 | std::move(entry_replaced_by_post_commit_error_); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1457 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1458 | details->previous_main_frame_url = GetLastCommittedEntry()->GetURL(); |
| 1459 | details->previous_entry_index = GetLastCommittedEntryIndex(); |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1460 | // Must honor user agent overrides in the |navigation_request|, such as |
| 1461 | // from things like RequestDesktopSiteWebContentsObserverAndroid. As a |
| 1462 | // result, besides comparing |pending_entry_|'s user agent against |
| 1463 | // LastCommittedEntry's, also need to compare |navigation_request|'s user |
| 1464 | // agent against LastCommittedEntry's. |
| 1465 | if (navigation_request->is_overriding_user_agent() != |
Shu Yang | 127e41a | 2023-12-19 01:26:54 | [diff] [blame] | 1466 | GetLastCommittedEntry()->GetIsOverridingUserAgent() || |
| 1467 | (PendingEntryMatchesRequest(navigation_request) && |
| 1468 | pending_entry_->GetIsOverridingUserAgent() != |
| 1469 | GetLastCommittedEntry()->GetIsOverridingUserAgent())) { |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1470 | overriding_user_agent_changed = true; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1471 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1472 | |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1473 | bool is_main_frame_navigation = !rfh->GetParent(); |
| 1474 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1475 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1476 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1477 | // For primary frame tree navigations, choose an appropriate |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1478 | // BackForwardCacheMetrics to be associated with the new navigation's |
| 1479 | // NavigationEntry, by either creating a new object or reusing the previous |
| 1480 | // entry's one. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1481 | scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics; |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 1482 | if (navigation_request->frame_tree_node()->frame_tree().is_primary()) { |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1483 | back_forward_cache_metrics = BackForwardCacheMetrics:: |
| 1484 | CreateOrReuseBackForwardCacheMetricsForNavigation( |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1485 | GetLastCommittedEntry(), is_main_frame_navigation, |
| 1486 | params.document_sequence_number); |
| 1487 | } |
Yuzu Saijo | 29f96ca9 | 2022-12-08 04:54:12 | [diff] [blame] | 1488 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1489 | // Notify the last active entry that we have navigated away. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1490 | if (is_main_frame_navigation && !is_same_document_navigation) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1491 | if (auto* metrics = GetLastCommittedEntry()->back_forward_cache_metrics()) { |
| 1492 | metrics->MainFrameDidNavigateAwayFromDocument(); |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1496 | // Use CommonNavigationParam's `should_replace_current_entry` to determine |
| 1497 | // whether the current NavigationEntry should be replaced. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1498 | // (See below for a case where we might override that.) |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1499 | details->did_replace_entry = |
| 1500 | navigation_request->common_params().should_replace_current_entry; |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1501 | |
fdegans | 9caf66a | 2015-07-30 21:10:42 | [diff] [blame] | 1502 | // 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] | 1503 | // except for restored entries. This should be true even if the current commit |
| 1504 | // is not related to the pending entry. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1505 | bool was_restored = false; |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1506 | DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() || |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 1507 | pending_entry_->IsRestored()); |
Charlie Reis | c4155af | 2022-10-19 15:33:11 | [diff] [blame] | 1508 | |
| 1509 | // Only make changes based on the pending entry if the NavigationRequest |
| 1510 | // matches it. Otherwise, the pending entry may be for a different request |
| 1511 | // (e.g., if a slow history navigation is pending while an auto-subframe |
| 1512 | // commit occurs). |
| 1513 | if (PendingEntryMatchesRequest(navigation_request)) { |
| 1514 | // It is no longer necessary to consider the pending entry as restored. |
| 1515 | if (pending_entry_->IsRestored()) { |
| 1516 | pending_entry_->set_restore_type(RestoreType::kNotRestored); |
| 1517 | was_restored = true; |
| 1518 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1519 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1520 | // If the SiteInstance has changed from the matching pending entry, this |
| 1521 | // must be treated as a new navigation with replacement. Set the replacement |
| 1522 | // bit here and ClassifyNavigation will identify this case and return |
| 1523 | // NEW_ENTRY. |
| 1524 | if (!rfh->GetParent() && pending_entry_->site_instance() && |
| 1525 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
| 1526 | DCHECK_NE(-1, pending_entry_index_); |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1527 | // TODO(nasko,creis,rakina): Move this to happen before committing the |
| 1528 | // navigation. This is a bit complicated because we don't currently |
| 1529 | // set `should_replace_current_entry` for reload/history navigations. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1530 | details->did_replace_entry = true; |
| 1531 | } |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1532 | } |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 1533 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1534 | // Do navigation-type specific actions. These will make and commit an entry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1535 | NavigationType navigation_type = |
| 1536 | ClassifyNavigation(rfh, params, navigation_request); |
| 1537 | navigation_request->set_navigation_type(navigation_type); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1538 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1539 | if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node())) { |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1540 | // Ensure that this navigation does not add a navigation entry, since |
| 1541 | // ShouldMaintainTrivialSessionHistory() means we should not add an entry |
| 1542 | // beyond the last committed one. Therefore, `should_replace_current_entry` |
| 1543 | // should be set, which replaces the current entry, or this should be a |
| 1544 | // reload, which does not create a new entry. |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1545 | DCHECK(navigation_request->common_params().should_replace_current_entry || |
Dominic Farolino | e0f8d7c | 2023-08-16 15:38:33 | [diff] [blame] | 1546 | navigation_request->GetReloadType() != ReloadType::NONE); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1547 | } |
| 1548 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1549 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1550 | if (rfh->GetParent()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1551 | // This is a subframe navigation on the initial empty document, which used |
| 1552 | // to not have a NavigationEntry to attach to. Now it can attach to the |
| 1553 | // initial NavigationEntry, and we must ensure that its NavigationEntry |
| 1554 | // will keep the "initial NavigationEntry" status and won't append a new |
| 1555 | // NavigationEntry (it should always do replacement instead). |
| 1556 | // See also https://crbug.com/1277414. |
| 1557 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1558 | // Subframe navigation on initial NavigationEntry must not append a new |
| 1559 | // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This |
| 1560 | // means every subframe navigation that happens while we're on the initial |
| 1561 | // NavigationEntry will always reuse the existing NavigationEntry and |
| 1562 | // just update the corresponding FrameNavigationEntry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1563 | DCHECK_EQ(navigation_type, NAVIGATION_TYPE_AUTO_SUBFRAME); |
| 1564 | } else if (navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1565 | // This is a navigation that modifies the initial NavigationEntry, either |
| 1566 | // for a replacement or a reload. The initial NavigationEntry should |
| 1567 | // retain its "initial NavigationEntry" status in this case. |
| 1568 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1569 | } else if (navigation_request->is_synchronous_renderer_commit() && |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1570 | !navigation_request->IsSameDocument() && !rfh->GetParent()) { |
| 1571 | DCHECK(navigation_request->common_params().should_replace_current_entry); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1572 | // This is a synchronous about:blank navigation on the main frame, which |
| 1573 | // used to not create a NavigationEntry when we have no NavigationEntry on |
| 1574 | // FrameTree creation. We now have the initial NavigationEntry and are on |
| 1575 | // the initial NavigationEntry. To preserve old behavior, we should still |
| 1576 | // keep the "initial" status for the new NavigationEntry that we will |
| 1577 | // create for this navigation, so that subframe navigations under the |
| 1578 | // synchronously committed about:blank document will never append new |
| 1579 | // NavigationEntry, and instead will just reuse the initial |
| 1580 | // NavigationEntry and modify the corresponding FrameNavigationEntries. |
| 1581 | // See also https://crbug.com/1277414. |
| 1582 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | DCHECK(!details->should_stay_as_initial_entry || |
| 1586 | GetLastCommittedEntry()->IsInitialEntry()); |
[email protected] | 4bf3522c | 2010-08-19 21:00:20 | [diff] [blame] | 1587 | |
eugenebut | ee08663a | 2017-04-27 17:43:12 | [diff] [blame] | 1588 | // is_same_document must be computed before the entry gets committed. |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1589 | details->is_same_document = is_same_document_navigation; |
[email protected] | b9d4dfdc | 2013-08-08 00:25:12 | [diff] [blame] | 1590 | |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1591 | details->is_prerender_activation = |
| 1592 | navigation_request->IsPrerenderedPageActivation(); |
Robert Lin | 540dbd1 | 2022-04-28 22:07:24 | [diff] [blame] | 1593 | details->is_in_active_page = navigation_request->GetRenderFrameHost() |
| 1594 | ->GetOutermostMainFrame() |
| 1595 | ->IsInPrimaryMainFrame(); |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1596 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1597 | // Make sure we do not discard the pending entry for a different ongoing |
| 1598 | // navigation when a same document commit comes in unexpectedly from the |
| 1599 | // renderer. Limit this to a very narrow set of conditions to avoid risks to |
| 1600 | // other navigation types. See https://crbug.com/900036. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 1601 | // TODO(crbug.com/41437754): Handle history.pushState() as well. |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1602 | bool keep_pending_entry = |
| 1603 | is_same_document_navigation && |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1604 | navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY && |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1605 | pending_entry_ && !PendingEntryMatchesRequest(navigation_request); |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1606 | |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1607 | switch (navigation_type) { |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1608 | case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1609 | RendererDidNavigateToNewEntry( |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 1610 | rfh, params, details->is_same_document, details->did_replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1611 | previous_document_had_history_intervention_activation, |
| 1612 | navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1613 | break; |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1614 | case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1615 | RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document, |
| 1616 | was_restored, navigation_request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1617 | keep_pending_entry, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1618 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1619 | case NAVIGATION_TYPE_NEW_SUBFRAME: |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1620 | RendererDidNavigateNewSubframe( |
| 1621 | rfh, params, details->is_same_document, details->did_replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 1622 | previous_document_had_history_intervention_activation, |
| 1623 | navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1624 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1625 | case NAVIGATION_TYPE_AUTO_SUBFRAME: |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1626 | if (!RendererDidNavigateAutoSubframe( |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1627 | rfh, params, details->is_same_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1628 | was_on_initial_empty_document, navigation_request, details)) { |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1629 | // We don't send a notification about auto-subframe PageState during |
| 1630 | // UpdateStateForFrame, since it looks like nothing has changed. Send |
| 1631 | // it here at commit time instead. |
| 1632 | NotifyEntryChanged(GetLastCommittedEntry()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1633 | return false; |
creis | 59d5a47cb | 2016-08-24 23:57:19 | [diff] [blame] | 1634 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1635 | break; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1636 | case NAVIGATION_TYPE_UNKNOWN: |
Peter Boström | 8472105d | 2024-05-15 04:36:02 | [diff] [blame] | 1637 | NOTREACHED_IN_MIGRATION(); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1638 | break; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1639 | } |
| 1640 | |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1641 | // At this point, we know that the navigation has just completed, so |
| 1642 | // record the time. |
| 1643 | // |
| 1644 | // TODO(akalin): Use "sane time" as described in |
Adam Langley | 4463fb83 | 2018-01-28 22:42:26 | [diff] [blame] | 1645 | // https://www.chromium.org/developers/design-documents/sane-time . |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 1646 | base::Time timestamp = |
| 1647 | time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run()); |
| 1648 | DVLOG(1) << "Navigation finished at (smoothed) timestamp " |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 1649 | << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1650 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1651 | // If we aren't keeping the pending entry, there shouldn't be one at this |
| 1652 | // point. Clear it again in case any error cases above forgot to do so. |
| 1653 | // TODO(pbos): Consider a CHECK here that verifies that the pending entry has |
| 1654 | // been cleared instead of protecting against it. |
| 1655 | if (!keep_pending_entry) |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1656 | DiscardNonCommittedEntriesWithCommitDetails(details); |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 1657 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1658 | // All committed entries should have nonempty content state so WebKit doesn't |
| 1659 | // get confused when we go back to them (see the function for details). |
creis | 0cade2e | 2017-02-28 06:37:47 | [diff] [blame] | 1660 | DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState."; |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1661 | NavigationEntryImpl* active_entry = GetLastCommittedEntry(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1662 | active_entry->SetTimestamp(timestamp); |
[email protected] | f49737b3 | 2013-08-28 07:51:44 | [diff] [blame] | 1663 | active_entry->SetHttpStatusCode(params.http_status_code); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1664 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1665 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1666 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1667 | if (back_forward_cache_metrics && |
| 1668 | !active_entry->back_forward_cache_metrics()) { |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1669 | active_entry->set_back_forward_cache_metrics( |
| 1670 | std::move(back_forward_cache_metrics)); |
| 1671 | } |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1672 | |
| 1673 | // `back_forward_cache_metrics()` may return null as we do not record |
| 1674 | // back-forward cache metrics for navigations in non-primary frame trees. |
| 1675 | if (active_entry->back_forward_cache_metrics()) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 1676 | // TODO(crbug.com/40229455): Remove this. |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1677 | // These are both only available from details at this point, so we capture |
| 1678 | // them here. |
| 1679 | SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type", |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1680 | navigation_type); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1681 | SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace", |
| 1682 | details->did_replace_entry); |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1683 | active_entry->back_forward_cache_metrics()->DidCommitNavigation( |
| 1684 | navigation_request, |
Rakina Zata Amni | 84a859df | 2024-06-07 15:56:32 | [diff] [blame] | 1685 | IsBackForwardCacheEnabled() && |
| 1686 | rfh->delegate()->IsBackForwardCacheSupported() && |
| 1687 | back_forward_cache_.IsAllowed(navigation_request->GetURL())); |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1688 | } |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1689 | |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1690 | // Grab the corresponding FrameNavigationEntry for a few updates, but only if |
| 1691 | // the SiteInstance matches (to avoid updating the wrong entry by mistake). |
| 1692 | // A mismatch can occur if the renderer lies or due to a unique name collision |
| 1693 | // after a race with an OOPIF (see https://crbug.com/616820). |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1694 | FrameNavigationEntry* frame_entry = |
| 1695 | active_entry->GetFrameEntry(rfh->frame_tree_node()); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1696 | if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance()) |
| 1697 | frame_entry = nullptr; |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1698 | // Make sure we've updated the PageState in one of the helper methods. |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1699 | // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed. |
| 1700 | if (frame_entry) { |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1701 | DCHECK(params.page_state == frame_entry->page_state()); |
Nasko Oskov | bbcfc000 | 2019-11-20 20:03:20 | [diff] [blame] | 1702 | |
| 1703 | // Remember the bindings the renderer process has at this point, so that |
| 1704 | // we do not grant this entry additional bindings if we come back to it. |
| 1705 | frame_entry->SetBindings(rfh->GetEnabledBindings()); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 1706 | } |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1707 | |
[email protected] | 97d8f0d | 2013-10-29 16:49:21 | [diff] [blame] | 1708 | // Once it is committed, we no longer need to track several pieces of state on |
| 1709 | // the entry. |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1710 | active_entry->ResetForCommit(frame_entry); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1711 | |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1712 | // The active entry's SiteInstance should match our SiteInstance. |
[email protected] | a1b9926 | 2013-12-27 21:56:22 | [diff] [blame] | 1713 | // TODO(creis): This check won't pass for subframes until we create entries |
| 1714 | // for subframe navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1715 | if (!rfh->GetParent()) |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1716 | CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance()); |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1717 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1718 | // Now prep the rest of the details for the notification and broadcast. |
[email protected] | 0f38dc455 | 2011-02-25 11:24:00 | [diff] [blame] | 1719 | details->entry = active_entry; |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1720 | details->is_main_frame = !rfh->GetParent(); |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame] | 1721 | details->http_status_code = params.http_status_code; |
estark | a5635c4 | 2015-07-14 00:06:53 | [diff] [blame] | 1722 | |
arthursonzogni | 7ddc654 | 2021-04-09 09:16:50 | [diff] [blame] | 1723 | active_entry->SetIsOverridingUserAgent( |
| 1724 | navigation_request->is_overriding_user_agent()); |
Scott Violet | c36f746 | 2020-05-06 23:13:03 | [diff] [blame] | 1725 | |
[email protected] | 93f230e0 | 2011-06-01 14:40:00 | [diff] [blame] | 1726 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1727 | |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1728 | if (overriding_user_agent_changed) |
| 1729 | delegate_->UpdateOverridingUserAgent(); |
| 1730 | |
creis | 03b4800 | 2015-11-04 00:54:56 | [diff] [blame] | 1731 | // Update the nav_entry_id for each RenderFrameHost in the tree, so that each |
| 1732 | // one knows the latest NavigationEntry it is showing (whether it has |
| 1733 | // committed anything in this navigation or not). This allows things like |
| 1734 | // state and title updates from RenderFrames to apply to the latest relevant |
| 1735 | // NavigationEntry. |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1736 | int nav_entry_id = active_entry->GetUniqueID(); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1737 | for (FrameTreeNode* node : frame_tree_->Nodes()) |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1738 | node->current_frame_host()->set_nav_entry_id(nav_entry_id); |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1739 | |
| 1740 | if (navigation_request->IsPrerenderedPageActivation()) { |
| 1741 | BroadcastHistoryOffsetAndLength(); |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 1742 | // TODO(crbug.com/40187392): Broadcasting happens after the prerendered page |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1743 | // is activated. As a result, a "prerenderingchange" event listener sees the |
| 1744 | // history.length which is not updated yet. We should guarantee that |
| 1745 | // history's length and offset should be updated before a |
| 1746 | // "prerenderingchange" event listener runs. One possible approach is to use |
| 1747 | // the same IPC which "prerenderingchange" uses, and propagate history's |
| 1748 | // length and offset together with that. |
| 1749 | } |
| 1750 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1751 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1752 | } |
| 1753 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1754 | NavigationType NavigationControllerImpl::ClassifyNavigation( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1755 | RenderFrameHostImpl* rfh, |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1756 | const mojom::DidCommitProvisionalLoadParams& params, |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1757 | NavigationRequest* navigation_request) { |
Piotr Tworek | bad5128 | 2020-09-30 19:17:59 | [diff] [blame] | 1758 | TraceReturnReason<tracing_category::kNavigation> trace_return( |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1759 | "ClassifyNavigation"); |
| 1760 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1761 | if (params.did_create_new_entry) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1762 | // A new entry. We may or may not have a corresponding pending entry, and |
| 1763 | // this may or may not be the main frame. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1764 | if (!rfh->GetParent()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1765 | trace_return.set_return_reason("new entry, no parent, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1766 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1767 | } |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1768 | // Valid subframe navigation. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1769 | trace_return.set_return_reason("new entry, new subframe"); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1770 | return NAVIGATION_TYPE_NEW_SUBFRAME; |
| 1771 | } |
| 1772 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1773 | // We only clear the session history in tests when navigating to a new entry. |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1774 | DCHECK(!params.history_list_was_cleared); |
| 1775 | |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1776 | if (rfh->GetParent()) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1777 | // All manual subframes would be did_create_new_entry and handled above, so |
| 1778 | // we know this is auto. |
Rakina Zata Amni | acd4df66 | 2022-11-15 06:49:08 | [diff] [blame] | 1779 | trace_return.set_return_reason("subframe, last commmited, auto subframe"); |
| 1780 | return NAVIGATION_TYPE_AUTO_SUBFRAME; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1781 | } |
| 1782 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1783 | const int nav_entry_id = navigation_request->commit_params().nav_entry_id; |
| 1784 | if (nav_entry_id == 0) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1785 | // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't |
| 1786 | // create a new page. |
| 1787 | |
Hayato Ito | 2ae4944 | 2021-07-02 02:59:25 | [diff] [blame] | 1788 | // This main frame navigation is not a history navigation (since |
| 1789 | // nav_entry_id is 0), but didn't create a new entry. So this must be a |
| 1790 | // reload or a replacement navigation, which will modify the existing entry. |
| 1791 | // |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1792 | // TODO(nasko): With error page isolation, reloading an existing session |
| 1793 | // history entry can result in change of SiteInstance. Check for such a case |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1794 | // here and classify it as NEW_ENTRY, as such navigations should be treated |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1795 | // as new with replacement. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1796 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1797 | "nav entry 0, last committed, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1798 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1799 | } |
| 1800 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1801 | if (PendingEntryMatchesRequest(navigation_request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1802 | // If the SiteInstance of the |pending_entry_| does not match the |
| 1803 | // SiteInstance that got committed, treat this as a new navigation with |
| 1804 | // replacement. This can happen if back/forward/reload encounters a server |
| 1805 | // redirect to a different site or an isolated error page gets successfully |
| 1806 | // reloaded into a different SiteInstance. |
| 1807 | if (pending_entry_->site_instance() && |
| 1808 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1809 | trace_return.set_return_reason("pending matching nav entry, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1810 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1811 | } |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1812 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1813 | if (pending_entry_index_ == -1) { |
| 1814 | // In this case, we have a pending entry for a load of a new URL but Blink |
| 1815 | // didn't do a new navigation (params.did_create_new_entry). First check |
| 1816 | // to make sure Blink didn't treat a new cross-process navigation as |
| 1817 | // 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] | 1818 | // we must treat it as NEW rather than the converted reload case below, |
| 1819 | // since the new SiteInstance doesn't match the last committed entry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1820 | if (GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1821 | trace_return.set_return_reason("new pending, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1822 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | // Otherwise, this happens when you press enter in the URL bar to reload. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1826 | // We will create a pending entry, but NavigateWithoutEntry will convert |
| 1827 | // it to a reload since it's the same page and not create a new entry for |
| 1828 | // it. (The user doesn't want to have a new back/forward entry when they |
| 1829 | // do this.) Therefore we want to just ignore the pending entry and go |
| 1830 | // back to where we were (the "existing entry"). |
| 1831 | trace_return.set_return_reason("new pending, existing (same) entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1832 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1833 | } |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1834 | } |
| 1835 | |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 1836 | if (navigation_request->commit_params().intended_as_new_entry) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1837 | // 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] | 1838 | // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may |
| 1839 | // or may not have a pending entry. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1840 | trace_return.set_return_reason("intended as new entry, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1841 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1842 | } |
| 1843 | |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 1844 | if (navigation_request->DidEncounterError() && |
| 1845 | failed_pending_entry_id_ != 0 && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1846 | nav_entry_id == failed_pending_entry_id_) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1847 | // If the renderer was going to a new pending entry that got cleared because |
| 1848 | // of an error, this is the case of the user trying to retry a failed load |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1849 | // by pressing return. Classify as EXISTING_ENTRY because we probably don't |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1850 | // have a pending entry. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1851 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1852 | "unreachable, matching pending, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1853 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1854 | } |
| 1855 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1856 | // 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] | 1857 | int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1858 | trace_return.traced_value()->SetInteger("existing_entry_index", |
| 1859 | existing_entry_index); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1860 | if (existing_entry_index == -1) { |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 1861 | // The renderer has committed a navigation to an entry that no longer |
| 1862 | // exists. Because the renderer is showing that page, resurrect that entry. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1863 | trace_return.set_return_reason("existing entry -1, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1864 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1865 | } |
| 1866 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1867 | // Since we weeded out "new" navigations above, we know this is an existing |
| 1868 | // (back/forward) navigation. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1869 | trace_return.set_return_reason("default return, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1870 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1871 | } |
| 1872 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1873 | void NavigationControllerImpl::UpdateNavigationEntryDetails( |
| 1874 | NavigationEntryImpl* entry, |
| 1875 | RenderFrameHostImpl* rfh, |
| 1876 | const mojom::DidCommitProvisionalLoadParams& params, |
| 1877 | NavigationRequest* request, |
| 1878 | NavigationEntryImpl::UpdatePolicy update_policy, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1879 | bool is_new_entry, |
| 1880 | LoadCommittedDetails* commit_details) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1881 | // Update the FrameNavigationEntry. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1882 | std::vector<GURL> redirects; |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1883 | entry->AddOrUpdateFrameEntry( |
| 1884 | rfh->frame_tree_node(), update_policy, params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1885 | params.document_sequence_number, params.navigation_api_key, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1886 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1887 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1888 | Referrer(*params.referrer), |
| 1889 | request ? request->common_params().initiator_origin : params.origin, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1890 | request ? request->common_params().initiator_base_url : std::nullopt, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1891 | request ? request->GetRedirectChain() : redirects, params.page_state, |
| 1892 | params.method, params.post_id, nullptr /* blob_url_loader_factory */, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1893 | ComputePolicyContainerPoliciesForFrameEntry( |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1894 | rfh, request && request->IsSameDocument(), |
| 1895 | request ? request->common_params().url : params.url)); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1896 | |
| 1897 | if (rfh->GetParent()) { |
| 1898 | // Only modify the NavigationEntry for main frame navigations. |
| 1899 | return; |
| 1900 | } |
| 1901 | if (entry->update_virtual_url_with_url()) |
| 1902 | UpdateVirtualURLToURL(entry, params.url); |
| 1903 | // Don't use the page type from the pending entry. Some interstitial page |
| 1904 | // may have set the type to interstitial. Once we commit, however, the page |
| 1905 | // type must always be normal or error. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1906 | entry->set_page_type((request && request->DidEncounterError()) |
| 1907 | ? PAGE_TYPE_ERROR |
| 1908 | : PAGE_TYPE_NORMAL); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1909 | if (commit_details && commit_details->should_stay_as_initial_entry) { |
| 1910 | // Retain the "initial NavigationEntry" status. |
| 1911 | if (request->IsSameDocument()) { |
| 1912 | // If this is for a same-document navigation, the NavigationEntry must be |
| 1913 | // reused and should already be marked as the initial NavigationEntry. |
| 1914 | DCHECK(entry->IsInitialEntry()); |
| 1915 | } else { |
| 1916 | // If this is for a cross-document navigation, it can be caused by a |
| 1917 | // renderer-initiated reload, or the synchronous about:blank commit. Mark |
| 1918 | // "for synchronous about:blank" in the latter case, and also when it is |
| 1919 | // reloading a "for synchronous about:blank" entry. Otherwise, the entry |
| 1920 | // is not for a synchronous about:blank commit. |
| 1921 | NavigationEntryImpl::InitialNavigationEntryState new_state = |
| 1922 | NavigationEntryImpl::InitialNavigationEntryState:: |
| 1923 | kInitialNotForSynchronousAboutBlank; |
| 1924 | if (entry->IsInitialEntryForSynchronousAboutBlank() || |
| 1925 | request->is_synchronous_renderer_commit()) { |
| 1926 | new_state = NavigationEntryImpl::InitialNavigationEntryState:: |
| 1927 | kInitialForSynchronousAboutBlank; |
| 1928 | } |
| 1929 | entry->set_initial_navigation_entry_state(new_state); |
| 1930 | } |
| 1931 | } else if (commit_details && !commit_details->should_stay_as_initial_entry) { |
| 1932 | // Remove the "initial NavigationEntry" status. |
| 1933 | entry->set_initial_navigation_entry_state( |
| 1934 | NavigationEntryImpl::InitialNavigationEntryState::kNonInitial); |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1935 | } |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1936 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1937 | if (is_new_entry) { |
| 1938 | // Some properties of the NavigationEntry are only set when the entry is |
| 1939 | // new (we aren't reusing it). |
| 1940 | entry->SetTransitionType(params.transition); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1941 | entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL() |
Peter Kasting | 8104ba8 | 2024-01-31 15:23:40 | [diff] [blame] | 1942 | : GURL()); |
Adithya Srinivasan | 72b0735 | 2023-12-21 15:56:01 | [diff] [blame] | 1943 | DCHECK_EQ(rfh->GetPage().is_overriding_user_agent(), |
| 1944 | params.is_overriding_user_agent); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1945 | entry->SetIsOverridingUserAgent(params.is_overriding_user_agent); |
| 1946 | } else { |
| 1947 | // We are reusing the NavigationEntry. The site instance will normally be |
| 1948 | // the same except for a few cases: |
| 1949 | // 1) session restore, when no site instance will be assigned or |
| 1950 | // 2) redirect, when the site instance is reset. |
| 1951 | DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() || |
| 1952 | entry->site_instance() == rfh->GetSiteInstance()); |
| 1953 | } |
| 1954 | } |
| 1955 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1956 | void NavigationControllerImpl::CreateInitialEntry() { |
| 1957 | DCHECK_EQ(entries_.size(), 0u); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1958 | RenderFrameHostImpl* rfh = frame_tree_->root()->current_frame_host(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1959 | auto params = mojom::DidCommitProvisionalLoadParams::New(); |
| 1960 | // The initial NavigationEntry's URL is the empty URL. This preserves the old |
| 1961 | // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from |
| 1962 | // before we have initial NavigationEntries. |
Peter Kasting | 8104ba8 | 2024-01-31 15:23:40 | [diff] [blame] | 1963 | params->url = GURL(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1964 | params->http_status_code = 0; |
| 1965 | params->url_is_unreachable = false; |
| 1966 | params->method = "GET"; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1967 | params->post_id = -1; |
| 1968 | params->embedding_token = base::UnguessableToken::Create(); |
| 1969 | params->navigation_token = base::UnguessableToken::Create(); |
| 1970 | params->did_create_new_entry = true; |
| 1971 | params->origin = rfh->GetLastCommittedOrigin(); |
| 1972 | params->should_update_history = true; |
| 1973 | params->item_sequence_number = 0; |
| 1974 | params->document_sequence_number = 0; |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 1975 | bool is_in_fenced_frame_tree = rfh->IsNestedWithinFencedFrame(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1976 | params->transition = is_in_fenced_frame_tree |
| 1977 | ? ui::PAGE_TRANSITION_AUTO_SUBFRAME |
| 1978 | : ui::PAGE_TRANSITION_LINK; |
| 1979 | params->referrer = blink::mojom::Referrer::New(); |
| 1980 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1981 | auto new_entry = std::make_unique<NavigationEntryImpl>( |
| 1982 | rfh->GetSiteInstance(), params->url, Referrer(*params->referrer), |
W. James MacLean | 78e2f87 | 2023-01-24 17:59:38 | [diff] [blame] | 1983 | rfh->GetLastCommittedOrigin(), rfh->GetInheritedBaseUrl(), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 1984 | std::u16string() /* title */, ui::PAGE_TRANSITION_TYPED, |
| 1985 | false /* renderer_initiated */, nullptr /* blob_url_loader_factory */, |
| 1986 | true /* is_initial_entry */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1987 | UpdateNavigationEntryDetails( |
| 1988 | new_entry.get(), rfh, *params, nullptr /* request */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1989 | NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */, |
| 1990 | nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1991 | |
| 1992 | InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */, |
| 1993 | false /* was_post_commit_error */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1994 | is_in_fenced_frame_tree, nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1995 | } |
| 1996 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1997 | void NavigationControllerImpl::RendererDidNavigateToNewEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1998 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1999 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2000 | bool is_same_document, |
clamy | 3bf35e3c | 2016-11-10 15:59:44 | [diff] [blame] | 2001 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2002 | bool previous_document_had_history_intervention_activation, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2003 | NavigationRequest* request, |
| 2004 | LoadCommittedDetails* commit_details) { |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 2005 | std::unique_ptr<NavigationEntryImpl> new_entry; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2006 | const std::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2007 | request->common_params().initiator_origin; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2008 | std::optional<GURL> initiator_base_url; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 2009 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 2010 | initiator_base_url = request->common_params().initiator_base_url; |
| 2011 | } |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 2012 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2013 | // First check if this is an in-page navigation. If so, clone the current |
| 2014 | // entry instead of looking at the pending entry, because the pending entry |
| 2015 | // does not have any subframe history items. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2016 | if (is_same_document) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2017 | FrameNavigationEntry* previous_frame_entry = |
| 2018 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2019 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2020 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2021 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2022 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2023 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2024 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
Rakina Zata Amni | 82fafba | 2021-03-11 07:07:09 | [diff] [blame] | 2025 | request->GetRedirectChain(), params.page_state, params.method, |
| 2026 | params.post_id, nullptr /* blob_url_loader_factory */, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2027 | // We will set the document policies later in this function. |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2028 | nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2029 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2030 | // FrameNavigationEntry. |
| 2031 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2032 | previous_frame_entry->protect_url_in_navigation_api()); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2033 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2034 | new_entry = GetLastCommittedEntry()->CloneAndReplace( |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2035 | frame_entry, true, request->frame_tree_node(), frame_tree_->root()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2036 | if (new_entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2037 | params.url.DeprecatedGetOriginAsURL()) { |
jam | a78746e | 2017-02-22 17:21:57 | [diff] [blame] | 2038 | // TODO(jam): we had one report of this with a URL that was redirecting to |
| 2039 | // only tildes. Until we understand that better, don't copy the cert in |
| 2040 | // this case. |
| 2041 | new_entry->GetSSL() = SSLStatus(); |
| 2042 | } |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2043 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2044 | // It is expected that |frame_entry| is now owned by |new_entry|. This means |
| 2045 | // that |frame_entry| should now have a reference count of exactly 2: one |
| 2046 | // due to the local variable |frame_entry|, and another due to |new_entry| |
| 2047 | // also retaining one. This should never fail, because it's the main frame. |
| 2048 | CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef()); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2049 | } |
| 2050 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2051 | // If this is an activation navigation from a prerendered page, transfer the |
| 2052 | // new entry from an entry already created and stored in the |
| 2053 | // NavigationRequest. |new_entry| will not have been set prior to this as |
| 2054 | // |is_same_document| is mutually exclusive with |
| 2055 | // |IsPrerenderedPageActivation|. |
| 2056 | if (request->IsPrerenderedPageActivation()) { |
| 2057 | DCHECK(!is_same_document); |
| 2058 | DCHECK(!new_entry); |
| 2059 | new_entry = request->TakePrerenderNavigationEntry(); |
| 2060 | DCHECK(new_entry); |
| 2061 | } |
| 2062 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2063 | // Only make a copy of the pending entry if it is appropriate for the new |
| 2064 | // document that just loaded. Verify this by checking if the entry corresponds |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2065 | // to the given NavigationRequest. Additionally, coarsely check that: |
csharrison | 9a9142bc4 | 2016-03-01 17:24:04 | [diff] [blame] | 2066 | // 1. The SiteInstance hasn't been assigned to something else. |
| 2067 | // 2. The pending entry was intended as a new entry, rather than being a |
| 2068 | // history navigation that was interrupted by an unrelated, |
| 2069 | // renderer-initiated navigation. |
| 2070 | // TODO(csharrison): Investigate whether we can remove some of the coarser |
| 2071 | // checks. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2072 | if (!new_entry && PendingEntryMatchesRequest(request) && |
| 2073 | pending_entry_index_ == -1 && |
[email protected] | 6dd86ab | 2013-02-27 00:30:34 | [diff] [blame] | 2074 | (!pending_entry_->site_instance() || |
[email protected] | 27dd82fd | 2014-03-03 22:11:43 | [diff] [blame] | 2075 | pending_entry_->site_instance() == rfh->GetSiteInstance())) { |
creis | ef4a0cb | 2015-03-12 19:14:35 | [diff] [blame] | 2076 | new_entry = pending_entry_->Clone(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2077 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2078 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2079 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2080 | } |
| 2081 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2082 | // For cross-document commits with no matching pending entry, create a new |
| 2083 | // entry. |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2084 | if (!new_entry) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 2085 | new_entry = std::make_unique<NavigationEntryImpl>( |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2086 | rfh->GetSiteInstance(), params.url, Referrer(*params.referrer), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2087 | initiator_origin, initiator_base_url, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2088 | std::u16string(), // title |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2089 | params.transition, request->IsRendererInitiated(), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2090 | nullptr, // blob_url_loader_factory |
| 2091 | false); // is_initial_entry |
[email protected] | f8f93eb | 2012-09-25 03:06:24 | [diff] [blame] | 2092 | |
| 2093 | // Find out whether the new entry needs to update its virtual URL on URL |
| 2094 | // change and set up the entry accordingly. This is needed to correctly |
| 2095 | // update the virtual URL when replaceState is called after a pushState. |
| 2096 | GURL url = params.url; |
| 2097 | bool needs_update = false; |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2098 | // 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] | 2099 | // update the virtual URL based on the new URL. For example, this is needed |
| 2100 | // to show chrome://bookmarks/#1 when the bookmarks webui extension changes |
| 2101 | // the URL. |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2102 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 2103 | &url, browser_context_, &needs_update); |
| 2104 | new_entry->set_update_virtual_url_with_url(needs_update); |
| 2105 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2106 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2107 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2108 | } |
| 2109 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 2110 | // TODO(crbug.com/40169536) - determine which parts of the entry need to be |
| 2111 | // set for prerendered contents, if any. This is because |
| 2112 | // prerendering/activation technically won't be creating a new document. |
| 2113 | // Unlike BFCache, prerendering creates a new NavigationEntry rather than |
| 2114 | // using an existing one. |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2115 | if (!request->IsPrerenderedPageActivation()) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2116 | UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request, |
| 2117 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2118 | true /* is_new_entry */, commit_details); |
creis | 8b5cd4c | 2015-06-19 00:11:08 | [diff] [blame] | 2119 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2120 | // history.pushState() is classified as a navigation to a new page, but sets |
| 2121 | // is_same_document to true. In this case, we already have the title and |
| 2122 | // favicon available, so set them immediately. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2123 | if (is_same_document) { |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2124 | new_entry->SetTitle(GetLastCommittedEntry()->GetTitle()); |
| 2125 | new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon(); |
| 2126 | } |
[email protected] | 3a868f21 | 2014-08-09 10:41:19 | [diff] [blame] | 2127 | } |
[email protected] | ff64b3e | 2014-05-31 04:07:33 | [diff] [blame] | 2128 | |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2129 | DCHECK(!params.history_list_was_cleared || !replace_entry); |
| 2130 | // The browser requested to clear the session history when it initiated the |
| 2131 | // navigation. Now we know that the renderer has updated its state accordingly |
| 2132 | // and it is safe to also clear the browser side history. |
| 2133 | if (params.history_list_was_cleared) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2134 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2135 | entries_.clear(); |
| 2136 | last_committed_entry_index_ = -1; |
| 2137 | } |
| 2138 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2139 | // If this is a new navigation with replacement and there is a |
| 2140 | // pending_entry_ which matches the navigation reported by the renderer |
| 2141 | // process, then it should be the one replaced, so update the |
| 2142 | // last_committed_entry_index_ to use it. |
| 2143 | if (replace_entry && pending_entry_index_ != -1 && |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2144 | PendingEntryMatchesRequest(request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2145 | last_committed_entry_index_ = pending_entry_index_; |
| 2146 | } |
| 2147 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2148 | SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2149 | replace_entry, previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2150 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 2151 | |
Yuzu Saijo | a725585f | 2022-11-28 04:14:03 | [diff] [blame] | 2152 | // If this is a history navigation and the old entry has an existing |
| 2153 | // back/forward cache metrics object, keep using the old one so that the |
| 2154 | // reasons logged from the last time the page navigated gets preserved. |
| 2155 | if (BackForwardCacheMetrics::IsCrossDocumentMainFrameHistoryNavigation( |
| 2156 | request)) { |
| 2157 | // Use |request->GetNavigationEntry()| instead of |pending_entry_| here |
| 2158 | // because some tests do not have a pending entry. |
| 2159 | NavigationEntryImpl* entry = |
| 2160 | static_cast<NavigationEntryImpl*>(request->GetNavigationEntry()); |
| 2161 | if (entry && entry->back_forward_cache_metrics()) { |
| 2162 | scoped_refptr<BackForwardCacheMetrics> metrics = |
| 2163 | entry->TakeBackForwardCacheMetrics(); |
| 2164 | new_entry->set_back_forward_cache_metrics(std::move(metrics)); |
| 2165 | } |
| 2166 | } |
| 2167 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2168 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2169 | !request->post_commit_error_page_html().empty(), |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2170 | rfh->IsNestedWithinFencedFrame(), commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2171 | } |
| 2172 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2173 | void NavigationControllerImpl::RendererDidNavigateToExistingEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2174 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2175 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2176 | bool is_same_document, |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2177 | bool was_restored, |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2178 | NavigationRequest* request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2179 | bool keep_pending_entry, |
| 2180 | LoadCommittedDetails* commit_details) { |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 2181 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2182 | << "that a last committed entry exists."; |
| 2183 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2184 | // We should only get here for main frame navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 2185 | DCHECK(!rfh->GetParent()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2186 | |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2187 | NavigationEntryImpl* entry = nullptr; |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 2188 | if (request->commit_params().intended_as_new_entry) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2189 | // We're guaranteed to have a last committed entry if intended_as_new_entry |
| 2190 | // is true. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2191 | entry = GetLastCommittedEntry(); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2192 | |
| 2193 | // If the NavigationRequest matches a new pending entry and is classified as |
| 2194 | // EXISTING_ENTRY, then it is a navigation to the same URL that was |
| 2195 | // converted to a reload, such as a user pressing enter in the omnibox. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2196 | if (pending_entry_index_ == -1 && PendingEntryMatchesRequest(request)) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2197 | // Note: The pending entry will usually have a real ReloadType here, but |
| 2198 | // it can still be ReloadType::NONE in cases that |
| 2199 | // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source). |
| 2200 | |
| 2201 | // If we classified this correctly, the SiteInstance should not have |
| 2202 | // changed. |
| 2203 | CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance()); |
| 2204 | |
| 2205 | // For converted reloads, we assign the entry's unique ID to be that of |
| 2206 | // the new one. Since this is always the result of a user action, we want |
| 2207 | // to dismiss infobars, etc. like a regular user-initiated navigation. |
| 2208 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
| 2209 | |
| 2210 | // The extra headers may have changed due to reloading with different |
| 2211 | // headers. |
| 2212 | entry->set_extra_headers(pending_entry_->extra_headers()); |
| 2213 | } |
| 2214 | // Otherwise, this was intended as a new entry but the pending entry was |
| 2215 | // lost in the meantime and no new entry was created. We are stuck at the |
| 2216 | // last committed entry. |
| 2217 | |
| 2218 | // Even if this is a converted reload from pressing enter in the omnibox, |
| 2219 | // the server could redirect, requiring an update to the SSL status. If this |
| 2220 | // is a same document navigation, though, there's no SSLStatus in the |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2221 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2222 | if (!is_same_document) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2223 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2224 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2225 | } |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2226 | } else if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2227 | // This is a browser-initiated navigation (back/forward/reload). |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2228 | entry = GetEntryWithUniqueID(nav_entry_id); |
jam | d208b90 | 2016-09-01 16:58:16 | [diff] [blame] | 2229 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2230 | if (is_same_document) { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2231 | // There's no SSLStatus in the NavigationRequest for same document |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2232 | // navigations, so normally we leave |entry|'s SSLStatus as is. However if |
| 2233 | // this was a restored same document navigation entry, then it won't have |
| 2234 | // an SSLStatus. So we need to copy over the SSLStatus from the entry that |
| 2235 | // navigated it. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2236 | NavigationEntryImpl* last_entry = GetLastCommittedEntry(); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2237 | if (entry->GetURL().DeprecatedGetOriginAsURL() == |
| 2238 | last_entry->GetURL().DeprecatedGetOriginAsURL() && |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2239 | last_entry->GetSSL().initialized && !entry->GetSSL().initialized && |
| 2240 | was_restored) { |
| 2241 | entry->GetSSL() = last_entry->GetSSL(); |
| 2242 | } |
| 2243 | } else { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2244 | // In rapid back/forward navigations |request| sometimes won't have a cert |
| 2245 | // (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] | 2246 | // otherwise we only reuse the existing cert if the origins match. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2247 | if (request->GetSSLInfo().has_value() && |
| 2248 | request->GetSSLInfo()->is_valid()) { |
| 2249 | entry->GetSSL() = SSLStatus(*(request->GetSSLInfo())); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2250 | } else if (entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2251 | request->GetURL().DeprecatedGetOriginAsURL()) { |
John Abd-El-Malek | 3f24708 | 2017-12-07 19:02:19 | [diff] [blame] | 2252 | entry->GetSSL() = SSLStatus(); |
| 2253 | } |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2254 | } |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2255 | } else { |
Feifei Wang | 2ab8ba6c | 2022-04-13 22:19:27 | [diff] [blame] | 2256 | // This is renderer-initiated. The only kinds of renderer-initiated |
Rakina Zata Amni | 557afb9 | 2021-07-17 04:39:57 | [diff] [blame] | 2257 | // navigations that are EXISTING_ENTRY are same-document navigations that |
| 2258 | // result in replacement (e.g. history.replaceState(), location.replace(), |
| 2259 | // forced replacements for trivial session history contexts). For these |
| 2260 | // cases, we reuse the last committed entry. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2261 | entry = GetLastCommittedEntry(); |
jam | 0576b13 | 2016-09-07 05:13:10 | [diff] [blame] | 2262 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 2263 | // TODO(crbug.com/40532777): Set page transition type to |
| 2264 | // PAGE_TRANSITION_LINK to avoid misleading interpretations (e.g. URLs |
| 2265 | // paired with PAGE_TRANSITION_TYPED that haven't actually been typed) as |
| 2266 | // well as to fix the inconsistency with what we report to observers |
| 2267 | // (PAGE_TRANSITION_LINK | PAGE_TRANSITION_CLIENT_REDIRECT). |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2268 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2269 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2270 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2271 | // 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] | 2272 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2273 | if (!is_same_document) |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2274 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2275 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2276 | } |
| 2277 | DCHECK(entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2278 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2279 | UpdateNavigationEntryDetails(entry, rfh, params, request, |
| 2280 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2281 | false /* is_new_entry */, commit_details); |
creis | 22a7b4c | 2016-04-28 07:20:30 | [diff] [blame] | 2282 | |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2283 | // The redirected to page should not inherit the favicon from the previous |
| 2284 | // page. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2285 | if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document) |
[email protected] | 91a4ff8 | 2012-10-29 20:29:48 | [diff] [blame] | 2286 | entry->GetFavicon() = FaviconStatus(); |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2287 | |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2288 | // Update the last committed index to reflect the committed entry. Do this |
| 2289 | // before calling DiscardNonCommittedEntriesWithCommitDetails, so that the |
| 2290 | // delegate sees the correct committed index when notified of navigation |
| 2291 | // state changes. (Otherwise CanGoBack may incorrectly return true, as in |
| 2292 | // https://crbug.com/1439948.) |
Charlie Reis | fbe5f102 | 2023-10-03 15:21:21 | [diff] [blame] | 2293 | last_committed_entry_index_ = GetIndexOfEntry(entry); |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2294 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2295 | // We should also usually discard the pending entry if it corresponds to a |
| 2296 | // different navigation, since that one is now likely canceled. In rare |
| 2297 | // cases, we leave the pending entry for another navigation in place when we |
| 2298 | // know it is still ongoing, to avoid a flicker in the omnibox (see |
| 2299 | // https://crbug.com/900036). |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2300 | // |
| 2301 | // Note that we need to use the "internal" version since we don't want to |
| 2302 | // actually change any other state, just kill the pointer. |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2303 | if (!keep_pending_entry) |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2304 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2305 | } |
| 2306 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2307 | void NavigationControllerImpl::RendererDidNavigateNewSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2308 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2309 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2310 | bool is_same_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2311 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2312 | bool previous_document_had_history_intervention_activation, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2313 | NavigationRequest* request, |
| 2314 | LoadCommittedDetails* commit_details) { |
avi | 25f5f9e | 2015-07-17 20:08:26 | [diff] [blame] | 2315 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2316 | ui::PAGE_TRANSITION_MANUAL_SUBFRAME)); |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2317 | // The NEW_SUBFRAME path should never result in an initial NavigationEntry. |
| 2318 | DCHECK(!commit_details->should_stay_as_initial_entry); |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 2319 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2320 | // Manual subframe navigations just get the current entry cloned so the user |
| 2321 | // can go back or forward to it. The actual subframe information will be |
| 2322 | // stored in the page state for each of those entries. This happens out of |
| 2323 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 2324 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2325 | << "that a last committed entry exists."; |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2326 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2327 | // The DCHECK below documents the fact that we don't know of any situation |
| 2328 | // where |replace_entry| is true for subframe navigations. This simplifies |
| 2329 | // reasoning about the replacement struct for subframes (see |
| 2330 | // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()). |
| 2331 | DCHECK(!replace_entry); |
| 2332 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2333 | // This FrameNavigationEntry might not end up being used in the |
| 2334 | // 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] | 2335 | const std::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2336 | request->common_params().initiator_origin; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2337 | std::optional<GURL> initiator_base_url; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 2338 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 2339 | initiator_base_url = request->common_params().initiator_base_url; |
| 2340 | } |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2341 | std::unique_ptr<PolicyContainerPolicies> policy_container_policies = |
| 2342 | ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document, |
| 2343 | request->GetURL()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2344 | bool protect_url_in_navigation_api = false; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2345 | if (is_same_document) { |
| 2346 | FrameNavigationEntry* previous_frame_entry = |
| 2347 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2348 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2349 | // FrameNavigationEntry. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2350 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2351 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2352 | previous_frame_entry->protect_url_in_navigation_api(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2353 | } else { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2354 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2355 | policy_container_policies && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2356 | ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2357 | policy_container_policies->referrer_policy); |
| 2358 | } |
| 2359 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2360 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2361 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2362 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2363 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2364 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2365 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
| 2366 | request->GetRedirectChain(), params.page_state, params.method, |
| 2367 | params.post_id, nullptr /* blob_url_loader_factory */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2368 | std::move(policy_container_policies), protect_url_in_navigation_api); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2369 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2370 | std::unique_ptr<NavigationEntryImpl> new_entry = |
| 2371 | GetLastCommittedEntry()->CloneAndReplace( |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2372 | std::move(frame_entry), is_same_document, rfh->frame_tree_node(), |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2373 | frame_tree_->root()); |
creis | e062d54 | 2015-08-25 02:01:55 | [diff] [blame] | 2374 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2375 | SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 2376 | replace_entry, previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2377 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2378 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2379 | // 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] | 2380 | // to replace, which can happen due to a unique name change. See |
| 2381 | // https://crbug.com/607205. For now, the call to CloneAndReplace() will |
| 2382 | // delete the |frame_entry| when the function exits if it doesn't get used. |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2383 | |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2384 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, false, |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2385 | rfh->IsNestedWithinFencedFrame(), commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2386 | } |
| 2387 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2388 | bool NavigationControllerImpl::RendererDidNavigateAutoSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2389 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2390 | const mojom::DidCommitProvisionalLoadParams& params, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2391 | bool is_same_document, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2392 | bool was_on_initial_empty_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2393 | NavigationRequest* request, |
| 2394 | LoadCommittedDetails* commit_details) { |
avi | 9f07a0c | 2015-02-18 22:51:29 | [diff] [blame] | 2395 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2396 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
| 2397 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2398 | // We're guaranteed to have a previously committed entry, and we now need to |
| 2399 | // handle navigation inside of a subframe in it without creating a new entry. |
| 2400 | DCHECK(GetLastCommittedEntry()); |
| 2401 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2402 | // For newly created subframes, we don't need to send a commit notification. |
| 2403 | // This is only necessary for history navigations in subframes. |
| 2404 | bool send_commit_notification = false; |
| 2405 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2406 | // If |nav_entry_id| is non-zero and matches an existing entry, this |
| 2407 | // is a history navigation. Update the last committed index accordingly. If |
| 2408 | // we don't recognize the |nav_entry_id|, it might be a recently |
| 2409 | // pruned entry. We'll handle it below. |
| 2410 | if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
| 2411 | int entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2412 | if (entry_index != -1 && entry_index != last_committed_entry_index_) { |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2413 | // Make sure that a subframe commit isn't changing the main frame's |
| 2414 | // origin. Otherwise the renderer process may be confused, leading to a |
| 2415 | // URL spoof. We can't check the path since that may change |
| 2416 | // (https://crbug.com/373041). |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2417 | // TODO(creis): For now, restrict this check to HTTP(S) origins, because |
| 2418 | // about:blank, file, and unique origins are more subtle to get right. |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 2419 | // We should use checks similar to RenderFrameHostImpl's |
| 2420 | // CanCommitUrlAndOrigin on the main frame during subframe commits. |
| 2421 | // See https://crbug.com/1209092. |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2422 | const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL(); |
| 2423 | const GURL& current_top_url = GetLastCommittedEntry()->GetURL(); |
| 2424 | if (current_top_url.SchemeIsHTTPOrHTTPS() && |
| 2425 | dest_top_url.SchemeIsHTTPOrHTTPS() && |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2426 | current_top_url.DeprecatedGetOriginAsURL() != |
| 2427 | dest_top_url.DeprecatedGetOriginAsURL()) { |
Chris Bookholt | 10f4b733 | 2022-02-14 18:25:44 | [diff] [blame] | 2428 | bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(), |
creis | fb6eeb6 | 2016-05-10 19:01:51 | [diff] [blame] | 2429 | bad_message::NC_AUTO_SUBFRAME); |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2430 | } |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2431 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2432 | // We only need to discard the pending entry in this history navigation |
| 2433 | // case. For newly created subframes, there was no pending entry. |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2434 | last_committed_entry_index_ = entry_index; |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2435 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2436 | |
| 2437 | // History navigations should send a commit notification. |
| 2438 | send_commit_notification = true; |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2439 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2440 | } |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 2441 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2442 | // This may be a "new auto" case where we add a new FrameNavigationEntry, or |
| 2443 | // it may be a "history auto" case where we update an existing one. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2444 | // We may want to update |last_committed|'s FrameNavigationEntry (if one |
| 2445 | // exists), or we may want to clobber it and create a new one. We update in |
| 2446 | // cases where the corresponding FrameNavigationEntry is conceptually similar |
| 2447 | // to the document described by the commit params: same-document |
| 2448 | // navigations, history traversal to an existing entry, and reloads (including |
| 2449 | // a "soft reload" where we navigate to the same url without flagging it as a |
| 2450 | // reload). But in the case of a different document that is not logically |
| 2451 | // related to the committed FrameNavigationEntry's document (cross-document, |
| 2452 | // not same url, not a reload, not a history traversal), we replace rather |
| 2453 | // than update. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2454 | // |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2455 | // In the case where we update, the FrameNavigationEntry will potentially be |
| 2456 | // shared across multiple NavigationEntries, and any updates will be reflected |
| 2457 | // in all of those NavigationEntries. In the replace case, any existing |
| 2458 | // sharing with other NavigationEntries will stop. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2459 | // |
| 2460 | // When navigating away from the initial empty document, we also update rather |
| 2461 | // than replace. Either update or replace will overwrite the initial empty |
| 2462 | // document state for |last_committed|, but if the FrameNavigationEntry for |
| 2463 | // the initial empty document is shared across multiple NavigationEntries (due |
| 2464 | // to a navigation in another frame), we want to make sure we overwrite the |
| 2465 | // initial empty document state everywhere this FrameNavigationEntry is used, |
| 2466 | // which is accompished by updating the existing FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2467 | NavigationEntryImpl* last_committed = GetLastCommittedEntry(); |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2468 | FrameNavigationEntry* last_committed_frame_entry = |
| 2469 | last_committed->GetFrameEntry(rfh->frame_tree_node()); |
| 2470 | NavigationEntryImpl::UpdatePolicy update_policy = |
| 2471 | NavigationEntryImpl::UpdatePolicy::kUpdate; |
| 2472 | if (request->common_params().navigation_type == |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 2473 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT && |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2474 | last_committed_frame_entry && |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2475 | last_committed_frame_entry->url() != params.url && |
| 2476 | !was_on_initial_empty_document) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2477 | update_policy = NavigationEntryImpl::UpdatePolicy::kReplace; |
| 2478 | } |
| 2479 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2480 | UpdateNavigationEntryDetails(last_committed, rfh, params, request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2481 | update_policy, false /* is_new_entry */, |
| 2482 | commit_details); |
creis | 625a0c7d | 2015-03-24 23:17:12 | [diff] [blame] | 2483 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2484 | return send_commit_notification; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2485 | } |
| 2486 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2487 | int NavigationControllerImpl::GetIndexOfEntry( |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 2488 | const NavigationEntryImpl* entry) const { |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 2489 | for (size_t i = 0; i < entries_.size(); ++i) { |
| 2490 | if (entries_[i].get() == entry) |
| 2491 | return i; |
| 2492 | } |
| 2493 | return -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2494 | } |
| 2495 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2496 | void NavigationControllerImpl::CopyStateFrom(NavigationController* temp, |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2497 | bool needs_reload) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2498 | NavigationControllerImpl* source = |
| 2499 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2500 | // Verify that we look new. |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 2501 | DCHECK_EQ(1, GetEntryCount()); |
| 2502 | DCHECK(GetLastCommittedEntry()->IsInitialEntry()); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 2503 | DCHECK(!GetPendingEntry()); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2504 | entries_.clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2505 | |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2506 | needs_reload_ = needs_reload; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 2507 | needs_reload_type_ = NeedsReloadType::kCopyStateFrom; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2508 | InsertEntriesFrom(source, source->GetEntryCount()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2509 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2510 | for (auto& it : source->session_storage_namespace_map_) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2511 | SessionStorageNamespaceImpl* source_namespace = |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2512 | static_cast<SessionStorageNamespaceImpl*>(it.second.get()); |
| 2513 | session_storage_namespace_map_[it.first] = source_namespace->Clone(); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2514 | } |
[email protected] | 4e6419c | 2010-01-15 04:50:34 | [diff] [blame] | 2515 | |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 2516 | FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2517 | } |
| 2518 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2519 | bool NavigationControllerImpl::CanPruneAllButLastCommitted() { |
Adithya Srinivasan | e8094c8 | 2024-06-26 22:45:02 | [diff] [blame] | 2520 | // If there is no last committed entry, we cannot prune. |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2521 | if (last_committed_entry_index_ == -1) |
| 2522 | return false; |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2523 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2524 | // We cannot prune if there is a pending entry at an existing entry index. |
| 2525 | // It may not commit, so we have to keep the last committed entry, and thus |
| 2526 | // there is no sensible place to keep the pending entry. It is ok to have |
| 2527 | // a new pending entry, which can optionally commit as a new navigation. |
| 2528 | if (pending_entry_index_ != -1) |
| 2529 | return false; |
| 2530 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2531 | return true; |
| 2532 | } |
| 2533 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2534 | void NavigationControllerImpl::PruneAllButLastCommitted() { |
| 2535 | PruneAllButLastCommittedInternal(); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2536 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2537 | DCHECK_EQ(0, last_committed_entry_index_); |
| 2538 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2539 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2540 | BroadcastHistoryOffsetAndLength(); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2541 | } |
| 2542 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2543 | void NavigationControllerImpl::PruneAllButLastCommittedInternal() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2544 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2545 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2546 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 2547 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
| 2548 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2549 | // Erase all entries but the last committed entry. There may still be a |
| 2550 | // new pending entry after this. |
| 2551 | entries_.erase(entries_.begin(), |
| 2552 | entries_.begin() + last_committed_entry_index_); |
| 2553 | entries_.erase(entries_.begin() + 1, entries_.end()); |
| 2554 | last_committed_entry_index_ = 0; |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2555 | } |
| 2556 | |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2557 | void NavigationControllerImpl::DeleteNavigationEntries( |
| 2558 | const DeletionPredicate& deletionPredicate) { |
| 2559 | // It is up to callers to check the invariants before calling this. |
| 2560 | CHECK(CanPruneAllButLastCommitted()); |
| 2561 | std::vector<int> delete_indices; |
| 2562 | for (size_t i = 0; i < entries_.size(); i++) { |
| 2563 | if (i != static_cast<size_t>(last_committed_entry_index_) && |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2564 | deletionPredicate.Run(entries_[i].get())) { |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2565 | delete_indices.push_back(i); |
| 2566 | } |
| 2567 | } |
| 2568 | if (delete_indices.empty()) |
| 2569 | return; |
| 2570 | |
| 2571 | if (delete_indices.size() == GetEntryCount() - 1U) { |
| 2572 | PruneAllButLastCommitted(); |
| 2573 | } else { |
| 2574 | // Do the deletion in reverse to preserve indices. |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 2575 | for (const auto& index : base::Reversed(delete_indices)) { |
| 2576 | RemoveEntryAtIndex(index); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2577 | } |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2578 | BroadcastHistoryOffsetAndLength(); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2579 | } |
| 2580 | delegate()->NotifyNavigationEntriesDeleted(); |
| 2581 | } |
| 2582 | |
Carlos Caballero | 35ce710c | 2019-09-19 10:59:45 | [diff] [blame] | 2583 | BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() { |
| 2584 | return back_forward_cache_; |
| 2585 | } |
| 2586 | |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2587 | NavigationEntryScreenshotCache* |
| 2588 | NavigationControllerImpl::GetNavigationEntryScreenshotCache() { |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 2589 | CHECK(frame_tree_->is_primary()); |
Khushal Sagar | 94ea2dc | 2024-08-06 17:15:26 | [diff] [blame] | 2590 | if (!nav_entry_screenshot_cache_ && |
| 2591 | NavigationTransitionConfig::AreBackForwardTransitionsEnabled()) { |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2592 | nav_entry_screenshot_cache_ = |
| 2593 | std::make_unique<NavigationEntryScreenshotCache>( |
| 2594 | BrowserContextImpl::From(browser_context_) |
| 2595 | ->GetNavigationEntryScreenshotManager() |
| 2596 | ->GetSafeRef(), |
| 2597 | this); |
| 2598 | } |
| 2599 | return nav_entry_screenshot_cache_.get(); |
| 2600 | } |
| 2601 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2602 | void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) { |
| 2603 | // It is not safe to call DiscardPendingEntry while NavigateToEntry is in |
| 2604 | // progress, since this will cause a use-after-free. (We only allow this |
| 2605 | // when the tab is being destroyed for shutdown, since it won't return to |
| 2606 | // NavigateToEntry in that case.) http://crbug.com/347742. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2607 | CHECK(!in_navigate_to_pending_entry_ || frame_tree_->IsBeingDestroyed()); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2608 | |
| 2609 | if (was_failure && pending_entry_) { |
| 2610 | failed_pending_entry_id_ = pending_entry_->GetUniqueID(); |
| 2611 | } else { |
| 2612 | failed_pending_entry_id_ = 0; |
| 2613 | } |
| 2614 | |
| 2615 | if (pending_entry_) { |
| 2616 | if (pending_entry_index_ == -1) |
Paul Semel | 7e51469e | 2022-07-12 12:16:33 | [diff] [blame] | 2617 | pending_entry_.ClearAndDelete(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2618 | pending_entry_index_ = -1; |
| 2619 | pending_entry_ = nullptr; |
| 2620 | } |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 2621 | |
| 2622 | // Ensure any refs to the current pending entry are ignored if they get |
| 2623 | // deleted, by clearing the set of known refs. All future pending entries will |
| 2624 | // only be affected by new refs. |
| 2625 | pending_entry_refs_.clear(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) { |
| 2629 | if (pending_entry_) |
| 2630 | pending_entry_->set_ssl_error(error); |
| 2631 | } |
| 2632 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 2633 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2634 | // static |
| 2635 | bool NavigationControllerImpl::ValidateDataURLAsString( |
| 2636 | const scoped_refptr<const base::RefCountedString>& data_url_as_string) { |
| 2637 | if (!data_url_as_string) |
| 2638 | return false; |
| 2639 | |
| 2640 | if (data_url_as_string->size() > kMaxLengthOfDataURLString) |
| 2641 | return false; |
| 2642 | |
| 2643 | // The number of characters that is enough for validating a data: URI. |
| 2644 | // From the GURL's POV, the only important part here is scheme, it doesn't |
| 2645 | // check the actual content. Thus we can take only the prefix of the url, to |
| 2646 | // avoid unneeded copying of a potentially long string. |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 2647 | constexpr size_t kDataUriPrefixMaxLen = 64; |
| 2648 | const size_t len = std::min(data_url_as_string->size(), kDataUriPrefixMaxLen); |
| 2649 | 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] | 2650 | if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme)) |
| 2651 | return false; |
| 2652 | |
| 2653 | return true; |
| 2654 | } |
| 2655 | #endif |
| 2656 | |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2657 | void NavigationControllerImpl::NotifyUserActivation() { |
| 2658 | // When a user activation occurs, ensure that all adjacent entries for the |
| 2659 | // same document clear their skippable bit, so that the history manipulation |
| 2660 | // intervention does not apply to them. |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2661 | const bool can_go_back = CanGoBack(); |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 2662 | SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false); |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2663 | // If the value of CanGoBack changes as a result of making some entries |
| 2664 | // non-skippable, then we must let the delegate know to update its UI state. |
| 2665 | // See https://crbug.com/1477784. |
| 2666 | if (!can_go_back && CanGoBack()) { |
| 2667 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
| 2668 | } |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2669 | } |
| 2670 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2671 | bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe( |
| 2672 | RenderFrameHostImpl* render_frame_host, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2673 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client, |
| 2674 | blink::LocalFrameToken initiator_frame_token, |
| 2675 | int initiator_process_id) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2676 | NavigationEntryImpl* entry = |
| 2677 | GetEntryWithUniqueID(render_frame_host->nav_entry_id()); |
| 2678 | if (!entry) |
| 2679 | return false; |
| 2680 | |
| 2681 | FrameNavigationEntry* frame_entry = |
| 2682 | entry->GetFrameEntry(render_frame_host->frame_tree_node()); |
| 2683 | if (!frame_entry) |
| 2684 | return false; |
| 2685 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2686 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2687 | render_frame_host->frame_tree_node(), entry, frame_entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2688 | ReloadType::NONE, false /* is_same_document_history_load */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2689 | true /* is_history_navigation_in_new_child */, initiator_frame_token, |
| 2690 | initiator_process_id); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2691 | |
| 2692 | if (!request) |
| 2693 | return false; |
| 2694 | |
arthursonzogni | f046d4a | 2019-12-12 19:08:10 | [diff] [blame] | 2695 | request->SetNavigationClient(std::move(*navigation_client)); |
Arthur Hemery | 06173ce | 2019-05-29 12:11:41 | [diff] [blame] | 2696 | |
Rakina Zata Amni | 1c83b08 | 2023-02-08 01:09:00 | [diff] [blame] | 2697 | SCOPED_CRASH_KEY_STRING256( |
| 2698 | "Bug1400009", "req_url", |
| 2699 | request->GetURL().GetWithEmptyPath().possibly_invalid_spec()); |
| 2700 | SCOPED_CRASH_KEY_NUMBER( |
| 2701 | "Bug1400009", "nav_entry_si", |
| 2702 | entry->site_instance() ? ((int)entry->site_instance()->GetId()) : -1); |
| 2703 | SCOPED_CRASH_KEY_NUMBER("Bug1400009", "fne_si", |
| 2704 | frame_entry->site_instance() |
| 2705 | ? ((int)frame_entry->site_instance()->GetId()) |
| 2706 | : -1); |
| 2707 | bool has_sig = |
| 2708 | (frame_entry->site_instance() && frame_entry->site_instance()->group()); |
| 2709 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_exists", has_sig); |
| 2710 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_has_rvh", |
| 2711 | has_sig ? (!!frame_tree_->GetRenderViewHost( |
| 2712 | frame_entry->site_instance()->group())) |
| 2713 | : false); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2714 | render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request), |
| 2715 | ReloadType::NONE); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2716 | |
| 2717 | return true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2718 | } |
| 2719 | |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2720 | bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) { |
| 2721 | NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex()); |
| 2722 | if (!entry) |
| 2723 | return false; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 2724 | |
| 2725 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 2726 | // We should never navigate to an existing initial NavigationEntry that is |
| 2727 | // the initial NavigationEntry for the initial empty document that hasn't |
| 2728 | // been overridden by the synchronous about:blank commit, to preserve |
| 2729 | // legacy behavior where trying to reload when the main frame is on the |
| 2730 | // initial empty document won't result in a navigation. See also |
| 2731 | // https://crbug.com/1277414. |
| 2732 | return false; |
| 2733 | } |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2734 | FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node); |
| 2735 | if (!frame_entry) |
| 2736 | return false; |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2737 | ReloadType reload_type = ReloadType::NORMAL; |
| 2738 | entry->set_reload_type(reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2739 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2740 | frame_tree_node, entry, frame_entry, reload_type, |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2741 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2742 | false /* is_history_navigation_in_new_child */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2743 | std::nullopt /* initiator_frame_token */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2744 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2745 | if (!request) |
| 2746 | return false; |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2747 | frame_tree_node->navigator().Navigate(std::move(request), reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2748 | return true; |
| 2749 | } |
| 2750 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2751 | void NavigationControllerImpl::NavigateFromFrameProxy( |
| 2752 | RenderFrameHostImpl* render_frame_host, |
| 2753 | const GURL& url, |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2754 | const blink::LocalFrameToken* initiator_frame_token, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2755 | int initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2756 | const std::optional<url::Origin>& initiator_origin, |
| 2757 | const std::optional<GURL>& initiator_base_url, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2758 | bool is_renderer_initiated, |
| 2759 | SiteInstance* source_site_instance, |
| 2760 | const Referrer& referrer, |
| 2761 | ui::PageTransition page_transition, |
| 2762 | bool should_replace_current_entry, |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 2763 | blink::NavigationDownloadPolicy download_policy, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2764 | const std::string& method, |
| 2765 | scoped_refptr<network::ResourceRequestBody> post_body, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 2766 | const std::string& extra_headers, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2767 | network::mojom::SourceLocationPtr source_location, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2768 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 2769 | bool is_form_submission, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2770 | const std::optional<blink::Impression>& impression, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 2771 | blink::mojom::NavigationInitiatorActivationAndAdStatus |
| 2772 | initiator_activation_and_ad_status, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 2773 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 2774 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 2775 | bool is_unfenced_top_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 2776 | bool force_new_browsing_instance, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2777 | bool is_container_initiated, |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 2778 | bool has_rel_opener, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 2779 | net::StorageAccessApiStatus storage_access_api_status, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2780 | std::optional<std::u16string> embedder_shared_storage_context) { |
Lukasz Anforowicz | 63f3b943 | 2019-05-30 05:42:58 | [diff] [blame] | 2781 | if (is_renderer_initiated) |
| 2782 | DCHECK(initiator_origin.has_value()); |
| 2783 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2784 | FrameTreeNode* node = render_frame_host->frame_tree_node(); |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2785 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2786 | // Don't allow an entry replacement if there is no entry to replace. |
| 2787 | // http://crbug.com/457149 |
| 2788 | if (GetEntryCount() == 0) |
| 2789 | should_replace_current_entry = false; |
| 2790 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2791 | // Create a NavigationEntry for the transfer, without making it the pending |
| 2792 | // entry. Subframe transfers should have a clone of the last committed entry |
| 2793 | // with a FrameNavigationEntry for the target frame. Main frame transfers |
| 2794 | // should have a new NavigationEntry. |
| 2795 | // TODO(creis): Make this unnecessary by creating (and validating) the params |
| 2796 | // directly, passing them to the destination RenderFrameHost. See |
| 2797 | // https://crbug.com/536906. |
| 2798 | std::unique_ptr<NavigationEntryImpl> entry; |
Harkiran Bolaria | e1b5158b | 2021-09-16 19:03:26 | [diff] [blame] | 2799 | if (!render_frame_host->is_main_frame()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2800 | // Subframe case: create FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2801 | DCHECK(GetLastCommittedEntry()); |
| 2802 | entry = GetLastCommittedEntry()->Clone(); |
| 2803 | entry->set_extra_headers(extra_headers); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2804 | // TODO(arthursonzogni): What about |is_renderer_initiated|? |
| 2805 | // Renderer-initiated navigation that target a remote frame are currently |
| 2806 | // classified as browser-initiated when this one has already navigated. |
| 2807 | // See https://crbug.com/722251. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2808 | // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter |
| 2809 | // to CreateNavigationRequestFromLoadParams(), so while kReplace might |
| 2810 | // remove child FrameNavigationEntries (e.g., if this is a cross-process |
| 2811 | // same-document navigation), they will still be present in the |
| 2812 | // committed NavigationEntry that will be referenced to construct the new |
| 2813 | // FrameNavigationEntry tree when this navigation commits. |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2814 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2815 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2816 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2817 | std::nullopt /* commit_origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2818 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2819 | blob_url_loader_factory, nullptr /* policy_container_policies */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2820 | } else { |
| 2821 | // Main frame case. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 2822 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 2823 | // to adjust the original input url if needed. For inner frames such as |
| 2824 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 2825 | // input urls by users. |
| 2826 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2827 | std::optional<GURL> source_process_site_url = std::nullopt; |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2828 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 2829 | source_process_site_url = |
| 2830 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 2831 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2832 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2833 | url, referrer, initiator_origin, initiator_base_url, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2834 | source_process_site_url, page_transition, is_renderer_initiated, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2835 | extra_headers, browser_context_, blob_url_loader_factory, |
| 2836 | rewrite_virtual_urls)); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2837 | entry->root_node()->frame_entry->set_source_site_instance( |
| 2838 | static_cast<SiteInstanceImpl*>(source_site_instance)); |
| 2839 | entry->root_node()->frame_entry->set_method(method); |
| 2840 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2841 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2842 | bool override_user_agent = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2843 | if (GetLastCommittedEntry()->GetIsOverridingUserAgent()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2844 | entry->SetIsOverridingUserAgent(true); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2845 | override_user_agent = true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2846 | } |
| 2847 | // TODO(creis): Set user gesture and intent received timestamp on Android. |
| 2848 | |
| 2849 | // We may not have successfully added the FrameNavigationEntry to |entry| |
| 2850 | // above (per https://crbug.com/608402), in which case we create it from |
| 2851 | // scratch. This works because we do not depend on |frame_entry| being inside |
| 2852 | // |entry| during NavigateToEntry. This will go away when we shortcut this |
| 2853 | // further in https://crbug.com/536906. |
| 2854 | scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node)); |
| 2855 | if (!frame_entry) { |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2856 | frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2857 | node->unique_name(), -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2858 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2859 | std::nullopt /* origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2860 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2861 | blob_url_loader_factory, nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2862 | false /* protect_url_in_navigation_api */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2863 | } |
| 2864 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2865 | LoadURLParams params(url); |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2866 | params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token); |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2867 | params.initiator_process_id = initiator_process_id; |
Nasko Oskov | 93e7c55c | 2018-12-19 01:59:29 | [diff] [blame] | 2868 | params.initiator_origin = initiator_origin; |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2869 | params.initiator_base_url = initiator_base_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2870 | params.source_site_instance = source_site_instance; |
| 2871 | params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT; |
| 2872 | params.transition_type = page_transition; |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2873 | params.frame_tree_node_id = node->frame_tree_node_id(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2874 | params.referrer = referrer; |
| 2875 | /* params.redirect_chain: skip */ |
| 2876 | params.extra_headers = extra_headers; |
| 2877 | params.is_renderer_initiated = is_renderer_initiated; |
| 2878 | params.override_user_agent = UA_OVERRIDE_INHERIT; |
| 2879 | /* params.base_url_for_data_url: skip */ |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 2880 | /* params.virtual_url_for_special_cases: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2881 | /* params.data_url_as_string: skip */ |
| 2882 | params.post_data = post_body; |
| 2883 | params.can_load_local_resources = false; |
Kevin McNee | e60e76b | 2019-11-27 20:01:58 | [diff] [blame] | 2884 | /* params.should_replace_current_entry: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2885 | /* params.frame_name: skip */ |
| 2886 | // TODO(clamy): See if user gesture should be propagated to this function. |
| 2887 | params.has_user_gesture = false; |
| 2888 | params.should_clear_history_list = false; |
| 2889 | params.started_from_context_menu = false; |
| 2890 | /* params.navigation_ui_data: skip */ |
| 2891 | /* params.input_start: skip */ |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 2892 | params.was_activated = blink::mojom::WasActivatedOption::kUnknown; |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 2893 | /* params.reload_type: skip */ |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2894 | params.impression = impression; |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 2895 | params.download_policy = std::move(download_policy); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 2896 | params.is_form_submission = is_form_submission; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 2897 | params.initiator_activation_and_ad_status = |
| 2898 | initiator_activation_and_ad_status; |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 2899 | params.has_rel_opener = has_rel_opener; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2900 | |
| 2901 | std::unique_ptr<NavigationRequest> request = |
| 2902 | CreateNavigationRequestFromLoadParams( |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2903 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2904 | false /* has_user_gesture */, std::move(source_location), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 2905 | ReloadType::NONE, entry.get(), frame_entry.get(), |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 2906 | navigation_start_time, is_embedder_initiated_fenced_frame_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2907 | is_unfenced_top_navigation, is_container_initiated, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 2908 | storage_access_api_status, embedder_shared_storage_context); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2909 | |
| 2910 | if (!request) |
| 2911 | return; |
| 2912 | |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 2913 | // Force the navigation to take place in a new browsing instance. |
| 2914 | // This is used by _unfencedTop in fenced frames to ensure that navigations |
| 2915 | // leaving the fenced context create a new browsing instance. |
| 2916 | if (force_new_browsing_instance) { |
| 2917 | request->coop_status().ForceBrowsingInstanceSwap(); |
| 2918 | } |
| 2919 | |
Arthur Hemery | 94874276 | 2019-09-18 10:06:24 | [diff] [blame] | 2920 | // At this stage we are proceeding with this navigation. If this was renderer |
| 2921 | // initiated with user gesture, we need to make sure we clear up potential |
| 2922 | // remains of a cancelled browser initiated navigation to avoid URL spoofs. |
| 2923 | DiscardNonCommittedEntries(); |
| 2924 | |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2925 | node->navigator().Navigate(std::move(request), ReloadType::NONE); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2926 | } |
| 2927 | |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2928 | void NavigationControllerImpl::SetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2929 | const StoragePartitionConfig& partition_config, |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 2930 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2931 | if (!session_storage_namespace) |
| 2932 | return; |
| 2933 | |
| 2934 | // We can't overwrite an existing SessionStorage without violating spec. |
| 2935 | // Attempts to do so may give a tab access to another tab's session storage |
| 2936 | // so die hard on an error. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2937 | bool successful_insert = |
| 2938 | session_storage_namespace_map_ |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2939 | .insert(std::make_pair(partition_config, |
Aaron Colwell | f3b316e | 2021-03-11 20:17:05 | [diff] [blame] | 2940 | static_cast<SessionStorageNamespaceImpl*>( |
| 2941 | session_storage_namespace))) |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2942 | .second; |
| 2943 | CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace"; |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2944 | } |
| 2945 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2946 | bool NavigationControllerImpl::IsUnmodifiedBlankTab() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2947 | return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() && |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2948 | !frame_tree_->has_accessed_initial_main_document(); |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 2949 | } |
| 2950 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2951 | SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2952 | const StoragePartitionConfig& partition_config) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2953 | StoragePartition* partition = |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 2954 | browser_context_->GetStoragePartition(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2955 | DOMStorageContextWrapper* context_wrapper = |
| 2956 | static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext()); |
| 2957 | |
| 2958 | SessionStorageNamespaceMap::const_iterator it = |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2959 | session_storage_namespace_map_.find(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2960 | if (it != session_storage_namespace_map_.end()) { |
| 2961 | // Ensure that this namespace actually belongs to this partition. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2962 | DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get()) |
| 2963 | ->IsFromContext(context_wrapper)); |
Aaron Colwell | b731a0ae | 2021-03-19 19:14:47 | [diff] [blame] | 2964 | |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2965 | return it->second.get(); |
| 2966 | } |
| 2967 | |
| 2968 | // Create one if no one has accessed session storage for this partition yet. |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2969 | scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace = |
| 2970 | SessionStorageNamespaceImpl::Create(context_wrapper); |
| 2971 | SessionStorageNamespaceImpl* session_storage_namespace_ptr = |
| 2972 | session_storage_namespace.get(); |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2973 | session_storage_namespace_map_[partition_config] = |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2974 | std::move(session_storage_namespace); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2975 | |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2976 | return session_storage_namespace_ptr; |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | SessionStorageNamespace* |
| 2980 | NavigationControllerImpl::GetDefaultSessionStorageNamespace() { |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2981 | return GetSessionStorageNamespace( |
| 2982 | StoragePartitionConfig::CreateDefault(GetBrowserContext())); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | const SessionStorageNamespaceMap& |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2986 | NavigationControllerImpl::GetSessionStorageNamespaceMap() { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2987 | return session_storage_namespace_map_; |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2988 | } |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2989 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2990 | bool NavigationControllerImpl::NeedsReload() { |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 2991 | return needs_reload_; |
| 2992 | } |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2993 | |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2994 | void NavigationControllerImpl::SetNeedsReload() { |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 2995 | SetNeedsReload(NeedsReloadType::kRequestedByClient); |
| 2996 | } |
| 2997 | |
| 2998 | void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) { |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2999 | needs_reload_ = true; |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 3000 | needs_reload_type_ = type; |
jaekyun | c8cefa8 | 2015-01-09 20:14:54 | [diff] [blame] | 3001 | |
| 3002 | if (last_committed_entry_index_ != -1) { |
| 3003 | entries_[last_committed_entry_index_]->SetTransitionType( |
| 3004 | ui::PAGE_TRANSITION_RELOAD); |
| 3005 | } |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 3006 | } |
| 3007 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3008 | void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) { |
Kevin McNee | 0516477 | 2019-09-03 17:24:57 | [diff] [blame] | 3009 | DCHECK_LT(index, GetEntryCount()); |
| 3010 | DCHECK_NE(index, last_committed_entry_index_); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3011 | DiscardNonCommittedEntries(); |
| 3012 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 3013 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3014 | entries_.erase(entries_.begin() + index); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 3015 | if (last_committed_entry_index_ > index) |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 3016 | last_committed_entry_index_--; |
| 3017 | } |
| 3018 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3019 | NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3020 | // If there is no pending_entry_, there should be no pending_entry_index_. |
| 3021 | DCHECK(pending_entry_ || pending_entry_index_ == -1); |
| 3022 | |
| 3023 | // 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] | 3024 | // at that index. An exception is while a reload of a post commit error page |
| 3025 | // is ongoing; in that case pending entry will point to the entry replaced |
| 3026 | // by the error. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3027 | DCHECK(pending_entry_index_ == -1 || |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3028 | pending_entry_ == GetEntryAtIndex(pending_entry_index_) || |
| 3029 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3030 | |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 3031 | return pending_entry_; |
| 3032 | } |
| 3033 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3034 | int NavigationControllerImpl::GetPendingEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3035 | // The pending entry index must always be less than the number of entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3036 | DCHECK_LT(pending_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 3037 | return pending_entry_index_; |
| 3038 | } |
| 3039 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 3040 | void NavigationControllerImpl::InsertOrReplaceEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 3041 | std::unique_ptr<NavigationEntryImpl> entry, |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3042 | bool replace, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 3043 | bool was_post_commit_error, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 3044 | bool in_fenced_frame_tree, |
| 3045 | LoadCommittedDetails* commit_details) { |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 3046 | // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME` |
| 3047 | // set because: |
| 3048 | // 1) They don't influence the history of the outer page. |
| 3049 | // 2) They are always replace only navigation (there is always only one entry |
| 3050 | // in their history stack). |
| 3051 | // 3) Are not top level navigations and appear similar to iframes. |
| 3052 | // Navigations of the fenced frame might create a new NavigationEntry, which |
| 3053 | // will call this function. Non fenced frame navigations will never have |
| 3054 | // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call |
| 3055 | // InsertOrReplaceEntry. |
| 3056 | DCHECK_EQ(in_fenced_frame_tree, |
| 3057 | ui::PageTransitionCoreTypeIs(entry->GetTransitionType(), |
| 3058 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3059 | |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 3060 | // If the pending_entry_index_ is -1, the navigation was to a new page, and we |
| 3061 | // need to keep continuity with the pending entry, so copy the pending entry's |
| 3062 | // unique ID to the committed entry. If the pending_entry_index_ isn't -1, |
| 3063 | // then the renderer navigated on its own, independent of the pending entry, |
| 3064 | // so don't copy anything. |
| 3065 | if (pending_entry_ && pending_entry_index_ == -1) |
| 3066 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3067 | |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 3068 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3069 | |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3070 | // When replacing, don't prune the forward history. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3071 | if (replace || was_post_commit_error) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 3072 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3073 | entries_[last_committed_entry_index_].get(), entry.get()); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3074 | // If the new entry is a post-commit error page, we store the current last |
| 3075 | // committed entry to the side so that we can put it back when navigating |
| 3076 | // away from the error. |
| 3077 | if (was_post_commit_error) { |
| 3078 | DCHECK(!entry_replaced_by_post_commit_error_); |
| 3079 | entry_replaced_by_post_commit_error_ = |
| 3080 | std::move(entries_[last_committed_entry_index_]); |
| 3081 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3082 | entries_[last_committed_entry_index_] = std::move(entry); |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3083 | return; |
| 3084 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3085 | |
creis | 37979a6 | 2015-08-04 19:48:18 | [diff] [blame] | 3086 | // We shouldn't see replace == true when there's no committed entries. |
| 3087 | DCHECK(!replace); |
| 3088 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 3089 | PruneForwardEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3090 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3091 | PruneOldestSkippableEntryIfFull(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3092 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3093 | entries_.push_back(std::move(entry)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3094 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3095 | } |
| 3096 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3097 | void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 3098 | if (entries_.size() < max_entry_count()) |
| 3099 | return; |
| 3100 | |
| 3101 | DCHECK_EQ(max_entry_count(), entries_.size()); |
| 3102 | DCHECK_GT(last_committed_entry_index_, 0); |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3103 | CHECK_EQ(pending_entry_index_, -1); |
| 3104 | |
| 3105 | int index = 0; |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 3106 | // Retrieve the oldest skippable entry. |
| 3107 | for (; index < GetEntryCount(); index++) { |
| 3108 | if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 3109 | break; |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | // If there is no skippable entry or if it is the last committed entry then |
| 3113 | // fall back to pruning the oldest entry. It is not safe to prune the last |
| 3114 | // committed entry. |
| 3115 | if (index == GetEntryCount() || index == last_committed_entry_index_) |
| 3116 | index = 0; |
| 3117 | |
| 3118 | bool should_succeed = RemoveEntryAtIndex(index); |
| 3119 | DCHECK_EQ(true, should_succeed); |
| 3120 | |
| 3121 | NotifyPrunedEntries(this, index, 1); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 3122 | } |
| 3123 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3124 | std::vector<base::WeakPtr<NavigationRequest>> |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3125 | NavigationControllerImpl::NavigateToExistingPendingEntry( |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3126 | ReloadType reload_type, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3127 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3128 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3129 | soft_navigation_heuristics_task_id, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3130 | const std::string* navigation_api_key) { |
Alexander Timin | 3a92df7 | 2019-09-20 11:59:50 | [diff] [blame] | 3131 | TRACE_EVENT0("navigation", |
| 3132 | "NavigationControllerImpl::NavigateToExistingPendingEntry"); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3133 | DCHECK(pending_entry_); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3134 | DCHECK(IsInitialNavigation() || pending_entry_index_ != -1); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3135 | if (pending_entry_index_ != -1) { |
| 3136 | // The pending entry may not be in entries_ if a post-commit error page is |
| 3137 | // showing. |
| 3138 | DCHECK(pending_entry_ == entries_[pending_entry_index_].get() || |
| 3139 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
| 3140 | } |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3141 | DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL())); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3142 | bool is_forced_reload = needs_reload_; |
[email protected] | 72097fd0 | 2010-01-21 23:36:01 | [diff] [blame] | 3143 | needs_reload_ = false; |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3144 | FrameTreeNode* root = frame_tree_->root(); |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3145 | int nav_entry_id = pending_entry_->GetUniqueID(); |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3146 | // Only pass down the soft_navigation_heuristics_task_id when the initiator is |
| 3147 | // the same as the top level frame being navigated. |
| 3148 | if (root->current_frame_host() != initiator_rfh) { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3149 | soft_navigation_heuristics_task_id = std::nullopt; |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3150 | } |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3151 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3152 | // If we were navigating to a slow-to-commit page, and the user performs |
| 3153 | // a session history navigation to the last committed page, RenderViewHost |
| 3154 | // will force the throbber to start, but WebKit will essentially ignore the |
| 3155 | // navigation, and won't send a message to stop the throbber. To prevent this |
| 3156 | // from happening, we drop the navigation here and stop the slow-to-commit |
| 3157 | // page from loading (which would normally happen during the navigation). |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3158 | if (pending_entry_index_ == last_committed_entry_index_ && |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 3159 | !pending_entry_->IsRestored() && |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3160 | pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) { |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3161 | frame_tree_->StopLoading(); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 3162 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3163 | DiscardNonCommittedEntries(); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3164 | return {}; |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3165 | } |
| 3166 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3167 | std::optional<blink::LocalFrameToken> initiator_frame_token; |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3168 | int initiator_process_id = ChildProcessHost::kInvalidUniqueID; |
| 3169 | if (initiator_rfh) { |
| 3170 | initiator_frame_token = initiator_rfh->GetFrameToken(); |
| 3171 | initiator_process_id = initiator_rfh->GetProcess()->GetID(); |
| 3172 | DCHECK(initiator_frame_token); |
| 3173 | } |
| 3174 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 3175 | // Compare FrameNavigationEntries to see which frames in the tree need to be |
| 3176 | // navigated. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3177 | std::vector<std::unique_ptr<NavigationRequest>> same_document_loads; |
| 3178 | std::vector<std::unique_ptr<NavigationRequest>> different_document_loads; |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3179 | FindFramesToNavigate(root, reload_type, initiator_frame_token, |
| 3180 | initiator_process_id, soft_navigation_heuristics_task_id, |
| 3181 | &same_document_loads, &different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3182 | |
| 3183 | if (same_document_loads.empty() && different_document_loads.empty()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3184 | // We were unable to match any frames to navigate. This can happen if a |
| 3185 | // history navigation targets a subframe that no longer exists |
| 3186 | // (https://crbug.com/705550). In this case, we need to update the current |
| 3187 | // history entry to the pending one but keep the main document loaded. We |
| 3188 | // also need to ensure that observers are informed about the updated |
| 3189 | // current history entry (e.g., for greying out back/forward buttons), and |
| 3190 | // that renderer processes update their history offsets. The easiest way |
| 3191 | // to do all that is to schedule a "redundant" same-document navigation in |
| 3192 | // the main frame. |
| 3193 | // |
| 3194 | // Note that we don't want to remove this history entry, as it might still |
| 3195 | // be valid later, since a frame that it's targeting may be recreated. |
| 3196 | // |
| 3197 | // TODO(alexmos, creis): This behavior isn't ideal, as the user would |
| 3198 | // need to repeat history navigations until finding the one that works. |
| 3199 | // Consider changing this behavior to keep looking for the first valid |
| 3200 | // history entry that finds frames to navigate. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3201 | std::unique_ptr<NavigationRequest> navigation_request = |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3202 | CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3203 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3204 | ReloadType::NONE /* reload_type */, |
| 3205 | true /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3206 | false /* is_history_navigation_in_new_child */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3207 | initiator_frame_token, initiator_process_id); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3208 | if (!navigation_request) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3209 | // If this navigation cannot start, delete the pending NavigationEntry. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3210 | DiscardPendingEntry(false); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3211 | return {}; |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3212 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3213 | same_document_loads.push_back(std::move(navigation_request)); |
| 3214 | |
| 3215 | // Sanity check that we never take this branch for any kinds of reloads, |
| 3216 | // as those should've queued a different-document load in the main frame. |
| 3217 | DCHECK(!is_forced_reload); |
| 3218 | DCHECK_EQ(reload_type, ReloadType::NONE); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3219 | } |
| 3220 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3221 | // If the initiator is top-navigation sandboxed, then track whether this |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3222 | // navigation affects any frame outside the frame's subtree. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3223 | if (initiator_rfh && initiator_rfh->IsSandboxed( |
| 3224 | network::mojom::WebSandboxFlags::kTopNavigation)) { |
| 3225 | bool navigates_inside_tree = DoesSandboxNavigationStayWithinSubtree( |
| 3226 | initiator_rfh, same_document_loads) && |
| 3227 | DoesSandboxNavigationStayWithinSubtree( |
| 3228 | initiator_rfh, different_document_loads); |
Dave Tapuska | 716ed3af | 2019-09-23 18:45:50 | [diff] [blame] | 3229 | // Count the navigations as web use counters so we can determine |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3230 | // the number of pages that trigger this. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3231 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 3232 | initiator_rfh, |
| 3233 | navigates_inside_tree |
| 3234 | ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree |
| 3235 | : blink::mojom::WebFeature:: |
| 3236 | kSandboxBackForwardAffectsFramesOutsideSubtree); |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3237 | |
| 3238 | // If the navigation occurred outside the tree discard it because |
| 3239 | // the sandboxed frame didn't have permission to navigate outside |
| 3240 | // its tree. If it is possible that the navigation is both inside and |
| 3241 | // outside the frame tree and we discard it entirely because we don't |
| 3242 | // want to end up in a history state that didn't exist before. |
Dominic Farolino | 05744004 | 2022-01-19 18:18:14 | [diff] [blame] | 3243 | if (!navigates_inside_tree) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3244 | // If a |navigation_api_key| was provided, this navigation originated from |
| 3245 | // the navigation API. Notify the renderer that the navigation was |
| 3246 | // cancelled so the navigation API can fire an error event and reject the |
| 3247 | // relevant promise. |
| 3248 | if (navigation_api_key) { |
| 3249 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 3250 | *navigation_api_key, |
| 3251 | blink::mojom::TraverseCancelledReason::kSandboxViolation); |
| 3252 | } |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3253 | DiscardPendingEntry(false); |
Rakina Zata Amni | 58681c6 | 2024-06-25 06:32:13 | [diff] [blame] | 3254 | |
| 3255 | for (auto& unused_request : same_document_loads) { |
| 3256 | unused_request->set_navigation_discard_reason( |
| 3257 | NavigationDiscardReason::kNeverStarted); |
| 3258 | } |
| 3259 | for (auto& unused_request : different_document_loads) { |
| 3260 | unused_request->set_navigation_discard_reason( |
| 3261 | NavigationDiscardReason::kNeverStarted); |
| 3262 | } |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3263 | return {}; |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3264 | } |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3265 | } |
| 3266 | |
Nate Chapin | 6c43c02 | 2023-02-13 23:32:42 | [diff] [blame] | 3267 | // If it is possible that this traverse may involve a same-document navigation |
| 3268 | // in the initiator and there is a Navigation API key involved, then we may |
| 3269 | // need to notify the initiator if it fails. (The early returns above either |
| 3270 | // do not involve these cases or already notify the initiator.) |
| 3271 | // The event only needs to fire for the initiator, and only if the initiator |
| 3272 | // itself is performing a same-document navigation (because the event will not |
| 3273 | // fire if it navigates cross-document). |
| 3274 | if (navigation_api_key) { |
| 3275 | for (auto& item : same_document_loads) { |
| 3276 | if (item->frame_tree_node() == initiator_rfh->frame_tree_node()) { |
| 3277 | item->set_pending_navigation_api_key(*navigation_api_key); |
| 3278 | break; |
| 3279 | } |
| 3280 | } |
| 3281 | } |
| 3282 | |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3283 | // BackForwardCache: |
| 3284 | // Navigate immediately if the document is in the BackForwardCache. |
Mingyu Lei | 7584b6b | 2023-04-13 03:02:56 | [diff] [blame] | 3285 | if (back_forward_cache_.GetOrEvictEntry(nav_entry_id).has_value()) { |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3286 | TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest"); |
| 3287 | DCHECK_EQ(reload_type, ReloadType::NONE); |
| 3288 | auto navigation_request = CreateNavigationRequestFromEntry( |
| 3289 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
| 3290 | ReloadType::NONE, false /* is_same_document_history_load */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3291 | false /* is_history_navigation_in_new_child */, initiator_frame_token, |
| 3292 | initiator_process_id); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3293 | base::WeakPtr<NavigationRequest> request = navigation_request->GetWeakPtr(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3294 | root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE); |
Rakina Zata Amni | 58681c6 | 2024-06-25 06:32:13 | [diff] [blame] | 3295 | |
| 3296 | for (auto& unused_request : same_document_loads) { |
| 3297 | unused_request->set_navigation_discard_reason( |
| 3298 | NavigationDiscardReason::kNeverStarted); |
| 3299 | } |
| 3300 | for (auto& unused_request : different_document_loads) { |
| 3301 | unused_request->set_navigation_discard_reason( |
| 3302 | NavigationDiscardReason::kNeverStarted); |
| 3303 | } |
| 3304 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3305 | std::vector<base::WeakPtr<NavigationRequest>> bf_cache_request; |
| 3306 | if (request) { |
| 3307 | bf_cache_request.push_back(std::move(request)); |
| 3308 | } |
| 3309 | return bf_cache_request; |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | // History navigation might try to reuse a specific BrowsingInstance, already |
| 3313 | // used by a page in the cache. To avoid having two different main frames that |
| 3314 | // live in the same BrowsingInstance, evict the all pages with this |
| 3315 | // BrowsingInstance from the cache. |
| 3316 | // |
| 3317 | // For example, take the following scenario: |
| 3318 | // |
| 3319 | // A1 = Some page on a.com |
| 3320 | // A2 = Some other page on a.com |
| 3321 | // B3 = An uncacheable page on b.com |
| 3322 | // |
| 3323 | // Then the following navigations occur: |
| 3324 | // A1->A2->B3->A1 |
| 3325 | // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't |
| 3326 | // take its place) and A1 will be created in the same BrowsingInstance (and |
| 3327 | // SiteInstance), as A2. |
| 3328 | // |
| 3329 | // If we didn't do anything, both A1 and A2 would remain alive in the same |
| 3330 | // BrowsingInstance/SiteInstance, which is unsupported by |
| 3331 | // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict |
| 3332 | // A2 from the cache. |
| 3333 | if (pending_entry_->site_instance()) { |
| 3334 | back_forward_cache_.EvictFramesInRelatedSiteInstances( |
| 3335 | pending_entry_->site_instance()); |
| 3336 | } |
| 3337 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3338 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored", |
| 3339 | pending_entry_ && pending_entry_->IsRestored()); |
| 3340 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id", |
| 3341 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3342 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index", |
| 3343 | pending_entry_index_); |
| 3344 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index", |
| 3345 | last_committed_entry_index_); |
| 3346 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size()); |
| 3347 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial", |
| 3348 | pending_entry_ && pending_entry_->IsInitialEntry()); |
| 3349 | SCOPED_CRASH_KEY_BOOL( |
| 3350 | "nav_reentrancy", "pending_entry_initial2", |
| 3351 | pending_entry_ && |
| 3352 | pending_entry_->IsInitialEntryNotForSynchronousAboutBlank()); |
| 3353 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav", |
| 3354 | IsInitialNavigation()); |
| 3355 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav", |
| 3356 | IsInitialBlankNavigation()); |
| 3357 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload); |
| 3358 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type", |
| 3359 | (int)pending_reload_); |
| 3360 | |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3361 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3362 | CHECK(!in_navigate_to_pending_entry_); |
| 3363 | in_navigate_to_pending_entry_ = true; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3364 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3365 | // If the navigation-reentrancy is caused by calling |
| 3366 | // NavigateToExistingPendingEntry twice, this will note the previous call's |
| 3367 | // pending entry's ID. |
| 3368 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id", |
| 3369 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3370 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3371 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3372 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3373 | // function. |
| 3374 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3375 | |
Nate Chapin | 154b14b | 2023-02-17 18:28:16 | [diff] [blame] | 3376 | // If there is a main-frame same-document history navigation, we may defer |
| 3377 | // the subframe history navigations in order to give JS in the main frame the |
| 3378 | // opportunity to cancel the entire traverse via the navigate event. In that |
| 3379 | // case, we need to stash the main frame request's navigation token on the |
| 3380 | // subframes, so they can look up the main frame request and defer themselves |
| 3381 | // until it completes. |
| 3382 | if (!same_document_loads.empty() && |
| 3383 | same_document_loads.at(0)->frame_tree_node()->IsMainFrame()) { |
| 3384 | NavigationRequest* main_frame_request = same_document_loads.at(0).get(); |
| 3385 | // The token will only be returned in cases where deferring the navigation |
| 3386 | // is necessary. |
| 3387 | if (auto main_frame_same_document_token = |
| 3388 | main_frame_request->GetNavigationTokenForDeferringSubframes()) { |
| 3389 | for (auto& item : same_document_loads) { |
| 3390 | if (item.get() != main_frame_request) { |
| 3391 | item->set_main_frame_same_document_history_token( |
| 3392 | main_frame_same_document_token); |
| 3393 | } |
| 3394 | } |
| 3395 | for (auto& item : different_document_loads) { |
| 3396 | item->set_main_frame_same_document_history_token( |
| 3397 | main_frame_same_document_token); |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | |
William Liu | 12275494 | 2024-01-18 22:34:39 | [diff] [blame] | 3402 | if (!initiator_rfh) { |
| 3403 | // A browser-initiated navigation won't have a `initiator_rfh`. |
| 3404 | CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions( |
| 3405 | different_document_loads, same_document_loads); |
| 3406 | } |
| 3407 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3408 | std::vector<base::WeakPtr<NavigationRequest>> all_requests; |
| 3409 | all_requests.reserve(same_document_loads.size() + |
| 3410 | different_document_loads.size()); |
| 3411 | bool seen_primary_main_frame_request = false; |
| 3412 | |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3413 | // Send all the same document frame loads before the different document loads. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3414 | for (auto& item : same_document_loads) { |
| 3415 | FrameTreeNode* frame = item->frame_tree_node(); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3416 | // The request could be destroyed before `navigator().Navigate()` returns. |
| 3417 | base::WeakPtr<NavigationRequest> request = item->GetWeakPtr(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3418 | frame->navigator().Navigate(std::move(item), reload_type); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3419 | if (request) { |
| 3420 | // Can only have one primary main frame request. |
| 3421 | CHECK(!seen_primary_main_frame_request || |
| 3422 | !request->IsInPrimaryMainFrame()); |
| 3423 | seen_primary_main_frame_request = request->IsInPrimaryMainFrame(); |
| 3424 | all_requests.push_back(std::move(request)); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3425 | } |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3426 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3427 | for (auto& item : different_document_loads) { |
| 3428 | FrameTreeNode* frame = item->frame_tree_node(); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3429 | base::WeakPtr<NavigationRequest> request = item->GetWeakPtr(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3430 | frame->navigator().Navigate(std::move(item), reload_type); |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3431 | if (request) { |
| 3432 | // Can only have one primary main frame request. |
| 3433 | CHECK(!seen_primary_main_frame_request || |
| 3434 | !request->IsInPrimaryMainFrame()); |
| 3435 | seen_primary_main_frame_request = request->IsInPrimaryMainFrame(); |
| 3436 | all_requests.push_back(std::move(request)); |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3437 | } |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3438 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3439 | |
| 3440 | in_navigate_to_pending_entry_ = false; |
William Liu | ec04e38 | 2024-05-23 18:03:27 | [diff] [blame] | 3441 | |
William Liu | 62ae26c | 2024-08-08 14:28:16 | [diff] [blame] | 3442 | return all_requests; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3443 | } |
| 3444 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3445 | NavigationControllerImpl::HistoryNavigationAction |
| 3446 | NavigationControllerImpl::DetermineActionForHistoryNavigation( |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3447 | FrameTreeNode* frame, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3448 | ReloadType reload_type) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3449 | RenderFrameHostImpl* render_frame_host = frame->current_frame_host(); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3450 | // Only active and prerendered documents are allowed to navigate in their |
| 3451 | // frame. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3452 | if (render_frame_host->lifecycle_state() != |
Sreeja Kamishetty | 299329ad | 2021-03-25 14:06:01 | [diff] [blame] | 3453 | RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) { |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3454 | // - If the document is in pending deletion, the browser already committed |
| 3455 | // to destroying this RenderFrameHost. See https://crbug.com/930278. |
| 3456 | // - If the document is in back-forward cache, it's not allowed to navigate |
| 3457 | // as it should remain frozen. Ignore the request and evict the document |
| 3458 | // from back-forward cache. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3459 | // |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3460 | // If the document is inactive, there's no need to recurse into subframes, |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3461 | // which should all be inactive as well. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3462 | if (frame->current_frame_host()->IsInactiveAndDisallowActivation( |
| 3463 | DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3464 | return HistoryNavigationAction::kStopLooking; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3465 | } |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3466 | } |
arthursonzogni | 03f7615 | 2019-02-12 10:35:20 | [diff] [blame] | 3467 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3468 | // Reloads should result in a different-document load. Note that reloads may |
| 3469 | // also happen via the |needs_reload_| mechanism where the reload_type is |
| 3470 | // NONE, so detect this by comparing whether we're going to the same |
| 3471 | // entry that we're currently on. Similarly to above, only main frames |
| 3472 | // should reach this. Note that subframes support reloads, but that's done |
| 3473 | // via a different path that doesn't involve FindFramesToNavigate (see |
| 3474 | // RenderFrameHost::Reload()). |
| 3475 | if (reload_type != ReloadType::NONE || |
| 3476 | pending_entry_index_ == last_committed_entry_index_) { |
| 3477 | DCHECK(frame->IsMainFrame()); |
| 3478 | return HistoryNavigationAction::kDifferentDocument; |
| 3479 | } |
| 3480 | |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3481 | // If there is no new FrameNavigationEntry for the frame, ignore the |
| 3482 | // load. For example, this may happen when going back to an entry before a |
| 3483 | // frame was created. Suppose we commit a same-document navigation that also |
| 3484 | // results in adding a new subframe somewhere in the tree. If we go back, |
| 3485 | // the new subframe will be missing a FrameNavigationEntry in the previous |
| 3486 | // NavigationEntry, but we shouldn't delete or change what's loaded in |
| 3487 | // it. |
| 3488 | // |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3489 | // Note that in this case, there is no need to keep looking for navigations |
| 3490 | // in subframes, which would be missing FrameNavigationEntries as well. |
| 3491 | // |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3492 | // It's important to check this before checking |old_item| below, since both |
| 3493 | // might be null, and in that case we still shouldn't change what's loaded in |
| 3494 | // this frame. Note that scheduling any loads assumes that |new_item| is |
| 3495 | // non-null. See https://crbug.com/1088354. |
| 3496 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3497 | if (!new_item) |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3498 | return HistoryNavigationAction::kStopLooking; |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3499 | |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3500 | // Use the RenderFrameHost's last committed FrameNavigationEntry to identify |
| 3501 | // which history item it is currently on, since this may be different than the |
| 3502 | // FrameNavigationEntry for the frame in the last committed NavigationEntry |
| 3503 | // (e.g., if a history navigation is targeting multiple frames and only some |
| 3504 | // have committed so far). |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3505 | FrameNavigationEntry* old_item = |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3506 | frame->current_frame_host()->last_committed_frame_entry(); |
| 3507 | if (!old_item) { |
| 3508 | // In cases where the RenderFrameHost does not have a FrameNavigationEntry, |
| 3509 | // fall back to the last committed NavigationEntry's record for this frame. |
| 3510 | // This may happen in cases like the initial state of the RenderFrameHost. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 3511 | // TODO(crbug.com/40217743): Ensure the RenderFrameHost always has an |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3512 | // accurate FrameNavigationEntry and eliminate this case. |
| 3513 | old_item = GetLastCommittedEntry()->GetFrameEntry(frame); |
| 3514 | } |
| 3515 | // If neither approach finds a FrameNavigationEntry, schedule a |
| 3516 | // different-document load. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 3517 | // TODO(crbug.com/40467594): Remove this case. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3518 | if (!old_item) |
| 3519 | return HistoryNavigationAction::kDifferentDocument; |
| 3520 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3521 | // If the new item is not in the same SiteInstance, schedule a |
| 3522 | // different-document load. Newly restored items may not have a SiteInstance |
| 3523 | // yet, in which case it will be assigned on first commit. |
| 3524 | if (new_item->site_instance() && |
| 3525 | new_item->site_instance() != old_item->site_instance()) |
| 3526 | return HistoryNavigationAction::kDifferentDocument; |
| 3527 | |
| 3528 | // Schedule a different-document load if the current RenderFrameHost is not |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3529 | // live. This case can happen for Ctrl+Back or after a renderer crash. Note |
| 3530 | // that we do this even if the history navigation would not be modifying this |
| 3531 | // frame were it live. |
| 3532 | if (!frame->current_frame_host()->IsRenderFrameLive()) |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3533 | return HistoryNavigationAction::kDifferentDocument; |
| 3534 | |
| 3535 | if (new_item->item_sequence_number() != old_item->item_sequence_number()) { |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3536 | // Starting a navigation after a crash early-promotes the speculative |
| 3537 | // RenderFrameHost. Then we have a RenderFrameHost with no document in it |
| 3538 | // committed yet, so we can not possibly perform a same-document history |
| 3539 | // navigation. The frame would need to be reloaded with a cross-document |
| 3540 | // navigation. |
| 3541 | if (!frame->current_frame_host()->has_committed_any_navigation()) |
| 3542 | return HistoryNavigationAction::kDifferentDocument; |
| 3543 | |
creis | 5413169 | 2016-08-12 18:32:25 | [diff] [blame] | 3544 | // Same document loads happen if the previous item has the same document |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3545 | // sequence number but different item sequence number. |
| 3546 | if (new_item->document_sequence_number() == |
| 3547 | old_item->document_sequence_number()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3548 | return HistoryNavigationAction::kSameDocument; |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3549 | } |
avi | b48cb31 | 2016-05-05 21:35:00 | [diff] [blame] | 3550 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3551 | // Otherwise, if both item and document sequence numbers differ, this |
| 3552 | // should be a different document load. |
| 3553 | return HistoryNavigationAction::kDifferentDocument; |
| 3554 | } |
| 3555 | |
| 3556 | // If the item sequence numbers match, there is no need to navigate this |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3557 | // frame. Keep looking for navigations in this frame's children. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3558 | DCHECK_EQ(new_item->document_sequence_number(), |
| 3559 | old_item->document_sequence_number()); |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3560 | return HistoryNavigationAction::kKeepLooking; |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | void NavigationControllerImpl::FindFramesToNavigate( |
| 3564 | FrameTreeNode* frame, |
| 3565 | ReloadType reload_type, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3566 | const std::optional<blink::LocalFrameToken>& initiator_frame_token, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3567 | int initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3568 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3569 | soft_navigation_heuristics_task_id, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3570 | std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads, |
| 3571 | std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) { |
| 3572 | DCHECK(pending_entry_); |
| 3573 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3574 | |
| 3575 | auto action = DetermineActionForHistoryNavigation(frame, reload_type); |
| 3576 | |
| 3577 | if (action == HistoryNavigationAction::kSameDocument) { |
| 3578 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3579 | CreateNavigationRequestFromEntry( |
| 3580 | frame, pending_entry_, new_item, reload_type, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3581 | /*is_same_document_history_load=*/true, |
| 3582 | /*is_history_navigation_in_new_child_frame=*/false, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3583 | initiator_frame_token, initiator_process_id, |
| 3584 | soft_navigation_heuristics_task_id); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3585 | if (navigation_request) { |
| 3586 | // Only add the request if was properly created. It's possible for the |
| 3587 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3588 | same_document_loads->push_back(std::move(navigation_request)); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3589 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3590 | } else if (action == HistoryNavigationAction::kDifferentDocument) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3591 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3592 | CreateNavigationRequestFromEntry( |
| 3593 | frame, pending_entry_, new_item, reload_type, |
| 3594 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3595 | false /* is_history_navigation_in_new_child */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3596 | initiator_frame_token, initiator_process_id); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3597 | if (navigation_request) { |
| 3598 | // Only add the request if was properly created. It's possible for the |
| 3599 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3600 | different_document_loads->push_back(std::move(navigation_request)); |
| 3601 | } |
| 3602 | // For a different document, the subframes will be destroyed, so there's |
| 3603 | // no need to consider them. |
| 3604 | return; |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3605 | } else if (action == HistoryNavigationAction::kStopLooking) { |
| 3606 | return; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3607 | } |
| 3608 | |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3609 | // Do not pass down the soft_navigation_heuristics_task_id to child frames, as |
| 3610 | // we currently only support soft navigation heuristics for the top level |
| 3611 | // frame. |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3612 | for (size_t i = 0; i < frame->child_count(); i++) { |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3613 | FindFramesToNavigate(frame->child_at(i), reload_type, initiator_frame_token, |
| 3614 | initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3615 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3616 | same_document_loads, different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3620 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry( |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3621 | const LoadURLParams& params) { |
| 3622 | // Find the appropriate FrameTreeNode. |
| 3623 | FrameTreeNode* node = nullptr; |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 3624 | if (params.frame_tree_node_id || !params.frame_name.empty()) { |
| 3625 | node = params.frame_tree_node_id |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3626 | ? frame_tree_->FindByID(params.frame_tree_node_id) |
| 3627 | : frame_tree_->FindByName(params.frame_name); |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 3628 | DCHECK(!node || &node->frame_tree() == &frame_tree()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | // If no FrameTreeNode was specified, navigate the main frame. |
| 3632 | if (!node) |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3633 | node = frame_tree_->root(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3634 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3635 | // Compute overrides to the LoadURLParams for |override_user_agent|, |
| 3636 | // |should_replace_current_entry| and |has_user_gesture| that will be used |
| 3637 | // both in the creation of the NavigationEntry and the NavigationRequest. |
| 3638 | // Ideally, the LoadURLParams themselves would be updated, but since they are |
| 3639 | // passed as a const reference, this is not possible. |
| 3640 | // TODO(clamy): When we only create a NavigationRequest, move this to |
| 3641 | // CreateNavigationRequestFromLoadURLParams. |
| 3642 | bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent, |
| 3643 | GetLastCommittedEntry()); |
| 3644 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3645 | // An entry replacement must happen if the current browsing context should |
| 3646 | // maintain a trivial session history. |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 3647 | bool should_replace_current_entry = |
| 3648 | (params.should_replace_current_entry || |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3649 | ShouldMaintainTrivialSessionHistory(node)); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3650 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3651 | // Javascript URLs should not create NavigationEntries. All other navigations |
| 3652 | // do, including navigations to chrome renderer debug URLs. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3653 | if (!params.url.SchemeIs(url::kJavaScriptScheme)) { |
Scott Violet | 5ae6c42e | 2020-10-28 02:47:37 | [diff] [blame] | 3654 | std::unique_ptr<NavigationEntryImpl> entry = |
| 3655 | CreateNavigationEntryFromLoadParams(node, params, override_user_agent, |
| 3656 | should_replace_current_entry, |
| 3657 | params.has_user_gesture); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3658 | DiscardPendingEntry(false); |
| 3659 | SetPendingEntry(std::move(entry)); |
| 3660 | } |
| 3661 | |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3662 | // Renderer-debug URLs are sent to the current renderer process immediately |
| 3663 | // for processing and don't need to create a NavigationRequest. Note: this |
| 3664 | // includes navigations to JavaScript URLs, which are considered |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3665 | // renderer-debug URLs. |
| 3666 | // Note: we intentionally leave the pending entry in place for renderer debug |
| 3667 | // URLs, unlike the cases below where we clear it if the navigation doesn't |
| 3668 | // proceed. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3669 | if (blink::IsRendererDebugURL(params.url)) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3670 | // Renderer-debug URLs won't go through NavigationThrottlers so we have to |
| 3671 | // check them explicitly. See bug 913334. |
Aaron Colwell | e1908d98 | 2020-06-26 22:08:15 | [diff] [blame] | 3672 | if (GetContentClient()->browser()->ShouldBlockRendererDebugURL( |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3673 | params.url, browser_context_, node->current_frame_host())) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3674 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3675 | return nullptr; |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3676 | } |
| 3677 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3678 | HandleRendererDebugURL(node, params.url); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3679 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3680 | } |
| 3681 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3682 | DCHECK(pending_entry_); |
| 3683 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3684 | // Convert navigations to the current URL to a reload. |
| 3685 | // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of |
| 3686 | // relying on the frame tree node id from LoadURLParams. Unfortunately, |
| 3687 | // DevTools sometimes issues navigations to main frames that they do not |
| 3688 | // expect to see treated as reload, and it only works because they pass a |
| 3689 | // FrameTreeNode id in their LoadURLParams. Change this once they no longer do |
| 3690 | // that. See https://crbug.com/850926. |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3691 | ReloadType reload_type = params.reload_type; |
| 3692 | if (reload_type == ReloadType::NONE && |
| 3693 | ShouldTreatNavigationAsReload( |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 3694 | node, params.url, pending_entry_->GetVirtualURL(), |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3695 | params.base_url_for_data_url, params.transition_type, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3696 | params.load_type == |
| 3697 | NavigationController::LOAD_TYPE_HTTP_POST /* is_post */, |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3698 | should_replace_current_entry, GetLastCommittedEntry())) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3699 | reload_type = ReloadType::NORMAL; |
Alexander Timin | b70f6738 | 2020-12-10 00:03:47 | [diff] [blame] | 3700 | pending_entry_->set_reload_type(reload_type); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3701 | |
| 3702 | // If this is a reload of an existing FrameNavigationEntry and we had a |
| 3703 | // policy container for it, then we should copy it into the pending entry, |
| 3704 | // so that it is copied to the navigation request in |
| 3705 | // CreateNavigationRequestFromLoadParams later. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3706 | FrameNavigationEntry* previous_frame_entry = |
| 3707 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 3708 | if (previous_frame_entry && |
| 3709 | previous_frame_entry->policy_container_policies()) { |
| 3710 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
| 3711 | previous_frame_entry->policy_container_policies()->ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3712 | } |
| 3713 | } |
| 3714 | |
| 3715 | // 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] | 3716 | // document, then we should copy the document polices from RenderFrameHost's |
| 3717 | // PolicyContainerHost. The NavigationRequest will create a new |
| 3718 | // PolicyContainerHost with the document policies from the |pending_entry_|, |
| 3719 | // and that PolicyContainerHost will be put in the final RenderFrameHost for |
| 3720 | // the navigation. This way, we ensure that we keep enforcing the right |
| 3721 | // policies on the initial empty document after the reload. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3722 | if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3723 | if (node->current_frame_host() && |
| 3724 | node->current_frame_host()->policy_container_host()) { |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3725 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 3726 | node->current_frame_host() |
| 3727 | ->policy_container_host() |
| 3728 | ->policies() |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 3729 | .ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3730 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | // navigation_ui_data should only be present for main frame navigations. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 3734 | DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3735 | |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3736 | // This will be used to set the Navigation Timing API navigationStart |
| 3737 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 3738 | // "Open link in new tab"). If the navigation must wait on the current |
| 3739 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 3740 | // will be updated when the BeforeUnload ack is received. |
| 3741 | const auto navigation_start_time = base::TimeTicks::Now(); |
| 3742 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3743 | std::unique_ptr<NavigationRequest> request = |
| 3744 | CreateNavigationRequestFromLoadParams( |
| 3745 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3746 | params.has_user_gesture, network::mojom::SourceLocation::New(), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3747 | reload_type, pending_entry_, pending_entry_->GetFrameEntry(node), |
| 3748 | navigation_start_time); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3749 | |
| 3750 | // If the navigation couldn't start, return immediately and discard the |
| 3751 | // pending NavigationEntry. |
| 3752 | if (!request) { |
| 3753 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3754 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3755 | } |
| 3756 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3757 | #if DCHECK_IS_ON() |
| 3758 | // Safety check that NavigationRequest and NavigationEntry match. |
| 3759 | ValidateRequestMatchesEntry(request.get(), pending_entry_); |
| 3760 | #endif |
| 3761 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3762 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3763 | CHECK(!in_navigate_to_pending_entry_); |
| 3764 | in_navigate_to_pending_entry_ = true; |
| 3765 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3766 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3767 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3768 | // function. |
| 3769 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3770 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3771 | base::WeakPtr<NavigationHandle> created_navigation_handle( |
| 3772 | request->GetWeakPtr()); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3773 | node->navigator().Navigate(std::move(request), reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3774 | |
| 3775 | in_navigate_to_pending_entry_ = false; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3776 | return created_navigation_handle; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3777 | } |
| 3778 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3779 | void NavigationControllerImpl::HandleRendererDebugURL( |
| 3780 | FrameTreeNode* frame_tree_node, |
| 3781 | const GURL& url) { |
| 3782 | if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3783 | // Any renderer-side debug URLs or javascript: URLs should be ignored if |
| 3784 | // the renderer process is not live, unless it is the initial navigation |
| 3785 | // of the tab. |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3786 | if (!IsInitialNavigation()) { |
| 3787 | DiscardNonCommittedEntries(); |
| 3788 | return; |
| 3789 | } |
Fergal Daly | ecd3b020 | 2020-06-25 01:57:37 | [diff] [blame] | 3790 | // The current frame is always a main frame. If IsInitialNavigation() is |
| 3791 | // true then there have been no navigations and any frames of this tab must |
| 3792 | // be in the same renderer process. If that has crashed then the only frame |
| 3793 | // that can be revived is the main frame. |
| 3794 | frame_tree_node->render_manager() |
| 3795 | ->InitializeMainRenderFrameForImmediateUse(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3796 | } |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3797 | |
| 3798 | // Several tests expect a load of Chrome Debug URLs to send a DidStopLoading |
| 3799 | // notification, so set is loading to true here to properly surface it when |
| 3800 | // the renderer process is done handling the URL. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 3801 | // TODO(crbug.com/40199456): Remove the test dependency on this behavior. |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3802 | if (!url.SchemeIs(url::kJavaScriptScheme)) { |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3803 | frame_tree_node->current_frame_host()->SetIsLoadingForRendererDebugURL(); |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3804 | } |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3805 | frame_tree_node->current_frame_host()->HandleRendererDebugURL(url); |
| 3806 | } |
| 3807 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3808 | std::unique_ptr<NavigationEntryImpl> |
| 3809 | NavigationControllerImpl::CreateNavigationEntryFromLoadParams( |
| 3810 | FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3811 | const LoadURLParams& params, |
| 3812 | bool override_user_agent, |
| 3813 | bool should_replace_current_entry, |
| 3814 | bool has_user_gesture) { |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3815 | // Browser initiated navigations might not have a blob_url_loader_factory set |
| 3816 | // in params even if the navigation is to a blob URL. If that happens, lookup |
| 3817 | // the correct url loader factory to use here. |
| 3818 | auto blob_url_loader_factory = params.blob_url_loader_factory; |
Kinuko Yasuda | 7d925ea2 | 2019-08-01 10:08:48 | [diff] [blame] | 3819 | if (!blob_url_loader_factory && params.url.SchemeIsBlob()) { |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3820 | // Resolve the blob URL in the storage partition associated with the target |
| 3821 | // frame. This is the storage partition the URL will be loaded in, and only |
| 3822 | // 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] | 3823 | blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl( |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3824 | node->current_frame_host()->GetStoragePartition(), params.url); |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3825 | } |
| 3826 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3827 | std::unique_ptr<NavigationEntryImpl> entry; |
Tommy C. Li | 03eee77a | 2019-02-05 02:07:44 | [diff] [blame] | 3828 | // extra_headers in params are \n separated; navigation entries want \r\n. |
| 3829 | std::string extra_headers_crlf; |
| 3830 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3831 | |
| 3832 | // For subframes, create a pending entry with a corresponding frame entry. |
| 3833 | if (!node->IsMainFrame()) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3834 | entry = GetLastCommittedEntry()->Clone(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3835 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 3836 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3837 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get()), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3838 | params.url, std::nullopt, params.referrer, params.initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 3839 | params.initiator_base_url, params.redirect_chain, blink::PageState(), |
| 3840 | "GET", -1, blob_url_loader_factory, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3841 | // If in NavigateWithoutEntry we later determine that this navigation is |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 3842 | // a conversion of a new navigation into a reload, we will set the right |
| 3843 | // document policies there. |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3844 | nullptr /* policy_container_policies */); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3845 | } else { |
| 3846 | // Otherwise, create a pending entry for the main frame. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 3847 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 3848 | // to adjust the original input url if needed. For inner frames such as |
| 3849 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 3850 | // input urls by users. |
| 3851 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 3852 | scoped_refptr<SiteInstance> source_site_instance = |
| 3853 | params.source_site_instance; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3854 | std::optional<GURL> source_process_site_url = std::nullopt; |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 3855 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 3856 | source_process_site_url = |
| 3857 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 3858 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3859 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 3860 | params.url, params.referrer, params.initiator_origin, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 3861 | params.initiator_base_url, source_process_site_url, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 3862 | params.transition_type, params.is_renderer_initiated, |
| 3863 | extra_headers_crlf, browser_context_, blob_url_loader_factory, |
| 3864 | rewrite_virtual_urls)); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3865 | entry->set_source_site_instance( |
| 3866 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get())); |
| 3867 | entry->SetRedirectChain(params.redirect_chain); |
| 3868 | } |
| 3869 | |
| 3870 | // Set the FTN ID (only used in non-site-per-process, for tests). |
| 3871 | entry->set_frame_tree_node_id(node->frame_tree_node_id()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3872 | entry->set_should_clear_history_list(params.should_clear_history_list); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3873 | entry->SetIsOverridingUserAgent(override_user_agent); |
| 3874 | entry->set_has_user_gesture(has_user_gesture); |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3875 | entry->set_reload_type(params.reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3876 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3877 | switch (params.load_type) { |
| 3878 | case LOAD_TYPE_DEFAULT: |
| 3879 | break; |
| 3880 | case LOAD_TYPE_HTTP_POST: |
| 3881 | entry->SetHasPostData(true); |
| 3882 | entry->SetPostData(params.post_data); |
| 3883 | break; |
| 3884 | case LOAD_TYPE_DATA: |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 3885 | // LoadDataWithBaseURL is a special case that needs to assign both a base |
| 3886 | // URL and a virtual URL, while loading the actual content from a data |
| 3887 | // URL. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3888 | entry->SetBaseURLForDataURL(params.base_url_for_data_url); |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 3889 | entry->SetVirtualURL(params.virtual_url_for_special_cases); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3890 | #if BUILDFLAG(IS_ANDROID) |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3891 | entry->SetDataURLAsString(params.data_url_as_string); |
| 3892 | #endif |
| 3893 | entry->SetCanLoadLocalResources(params.can_load_local_resources); |
| 3894 | break; |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 3895 | #if BUILDFLAG(IS_ANDROID) |
| 3896 | case LOAD_TYPE_PDF_ANDROID: |
| 3897 | // Android PDF URLs show the actual PDF URL as a virtual URL, while an |
| 3898 | // internal URL is used for the navigation URL. |
| 3899 | entry->SetVirtualURL(params.virtual_url_for_special_cases); |
| 3900 | break; |
| 3901 | #endif |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | // TODO(clamy): NavigationEntry is meant for information that will be kept |
| 3905 | // after the navigation ended and therefore is not appropriate for |
| 3906 | // started_from_context_menu. Move started_from_context_menu to |
| 3907 | // NavigationUIData. |
| 3908 | entry->set_started_from_context_menu(params.started_from_context_menu); |
| 3909 | |
| 3910 | return entry; |
| 3911 | } |
| 3912 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3913 | std::unique_ptr<NavigationRequest> |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3914 | NavigationControllerImpl::CreateNavigationRequestFromLoadParams( |
| 3915 | FrameTreeNode* node, |
| 3916 | const LoadURLParams& params, |
| 3917 | bool override_user_agent, |
| 3918 | bool should_replace_current_entry, |
| 3919 | bool has_user_gesture, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3920 | network::mojom::SourceLocationPtr source_location, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3921 | ReloadType reload_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3922 | NavigationEntryImpl* entry, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3923 | FrameNavigationEntry* frame_entry, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 3924 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 3925 | bool is_embedder_initiated_fenced_frame_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 3926 | bool is_unfenced_top_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 3927 | bool is_container_initiated, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 3928 | net::StorageAccessApiStatus storage_access_api_status, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 3929 | std::optional<std::u16string> embedder_shared_storage_context) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3930 | DCHECK_EQ(-1, GetIndexOfEntry(entry)); |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 3931 | |
| 3932 | // TODO(https://crbug.com/40467594): Add a CHECK(frame_entry) once all |
| 3933 | // subframes have FrameNavigationEntries associated with them. Until then, |
| 3934 | // there may be cases where a subframe navigation is missing `frame_entry` |
| 3935 | // (e.g., see https://crbug.com/358084015). |
| 3936 | |
Nasko Oskov | 3c2f9e25 | 2019-01-10 17:45:53 | [diff] [blame] | 3937 | // All renderer-initiated navigations must have an initiator_origin. |
| 3938 | DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value()); |
Camille Lamy | ff7c482 | 2018-11-07 15:42:51 | [diff] [blame] | 3939 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3940 | GURL url_to_load; |
| 3941 | GURL virtual_url; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3942 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3943 | // For main frames, rewrite the URL if necessary and compute the virtual URL |
| 3944 | // that should be shown in the address bar. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 3945 | if (node->IsOutermostMainFrame()) { |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3946 | bool ignored_reverse_on_redirect = false; |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3947 | RewriteUrlForNavigation(params.url, browser_context_, &url_to_load, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3948 | &virtual_url, &ignored_reverse_on_redirect); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3949 | |
Shu Yang | 112ad49 | 2024-07-25 17:11:54 | [diff] [blame] | 3950 | // Both LoadDataWithBaseURL and Android PDF navigations are special cases |
| 3951 | // that need to define a virtual URL to display, which differs from the |
| 3952 | // navigation URL. |
| 3953 | if (params.load_type == LOAD_TYPE_DATA) { |
| 3954 | virtual_url = params.virtual_url_for_special_cases; |
| 3955 | } |
| 3956 | #if BUILDFLAG(IS_ANDROID) |
| 3957 | if (params.load_type == LOAD_TYPE_PDF_ANDROID) { |
| 3958 | virtual_url = params.virtual_url_for_special_cases; |
| 3959 | } |
| 3960 | #endif |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3961 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3962 | if (virtual_url.is_empty()) |
| 3963 | virtual_url = url_to_load; |
| 3964 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3965 | CHECK(virtual_url == entry->GetVirtualURL()); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3966 | |
Aran Gilman | 249eb12 | 2019-12-02 23:32:46 | [diff] [blame] | 3967 | // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic |
| 3968 | // behavior: it is possible for two calls to RewriteUrlForNavigation to |
| 3969 | // return different results, leading to a different URL in the |
| 3970 | // NavigationRequest and FrameEntry. This will be fixed once we remove the |
| 3971 | // pending NavigationEntry, as we'll only make one call to |
| 3972 | // RewriteUrlForNavigation. |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 3973 | if (frame_entry) { |
| 3974 | VLOG_IF(1, (url_to_load != frame_entry->url())) |
| 3975 | << "NavigationRequest and FrameEntry have different URLs: " |
| 3976 | << url_to_load << " vs " << frame_entry->url(); |
| 3977 | } |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3978 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3979 | // TODO(clamy): In order to remove the pending NavigationEntry, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3980 | // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3981 | // NavigationRequest. |
| 3982 | } else { |
| 3983 | url_to_load = params.url; |
| 3984 | virtual_url = params.url; |
Camille Lamy | f664f762 | 2019-01-07 19:28:24 | [diff] [blame] | 3985 | CHECK(!frame_entry || url_to_load == frame_entry->url()); |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3986 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3987 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 3988 | if (node->render_manager()->is_attaching_inner_delegate()) { |
| 3989 | // Avoid starting any new navigations since this node is now preparing for |
| 3990 | // attaching an inner delegate. |
| 3991 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 3992 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3993 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 3994 | if (!IsValidURLForNavigation(node, virtual_url, url_to_load)) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3995 | return nullptr; |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 3996 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3997 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3998 | // Look for a pending commit that is to another document in this |
| 3999 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 4000 | // current URL by the time this navigation commits. |
| 4001 | bool has_pending_cross_document_commit = |
| 4002 | node->render_manager()->HasPendingCommitForCrossDocumentNavigation(); |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 4003 | bool is_currently_error_page = node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4004 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4005 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4006 | /*old_url=*/node->current_url(), |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4007 | /*new_url=*/url_to_load, reload_type, entry, frame_entry, |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4008 | has_pending_cross_document_commit, is_currently_error_page, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 4009 | /*is_same_document_history_load=*/false, |
| 4010 | is_embedder_initiated_fenced_frame_navigation, |
| 4011 | is_unfenced_top_navigation); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4012 | |
| 4013 | // Create the NavigationParams based on |params|. |
| 4014 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 4015 | bool is_view_source_mode = entry->IsViewSourceMode(); |
| 4016 | DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme)); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4017 | |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 4018 | blink::NavigationDownloadPolicy download_policy = params.download_policy; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4019 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 4020 | // Update |download_policy| if the virtual URL is view-source. |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4021 | if (is_view_source_mode) |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 4022 | download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 4023 | |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4024 | std::string page_state_data = |
| 4025 | frame_entry ? frame_entry->page_state().ToEncodedData() : std::string(); |
| 4026 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4027 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 4028 | blink::mojom::CommonNavigationParams::New( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 4029 | url_to_load, params.initiator_origin, params.initiator_base_url, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4030 | blink::mojom::Referrer::New(params.referrer.url, |
| 4031 | params.referrer.policy), |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 4032 | params.transition_type, navigation_type, download_policy, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4033 | should_replace_current_entry, params.base_url_for_data_url, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 4034 | navigation_start_time, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4035 | params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET", |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 4036 | params.post_data, std::move(source_location), |
arthursonzogni | af7c62c5 | 2020-02-12 10:49:41 | [diff] [blame] | 4037 | params.started_from_context_menu, has_user_gesture, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 4038 | false /* has_text_fragment_token */, |
| 4039 | network::mojom::CSPDisposition::CHECK, std::vector<int>(), |
| 4040 | params.href_translate, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4041 | false /* is_history_navigation_in_new_child_frame */, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 4042 | params.input_start, network::mojom::RequestDestination::kEmpty); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4043 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4044 | blink::mojom::CommitNavigationParamsPtr commit_params = |
| 4045 | blink::mojom::CommitNavigationParams::New( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4046 | std::nullopt, |
Ari Chivukula | c753352 | 2024-05-14 21:52:34 | [diff] [blame] | 4047 | // The correct storage key will be computed before committing the |
| 4048 | // navigation. |
| 4049 | blink::StorageKey(), override_user_agent, params.redirect_chain, |
Lucas Furukawa Gadani | 81e294b | 2019-08-29 16:26:32 | [diff] [blame] | 4050 | std::vector<network::mojom::URLResponseHeadPtr>(), |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4051 | std::vector<net::RedirectInfo>(), params.post_content_type, |
| 4052 | common_params->url, common_params->method, |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4053 | params.can_load_local_resources, page_state_data, |
| 4054 | entry->GetUniqueID(), entry->GetSubframeUniqueNames(node), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4055 | /*intended_as_new_entry=*/true, |
| 4056 | /*pending_history_list_offset=*/-1, |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4057 | params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(), |
| 4058 | params.should_clear_history_list ? 0 : GetEntryCount(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4059 | /*was_discarded=*/false, is_view_source_mode, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4060 | params.should_clear_history_list, |
| 4061 | blink::mojom::NavigationTiming::New(), |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 4062 | blink::mojom::WasActivatedOption::kUnknown, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4063 | /*navigation_token=*/base::UnguessableToken::Create(), |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 4064 | std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(), |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 4065 | #if BUILDFLAG(IS_ANDROID) |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4066 | /*data_url_as_string=*/std::string(), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4067 | #endif |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4068 | /*is_browser_initiated=*/!params.is_renderer_initiated, |
Khushal Sagar | 71837ad8 | 2024-08-09 18:03:15 | [diff] [blame] | 4069 | /*has_ua_visual_transition*/ false, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4070 | /*document_ukm_source_id=*/ukm::kInvalidSourceId, |
Jiewei Qian | 0406fc0 | 2020-03-09 06:02:07 | [diff] [blame] | 4071 | node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4072 | /*force_enabled_origin_trials=*/std::vector<std::string>(), |
| 4073 | /*origin_agent_cluster=*/false, |
| 4074 | /*origin_agent_cluster_left_as_default=*/true, |
| 4075 | /*enabled_client_hints=*/ |
| 4076 | std::vector<network::mojom::WebClientHintsType>(), |
Liam Brady | 38b8456 | 2024-03-07 22:11:26 | [diff] [blame] | 4077 | /*is_cross_site_cross_browsing_context_group=*/false, |
| 4078 | /*should_have_sticky_user_activation=*/false, |
| 4079 | /*old_page_info=*/nullptr, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4080 | /*http_response_code=*/-1, |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4081 | blink::mojom::NavigationApiHistoryEntryArrays::New(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4082 | /*early_hints_preloaded_resources=*/std::vector<GURL>(), |
Clark DuVall | 8ee487a2 | 2021-11-10 02:25:58 | [diff] [blame] | 4083 | // This timestamp will be populated when the commit IPC is sent. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4084 | /*commit_sent=*/base::TimeTicks(), /*srcdoc_value=*/std::string(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4085 | /*should_load_data_url=*/false, |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 4086 | /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4087 | /*reduced_accept_language=*/std::string(), |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 4088 | /*navigation_delivery_type=*/ |
Khushal Sagar | 7b26135c6 | 2022-11-08 20:25:42 | [diff] [blame] | 4089 | network::mojom::NavigationDeliveryType::kDefault, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4090 | /*view_transition_state=*/std::nullopt, |
| 4091 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
sbingler | a07ae73 | 2022-12-02 20:49:05 | [diff] [blame] | 4092 | /*modified_runtime_features=*/ |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 4093 | base::flat_map<::blink::mojom::RuntimeFeature, bool>(), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4094 | /*fenced_frame_properties=*/std::nullopt, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 4095 | /*not_restored_reasons=*/nullptr, |
Chris Fredrickson | 9ffdf5b | 2024-07-09 20:05:09 | [diff] [blame] | 4096 | /*load_with_storage_access=*/ |
| 4097 | net::StorageAccessApiStatus::kNone, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4098 | /*browsing_context_group_info=*/std::nullopt, |
Nan Lin | da394ba | 2023-11-03 21:17:37 | [diff] [blame] | 4099 | /*lcpp_hint=*/nullptr, blink::CreateDefaultRendererContentSettings(), |
Kyra | 800d1d01 | 2024-03-27 17:05:48 | [diff] [blame] | 4100 | /*cookie_deprecation_label=*/std::nullopt, |
Khushal Sagar | 81f8981 | 2024-06-11 13:40:31 | [diff] [blame] | 4101 | /*visited_link_salt=*/std::nullopt, |
| 4102 | /*local_surface_id=*/std::nullopt); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 4103 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4104 | if (ValidateDataURLAsString(params.data_url_as_string)) { |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 4105 | commit_params->data_url_as_string = params.data_url_as_string->as_string(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4106 | } |
| 4107 | #endif |
| 4108 | |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4109 | commit_params->was_activated = params.was_activated; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4110 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4111 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4112 | node, params.suggested_system_entropy); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4113 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4114 | // extra_headers in params are \n separated; NavigationRequests want \r\n. |
| 4115 | std::string extra_headers_crlf; |
| 4116 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4117 | |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 4118 | auto navigation_request = NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4119 | node, std::move(common_params), std::move(commit_params), |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 4120 | !params.is_renderer_initiated, params.was_opener_suppressed, |
Hiroshige Hayashizaki | f07ad781 | 2023-05-10 02:26:09 | [diff] [blame] | 4121 | params.initiator_frame_token, params.initiator_process_id, |
| 4122 | extra_headers_crlf, frame_entry, entry, params.is_form_submission, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 4123 | params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4124 | params.impression, params.initiator_activation_and_ad_status, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 4125 | params.is_pdf, is_embedder_initiated_fenced_frame_navigation, |
W. James MacLean | 443ef3e | 2024-07-16 13:42:34 | [diff] [blame] | 4126 | is_container_initiated, params.has_rel_opener, storage_access_api_status, |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 4127 | embedder_shared_storage_context); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4128 | navigation_request->set_from_download_cross_origin_redirect( |
| 4129 | params.from_download_cross_origin_redirect); |
W. James MacLean | 00568d7 | 2022-02-24 19:36:55 | [diff] [blame] | 4130 | navigation_request->set_force_new_browsing_instance( |
| 4131 | params.force_new_browsing_instance); |
Mustafa Emre Acer | 1001b3d | 2024-07-09 23:10:43 | [diff] [blame] | 4132 | if (params.force_no_https_upgrade) { |
| 4133 | navigation_request->set_force_no_https_upgrade(); |
| 4134 | } |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 4135 | return navigation_request; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | std::unique_ptr<NavigationRequest> |
| 4139 | NavigationControllerImpl::CreateNavigationRequestFromEntry( |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4140 | FrameTreeNode* frame_tree_node, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4141 | NavigationEntryImpl* entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4142 | FrameNavigationEntry* frame_entry, |
| 4143 | ReloadType reload_type, |
| 4144 | bool is_same_document_history_load, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4145 | bool is_history_navigation_in_new_child_frame, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4146 | const std::optional<blink::LocalFrameToken>& initiator_frame_token, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4147 | int initiator_process_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4148 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4149 | soft_navigation_heuristics_task_id) { |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 4150 | DCHECK(frame_entry); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4151 | GURL dest_url = frame_entry->url(); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4152 | // We should never navigate to an existing initial NavigationEntry that is the |
| 4153 | // initial NavigationEntry for the initial empty document that hasn't been |
| 4154 | // overridden by the synchronous about:blank commit, to preserve previous |
| 4155 | // behavior where trying to reload when the main frame is on the initial empty |
| 4156 | // document won't result in a navigation. |
| 4157 | // See also https://crbug.com/1277414. |
| 4158 | DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank()); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 4159 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4160 | Referrer dest_referrer = frame_entry->referrer(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4161 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 4162 | if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) { |
| 4163 | // Avoid starting any new navigations since this node is now preparing for |
| 4164 | // attaching an inner delegate. |
| 4165 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 4166 | } |
| 4167 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 4168 | if (!IsValidURLForNavigation(frame_tree_node, entry->GetVirtualURL(), |
| 4169 | dest_url)) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4170 | return nullptr; |
| 4171 | } |
| 4172 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4173 | // This will be used to set the Navigation Timing API navigationStart |
| 4174 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 4175 | // "Open link in new tab"). If the navigation must wait on the current |
| 4176 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 4177 | // will be updated when the BeforeUnload ack is received. |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4178 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4179 | base::TimeTicks navigation_start = base::TimeTicks::Now(); |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4180 | const auto navigation_start_system_entropy = |
| 4181 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4182 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4183 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4184 | // Look for a pending commit that is to another document in this |
| 4185 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 4186 | // current URL by the time this navigation commits. |
| 4187 | bool has_pending_cross_document_commit = |
| 4188 | frame_tree_node->render_manager() |
| 4189 | ->HasPendingCommitForCrossDocumentNavigation(); |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4190 | bool is_currently_error_page = |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 4191 | frame_tree_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 | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4194 | /*old_url=*/frame_tree_node->current_url(), |
Charlie Reis | f21cd18 | 2024-08-09 21:44:52 | [diff] [blame] | 4195 | /*new_url=*/dest_url, 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 | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4197 | is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 4198 | /*is_embedder_initiated_fenced_frame_navigation=*/false, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4199 | /*is_unfenced_top_navigation=*/false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4200 | |
| 4201 | // A form submission may happen here if the navigation is a |
| 4202 | // back/forward/reload navigation that does a form resubmission. |
| 4203 | scoped_refptr<network::ResourceRequestBody> request_body; |
| 4204 | std::string post_content_type; |
Alison Gale | d94ce4f | 2024-04-22 15:20:39 | [diff] [blame] | 4205 | // TODO(crbug.com/41440869) Store |is_form_submission| in the history |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4206 | // entry. This way, it could be directly retrieved here. Right now, it is only |
| 4207 | // partially recovered when request.method == "POST" and request.body exists. |
| 4208 | bool is_form_submission = false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4209 | if (frame_entry->method() == "POST") { |
| 4210 | request_body = frame_entry->GetPostData(&post_content_type); |
| 4211 | // Might have a LF at end. |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 4212 | post_content_type = std::string( |
| 4213 | base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4214 | is_form_submission = !!request_body; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | // Create the NavigationParams based on |entry| and |frame_entry|. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4218 | blink::mojom::CommonNavigationParamsPtr common_params = |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4219 | entry->ConstructCommonNavigationParams( |
| 4220 | *frame_entry, request_body, dest_url, |
| 4221 | blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy), |
Tarun Bansal | bcd62c8 | 2022-01-18 17:27:38 | [diff] [blame] | 4222 | navigation_type, navigation_start, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 4223 | base::TimeTicks() /* input_start */); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4224 | common_params->is_history_navigation_in_new_child_frame = |
Arthur Hemery | bee4a75 | 2019-05-29 10:50:55 | [diff] [blame] | 4225 | is_history_navigation_in_new_child_frame; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4226 | |
| 4227 | // TODO(clamy): |intended_as_new_entry| below should always be false once |
| 4228 | // Reload no longer leads to this being called for a pending NavigationEntry |
| 4229 | // of index -1. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4230 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4231 | entry->ConstructCommitNavigationParams( |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4232 | *frame_entry, common_params->url, common_params->method, |
| 4233 | entry->GetSubframeUniqueNames(frame_tree_node), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4234 | GetPendingEntryIndex() == -1 /* intended_as_new_entry */, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 4235 | GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(), |
Liam Brady | d2a41e15 | 2022-07-19 13:58:48 | [diff] [blame] | 4236 | frame_tree_node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4237 | frame_tree_node->AncestorOrSelfHasCSPEE(), |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4238 | navigation_start_system_entropy, soft_navigation_heuristics_task_id); |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4239 | commit_params->post_content_type = post_content_type; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4240 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4241 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4242 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4243 | |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4244 | if (common_params->url.IsAboutSrcdoc()) { |
| 4245 | // TODO(wjmaclean): initialize this in NavigationRequest's constructor |
| 4246 | // instead. |
W. James MacLean | 81b8d01f | 2022-01-25 20:50:59 | [diff] [blame] | 4247 | commit_params->srcdoc_value = frame_tree_node->srcdoc_value(); |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4248 | } |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4249 | const bool is_browser_initiated = !initiator_frame_token; |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 4250 | return NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4251 | frame_tree_node, std::move(common_params), std::move(commit_params), |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4252 | is_browser_initiated, false /* was_opener_suppressed */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4253 | initiator_frame_token, initiator_process_id, entry->extra_headers(), |
| 4254 | frame_entry, entry, is_form_submission, nullptr /* navigation_ui_data */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4255 | std::nullopt /* impression */, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4256 | blink::mojom::NavigationInitiatorActivationAndAdStatus:: |
| 4257 | kDidNotStartWithTransientActivation, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 4258 | false /* is_pdf */); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4259 | } |
| 4260 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4261 | void NavigationControllerImpl::NotifyNavigationEntryCommitted( |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4262 | LoadCommittedDetails* details) { |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 4263 | details->entry = GetLastCommittedEntry(); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4264 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 4265 | // We need to notify the ssl_manager_ before the WebContents so the |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4266 | // location bar will have up-to-date information about the security style |
| 4267 | // when it wants to draw. See http://crbug.com/11157 |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 4268 | ssl_manager_.DidCommitProvisionalLoad(*details); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4269 | |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4270 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 4271 | delegate_->NotifyNavigationEntryCommitted(*details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4272 | } |
| 4273 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4274 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4275 | size_t NavigationControllerImpl::max_entry_count() { |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4276 | if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4277 | return max_entry_count_for_testing_; |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 4278 | return blink::kMaxSessionHistoryEntries; |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4279 | } |
| 4280 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4281 | void NavigationControllerImpl::SetActive(bool is_active) { |
[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 4282 | if (is_active && needs_reload_) |
| 4283 | LoadIfNecessary(); |
Khushal Sagar | ace0e734 | 2024-08-07 21:34:21 | [diff] [blame] | 4284 | |
| 4285 | if (frame_tree_->is_primary(); |
| 4286 | auto* cache = GetNavigationEntryScreenshotCache()) { |
| 4287 | cache->SetVisible(is_active); |
| 4288 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4289 | } |
| 4290 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4291 | void NavigationControllerImpl::LoadIfNecessary() { |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 4292 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending", |
| 4293 | !!pending_entry_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4294 | if (!needs_reload_) |
| 4295 | return; |
| 4296 | |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 4297 | UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType", |
| 4298 | needs_reload_type_); |
| 4299 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4300 | // Calling Reload() results in ignoring state, and not loading. |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 4301 | // Explicitly use NavigateToExistingPendingEntry so that the renderer uses the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4302 | // cached state. |
Xinghui Lu | e4e50be | 2024-05-15 21:46:00 | [diff] [blame] | 4303 | if (entry_replaced_by_post_commit_error_) { |
| 4304 | // If the current entry is a post commit error, we reload the entry it |
| 4305 | // replaced instead. We leave the error entry in place until a commit |
| 4306 | // replaces it, but the pending entry points to the original entry in the |
| 4307 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 4308 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 4309 | // Note that this handling is similar to |
| 4310 | // `NavigationControllerImpl::Reload()`. |
| 4311 | pending_entry_ = entry_replaced_by_post_commit_error_.get(); |
| 4312 | pending_entry_index_ = GetCurrentEntryIndex(); |
| 4313 | NavigateToExistingPendingEntry( |
| 4314 | ReloadType::NONE, |
| 4315 | /*initiator_rfh=*/nullptr, |
| 4316 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
| 4317 | /*navigation_api_key=*/nullptr); |
| 4318 | } else if (pending_entry_) { |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4319 | NavigateToExistingPendingEntry( |
| 4320 | ReloadType::NONE, |
| 4321 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4322 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4323 | /*navigation_api_key=*/nullptr); |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4324 | } else if (!GetLastCommittedEntry() |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4325 | ->IsInitialEntryNotForSynchronousAboutBlank()) { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4326 | pending_entry_ = entries_[last_committed_entry_index_].get(); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4327 | pending_entry_index_ = last_committed_entry_index_; |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4328 | NavigateToExistingPendingEntry( |
| 4329 | ReloadType::NONE, |
| 4330 | /*initiator_rfh=*/nullptr, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4331 | /*soft_navigation_heuristics_task_id=*/std::nullopt, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4332 | /*navigation_api_key=*/nullptr); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4333 | } else { |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4334 | // We should never navigate to an existing initial NavigationEntry that is |
| 4335 | // the initial NavigationEntry for the initial empty document that hasn't |
| 4336 | // been overridden by the synchronous about:blank commit, to preserve |
| 4337 | // legacy behavior where trying to reload when the main frame is on the |
| 4338 | // initial empty document won't result in a navigation. See also |
| 4339 | // https://crbug.com/1277414. If there is something to reload, the |
| 4340 | // successful reload will clear the |needs_reload_| flag. Otherwise, just do |
| 4341 | // it here. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4342 | needs_reload_ = false; |
| 4343 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4344 | } |
| 4345 | |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4346 | base::WeakPtr<NavigationHandle> |
| 4347 | NavigationControllerImpl::LoadPostCommitErrorPage( |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4348 | RenderFrameHost* render_frame_host, |
| 4349 | const GURL& url, |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 4350 | const std::string& error_page_html) { |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4351 | RenderFrameHostImpl* rfhi = |
| 4352 | static_cast<RenderFrameHostImpl*>(render_frame_host); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 4353 | |
| 4354 | // Only active documents can load post-commit error pages: |
| 4355 | // - If the document is in pending deletion, the browser already committed to |
| 4356 | // destroying this RenderFrameHost so ignore loading the error page. |
| 4357 | // - If the document is in back-forward cache, it's not allowed to navigate as |
| 4358 | // it should remain frozen. Ignore the request and evict the document from |
| 4359 | // back-forward cache. |
| 4360 | // - If the document is prerendering, it can navigate but when loading error |
| 4361 | // pages, cancel prerendering. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 4362 | if (rfhi->IsInactiveAndDisallowActivation( |
| 4363 | DisallowActivationReasonId::kLoadPostCommitErrorPage)) { |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4364 | return nullptr; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 4365 | } |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 4366 | |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4367 | FrameTreeNode* node = rfhi->frame_tree_node(); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4368 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4369 | blink::mojom::CommonNavigationParamsPtr common_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 4370 | blink::CreateCommonNavigationParams(); |
Rakina Zata Amni | d2da154 | 2020-12-23 00:52:59 | [diff] [blame] | 4371 | // |url| might be empty, such as when LoadPostCommitErrorPage happens before |
| 4372 | // the frame actually committed (e.g. iframe with "src" set to a |
| 4373 | // slow-responding URL). We should rewrite the URL to about:blank in this |
| 4374 | // case, as the renderer will only think a page is an error page if it has a |
| 4375 | // non-empty unreachable URL. |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4376 | common_params->url = url.is_empty() ? GURL("about:blank") : url; |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4377 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 4378 | blink::CreateCommitNavigationParams(); |
Antonio Sartori | 58591c89 | 2021-04-21 06:54:33 | [diff] [blame] | 4379 | commit_params->original_url = common_params->url; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4380 | |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4381 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4382 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4383 | node, blink::mojom::SystemEntropy::kNormal); |
| 4384 | |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 4385 | // TODO(arthursonzogni): Consider providing the minimal capabilities to the |
| 4386 | // error pages. |
Liam Brady | 34639ec9 | 2023-10-13 19:50:45 | [diff] [blame] | 4387 | commit_params->frame_policy = node->pending_frame_policy(); |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 4388 | |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4389 | std::unique_ptr<NavigationRequest> navigation_request = |
| 4390 | NavigationRequest::CreateBrowserInitiated( |
| 4391 | node, std::move(common_params), std::move(commit_params), |
Hiroshige Hayashizaki | f07ad781 | 2023-05-10 02:26:09 | [diff] [blame] | 4392 | false /* was_opener_suppressed */, "" /* extra_headers */, |
| 4393 | nullptr /* frame_entry */, nullptr /* entry */, |
| 4394 | false /* is_form_submission */, nullptr /* navigation_ui_data */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4395 | std::nullopt /* impression */, false /* is_pdf */); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4396 | navigation_request->set_post_commit_error_page_html(error_page_html); |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 4397 | navigation_request->set_net_error(net::ERR_BLOCKED_BY_CLIENT); |
Charlie Reis | 09952ee | 2022-12-08 16:35:07 | [diff] [blame] | 4398 | node->TakeNavigationRequest(std::move(navigation_request)); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4399 | DCHECK(node->navigation_request()); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4400 | |
| 4401 | // Calling BeginNavigation may destroy the NavigationRequest. |
| 4402 | base::WeakPtr<NavigationRequest> created_navigation_request( |
| 4403 | node->navigation_request()->GetWeakPtr()); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4404 | node->navigation_request()->BeginNavigation(); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4405 | return created_navigation_request; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4406 | } |
| 4407 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4408 | void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4409 | EntryChangedDetails det; |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 4410 | det.changed_entry = entry; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4411 | det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry)); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 4412 | delegate_->NotifyNavigationEntryChanged(det); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4413 | } |
| 4414 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4415 | void NavigationControllerImpl::FinishRestore(int selected_index, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4416 | RestoreType type) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4417 | // TODO(crbug.com/40816356): Don't allow an index of -1, which would |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4418 | // represent a no-committed-entry state. |
| 4419 | DCHECK(selected_index >= -1 && selected_index < GetEntryCount()); |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4420 | ConfigureEntriesForRestore(&entries_, type); |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4421 | // TODO(crbug.com/40816356): This will be pointing to the wrong entry |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4422 | // if `entries_` contains pre-existing entries from the NavigationController |
| 4423 | // before restore, which would not be removed and will be at the front of the |
| 4424 | // entries list, causing the index to be off by the amount of pre-existing |
| 4425 | // entries in the list. Fix this to point to the correct entry. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4426 | last_committed_entry_index_ = selected_index; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4427 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 4428 | |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 4429 | void NavigationControllerImpl::DiscardNonCommittedEntries() { |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 4430 | DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */); |
| 4431 | } |
| 4432 | |
| 4433 | void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails( |
| 4434 | LoadCommittedDetails* commit_details) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4435 | // Avoid sending a notification if there is nothing to discard. |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4436 | // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help |
| 4437 | // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570. |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 4438 | if (!pending_entry_ && failed_pending_entry_id_ == 0) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4439 | return; |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4440 | } |
avi | 45a7253 | 2015-04-07 21:01:45 | [diff] [blame] | 4441 | DiscardPendingEntry(false); |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 4442 | |
| 4443 | if (!delegate_) |
| 4444 | return; |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4445 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | b12eb22 | 2013-09-10 00:11:48 | [diff] [blame] | 4446 | } |
| 4447 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 4448 | int NavigationControllerImpl::GetEntryIndexWithUniqueID( |
| 4449 | int nav_entry_id) const { |
| 4450 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
| 4451 | if (entries_[i]->GetUniqueID() == nav_entry_id) |
| 4452 | return i; |
| 4453 | } |
| 4454 | return -1; |
| 4455 | } |
| 4456 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4457 | void NavigationControllerImpl::InsertEntriesFrom( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4458 | NavigationControllerImpl* source, |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4459 | int max_index) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4460 | DCHECK_LE(max_index, source->GetEntryCount()); |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4461 | NavigationEntryRestoreContextImpl context; |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4462 | for (int i = 0; i < max_index; i++) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4463 | // Normally, cloning a NavigationEntryImpl results in sharing |
| 4464 | // FrameNavigationEntries between the original and the clone. However, when |
| 4465 | // cloning from a different NavigationControllerImpl, we want to fork the |
| 4466 | // FrameNavigationEntries. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4467 | entries_.insert(entries_.begin() + i, |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4468 | source->entries_[i]->CloneWithoutSharing(&context)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4469 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4470 | DCHECK_GE(entries_.size(), 1u); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4471 | DCHECK(pending_entry_index_ == -1 || |
| 4472 | pending_entry_ == GetEntryAtIndex(pending_entry_index_)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4473 | } |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4474 | |
| 4475 | void NavigationControllerImpl::SetGetTimestampCallbackForTest( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 4476 | const base::RepeatingCallback<base::Time()>& get_timestamp_callback) { |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4477 | get_timestamp_callback_ = get_timestamp_callback; |
| 4478 | } |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4479 | |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4480 | // History manipulation intervention: |
| 4481 | void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4482 | bool replace_entry, |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4483 | bool previous_document_had_history_intervention_activation, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4484 | bool is_renderer_initiated, |
| 4485 | ukm::SourceId previous_page_load_ukm_source_id) { |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4486 | // Note that for a subframe, |
| 4487 | // previous_document_had_history_intervention_activation is true if the |
Shivani Sharma | 712d5d7 | 2019-04-16 21:56:45 | [diff] [blame] | 4488 | // gesture happened in any subframe (propagated to main frame) or in the main |
| 4489 | // frame itself. |
Shivani Sharma | eef521b | 2024-01-18 13:03:56 | [diff] [blame] | 4490 | if (replace_entry || previous_document_had_history_intervention_activation || |
shivanigithub | cceeacf | 2020-03-06 20:00:27 | [diff] [blame] | 4491 | !is_renderer_initiated) { |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4492 | return; |
| 4493 | } |
| 4494 | if (last_committed_entry_index_ == -1) |
| 4495 | return; |
| 4496 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4497 | SetSkippableForSameDocumentEntries(last_committed_entry_index_, true); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4498 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4499 | // Log UKM with the URL we are navigating away from. |
| 4500 | ukm::builders::HistoryManipulationIntervention( |
| 4501 | previous_page_load_ukm_source_id) |
| 4502 | .Record(ukm::UkmRecorder::Get()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4503 | } |
| 4504 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4505 | void NavigationControllerImpl::SetSkippableForSameDocumentEntries( |
| 4506 | int reference_index, |
| 4507 | bool skippable) { |
| 4508 | auto* reference_entry = GetEntryAtIndex(reference_index); |
| 4509 | reference_entry->set_should_skip_on_back_forward_ui(skippable); |
| 4510 | |
| 4511 | int64_t document_sequence_number = |
| 4512 | reference_entry->root_node()->frame_entry->document_sequence_number(); |
| 4513 | for (int index = 0; index < GetEntryCount(); index++) { |
| 4514 | auto* entry = GetEntryAtIndex(index); |
| 4515 | if (entry->root_node()->frame_entry->document_sequence_number() == |
| 4516 | document_sequence_number) { |
| 4517 | entry->set_should_skip_on_back_forward_ui(skippable); |
| 4518 | } |
| 4519 | } |
| 4520 | } |
| 4521 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4522 | std::unique_ptr<NavigationControllerImpl::PendingEntryRef> |
| 4523 | NavigationControllerImpl::ReferencePendingEntry() { |
| 4524 | DCHECK(pending_entry_); |
| 4525 | auto pending_entry_ref = |
| 4526 | std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr()); |
| 4527 | pending_entry_refs_.insert(pending_entry_ref.get()); |
| 4528 | return pending_entry_ref; |
| 4529 | } |
| 4530 | |
| 4531 | void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) { |
| 4532 | // Ignore refs that don't correspond to the current pending entry. |
| 4533 | auto it = pending_entry_refs_.find(ref); |
| 4534 | if (it == pending_entry_refs_.end()) |
| 4535 | return; |
| 4536 | pending_entry_refs_.erase(it); |
| 4537 | |
| 4538 | if (!pending_entry_refs_.empty()) |
| 4539 | return; |
| 4540 | |
| 4541 | // The pending entry may be deleted before the last PendingEntryRef. |
| 4542 | if (!pending_entry_) |
| 4543 | return; |
| 4544 | |
| 4545 | // We usually clear the pending entry when the matching NavigationRequest |
| 4546 | // fails, so that an arbitrary URL isn't left visible above a committed page. |
| 4547 | // |
| 4548 | // However, we do preserve the pending entry in some cases, such as on the |
| 4549 | // initial navigation of an unmodified blank tab. We also allow the delegate |
| 4550 | // to say when it's safe to leave aborted URLs in the omnibox, to let the |
| 4551 | // user edit the URL and try again. This may be useful in cases that the |
| 4552 | // committed page cannot be attacker-controlled. In these cases, we still |
| 4553 | // allow the view to clear the pending entry and typed URL if the user |
| 4554 | // requests (e.g., hitting Escape with focus in the address bar). |
| 4555 | // |
| 4556 | // Do not leave the pending entry visible if it has an invalid URL, since this |
| 4557 | // might be formatted in an unexpected or unsafe way. |
| 4558 | // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824. |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4559 | bool should_preserve_entry = |
| 4560 | (pending_entry_ == GetVisibleEntry()) && |
| 4561 | pending_entry_->GetURL().is_valid() && |
| 4562 | (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs()); |
| 4563 | if (should_preserve_entry) |
| 4564 | return; |
| 4565 | |
| 4566 | DiscardPendingEntry(true); |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4567 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4568 | } |
| 4569 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4570 | std::unique_ptr<PolicyContainerPolicies> |
| 4571 | NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry( |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4572 | RenderFrameHostImpl* rfh, |
| 4573 | bool is_same_document, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4574 | const GURL& url) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4575 | if (is_same_document) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4576 | DCHECK(GetLastCommittedEntry()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4577 | FrameNavigationEntry* previous_frame_entry = |
| 4578 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
| 4579 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4580 | // TODO(crbug.com/40467594): Remove this nullptr check when we can |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4581 | // ensure we always have a FrameNavigationEntry here. |
| 4582 | if (!previous_frame_entry) |
| 4583 | return nullptr; |
| 4584 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4585 | const PolicyContainerPolicies* previous_policies = |
| 4586 | previous_frame_entry->policy_container_policies(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4587 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4588 | if (!previous_policies) |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4589 | return nullptr; |
| 4590 | |
| 4591 | // Make a copy of the policy container for the new FrameNavigationEntry. |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4592 | return previous_policies->ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4593 | } |
| 4594 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4595 | return rfh->policy_container_host()->policies().ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4596 | } |
| 4597 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4598 | void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() { |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4599 | OPTIONAL_TRACE_EVENT2( |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4600 | "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength", |
| 4601 | "history_offset", GetLastCommittedEntryIndex(), "history_length", |
| 4602 | GetEntryCount()); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4603 | |
| 4604 | auto callback = base::BindRepeating( |
| 4605 | [](int history_offset, int history_length, RenderViewHostImpl* rvh) { |
| 4606 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) { |
| 4607 | broadcast->SetHistoryOffsetAndLength(history_offset, history_length); |
| 4608 | } |
| 4609 | }, |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4610 | GetLastCommittedEntryIndex(), GetEntryCount()); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4611 | frame_tree_->root()->render_manager()->ExecutePageBroadcastMethod(callback); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | void NavigationControllerImpl::DidAccessInitialMainDocument() { |
| 4615 | // We may have left a failed browser-initiated navigation in the address bar |
| 4616 | // to let the user edit it and try again. Clear it now that content might |
| 4617 | // show up underneath it. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4618 | if (!frame_tree_->IsLoadingIncludingInnerFrameTrees() && GetPendingEntry()) |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4619 | DiscardPendingEntry(false); |
| 4620 | |
| 4621 | // Update the URL display. |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4622 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | void NavigationControllerImpl::UpdateStateForFrame( |
| 4626 | RenderFrameHostImpl* rfhi, |
| 4627 | const blink::PageState& page_state) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4628 | OPTIONAL_TRACE_EVENT1("content", |
| 4629 | "NavigationControllerImpl::UpdateStateForFrame", |
| 4630 | "render_frame_host", rfhi); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4631 | // The state update affects the last NavigationEntry associated with the given |
| 4632 | // |render_frame_host|. This may not be the last committed NavigationEntry (as |
| 4633 | // in the case of an UpdateState from a frame being swapped out). We track |
| 4634 | // which entry this is in the RenderFrameHost's nav_entry_id. |
| 4635 | NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id()); |
| 4636 | if (!entry) |
| 4637 | return; |
| 4638 | |
| 4639 | FrameNavigationEntry* frame_entry = |
| 4640 | entry->GetFrameEntry(rfhi->frame_tree_node()); |
| 4641 | if (!frame_entry) |
| 4642 | return; |
| 4643 | |
| 4644 | // The SiteInstance might not match if we do a cross-process navigation with |
| 4645 | // replacement (e.g., auto-subframe), in which case the swap out of the old |
| 4646 | // RenderFrameHost runs in the background after the old FrameNavigationEntry |
| 4647 | // has already been replaced and destroyed. |
| 4648 | if (frame_entry->site_instance() != rfhi->GetSiteInstance()) |
| 4649 | return; |
| 4650 | |
| 4651 | if (page_state == frame_entry->page_state()) |
| 4652 | return; // Nothing to update. |
| 4653 | |
| 4654 | DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState."; |
| 4655 | |
| 4656 | // The document_sequence_number and item_sequence_number recorded in the |
| 4657 | // FrameNavigationEntry should not differ from the one coming with the update, |
| 4658 | // since it must come from the same document. Do not update it if a difference |
| 4659 | // is detected, as this indicates that |frame_entry| is not the correct one. |
| 4660 | blink::ExplodedPageState exploded_state; |
| 4661 | if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state)) |
| 4662 | return; |
| 4663 | |
| 4664 | if (exploded_state.top.document_sequence_number != |
| 4665 | frame_entry->document_sequence_number() || |
| 4666 | exploded_state.top.item_sequence_number != |
| 4667 | frame_entry->item_sequence_number()) { |
| 4668 | return; |
| 4669 | } |
| 4670 | |
| 4671 | frame_entry->SetPageState(page_state); |
| 4672 | NotifyEntryChanged(entry); |
| 4673 | } |
| 4674 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4675 | namespace { |
| 4676 | |
| 4677 | // The caller is responsible for ensuring the entry is same-origin to the |
| 4678 | // origin to be committed. |
| 4679 | blink::mojom::NavigationApiHistoryEntryPtr ToNavigationApiHistoryEntry( |
| 4680 | FrameNavigationEntry* frame_entry, |
| 4681 | int64_t pending_document_sequence_number) { |
| 4682 | blink::ExplodedPageState exploded_state; |
| 4683 | if (!blink::DecodePageState(frame_entry->page_state().ToEncodedData(), |
| 4684 | &exploded_state)) { |
| 4685 | return nullptr; |
| 4686 | } |
| 4687 | blink::ExplodedFrameState frame_state = exploded_state.top; |
| 4688 | |
| 4689 | // If the document represented by this FNE hid its full url from appearing in |
| 4690 | // a referrer via a "no-referrer" or "origin" referrer policy, censor the url |
| 4691 | // in the navigation API as well (unless we're navigating to that document). |
| 4692 | std::u16string url; |
| 4693 | if (pending_document_sequence_number == |
| 4694 | frame_entry->document_sequence_number() || |
| 4695 | !frame_entry->protect_url_in_navigation_api()) { |
| 4696 | url = frame_state.url_string.value_or(std::u16string()); |
| 4697 | } |
| 4698 | |
| 4699 | return blink::mojom::NavigationApiHistoryEntry::New( |
| 4700 | frame_state.navigation_api_key.value_or(std::u16string()), |
| 4701 | frame_state.navigation_api_id.value_or(std::u16string()), url, |
| 4702 | frame_state.item_sequence_number, frame_state.document_sequence_number, |
| 4703 | frame_state.navigation_api_state); |
| 4704 | } |
| 4705 | |
| 4706 | } // namespace |
| 4707 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4708 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> |
| 4709 | NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4710 | Direction direction, |
| 4711 | int entry_index, |
| 4712 | const url::Origin& pending_origin, |
| 4713 | FrameTreeNode* node, |
| 4714 | SiteInstance* site_instance, |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4715 | int64_t pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4716 | int64_t pending_document_sequence_number, |
| 4717 | int& last_index_checked) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4718 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4719 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4720 | // Don't process the initial entry. |
| 4721 | DCHECK_EQ(GetEntryCount(), 1); |
| 4722 | return entries; |
| 4723 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4724 | int offset = direction == Direction::kForward ? 1 : -1; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4725 | int64_t previous_item_sequence_number = pending_item_sequence_number; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4726 | for (int i = entry_index + offset; i >= 0 && i < GetEntryCount(); |
| 4727 | i += offset) { |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4728 | last_index_checked = i; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4729 | FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node); |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4730 | if (!frame_entry) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4731 | break; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4732 | // An entry should only appear in the navigation API entries if it is for |
| 4733 | // the same origin as the document being committed. Check the committed |
| 4734 | // origin, or if that is not available (during restore), check against the |
| 4735 | // FNE's url. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4736 | // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry() |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4737 | // once we can be sure that entries with the same ISN will never be |
| 4738 | // cross-origin. |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4739 | url::Origin frame_entry_origin = |
| 4740 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4741 | frame_entry->url(), |
| 4742 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 4743 | if (!pending_origin.IsSameOriginWith(frame_entry_origin)) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4744 | break; |
| 4745 | if (previous_item_sequence_number == frame_entry->item_sequence_number()) |
| 4746 | continue; |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4747 | if (blink::mojom::NavigationApiHistoryEntryPtr entry = |
| 4748 | ToNavigationApiHistoryEntry(frame_entry, |
| 4749 | pending_document_sequence_number)) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4750 | DCHECK(entry->url.empty() || |
| 4751 | pending_origin.CanBeDerivedFrom(GURL(entry->url))); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4752 | entries.push_back(std::move(entry)); |
| 4753 | previous_item_sequence_number = frame_entry->item_sequence_number(); |
| 4754 | } |
| 4755 | } |
| 4756 | // If |entries| was constructed by iterating backwards from |
| 4757 | // |entry_index|, it's latest-at-the-front, but the renderer will want it |
| 4758 | // earliest-at-the-front. Reverse it. |
| 4759 | if (direction == Direction::kBack) |
| 4760 | std::reverse(entries.begin(), entries.end()); |
| 4761 | return entries; |
| 4762 | } |
| 4763 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4764 | blink::mojom::NavigationApiHistoryEntryArraysPtr |
| 4765 | NavigationControllerImpl::GetNavigationApiHistoryEntryVectors( |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4766 | FrameTreeNode* node, |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4767 | NavigationRequest* request) { |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4768 | url::Origin pending_origin = request |
Lukasz Anforowicz | 435e68d | 2022-11-09 21:47:44 | [diff] [blame] | 4769 | ? request->GetOriginToCommit().value() |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4770 | : url::Origin::Create(node->current_url()); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4771 | |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4772 | scoped_refptr<SiteInstance> site_instance = |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4773 | node->current_frame_host()->GetSiteInstance(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4774 | |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4775 | // NOTE: |entry_index| is the index where this entry will commit if no |
| 4776 | // modifications are made between now and DidCommitNavigation. This is used to |
| 4777 | // walk |entries_| and determine which entries should be exposed by the |
| 4778 | // navigation API. It is important to calculate this correctly, because blink |
| 4779 | // will cancel a same-document history commit if it's not present in the |
| 4780 | // entries blink knows about. |
| 4781 | int entry_index = GetLastCommittedEntryIndex(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4782 | int64_t pending_item_sequence_number = 0; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4783 | int64_t pending_document_sequence_number = 0; |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4784 | bool will_create_new_entry = false; |
| 4785 | if (GetPendingEntryIndex() != -1) { |
| 4786 | entry_index = GetPendingEntryIndex(); |
| 4787 | if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) { |
| 4788 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
| 4789 | pending_document_sequence_number = |
| 4790 | frame_entry->document_sequence_number(); |
| 4791 | } |
| 4792 | } else if (request && |
| 4793 | !NavigationTypeUtils::IsReload( |
| 4794 | request->common_params().navigation_type) && |
| 4795 | !NavigationTypeUtils::IsHistory( |
| 4796 | request->common_params().navigation_type) && |
| 4797 | !request->common_params().should_replace_current_entry && |
| 4798 | !request->common_params() |
| 4799 | .is_history_navigation_in_new_child_frame) { |
| 4800 | will_create_new_entry = true; |
| 4801 | entry_index = GetLastCommittedEntryIndex() + 1; |
| 4802 | // Don't set pending_item_sequence_number or |
| 4803 | // pending_document_sequence_number in this case - a new unique isn/dsn will |
| 4804 | // be calculated in the renderer later. |
| 4805 | } else if (GetLastCommittedEntryIndex() != -1) { |
| 4806 | entry_index = GetLastCommittedEntryIndex(); |
| 4807 | if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) { |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4808 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4809 | pending_document_sequence_number = |
| 4810 | frame_entry->document_sequence_number(); |
| 4811 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4812 | } |
| 4813 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4814 | auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New(); |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4815 | if (entry_index == -1) { |
| 4816 | // TODO(rakina): Exit early when there is no last committed entry. |
| 4817 | // Remove when InitialNavigationEntry ships. |
| 4818 | return entry_arrays; |
| 4819 | } |
| 4820 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4821 | int backmost_index = entry_index; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4822 | entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4823 | Direction::kBack, entry_index, pending_origin, node, site_instance.get(), |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4824 | pending_item_sequence_number, pending_document_sequence_number, |
| 4825 | backmost_index); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4826 | |
| 4827 | // 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] | 4828 | int forwardmost_index = entry_index; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4829 | if (!will_create_new_entry) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4830 | entry_arrays->forward_entries = |
| 4831 | PopulateSingleNavigationApiHistoryEntryVector( |
| 4832 | Direction::kForward, entry_index, pending_origin, node, |
| 4833 | site_instance.get(), pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4834 | pending_document_sequence_number, forwardmost_index); |
| 4835 | } |
| 4836 | |
| 4837 | // If the previous entry is within the block of contiguous entries being |
| 4838 | // provided, then report it as the `previous_entry`. |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4839 | FrameNavigationEntry* previous_entry = nullptr; |
| 4840 | if (frame_tree_->is_prerendering()) { |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 4841 | FrameTreeNodeId initiator_id = PrerenderHost::GetFromFrameTreeNode(*node) |
| 4842 | .initiator_frame_tree_node_id(); |
| 4843 | if (initiator_id) { |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4844 | auto* initiator_node = FrameTreeNode::GloballyFindByID(initiator_id); |
| 4845 | previous_entry = initiator_node->frame_tree() |
| 4846 | .controller() |
| 4847 | .GetLastCommittedEntry() |
| 4848 | ->GetFrameEntry(initiator_node); |
| 4849 | } |
| 4850 | } else if (GetLastCommittedEntryIndex() != -1 && |
| 4851 | GetLastCommittedEntryIndex() >= backmost_index && |
| 4852 | GetLastCommittedEntryIndex() <= forwardmost_index) { |
| 4853 | previous_entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 4854 | } |
| 4855 | if (previous_entry) { |
| 4856 | url::Origin previous_entry_origin = |
| 4857 | previous_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4858 | previous_entry->url(), |
| 4859 | previous_entry->initiator_origin().value_or(url::Origin()))); |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 4860 | // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry() |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4861 | // once we can be sure that entries with the same ISN will never be |
| 4862 | // cross-origin. |
| 4863 | if (pending_origin.IsSameOriginWith(previous_entry_origin)) { |
| 4864 | entry_arrays->previous_entry = ToNavigationApiHistoryEntry( |
| 4865 | previous_entry, pending_document_sequence_number); |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4866 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4867 | } |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4868 | |
Nate Chapin | 4e657a47 | 2022-02-06 03:38:37 | [diff] [blame] | 4869 | return entry_arrays; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4870 | } |
| 4871 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4872 | NavigationControllerImpl::HistoryNavigationAction |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4873 | NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4874 | FrameNavigationEntry* current_entry, |
| 4875 | FrameNavigationEntry* target_entry, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4876 | const std::string& navigation_api_key) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4877 | if (!target_entry || !target_entry->committed_origin()) |
| 4878 | return HistoryNavigationAction::kStopLooking; |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4879 | if (!current_entry->committed_origin()->IsSameOriginWith( |
| 4880 | *target_entry->committed_origin())) { |
| 4881 | return HistoryNavigationAction::kStopLooking; |
| 4882 | } |
| 4883 | |
| 4884 | // NOTE: We don't actually care between kSameDocument and |
| 4885 | // kDifferentDocument, so always use kDifferentDocument by convention. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4886 | if (target_entry->navigation_api_key() == navigation_api_key) |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4887 | return HistoryNavigationAction::kDifferentDocument; |
| 4888 | return HistoryNavigationAction::kKeepLooking; |
| 4889 | } |
| 4890 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4891 | void NavigationControllerImpl::NavigateToNavigationApiKey( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4892 | RenderFrameHostImpl* initiator_rfh, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 4893 | std::optional<blink::scheduler::TaskAttributionId> |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4894 | soft_navigation_heuristics_task_id, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4895 | const std::string& key) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4896 | FrameTreeNode* node = initiator_rfh->frame_tree_node(); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4897 | FrameNavigationEntry* current_entry = |
| 4898 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 4899 | if (!current_entry) |
| 4900 | return; |
| 4901 | |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 4902 | // TODO(crbug.com/40878000): Make sure that the right task ID is passed |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4903 | // when `navigation.traverseTo()` is called. |
| 4904 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4905 | // We want to find the nearest matching entry that is contiguously |
| 4906 | // same-instance and same-origin. Check back first, then forward. |
| 4907 | // TODO(japhet): Link spec here once it exists. |
| 4908 | for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4909 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4910 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4911 | if (result == HistoryNavigationAction::kStopLooking) |
| 4912 | break; |
| 4913 | if (result != HistoryNavigationAction::kKeepLooking) { |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4914 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4915 | return; |
| 4916 | } |
| 4917 | } |
| 4918 | for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4919 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4920 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4921 | if (result == HistoryNavigationAction::kStopLooking) |
| 4922 | break; |
| 4923 | if (result != HistoryNavigationAction::kKeepLooking) { |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4924 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4925 | return; |
| 4926 | } |
| 4927 | } |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4928 | |
| 4929 | // If we fall through to here, a matching NavigationEntry couldn't be found. |
| 4930 | // Notify the renderer that the navigation was cancelled. |
| 4931 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 4932 | key, blink::mojom::TraverseCancelledReason::kNotFound); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4933 | } |
| 4934 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4935 | bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4936 | network::mojom::ReferrerPolicy referrer_policy) { |
| 4937 | return referrer_policy == network::mojom::ReferrerPolicy::kNever || |
| 4938 | referrer_policy == network::mojom::ReferrerPolicy::kOrigin; |
| 4939 | } |
| 4940 | |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4941 | bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory( |
| 4942 | const FrameTreeNode* frame_tree_node) const { |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 4943 | // TODO(crbug.com/40176906): We may have to add portals in addition to |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4944 | // prerender and fenced frames. This should be kept in sync with |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4945 | // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory. |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 4946 | // The preview mode appears as prerendered page in renderers, and |
| 4947 | // GetDocument()->IsPrerendering() covers the case together. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4948 | return frame_tree_->is_prerendering() || |
Takashi Toyoshima | 8dfc05c | 2024-01-29 21:03:51 | [diff] [blame] | 4949 | frame_tree_->page_delegate()->IsPageInPreviewMode() || |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4950 | frame_tree_node->IsInFencedFrameTree(); |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4951 | } |
| 4952 | |
Julie Jeongeun Kim | 0e24224 | 2022-11-30 10:45:09 | [diff] [blame] | 4953 | void NavigationControllerImpl::DidChangeReferrerPolicy( |
| 4954 | FrameTreeNode* node, |
| 4955 | network::mojom::ReferrerPolicy referrer_policy) { |
| 4956 | FrameNavigationEntry* entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 4957 | if (!entry) |
| 4958 | return; |
| 4959 | |
| 4960 | // The FrameNavigationEntry may want to change whether to protect its url |
| 4961 | // in the navigation API when the referrer policy changes. |
| 4962 | entry->set_protect_url_in_navigation_api( |
| 4963 | ShouldProtectUrlInNavigationApi(referrer_policy)); |
| 4964 | } |
| 4965 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4966 | } // namespace content |