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" |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 49 | #include "base/strings/string_piece.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 50 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 51 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 52 | #include "base/time/time.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 53 | #include "base/trace_event/optional_trace_event.h" |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 54 | #include "base/trace_event/trace_conversion_helper.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 55 | #include "base/trace_event/trace_event.h" |
Daniel Cheng | abb00686 | 2022-09-09 22:39:08 | [diff] [blame] | 56 | #include "base/types/optional_util.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 57 | #include "build/build_config.h" |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 58 | #include "cc/base/switches.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 59 | #include "content/browser/bad_message.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 60 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 61 | #include "content/browser/browser_context_impl.h" |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 62 | #include "content/browser/browser_url_handler_impl.h" |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 63 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 64 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 65 | #include "content/browser/preloading/prerender/prerender_host.h" |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 66 | #include "content/browser/process_lock.h" |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 67 | #include "content/browser/renderer_host/back_forward_cache_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 68 | #include "content/browser/renderer_host/debug_urls.h" |
| 69 | #include "content/browser/renderer_host/frame_tree.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 70 | #include "content/browser/renderer_host/frame_tree_node.h" |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 71 | #include "content/browser/renderer_host/navigation_controller_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 72 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 73 | #include "content/browser/renderer_host/navigation_entry_restore_context_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 74 | #include "content/browser/renderer_host/navigation_request.h" |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 75 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h" |
| 76 | #include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_manager.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 77 | #include "content/browser/renderer_host/navigator.h" |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 78 | #include "content/browser/renderer_host/page_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 79 | #include "content/browser/renderer_host/render_frame_host_delegate.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 80 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 81 | #include "content/browser/renderer_host/system_entropy_utils.h" |
Sharon Yang | d70a539 | 2021-10-26 23:06:32 | [diff] [blame] | 82 | #include "content/browser/site_info.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 83 | #include "content/browser/site_instance_impl.h" |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 84 | #include "content/common/content_constants_internal.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" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 92 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 93 | #include "content/public/browser/notification_types.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 94 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 95 | #include "content/public/browser/render_widget_host.h" |
| 96 | #include "content/public/browser/render_widget_host_view.h" |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 97 | #include "content/public/browser/replaced_navigation_entry_data.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 98 | #include "content/public/browser/storage_partition.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 99 | #include "content/public/common/content_client.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 100 | #include "content/public/common/content_constants.h" |
toyoshim | 86e34ec | 2016-02-25 08:56:10 | [diff] [blame] | 101 | #include "content/public/common/content_features.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 102 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 103 | #include "content/public/common/url_utils.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 104 | #include "media/base/mime_util.h" |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 105 | #include "net/base/net_errors.h" |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 106 | #include "net/http/http_status_code.h" |
Shivani Sharma | 9332910 | 2019-01-24 19:44:18 | [diff] [blame] | 107 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 108 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Yue Ru Sun | 12880493 | 2020-09-30 22:19:17 | [diff] [blame] | 109 | #include "services/metrics/public/cpp/ukm_source_id.h" |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 110 | #include "services/network/public/mojom/fetch_api.mojom.h" |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 111 | #include "services/network/public/mojom/url_response_head.mojom-shared.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 112 | #include "skia/ext/platform_canvas.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 113 | #include "third_party/blink/public/common/blob/blob_utils.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 114 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 115 | #include "third_party/blink/public/common/history/session_history_constants.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 116 | #include "third_party/blink/public/common/mime_util/mime_util.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 117 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 118 | #include "third_party/blink/public/common/page_state/page_state_serialization.h" |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 119 | #include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h" |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 120 | #include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h" |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 121 | #include "third_party/blink/public/mojom/runtime_feature_state/runtime_feature.mojom.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 122 | #include "url/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 124 | namespace content { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 125 | namespace { |
| 126 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 127 | // Invoked when entries have been pruned, or removed. For example, if the |
| 128 | // current entries are [google, digg, yahoo], with the current entry google, |
| 129 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 130 | void NotifyPrunedEntries(NavigationControllerImpl* nav_controller, |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 131 | int index, |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 132 | int count) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 133 | PrunedDetails details; |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 134 | details.index = index; |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 135 | details.count = count; |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 136 | nav_controller->delegate()->NotifyNavigationListPruned(details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 139 | // Configure all the NavigationEntries in entries for restore. This resets |
| 140 | // the transition type to reload and makes sure the content state isn't empty. |
| 141 | void ConfigureEntriesForRestore( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 142 | std::vector<std::unique_ptr<NavigationEntryImpl>>* entries, |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 143 | RestoreType type) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 144 | for (auto& entry : *entries) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 145 | // Use a transition type of reload so that we don't incorrectly increase |
| 146 | // the typed count. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 147 | entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 148 | entry->set_restore_type(type); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 152 | // Determines whether or not we should be carrying over a user agent override |
| 153 | // between two NavigationEntries. |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 154 | bool ShouldKeepOverride(NavigationEntry* last_entry) { |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 155 | return last_entry && last_entry->GetIsOverridingUserAgent(); |
| 156 | } |
| 157 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 158 | // Determines whether to override user agent for a navigation. |
| 159 | bool ShouldOverrideUserAgent( |
| 160 | NavigationController::UserAgentOverrideOption override_user_agent, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 161 | NavigationEntry* last_committed_entry) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 162 | switch (override_user_agent) { |
| 163 | case NavigationController::UA_OVERRIDE_INHERIT: |
| 164 | return ShouldKeepOverride(last_committed_entry); |
| 165 | case NavigationController::UA_OVERRIDE_TRUE: |
| 166 | return true; |
| 167 | case NavigationController::UA_OVERRIDE_FALSE: |
| 168 | return false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 169 | } |
| 170 | NOTREACHED(); |
| 171 | return false; |
| 172 | } |
| 173 | |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 174 | // Returns true if this navigation should be treated as a reload. For e.g. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 175 | // 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] | 176 | // link which results in a navigation to the last committed URL (but wasn't |
| 177 | // converted to do a replacement navigation in the renderer), etc. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 178 | // |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|, |
| 179 | // |base_url_for_data_url|, |transition_type| correspond to the new navigation |
| 180 | // (i.e. the pending NavigationEntry). |last_committed_entry| is the last |
| 181 | // navigation that committed. |
| 182 | bool ShouldTreatNavigationAsReload(FrameTreeNode* node, |
| 183 | const GURL& url, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 184 | const GURL& virtual_url, |
| 185 | const GURL& base_url_for_data_url, |
| 186 | ui::PageTransition transition_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 187 | bool is_post, |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 188 | bool should_replace_current_entry, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 189 | NavigationEntryImpl* last_committed_entry) { |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 190 | // Navigations intended to do a replacement shouldn't be converted to do a |
| 191 | // reload. |
| 192 | if (should_replace_current_entry) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 193 | return false; |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 194 | // Only convert to reload if at least one navigation committed. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 195 | if (last_committed_entry->IsInitialEntry()) |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 196 | return false; |
| 197 | |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 198 | // Skip navigations initiated by external applications. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 199 | if (transition_type & ui::PAGE_TRANSITION_FROM_API) |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 200 | return false; |
| 201 | |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 202 | // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR), |
| 203 | // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations |
| 204 | // which should be treated as reloads. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 205 | bool transition_type_can_be_converted = false; |
| 206 | if (ui::PageTransitionCoreTypeIs(transition_type, |
| 207 | ui::PAGE_TRANSITION_RELOAD) && |
| 208 | (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) { |
| 209 | transition_type_can_be_converted = true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 210 | } |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 211 | if (ui::PageTransitionCoreTypeIs(transition_type, |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 212 | ui::PAGE_TRANSITION_TYPED)) { |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 213 | transition_type_can_be_converted = true; |
| 214 | } |
| 215 | if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK)) |
| 216 | transition_type_can_be_converted = true; |
| 217 | if (!transition_type_can_be_converted) |
| 218 | return false; |
| 219 | |
| 220 | // This check is required for cases like view-source:, etc. Here the URL of |
| 221 | // the navigation entry would contain the url of the page, while the virtual |
| 222 | // URL contains the full URL including the view-source prefix. |
| 223 | if (virtual_url != last_committed_entry->GetVirtualURL()) |
| 224 | return false; |
| 225 | |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 226 | // Check that the URLs match. |
| 227 | FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node); |
| 228 | // If there's no frame entry then by definition the URLs don't match. |
| 229 | if (!frame_entry) |
| 230 | return false; |
| 231 | |
| 232 | if (url != frame_entry->url()) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 233 | return false; |
| 234 | |
| 235 | // This check is required for Android WebView loadDataWithBaseURL. Apps |
| 236 | // can pass in anything in the base URL and we need to ensure that these |
| 237 | // match before classifying it as a reload. |
| 238 | if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) { |
| 239 | if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL()) |
| 240 | return false; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 241 | } |
| 242 | |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 243 | // Skip entries with SSL errors. |
| 244 | if (last_committed_entry->ssl_error()) |
| 245 | return false; |
| 246 | |
| 247 | // Don't convert to a reload when the last navigation was a POST or the new |
| 248 | // navigation is a POST. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 249 | if (frame_entry->get_has_post_data() || is_post) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 250 | return false; |
| 251 | |
| 252 | return true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 253 | } |
| 254 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 255 | absl::optional<url::Origin> GetCommittedOriginForFrameEntry( |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 256 | const mojom::DidCommitProvisionalLoadParams& params, |
| 257 | NavigationRequest* request) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 258 | // Error pages commit in an opaque origin, yet have the real URL that resulted |
| 259 | // in an error as the |params.url|. Since successful reload of an error page |
| 260 | // should commit in the correct origin, setting the opaque origin on the |
| 261 | // FrameNavigationEntry will be incorrect. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 262 | if (request && request->DidEncounterError()) |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 263 | return absl::nullopt; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 264 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 265 | return absl::make_optional(params.origin); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 266 | } |
| 267 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 268 | bool IsValidURLForNavigation(FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 269 | const GURL& virtual_url, |
| 270 | const GURL& dest_url) { |
| 271 | // 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] | 272 | if (node->IsOutermostMainFrame() && !virtual_url.is_valid() && |
| 273 | !virtual_url.is_empty()) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 274 | LOG(WARNING) << "Refusing to load for invalid virtual URL: " |
| 275 | << virtual_url.possibly_invalid_spec(); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | // Don't attempt to navigate to non-empty invalid URLs. |
| 280 | if (!dest_url.is_valid() && !dest_url.is_empty()) { |
| 281 | LOG(WARNING) << "Refusing to load invalid URL: " |
| 282 | << dest_url.possibly_invalid_spec(); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // The renderer will reject IPC messages with URLs longer than |
| 287 | // this limit, so don't attempt to navigate with a longer URL. |
| 288 | if (dest_url.spec().size() > url::kMaxURLChars) { |
| 289 | LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars |
| 290 | << " characters."; |
| 291 | return false; |
| 292 | } |
| 293 | |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 294 | // Reject renderer debug URLs because they should have been handled before |
| 295 | // we get to this point. This check handles renderer debug URLs |
| 296 | // that are inside a view-source: URL (e.g. view-source:chrome://kill) and |
| 297 | // provides defense-in-depth if a renderer debug URL manages to get here via |
| 298 | // some other path. We want to reject the navigation here so it doesn't |
| 299 | // violate assumptions in downstream code. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 300 | if (blink::IsRendererDebugURL(dest_url)) { |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 301 | LOG(WARNING) << "Refusing to load renderer debug URL: " |
| 302 | << dest_url.possibly_invalid_spec(); |
| 303 | return false; |
| 304 | } |
| 305 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 306 | // Guests only support navigations to known-safe schemes. This check already |
| 307 | // exists in the extensions layer, where it also dispatches proper events to |
| 308 | // the guest's embedder (see WebViewGuest::LoadURLWithParams). This check is |
| 309 | // for defense-in-depth to ensure that no other places in the codebase |
| 310 | // accidentally navigate guests to schemes such as WebUI, which is not |
| 311 | // supported. See https://crbug.com/1444221. |
| 312 | if (node->current_frame_host()->GetSiteInstance()->IsGuest()) { |
| 313 | auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance(); |
| 314 | if (!cpsp->IsWebSafeScheme(dest_url.scheme()) && |
| 315 | !dest_url.SchemeIs(url::kAboutScheme)) { |
| 316 | LOG(WARNING) << "Refusing to load unsafe URL in a guest: " |
| 317 | << dest_url.possibly_invalid_spec(); |
| 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 325 | // See replaced_navigation_entry_data.h for details: this information is meant |
| 326 | // to ensure |*output_entry| keeps track of its original URL (landing page in |
| 327 | // case of server redirects) as it gets replaced (e.g. history.replaceState()), |
| 328 | // without overwriting it later, for main frames. |
| 329 | void CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 330 | NavigationEntryImpl* replaced_entry, |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 331 | NavigationEntryImpl* output_entry) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 332 | if (output_entry->GetReplacedEntryData().has_value() || |
| 333 | replaced_entry->IsInitialEntry()) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 334 | return; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 335 | } |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 336 | |
| 337 | ReplacedNavigationEntryData data; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 338 | data.first_committed_url = replaced_entry->GetURL(); |
| 339 | data.first_timestamp = replaced_entry->GetTimestamp(); |
| 340 | data.first_transition_type = replaced_entry->GetTransitionType(); |
Charlie Reis | b55438f | 2019-01-08 01:54:29 | [diff] [blame] | 341 | output_entry->set_replaced_entry_data(data); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 342 | } |
| 343 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 344 | blink::mojom::NavigationType GetNavigationType( |
| 345 | const GURL& old_url, |
| 346 | const GURL& new_url, |
| 347 | ReloadType reload_type, |
| 348 | NavigationEntryImpl* entry, |
| 349 | const FrameNavigationEntry& frame_entry, |
| 350 | bool has_pending_cross_document_commit, |
| 351 | bool is_currently_error_page, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 352 | bool is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 353 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 354 | bool is_unfenced_top_navigation) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 355 | // Reload navigations |
| 356 | switch (reload_type) { |
| 357 | case ReloadType::NORMAL: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 358 | return blink::mojom::NavigationType::RELOAD; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 359 | case ReloadType::BYPASSING_CACHE: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 360 | return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 361 | case ReloadType::NONE: |
| 362 | break; // Fall through to rest of function. |
| 363 | } |
| 364 | |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 365 | if (entry->IsRestored()) { |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 366 | return entry->GetHasPostData() |
| 367 | ? blink::mojom::NavigationType::RESTORE_WITH_POST |
| 368 | : blink::mojom::NavigationType::RESTORE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 369 | } |
| 370 | |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 371 | const bool can_be_same_document = |
| 372 | // A pending cross-document commit means this navigation will not occur in |
| 373 | // the current document, as that document would end up being replaced in |
| 374 | // the meantime. |
| 375 | !has_pending_cross_document_commit && |
| 376 | // If the current document is an error page, we should always treat it as |
| 377 | // a different-document navigation so that we'll attempt to load the |
| 378 | // document we're navigating to (and not stay in the current error page). |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 379 | !is_currently_error_page && |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 380 | // If the navigation is an embedder-initiated navigation of a fenced |
| 381 | // frame root (i.e. enters a fenced frame tree from outside), |
| 382 | // same-document navigations should be disabled because we don't want to |
| 383 | // allow information to be joined across multiple embedder-initiated |
| 384 | // fenced frame navigations (which may contain separate cross-site data). |
| 385 | !is_embedder_initiated_fenced_frame_navigation && |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 386 | // If the navigation is to _unfencedTop (i.e. escapes a fenced frame), |
| 387 | // same-document navigations should be disabled because we want to force |
| 388 | // the creation of a new browsing context group. |
| 389 | !is_unfenced_top_navigation; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 390 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 391 | // History navigations. |
| 392 | if (frame_entry.page_state().IsValid()) { |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 393 | return can_be_same_document && is_same_document_history_load |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 394 | ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT |
| 395 | : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 396 | } |
| 397 | DCHECK(!is_same_document_history_load); |
| 398 | |
| 399 | // A same-document fragment-navigation happens when the only part of the url |
| 400 | // that is modified is after the '#' character. |
| 401 | // |
| 402 | // When modifying this condition, please take a look at: |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 403 | // FrameLoader::ShouldPerformFragmentNavigation(). |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 404 | // |
| 405 | // Note: this check is only valid for navigations that are not history |
| 406 | // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a |
| 407 | // history navigation from 'A#foo' to 'A#bar' is not a same-document |
| 408 | // navigation, but a different-document one. This is why history navigation |
| 409 | // are classified before this check. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 410 | bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) && |
| 411 | frame_entry.method() == "GET"; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 412 | |
| 413 | // The one case where we do the wrong thing here and incorrectly choose |
| 414 | // SAME_DOCUMENT is if the navigation is browser-initiated but the document in |
| 415 | // the renderer is a frameset. All frameset navigations should be |
| 416 | // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated |
| 417 | // navigation would do the right thing, as it would send it to the browser and |
| 418 | // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get |
| 419 | // into this method). But since we can't tell that case here for browser- |
| 420 | // initiated navigations, we have to get the renderer involved. In that case |
| 421 | // the navigation would be restarted due to the renderer spending a reply of |
| 422 | // mojom::CommitResult::RestartCrossDocument. |
| 423 | |
| 424 | return can_be_same_document && is_same_doc |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 425 | ? blink::mojom::NavigationType::SAME_DOCUMENT |
| 426 | : blink::mojom::NavigationType::DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 427 | } |
| 428 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 429 | // Adjusts the original input URL if needed, to get the URL to actually load and |
| 430 | // the virtual URL, which may differ. |
| 431 | void RewriteUrlForNavigation(const GURL& original_url, |
| 432 | BrowserContext* browser_context, |
| 433 | GURL* url_to_load, |
| 434 | GURL* virtual_url, |
| 435 | bool* reverse_on_redirect) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 436 | // Allow the browser URL handler to rewrite the URL. This will, for example, |
| 437 | // remove "view-source:" from the beginning of the URL to get the URL that |
| 438 | // will actually be loaded. This real URL won't be shown to the user, just |
| 439 | // used internally. |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 440 | *url_to_load = *virtual_url = original_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 441 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 442 | url_to_load, browser_context, reverse_on_redirect); |
| 443 | } |
| 444 | |
| 445 | #if DCHECK_IS_ON() |
| 446 | // Helper sanity check function used in debug mode. |
| 447 | void ValidateRequestMatchesEntry(NavigationRequest* request, |
| 448 | NavigationEntryImpl* entry) { |
| 449 | if (request->frame_tree_node()->IsMainFrame()) { |
| 450 | DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated()); |
| 451 | DCHECK(ui::PageTransitionTypeIncludingQualifiersIs( |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 452 | ui::PageTransitionFromInt(request->common_params().transition), |
| 453 | entry->GetTransitionType())); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 454 | } |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 455 | DCHECK_EQ(request->commit_params().should_clear_history_list, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 456 | entry->should_clear_history_list()); |
| 457 | DCHECK_EQ(request->common_params().has_user_gesture, |
| 458 | entry->has_user_gesture()); |
| 459 | DCHECK_EQ(request->common_params().base_url_for_data_url, |
| 460 | entry->GetBaseURLForDataURL()); |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 461 | DCHECK_EQ(request->commit_params().can_load_local_resources, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 462 | entry->GetCanLoadLocalResources()); |
| 463 | DCHECK_EQ(request->common_params().started_from_context_menu, |
| 464 | entry->has_started_from_context_menu()); |
| 465 | |
| 466 | FrameNavigationEntry* frame_entry = |
| 467 | entry->GetFrameEntry(request->frame_tree_node()); |
| 468 | if (!frame_entry) { |
| 469 | NOTREACHED(); |
| 470 | return; |
| 471 | } |
| 472 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 473 | DCHECK_EQ(request->common_params().method, frame_entry->method()); |
| 474 | |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 475 | size_t redirect_size = request->commit_params().redirects.size(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 476 | if (redirect_size == frame_entry->redirect_chain().size()) { |
| 477 | for (size_t i = 0; i < redirect_size; ++i) { |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 478 | DCHECK_EQ(request->commit_params().redirects[i], |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 479 | frame_entry->redirect_chain()[i]); |
| 480 | } |
| 481 | } else { |
| 482 | NOTREACHED(); |
| 483 | } |
| 484 | } |
| 485 | #endif // DCHECK_IS_ON() |
| 486 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 487 | // Returns whether the session history NavigationRequests in |navigations| |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 488 | // would stay within the subtree of |sandboxed_initiator_rfh|. |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 489 | bool DoesSandboxNavigationStayWithinSubtree( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 490 | RenderFrameHostImpl* sandboxed_initiator_rfh, |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 491 | const std::vector<std::unique_ptr<NavigationRequest>>& navigations) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 492 | DCHECK(sandboxed_initiator_rfh); |
| 493 | DCHECK(sandboxed_initiator_rfh->IsSandboxed( |
| 494 | network::mojom::WebSandboxFlags::kTopNavigation)); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 495 | for (auto& item : navigations) { |
| 496 | bool within_subtree = false; |
| 497 | // Check whether this NavigationRequest affects a frame within the |
| 498 | // sandboxed frame's subtree by walking up the tree looking for the |
| 499 | // sandboxed frame. |
| 500 | for (auto* frame = item->frame_tree_node(); frame; |
Alexander Timin | 381e7e18 | 2020-04-28 19:04:03 | [diff] [blame] | 501 | frame = FrameTreeNode::From(frame->parent())) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 502 | if (frame == sandboxed_initiator_rfh->frame_tree_node()) { |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 503 | within_subtree = true; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | if (!within_subtree) |
| 508 | return false; |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 513 | } // namespace |
| 514 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 515 | // NavigationControllerImpl::PendingEntryRef------------------------------------ |
| 516 | |
| 517 | NavigationControllerImpl::PendingEntryRef::PendingEntryRef( |
| 518 | base::WeakPtr<NavigationControllerImpl> controller) |
| 519 | : controller_(controller) {} |
| 520 | |
| 521 | NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() { |
| 522 | if (!controller_) // Can be null with interstitials. |
| 523 | return; |
| 524 | |
| 525 | controller_->PendingEntryRefDeleted(this); |
| 526 | } |
| 527 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 528 | // NavigationControllerImpl ---------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | |
[email protected] | 23a918b | 2014-07-15 09:51:36 | [diff] [blame] | 530 | const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1); |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 531 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 532 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 533 | size_t NavigationControllerImpl::max_entry_count_for_testing_ = |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 534 | kMaxEntryCountForTestingNotSet; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 535 | |
[email protected] | e6fec47 | 2013-05-14 05:29:02 | [diff] [blame] | 536 | // 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] | 537 | // when testing. |
| 538 | static bool g_check_for_repost = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 540 | // static |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 541 | std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry( |
| 542 | const GURL& url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 543 | Referrer referrer, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 544 | absl::optional<url::Origin> initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 545 | absl::optional<GURL> initiator_base_url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 546 | ui::PageTransition transition, |
| 547 | bool is_renderer_initiated, |
| 548 | const std::string& extra_headers, |
| 549 | BrowserContext* browser_context, |
| 550 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { |
| 551 | return NavigationControllerImpl::CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 552 | url, referrer, std::move(initiator_origin), std::move(initiator_base_url), |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 553 | absl::nullopt /* source_process_site_url */, transition, |
| 554 | is_renderer_initiated, extra_headers, browser_context, |
| 555 | std::move(blob_url_loader_factory), true /* rewrite_virtual_urls */); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // static |
| 559 | std::unique_ptr<NavigationEntryImpl> |
| 560 | NavigationControllerImpl::CreateNavigationEntry( |
| 561 | const GURL& url, |
| 562 | Referrer referrer, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 563 | absl::optional<url::Origin> initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 564 | absl::optional<GURL> initiator_base_url, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 565 | absl::optional<GURL> source_process_site_url, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 566 | ui::PageTransition transition, |
| 567 | bool is_renderer_initiated, |
| 568 | const std::string& extra_headers, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 569 | BrowserContext* browser_context, |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 570 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
| 571 | bool rewrite_virtual_urls) { |
| 572 | GURL url_to_load = url; |
| 573 | GURL virtual_url = url; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 574 | bool reverse_on_redirect = false; |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 575 | if (rewrite_virtual_urls) { |
| 576 | RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url, |
| 577 | &reverse_on_redirect); |
| 578 | } |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 579 | // Let the NTP override the navigation params and pretend that this is a |
| 580 | // browser-initiated, bookmark-like navigation. |
| 581 | GetContentClient()->browser()->OverrideNavigationParams( |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 582 | source_process_site_url, &transition, &is_renderer_initiated, &referrer, |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 583 | &initiator_origin); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 584 | |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 585 | auto entry = std::make_unique<NavigationEntryImpl>( |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 586 | nullptr, // The site instance for tabs is sent on navigation |
| 587 | // (WebContents::GetSiteInstance). |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 588 | url_to_load, referrer, initiator_origin, initiator_base_url, |
| 589 | std::u16string(), transition, is_renderer_initiated, |
| 590 | blob_url_loader_factory, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 591 | /* is_initial_entry = */ false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 592 | entry->SetVirtualURL(virtual_url); |
| 593 | entry->set_user_typed_url(virtual_url); |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 594 | entry->set_update_virtual_url_with_url(reverse_on_redirect); |
| 595 | entry->set_extra_headers(extra_headers); |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 596 | return entry; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 597 | } |
| 598 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 599 | // static |
| 600 | void NavigationController::DisablePromptOnRepost() { |
| 601 | g_check_for_repost = false; |
| 602 | } |
| 603 | |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 604 | base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime( |
| 605 | base::Time t) { |
| 606 | // If |t| is between the water marks, we're in a run of duplicates |
| 607 | // or just getting out of it, so increase the high-water mark to get |
| 608 | // a time that probably hasn't been used before and return it. |
| 609 | if (low_water_mark_ <= t && t <= high_water_mark_) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 610 | high_water_mark_ += base::Microseconds(1); |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 611 | return high_water_mark_; |
| 612 | } |
| 613 | |
| 614 | // Otherwise, we're clear of the last duplicate run, so reset the |
| 615 | // water marks. |
| 616 | low_water_mark_ = high_water_mark_ = t; |
| 617 | return t; |
| 618 | } |
| 619 | |
ckitagawa | 0faa5e4 | 2020-06-17 17:30:54 | [diff] [blame] | 620 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 621 | ScopedShowRepostDialogForTesting() |
| 622 | : was_disallowed_(g_check_for_repost) { |
| 623 | g_check_for_repost = true; |
| 624 | } |
| 625 | |
| 626 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 627 | ~ScopedShowRepostDialogForTesting() { |
| 628 | g_check_for_repost = was_disallowed_; |
| 629 | } |
| 630 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 631 | NavigationControllerImpl::RemovedEntriesTracker::RemovedEntriesTracker( |
| 632 | base::SafeRef<NavigationControllerImpl> controller) |
| 633 | : controller_(controller) { |
| 634 | for (FrameTreeNode* frame_tree_node : controller_->frame_tree().Nodes()) { |
| 635 | names_to_nodes_.insert({frame_tree_node->unique_name(), frame_tree_node}); |
| 636 | frame_tree_node_id_to_keys_.insert( |
| 637 | {frame_tree_node->frame_tree_node_id(), std::set<std::string>()}); |
| 638 | if (auto* frame_entry = frame_tree_node->current_frame_host() |
| 639 | ->last_committed_frame_entry()) { |
| 640 | frame_tree_node_id_to_doc_seq_nos_.insert( |
| 641 | {frame_tree_node->frame_tree_node_id(), |
| 642 | frame_entry->document_sequence_number()}); |
| 643 | } |
| 644 | } |
| 645 | PopulateKeySet(Direction::kBack); |
| 646 | PopulateKeySet(Direction::kForward); |
| 647 | } |
| 648 | |
| 649 | void NavigationControllerImpl::RemovedEntriesTracker::PopulateKeySet( |
| 650 | Direction direction) { |
| 651 | // Keep track of which FrameTreeNodes may still have relevant API keys in |
| 652 | // additional FrameNavigationEntries. |
| 653 | std::set<FrameTreeNode*> nodes_to_process; |
| 654 | for (FrameTreeNode* node : controller_->frame_tree().Nodes()) { |
| 655 | nodes_to_process.insert(node); |
| 656 | } |
| 657 | |
| 658 | const int offset = direction == Direction::kForward ? 1 : -1; |
| 659 | const int start = direction == Direction::kForward |
| 660 | ? controller_->GetLastCommittedEntryIndex() |
| 661 | : controller_->GetLastCommittedEntryIndex() - 1; |
| 662 | for (int i = start; |
| 663 | i >= 0 && i < controller_->GetEntryCount() && !nodes_to_process.empty(); |
| 664 | i += offset) { |
| 665 | std::set<FrameTreeNode*> nodes_to_continue_processing; |
| 666 | |
| 667 | NavigationEntryImpl* entry = controller_->GetEntryAtIndex(i); |
| 668 | entry->ForEachFrameEntry([this, &nodes_to_process, |
| 669 | &nodes_to_continue_processing, |
| 670 | &entry](FrameNavigationEntry* frame_entry) { |
| 671 | // Find the |node| that matches |frame_entry|, if any. |
| 672 | FrameTreeNode* node = nullptr; |
| 673 | if (frame_entry == entry->root_node()->frame_entry) { |
| 674 | node = controller_->frame_tree().root(); |
| 675 | } else { |
| 676 | auto it = names_to_nodes_.find(frame_entry->frame_unique_name()); |
| 677 | if (it == names_to_nodes_.end()) |
| 678 | return; |
| 679 | node = it->second; |
| 680 | } |
| 681 | |
| 682 | // Skip this node if a previous step determine there are no longer |
| 683 | // relevant navigation API keys in this direction. |
| 684 | if (nodes_to_process.find(node) == nodes_to_process.end()) |
| 685 | return; |
| 686 | |
| 687 | // Stop processing |node| if we reach a point where it's cross-origin. |
| 688 | // See also PopulateSingleNavigationApiHistoryEntryVector(). |
| 689 | url::Origin frame_entry_origin = |
| 690 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 691 | frame_entry->url(), |
| 692 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 693 | if (!node->current_origin().IsSameOriginWith(frame_entry_origin)) |
| 694 | return; |
| 695 | |
| 696 | frame_tree_node_id_to_keys_[node->frame_tree_node_id()].insert( |
| 697 | frame_entry->navigation_api_key()); |
| 698 | // Mark |node| as needing more processing for the next entry. |
| 699 | nodes_to_continue_processing.insert(node); |
| 700 | |
| 701 | // Check whether |node| went cross-document. If so, its children are |
| 702 | // no longer the same conceptual iframe as the current one, and |
| 703 | // should no longer be processed. This check is intentionally done |
| 704 | // after processing the current |node|, which may still have relevant |
| 705 | // discarded API keys. |
| 706 | if (frame_entry->document_sequence_number() != |
| 707 | frame_tree_node_id_to_doc_seq_nos_[node->frame_tree_node_id()]) { |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 708 | for (auto* descendant : node->frame_tree().SubtreeNodes(node)) |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 709 | nodes_to_process.erase(descendant); |
| 710 | } |
| 711 | }); |
| 712 | |
| 713 | nodes_to_process.swap(nodes_to_continue_processing); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | NavigationControllerImpl::RemovedEntriesTracker::~RemovedEntriesTracker() { |
| 718 | std::set<std::string> all_keys; |
| 719 | // Find all remaining navigation API keys after some entries have been |
| 720 | // removed. |
| 721 | for (auto& entry : controller_->entries_) { |
| 722 | entry->ForEachFrameEntry([&all_keys](FrameNavigationEntry* frame_entry) { |
| 723 | all_keys.insert(frame_entry->navigation_api_key()); |
| 724 | }); |
| 725 | } |
| 726 | |
| 727 | // Notify each frame in the renderer of any disposed navigation API keys. |
| 728 | for (auto& id_to_keys : frame_tree_node_id_to_keys_) { |
| 729 | std::vector<std::string> disposed_keys; |
| 730 | for (const auto& key : id_to_keys.second) { |
| 731 | if (all_keys.find(key) == all_keys.end()) |
| 732 | disposed_keys.push_back(key); |
| 733 | } |
| 734 | if (disposed_keys.empty()) |
| 735 | continue; |
| 736 | |
| 737 | FrameTreeNode* node = controller_->frame_tree().FindByID(id_to_keys.first); |
| 738 | auto& frame = node->current_frame_host()->GetAssociatedLocalFrame(); |
| 739 | frame->NotifyNavigationApiOfDisposedEntries(disposed_keys); |
| 740 | } |
| 741 | } |
| 742 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 743 | NavigationControllerImpl::NavigationControllerImpl( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 744 | BrowserContext* browser_context, |
| 745 | FrameTree& frame_tree, |
| 746 | NavigationControllerDelegate* delegate) |
| 747 | : frame_tree_(frame_tree), |
| 748 | browser_context_(browser_context), |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 749 | delegate_(delegate), |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 750 | ssl_manager_(this), |
Mingyu Lei | 68f3441 | 2023-08-21 07:31:37 | [diff] [blame] | 751 | get_timestamp_callback_(base::BindRepeating(&base::Time::Now)), |
| 752 | back_forward_cache_(browser_context) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 753 | DCHECK(browser_context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 754 | } |
| 755 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 756 | NavigationControllerImpl::~NavigationControllerImpl() { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 757 | // The NavigationControllerImpl might be called inside its delegate |
| 758 | // destructor. Calling it is not valid anymore. |
| 759 | delegate_ = nullptr; |
| 760 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | } |
| 762 | |
Matt Falkenhagen | 548ed156 | 2021-07-06 01:38:26 | [diff] [blame] | 763 | WebContents* NavigationControllerImpl::DeprecatedGetWebContents() { |
| 764 | return delegate_->DeprecatedGetWebContents(); |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 765 | } |
| 766 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 767 | BrowserContext* NavigationControllerImpl::GetBrowserContext() { |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 768 | return browser_context_; |
| 769 | } |
| 770 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 771 | void NavigationControllerImpl::Restore( |
[email protected] | 5e36967 | 2009-11-03 23:48:30 | [diff] [blame] | 772 | int selected_navigation, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 773 | RestoreType type, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 774 | std::vector<std::unique_ptr<NavigationEntry>>* entries) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 775 | // Note that it's possible for `entries_` to contain multiple entries at this |
| 776 | // point, as Restore() might be called on a NavigationController that is |
| 777 | // already used (e.g. due to WebView's restoreState() API), not only for fresh |
| 778 | // NavigationControllers. These entries are not cleared to preserve legacy |
| 779 | // behavior and also because `pending_entry_` might point to one of the |
| 780 | // pre-existing entries. An exception for this is when `entries_` contains |
| 781 | // only the initial NavigationEntry, which must be removed. |
| 782 | |
| 783 | // Do not proceed if selected_navigation will be out of bounds for the updated |
| 784 | // entries_ list, since it will be assigned to last_committed_entry_index_ and |
| 785 | // used to index entries_. |
| 786 | // TODO(https://crbug.com/1287624): Consider also returning early if entries |
| 787 | // is empty, since there should be no work to do (rather than marking the |
| 788 | // existing entries as needing reload). Also consider returning early if the |
| 789 | // selected index is -1, which represents a no-committed-entry state. |
| 790 | if (selected_navigation < -1 || |
| 791 | selected_navigation >= |
| 792 | base::checked_cast<int>(entries->size() + entries_.size())) { |
| 793 | return; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 794 | } |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 795 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 796 | // There will always be at least one entry (new NavigationControllers will |
| 797 | // have the initial NavigationEntry). |
| 798 | if (selected_navigation == -1) |
| 799 | selected_navigation = 0; |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 800 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 801 | if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) { |
| 802 | // If we are on the initial NavigationEntry, it must be the only entry in |
| 803 | // the list. Since it's impossible to do a history navigation to the |
| 804 | // initial NavigationEntry, `pending_entry_index_` must be -1 (but |
| 805 | // `pending_entry` might be set for a new non-history navigation). |
| 806 | // Note that we should not clear `entries_` if `entries` is empty (when |
| 807 | // InitialNavigationEntry mode is enabled), since that would leave us |
| 808 | // without any NavigationEntry. |
| 809 | CHECK_EQ(1, GetEntryCount()); |
| 810 | CHECK_EQ(-1, pending_entry_index_); |
| 811 | entries_.clear(); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 812 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 813 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 814 | needs_reload_ = true; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 815 | needs_reload_type_ = NeedsReloadType::kRestoreSession; |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 816 | entries_.reserve(entries->size()); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 817 | for (auto& entry : *entries) { |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 818 | if (entry->GetURL().is_empty()) { |
| 819 | // 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] | 820 | // persisting the initial NavigationEntry [which is no longer possible but |
| 821 | // some old persisted sessions might still contain it] or when the |
| 822 | // serializer failed to write the URL because it's too long). Trying to |
| 823 | // restore and navigate to an entry with an empty URL will result in |
| 824 | // crashes, so change the URL to about:blank. See also |
| 825 | // https://crbug.com/1240138 and https://crbug.com/1299335. |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 826 | entry->SetURL(GURL(url::kAboutBlankURL)); |
| 827 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 828 | entries_.push_back( |
| 829 | NavigationEntryImpl::FromNavigationEntry(std::move(entry))); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 830 | } |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 831 | |
| 832 | // At this point, the |entries| is full of empty scoped_ptrs, so it can be |
| 833 | // cleared out safely. |
| 834 | entries->clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 835 | |
| 836 | // And finish the restore. |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 837 | FinishRestore(selected_navigation, type); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 838 | } |
| 839 | |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 840 | void NavigationControllerImpl::Reload(ReloadType reload_type, |
| 841 | bool check_for_repost) { |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 842 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type", |
| 843 | (int)reload_type); |
| 844 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check", |
| 845 | check_for_repost); |
liaoyuke | 9168fba | 2017-03-10 19:20:28 | [diff] [blame] | 846 | DCHECK_NE(ReloadType::NONE, reload_type); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 847 | NavigationEntryImpl* entry = nullptr; |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 848 | int current_index = -1; |
| 849 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 850 | if (entry_replaced_by_post_commit_error_) { |
| 851 | // If there is an entry that was replaced by a currently active post-commit |
| 852 | // error navigation, this can't be the initial navigation. |
| 853 | DCHECK(!IsInitialNavigation()); |
| 854 | // If the current entry is a post commit error, we reload the entry it |
| 855 | // replaced instead. We leave the error entry in place until a commit |
| 856 | // replaces it, but the pending entry points to the original entry in the |
| 857 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 858 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 859 | entry = entry_replaced_by_post_commit_error_.get(); |
| 860 | current_index = GetCurrentEntryIndex(); |
| 861 | } else if (IsInitialNavigation() && pending_entry_) { |
| 862 | // If we are reloading the initial navigation, just use the current |
| 863 | // pending entry. Otherwise look up the current entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 864 | entry = pending_entry_; |
| 865 | // The pending entry might be in entries_ (e.g., after a Clone), so we |
| 866 | // should also update the current_index. |
| 867 | current_index = pending_entry_index_; |
| 868 | } else { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 869 | DiscardNonCommittedEntries(); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 870 | current_index = GetCurrentEntryIndex(); |
| 871 | if (current_index != -1) { |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 872 | entry = GetEntryAtIndex(current_index); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 873 | } |
[email protected] | 979a4bc | 2013-04-24 01:27:15 | [diff] [blame] | 874 | } |
[email protected] | 241db35 | 2013-04-22 18:04:05 | [diff] [blame] | 875 | |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 876 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 877 | // CanReload method. |
| 878 | if (!entry) |
| 879 | return; |
| 880 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 881 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 882 | // We should never navigate to an existing initial NavigationEntry that is |
| 883 | // the initial NavigationEntry for the initial empty document that hasn't |
| 884 | // been overridden by the synchronous about:blank commit, to preserve |
| 885 | // legacy behavior where trying to reload when the main frame is on the |
| 886 | // initial empty document won't result in a navigation. See also |
| 887 | // https://crbug.com/1277414. |
| 888 | return; |
| 889 | } |
| 890 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 891 | if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) { |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 892 | // 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] | 893 | // they really want to do this. If they do, the dialog will call us back |
| 894 | // with check_for_repost = false. |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 895 | delegate_->NotifyBeforeFormRepostWarningShow(); |
[email protected] | 965bb09 | 2010-04-09 11:59:02 | [diff] [blame] | 896 | |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 897 | pending_reload_ = reload_type; |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 898 | delegate_->ActivateAndShowRepostFormWarningDialog(); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 899 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 900 | } |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 901 | |
Wang Hui | a25efabc | 2022-09-24 17:27:22 | [diff] [blame] | 902 | // Set ReloadType for |entry|. |
| 903 | entry->set_reload_type(reload_type); |
| 904 | |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 905 | if (!IsInitialNavigation()) |
| 906 | DiscardNonCommittedEntries(); |
| 907 | |
| 908 | pending_entry_ = entry; |
| 909 | pending_entry_index_ = current_index; |
| 910 | pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 911 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 912 | // location.reload() goes through BeginNavigation, so all reloads triggered |
| 913 | // via this codepath are browser initiated. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 914 | NavigateToExistingPendingEntry( |
| 915 | reload_type, |
| 916 | /*initiator_rfh=*/nullptr, |
| 917 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
| 918 | /*navigation_api_key=*/nullptr); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | } |
| 920 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 921 | void NavigationControllerImpl::CancelPendingReload() { |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 922 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 923 | } |
| 924 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 925 | void NavigationControllerImpl::ContinuePendingReload() { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 926 | // If the pending reload type has been cleared by another navigation |
| 927 | // committing, then do not proceed to reload after a form repost dialog. |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 928 | if (pending_reload_ == ReloadType::NONE) { |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 929 | return; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 930 | } |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 931 | Reload(pending_reload_, false); |
| 932 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 933 | } |
| 934 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 935 | bool NavigationControllerImpl::IsInitialNavigation() { |
[email protected] | 27ba81c | 2012-08-21 17:04:09 | [diff] [blame] | 936 | return is_initial_navigation_; |
[email protected] | c70f9b8 | 2010-04-21 07:31:11 | [diff] [blame] | 937 | } |
| 938 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 939 | bool NavigationControllerImpl::IsInitialBlankNavigation() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 940 | // Check that we're on the initial NavigationEntry and that this is not a |
| 941 | // cloned tab. |
| 942 | return IsInitialNavigation() && GetEntryCount() == 1 && |
| 943 | GetLastCommittedEntry()->IsInitialEntry() && |
| 944 | GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored; |
creis | 10a4ab7 | 2015-10-13 17:22:40 | [diff] [blame] | 945 | } |
| 946 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 947 | NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID( |
| 948 | int nav_entry_id) const { |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 949 | int index = GetEntryIndexWithUniqueID(nav_entry_id); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 950 | return (index != -1) ? entries_[index].get() : nullptr; |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 951 | } |
| 952 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 953 | NavigationEntryImpl* |
| 954 | NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending( |
| 955 | int nav_entry_id) const { |
| 956 | NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id); |
| 957 | if (entry) |
| 958 | return entry; |
| 959 | return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 960 | ? pending_entry_.get() |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 961 | : nullptr; |
| 962 | } |
| 963 | |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 964 | void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation( |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 965 | const url::Origin& origin) { |
| 966 | for (int i = 0; i < GetEntryCount(); i++) { |
| 967 | auto* entry = GetEntryAtIndex(i); |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 968 | entry->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 969 | } |
| 970 | if (entry_replaced_by_post_commit_error_) { |
| 971 | // It's possible we could come back to this entry if the error |
| 972 | // page/interstitial goes away. |
| 973 | entry_replaced_by_post_commit_error_ |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 974 | ->RegisterExistingOriginAsHavingDefaultIsolation(origin); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 975 | } |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 976 | } |
| 977 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 978 | void NavigationControllerImpl::SetPendingEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 979 | std::unique_ptr<NavigationEntryImpl> entry) { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 980 | DiscardNonCommittedEntries(); |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 981 | pending_entry_ = entry.release(); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 982 | DCHECK_EQ(-1, pending_entry_index_); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 983 | } |
| 984 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 985 | NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 986 | if (pending_entry_) |
| 987 | return pending_entry_; |
| 988 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 989 | } |
| 990 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 991 | NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() { |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 992 | // The pending entry is safe to return for new (non-history), browser- |
| 993 | // initiated navigations. Most renderer-initiated navigations should not |
| 994 | // show the pending entry, to prevent URL spoof attacks. |
| 995 | // |
| 996 | // We make an exception for renderer-initiated navigations in new tabs, as |
| 997 | // long as no other page has tried to access the initial empty document in |
| 998 | // the new tab. If another page modifies this blank page, a URL spoof is |
| 999 | // possible, so we must stop showing the pending entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1000 | bool safe_to_show_pending = |
| 1001 | pending_entry_ && |
| 1002 | // Require a new navigation. |
avi | 0dca04d | 2015-01-26 20:21:09 | [diff] [blame] | 1003 | pending_entry_index_ == -1 && |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1004 | // Require either browser-initiated or an unmodified new tab. |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 1005 | (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab()); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1006 | |
| 1007 | // Also allow showing the pending entry for history navigations in a new tab, |
| 1008 | // such as Ctrl+Back. In this case, no existing page is visible and no one |
| 1009 | // can script the new tab before it commits. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1010 | if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 && |
| 1011 | IsInitialNavigation() && !pending_entry_->is_renderer_initiated()) |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 1012 | safe_to_show_pending = true; |
| 1013 | |
| 1014 | if (safe_to_show_pending) |
[email protected] | 867e1f9 | 2011-08-30 19:01:19 | [diff] [blame] | 1015 | return pending_entry_; |
| 1016 | return GetLastCommittedEntry(); |
| 1017 | } |
| 1018 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1019 | int NavigationControllerImpl::GetCurrentEntryIndex() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1020 | if (pending_entry_index_ != -1) |
| 1021 | return pending_entry_index_; |
| 1022 | return last_committed_entry_index_; |
| 1023 | } |
| 1024 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1025 | NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 1026 | CHECK_NE(last_committed_entry_index_, -1); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1027 | return entries_[last_committed_entry_index_].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1028 | } |
| 1029 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1030 | bool NavigationControllerImpl::CanViewSource() { |
Takashi Toyoshima | 6c58bbd | 2023-05-19 09:41:35 | [diff] [blame] | 1031 | const std::string& mime_type = frame_tree_->root() |
| 1032 | ->current_frame_host() |
| 1033 | ->GetPage() |
| 1034 | .GetContentsMimeType(); |
Kinuko Yasuda | 74702f9 | 2017-07-31 03:27:53 | [diff] [blame] | 1035 | bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) && |
| 1036 | !media::IsSupportedMediaMimeType(mime_type); |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 1037 | NavigationEntry* visible_entry = GetVisibleEntry(); |
| 1038 | return visible_entry && !visible_entry->IsViewSourceMode() && |
Carlos IL | d51e770 | 2020-05-07 18:51:39 | [diff] [blame] | 1039 | is_viewable_mime_type; |
[email protected] | 3168228 | 2010-01-15 18:05:16 | [diff] [blame] | 1040 | } |
| 1041 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1042 | int NavigationControllerImpl::GetLastCommittedEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1043 | // 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] | 1044 | // entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1045 | DCHECK_LT(last_committed_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1046 | return last_committed_entry_index_; |
| 1047 | } |
| 1048 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1049 | int NavigationControllerImpl::GetEntryCount() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1050 | DCHECK_GE(entries_.size(), 1u); |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 1051 | DCHECK_LE(entries_.size(), max_entry_count()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1052 | return static_cast<int>(entries_.size()); |
| 1053 | } |
| 1054 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1055 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) { |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 1056 | if (index < 0 || index >= GetEntryCount()) |
| 1057 | return nullptr; |
| 1058 | |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 1059 | return entries_[index].get(); |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 1060 | } |
| 1061 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1062 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) { |
avi | 057ce149 | 2015-06-29 15:59:47 | [diff] [blame] | 1063 | return GetEntryAtIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1064 | } |
| 1065 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1066 | int NavigationControllerImpl::GetIndexForOffset(int offset) { |
[email protected] | 7bc2b03 | 2012-12-19 22:45:46 | [diff] [blame] | 1067 | return GetCurrentEntryIndex() + offset; |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1068 | } |
| 1069 | |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1070 | absl::optional<int> NavigationControllerImpl::GetIndexForGoBack() { |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1071 | for (int index = GetIndexForOffset(-1); index >= 0; index--) { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1072 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1073 | return index; |
| 1074 | } |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1075 | } |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1076 | return absl::nullopt; |
| 1077 | } |
| 1078 | |
| 1079 | bool NavigationControllerImpl::CanGoBack() { |
| 1080 | return GetIndexForGoBack().has_value(); |
| 1081 | } |
| 1082 | |
| 1083 | absl::optional<int> NavigationControllerImpl::GetIndexForGoForward() { |
| 1084 | for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) { |
| 1085 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) { |
| 1086 | return index; |
| 1087 | } |
| 1088 | } |
| 1089 | return absl::nullopt; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1090 | } |
| 1091 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1092 | bool NavigationControllerImpl::CanGoForward() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1093 | return GetIndexForGoForward().has_value(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1094 | } |
| 1095 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 1096 | bool NavigationControllerImpl::CanGoToOffset(int offset) { |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1097 | int index = GetIndexForOffset(offset); |
| 1098 | return index >= 0 && index < GetEntryCount(); |
| 1099 | } |
| 1100 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1101 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1102 | bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1103 | if (offset == 0) |
| 1104 | return true; |
| 1105 | int increment = offset > 0 ? 1 : -1; |
| 1106 | int non_skippable_entries = 0; |
| 1107 | for (int index = GetIndexForOffset(increment); |
| 1108 | index >= 0 && index < GetEntryCount(); index += increment) { |
| 1109 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1110 | non_skippable_entries++; |
| 1111 | |
| 1112 | if (non_skippable_entries == std::abs(offset)) |
| 1113 | return true; |
| 1114 | } |
| 1115 | return false; |
| 1116 | } |
| 1117 | #endif |
| 1118 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1119 | void NavigationControllerImpl::GoBack() { |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1120 | const absl::optional<int> target_index = GetIndexForGoBack(); |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1121 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1122 | CHECK(target_index.has_value()); |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 1123 | |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1124 | GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1125 | } |
| 1126 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1127 | void NavigationControllerImpl::GoForward() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 1128 | // Note that at least one entry (the last one) will be non-skippable since |
| 1129 | // entries are marked skippable only when they add another entry because of |
| 1130 | // redirect or pushState. |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1131 | const absl::optional<int> target_index = GetIndexForGoForward(); |
| 1132 | |
Kevin McNee | edc481c | 2023-04-27 22:30:58 | [diff] [blame] | 1133 | CHECK(target_index.has_value()); |
Kevin McNee | 3b3a5619 | 2023-03-17 14:40:59 | [diff] [blame] | 1134 | |
| 1135 | GoToIndex(*target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1136 | } |
| 1137 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1138 | void NavigationControllerImpl::GoToIndex(int index) { |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1139 | GoToIndex(index, /*initiator_rfh=*/nullptr, |
| 1140 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
| 1141 | /*navigation_api_key=*/nullptr); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 1142 | } |
| 1143 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1144 | void NavigationControllerImpl::GoToIndex( |
| 1145 | int index, |
| 1146 | RenderFrameHostImpl* initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1147 | absl::optional<blink::scheduler::TaskAttributionId> |
| 1148 | soft_navigation_heuristics_task_id, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1149 | const std::string* navigation_api_key) { |
sunjian | 30574a6 | 2017-03-21 21:39:44 | [diff] [blame] | 1150 | TRACE_EVENT0("browser,navigation,benchmark", |
| 1151 | "NavigationControllerImpl::GoToIndex"); |
Peter Boström | f68fe04 | 2023-06-07 18:27:50 | [diff] [blame] | 1152 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index); |
| 1153 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_size", |
| 1154 | entries_.size()); |
| 1155 | // Indices should have have been validated by the caller. |
| 1156 | CHECK_GE(index, 0); |
| 1157 | CHECK_LT(static_cast<size_t>(index), entries_.size()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1158 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1159 | if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 1160 | // We should never navigate to an existing initial NavigationEntry that is |
| 1161 | // the initial NavigationEntry for the initial empty document that hasn't |
| 1162 | // been overridden by the synchronous about:blank commit, to preserve |
| 1163 | // legacy behavior where trying to reload when the main frame is on the |
| 1164 | // initial empty document won't result in a navigation. See also |
| 1165 | // https://crbug.com/1277414. |
| 1166 | return; |
| 1167 | } |
| 1168 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1169 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1170 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1171 | DCHECK_EQ(nullptr, pending_entry_); |
| 1172 | DCHECK_EQ(-1, pending_entry_index_); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1173 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1174 | pending_entry_ = entries_[index].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1175 | pending_entry_index_ = index; |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1176 | pending_entry_->SetTransitionType(ui::PageTransitionFromInt( |
| 1177 | pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK)); |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1178 | NavigateToExistingPendingEntry(ReloadType::NONE, initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1179 | soft_navigation_heuristics_task_id, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1180 | navigation_api_key); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1181 | } |
| 1182 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1183 | void NavigationControllerImpl::GoToOffset(int offset) { |
toyoshim | 3af4d50 | 2016-03-30 12:38:12 | [diff] [blame] | 1184 | // Note: This is actually reached in unit tests. |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1185 | if (!CanGoToOffset(offset)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1186 | return; |
| 1187 | |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1188 | GoToIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1189 | } |
| 1190 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1191 | void NavigationControllerImpl::GoToOffsetFromRenderer( |
| 1192 | int offset, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1193 | RenderFrameHostImpl* initiator_rfh, |
| 1194 | absl::optional<blink::scheduler::TaskAttributionId> |
| 1195 | soft_navigation_heuristics_task_id) { |
Nate Chapin | bb74c5a5 | 2023-07-21 23:13:35 | [diff] [blame] | 1196 | // Note: This is actually reached in unit tests. |
| 1197 | if (!CanGoToOffset(offset)) |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1198 | return; |
| 1199 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 1200 | GoToIndex(GetIndexForOffset(offset), initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 1201 | soft_navigation_heuristics_task_id, |
| 1202 | /*navigation_api_key=*/nullptr); |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1203 | } |
| 1204 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1205 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1206 | void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) { |
| 1207 | // Note: This is actually reached in unit tests. |
| 1208 | if (!CanGoToOffsetWithSkipping(offset)) |
| 1209 | return; |
| 1210 | |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 1211 | if (offset == 0) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1212 | GoToIndex(GetIndexForOffset(offset)); |
| 1213 | return; |
| 1214 | } |
| 1215 | int increment = offset > 0 ? 1 : -1; |
| 1216 | // Find the offset without counting skippable entries. |
| 1217 | int target_index = GetIndexForOffset(increment); |
| 1218 | int non_skippable_entries = 0; |
| 1219 | for (int index = target_index; index >= 0 && index < GetEntryCount(); |
| 1220 | index += increment) { |
| 1221 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1222 | non_skippable_entries++; |
| 1223 | |
| 1224 | if (non_skippable_entries == std::abs(offset)) { |
| 1225 | target_index = index; |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | GoToIndex(target_index); |
| 1231 | } |
| 1232 | #endif |
| 1233 | |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1234 | bool NavigationControllerImpl::RemoveEntryAtIndex(int index) { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1235 | if (index == last_committed_entry_index_ || index == pending_entry_index_) |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1236 | return false; |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 1237 | |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 1238 | RemoveEntryAtIndexInternal(index); |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1239 | return true; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1240 | } |
| 1241 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1242 | void NavigationControllerImpl::PruneForwardEntries() { |
| 1243 | DiscardNonCommittedEntries(); |
| 1244 | int remove_start_index = last_committed_entry_index_ + 1; |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1245 | int num_removed = static_cast<int>(entries_.size()) - remove_start_index; |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1246 | if (num_removed <= 0) |
| 1247 | return; |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 1248 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1249 | entries_.erase(entries_.begin() + remove_start_index, entries_.end()); |
| 1250 | NotifyPrunedEntries(this, remove_start_index /* start index */, |
| 1251 | num_removed /* count */); |
| 1252 | } |
| 1253 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1254 | void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry, |
| 1255 | const GURL& new_url) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1256 | GURL new_virtual_url(new_url); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 1257 | if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite( |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 1258 | &new_virtual_url, entry->GetVirtualURL(), browser_context_)) { |
| 1259 | entry->SetVirtualURL(new_virtual_url); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1263 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL( |
| 1264 | const GURL& url, |
| 1265 | const Referrer& referrer, |
| 1266 | ui::PageTransition transition, |
| 1267 | const std::string& extra_headers) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1268 | LoadURLParams params(url); |
| 1269 | params.referrer = referrer; |
| 1270 | params.transition_type = transition; |
| 1271 | params.extra_headers = extra_headers; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1272 | return LoadURLWithParams(params); |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1273 | } |
| 1274 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1275 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams( |
| 1276 | const LoadURLParams& params) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1277 | if (params.is_renderer_initiated) |
| 1278 | DCHECK(params.initiator_origin.has_value()); |
| 1279 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 1280 | TRACE_EVENT1("browser,navigation", |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1281 | "NavigationControllerImpl::LoadURLWithParams", "url", |
| 1282 | params.url.possibly_invalid_spec()); |
Ian Vollick | 9dda052 | 2019-09-11 02:24:29 | [diff] [blame] | 1283 | bool is_explicit_navigation = |
| 1284 | GetContentClient()->browser()->IsExplicitNavigation( |
| 1285 | params.transition_type); |
| 1286 | if (HandleDebugURL(params.url, params.transition_type, |
| 1287 | is_explicit_navigation)) { |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1288 | // If Telemetry is running, allow the URL load to proceed as if it's |
| 1289 | // unhandled, otherwise Telemetry can't tell if Navigation completed. |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 1290 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1291 | cc::switches::kEnableGpuBenchmarking)) |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1292 | return nullptr; |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1293 | } |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 1294 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1295 | // Checks based on params.load_type. |
| 1296 | switch (params.load_type) { |
| 1297 | case LOAD_TYPE_DEFAULT: |
lukasza | 477a5a2 | 2016-06-16 18:28:43 | [diff] [blame] | 1298 | case LOAD_TYPE_HTTP_POST: |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1299 | break; |
| 1300 | case LOAD_TYPE_DATA: |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 1301 | if (!params.url.SchemeIs(url::kDataScheme)) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1302 | NOTREACHED() << "Data load must use data scheme."; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1303 | return nullptr; |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1304 | } |
| 1305 | break; |
Lukasz Anforowicz | bb0cfd5e | 2017-12-14 22:39:46 | [diff] [blame] | 1306 | } |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1307 | |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1308 | // The user initiated a load, we don't need to reload anymore. |
| 1309 | needs_reload_ = false; |
| 1310 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1311 | return NavigateWithoutEntry(params); |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1312 | } |
| 1313 | |
Charlie Reis | 4c53a96 | 2023-06-21 23:17:53 | [diff] [blame] | 1314 | void NavigationControllerImpl::LoadOriginalRequestURL() { |
| 1315 | // If the original request URL is not valid, matches the current URL, or |
| 1316 | // involves POST data, then simply reload. The POST check avoids issues with |
| 1317 | // sending data to the wrong page. |
| 1318 | const GURL& last_committed_url = GetLastCommittedEntry()->GetURL(); |
| 1319 | const GURL& original_request_url = |
| 1320 | GetLastCommittedEntry()->GetOriginalRequestURL(); |
| 1321 | if (!original_request_url.is_valid() || |
| 1322 | original_request_url == last_committed_url || |
| 1323 | GetLastCommittedEntry()->GetHasPostData()) { |
| 1324 | Reload(ReloadType::NORMAL, true); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | // Otherwise, attempt to load the original request URL without any of the |
| 1329 | // other data from the current NavigationEntry, replacing the current entry. |
| 1330 | // Loading the original URL is useful in cases such as modifying the user |
| 1331 | // agent. |
| 1332 | std::unique_ptr<NavigationController::LoadURLParams> load_params = |
| 1333 | std::make_unique<NavigationController::LoadURLParams>( |
| 1334 | original_request_url); |
| 1335 | load_params->should_replace_current_entry = true; |
| 1336 | load_params->transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 1337 | LoadURLWithParams(*load_params.get()); |
| 1338 | } |
| 1339 | |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1340 | bool NavigationControllerImpl::PendingEntryMatchesRequest( |
| 1341 | NavigationRequest* request) const { |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1342 | return pending_entry_ && |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1343 | pending_entry_->GetUniqueID() == request->nav_entry_id(); |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1344 | } |
| 1345 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1346 | bool NavigationControllerImpl::RendererDidNavigate( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1347 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1348 | const mojom::DidCommitProvisionalLoadParams& params, |
peary2 | 1b0f797b | 2016-09-28 17:28:33 | [diff] [blame] | 1349 | LoadCommittedDetails* details, |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1350 | bool is_same_document_navigation, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1351 | bool was_on_initial_empty_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1352 | bool previous_document_was_activated, |
Camille Lamy | 10aafcd3 | 2018-12-05 15:48:13 | [diff] [blame] | 1353 | NavigationRequest* navigation_request) { |
| 1354 | DCHECK(navigation_request); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1355 | |
| 1356 | // Note: validation checks and renderer kills due to invalid commit messages |
| 1357 | // must happen before getting here, in |
| 1358 | // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some |
| 1359 | // effects of the navigation have already occurred. |
| 1360 | |
[email protected] | cd2e1574 | 2013-03-08 04:08:31 | [diff] [blame] | 1361 | is_initial_navigation_ = false; |
| 1362 | |
Wang Hui | 96ab101 | 2022-10-11 02:05:49 | [diff] [blame] | 1363 | // Any pending request to repost a form submission is no longer valid, since a |
| 1364 | // different NavigationEntry is committing. |
| 1365 | pending_reload_ = ReloadType::NONE; |
| 1366 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1367 | // Save the previous state before we clobber it. |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1368 | bool overriding_user_agent_changed = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1369 | if (entry_replaced_by_post_commit_error_) { |
| 1370 | // Same document navigation events with a post-commit error should already |
| 1371 | // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before |
| 1372 | // reaching here. |
| 1373 | CHECK(!is_same_document_navigation); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1374 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1375 | // Any commit while a post-commit error page is showing should put the |
| 1376 | // original entry back, replacing the error page's entry. This includes |
| 1377 | // reloads, where the original entry was used as the pending entry and |
| 1378 | // should now be at the correct index at commit time. |
| 1379 | entries_[last_committed_entry_index_] = |
| 1380 | std::move(entry_replaced_by_post_commit_error_); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1381 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1382 | details->previous_main_frame_url = GetLastCommittedEntry()->GetURL(); |
| 1383 | details->previous_entry_index = GetLastCommittedEntryIndex(); |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1384 | // Must honor user agent overrides in the |navigation_request|, such as |
| 1385 | // from things like RequestDesktopSiteWebContentsObserverAndroid. As a |
| 1386 | // result, besides comparing |pending_entry_|'s user agent against |
| 1387 | // LastCommittedEntry's, also need to compare |navigation_request|'s user |
| 1388 | // agent against LastCommittedEntry's. |
| 1389 | if (navigation_request->is_overriding_user_agent() != |
Shu Yang | 127e41a | 2023-12-19 01:26:54 | [diff] [blame^] | 1390 | GetLastCommittedEntry()->GetIsOverridingUserAgent() || |
| 1391 | (PendingEntryMatchesRequest(navigation_request) && |
| 1392 | pending_entry_->GetIsOverridingUserAgent() != |
| 1393 | GetLastCommittedEntry()->GetIsOverridingUserAgent())) { |
Shu Yang | 7a3ec53 | 2023-06-21 17:49:00 | [diff] [blame] | 1394 | overriding_user_agent_changed = true; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1395 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1396 | |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1397 | bool is_main_frame_navigation = !rfh->GetParent(); |
| 1398 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1399 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1400 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1401 | // For primary frame tree navigations, choose an appropriate |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1402 | // BackForwardCacheMetrics to be associated with the new navigation's |
| 1403 | // NavigationEntry, by either creating a new object or reusing the previous |
| 1404 | // entry's one. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1405 | scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics; |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 1406 | if (navigation_request->frame_tree_node()->frame_tree().is_primary()) { |
Rakina Zata Amni | 63b5e809 | 2022-05-20 11:25:14 | [diff] [blame] | 1407 | back_forward_cache_metrics = BackForwardCacheMetrics:: |
| 1408 | CreateOrReuseBackForwardCacheMetricsForNavigation( |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1409 | GetLastCommittedEntry(), is_main_frame_navigation, |
| 1410 | params.document_sequence_number); |
| 1411 | } |
Yuzu Saijo | 29f96ca9 | 2022-12-08 04:54:12 | [diff] [blame] | 1412 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1413 | // Notify the last active entry that we have navigated away. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1414 | if (is_main_frame_navigation && !is_same_document_navigation) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1415 | if (auto* metrics = GetLastCommittedEntry()->back_forward_cache_metrics()) { |
| 1416 | metrics->MainFrameDidNavigateAwayFromDocument(); |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1420 | // Use CommonNavigationParam's `should_replace_current_entry` to determine |
| 1421 | // whether the current NavigationEntry should be replaced. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1422 | // (See below for a case where we might override that.) |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1423 | details->did_replace_entry = |
| 1424 | navigation_request->common_params().should_replace_current_entry; |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1425 | |
fdegans | 9caf66a | 2015-07-30 21:10:42 | [diff] [blame] | 1426 | // 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] | 1427 | // except for restored entries. This should be true even if the current commit |
| 1428 | // is not related to the pending entry. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1429 | bool was_restored = false; |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1430 | DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() || |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 1431 | pending_entry_->IsRestored()); |
Charlie Reis | c4155af | 2022-10-19 15:33:11 | [diff] [blame] | 1432 | |
| 1433 | // Only make changes based on the pending entry if the NavigationRequest |
| 1434 | // matches it. Otherwise, the pending entry may be for a different request |
| 1435 | // (e.g., if a slow history navigation is pending while an auto-subframe |
| 1436 | // commit occurs). |
| 1437 | if (PendingEntryMatchesRequest(navigation_request)) { |
| 1438 | // It is no longer necessary to consider the pending entry as restored. |
| 1439 | if (pending_entry_->IsRestored()) { |
| 1440 | pending_entry_->set_restore_type(RestoreType::kNotRestored); |
| 1441 | was_restored = true; |
| 1442 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1443 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1444 | // If the SiteInstance has changed from the matching pending entry, this |
| 1445 | // must be treated as a new navigation with replacement. Set the replacement |
| 1446 | // bit here and ClassifyNavigation will identify this case and return |
| 1447 | // NEW_ENTRY. |
| 1448 | if (!rfh->GetParent() && pending_entry_->site_instance() && |
| 1449 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
| 1450 | DCHECK_NE(-1, pending_entry_index_); |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1451 | // TODO(nasko,creis,rakina): Move this to happen before committing the |
| 1452 | // navigation. This is a bit complicated because we don't currently |
| 1453 | // set `should_replace_current_entry` for reload/history navigations. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1454 | details->did_replace_entry = true; |
| 1455 | } |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1456 | } |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 1457 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1458 | // Do navigation-type specific actions. These will make and commit an entry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1459 | NavigationType navigation_type = |
| 1460 | ClassifyNavigation(rfh, params, navigation_request); |
| 1461 | navigation_request->set_navigation_type(navigation_type); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1462 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1463 | if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node())) { |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1464 | // Ensure that this navigation does not add a navigation entry, since |
| 1465 | // ShouldMaintainTrivialSessionHistory() means we should not add an entry |
| 1466 | // beyond the last committed one. Therefore, `should_replace_current_entry` |
| 1467 | // should be set, which replaces the current entry, or this should be a |
| 1468 | // reload, which does not create a new entry. |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1469 | DCHECK(navigation_request->common_params().should_replace_current_entry || |
Dominic Farolino | e0f8d7c | 2023-08-16 15:38:33 | [diff] [blame] | 1470 | navigation_request->GetReloadType() != ReloadType::NONE); |
shivanigithub | 189833f | 2022-04-27 18:08:45 | [diff] [blame] | 1471 | } |
| 1472 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1473 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1474 | if (rfh->GetParent()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1475 | // This is a subframe navigation on the initial empty document, which used |
| 1476 | // to not have a NavigationEntry to attach to. Now it can attach to the |
| 1477 | // initial NavigationEntry, and we must ensure that its NavigationEntry |
| 1478 | // will keep the "initial NavigationEntry" status and won't append a new |
| 1479 | // NavigationEntry (it should always do replacement instead). |
| 1480 | // See also https://crbug.com/1277414. |
| 1481 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1482 | // Subframe navigation on initial NavigationEntry must not append a new |
| 1483 | // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This |
| 1484 | // means every subframe navigation that happens while we're on the initial |
| 1485 | // NavigationEntry will always reuse the existing NavigationEntry and |
| 1486 | // just update the corresponding FrameNavigationEntry. |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1487 | DCHECK_EQ(navigation_type, NAVIGATION_TYPE_AUTO_SUBFRAME); |
| 1488 | } else if (navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1489 | // This is a navigation that modifies the initial NavigationEntry, either |
| 1490 | // for a replacement or a reload. The initial NavigationEntry should |
| 1491 | // retain its "initial NavigationEntry" status in this case. |
| 1492 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1493 | } else if (navigation_request->is_synchronous_renderer_commit() && |
Rakina Zata Amni | fd8370b | 2022-11-14 13:32:25 | [diff] [blame] | 1494 | !navigation_request->IsSameDocument() && !rfh->GetParent()) { |
| 1495 | DCHECK(navigation_request->common_params().should_replace_current_entry); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1496 | // This is a synchronous about:blank navigation on the main frame, which |
| 1497 | // used to not create a NavigationEntry when we have no NavigationEntry on |
| 1498 | // FrameTree creation. We now have the initial NavigationEntry and are on |
| 1499 | // the initial NavigationEntry. To preserve old behavior, we should still |
| 1500 | // keep the "initial" status for the new NavigationEntry that we will |
| 1501 | // create for this navigation, so that subframe navigations under the |
| 1502 | // synchronously committed about:blank document will never append new |
| 1503 | // NavigationEntry, and instead will just reuse the initial |
| 1504 | // NavigationEntry and modify the corresponding FrameNavigationEntries. |
| 1505 | // See also https://crbug.com/1277414. |
| 1506 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | DCHECK(!details->should_stay_as_initial_entry || |
| 1510 | GetLastCommittedEntry()->IsInitialEntry()); |
[email protected] | 4bf3522c | 2010-08-19 21:00:20 | [diff] [blame] | 1511 | |
eugenebut | ee08663a | 2017-04-27 17:43:12 | [diff] [blame] | 1512 | // is_same_document must be computed before the entry gets committed. |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1513 | details->is_same_document = is_same_document_navigation; |
[email protected] | b9d4dfdc | 2013-08-08 00:25:12 | [diff] [blame] | 1514 | |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1515 | details->is_prerender_activation = |
| 1516 | navigation_request->IsPrerenderedPageActivation(); |
Robert Lin | 540dbd1 | 2022-04-28 22:07:24 | [diff] [blame] | 1517 | details->is_in_active_page = navigation_request->GetRenderFrameHost() |
| 1518 | ->GetOutermostMainFrame() |
| 1519 | ->IsInPrimaryMainFrame(); |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1520 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1521 | // Make sure we do not discard the pending entry for a different ongoing |
| 1522 | // navigation when a same document commit comes in unexpectedly from the |
| 1523 | // renderer. Limit this to a very narrow set of conditions to avoid risks to |
| 1524 | // other navigation types. See https://crbug.com/900036. |
| 1525 | // TODO(crbug.com/926009): Handle history.pushState() as well. |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1526 | bool keep_pending_entry = |
| 1527 | is_same_document_navigation && |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1528 | navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY && |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1529 | pending_entry_ && !PendingEntryMatchesRequest(navigation_request); |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1530 | |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1531 | switch (navigation_type) { |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1532 | case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1533 | RendererDidNavigateToNewEntry( |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 1534 | rfh, params, details->is_same_document, details->did_replace_entry, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1535 | previous_document_was_activated, navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1536 | break; |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1537 | case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1538 | RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document, |
| 1539 | was_restored, navigation_request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1540 | keep_pending_entry, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1541 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1542 | case NAVIGATION_TYPE_NEW_SUBFRAME: |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1543 | RendererDidNavigateNewSubframe( |
| 1544 | rfh, params, details->is_same_document, details->did_replace_entry, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1545 | previous_document_was_activated, navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1546 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1547 | case NAVIGATION_TYPE_AUTO_SUBFRAME: |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1548 | if (!RendererDidNavigateAutoSubframe( |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1549 | rfh, params, details->is_same_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1550 | was_on_initial_empty_document, navigation_request, details)) { |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1551 | // We don't send a notification about auto-subframe PageState during |
| 1552 | // UpdateStateForFrame, since it looks like nothing has changed. Send |
| 1553 | // it here at commit time instead. |
| 1554 | NotifyEntryChanged(GetLastCommittedEntry()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1555 | return false; |
creis | 59d5a47cb | 2016-08-24 23:57:19 | [diff] [blame] | 1556 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1557 | break; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1558 | case NAVIGATION_TYPE_UNKNOWN: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1559 | NOTREACHED(); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1560 | break; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1561 | } |
| 1562 | |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1563 | // At this point, we know that the navigation has just completed, so |
| 1564 | // record the time. |
| 1565 | // |
| 1566 | // TODO(akalin): Use "sane time" as described in |
Adam Langley | 4463fb83 | 2018-01-28 22:42:26 | [diff] [blame] | 1567 | // https://www.chromium.org/developers/design-documents/sane-time . |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 1568 | base::Time timestamp = |
| 1569 | time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run()); |
| 1570 | DVLOG(1) << "Navigation finished at (smoothed) timestamp " |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 1571 | << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1572 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1573 | // If we aren't keeping the pending entry, there shouldn't be one at this |
| 1574 | // point. Clear it again in case any error cases above forgot to do so. |
| 1575 | // TODO(pbos): Consider a CHECK here that verifies that the pending entry has |
| 1576 | // been cleared instead of protecting against it. |
| 1577 | if (!keep_pending_entry) |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1578 | DiscardNonCommittedEntriesWithCommitDetails(details); |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 1579 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1580 | // All committed entries should have nonempty content state so WebKit doesn't |
| 1581 | // get confused when we go back to them (see the function for details). |
creis | 0cade2e | 2017-02-28 06:37:47 | [diff] [blame] | 1582 | DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState."; |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1583 | NavigationEntryImpl* active_entry = GetLastCommittedEntry(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1584 | active_entry->SetTimestamp(timestamp); |
[email protected] | f49737b3 | 2013-08-28 07:51:44 | [diff] [blame] | 1585 | active_entry->SetHttpStatusCode(params.http_status_code); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1586 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1587 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1588 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1589 | if (back_forward_cache_metrics && |
| 1590 | !active_entry->back_forward_cache_metrics()) { |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1591 | active_entry->set_back_forward_cache_metrics( |
| 1592 | std::move(back_forward_cache_metrics)); |
| 1593 | } |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1594 | |
| 1595 | // `back_forward_cache_metrics()` may return null as we do not record |
| 1596 | // back-forward cache metrics for navigations in non-primary frame trees. |
| 1597 | if (active_entry->back_forward_cache_metrics()) { |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1598 | // TODO(https://crbug.com/1338089): Remove this. |
| 1599 | // These are both only available from details at this point, so we capture |
| 1600 | // them here. |
| 1601 | SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type", |
Miyoung Shin | 3299cbf | 2022-11-22 01:41:10 | [diff] [blame] | 1602 | navigation_type); |
Fergal Daly | 0686c0e | 2022-06-28 02:08:14 | [diff] [blame] | 1603 | SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace", |
| 1604 | details->did_replace_entry); |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1605 | active_entry->back_forward_cache_metrics()->DidCommitNavigation( |
| 1606 | navigation_request, |
| 1607 | back_forward_cache_.IsAllowed(navigation_request->GetURL())); |
| 1608 | } |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1609 | |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1610 | // Grab the corresponding FrameNavigationEntry for a few updates, but only if |
| 1611 | // the SiteInstance matches (to avoid updating the wrong entry by mistake). |
| 1612 | // A mismatch can occur if the renderer lies or due to a unique name collision |
| 1613 | // after a race with an OOPIF (see https://crbug.com/616820). |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1614 | FrameNavigationEntry* frame_entry = |
| 1615 | active_entry->GetFrameEntry(rfh->frame_tree_node()); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1616 | if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance()) |
| 1617 | frame_entry = nullptr; |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1618 | // Make sure we've updated the PageState in one of the helper methods. |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1619 | // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed. |
| 1620 | if (frame_entry) { |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1621 | DCHECK(params.page_state == frame_entry->page_state()); |
Nasko Oskov | bbcfc000 | 2019-11-20 20:03:20 | [diff] [blame] | 1622 | |
| 1623 | // Remember the bindings the renderer process has at this point, so that |
| 1624 | // we do not grant this entry additional bindings if we come back to it. |
| 1625 | frame_entry->SetBindings(rfh->GetEnabledBindings()); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 1626 | } |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1627 | |
[email protected] | 97d8f0d | 2013-10-29 16:49:21 | [diff] [blame] | 1628 | // Once it is committed, we no longer need to track several pieces of state on |
| 1629 | // the entry. |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1630 | active_entry->ResetForCommit(frame_entry); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1631 | |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1632 | // The active entry's SiteInstance should match our SiteInstance. |
[email protected] | a1b9926 | 2013-12-27 21:56:22 | [diff] [blame] | 1633 | // TODO(creis): This check won't pass for subframes until we create entries |
| 1634 | // for subframe navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1635 | if (!rfh->GetParent()) |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1636 | CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance()); |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1637 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1638 | // Now prep the rest of the details for the notification and broadcast. |
[email protected] | 0f38dc455 | 2011-02-25 11:24:00 | [diff] [blame] | 1639 | details->entry = active_entry; |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1640 | details->is_main_frame = !rfh->GetParent(); |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame] | 1641 | details->http_status_code = params.http_status_code; |
estark | a5635c4 | 2015-07-14 00:06:53 | [diff] [blame] | 1642 | |
arthursonzogni | 7ddc654 | 2021-04-09 09:16:50 | [diff] [blame] | 1643 | active_entry->SetIsOverridingUserAgent( |
| 1644 | navigation_request->is_overriding_user_agent()); |
Scott Violet | c36f746 | 2020-05-06 23:13:03 | [diff] [blame] | 1645 | |
[email protected] | 93f230e0 | 2011-06-01 14:40:00 | [diff] [blame] | 1646 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1647 | |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1648 | if (overriding_user_agent_changed) |
| 1649 | delegate_->UpdateOverridingUserAgent(); |
| 1650 | |
creis | 03b4800 | 2015-11-04 00:54:56 | [diff] [blame] | 1651 | // Update the nav_entry_id for each RenderFrameHost in the tree, so that each |
| 1652 | // one knows the latest NavigationEntry it is showing (whether it has |
| 1653 | // committed anything in this navigation or not). This allows things like |
| 1654 | // state and title updates from RenderFrames to apply to the latest relevant |
| 1655 | // NavigationEntry. |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1656 | int nav_entry_id = active_entry->GetUniqueID(); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1657 | for (FrameTreeNode* node : frame_tree_->Nodes()) |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1658 | node->current_frame_host()->set_nav_entry_id(nav_entry_id); |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1659 | |
| 1660 | if (navigation_request->IsPrerenderedPageActivation()) { |
| 1661 | BroadcastHistoryOffsetAndLength(); |
| 1662 | // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page |
| 1663 | // is activated. As a result, a "prerenderingchange" event listener sees the |
| 1664 | // history.length which is not updated yet. We should guarantee that |
| 1665 | // history's length and offset should be updated before a |
| 1666 | // "prerenderingchange" event listener runs. One possible approach is to use |
| 1667 | // the same IPC which "prerenderingchange" uses, and propagate history's |
| 1668 | // length and offset together with that. |
| 1669 | } |
| 1670 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1671 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1672 | } |
| 1673 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1674 | NavigationType NavigationControllerImpl::ClassifyNavigation( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1675 | RenderFrameHostImpl* rfh, |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1676 | const mojom::DidCommitProvisionalLoadParams& params, |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1677 | NavigationRequest* navigation_request) { |
Piotr Tworek | bad5128 | 2020-09-30 19:17:59 | [diff] [blame] | 1678 | TraceReturnReason<tracing_category::kNavigation> trace_return( |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1679 | "ClassifyNavigation"); |
| 1680 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1681 | if (params.did_create_new_entry) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1682 | // A new entry. We may or may not have a corresponding pending entry, and |
| 1683 | // this may or may not be the main frame. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1684 | if (!rfh->GetParent()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1685 | trace_return.set_return_reason("new entry, no parent, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1686 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1687 | } |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1688 | // Valid subframe navigation. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1689 | trace_return.set_return_reason("new entry, new subframe"); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1690 | return NAVIGATION_TYPE_NEW_SUBFRAME; |
| 1691 | } |
| 1692 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1693 | // We only clear the session history in tests when navigating to a new entry. |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1694 | DCHECK(!params.history_list_was_cleared); |
| 1695 | |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1696 | if (rfh->GetParent()) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1697 | // All manual subframes would be did_create_new_entry and handled above, so |
| 1698 | // we know this is auto. |
Rakina Zata Amni | acd4df66 | 2022-11-15 06:49:08 | [diff] [blame] | 1699 | trace_return.set_return_reason("subframe, last commmited, auto subframe"); |
| 1700 | return NAVIGATION_TYPE_AUTO_SUBFRAME; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1701 | } |
| 1702 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1703 | const int nav_entry_id = navigation_request->commit_params().nav_entry_id; |
| 1704 | if (nav_entry_id == 0) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1705 | // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't |
| 1706 | // create a new page. |
| 1707 | |
Hayato Ito | 2ae4944 | 2021-07-02 02:59:25 | [diff] [blame] | 1708 | // This main frame navigation is not a history navigation (since |
| 1709 | // nav_entry_id is 0), but didn't create a new entry. So this must be a |
| 1710 | // reload or a replacement navigation, which will modify the existing entry. |
| 1711 | // |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1712 | // TODO(nasko): With error page isolation, reloading an existing session |
| 1713 | // history entry can result in change of SiteInstance. Check for such a case |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1714 | // here and classify it as NEW_ENTRY, as such navigations should be treated |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1715 | // as new with replacement. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1716 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1717 | "nav entry 0, last committed, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1718 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1719 | } |
| 1720 | |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 1721 | if (PendingEntryMatchesRequest(navigation_request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1722 | // If the SiteInstance of the |pending_entry_| does not match the |
| 1723 | // SiteInstance that got committed, treat this as a new navigation with |
| 1724 | // replacement. This can happen if back/forward/reload encounters a server |
| 1725 | // redirect to a different site or an isolated error page gets successfully |
| 1726 | // reloaded into a different SiteInstance. |
| 1727 | if (pending_entry_->site_instance() && |
| 1728 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1729 | trace_return.set_return_reason("pending matching nav entry, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1730 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1731 | } |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1732 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1733 | if (pending_entry_index_ == -1) { |
| 1734 | // In this case, we have a pending entry for a load of a new URL but Blink |
| 1735 | // didn't do a new navigation (params.did_create_new_entry). First check |
| 1736 | // to make sure Blink didn't treat a new cross-process navigation as |
| 1737 | // 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] | 1738 | // we must treat it as NEW rather than the converted reload case below, |
| 1739 | // since the new SiteInstance doesn't match the last committed entry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1740 | if (GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1741 | trace_return.set_return_reason("new pending, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1742 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | // Otherwise, this happens when you press enter in the URL bar to reload. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1746 | // We will create a pending entry, but NavigateWithoutEntry will convert |
| 1747 | // it to a reload since it's the same page and not create a new entry for |
| 1748 | // it. (The user doesn't want to have a new back/forward entry when they |
| 1749 | // do this.) Therefore we want to just ignore the pending entry and go |
| 1750 | // back to where we were (the "existing entry"). |
| 1751 | trace_return.set_return_reason("new pending, existing (same) entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1752 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1753 | } |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1754 | } |
| 1755 | |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 1756 | if (navigation_request->commit_params().intended_as_new_entry) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1757 | // 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] | 1758 | // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may |
| 1759 | // or may not have a pending entry. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1760 | trace_return.set_return_reason("intended as new entry, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1761 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1762 | } |
| 1763 | |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 1764 | if (navigation_request->DidEncounterError() && |
| 1765 | failed_pending_entry_id_ != 0 && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1766 | nav_entry_id == failed_pending_entry_id_) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1767 | // If the renderer was going to a new pending entry that got cleared because |
| 1768 | // 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] | 1769 | // by pressing return. Classify as EXISTING_ENTRY because we probably don't |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1770 | // have a pending entry. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1771 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1772 | "unreachable, matching pending, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1773 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1774 | } |
| 1775 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1776 | // 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] | 1777 | int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1778 | trace_return.traced_value()->SetInteger("existing_entry_index", |
| 1779 | existing_entry_index); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1780 | if (existing_entry_index == -1) { |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 1781 | // The renderer has committed a navigation to an entry that no longer |
| 1782 | // exists. Because the renderer is showing that page, resurrect that entry. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1783 | trace_return.set_return_reason("existing entry -1, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1784 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1785 | } |
| 1786 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1787 | // Since we weeded out "new" navigations above, we know this is an existing |
| 1788 | // (back/forward) navigation. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1789 | trace_return.set_return_reason("default return, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1790 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1791 | } |
| 1792 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1793 | void NavigationControllerImpl::UpdateNavigationEntryDetails( |
| 1794 | NavigationEntryImpl* entry, |
| 1795 | RenderFrameHostImpl* rfh, |
| 1796 | const mojom::DidCommitProvisionalLoadParams& params, |
| 1797 | NavigationRequest* request, |
| 1798 | NavigationEntryImpl::UpdatePolicy update_policy, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1799 | bool is_new_entry, |
| 1800 | LoadCommittedDetails* commit_details) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1801 | // Update the FrameNavigationEntry. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1802 | std::vector<GURL> redirects; |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1803 | entry->AddOrUpdateFrameEntry( |
| 1804 | rfh->frame_tree_node(), update_policy, params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1805 | params.document_sequence_number, params.navigation_api_key, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1806 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1807 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1808 | Referrer(*params.referrer), |
| 1809 | request ? request->common_params().initiator_origin : params.origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 1810 | request ? request->common_params().initiator_base_url : absl::nullopt, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1811 | request ? request->GetRedirectChain() : redirects, params.page_state, |
| 1812 | params.method, params.post_id, nullptr /* blob_url_loader_factory */, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1813 | ComputePolicyContainerPoliciesForFrameEntry( |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1814 | rfh, request && request->IsSameDocument(), |
| 1815 | request ? request->common_params().url : params.url)); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1816 | |
| 1817 | if (rfh->GetParent()) { |
| 1818 | // Only modify the NavigationEntry for main frame navigations. |
| 1819 | return; |
| 1820 | } |
| 1821 | if (entry->update_virtual_url_with_url()) |
| 1822 | UpdateVirtualURLToURL(entry, params.url); |
| 1823 | // Don't use the page type from the pending entry. Some interstitial page |
| 1824 | // may have set the type to interstitial. Once we commit, however, the page |
| 1825 | // type must always be normal or error. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1826 | entry->set_page_type((request && request->DidEncounterError()) |
| 1827 | ? PAGE_TYPE_ERROR |
| 1828 | : PAGE_TYPE_NORMAL); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1829 | if (commit_details && commit_details->should_stay_as_initial_entry) { |
| 1830 | // Retain the "initial NavigationEntry" status. |
| 1831 | if (request->IsSameDocument()) { |
| 1832 | // If this is for a same-document navigation, the NavigationEntry must be |
| 1833 | // reused and should already be marked as the initial NavigationEntry. |
| 1834 | DCHECK(entry->IsInitialEntry()); |
| 1835 | } else { |
| 1836 | // If this is for a cross-document navigation, it can be caused by a |
| 1837 | // renderer-initiated reload, or the synchronous about:blank commit. Mark |
| 1838 | // "for synchronous about:blank" in the latter case, and also when it is |
| 1839 | // reloading a "for synchronous about:blank" entry. Otherwise, the entry |
| 1840 | // is not for a synchronous about:blank commit. |
| 1841 | NavigationEntryImpl::InitialNavigationEntryState new_state = |
| 1842 | NavigationEntryImpl::InitialNavigationEntryState:: |
| 1843 | kInitialNotForSynchronousAboutBlank; |
| 1844 | if (entry->IsInitialEntryForSynchronousAboutBlank() || |
| 1845 | request->is_synchronous_renderer_commit()) { |
| 1846 | new_state = NavigationEntryImpl::InitialNavigationEntryState:: |
| 1847 | kInitialForSynchronousAboutBlank; |
| 1848 | } |
| 1849 | entry->set_initial_navigation_entry_state(new_state); |
| 1850 | } |
| 1851 | } else if (commit_details && !commit_details->should_stay_as_initial_entry) { |
| 1852 | // Remove the "initial NavigationEntry" status. |
| 1853 | entry->set_initial_navigation_entry_state( |
| 1854 | NavigationEntryImpl::InitialNavigationEntryState::kNonInitial); |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1855 | } |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1856 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1857 | if (is_new_entry) { |
| 1858 | // Some properties of the NavigationEntry are only set when the entry is |
| 1859 | // new (we aren't reusing it). |
| 1860 | entry->SetTransitionType(params.transition); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1861 | entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL() |
| 1862 | : GURL::EmptyGURL()); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1863 | DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent); |
| 1864 | entry->SetIsOverridingUserAgent(params.is_overriding_user_agent); |
| 1865 | } else { |
| 1866 | // We are reusing the NavigationEntry. The site instance will normally be |
| 1867 | // the same except for a few cases: |
| 1868 | // 1) session restore, when no site instance will be assigned or |
| 1869 | // 2) redirect, when the site instance is reset. |
| 1870 | DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() || |
| 1871 | entry->site_instance() == rfh->GetSiteInstance()); |
| 1872 | } |
| 1873 | } |
| 1874 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1875 | void NavigationControllerImpl::CreateInitialEntry() { |
| 1876 | DCHECK_EQ(entries_.size(), 0u); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1877 | RenderFrameHostImpl* rfh = frame_tree_->root()->current_frame_host(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1878 | auto params = mojom::DidCommitProvisionalLoadParams::New(); |
| 1879 | // The initial NavigationEntry's URL is the empty URL. This preserves the old |
| 1880 | // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from |
| 1881 | // before we have initial NavigationEntries. |
| 1882 | params->url = GURL::EmptyGURL(); |
| 1883 | params->http_status_code = 0; |
| 1884 | params->url_is_unreachable = false; |
| 1885 | params->method = "GET"; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1886 | params->post_id = -1; |
| 1887 | params->embedding_token = base::UnguessableToken::Create(); |
| 1888 | params->navigation_token = base::UnguessableToken::Create(); |
| 1889 | params->did_create_new_entry = true; |
| 1890 | params->origin = rfh->GetLastCommittedOrigin(); |
| 1891 | params->should_update_history = true; |
| 1892 | params->item_sequence_number = 0; |
| 1893 | params->document_sequence_number = 0; |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 1894 | bool is_in_fenced_frame_tree = rfh->IsNestedWithinFencedFrame(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1895 | params->transition = is_in_fenced_frame_tree |
| 1896 | ? ui::PAGE_TRANSITION_AUTO_SUBFRAME |
| 1897 | : ui::PAGE_TRANSITION_LINK; |
| 1898 | params->referrer = blink::mojom::Referrer::New(); |
| 1899 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1900 | auto new_entry = std::make_unique<NavigationEntryImpl>( |
| 1901 | rfh->GetSiteInstance(), params->url, Referrer(*params->referrer), |
W. James MacLean | 78e2f87 | 2023-01-24 17:59:38 | [diff] [blame] | 1902 | rfh->GetLastCommittedOrigin(), rfh->GetInheritedBaseUrl(), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 1903 | std::u16string() /* title */, ui::PAGE_TRANSITION_TYPED, |
| 1904 | false /* renderer_initiated */, nullptr /* blob_url_loader_factory */, |
| 1905 | true /* is_initial_entry */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1906 | UpdateNavigationEntryDetails( |
| 1907 | new_entry.get(), rfh, *params, nullptr /* request */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1908 | NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */, |
| 1909 | nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1910 | |
| 1911 | InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */, |
| 1912 | false /* was_post_commit_error */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1913 | is_in_fenced_frame_tree, nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1914 | } |
| 1915 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1916 | void NavigationControllerImpl::RendererDidNavigateToNewEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1917 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1918 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 1919 | bool is_same_document, |
clamy | 3bf35e3c | 2016-11-10 15:59:44 | [diff] [blame] | 1920 | bool replace_entry, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1921 | bool previous_document_was_activated, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1922 | NavigationRequest* request, |
| 1923 | LoadCommittedDetails* commit_details) { |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 1924 | std::unique_ptr<NavigationEntryImpl> new_entry; |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 1925 | const absl::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1926 | request->common_params().initiator_origin; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 1927 | absl::optional<GURL> initiator_base_url; |
| 1928 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 1929 | initiator_base_url = request->common_params().initiator_base_url; |
| 1930 | } |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1931 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1932 | // First check if this is an in-page navigation. If so, clone the current |
| 1933 | // entry instead of looking at the pending entry, because the pending entry |
| 1934 | // does not have any subframe history items. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 1935 | if (is_same_document) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1936 | FrameNavigationEntry* previous_frame_entry = |
| 1937 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 1938 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 1939 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1940 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 1941 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1942 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 1943 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
Rakina Zata Amni | 82fafba | 2021-03-11 07:07:09 | [diff] [blame] | 1944 | request->GetRedirectChain(), params.page_state, params.method, |
| 1945 | params.post_id, nullptr /* blob_url_loader_factory */, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1946 | // We will set the document policies later in this function. |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1947 | nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1948 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1949 | // FrameNavigationEntry. |
| 1950 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 1951 | previous_frame_entry->protect_url_in_navigation_api()); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1952 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1953 | new_entry = GetLastCommittedEntry()->CloneAndReplace( |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 1954 | frame_entry, true, request->frame_tree_node(), frame_tree_->root()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1955 | if (new_entry->GetURL().DeprecatedGetOriginAsURL() != |
| 1956 | params.url.DeprecatedGetOriginAsURL()) { |
jam | a78746e | 2017-02-22 17:21:57 | [diff] [blame] | 1957 | // TODO(jam): we had one report of this with a URL that was redirecting to |
| 1958 | // only tildes. Until we understand that better, don't copy the cert in |
| 1959 | // this case. |
| 1960 | new_entry->GetSSL() = SSLStatus(); |
| 1961 | } |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1962 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 1963 | // It is expected that |frame_entry| is now owned by |new_entry|. This means |
| 1964 | // that |frame_entry| should now have a reference count of exactly 2: one |
| 1965 | // due to the local variable |frame_entry|, and another due to |new_entry| |
| 1966 | // also retaining one. This should never fail, because it's the main frame. |
| 1967 | CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef()); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1968 | } |
| 1969 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 1970 | // If this is an activation navigation from a prerendered page, transfer the |
| 1971 | // new entry from an entry already created and stored in the |
| 1972 | // NavigationRequest. |new_entry| will not have been set prior to this as |
| 1973 | // |is_same_document| is mutually exclusive with |
| 1974 | // |IsPrerenderedPageActivation|. |
| 1975 | if (request->IsPrerenderedPageActivation()) { |
| 1976 | DCHECK(!is_same_document); |
| 1977 | DCHECK(!new_entry); |
| 1978 | new_entry = request->TakePrerenderNavigationEntry(); |
| 1979 | DCHECK(new_entry); |
| 1980 | } |
| 1981 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1982 | // Only make a copy of the pending entry if it is appropriate for the new |
| 1983 | // document that just loaded. Verify this by checking if the entry corresponds |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1984 | // to the given NavigationRequest. Additionally, coarsely check that: |
csharrison | 9a9142bc4 | 2016-03-01 17:24:04 | [diff] [blame] | 1985 | // 1. The SiteInstance hasn't been assigned to something else. |
| 1986 | // 2. The pending entry was intended as a new entry, rather than being a |
| 1987 | // history navigation that was interrupted by an unrelated, |
| 1988 | // renderer-initiated navigation. |
| 1989 | // TODO(csharrison): Investigate whether we can remove some of the coarser |
| 1990 | // checks. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1991 | if (!new_entry && PendingEntryMatchesRequest(request) && |
| 1992 | pending_entry_index_ == -1 && |
[email protected] | 6dd86ab | 2013-02-27 00:30:34 | [diff] [blame] | 1993 | (!pending_entry_->site_instance() || |
[email protected] | 27dd82fd | 2014-03-03 22:11:43 | [diff] [blame] | 1994 | pending_entry_->site_instance() == rfh->GetSiteInstance())) { |
creis | ef4a0cb | 2015-03-12 19:14:35 | [diff] [blame] | 1995 | new_entry = pending_entry_->Clone(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1996 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 1997 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1998 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1999 | } |
| 2000 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2001 | // For cross-document commits with no matching pending entry, create a new |
| 2002 | // entry. |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 2003 | if (!new_entry) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 2004 | new_entry = std::make_unique<NavigationEntryImpl>( |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2005 | rfh->GetSiteInstance(), params.url, Referrer(*params.referrer), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2006 | initiator_origin, initiator_base_url, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2007 | std::u16string(), // title |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2008 | params.transition, request->IsRendererInitiated(), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2009 | nullptr, // blob_url_loader_factory |
| 2010 | false); // is_initial_entry |
[email protected] | f8f93eb | 2012-09-25 03:06:24 | [diff] [blame] | 2011 | |
| 2012 | // Find out whether the new entry needs to update its virtual URL on URL |
| 2013 | // change and set up the entry accordingly. This is needed to correctly |
| 2014 | // update the virtual URL when replaceState is called after a pushState. |
| 2015 | GURL url = params.url; |
| 2016 | bool needs_update = false; |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2017 | // 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] | 2018 | // update the virtual URL based on the new URL. For example, this is needed |
| 2019 | // to show chrome://bookmarks/#1 when the bookmarks webui extension changes |
| 2020 | // the URL. |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2021 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 2022 | &url, browser_context_, &needs_update); |
| 2023 | new_entry->set_update_virtual_url_with_url(needs_update); |
| 2024 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2025 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2026 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2027 | } |
| 2028 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2029 | // TODO(crbug.com/1179428) - determine which parts of the entry need to be set |
| 2030 | // for prerendered contents, if any. This is because prerendering/activation |
| 2031 | // technically won't be creating a new document. Unlike BFCache, prerendering |
| 2032 | // creates a new NavigationEntry rather than using an existing one. |
| 2033 | if (!request->IsPrerenderedPageActivation()) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2034 | UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request, |
| 2035 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2036 | true /* is_new_entry */, commit_details); |
creis | 8b5cd4c | 2015-06-19 00:11:08 | [diff] [blame] | 2037 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2038 | // history.pushState() is classified as a navigation to a new page, but sets |
| 2039 | // is_same_document to true. In this case, we already have the title and |
| 2040 | // favicon available, so set them immediately. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2041 | if (is_same_document) { |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 2042 | new_entry->SetTitle(GetLastCommittedEntry()->GetTitle()); |
| 2043 | new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon(); |
| 2044 | } |
[email protected] | 3a868f21 | 2014-08-09 10:41:19 | [diff] [blame] | 2045 | } |
[email protected] | ff64b3e | 2014-05-31 04:07:33 | [diff] [blame] | 2046 | |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2047 | DCHECK(!params.history_list_was_cleared || !replace_entry); |
| 2048 | // The browser requested to clear the session history when it initiated the |
| 2049 | // navigation. Now we know that the renderer has updated its state accordingly |
| 2050 | // and it is safe to also clear the browser side history. |
| 2051 | if (params.history_list_was_cleared) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2052 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 2053 | entries_.clear(); |
| 2054 | last_committed_entry_index_ = -1; |
| 2055 | } |
| 2056 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2057 | // If this is a new navigation with replacement and there is a |
| 2058 | // pending_entry_ which matches the navigation reported by the renderer |
| 2059 | // process, then it should be the one replaced, so update the |
| 2060 | // last_committed_entry_index_ to use it. |
| 2061 | if (replace_entry && pending_entry_index_ != -1 && |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2062 | PendingEntryMatchesRequest(request)) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 2063 | last_committed_entry_index_ = pending_entry_index_; |
| 2064 | } |
| 2065 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2066 | SetShouldSkipOnBackForwardUIIfNeeded( |
shivanigithub | e92c33da | 2020-09-14 13:01:41 | [diff] [blame] | 2067 | replace_entry, previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2068 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 2069 | |
Yuzu Saijo | a725585f | 2022-11-28 04:14:03 | [diff] [blame] | 2070 | // If this is a history navigation and the old entry has an existing |
| 2071 | // back/forward cache metrics object, keep using the old one so that the |
| 2072 | // reasons logged from the last time the page navigated gets preserved. |
| 2073 | if (BackForwardCacheMetrics::IsCrossDocumentMainFrameHistoryNavigation( |
| 2074 | request)) { |
| 2075 | // Use |request->GetNavigationEntry()| instead of |pending_entry_| here |
| 2076 | // because some tests do not have a pending entry. |
| 2077 | NavigationEntryImpl* entry = |
| 2078 | static_cast<NavigationEntryImpl*>(request->GetNavigationEntry()); |
| 2079 | if (entry && entry->back_forward_cache_metrics()) { |
| 2080 | scoped_refptr<BackForwardCacheMetrics> metrics = |
| 2081 | entry->TakeBackForwardCacheMetrics(); |
| 2082 | new_entry->set_back_forward_cache_metrics(std::move(metrics)); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2086 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2087 | !request->post_commit_error_page_html().empty(), |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2088 | rfh->IsNestedWithinFencedFrame(), commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2089 | } |
| 2090 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 2091 | void NavigationControllerImpl::RendererDidNavigateToExistingEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2092 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2093 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2094 | bool is_same_document, |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2095 | bool was_restored, |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2096 | NavigationRequest* request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2097 | bool keep_pending_entry, |
| 2098 | LoadCommittedDetails* commit_details) { |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 2099 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2100 | << "that a last committed entry exists."; |
| 2101 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2102 | // We should only get here for main frame navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 2103 | DCHECK(!rfh->GetParent()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2104 | |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2105 | NavigationEntryImpl* entry = nullptr; |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 2106 | if (request->commit_params().intended_as_new_entry) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2107 | // We're guaranteed to have a last committed entry if intended_as_new_entry |
| 2108 | // is true. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2109 | entry = GetLastCommittedEntry(); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2110 | |
| 2111 | // If the NavigationRequest matches a new pending entry and is classified as |
| 2112 | // EXISTING_ENTRY, then it is a navigation to the same URL that was |
| 2113 | // converted to a reload, such as a user pressing enter in the omnibox. |
Charlie Reis | f8cde71 | 2022-10-20 16:25:09 | [diff] [blame] | 2114 | if (pending_entry_index_ == -1 && PendingEntryMatchesRequest(request)) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2115 | // Note: The pending entry will usually have a real ReloadType here, but |
| 2116 | // it can still be ReloadType::NONE in cases that |
| 2117 | // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source). |
| 2118 | |
| 2119 | // If we classified this correctly, the SiteInstance should not have |
| 2120 | // changed. |
| 2121 | CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance()); |
| 2122 | |
| 2123 | // For converted reloads, we assign the entry's unique ID to be that of |
| 2124 | // the new one. Since this is always the result of a user action, we want |
| 2125 | // to dismiss infobars, etc. like a regular user-initiated navigation. |
| 2126 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
| 2127 | |
| 2128 | // The extra headers may have changed due to reloading with different |
| 2129 | // headers. |
| 2130 | entry->set_extra_headers(pending_entry_->extra_headers()); |
| 2131 | } |
| 2132 | // Otherwise, this was intended as a new entry but the pending entry was |
| 2133 | // lost in the meantime and no new entry was created. We are stuck at the |
| 2134 | // last committed entry. |
| 2135 | |
| 2136 | // Even if this is a converted reload from pressing enter in the omnibox, |
| 2137 | // the server could redirect, requiring an update to the SSL status. If this |
| 2138 | // is a same document navigation, though, there's no SSLStatus in the |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2139 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2140 | if (!is_same_document) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2141 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2142 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2143 | } |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2144 | } else if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2145 | // This is a browser-initiated navigation (back/forward/reload). |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2146 | entry = GetEntryWithUniqueID(nav_entry_id); |
jam | d208b90 | 2016-09-01 16:58:16 | [diff] [blame] | 2147 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2148 | if (is_same_document) { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2149 | // There's no SSLStatus in the NavigationRequest for same document |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2150 | // navigations, so normally we leave |entry|'s SSLStatus as is. However if |
| 2151 | // this was a restored same document navigation entry, then it won't have |
| 2152 | // an SSLStatus. So we need to copy over the SSLStatus from the entry that |
| 2153 | // navigated it. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2154 | NavigationEntryImpl* last_entry = GetLastCommittedEntry(); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2155 | if (entry->GetURL().DeprecatedGetOriginAsURL() == |
| 2156 | last_entry->GetURL().DeprecatedGetOriginAsURL() && |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2157 | last_entry->GetSSL().initialized && !entry->GetSSL().initialized && |
| 2158 | was_restored) { |
| 2159 | entry->GetSSL() = last_entry->GetSSL(); |
| 2160 | } |
| 2161 | } else { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2162 | // In rapid back/forward navigations |request| sometimes won't have a cert |
| 2163 | // (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] | 2164 | // otherwise we only reuse the existing cert if the origins match. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2165 | if (request->GetSSLInfo().has_value() && |
| 2166 | request->GetSSLInfo()->is_valid()) { |
| 2167 | entry->GetSSL() = SSLStatus(*(request->GetSSLInfo())); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2168 | } else if (entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2169 | request->GetURL().DeprecatedGetOriginAsURL()) { |
John Abd-El-Malek | 3f24708 | 2017-12-07 19:02:19 | [diff] [blame] | 2170 | entry->GetSSL() = SSLStatus(); |
| 2171 | } |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2172 | } |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2173 | } else { |
Feifei Wang | 2ab8ba6c | 2022-04-13 22:19:27 | [diff] [blame] | 2174 | // This is renderer-initiated. The only kinds of renderer-initiated |
Rakina Zata Amni | 557afb9 | 2021-07-17 04:39:57 | [diff] [blame] | 2175 | // navigations that are EXISTING_ENTRY are same-document navigations that |
| 2176 | // result in replacement (e.g. history.replaceState(), location.replace(), |
| 2177 | // forced replacements for trivial session history contexts). For these |
| 2178 | // cases, we reuse the last committed entry. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2179 | entry = GetLastCommittedEntry(); |
jam | 0576b13 | 2016-09-07 05:13:10 | [diff] [blame] | 2180 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2181 | // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK |
| 2182 | // to avoid misleading interpretations (e.g. URLs paired with |
| 2183 | // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix |
| 2184 | // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK |
| 2185 | // | PAGE_TRANSITION_CLIENT_REDIRECT). |
| 2186 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2187 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2188 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2189 | // 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] | 2190 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2191 | if (!is_same_document) |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2192 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2193 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2194 | } |
| 2195 | DCHECK(entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2196 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2197 | UpdateNavigationEntryDetails(entry, rfh, params, request, |
| 2198 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2199 | false /* is_new_entry */, commit_details); |
creis | 22a7b4c | 2016-04-28 07:20:30 | [diff] [blame] | 2200 | |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2201 | // The redirected to page should not inherit the favicon from the previous |
| 2202 | // page. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2203 | if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document) |
[email protected] | 91a4ff8 | 2012-10-29 20:29:48 | [diff] [blame] | 2204 | entry->GetFavicon() = FaviconStatus(); |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2205 | |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2206 | // Update the last committed index to reflect the committed entry. Do this |
| 2207 | // before calling DiscardNonCommittedEntriesWithCommitDetails, so that the |
| 2208 | // delegate sees the correct committed index when notified of navigation |
| 2209 | // state changes. (Otherwise CanGoBack may incorrectly return true, as in |
| 2210 | // https://crbug.com/1439948.) |
Charlie Reis | fbe5f102 | 2023-10-03 15:21:21 | [diff] [blame] | 2211 | last_committed_entry_index_ = GetIndexOfEntry(entry); |
Charlie Reis | 951f4337 | 2023-05-05 00:30:07 | [diff] [blame] | 2212 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2213 | // We should also usually discard the pending entry if it corresponds to a |
| 2214 | // different navigation, since that one is now likely canceled. In rare |
| 2215 | // cases, we leave the pending entry for another navigation in place when we |
| 2216 | // know it is still ongoing, to avoid a flicker in the omnibox (see |
| 2217 | // https://crbug.com/900036). |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2218 | // |
| 2219 | // Note that we need to use the "internal" version since we don't want to |
| 2220 | // actually change any other state, just kill the pointer. |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2221 | if (!keep_pending_entry) |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2222 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2223 | } |
| 2224 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2225 | void NavigationControllerImpl::RendererDidNavigateNewSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2226 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2227 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2228 | bool is_same_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2229 | bool replace_entry, |
| 2230 | bool previous_document_was_activated, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2231 | NavigationRequest* request, |
| 2232 | LoadCommittedDetails* commit_details) { |
avi | 25f5f9e | 2015-07-17 20:08:26 | [diff] [blame] | 2233 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2234 | ui::PAGE_TRANSITION_MANUAL_SUBFRAME)); |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2235 | // The NEW_SUBFRAME path should never result in an initial NavigationEntry. |
| 2236 | DCHECK(!commit_details->should_stay_as_initial_entry); |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 2237 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2238 | // Manual subframe navigations just get the current entry cloned so the user |
| 2239 | // can go back or forward to it. The actual subframe information will be |
| 2240 | // stored in the page state for each of those entries. This happens out of |
| 2241 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 2242 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2243 | << "that a last committed entry exists."; |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2244 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2245 | // The DCHECK below documents the fact that we don't know of any situation |
| 2246 | // where |replace_entry| is true for subframe navigations. This simplifies |
| 2247 | // reasoning about the replacement struct for subframes (see |
| 2248 | // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()). |
| 2249 | DCHECK(!replace_entry); |
| 2250 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2251 | // This FrameNavigationEntry might not end up being used in the |
| 2252 | // CloneAndReplace() call below, if a spot can't be found for it in the tree. |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2253 | const absl::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2254 | request->common_params().initiator_origin; |
W. James MacLean | 8be423a | 2023-03-31 21:35:52 | [diff] [blame] | 2255 | absl::optional<GURL> initiator_base_url; |
| 2256 | if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) { |
| 2257 | initiator_base_url = request->common_params().initiator_base_url; |
| 2258 | } |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2259 | std::unique_ptr<PolicyContainerPolicies> policy_container_policies = |
| 2260 | ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document, |
| 2261 | request->GetURL()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2262 | bool protect_url_in_navigation_api = false; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2263 | if (is_same_document) { |
| 2264 | FrameNavigationEntry* previous_frame_entry = |
| 2265 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2266 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2267 | // FrameNavigationEntry. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2268 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2269 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2270 | previous_frame_entry->protect_url_in_navigation_api(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2271 | } else { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2272 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2273 | policy_container_policies && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2274 | ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2275 | policy_container_policies->referrer_policy); |
| 2276 | } |
| 2277 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2278 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2279 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2280 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2281 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2282 | GetCommittedOriginForFrameEntry(params, request), |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2283 | Referrer(*params.referrer), initiator_origin, initiator_base_url, |
| 2284 | request->GetRedirectChain(), params.page_state, params.method, |
| 2285 | params.post_id, nullptr /* blob_url_loader_factory */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2286 | std::move(policy_container_policies), protect_url_in_navigation_api); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2287 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2288 | std::unique_ptr<NavigationEntryImpl> new_entry = |
| 2289 | GetLastCommittedEntry()->CloneAndReplace( |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2290 | std::move(frame_entry), is_same_document, rfh->frame_tree_node(), |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2291 | frame_tree_->root()); |
creis | e062d54 | 2015-08-25 02:01:55 | [diff] [blame] | 2292 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2293 | SetShouldSkipOnBackForwardUIIfNeeded( |
shivanigithub | e92c33da | 2020-09-14 13:01:41 | [diff] [blame] | 2294 | replace_entry, previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2295 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2296 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2297 | // 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] | 2298 | // to replace, which can happen due to a unique name change. See |
| 2299 | // https://crbug.com/607205. For now, the call to CloneAndReplace() will |
| 2300 | // delete the |frame_entry| when the function exits if it doesn't get used. |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2301 | |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2302 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, false, |
Abhijeet Kandalkar | e26014a9 | 2022-10-13 04:21:15 | [diff] [blame] | 2303 | rfh->IsNestedWithinFencedFrame(), commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2304 | } |
| 2305 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2306 | bool NavigationControllerImpl::RendererDidNavigateAutoSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2307 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2308 | const mojom::DidCommitProvisionalLoadParams& params, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2309 | bool is_same_document, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2310 | bool was_on_initial_empty_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2311 | NavigationRequest* request, |
| 2312 | LoadCommittedDetails* commit_details) { |
avi | 9f07a0c | 2015-02-18 22:51:29 | [diff] [blame] | 2313 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2314 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
| 2315 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2316 | // We're guaranteed to have a previously committed entry, and we now need to |
| 2317 | // handle navigation inside of a subframe in it without creating a new entry. |
| 2318 | DCHECK(GetLastCommittedEntry()); |
| 2319 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2320 | // For newly created subframes, we don't need to send a commit notification. |
| 2321 | // This is only necessary for history navigations in subframes. |
| 2322 | bool send_commit_notification = false; |
| 2323 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2324 | // If |nav_entry_id| is non-zero and matches an existing entry, this |
| 2325 | // is a history navigation. Update the last committed index accordingly. If |
| 2326 | // we don't recognize the |nav_entry_id|, it might be a recently |
| 2327 | // pruned entry. We'll handle it below. |
| 2328 | if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
| 2329 | int entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2330 | if (entry_index != -1 && entry_index != last_committed_entry_index_) { |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2331 | // Make sure that a subframe commit isn't changing the main frame's |
| 2332 | // origin. Otherwise the renderer process may be confused, leading to a |
| 2333 | // URL spoof. We can't check the path since that may change |
| 2334 | // (https://crbug.com/373041). |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2335 | // TODO(creis): For now, restrict this check to HTTP(S) origins, because |
| 2336 | // about:blank, file, and unique origins are more subtle to get right. |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 2337 | // We should use checks similar to RenderFrameHostImpl's |
| 2338 | // CanCommitUrlAndOrigin on the main frame during subframe commits. |
| 2339 | // See https://crbug.com/1209092. |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2340 | const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL(); |
| 2341 | const GURL& current_top_url = GetLastCommittedEntry()->GetURL(); |
| 2342 | if (current_top_url.SchemeIsHTTPOrHTTPS() && |
| 2343 | dest_top_url.SchemeIsHTTPOrHTTPS() && |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2344 | current_top_url.DeprecatedGetOriginAsURL() != |
| 2345 | dest_top_url.DeprecatedGetOriginAsURL()) { |
Chris Bookholt | 10f4b733 | 2022-02-14 18:25:44 | [diff] [blame] | 2346 | bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(), |
creis | fb6eeb6 | 2016-05-10 19:01:51 | [diff] [blame] | 2347 | bad_message::NC_AUTO_SUBFRAME); |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2348 | } |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2349 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2350 | // We only need to discard the pending entry in this history navigation |
| 2351 | // case. For newly created subframes, there was no pending entry. |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2352 | last_committed_entry_index_ = entry_index; |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2353 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2354 | |
| 2355 | // History navigations should send a commit notification. |
| 2356 | send_commit_notification = true; |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2357 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2358 | } |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 2359 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2360 | // This may be a "new auto" case where we add a new FrameNavigationEntry, or |
| 2361 | // it may be a "history auto" case where we update an existing one. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2362 | // We may want to update |last_committed|'s FrameNavigationEntry (if one |
| 2363 | // exists), or we may want to clobber it and create a new one. We update in |
| 2364 | // cases where the corresponding FrameNavigationEntry is conceptually similar |
| 2365 | // to the document described by the commit params: same-document |
| 2366 | // navigations, history traversal to an existing entry, and reloads (including |
| 2367 | // a "soft reload" where we navigate to the same url without flagging it as a |
| 2368 | // reload). But in the case of a different document that is not logically |
| 2369 | // related to the committed FrameNavigationEntry's document (cross-document, |
| 2370 | // not same url, not a reload, not a history traversal), we replace rather |
| 2371 | // than update. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2372 | // |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2373 | // In the case where we update, the FrameNavigationEntry will potentially be |
| 2374 | // shared across multiple NavigationEntries, and any updates will be reflected |
| 2375 | // in all of those NavigationEntries. In the replace case, any existing |
| 2376 | // sharing with other NavigationEntries will stop. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2377 | // |
| 2378 | // When navigating away from the initial empty document, we also update rather |
| 2379 | // than replace. Either update or replace will overwrite the initial empty |
| 2380 | // document state for |last_committed|, but if the FrameNavigationEntry for |
| 2381 | // the initial empty document is shared across multiple NavigationEntries (due |
| 2382 | // to a navigation in another frame), we want to make sure we overwrite the |
| 2383 | // initial empty document state everywhere this FrameNavigationEntry is used, |
| 2384 | // which is accompished by updating the existing FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2385 | NavigationEntryImpl* last_committed = GetLastCommittedEntry(); |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2386 | FrameNavigationEntry* last_committed_frame_entry = |
| 2387 | last_committed->GetFrameEntry(rfh->frame_tree_node()); |
| 2388 | NavigationEntryImpl::UpdatePolicy update_policy = |
| 2389 | NavigationEntryImpl::UpdatePolicy::kUpdate; |
| 2390 | if (request->common_params().navigation_type == |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 2391 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT && |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2392 | last_committed_frame_entry && |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2393 | last_committed_frame_entry->url() != params.url && |
| 2394 | !was_on_initial_empty_document) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2395 | update_policy = NavigationEntryImpl::UpdatePolicy::kReplace; |
| 2396 | } |
| 2397 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2398 | UpdateNavigationEntryDetails(last_committed, rfh, params, request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2399 | update_policy, false /* is_new_entry */, |
| 2400 | commit_details); |
creis | 625a0c7d | 2015-03-24 23:17:12 | [diff] [blame] | 2401 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2402 | return send_commit_notification; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2403 | } |
| 2404 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2405 | int NavigationControllerImpl::GetIndexOfEntry( |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 2406 | const NavigationEntryImpl* entry) const { |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 2407 | for (size_t i = 0; i < entries_.size(); ++i) { |
| 2408 | if (entries_[i].get() == entry) |
| 2409 | return i; |
| 2410 | } |
| 2411 | return -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2412 | } |
| 2413 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2414 | void NavigationControllerImpl::CopyStateFrom(NavigationController* temp, |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2415 | bool needs_reload) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2416 | NavigationControllerImpl* source = |
| 2417 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2418 | // Verify that we look new. |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 2419 | DCHECK_EQ(1, GetEntryCount()); |
| 2420 | DCHECK(GetLastCommittedEntry()->IsInitialEntry()); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 2421 | DCHECK(!GetPendingEntry()); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2422 | entries_.clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2423 | |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2424 | needs_reload_ = needs_reload; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 2425 | needs_reload_type_ = NeedsReloadType::kCopyStateFrom; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2426 | InsertEntriesFrom(source, source->GetEntryCount()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2427 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2428 | for (auto& it : source->session_storage_namespace_map_) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2429 | SessionStorageNamespaceImpl* source_namespace = |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2430 | static_cast<SessionStorageNamespaceImpl*>(it.second.get()); |
| 2431 | session_storage_namespace_map_[it.first] = source_namespace->Clone(); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2432 | } |
[email protected] | 4e6419c | 2010-01-15 04:50:34 | [diff] [blame] | 2433 | |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 2434 | FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2435 | } |
| 2436 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2437 | void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp, |
| 2438 | bool replace_entry) { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2439 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2440 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2441 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2442 | NavigationControllerImpl* source = |
| 2443 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2444 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2445 | // Remove all the entries leaving the last committed entry. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2446 | PruneAllButLastCommittedInternal(); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2447 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2448 | // We now have one entry, possibly with a new pending entry. Ensure that |
| 2449 | // adding the entries from source won't put us over the limit. |
| 2450 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | e78a685 | 2013-12-13 08:08:57 | [diff] [blame] | 2451 | if (!replace_entry) |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2452 | source->PruneOldestSkippableEntryIfFull(); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 2453 | |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 2454 | // Insert the entries from source. Ignore any pending entry, since it has not |
| 2455 | // committed in source. |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2456 | int max_source_index = source->last_committed_entry_index_; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2457 | DCHECK_NE(max_source_index, -1); |
| 2458 | max_source_index++; |
[email protected] | e78a685 | 2013-12-13 08:08:57 | [diff] [blame] | 2459 | |
| 2460 | // Ignore the source's current entry if merging with replacement. |
| 2461 | // TODO(davidben): This should preserve entries forward of the current |
| 2462 | // too. http://crbug.com/317872 |
| 2463 | if (replace_entry && max_source_index > 0) |
| 2464 | max_source_index--; |
| 2465 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2466 | InsertEntriesFrom(source, max_source_index); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2467 | |
| 2468 | // Adjust indices such that the last entry and pending are at the end now. |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2469 | last_committed_entry_index_ = GetEntryCount() - 1; |
[email protected] | 796931a9 | 2011-08-10 01:32:14 | [diff] [blame] | 2470 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2471 | BroadcastHistoryOffsetAndLength(); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2472 | } |
| 2473 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2474 | bool NavigationControllerImpl::CanPruneAllButLastCommitted() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2475 | // If there is no last committed entry, we cannot prune. Even if there is a |
| 2476 | // pending entry, it may not commit, leaving this WebContents blank, despite |
| 2477 | // possibly giving it new entries via CopyStateFromAndPrune. |
| 2478 | if (last_committed_entry_index_ == -1) |
| 2479 | return false; |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2480 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2481 | // We cannot prune if there is a pending entry at an existing entry index. |
| 2482 | // It may not commit, so we have to keep the last committed entry, and thus |
| 2483 | // there is no sensible place to keep the pending entry. It is ok to have |
| 2484 | // a new pending entry, which can optionally commit as a new navigation. |
| 2485 | if (pending_entry_index_ != -1) |
| 2486 | return false; |
| 2487 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2488 | return true; |
| 2489 | } |
| 2490 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2491 | void NavigationControllerImpl::PruneAllButLastCommitted() { |
| 2492 | PruneAllButLastCommittedInternal(); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2493 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2494 | DCHECK_EQ(0, last_committed_entry_index_); |
| 2495 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2496 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2497 | BroadcastHistoryOffsetAndLength(); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2498 | } |
| 2499 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2500 | void NavigationControllerImpl::PruneAllButLastCommittedInternal() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2501 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2502 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2503 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 2504 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
| 2505 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2506 | // Erase all entries but the last committed entry. There may still be a |
| 2507 | // new pending entry after this. |
| 2508 | entries_.erase(entries_.begin(), |
| 2509 | entries_.begin() + last_committed_entry_index_); |
| 2510 | entries_.erase(entries_.begin() + 1, entries_.end()); |
| 2511 | last_committed_entry_index_ = 0; |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2512 | } |
| 2513 | |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2514 | void NavigationControllerImpl::DeleteNavigationEntries( |
| 2515 | const DeletionPredicate& deletionPredicate) { |
| 2516 | // It is up to callers to check the invariants before calling this. |
| 2517 | CHECK(CanPruneAllButLastCommitted()); |
| 2518 | std::vector<int> delete_indices; |
| 2519 | for (size_t i = 0; i < entries_.size(); i++) { |
| 2520 | if (i != static_cast<size_t>(last_committed_entry_index_) && |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2521 | deletionPredicate.Run(entries_[i].get())) { |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2522 | delete_indices.push_back(i); |
| 2523 | } |
| 2524 | } |
| 2525 | if (delete_indices.empty()) |
| 2526 | return; |
| 2527 | |
| 2528 | if (delete_indices.size() == GetEntryCount() - 1U) { |
| 2529 | PruneAllButLastCommitted(); |
| 2530 | } else { |
| 2531 | // Do the deletion in reverse to preserve indices. |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 2532 | for (const auto& index : base::Reversed(delete_indices)) { |
| 2533 | RemoveEntryAtIndex(index); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2534 | } |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2535 | BroadcastHistoryOffsetAndLength(); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2536 | } |
| 2537 | delegate()->NotifyNavigationEntriesDeleted(); |
| 2538 | } |
| 2539 | |
Carlos Caballero | 35ce710c | 2019-09-19 10:59:45 | [diff] [blame] | 2540 | BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() { |
| 2541 | return back_forward_cache_; |
| 2542 | } |
| 2543 | |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2544 | NavigationEntryScreenshotCache* |
| 2545 | NavigationControllerImpl::GetNavigationEntryScreenshotCache() { |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 2546 | CHECK(frame_tree_->is_primary()); |
William Liu | 055a354 | 2023-04-02 17:21:19 | [diff] [blame] | 2547 | if (!nav_entry_screenshot_cache_ && AreBackForwardTransitionsEnabled()) { |
| 2548 | nav_entry_screenshot_cache_ = |
| 2549 | std::make_unique<NavigationEntryScreenshotCache>( |
| 2550 | BrowserContextImpl::From(browser_context_) |
| 2551 | ->GetNavigationEntryScreenshotManager() |
| 2552 | ->GetSafeRef(), |
| 2553 | this); |
| 2554 | } |
| 2555 | return nav_entry_screenshot_cache_.get(); |
| 2556 | } |
| 2557 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2558 | void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) { |
| 2559 | // It is not safe to call DiscardPendingEntry while NavigateToEntry is in |
| 2560 | // progress, since this will cause a use-after-free. (We only allow this |
| 2561 | // when the tab is being destroyed for shutdown, since it won't return to |
| 2562 | // NavigateToEntry in that case.) http://crbug.com/347742. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2563 | CHECK(!in_navigate_to_pending_entry_ || frame_tree_->IsBeingDestroyed()); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2564 | |
| 2565 | if (was_failure && pending_entry_) { |
| 2566 | failed_pending_entry_id_ = pending_entry_->GetUniqueID(); |
| 2567 | } else { |
| 2568 | failed_pending_entry_id_ = 0; |
| 2569 | } |
| 2570 | |
| 2571 | if (pending_entry_) { |
| 2572 | if (pending_entry_index_ == -1) |
Paul Semel | 7e51469e | 2022-07-12 12:16:33 | [diff] [blame] | 2573 | pending_entry_.ClearAndDelete(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2574 | pending_entry_index_ = -1; |
| 2575 | pending_entry_ = nullptr; |
| 2576 | } |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 2577 | |
| 2578 | // Ensure any refs to the current pending entry are ignored if they get |
| 2579 | // deleted, by clearing the set of known refs. All future pending entries will |
| 2580 | // only be affected by new refs. |
| 2581 | pending_entry_refs_.clear(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) { |
| 2585 | if (pending_entry_) |
| 2586 | pending_entry_->set_ssl_error(error); |
| 2587 | } |
| 2588 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 2589 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2590 | // static |
| 2591 | bool NavigationControllerImpl::ValidateDataURLAsString( |
| 2592 | const scoped_refptr<const base::RefCountedString>& data_url_as_string) { |
| 2593 | if (!data_url_as_string) |
| 2594 | return false; |
| 2595 | |
| 2596 | if (data_url_as_string->size() > kMaxLengthOfDataURLString) |
| 2597 | return false; |
| 2598 | |
| 2599 | // The number of characters that is enough for validating a data: URI. |
| 2600 | // From the GURL's POV, the only important part here is scheme, it doesn't |
| 2601 | // check the actual content. Thus we can take only the prefix of the url, to |
| 2602 | // avoid unneeded copying of a potentially long string. |
| 2603 | const size_t kDataUriPrefixMaxLen = 64; |
| 2604 | GURL data_url( |
| 2605 | std::string(data_url_as_string->front_as<char>(), |
| 2606 | std::min(data_url_as_string->size(), kDataUriPrefixMaxLen))); |
| 2607 | if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme)) |
| 2608 | return false; |
| 2609 | |
| 2610 | return true; |
| 2611 | } |
| 2612 | #endif |
| 2613 | |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2614 | void NavigationControllerImpl::NotifyUserActivation() { |
| 2615 | // When a user activation occurs, ensure that all adjacent entries for the |
| 2616 | // same document clear their skippable bit, so that the history manipulation |
| 2617 | // intervention does not apply to them. |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2618 | const bool can_go_back = CanGoBack(); |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 2619 | SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false); |
Lijin Shen | 9c475d3 | 2023-09-02 00:15:01 | [diff] [blame] | 2620 | // If the value of CanGoBack changes as a result of making some entries |
| 2621 | // non-skippable, then we must let the delegate know to update its UI state. |
| 2622 | // See https://crbug.com/1477784. |
| 2623 | if (!can_go_back && CanGoBack()) { |
| 2624 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
| 2625 | } |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2626 | } |
| 2627 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2628 | bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe( |
| 2629 | RenderFrameHostImpl* render_frame_host, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2630 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client, |
| 2631 | blink::LocalFrameToken initiator_frame_token, |
| 2632 | int initiator_process_id) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2633 | NavigationEntryImpl* entry = |
| 2634 | GetEntryWithUniqueID(render_frame_host->nav_entry_id()); |
| 2635 | if (!entry) |
| 2636 | return false; |
| 2637 | |
| 2638 | FrameNavigationEntry* frame_entry = |
| 2639 | entry->GetFrameEntry(render_frame_host->frame_tree_node()); |
| 2640 | if (!frame_entry) |
| 2641 | return false; |
| 2642 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2643 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2644 | render_frame_host->frame_tree_node(), entry, frame_entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2645 | ReloadType::NONE, false /* is_same_document_history_load */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2646 | true /* is_history_navigation_in_new_child */, initiator_frame_token, |
| 2647 | initiator_process_id); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2648 | |
| 2649 | if (!request) |
| 2650 | return false; |
| 2651 | |
arthursonzogni | f046d4a | 2019-12-12 19:08:10 | [diff] [blame] | 2652 | request->SetNavigationClient(std::move(*navigation_client)); |
Arthur Hemery | 06173ce | 2019-05-29 12:11:41 | [diff] [blame] | 2653 | |
Rakina Zata Amni | 1c83b08 | 2023-02-08 01:09:00 | [diff] [blame] | 2654 | SCOPED_CRASH_KEY_STRING256( |
| 2655 | "Bug1400009", "req_url", |
| 2656 | request->GetURL().GetWithEmptyPath().possibly_invalid_spec()); |
| 2657 | SCOPED_CRASH_KEY_NUMBER( |
| 2658 | "Bug1400009", "nav_entry_si", |
| 2659 | entry->site_instance() ? ((int)entry->site_instance()->GetId()) : -1); |
| 2660 | SCOPED_CRASH_KEY_NUMBER("Bug1400009", "fne_si", |
| 2661 | frame_entry->site_instance() |
| 2662 | ? ((int)frame_entry->site_instance()->GetId()) |
| 2663 | : -1); |
| 2664 | bool has_sig = |
| 2665 | (frame_entry->site_instance() && frame_entry->site_instance()->group()); |
| 2666 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_exists", has_sig); |
| 2667 | SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_has_rvh", |
| 2668 | has_sig ? (!!frame_tree_->GetRenderViewHost( |
| 2669 | frame_entry->site_instance()->group())) |
| 2670 | : false); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2671 | render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request), |
| 2672 | ReloadType::NONE); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2673 | |
| 2674 | return true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2675 | } |
| 2676 | |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2677 | bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) { |
| 2678 | NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex()); |
| 2679 | if (!entry) |
| 2680 | return false; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 2681 | |
| 2682 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 2683 | // We should never navigate to an existing initial NavigationEntry that is |
| 2684 | // the initial NavigationEntry for the initial empty document that hasn't |
| 2685 | // been overridden by the synchronous about:blank commit, to preserve |
| 2686 | // legacy behavior where trying to reload when the main frame is on the |
| 2687 | // initial empty document won't result in a navigation. See also |
| 2688 | // https://crbug.com/1277414. |
| 2689 | return false; |
| 2690 | } |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2691 | FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node); |
| 2692 | if (!frame_entry) |
| 2693 | return false; |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2694 | ReloadType reload_type = ReloadType::NORMAL; |
| 2695 | entry->set_reload_type(reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2696 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2697 | frame_tree_node, entry, frame_entry, reload_type, |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2698 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2699 | false /* is_history_navigation_in_new_child */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 2700 | absl::nullopt /* initiator_frame_token */, |
| 2701 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2702 | if (!request) |
| 2703 | return false; |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2704 | frame_tree_node->navigator().Navigate(std::move(request), reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2705 | return true; |
| 2706 | } |
| 2707 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2708 | void NavigationControllerImpl::NavigateFromFrameProxy( |
| 2709 | RenderFrameHostImpl* render_frame_host, |
| 2710 | const GURL& url, |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2711 | const blink::LocalFrameToken* initiator_frame_token, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2712 | int initiator_process_id, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2713 | const absl::optional<url::Origin>& initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2714 | const absl::optional<GURL>& initiator_base_url, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2715 | bool is_renderer_initiated, |
| 2716 | SiteInstance* source_site_instance, |
| 2717 | const Referrer& referrer, |
| 2718 | ui::PageTransition page_transition, |
| 2719 | bool should_replace_current_entry, |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 2720 | blink::NavigationDownloadPolicy download_policy, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2721 | const std::string& method, |
| 2722 | scoped_refptr<network::ResourceRequestBody> post_body, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 2723 | const std::string& extra_headers, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2724 | network::mojom::SourceLocationPtr source_location, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2725 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 2726 | bool is_form_submission, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 2727 | const absl::optional<blink::Impression>& impression, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 2728 | blink::mojom::NavigationInitiatorActivationAndAdStatus |
| 2729 | initiator_activation_and_ad_status, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 2730 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 2731 | bool is_embedder_initiated_fenced_frame_navigation, |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 2732 | bool is_unfenced_top_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 2733 | bool force_new_browsing_instance, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2734 | bool is_container_initiated, |
| 2735 | absl::optional<std::u16string> embedder_shared_storage_context) { |
Lukasz Anforowicz | 63f3b943 | 2019-05-30 05:42:58 | [diff] [blame] | 2736 | if (is_renderer_initiated) |
| 2737 | DCHECK(initiator_origin.has_value()); |
| 2738 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2739 | FrameTreeNode* node = render_frame_host->frame_tree_node(); |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2740 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2741 | // Don't allow an entry replacement if there is no entry to replace. |
| 2742 | // http://crbug.com/457149 |
| 2743 | if (GetEntryCount() == 0) |
| 2744 | should_replace_current_entry = false; |
| 2745 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2746 | // Create a NavigationEntry for the transfer, without making it the pending |
| 2747 | // entry. Subframe transfers should have a clone of the last committed entry |
| 2748 | // with a FrameNavigationEntry for the target frame. Main frame transfers |
| 2749 | // should have a new NavigationEntry. |
| 2750 | // TODO(creis): Make this unnecessary by creating (and validating) the params |
| 2751 | // directly, passing them to the destination RenderFrameHost. See |
| 2752 | // https://crbug.com/536906. |
| 2753 | std::unique_ptr<NavigationEntryImpl> entry; |
Harkiran Bolaria | e1b5158b | 2021-09-16 19:03:26 | [diff] [blame] | 2754 | if (!render_frame_host->is_main_frame()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2755 | // Subframe case: create FrameNavigationEntry. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2756 | DCHECK(GetLastCommittedEntry()); |
| 2757 | entry = GetLastCommittedEntry()->Clone(); |
| 2758 | entry->set_extra_headers(extra_headers); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2759 | // TODO(arthursonzogni): What about |is_renderer_initiated|? |
| 2760 | // Renderer-initiated navigation that target a remote frame are currently |
| 2761 | // classified as browser-initiated when this one has already navigated. |
| 2762 | // See https://crbug.com/722251. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2763 | // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter |
| 2764 | // to CreateNavigationRequestFromLoadParams(), so while kReplace might |
| 2765 | // remove child FrameNavigationEntries (e.g., if this is a cross-process |
| 2766 | // same-document navigation), they will still be present in the |
| 2767 | // committed NavigationEntry that will be referenced to construct the new |
| 2768 | // FrameNavigationEntry tree when this navigation commits. |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2769 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2770 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2771 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2772 | absl::nullopt /* commit_origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2773 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2774 | blob_url_loader_factory, nullptr /* policy_container_policies */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2775 | } else { |
| 2776 | // Main frame case. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 2777 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 2778 | // to adjust the original input url if needed. For inner frames such as |
| 2779 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 2780 | // input urls by users. |
| 2781 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2782 | absl::optional<GURL> source_process_site_url = absl::nullopt; |
| 2783 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 2784 | source_process_site_url = |
| 2785 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 2786 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2787 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2788 | url, referrer, initiator_origin, initiator_base_url, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 2789 | source_process_site_url, page_transition, is_renderer_initiated, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2790 | extra_headers, browser_context_, blob_url_loader_factory, |
| 2791 | rewrite_virtual_urls)); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2792 | entry->root_node()->frame_entry->set_source_site_instance( |
| 2793 | static_cast<SiteInstanceImpl*>(source_site_instance)); |
| 2794 | entry->root_node()->frame_entry->set_method(method); |
| 2795 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2796 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2797 | bool override_user_agent = false; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2798 | if (GetLastCommittedEntry()->GetIsOverridingUserAgent()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2799 | entry->SetIsOverridingUserAgent(true); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2800 | override_user_agent = true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2801 | } |
| 2802 | // TODO(creis): Set user gesture and intent received timestamp on Android. |
| 2803 | |
| 2804 | // We may not have successfully added the FrameNavigationEntry to |entry| |
| 2805 | // above (per https://crbug.com/608402), in which case we create it from |
| 2806 | // scratch. This works because we do not depend on |frame_entry| being inside |
| 2807 | // |entry| during NavigateToEntry. This will go away when we shortcut this |
| 2808 | // further in https://crbug.com/536906. |
| 2809 | scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node)); |
| 2810 | if (!frame_entry) { |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2811 | frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2812 | node->unique_name(), -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2813 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2814 | absl::nullopt /* origin */, referrer, initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2815 | initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1, |
Kunihiko Sakamoto | 2ae79e6 | 2023-05-26 00:34:15 | [diff] [blame] | 2816 | blob_url_loader_factory, nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 2817 | false /* protect_url_in_navigation_api */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2818 | } |
| 2819 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2820 | LoadURLParams params(url); |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2821 | params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token); |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2822 | params.initiator_process_id = initiator_process_id; |
Nasko Oskov | 93e7c55c | 2018-12-19 01:59:29 | [diff] [blame] | 2823 | params.initiator_origin = initiator_origin; |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 2824 | params.initiator_base_url = initiator_base_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2825 | params.source_site_instance = source_site_instance; |
| 2826 | params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT; |
| 2827 | params.transition_type = page_transition; |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2828 | params.frame_tree_node_id = node->frame_tree_node_id(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2829 | params.referrer = referrer; |
| 2830 | /* params.redirect_chain: skip */ |
| 2831 | params.extra_headers = extra_headers; |
| 2832 | params.is_renderer_initiated = is_renderer_initiated; |
| 2833 | params.override_user_agent = UA_OVERRIDE_INHERIT; |
| 2834 | /* params.base_url_for_data_url: skip */ |
| 2835 | /* params.virtual_url_for_data_url: skip */ |
| 2836 | /* params.data_url_as_string: skip */ |
| 2837 | params.post_data = post_body; |
| 2838 | params.can_load_local_resources = false; |
Kevin McNee | e60e76b | 2019-11-27 20:01:58 | [diff] [blame] | 2839 | /* params.should_replace_current_entry: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2840 | /* params.frame_name: skip */ |
| 2841 | // TODO(clamy): See if user gesture should be propagated to this function. |
| 2842 | params.has_user_gesture = false; |
| 2843 | params.should_clear_history_list = false; |
| 2844 | params.started_from_context_menu = false; |
| 2845 | /* params.navigation_ui_data: skip */ |
| 2846 | /* params.input_start: skip */ |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 2847 | params.was_activated = blink::mojom::WasActivatedOption::kUnknown; |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 2848 | /* params.reload_type: skip */ |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2849 | params.impression = impression; |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 2850 | params.download_policy = std::move(download_policy); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 2851 | params.is_form_submission = is_form_submission; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 2852 | params.initiator_activation_and_ad_status = |
| 2853 | initiator_activation_and_ad_status; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2854 | |
| 2855 | std::unique_ptr<NavigationRequest> request = |
| 2856 | CreateNavigationRequestFromLoadParams( |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2857 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2858 | false /* has_user_gesture */, std::move(source_location), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 2859 | ReloadType::NONE, entry.get(), frame_entry.get(), |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 2860 | navigation_start_time, is_embedder_initiated_fenced_frame_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2861 | is_unfenced_top_navigation, is_container_initiated, |
| 2862 | embedder_shared_storage_context); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2863 | |
| 2864 | if (!request) |
| 2865 | return; |
| 2866 | |
Garrett Tanzer | bb8db41 | 2022-09-27 21:59:46 | [diff] [blame] | 2867 | // Force the navigation to take place in a new browsing instance. |
| 2868 | // This is used by _unfencedTop in fenced frames to ensure that navigations |
| 2869 | // leaving the fenced context create a new browsing instance. |
| 2870 | if (force_new_browsing_instance) { |
| 2871 | request->coop_status().ForceBrowsingInstanceSwap(); |
| 2872 | } |
| 2873 | |
Arthur Hemery | 94874276 | 2019-09-18 10:06:24 | [diff] [blame] | 2874 | // At this stage we are proceeding with this navigation. If this was renderer |
| 2875 | // initiated with user gesture, we need to make sure we clear up potential |
| 2876 | // remains of a cancelled browser initiated navigation to avoid URL spoofs. |
| 2877 | DiscardNonCommittedEntries(); |
| 2878 | |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2879 | node->navigator().Navigate(std::move(request), ReloadType::NONE); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2880 | } |
| 2881 | |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2882 | void NavigationControllerImpl::SetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2883 | const StoragePartitionConfig& partition_config, |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 2884 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2885 | if (!session_storage_namespace) |
| 2886 | return; |
| 2887 | |
| 2888 | // We can't overwrite an existing SessionStorage without violating spec. |
| 2889 | // Attempts to do so may give a tab access to another tab's session storage |
| 2890 | // so die hard on an error. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2891 | bool successful_insert = |
| 2892 | session_storage_namespace_map_ |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2893 | .insert(std::make_pair(partition_config, |
Aaron Colwell | f3b316e | 2021-03-11 20:17:05 | [diff] [blame] | 2894 | static_cast<SessionStorageNamespaceImpl*>( |
| 2895 | session_storage_namespace))) |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2896 | .second; |
| 2897 | CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace"; |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2898 | } |
| 2899 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2900 | bool NavigationControllerImpl::IsUnmodifiedBlankTab() { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 2901 | return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() && |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 2902 | !frame_tree_->has_accessed_initial_main_document(); |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 2903 | } |
| 2904 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2905 | SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2906 | const StoragePartitionConfig& partition_config) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2907 | StoragePartition* partition = |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 2908 | browser_context_->GetStoragePartition(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2909 | DOMStorageContextWrapper* context_wrapper = |
| 2910 | static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext()); |
| 2911 | |
| 2912 | SessionStorageNamespaceMap::const_iterator it = |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2913 | session_storage_namespace_map_.find(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2914 | if (it != session_storage_namespace_map_.end()) { |
| 2915 | // Ensure that this namespace actually belongs to this partition. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2916 | DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get()) |
| 2917 | ->IsFromContext(context_wrapper)); |
Aaron Colwell | b731a0ae | 2021-03-19 19:14:47 | [diff] [blame] | 2918 | |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2919 | return it->second.get(); |
| 2920 | } |
| 2921 | |
| 2922 | // Create one if no one has accessed session storage for this partition yet. |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2923 | scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace = |
| 2924 | SessionStorageNamespaceImpl::Create(context_wrapper); |
| 2925 | SessionStorageNamespaceImpl* session_storage_namespace_ptr = |
| 2926 | session_storage_namespace.get(); |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2927 | session_storage_namespace_map_[partition_config] = |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2928 | std::move(session_storage_namespace); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2929 | |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2930 | return session_storage_namespace_ptr; |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | SessionStorageNamespace* |
| 2934 | NavigationControllerImpl::GetDefaultSessionStorageNamespace() { |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2935 | return GetSessionStorageNamespace( |
| 2936 | StoragePartitionConfig::CreateDefault(GetBrowserContext())); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | const SessionStorageNamespaceMap& |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2940 | NavigationControllerImpl::GetSessionStorageNamespaceMap() { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2941 | return session_storage_namespace_map_; |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2942 | } |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2943 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2944 | bool NavigationControllerImpl::NeedsReload() { |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 2945 | return needs_reload_; |
| 2946 | } |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2947 | |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2948 | void NavigationControllerImpl::SetNeedsReload() { |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 2949 | SetNeedsReload(NeedsReloadType::kRequestedByClient); |
| 2950 | } |
| 2951 | |
| 2952 | void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) { |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2953 | needs_reload_ = true; |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 2954 | needs_reload_type_ = type; |
jaekyun | c8cefa8 | 2015-01-09 20:14:54 | [diff] [blame] | 2955 | |
| 2956 | if (last_committed_entry_index_ != -1) { |
| 2957 | entries_[last_committed_entry_index_]->SetTransitionType( |
| 2958 | ui::PAGE_TRANSITION_RELOAD); |
| 2959 | } |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2960 | } |
| 2961 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2962 | void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) { |
Kevin McNee | 0516477 | 2019-09-03 17:24:57 | [diff] [blame] | 2963 | DCHECK_LT(index, GetEntryCount()); |
| 2964 | DCHECK_NE(index, last_committed_entry_index_); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 2965 | DiscardNonCommittedEntries(); |
| 2966 | |
Nate Chapin | 9eb16be7 | 2022-09-23 22:54:31 | [diff] [blame] | 2967 | RemovedEntriesTracker tracker(weak_factory_.GetSafeRef()); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 2968 | entries_.erase(entries_.begin() + index); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 2969 | if (last_committed_entry_index_ > index) |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 2970 | last_committed_entry_index_--; |
| 2971 | } |
| 2972 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2973 | NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2974 | // If there is no pending_entry_, there should be no pending_entry_index_. |
| 2975 | DCHECK(pending_entry_ || pending_entry_index_ == -1); |
| 2976 | |
| 2977 | // 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] | 2978 | // at that index. An exception is while a reload of a post commit error page |
| 2979 | // is ongoing; in that case pending entry will point to the entry replaced |
| 2980 | // by the error. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2981 | DCHECK(pending_entry_index_ == -1 || |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2982 | pending_entry_ == GetEntryAtIndex(pending_entry_index_) || |
| 2983 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2984 | |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 2985 | return pending_entry_; |
| 2986 | } |
| 2987 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2988 | int NavigationControllerImpl::GetPendingEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2989 | // The pending entry index must always be less than the number of entries. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2990 | DCHECK_LT(pending_entry_index_, GetEntryCount()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2991 | return pending_entry_index_; |
| 2992 | } |
| 2993 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 2994 | void NavigationControllerImpl::InsertOrReplaceEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 2995 | std::unique_ptr<NavigationEntryImpl> entry, |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2996 | bool replace, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2997 | bool was_post_commit_error, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2998 | bool in_fenced_frame_tree, |
| 2999 | LoadCommittedDetails* commit_details) { |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 3000 | // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME` |
| 3001 | // set because: |
| 3002 | // 1) They don't influence the history of the outer page. |
| 3003 | // 2) They are always replace only navigation (there is always only one entry |
| 3004 | // in their history stack). |
| 3005 | // 3) Are not top level navigations and appear similar to iframes. |
| 3006 | // Navigations of the fenced frame might create a new NavigationEntry, which |
| 3007 | // will call this function. Non fenced frame navigations will never have |
| 3008 | // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call |
| 3009 | // InsertOrReplaceEntry. |
| 3010 | DCHECK_EQ(in_fenced_frame_tree, |
| 3011 | ui::PageTransitionCoreTypeIs(entry->GetTransitionType(), |
| 3012 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3013 | |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 3014 | // If the pending_entry_index_ is -1, the navigation was to a new page, and we |
| 3015 | // need to keep continuity with the pending entry, so copy the pending entry's |
| 3016 | // unique ID to the committed entry. If the pending_entry_index_ isn't -1, |
| 3017 | // then the renderer navigated on its own, independent of the pending entry, |
| 3018 | // so don't copy anything. |
| 3019 | if (pending_entry_ && pending_entry_index_ == -1) |
| 3020 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3021 | |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 3022 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3023 | |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3024 | // When replacing, don't prune the forward history. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3025 | if (replace || was_post_commit_error) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 3026 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3027 | entries_[last_committed_entry_index_].get(), entry.get()); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3028 | // If the new entry is a post-commit error page, we store the current last |
| 3029 | // committed entry to the side so that we can put it back when navigating |
| 3030 | // away from the error. |
| 3031 | if (was_post_commit_error) { |
| 3032 | DCHECK(!entry_replaced_by_post_commit_error_); |
| 3033 | entry_replaced_by_post_commit_error_ = |
| 3034 | std::move(entries_[last_committed_entry_index_]); |
| 3035 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3036 | entries_[last_committed_entry_index_] = std::move(entry); |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 3037 | return; |
| 3038 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3039 | |
creis | 37979a6 | 2015-08-04 19:48:18 | [diff] [blame] | 3040 | // We shouldn't see replace == true when there's no committed entries. |
| 3041 | DCHECK(!replace); |
| 3042 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 3043 | PruneForwardEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3044 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3045 | PruneOldestSkippableEntryIfFull(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3046 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 3047 | entries_.push_back(std::move(entry)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 3048 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3049 | } |
| 3050 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3051 | void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 3052 | if (entries_.size() < max_entry_count()) |
| 3053 | return; |
| 3054 | |
| 3055 | DCHECK_EQ(max_entry_count(), entries_.size()); |
| 3056 | DCHECK_GT(last_committed_entry_index_, 0); |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3057 | CHECK_EQ(pending_entry_index_, -1); |
| 3058 | |
| 3059 | int index = 0; |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 3060 | // Retrieve the oldest skippable entry. |
| 3061 | for (; index < GetEntryCount(); index++) { |
| 3062 | if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 3063 | break; |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | // If there is no skippable entry or if it is the last committed entry then |
| 3067 | // fall back to pruning the oldest entry. It is not safe to prune the last |
| 3068 | // committed entry. |
| 3069 | if (index == GetEntryCount() || index == last_committed_entry_index_) |
| 3070 | index = 0; |
| 3071 | |
| 3072 | bool should_succeed = RemoveEntryAtIndex(index); |
| 3073 | DCHECK_EQ(true, should_succeed); |
| 3074 | |
| 3075 | NotifyPrunedEntries(this, index, 1); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 3076 | } |
| 3077 | |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3078 | void NavigationControllerImpl::NavigateToExistingPendingEntry( |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3079 | ReloadType reload_type, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3080 | RenderFrameHostImpl* initiator_rfh, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3081 | absl::optional<blink::scheduler::TaskAttributionId> |
| 3082 | soft_navigation_heuristics_task_id, |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3083 | const std::string* navigation_api_key) { |
Alexander Timin | 3a92df7 | 2019-09-20 11:59:50 | [diff] [blame] | 3084 | TRACE_EVENT0("navigation", |
| 3085 | "NavigationControllerImpl::NavigateToExistingPendingEntry"); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3086 | DCHECK(pending_entry_); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3087 | DCHECK(IsInitialNavigation() || pending_entry_index_ != -1); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3088 | if (pending_entry_index_ != -1) { |
| 3089 | // The pending entry may not be in entries_ if a post-commit error page is |
| 3090 | // showing. |
| 3091 | DCHECK(pending_entry_ == entries_[pending_entry_index_].get() || |
| 3092 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
| 3093 | } |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3094 | DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL())); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3095 | bool is_forced_reload = needs_reload_; |
[email protected] | 72097fd0 | 2010-01-21 23:36:01 | [diff] [blame] | 3096 | needs_reload_ = false; |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3097 | FrameTreeNode* root = frame_tree_->root(); |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3098 | int nav_entry_id = pending_entry_->GetUniqueID(); |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3099 | // Only pass down the soft_navigation_heuristics_task_id when the initiator is |
| 3100 | // the same as the top level frame being navigated. |
| 3101 | if (root->current_frame_host() != initiator_rfh) { |
| 3102 | soft_navigation_heuristics_task_id = absl::nullopt; |
| 3103 | } |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 3104 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3105 | // If we were navigating to a slow-to-commit page, and the user performs |
| 3106 | // a session history navigation to the last committed page, RenderViewHost |
| 3107 | // will force the throbber to start, but WebKit will essentially ignore the |
| 3108 | // navigation, and won't send a message to stop the throbber. To prevent this |
| 3109 | // from happening, we drop the navigation here and stop the slow-to-commit |
| 3110 | // page from loading (which would normally happen during the navigation). |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3111 | if (pending_entry_index_ == last_committed_entry_index_ && |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 3112 | !pending_entry_->IsRestored() && |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3113 | pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) { |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3114 | frame_tree_->StopLoading(); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 3115 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 3116 | DiscardNonCommittedEntries(); |
| 3117 | return; |
| 3118 | } |
| 3119 | |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3120 | absl::optional<blink::LocalFrameToken> initiator_frame_token; |
| 3121 | int initiator_process_id = ChildProcessHost::kInvalidUniqueID; |
| 3122 | if (initiator_rfh) { |
| 3123 | initiator_frame_token = initiator_rfh->GetFrameToken(); |
| 3124 | initiator_process_id = initiator_rfh->GetProcess()->GetID(); |
| 3125 | DCHECK(initiator_frame_token); |
| 3126 | } |
| 3127 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 3128 | // Compare FrameNavigationEntries to see which frames in the tree need to be |
| 3129 | // navigated. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3130 | std::vector<std::unique_ptr<NavigationRequest>> same_document_loads; |
| 3131 | std::vector<std::unique_ptr<NavigationRequest>> different_document_loads; |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3132 | FindFramesToNavigate(root, reload_type, initiator_frame_token, |
| 3133 | initiator_process_id, soft_navigation_heuristics_task_id, |
| 3134 | &same_document_loads, &different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3135 | |
| 3136 | if (same_document_loads.empty() && different_document_loads.empty()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3137 | // We were unable to match any frames to navigate. This can happen if a |
| 3138 | // history navigation targets a subframe that no longer exists |
| 3139 | // (https://crbug.com/705550). In this case, we need to update the current |
| 3140 | // history entry to the pending one but keep the main document loaded. We |
| 3141 | // also need to ensure that observers are informed about the updated |
| 3142 | // current history entry (e.g., for greying out back/forward buttons), and |
| 3143 | // that renderer processes update their history offsets. The easiest way |
| 3144 | // to do all that is to schedule a "redundant" same-document navigation in |
| 3145 | // the main frame. |
| 3146 | // |
| 3147 | // Note that we don't want to remove this history entry, as it might still |
| 3148 | // be valid later, since a frame that it's targeting may be recreated. |
| 3149 | // |
| 3150 | // TODO(alexmos, creis): This behavior isn't ideal, as the user would |
| 3151 | // need to repeat history navigations until finding the one that works. |
| 3152 | // Consider changing this behavior to keep looking for the first valid |
| 3153 | // history entry that finds frames to navigate. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3154 | std::unique_ptr<NavigationRequest> navigation_request = |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3155 | CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3156 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3157 | ReloadType::NONE /* reload_type */, |
| 3158 | true /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3159 | false /* is_history_navigation_in_new_child */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3160 | initiator_frame_token, initiator_process_id); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3161 | if (!navigation_request) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3162 | // If this navigation cannot start, delete the pending NavigationEntry. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3163 | DiscardPendingEntry(false); |
| 3164 | return; |
| 3165 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3166 | same_document_loads.push_back(std::move(navigation_request)); |
| 3167 | |
| 3168 | // Sanity check that we never take this branch for any kinds of reloads, |
| 3169 | // as those should've queued a different-document load in the main frame. |
| 3170 | DCHECK(!is_forced_reload); |
| 3171 | DCHECK_EQ(reload_type, ReloadType::NONE); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3172 | } |
| 3173 | |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3174 | // If the initiator is top-navigation sandboxed, then track whether this |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3175 | // navigation affects any frame outside the frame's subtree. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3176 | if (initiator_rfh && initiator_rfh->IsSandboxed( |
| 3177 | network::mojom::WebSandboxFlags::kTopNavigation)) { |
| 3178 | bool navigates_inside_tree = DoesSandboxNavigationStayWithinSubtree( |
| 3179 | initiator_rfh, same_document_loads) && |
| 3180 | DoesSandboxNavigationStayWithinSubtree( |
| 3181 | initiator_rfh, different_document_loads); |
Dave Tapuska | 716ed3af | 2019-09-23 18:45:50 | [diff] [blame] | 3182 | // Count the navigations as web use counters so we can determine |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3183 | // the number of pages that trigger this. |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3184 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 3185 | initiator_rfh, |
| 3186 | navigates_inside_tree |
| 3187 | ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree |
| 3188 | : blink::mojom::WebFeature:: |
| 3189 | kSandboxBackForwardAffectsFramesOutsideSubtree); |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3190 | |
| 3191 | // If the navigation occurred outside the tree discard it because |
| 3192 | // the sandboxed frame didn't have permission to navigate outside |
| 3193 | // its tree. If it is possible that the navigation is both inside and |
| 3194 | // outside the frame tree and we discard it entirely because we don't |
| 3195 | // want to end up in a history state that didn't exist before. |
Dominic Farolino | 05744004 | 2022-01-19 18:18:14 | [diff] [blame] | 3196 | if (!navigates_inside_tree) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 3197 | // If a |navigation_api_key| was provided, this navigation originated from |
| 3198 | // the navigation API. Notify the renderer that the navigation was |
| 3199 | // cancelled so the navigation API can fire an error event and reject the |
| 3200 | // relevant promise. |
| 3201 | if (navigation_api_key) { |
| 3202 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 3203 | *navigation_api_key, |
| 3204 | blink::mojom::TraverseCancelledReason::kSandboxViolation); |
| 3205 | } |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3206 | DiscardPendingEntry(false); |
| 3207 | return; |
| 3208 | } |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3209 | } |
| 3210 | |
Nate Chapin | 6c43c02 | 2023-02-13 23:32:42 | [diff] [blame] | 3211 | // If it is possible that this traverse may involve a same-document navigation |
| 3212 | // in the initiator and there is a Navigation API key involved, then we may |
| 3213 | // need to notify the initiator if it fails. (The early returns above either |
| 3214 | // do not involve these cases or already notify the initiator.) |
| 3215 | // The event only needs to fire for the initiator, and only if the initiator |
| 3216 | // itself is performing a same-document navigation (because the event will not |
| 3217 | // fire if it navigates cross-document). |
| 3218 | if (navigation_api_key) { |
| 3219 | for (auto& item : same_document_loads) { |
| 3220 | if (item->frame_tree_node() == initiator_rfh->frame_tree_node()) { |
| 3221 | item->set_pending_navigation_api_key(*navigation_api_key); |
| 3222 | break; |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3227 | // BackForwardCache: |
| 3228 | // Navigate immediately if the document is in the BackForwardCache. |
Mingyu Lei | 7584b6b | 2023-04-13 03:02:56 | [diff] [blame] | 3229 | if (back_forward_cache_.GetOrEvictEntry(nav_entry_id).has_value()) { |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3230 | TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest"); |
| 3231 | DCHECK_EQ(reload_type, ReloadType::NONE); |
| 3232 | auto navigation_request = CreateNavigationRequestFromEntry( |
| 3233 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
| 3234 | ReloadType::NONE, false /* is_same_document_history_load */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3235 | false /* is_history_navigation_in_new_child */, initiator_frame_token, |
| 3236 | initiator_process_id); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3237 | root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE); |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3238 | |
| 3239 | return; |
| 3240 | } |
| 3241 | |
| 3242 | // History navigation might try to reuse a specific BrowsingInstance, already |
| 3243 | // used by a page in the cache. To avoid having two different main frames that |
| 3244 | // live in the same BrowsingInstance, evict the all pages with this |
| 3245 | // BrowsingInstance from the cache. |
| 3246 | // |
| 3247 | // For example, take the following scenario: |
| 3248 | // |
| 3249 | // A1 = Some page on a.com |
| 3250 | // A2 = Some other page on a.com |
| 3251 | // B3 = An uncacheable page on b.com |
| 3252 | // |
| 3253 | // Then the following navigations occur: |
| 3254 | // A1->A2->B3->A1 |
| 3255 | // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't |
| 3256 | // take its place) and A1 will be created in the same BrowsingInstance (and |
| 3257 | // SiteInstance), as A2. |
| 3258 | // |
| 3259 | // If we didn't do anything, both A1 and A2 would remain alive in the same |
| 3260 | // BrowsingInstance/SiteInstance, which is unsupported by |
| 3261 | // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict |
| 3262 | // A2 from the cache. |
| 3263 | if (pending_entry_->site_instance()) { |
| 3264 | back_forward_cache_.EvictFramesInRelatedSiteInstances( |
| 3265 | pending_entry_->site_instance()); |
| 3266 | } |
| 3267 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3268 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored", |
| 3269 | pending_entry_ && pending_entry_->IsRestored()); |
| 3270 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id", |
| 3271 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3272 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index", |
| 3273 | pending_entry_index_); |
| 3274 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index", |
| 3275 | last_committed_entry_index_); |
| 3276 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size()); |
| 3277 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial", |
| 3278 | pending_entry_ && pending_entry_->IsInitialEntry()); |
| 3279 | SCOPED_CRASH_KEY_BOOL( |
| 3280 | "nav_reentrancy", "pending_entry_initial2", |
| 3281 | pending_entry_ && |
| 3282 | pending_entry_->IsInitialEntryNotForSynchronousAboutBlank()); |
| 3283 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav", |
| 3284 | IsInitialNavigation()); |
| 3285 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav", |
| 3286 | IsInitialBlankNavigation()); |
| 3287 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload); |
| 3288 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type", |
| 3289 | (int)pending_reload_); |
| 3290 | |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3291 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3292 | CHECK(!in_navigate_to_pending_entry_); |
| 3293 | in_navigate_to_pending_entry_ = true; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3294 | |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 3295 | // If the navigation-reentrancy is caused by calling |
| 3296 | // NavigateToExistingPendingEntry twice, this will note the previous call's |
| 3297 | // pending entry's ID. |
| 3298 | SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id", |
| 3299 | pending_entry_ ? pending_entry_->GetUniqueID() : -1); |
| 3300 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3301 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3302 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3303 | // function. |
| 3304 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3305 | |
Nate Chapin | 154b14b | 2023-02-17 18:28:16 | [diff] [blame] | 3306 | // If there is a main-frame same-document history navigation, we may defer |
| 3307 | // the subframe history navigations in order to give JS in the main frame the |
| 3308 | // opportunity to cancel the entire traverse via the navigate event. In that |
| 3309 | // case, we need to stash the main frame request's navigation token on the |
| 3310 | // subframes, so they can look up the main frame request and defer themselves |
| 3311 | // until it completes. |
| 3312 | if (!same_document_loads.empty() && |
| 3313 | same_document_loads.at(0)->frame_tree_node()->IsMainFrame()) { |
| 3314 | NavigationRequest* main_frame_request = same_document_loads.at(0).get(); |
| 3315 | // The token will only be returned in cases where deferring the navigation |
| 3316 | // is necessary. |
| 3317 | if (auto main_frame_same_document_token = |
| 3318 | main_frame_request->GetNavigationTokenForDeferringSubframes()) { |
| 3319 | for (auto& item : same_document_loads) { |
| 3320 | if (item.get() != main_frame_request) { |
| 3321 | item->set_main_frame_same_document_history_token( |
| 3322 | main_frame_same_document_token); |
| 3323 | } |
| 3324 | } |
| 3325 | for (auto& item : different_document_loads) { |
| 3326 | item->set_main_frame_same_document_history_token( |
| 3327 | main_frame_same_document_token); |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3332 | // Send all the same document frame loads before the different document loads. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3333 | for (auto& item : same_document_loads) { |
| 3334 | FrameTreeNode* frame = item->frame_tree_node(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3335 | frame->navigator().Navigate(std::move(item), reload_type); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3336 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3337 | for (auto& item : different_document_loads) { |
| 3338 | FrameTreeNode* frame = item->frame_tree_node(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3339 | frame->navigator().Navigate(std::move(item), reload_type); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3340 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3341 | |
| 3342 | in_navigate_to_pending_entry_ = false; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3343 | } |
| 3344 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3345 | NavigationControllerImpl::HistoryNavigationAction |
| 3346 | NavigationControllerImpl::DetermineActionForHistoryNavigation( |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3347 | FrameTreeNode* frame, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3348 | ReloadType reload_type) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3349 | RenderFrameHostImpl* render_frame_host = frame->current_frame_host(); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3350 | // Only active and prerendered documents are allowed to navigate in their |
| 3351 | // frame. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3352 | if (render_frame_host->lifecycle_state() != |
Sreeja Kamishetty | 299329ad | 2021-03-25 14:06:01 | [diff] [blame] | 3353 | RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) { |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3354 | // - If the document is in pending deletion, the browser already committed |
| 3355 | // to destroying this RenderFrameHost. See https://crbug.com/930278. |
| 3356 | // - If the document is in back-forward cache, it's not allowed to navigate |
| 3357 | // as it should remain frozen. Ignore the request and evict the document |
| 3358 | // from back-forward cache. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3359 | // |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3360 | // If the document is inactive, there's no need to recurse into subframes, |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3361 | // which should all be inactive as well. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3362 | if (frame->current_frame_host()->IsInactiveAndDisallowActivation( |
| 3363 | DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3364 | return HistoryNavigationAction::kStopLooking; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3365 | } |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3366 | } |
arthursonzogni | 03f7615 | 2019-02-12 10:35:20 | [diff] [blame] | 3367 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3368 | // Reloads should result in a different-document load. Note that reloads may |
| 3369 | // also happen via the |needs_reload_| mechanism where the reload_type is |
| 3370 | // NONE, so detect this by comparing whether we're going to the same |
| 3371 | // entry that we're currently on. Similarly to above, only main frames |
| 3372 | // should reach this. Note that subframes support reloads, but that's done |
| 3373 | // via a different path that doesn't involve FindFramesToNavigate (see |
| 3374 | // RenderFrameHost::Reload()). |
| 3375 | if (reload_type != ReloadType::NONE || |
| 3376 | pending_entry_index_ == last_committed_entry_index_) { |
| 3377 | DCHECK(frame->IsMainFrame()); |
| 3378 | return HistoryNavigationAction::kDifferentDocument; |
| 3379 | } |
| 3380 | |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3381 | // If there is no new FrameNavigationEntry for the frame, ignore the |
| 3382 | // load. For example, this may happen when going back to an entry before a |
| 3383 | // frame was created. Suppose we commit a same-document navigation that also |
| 3384 | // results in adding a new subframe somewhere in the tree. If we go back, |
| 3385 | // the new subframe will be missing a FrameNavigationEntry in the previous |
| 3386 | // NavigationEntry, but we shouldn't delete or change what's loaded in |
| 3387 | // it. |
| 3388 | // |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3389 | // Note that in this case, there is no need to keep looking for navigations |
| 3390 | // in subframes, which would be missing FrameNavigationEntries as well. |
| 3391 | // |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3392 | // It's important to check this before checking |old_item| below, since both |
| 3393 | // might be null, and in that case we still shouldn't change what's loaded in |
| 3394 | // this frame. Note that scheduling any loads assumes that |new_item| is |
| 3395 | // non-null. See https://crbug.com/1088354. |
| 3396 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3397 | if (!new_item) |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3398 | return HistoryNavigationAction::kStopLooking; |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3399 | |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3400 | // Use the RenderFrameHost's last committed FrameNavigationEntry to identify |
| 3401 | // which history item it is currently on, since this may be different than the |
| 3402 | // FrameNavigationEntry for the frame in the last committed NavigationEntry |
| 3403 | // (e.g., if a history navigation is targeting multiple frames and only some |
| 3404 | // have committed so far). |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3405 | FrameNavigationEntry* old_item = |
Charlie Reis | a474fb6 | 2022-03-17 02:31:36 | [diff] [blame] | 3406 | frame->current_frame_host()->last_committed_frame_entry(); |
| 3407 | if (!old_item) { |
| 3408 | // In cases where the RenderFrameHost does not have a FrameNavigationEntry, |
| 3409 | // fall back to the last committed NavigationEntry's record for this frame. |
| 3410 | // This may happen in cases like the initial state of the RenderFrameHost. |
| 3411 | // TODO(https://crbug.com/1304466): Ensure the RenderFrameHost always has an |
| 3412 | // accurate FrameNavigationEntry and eliminate this case. |
| 3413 | old_item = GetLastCommittedEntry()->GetFrameEntry(frame); |
| 3414 | } |
| 3415 | // If neither approach finds a FrameNavigationEntry, schedule a |
| 3416 | // different-document load. |
| 3417 | // TODO(https://crbug.com/608402): Remove this case. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3418 | if (!old_item) |
| 3419 | return HistoryNavigationAction::kDifferentDocument; |
| 3420 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3421 | // If the new item is not in the same SiteInstance, schedule a |
| 3422 | // different-document load. Newly restored items may not have a SiteInstance |
| 3423 | // yet, in which case it will be assigned on first commit. |
| 3424 | if (new_item->site_instance() && |
| 3425 | new_item->site_instance() != old_item->site_instance()) |
| 3426 | return HistoryNavigationAction::kDifferentDocument; |
| 3427 | |
| 3428 | // Schedule a different-document load if the current RenderFrameHost is not |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3429 | // live. This case can happen for Ctrl+Back or after a renderer crash. Note |
| 3430 | // that we do this even if the history navigation would not be modifying this |
| 3431 | // frame were it live. |
| 3432 | if (!frame->current_frame_host()->IsRenderFrameLive()) |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3433 | return HistoryNavigationAction::kDifferentDocument; |
| 3434 | |
| 3435 | if (new_item->item_sequence_number() != old_item->item_sequence_number()) { |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3436 | // Starting a navigation after a crash early-promotes the speculative |
| 3437 | // RenderFrameHost. Then we have a RenderFrameHost with no document in it |
| 3438 | // committed yet, so we can not possibly perform a same-document history |
| 3439 | // navigation. The frame would need to be reloaded with a cross-document |
| 3440 | // navigation. |
| 3441 | if (!frame->current_frame_host()->has_committed_any_navigation()) |
| 3442 | return HistoryNavigationAction::kDifferentDocument; |
| 3443 | |
creis | 5413169 | 2016-08-12 18:32:25 | [diff] [blame] | 3444 | // Same document loads happen if the previous item has the same document |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3445 | // sequence number but different item sequence number. |
| 3446 | if (new_item->document_sequence_number() == |
| 3447 | old_item->document_sequence_number()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3448 | return HistoryNavigationAction::kSameDocument; |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3449 | } |
avi | b48cb31 | 2016-05-05 21:35:00 | [diff] [blame] | 3450 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3451 | // Otherwise, if both item and document sequence numbers differ, this |
| 3452 | // should be a different document load. |
| 3453 | return HistoryNavigationAction::kDifferentDocument; |
| 3454 | } |
| 3455 | |
| 3456 | // If the item sequence numbers match, there is no need to navigate this |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3457 | // frame. Keep looking for navigations in this frame's children. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3458 | DCHECK_EQ(new_item->document_sequence_number(), |
| 3459 | old_item->document_sequence_number()); |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3460 | return HistoryNavigationAction::kKeepLooking; |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | void NavigationControllerImpl::FindFramesToNavigate( |
| 3464 | FrameTreeNode* frame, |
| 3465 | ReloadType reload_type, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3466 | const absl::optional<blink::LocalFrameToken>& initiator_frame_token, |
| 3467 | int initiator_process_id, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3468 | absl::optional<blink::scheduler::TaskAttributionId> |
| 3469 | soft_navigation_heuristics_task_id, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3470 | std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads, |
| 3471 | std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) { |
| 3472 | DCHECK(pending_entry_); |
| 3473 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3474 | |
| 3475 | auto action = DetermineActionForHistoryNavigation(frame, reload_type); |
| 3476 | |
| 3477 | if (action == HistoryNavigationAction::kSameDocument) { |
| 3478 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3479 | CreateNavigationRequestFromEntry( |
| 3480 | frame, pending_entry_, new_item, reload_type, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3481 | /*is_same_document_history_load=*/true, |
| 3482 | /*is_history_navigation_in_new_child_frame=*/false, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3483 | initiator_frame_token, initiator_process_id, |
| 3484 | soft_navigation_heuristics_task_id); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3485 | if (navigation_request) { |
| 3486 | // Only add the request if was properly created. It's possible for the |
| 3487 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3488 | same_document_loads->push_back(std::move(navigation_request)); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3489 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3490 | } else if (action == HistoryNavigationAction::kDifferentDocument) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3491 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3492 | CreateNavigationRequestFromEntry( |
| 3493 | frame, pending_entry_, new_item, reload_type, |
| 3494 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3495 | false /* is_history_navigation_in_new_child */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3496 | initiator_frame_token, initiator_process_id); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3497 | if (navigation_request) { |
| 3498 | // Only add the request if was properly created. It's possible for the |
| 3499 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3500 | different_document_loads->push_back(std::move(navigation_request)); |
| 3501 | } |
| 3502 | // For a different document, the subframes will be destroyed, so there's |
| 3503 | // no need to consider them. |
| 3504 | return; |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3505 | } else if (action == HistoryNavigationAction::kStopLooking) { |
| 3506 | return; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3507 | } |
| 3508 | |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3509 | // Do not pass down the soft_navigation_heuristics_task_id to child frames, as |
| 3510 | // we currently only support soft navigation heuristics for the top level |
| 3511 | // frame. |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3512 | for (size_t i = 0; i < frame->child_count(); i++) { |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 3513 | FindFramesToNavigate(frame->child_at(i), reload_type, initiator_frame_token, |
| 3514 | initiator_process_id, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3515 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3516 | same_document_loads, different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3520 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry( |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3521 | const LoadURLParams& params) { |
| 3522 | // Find the appropriate FrameTreeNode. |
| 3523 | FrameTreeNode* node = nullptr; |
| 3524 | if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId || |
| 3525 | !params.frame_name.empty()) { |
| 3526 | node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3527 | ? frame_tree_->FindByID(params.frame_tree_node_id) |
| 3528 | : frame_tree_->FindByName(params.frame_name); |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 3529 | DCHECK(!node || &node->frame_tree() == &frame_tree()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | // If no FrameTreeNode was specified, navigate the main frame. |
| 3533 | if (!node) |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 3534 | node = frame_tree_->root(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3535 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3536 | // Compute overrides to the LoadURLParams for |override_user_agent|, |
| 3537 | // |should_replace_current_entry| and |has_user_gesture| that will be used |
| 3538 | // both in the creation of the NavigationEntry and the NavigationRequest. |
| 3539 | // Ideally, the LoadURLParams themselves would be updated, but since they are |
| 3540 | // passed as a const reference, this is not possible. |
| 3541 | // TODO(clamy): When we only create a NavigationRequest, move this to |
| 3542 | // CreateNavigationRequestFromLoadURLParams. |
| 3543 | bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent, |
| 3544 | GetLastCommittedEntry()); |
| 3545 | |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3546 | // An entry replacement must happen if the current browsing context should |
| 3547 | // maintain a trivial session history. |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 3548 | bool should_replace_current_entry = |
| 3549 | (params.should_replace_current_entry || |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3550 | ShouldMaintainTrivialSessionHistory(node)); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3551 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3552 | // Javascript URLs should not create NavigationEntries. All other navigations |
| 3553 | // do, including navigations to chrome renderer debug URLs. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3554 | if (!params.url.SchemeIs(url::kJavaScriptScheme)) { |
Scott Violet | 5ae6c42e | 2020-10-28 02:47:37 | [diff] [blame] | 3555 | std::unique_ptr<NavigationEntryImpl> entry = |
| 3556 | CreateNavigationEntryFromLoadParams(node, params, override_user_agent, |
| 3557 | should_replace_current_entry, |
| 3558 | params.has_user_gesture); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3559 | DiscardPendingEntry(false); |
| 3560 | SetPendingEntry(std::move(entry)); |
| 3561 | } |
| 3562 | |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3563 | // Renderer-debug URLs are sent to the current renderer process immediately |
| 3564 | // for processing and don't need to create a NavigationRequest. Note: this |
| 3565 | // includes navigations to JavaScript URLs, which are considered |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3566 | // renderer-debug URLs. |
| 3567 | // Note: we intentionally leave the pending entry in place for renderer debug |
| 3568 | // URLs, unlike the cases below where we clear it if the navigation doesn't |
| 3569 | // proceed. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3570 | if (blink::IsRendererDebugURL(params.url)) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3571 | // Renderer-debug URLs won't go through NavigationThrottlers so we have to |
| 3572 | // check them explicitly. See bug 913334. |
Aaron Colwell | e1908d98 | 2020-06-26 22:08:15 | [diff] [blame] | 3573 | if (GetContentClient()->browser()->ShouldBlockRendererDebugURL( |
Tim Judkins | 5954819 | 2023-05-17 17:51:20 | [diff] [blame] | 3574 | params.url, browser_context_, node->current_frame_host())) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3575 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3576 | return nullptr; |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3577 | } |
| 3578 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3579 | HandleRendererDebugURL(node, params.url); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3580 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3581 | } |
| 3582 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3583 | DCHECK(pending_entry_); |
| 3584 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3585 | // Convert navigations to the current URL to a reload. |
| 3586 | // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of |
| 3587 | // relying on the frame tree node id from LoadURLParams. Unfortunately, |
| 3588 | // DevTools sometimes issues navigations to main frames that they do not |
| 3589 | // expect to see treated as reload, and it only works because they pass a |
| 3590 | // FrameTreeNode id in their LoadURLParams. Change this once they no longer do |
| 3591 | // that. See https://crbug.com/850926. |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3592 | ReloadType reload_type = params.reload_type; |
| 3593 | if (reload_type == ReloadType::NONE && |
| 3594 | ShouldTreatNavigationAsReload( |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 3595 | node, params.url, pending_entry_->GetVirtualURL(), |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3596 | params.base_url_for_data_url, params.transition_type, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3597 | params.load_type == |
| 3598 | NavigationController::LOAD_TYPE_HTTP_POST /* is_post */, |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3599 | should_replace_current_entry, GetLastCommittedEntry())) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3600 | reload_type = ReloadType::NORMAL; |
Alexander Timin | b70f6738 | 2020-12-10 00:03:47 | [diff] [blame] | 3601 | pending_entry_->set_reload_type(reload_type); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3602 | |
| 3603 | // If this is a reload of an existing FrameNavigationEntry and we had a |
| 3604 | // policy container for it, then we should copy it into the pending entry, |
| 3605 | // so that it is copied to the navigation request in |
| 3606 | // CreateNavigationRequestFromLoadParams later. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3607 | FrameNavigationEntry* previous_frame_entry = |
| 3608 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 3609 | if (previous_frame_entry && |
| 3610 | previous_frame_entry->policy_container_policies()) { |
| 3611 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
| 3612 | previous_frame_entry->policy_container_policies()->ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | // 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] | 3617 | // document, then we should copy the document polices from RenderFrameHost's |
| 3618 | // PolicyContainerHost. The NavigationRequest will create a new |
| 3619 | // PolicyContainerHost with the document policies from the |pending_entry_|, |
| 3620 | // and that PolicyContainerHost will be put in the final RenderFrameHost for |
| 3621 | // the navigation. This way, we ensure that we keep enforcing the right |
| 3622 | // policies on the initial empty document after the reload. |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3623 | if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3624 | if (node->current_frame_host() && |
| 3625 | node->current_frame_host()->policy_container_host()) { |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3626 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 3627 | node->current_frame_host() |
| 3628 | ->policy_container_host() |
| 3629 | ->policies() |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 3630 | .ClonePtr()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3631 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | // navigation_ui_data should only be present for main frame navigations. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 3635 | DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3636 | |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3637 | // This will be used to set the Navigation Timing API navigationStart |
| 3638 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 3639 | // "Open link in new tab"). If the navigation must wait on the current |
| 3640 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 3641 | // will be updated when the BeforeUnload ack is received. |
| 3642 | const auto navigation_start_time = base::TimeTicks::Now(); |
| 3643 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3644 | std::unique_ptr<NavigationRequest> request = |
| 3645 | CreateNavigationRequestFromLoadParams( |
| 3646 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3647 | params.has_user_gesture, network::mojom::SourceLocation::New(), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3648 | reload_type, pending_entry_, pending_entry_->GetFrameEntry(node), |
| 3649 | navigation_start_time); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3650 | |
| 3651 | // If the navigation couldn't start, return immediately and discard the |
| 3652 | // pending NavigationEntry. |
| 3653 | if (!request) { |
| 3654 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3655 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3656 | } |
| 3657 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3658 | #if DCHECK_IS_ON() |
| 3659 | // Safety check that NavigationRequest and NavigationEntry match. |
| 3660 | ValidateRequestMatchesEntry(request.get(), pending_entry_); |
| 3661 | #endif |
| 3662 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3663 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3664 | CHECK(!in_navigate_to_pending_entry_); |
| 3665 | in_navigate_to_pending_entry_ = true; |
| 3666 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3667 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3668 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3669 | // function. |
| 3670 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3671 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3672 | base::WeakPtr<NavigationHandle> created_navigation_handle( |
| 3673 | request->GetWeakPtr()); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3674 | node->navigator().Navigate(std::move(request), reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3675 | |
| 3676 | in_navigate_to_pending_entry_ = false; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3677 | return created_navigation_handle; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3678 | } |
| 3679 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3680 | void NavigationControllerImpl::HandleRendererDebugURL( |
| 3681 | FrameTreeNode* frame_tree_node, |
| 3682 | const GURL& url) { |
| 3683 | if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3684 | // Any renderer-side debug URLs or javascript: URLs should be ignored if |
| 3685 | // the renderer process is not live, unless it is the initial navigation |
| 3686 | // of the tab. |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3687 | if (!IsInitialNavigation()) { |
| 3688 | DiscardNonCommittedEntries(); |
| 3689 | return; |
| 3690 | } |
Fergal Daly | ecd3b020 | 2020-06-25 01:57:37 | [diff] [blame] | 3691 | // The current frame is always a main frame. If IsInitialNavigation() is |
| 3692 | // true then there have been no navigations and any frames of this tab must |
| 3693 | // be in the same renderer process. If that has crashed then the only frame |
| 3694 | // that can be revived is the main frame. |
| 3695 | frame_tree_node->render_manager() |
| 3696 | ->InitializeMainRenderFrameForImmediateUse(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3697 | } |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3698 | |
| 3699 | // Several tests expect a load of Chrome Debug URLs to send a DidStopLoading |
| 3700 | // notification, so set is loading to true here to properly surface it when |
| 3701 | // the renderer process is done handling the URL. |
| 3702 | // TODO(crbug.com/1254130): Remove the test dependency on this behavior. |
| 3703 | if (!url.SchemeIs(url::kJavaScriptScheme)) { |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3704 | frame_tree_node->current_frame_host()->SetIsLoadingForRendererDebugURL(); |
Julie Jeongeun Kim | 50d124c | 2022-10-21 13:51:22 | [diff] [blame] | 3705 | } |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3706 | frame_tree_node->current_frame_host()->HandleRendererDebugURL(url); |
| 3707 | } |
| 3708 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3709 | std::unique_ptr<NavigationEntryImpl> |
| 3710 | NavigationControllerImpl::CreateNavigationEntryFromLoadParams( |
| 3711 | FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3712 | const LoadURLParams& params, |
| 3713 | bool override_user_agent, |
| 3714 | bool should_replace_current_entry, |
| 3715 | bool has_user_gesture) { |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3716 | // Browser initiated navigations might not have a blob_url_loader_factory set |
| 3717 | // in params even if the navigation is to a blob URL. If that happens, lookup |
| 3718 | // the correct url loader factory to use here. |
| 3719 | auto blob_url_loader_factory = params.blob_url_loader_factory; |
Kinuko Yasuda | 7d925ea2 | 2019-08-01 10:08:48 | [diff] [blame] | 3720 | if (!blob_url_loader_factory && params.url.SchemeIsBlob()) { |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3721 | // Resolve the blob URL in the storage partition associated with the target |
| 3722 | // frame. This is the storage partition the URL will be loaded in, and only |
| 3723 | // 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] | 3724 | blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl( |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3725 | node->current_frame_host()->GetStoragePartition(), params.url); |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3726 | } |
| 3727 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3728 | std::unique_ptr<NavigationEntryImpl> entry; |
Tommy C. Li | 03eee77a | 2019-02-05 02:07:44 | [diff] [blame] | 3729 | // extra_headers in params are \n separated; navigation entries want \r\n. |
| 3730 | std::string extra_headers_crlf; |
| 3731 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3732 | |
| 3733 | // For subframes, create a pending entry with a corresponding frame entry. |
| 3734 | if (!node->IsMainFrame()) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 3735 | entry = GetLastCommittedEntry()->Clone(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3736 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 3737 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3738 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get()), |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 3739 | params.url, absl::nullopt, params.referrer, params.initiator_origin, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 3740 | params.initiator_base_url, params.redirect_chain, blink::PageState(), |
| 3741 | "GET", -1, blob_url_loader_factory, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3742 | // If in NavigateWithoutEntry we later determine that this navigation is |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 3743 | // a conversion of a new navigation into a reload, we will set the right |
| 3744 | // document policies there. |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3745 | nullptr /* policy_container_policies */); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3746 | } else { |
| 3747 | // Otherwise, create a pending entry for the main frame. |
Julie Jeongeun Kim | 5b9aff7 | 2022-05-02 02:10:17 | [diff] [blame] | 3748 | // If `node` is the outermost main frame, it rewrites a virtual url in order |
| 3749 | // to adjust the original input url if needed. For inner frames such as |
| 3750 | // fenced frames or subframes, they don't rewrite urls as the urls are not |
| 3751 | // input urls by users. |
| 3752 | bool rewrite_virtual_urls = node->IsOutermostMainFrame(); |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 3753 | scoped_refptr<SiteInstance> source_site_instance = |
| 3754 | params.source_site_instance; |
| 3755 | absl::optional<GURL> source_process_site_url = absl::nullopt; |
| 3756 | if (source_site_instance && source_site_instance->HasProcess()) { |
| 3757 | source_process_site_url = |
| 3758 | source_site_instance->GetProcess()->GetProcessLock().site_url(); |
| 3759 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3760 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 3761 | params.url, params.referrer, params.initiator_origin, |
Sharon Yang | 242ef82 | 2023-05-15 21:07:32 | [diff] [blame] | 3762 | params.initiator_base_url, source_process_site_url, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 3763 | params.transition_type, params.is_renderer_initiated, |
| 3764 | extra_headers_crlf, browser_context_, blob_url_loader_factory, |
| 3765 | rewrite_virtual_urls)); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3766 | entry->set_source_site_instance( |
| 3767 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get())); |
| 3768 | entry->SetRedirectChain(params.redirect_chain); |
| 3769 | } |
| 3770 | |
| 3771 | // Set the FTN ID (only used in non-site-per-process, for tests). |
| 3772 | entry->set_frame_tree_node_id(node->frame_tree_node_id()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3773 | entry->set_should_clear_history_list(params.should_clear_history_list); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3774 | entry->SetIsOverridingUserAgent(override_user_agent); |
| 3775 | entry->set_has_user_gesture(has_user_gesture); |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3776 | entry->set_reload_type(params.reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3777 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3778 | switch (params.load_type) { |
| 3779 | case LOAD_TYPE_DEFAULT: |
| 3780 | break; |
| 3781 | case LOAD_TYPE_HTTP_POST: |
| 3782 | entry->SetHasPostData(true); |
| 3783 | entry->SetPostData(params.post_data); |
| 3784 | break; |
| 3785 | case LOAD_TYPE_DATA: |
| 3786 | entry->SetBaseURLForDataURL(params.base_url_for_data_url); |
| 3787 | entry->SetVirtualURL(params.virtual_url_for_data_url); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3788 | #if BUILDFLAG(IS_ANDROID) |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3789 | entry->SetDataURLAsString(params.data_url_as_string); |
| 3790 | #endif |
| 3791 | entry->SetCanLoadLocalResources(params.can_load_local_resources); |
| 3792 | break; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | // TODO(clamy): NavigationEntry is meant for information that will be kept |
| 3796 | // after the navigation ended and therefore is not appropriate for |
| 3797 | // started_from_context_menu. Move started_from_context_menu to |
| 3798 | // NavigationUIData. |
| 3799 | entry->set_started_from_context_menu(params.started_from_context_menu); |
| 3800 | |
| 3801 | return entry; |
| 3802 | } |
| 3803 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3804 | std::unique_ptr<NavigationRequest> |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3805 | NavigationControllerImpl::CreateNavigationRequestFromLoadParams( |
| 3806 | FrameTreeNode* node, |
| 3807 | const LoadURLParams& params, |
| 3808 | bool override_user_agent, |
| 3809 | bool should_replace_current_entry, |
| 3810 | bool has_user_gesture, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3811 | network::mojom::SourceLocationPtr source_location, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3812 | ReloadType reload_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3813 | NavigationEntryImpl* entry, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3814 | FrameNavigationEntry* frame_entry, |
Nan Lin | 944e9b4e | 2022-04-12 13:51:22 | [diff] [blame] | 3815 | base::TimeTicks navigation_start_time, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 3816 | bool is_embedder_initiated_fenced_frame_navigation, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 3817 | bool is_unfenced_top_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 3818 | bool is_container_initiated, |
| 3819 | absl::optional<std::u16string> embedder_shared_storage_context) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3820 | DCHECK_EQ(-1, GetIndexOfEntry(entry)); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3821 | DCHECK(frame_entry); |
Nasko Oskov | 3c2f9e25 | 2019-01-10 17:45:53 | [diff] [blame] | 3822 | // All renderer-initiated navigations must have an initiator_origin. |
| 3823 | DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value()); |
Camille Lamy | ff7c482 | 2018-11-07 15:42:51 | [diff] [blame] | 3824 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3825 | GURL url_to_load; |
| 3826 | GURL virtual_url; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3827 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3828 | // For main frames, rewrite the URL if necessary and compute the virtual URL |
| 3829 | // that should be shown in the address bar. |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 3830 | if (node->IsOutermostMainFrame()) { |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3831 | bool ignored_reverse_on_redirect = false; |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3832 | RewriteUrlForNavigation(params.url, browser_context_, &url_to_load, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3833 | &virtual_url, &ignored_reverse_on_redirect); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3834 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3835 | // For DATA loads, override the virtual URL. |
| 3836 | if (params.load_type == LOAD_TYPE_DATA) |
| 3837 | virtual_url = params.virtual_url_for_data_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3838 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3839 | if (virtual_url.is_empty()) |
| 3840 | virtual_url = url_to_load; |
| 3841 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3842 | CHECK(virtual_url == entry->GetVirtualURL()); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3843 | |
Aran Gilman | 249eb12 | 2019-12-02 23:32:46 | [diff] [blame] | 3844 | // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic |
| 3845 | // behavior: it is possible for two calls to RewriteUrlForNavigation to |
| 3846 | // return different results, leading to a different URL in the |
| 3847 | // NavigationRequest and FrameEntry. This will be fixed once we remove the |
| 3848 | // pending NavigationEntry, as we'll only make one call to |
| 3849 | // RewriteUrlForNavigation. |
| 3850 | VLOG_IF(1, (url_to_load != frame_entry->url())) |
| 3851 | << "NavigationRequest and FrameEntry have different URLs: " |
| 3852 | << url_to_load << " vs " << frame_entry->url(); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3853 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3854 | // TODO(clamy): In order to remove the pending NavigationEntry, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3855 | // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3856 | // NavigationRequest. |
| 3857 | } else { |
| 3858 | url_to_load = params.url; |
| 3859 | virtual_url = params.url; |
Camille Lamy | f664f762 | 2019-01-07 19:28:24 | [diff] [blame] | 3860 | CHECK(!frame_entry || url_to_load == frame_entry->url()); |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3861 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3862 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 3863 | if (node->render_manager()->is_attaching_inner_delegate()) { |
| 3864 | // Avoid starting any new navigations since this node is now preparing for |
| 3865 | // attaching an inner delegate. |
| 3866 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 3867 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3868 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 3869 | if (!IsValidURLForNavigation(node, virtual_url, url_to_load)) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3870 | return nullptr; |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 3871 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3872 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3873 | // Look for a pending commit that is to another document in this |
| 3874 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 3875 | // current URL by the time this navigation commits. |
| 3876 | bool has_pending_cross_document_commit = |
| 3877 | node->render_manager()->HasPendingCommitForCrossDocumentNavigation(); |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 3878 | bool is_currently_error_page = node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3879 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3880 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3881 | /*old_url=*/node->current_url(), |
| 3882 | /*new_url=*/url_to_load, reload_type, entry, *frame_entry, |
| 3883 | has_pending_cross_document_commit, is_currently_error_page, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 3884 | /*is_same_document_history_load=*/false, |
| 3885 | is_embedder_initiated_fenced_frame_navigation, |
| 3886 | is_unfenced_top_navigation); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3887 | |
| 3888 | // Create the NavigationParams based on |params|. |
| 3889 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 3890 | bool is_view_source_mode = entry->IsViewSourceMode(); |
| 3891 | DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme)); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3892 | |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 3893 | blink::NavigationDownloadPolicy download_policy = params.download_policy; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 3894 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 3895 | // Update |download_policy| if the virtual URL is view-source. |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3896 | if (is_view_source_mode) |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 3897 | download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3898 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3899 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 3900 | blink::mojom::CommonNavigationParams::New( |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 3901 | url_to_load, params.initiator_origin, params.initiator_base_url, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3902 | blink::mojom::Referrer::New(params.referrer.url, |
| 3903 | params.referrer.policy), |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 3904 | params.transition_type, navigation_type, download_policy, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3905 | should_replace_current_entry, params.base_url_for_data_url, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3906 | navigation_start_time, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3907 | params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET", |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3908 | params.post_data, std::move(source_location), |
arthursonzogni | af7c62c5 | 2020-02-12 10:49:41 | [diff] [blame] | 3909 | params.started_from_context_menu, has_user_gesture, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 3910 | false /* has_text_fragment_token */, |
| 3911 | network::mojom::CSPDisposition::CHECK, std::vector<int>(), |
| 3912 | params.href_translate, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3913 | false /* is_history_navigation_in_new_child_frame */, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 3914 | params.input_start, network::mojom::RequestDestination::kEmpty); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3915 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3916 | blink::mojom::CommitNavigationParamsPtr commit_params = |
| 3917 | blink::mojom::CommitNavigationParams::New( |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 3918 | absl::nullopt, |
Ari Chivukula | 43d2cf8 | 2023-01-24 03:16:07 | [diff] [blame] | 3919 | // The correct storage key and session storage key will be computed |
| 3920 | // before committing the navigation. |
| 3921 | blink::StorageKey(), blink::StorageKey(), override_user_agent, |
| 3922 | params.redirect_chain, |
Lucas Furukawa Gadani | 81e294b | 2019-08-29 16:26:32 | [diff] [blame] | 3923 | std::vector<network::mojom::URLResponseHeadPtr>(), |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 3924 | std::vector<net::RedirectInfo>(), params.post_content_type, |
| 3925 | common_params->url, common_params->method, |
| 3926 | params.can_load_local_resources, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3927 | frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3928 | entry->GetSubframeUniqueNames(node), |
| 3929 | /*intended_as_new_entry=*/true, |
| 3930 | /*pending_history_list_offset=*/-1, |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3931 | params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(), |
| 3932 | params.should_clear_history_list ? 0 : GetEntryCount(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3933 | /*was_discarded=*/false, is_view_source_mode, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3934 | params.should_clear_history_list, |
| 3935 | blink::mojom::NavigationTiming::New(), |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 3936 | blink::mojom::WasActivatedOption::kUnknown, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3937 | /*navigation_token=*/base::UnguessableToken::Create(), |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 3938 | std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(), |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3939 | #if BUILDFLAG(IS_ANDROID) |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3940 | /*data_url_as_string=*/std::string(), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3941 | #endif |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3942 | /*is_browser_initiated=*/!params.is_renderer_initiated, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3943 | /*document_ukm_source_id=*/ukm::kInvalidSourceId, |
Jiewei Qian | 0406fc0 | 2020-03-09 06:02:07 | [diff] [blame] | 3944 | node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3945 | /*force_enabled_origin_trials=*/std::vector<std::string>(), |
| 3946 | /*origin_agent_cluster=*/false, |
| 3947 | /*origin_agent_cluster_left_as_default=*/true, |
| 3948 | /*enabled_client_hints=*/ |
| 3949 | std::vector<network::mojom::WebClientHintsType>(), |
| 3950 | /*is_cross_browsing_instance=*/false, /*old_page_info=*/nullptr, |
| 3951 | /*http_response_code=*/-1, |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 3952 | blink::mojom::NavigationApiHistoryEntryArrays::New(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3953 | /*early_hints_preloaded_resources=*/std::vector<GURL>(), |
Clark DuVall | 8ee487a2 | 2021-11-10 02:25:58 | [diff] [blame] | 3954 | // This timestamp will be populated when the commit IPC is sent. |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3955 | /*commit_sent=*/base::TimeTicks(), /*srcdoc_value=*/std::string(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3956 | /*should_load_data_url=*/false, |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 3957 | /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3958 | /*reduced_accept_language=*/std::string(), |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 3959 | /*navigation_delivery_type=*/ |
Khushal Sagar | 7b26135c6 | 2022-11-08 20:25:42 | [diff] [blame] | 3960 | network::mojom::NavigationDeliveryType::kDefault, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 3961 | /*view_transition_state=*/absl::nullopt, |
sbingler | a07ae73 | 2022-12-02 20:49:05 | [diff] [blame] | 3962 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
| 3963 | /*modified_runtime_features=*/ |
Jiewei Qian | 87951f5 | 2023-10-17 01:33:46 | [diff] [blame] | 3964 | base::flat_map<::blink::mojom::RuntimeFeature, bool>(), |
Yuzu Saijo | 29f96ca9 | 2022-12-08 04:54:12 | [diff] [blame] | 3965 | /*fenced_frame_properties=*/absl::nullopt, |
Chris Fredrickson | b52bcd0 | 2023-03-28 14:48:05 | [diff] [blame] | 3966 | /*not_restored_reasons=*/nullptr, |
Arthur Hemery | d51484b | 2023-06-02 15:50:00 | [diff] [blame] | 3967 | /*load_with_storage_access=*/false, |
Kouhei Ueno | a0297b0b | 2023-06-15 05:39:48 | [diff] [blame] | 3968 | /*browsing_context_group_info=*/absl::nullopt, |
Nan Lin | da394ba | 2023-11-03 21:17:37 | [diff] [blame] | 3969 | /*lcpp_hint=*/nullptr, blink::CreateDefaultRendererContentSettings(), |
| 3970 | /*cookie_deprecation_label=*/absl::nullopt); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3971 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3972 | if (ValidateDataURLAsString(params.data_url_as_string)) { |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3973 | commit_params->data_url_as_string = params.data_url_as_string->data(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3974 | } |
| 3975 | #endif |
| 3976 | |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3977 | commit_params->was_activated = params.was_activated; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 3978 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 3979 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 3980 | node, params.suggested_system_entropy); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3981 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3982 | // extra_headers in params are \n separated; NavigationRequests want \r\n. |
| 3983 | std::string extra_headers_crlf; |
| 3984 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3985 | |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 3986 | auto navigation_request = NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3987 | node, std::move(common_params), std::move(commit_params), |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 3988 | !params.is_renderer_initiated, params.was_opener_suppressed, |
Hiroshige Hayashizaki | f07ad781 | 2023-05-10 02:26:09 | [diff] [blame] | 3989 | params.initiator_frame_token, params.initiator_process_id, |
| 3990 | extra_headers_crlf, frame_entry, entry, params.is_form_submission, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 3991 | params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 3992 | params.impression, params.initiator_activation_and_ad_status, |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 3993 | params.is_pdf, is_embedder_initiated_fenced_frame_navigation, |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 3994 | is_container_initiated, embedder_shared_storage_context); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3995 | navigation_request->set_from_download_cross_origin_redirect( |
| 3996 | params.from_download_cross_origin_redirect); |
W. James MacLean | 00568d7 | 2022-02-24 19:36:55 | [diff] [blame] | 3997 | navigation_request->set_force_new_browsing_instance( |
| 3998 | params.force_new_browsing_instance); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3999 | return navigation_request; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | std::unique_ptr<NavigationRequest> |
| 4003 | NavigationControllerImpl::CreateNavigationRequestFromEntry( |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4004 | FrameTreeNode* frame_tree_node, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4005 | NavigationEntryImpl* entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4006 | FrameNavigationEntry* frame_entry, |
| 4007 | ReloadType reload_type, |
| 4008 | bool is_same_document_history_load, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4009 | bool is_history_navigation_in_new_child_frame, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4010 | const absl::optional<blink::LocalFrameToken>& initiator_frame_token, |
| 4011 | int initiator_process_id, |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4012 | absl::optional<blink::scheduler::TaskAttributionId> |
| 4013 | soft_navigation_heuristics_task_id) { |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 4014 | DCHECK(frame_entry); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4015 | GURL dest_url = frame_entry->url(); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4016 | // We should never navigate to an existing initial NavigationEntry that is the |
| 4017 | // initial NavigationEntry for the initial empty document that hasn't been |
| 4018 | // overridden by the synchronous about:blank commit, to preserve previous |
| 4019 | // behavior where trying to reload when the main frame is on the initial empty |
| 4020 | // document won't result in a navigation. |
| 4021 | // See also https://crbug.com/1277414. |
| 4022 | DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank()); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 4023 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4024 | Referrer dest_referrer = frame_entry->referrer(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4025 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 4026 | if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) { |
| 4027 | // Avoid starting any new navigations since this node is now preparing for |
| 4028 | // attaching an inner delegate. |
| 4029 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 4030 | } |
| 4031 | |
Alex Moshchuk | 9924283 | 2023-05-22 17:21:44 | [diff] [blame] | 4032 | if (!IsValidURLForNavigation(frame_tree_node, entry->GetVirtualURL(), |
| 4033 | dest_url)) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4034 | return nullptr; |
| 4035 | } |
| 4036 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4037 | // This will be used to set the Navigation Timing API navigationStart |
| 4038 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 4039 | // "Open link in new tab"). If the navigation must wait on the current |
| 4040 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 4041 | // will be updated when the BeforeUnload ack is received. |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4042 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4043 | base::TimeTicks navigation_start = base::TimeTicks::Now(); |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4044 | const auto navigation_start_system_entropy = |
| 4045 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4046 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4047 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4048 | // Look for a pending commit that is to another document in this |
| 4049 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 4050 | // current URL by the time this navigation commits. |
| 4051 | bool has_pending_cross_document_commit = |
| 4052 | frame_tree_node->render_manager() |
| 4053 | ->HasPendingCommitForCrossDocumentNavigation(); |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4054 | bool is_currently_error_page = |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 4055 | frame_tree_node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4056 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4057 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 4058 | /*old_url=*/frame_tree_node->current_url(), |
| 4059 | /*new_url=*/dest_url, reload_type, entry, *frame_entry, |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 4060 | has_pending_cross_document_commit, is_currently_error_page, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4061 | is_same_document_history_load, |
Garrett Tanzer | 267c2b8 | 2022-07-26 16:53:13 | [diff] [blame] | 4062 | /*is_embedder_initiated_fenced_frame_navigation=*/false, |
Garrett Tanzer | 405f340 | 2022-07-21 20:12:49 | [diff] [blame] | 4063 | /*is_unfenced_top_navigation=*/false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4064 | |
| 4065 | // A form submission may happen here if the navigation is a |
| 4066 | // back/forward/reload navigation that does a form resubmission. |
| 4067 | scoped_refptr<network::ResourceRequestBody> request_body; |
| 4068 | std::string post_content_type; |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4069 | // TODO(https://crbug.com/931209) Store |is_form_submission| in the history |
| 4070 | // entry. This way, it could be directly retrieved here. Right now, it is only |
| 4071 | // partially recovered when request.method == "POST" and request.body exists. |
| 4072 | bool is_form_submission = false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4073 | if (frame_entry->method() == "POST") { |
| 4074 | request_body = frame_entry->GetPostData(&post_content_type); |
| 4075 | // Might have a LF at end. |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 4076 | post_content_type = std::string( |
| 4077 | base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)); |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4078 | is_form_submission = !!request_body; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | // Create the NavigationParams based on |entry| and |frame_entry|. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4082 | blink::mojom::CommonNavigationParamsPtr common_params = |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4083 | entry->ConstructCommonNavigationParams( |
| 4084 | *frame_entry, request_body, dest_url, |
| 4085 | blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy), |
Tarun Bansal | bcd62c8 | 2022-01-18 17:27:38 | [diff] [blame] | 4086 | navigation_type, navigation_start, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 4087 | base::TimeTicks() /* input_start */); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 4088 | common_params->is_history_navigation_in_new_child_frame = |
Arthur Hemery | bee4a75 | 2019-05-29 10:50:55 | [diff] [blame] | 4089 | is_history_navigation_in_new_child_frame; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4090 | |
| 4091 | // TODO(clamy): |intended_as_new_entry| below should always be false once |
| 4092 | // Reload no longer leads to this being called for a pending NavigationEntry |
| 4093 | // of index -1. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4094 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4095 | entry->ConstructCommitNavigationParams( |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4096 | *frame_entry, common_params->url, common_params->method, |
| 4097 | entry->GetSubframeUniqueNames(frame_tree_node), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4098 | GetPendingEntryIndex() == -1 /* intended_as_new_entry */, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 4099 | GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(), |
Liam Brady | d2a41e15 | 2022-07-19 13:58:48 | [diff] [blame] | 4100 | frame_tree_node->pending_frame_policy(), |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4101 | frame_tree_node->AncestorOrSelfHasCSPEE(), |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4102 | navigation_start_system_entropy, soft_navigation_heuristics_task_id); |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4103 | commit_params->post_content_type = post_content_type; |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4104 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4105 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4106 | frame_tree_node, blink::mojom::SystemEntropy::kNormal); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 4107 | |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4108 | if (common_params->url.IsAboutSrcdoc()) { |
| 4109 | // TODO(wjmaclean): initialize this in NavigationRequest's constructor |
| 4110 | // instead. |
W. James MacLean | 81b8d01f | 2022-01-25 20:50:59 | [diff] [blame] | 4111 | commit_params->srcdoc_value = frame_tree_node->srcdoc_value(); |
W. James MacLean | b7d609268 | 2022-10-05 15:23:26 | [diff] [blame] | 4112 | } |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4113 | const bool is_browser_initiated = !initiator_frame_token; |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 4114 | return NavigationRequest::Create( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 4115 | frame_tree_node, std::move(common_params), std::move(commit_params), |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 4116 | is_browser_initiated, false /* was_opener_suppressed */, |
Hiroshige Hayashizaki | 5466bfe8 | 2023-05-17 00:34:33 | [diff] [blame] | 4117 | initiator_frame_token, initiator_process_id, entry->extra_headers(), |
| 4118 | frame_entry, entry, is_form_submission, nullptr /* navigation_ui_data */, |
| 4119 | absl::nullopt /* impression */, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4120 | blink::mojom::NavigationInitiatorActivationAndAdStatus:: |
| 4121 | kDidNotStartWithTransientActivation, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 4122 | false /* is_pdf */); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 4123 | } |
| 4124 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4125 | void NavigationControllerImpl::NotifyNavigationEntryCommitted( |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4126 | LoadCommittedDetails* details) { |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 4127 | details->entry = GetLastCommittedEntry(); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4128 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 4129 | // We need to notify the ssl_manager_ before the WebContents so the |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4130 | // location bar will have up-to-date information about the security style |
| 4131 | // when it wants to draw. See http://crbug.com/11157 |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 4132 | ssl_manager_.DidCommitProvisionalLoad(*details); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 4133 | |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4134 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 4135 | delegate_->NotifyNavigationEntryCommitted(*details); |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 4136 | |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 4137 | // TODO(avi): Remove. http://crbug.com/170921 |
| 4138 | NotificationDetails notification_details = |
| 4139 | Details<LoadCommittedDetails>(details); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4140 | NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 4141 | Source<NavigationController>(this), |
| 4142 | notification_details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4143 | } |
| 4144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4145 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4146 | size_t NavigationControllerImpl::max_entry_count() { |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4147 | if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4148 | return max_entry_count_for_testing_; |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 4149 | return blink::kMaxSessionHistoryEntries; |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 4150 | } |
| 4151 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4152 | void NavigationControllerImpl::SetActive(bool is_active) { |
[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 4153 | if (is_active && needs_reload_) |
| 4154 | LoadIfNecessary(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4155 | } |
| 4156 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4157 | void NavigationControllerImpl::LoadIfNecessary() { |
Rakina Zata Amni | d605d46 | 2022-06-01 10:17:03 | [diff] [blame] | 4158 | SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending", |
| 4159 | !!pending_entry_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4160 | if (!needs_reload_) |
| 4161 | return; |
| 4162 | |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 4163 | UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType", |
| 4164 | needs_reload_type_); |
| 4165 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4166 | // Calling Reload() results in ignoring state, and not loading. |
| 4167 | // Explicitly use NavigateToPendingEntry so that the renderer uses the |
| 4168 | // cached state. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4169 | if (pending_entry_) { |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4170 | NavigateToExistingPendingEntry( |
| 4171 | ReloadType::NONE, |
| 4172 | /*initiator_rfh=*/nullptr, |
| 4173 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
| 4174 | /*navigation_api_key=*/nullptr); |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4175 | } else if (!GetLastCommittedEntry() |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4176 | ->IsInitialEntryNotForSynchronousAboutBlank()) { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4177 | pending_entry_ = entries_[last_committed_entry_index_].get(); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4178 | pending_entry_index_ = last_committed_entry_index_; |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4179 | NavigateToExistingPendingEntry( |
| 4180 | ReloadType::NONE, |
| 4181 | /*initiator_rfh=*/nullptr, |
| 4182 | /*soft_navigation_heuristics_task_id=*/absl::nullopt, |
| 4183 | /*navigation_api_key=*/nullptr); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4184 | } else { |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 4185 | // We should never navigate to an existing initial NavigationEntry that is |
| 4186 | // the initial NavigationEntry for the initial empty document that hasn't |
| 4187 | // been overridden by the synchronous about:blank commit, to preserve |
| 4188 | // legacy behavior where trying to reload when the main frame is on the |
| 4189 | // initial empty document won't result in a navigation. See also |
| 4190 | // https://crbug.com/1277414. If there is something to reload, the |
| 4191 | // successful reload will clear the |needs_reload_| flag. Otherwise, just do |
| 4192 | // it here. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 4193 | needs_reload_ = false; |
| 4194 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4195 | } |
| 4196 | |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4197 | base::WeakPtr<NavigationHandle> |
| 4198 | NavigationControllerImpl::LoadPostCommitErrorPage( |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4199 | RenderFrameHost* render_frame_host, |
| 4200 | const GURL& url, |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 4201 | const std::string& error_page_html) { |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4202 | RenderFrameHostImpl* rfhi = |
| 4203 | static_cast<RenderFrameHostImpl*>(render_frame_host); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 4204 | |
| 4205 | // Only active documents can load post-commit error pages: |
| 4206 | // - If the document is in pending deletion, the browser already committed to |
| 4207 | // destroying this RenderFrameHost so ignore loading the error page. |
| 4208 | // - If the document is in back-forward cache, it's not allowed to navigate as |
| 4209 | // it should remain frozen. Ignore the request and evict the document from |
| 4210 | // back-forward cache. |
| 4211 | // - If the document is prerendering, it can navigate but when loading error |
| 4212 | // pages, cancel prerendering. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 4213 | if (rfhi->IsInactiveAndDisallowActivation( |
| 4214 | DisallowActivationReasonId::kLoadPostCommitErrorPage)) { |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4215 | return nullptr; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 4216 | } |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 4217 | |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4218 | FrameTreeNode* node = rfhi->frame_tree_node(); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4219 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4220 | blink::mojom::CommonNavigationParamsPtr common_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 4221 | blink::CreateCommonNavigationParams(); |
Rakina Zata Amni | d2da154 | 2020-12-23 00:52:59 | [diff] [blame] | 4222 | // |url| might be empty, such as when LoadPostCommitErrorPage happens before |
| 4223 | // the frame actually committed (e.g. iframe with "src" set to a |
| 4224 | // slow-responding URL). We should rewrite the URL to about:blank in this |
| 4225 | // case, as the renderer will only think a page is an error page if it has a |
| 4226 | // non-empty unreachable URL. |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 4227 | common_params->url = url.is_empty() ? GURL("about:blank") : url; |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 4228 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 4229 | blink::CreateCommitNavigationParams(); |
Antonio Sartori | 58591c89 | 2021-04-21 06:54:33 | [diff] [blame] | 4230 | commit_params->original_url = common_params->url; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4231 | |
Mike Jackson | e2aa7af | 2023-05-17 06:45:07 | [diff] [blame] | 4232 | commit_params->navigation_timing->system_entropy_at_navigation_start = |
| 4233 | SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode( |
| 4234 | node, blink::mojom::SystemEntropy::kNormal); |
| 4235 | |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 4236 | // TODO(arthursonzogni): Consider providing the minimal capabilities to the |
| 4237 | // error pages. |
Liam Brady | 34639ec9 | 2023-10-13 19:50:45 | [diff] [blame] | 4238 | commit_params->frame_policy = node->pending_frame_policy(); |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 4239 | |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4240 | std::unique_ptr<NavigationRequest> navigation_request = |
| 4241 | NavigationRequest::CreateBrowserInitiated( |
| 4242 | node, std::move(common_params), std::move(commit_params), |
Hiroshige Hayashizaki | f07ad781 | 2023-05-10 02:26:09 | [diff] [blame] | 4243 | false /* was_opener_suppressed */, "" /* extra_headers */, |
| 4244 | nullptr /* frame_entry */, nullptr /* entry */, |
| 4245 | false /* is_form_submission */, nullptr /* navigation_ui_data */, |
| 4246 | absl::nullopt /* impression */, false /* is_pdf */); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4247 | navigation_request->set_post_commit_error_page_html(error_page_html); |
Lei Zhang | a477083 | 2023-07-19 18:02:36 | [diff] [blame] | 4248 | navigation_request->set_net_error(net::ERR_BLOCKED_BY_CLIENT); |
Charlie Reis | 09952ee | 2022-12-08 16:35:07 | [diff] [blame] | 4249 | node->TakeNavigationRequest(std::move(navigation_request)); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4250 | DCHECK(node->navigation_request()); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4251 | |
| 4252 | // Calling BeginNavigation may destroy the NavigationRequest. |
| 4253 | base::WeakPtr<NavigationRequest> created_navigation_request( |
| 4254 | node->navigation_request()->GetWeakPtr()); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4255 | node->navigation_request()->BeginNavigation(); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4256 | return created_navigation_request; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4257 | } |
| 4258 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4259 | void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4260 | EntryChangedDetails det; |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 4261 | det.changed_entry = entry; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4262 | det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry)); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 4263 | delegate_->NotifyNavigationEntryChanged(det); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4264 | } |
| 4265 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4266 | void NavigationControllerImpl::FinishRestore(int selected_index, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4267 | RestoreType type) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4268 | // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would |
| 4269 | // represent a no-committed-entry state. |
| 4270 | DCHECK(selected_index >= -1 && selected_index < GetEntryCount()); |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4271 | ConfigureEntriesForRestore(&entries_, type); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4272 | // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry |
| 4273 | // if `entries_` contains pre-existing entries from the NavigationController |
| 4274 | // before restore, which would not be removed and will be at the front of the |
| 4275 | // entries list, causing the index to be off by the amount of pre-existing |
| 4276 | // entries in the list. Fix this to point to the correct entry. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4277 | last_committed_entry_index_ = selected_index; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4278 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 4279 | |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 4280 | void NavigationControllerImpl::DiscardNonCommittedEntries() { |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 4281 | DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */); |
| 4282 | } |
| 4283 | |
| 4284 | void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails( |
| 4285 | LoadCommittedDetails* commit_details) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4286 | // Avoid sending a notification if there is nothing to discard. |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4287 | // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help |
| 4288 | // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570. |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 4289 | if (!pending_entry_ && failed_pending_entry_id_ == 0) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4290 | return; |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4291 | } |
avi | 45a7253 | 2015-04-07 21:01:45 | [diff] [blame] | 4292 | DiscardPendingEntry(false); |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 4293 | |
| 4294 | if (!delegate_) |
| 4295 | return; |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4296 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL); |
[email protected] | b12eb22 | 2013-09-10 00:11:48 | [diff] [blame] | 4297 | } |
| 4298 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 4299 | int NavigationControllerImpl::GetEntryIndexWithUniqueID( |
| 4300 | int nav_entry_id) const { |
| 4301 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
| 4302 | if (entries_[i]->GetUniqueID() == nav_entry_id) |
| 4303 | return i; |
| 4304 | } |
| 4305 | return -1; |
| 4306 | } |
| 4307 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4308 | void NavigationControllerImpl::InsertEntriesFrom( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4309 | NavigationControllerImpl* source, |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4310 | int max_index) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4311 | DCHECK_LE(max_index, source->GetEntryCount()); |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4312 | NavigationEntryRestoreContextImpl context; |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4313 | for (int i = 0; i < max_index; i++) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4314 | // Normally, cloning a NavigationEntryImpl results in sharing |
| 4315 | // FrameNavigationEntries between the original and the clone. However, when |
| 4316 | // cloning from a different NavigationControllerImpl, we want to fork the |
| 4317 | // FrameNavigationEntries. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4318 | entries_.insert(entries_.begin() + i, |
Kevin McNee | 433daf24 | 2023-10-31 20:15:59 | [diff] [blame] | 4319 | source->entries_[i]->CloneWithoutSharing(&context)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4320 | } |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4321 | DCHECK_GE(entries_.size(), 1u); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4322 | DCHECK(pending_entry_index_ == -1 || |
| 4323 | pending_entry_ == GetEntryAtIndex(pending_entry_index_)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4324 | } |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4325 | |
| 4326 | void NavigationControllerImpl::SetGetTimestampCallbackForTest( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 4327 | const base::RepeatingCallback<base::Time()>& get_timestamp_callback) { |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4328 | get_timestamp_callback_ = get_timestamp_callback; |
| 4329 | } |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4330 | |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4331 | // History manipulation intervention: |
| 4332 | void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4333 | bool replace_entry, |
| 4334 | bool previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4335 | bool is_renderer_initiated, |
| 4336 | ukm::SourceId previous_page_load_ukm_source_id) { |
Shivani Sharma | 712d5d7 | 2019-04-16 21:56:45 | [diff] [blame] | 4337 | // Note that for a subframe, previous_document_was_activated is true if the |
| 4338 | // gesture happened in any subframe (propagated to main frame) or in the main |
| 4339 | // frame itself. |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4340 | if (replace_entry || previous_document_was_activated || |
shivanigithub | cceeacf | 2020-03-06 20:00:27 | [diff] [blame] | 4341 | !is_renderer_initiated) { |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4342 | return; |
| 4343 | } |
| 4344 | if (last_committed_entry_index_ == -1) |
| 4345 | return; |
| 4346 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4347 | SetSkippableForSameDocumentEntries(last_committed_entry_index_, true); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4348 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4349 | // Log UKM with the URL we are navigating away from. |
| 4350 | ukm::builders::HistoryManipulationIntervention( |
| 4351 | previous_page_load_ukm_source_id) |
| 4352 | .Record(ukm::UkmRecorder::Get()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4353 | } |
| 4354 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4355 | void NavigationControllerImpl::SetSkippableForSameDocumentEntries( |
| 4356 | int reference_index, |
| 4357 | bool skippable) { |
| 4358 | auto* reference_entry = GetEntryAtIndex(reference_index); |
| 4359 | reference_entry->set_should_skip_on_back_forward_ui(skippable); |
| 4360 | |
| 4361 | int64_t document_sequence_number = |
| 4362 | reference_entry->root_node()->frame_entry->document_sequence_number(); |
| 4363 | for (int index = 0; index < GetEntryCount(); index++) { |
| 4364 | auto* entry = GetEntryAtIndex(index); |
| 4365 | if (entry->root_node()->frame_entry->document_sequence_number() == |
| 4366 | document_sequence_number) { |
| 4367 | entry->set_should_skip_on_back_forward_ui(skippable); |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4372 | std::unique_ptr<NavigationControllerImpl::PendingEntryRef> |
| 4373 | NavigationControllerImpl::ReferencePendingEntry() { |
| 4374 | DCHECK(pending_entry_); |
| 4375 | auto pending_entry_ref = |
| 4376 | std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr()); |
| 4377 | pending_entry_refs_.insert(pending_entry_ref.get()); |
| 4378 | return pending_entry_ref; |
| 4379 | } |
| 4380 | |
| 4381 | void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) { |
| 4382 | // Ignore refs that don't correspond to the current pending entry. |
| 4383 | auto it = pending_entry_refs_.find(ref); |
| 4384 | if (it == pending_entry_refs_.end()) |
| 4385 | return; |
| 4386 | pending_entry_refs_.erase(it); |
| 4387 | |
| 4388 | if (!pending_entry_refs_.empty()) |
| 4389 | return; |
| 4390 | |
| 4391 | // The pending entry may be deleted before the last PendingEntryRef. |
| 4392 | if (!pending_entry_) |
| 4393 | return; |
| 4394 | |
| 4395 | // We usually clear the pending entry when the matching NavigationRequest |
| 4396 | // fails, so that an arbitrary URL isn't left visible above a committed page. |
| 4397 | // |
| 4398 | // However, we do preserve the pending entry in some cases, such as on the |
| 4399 | // initial navigation of an unmodified blank tab. We also allow the delegate |
| 4400 | // to say when it's safe to leave aborted URLs in the omnibox, to let the |
| 4401 | // user edit the URL and try again. This may be useful in cases that the |
| 4402 | // committed page cannot be attacker-controlled. In these cases, we still |
| 4403 | // allow the view to clear the pending entry and typed URL if the user |
| 4404 | // requests (e.g., hitting Escape with focus in the address bar). |
| 4405 | // |
| 4406 | // Do not leave the pending entry visible if it has an invalid URL, since this |
| 4407 | // might be formatted in an unexpected or unsafe way. |
| 4408 | // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824. |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4409 | bool should_preserve_entry = |
| 4410 | (pending_entry_ == GetVisibleEntry()) && |
| 4411 | pending_entry_->GetURL().is_valid() && |
| 4412 | (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs()); |
| 4413 | if (should_preserve_entry) |
| 4414 | return; |
| 4415 | |
| 4416 | DiscardPendingEntry(true); |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4417 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4418 | } |
| 4419 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4420 | std::unique_ptr<PolicyContainerPolicies> |
| 4421 | NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry( |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4422 | RenderFrameHostImpl* rfh, |
| 4423 | bool is_same_document, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4424 | const GURL& url) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4425 | if (is_same_document) { |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4426 | DCHECK(GetLastCommittedEntry()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4427 | FrameNavigationEntry* previous_frame_entry = |
| 4428 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
| 4429 | |
| 4430 | // TODO(https://crbug.com/608402): Remove this nullptr check when we can |
| 4431 | // ensure we always have a FrameNavigationEntry here. |
| 4432 | if (!previous_frame_entry) |
| 4433 | return nullptr; |
| 4434 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4435 | const PolicyContainerPolicies* previous_policies = |
| 4436 | previous_frame_entry->policy_container_policies(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4437 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4438 | if (!previous_policies) |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4439 | return nullptr; |
| 4440 | |
| 4441 | // Make a copy of the policy container for the new FrameNavigationEntry. |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4442 | return previous_policies->ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4443 | } |
| 4444 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 4445 | return rfh->policy_container_host()->policies().ClonePtr(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4446 | } |
| 4447 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4448 | void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() { |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4449 | OPTIONAL_TRACE_EVENT2( |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4450 | "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength", |
| 4451 | "history_offset", GetLastCommittedEntryIndex(), "history_length", |
| 4452 | GetEntryCount()); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4453 | |
| 4454 | auto callback = base::BindRepeating( |
| 4455 | [](int history_offset, int history_length, RenderViewHostImpl* rvh) { |
| 4456 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) { |
| 4457 | broadcast->SetHistoryOffsetAndLength(history_offset, history_length); |
| 4458 | } |
| 4459 | }, |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4460 | GetLastCommittedEntryIndex(), GetEntryCount()); |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4461 | frame_tree_->root()->render_manager()->ExecutePageBroadcastMethod(callback); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | void NavigationControllerImpl::DidAccessInitialMainDocument() { |
| 4465 | // We may have left a failed browser-initiated navigation in the address bar |
| 4466 | // to let the user edit it and try again. Clear it now that content might |
| 4467 | // show up underneath it. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4468 | if (!frame_tree_->IsLoadingIncludingInnerFrameTrees() && GetPendingEntry()) |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4469 | DiscardPendingEntry(false); |
| 4470 | |
| 4471 | // Update the URL display. |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 4472 | delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | void NavigationControllerImpl::UpdateStateForFrame( |
| 4476 | RenderFrameHostImpl* rfhi, |
| 4477 | const blink::PageState& page_state) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4478 | OPTIONAL_TRACE_EVENT1("content", |
| 4479 | "NavigationControllerImpl::UpdateStateForFrame", |
| 4480 | "render_frame_host", rfhi); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4481 | // The state update affects the last NavigationEntry associated with the given |
| 4482 | // |render_frame_host|. This may not be the last committed NavigationEntry (as |
| 4483 | // in the case of an UpdateState from a frame being swapped out). We track |
| 4484 | // which entry this is in the RenderFrameHost's nav_entry_id. |
| 4485 | NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id()); |
| 4486 | if (!entry) |
| 4487 | return; |
| 4488 | |
| 4489 | FrameNavigationEntry* frame_entry = |
| 4490 | entry->GetFrameEntry(rfhi->frame_tree_node()); |
| 4491 | if (!frame_entry) |
| 4492 | return; |
| 4493 | |
| 4494 | // The SiteInstance might not match if we do a cross-process navigation with |
| 4495 | // replacement (e.g., auto-subframe), in which case the swap out of the old |
| 4496 | // RenderFrameHost runs in the background after the old FrameNavigationEntry |
| 4497 | // has already been replaced and destroyed. |
| 4498 | if (frame_entry->site_instance() != rfhi->GetSiteInstance()) |
| 4499 | return; |
| 4500 | |
| 4501 | if (page_state == frame_entry->page_state()) |
| 4502 | return; // Nothing to update. |
| 4503 | |
| 4504 | DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState."; |
| 4505 | |
| 4506 | // The document_sequence_number and item_sequence_number recorded in the |
| 4507 | // FrameNavigationEntry should not differ from the one coming with the update, |
| 4508 | // since it must come from the same document. Do not update it if a difference |
| 4509 | // is detected, as this indicates that |frame_entry| is not the correct one. |
| 4510 | blink::ExplodedPageState exploded_state; |
| 4511 | if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state)) |
| 4512 | return; |
| 4513 | |
| 4514 | if (exploded_state.top.document_sequence_number != |
| 4515 | frame_entry->document_sequence_number() || |
| 4516 | exploded_state.top.item_sequence_number != |
| 4517 | frame_entry->item_sequence_number()) { |
| 4518 | return; |
| 4519 | } |
| 4520 | |
| 4521 | frame_entry->SetPageState(page_state); |
| 4522 | NotifyEntryChanged(entry); |
| 4523 | } |
| 4524 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4525 | namespace { |
| 4526 | |
| 4527 | // The caller is responsible for ensuring the entry is same-origin to the |
| 4528 | // origin to be committed. |
| 4529 | blink::mojom::NavigationApiHistoryEntryPtr ToNavigationApiHistoryEntry( |
| 4530 | FrameNavigationEntry* frame_entry, |
| 4531 | int64_t pending_document_sequence_number) { |
| 4532 | blink::ExplodedPageState exploded_state; |
| 4533 | if (!blink::DecodePageState(frame_entry->page_state().ToEncodedData(), |
| 4534 | &exploded_state)) { |
| 4535 | return nullptr; |
| 4536 | } |
| 4537 | blink::ExplodedFrameState frame_state = exploded_state.top; |
| 4538 | |
| 4539 | // If the document represented by this FNE hid its full url from appearing in |
| 4540 | // a referrer via a "no-referrer" or "origin" referrer policy, censor the url |
| 4541 | // in the navigation API as well (unless we're navigating to that document). |
| 4542 | std::u16string url; |
| 4543 | if (pending_document_sequence_number == |
| 4544 | frame_entry->document_sequence_number() || |
| 4545 | !frame_entry->protect_url_in_navigation_api()) { |
| 4546 | url = frame_state.url_string.value_or(std::u16string()); |
| 4547 | } |
| 4548 | |
| 4549 | return blink::mojom::NavigationApiHistoryEntry::New( |
| 4550 | frame_state.navigation_api_key.value_or(std::u16string()), |
| 4551 | frame_state.navigation_api_id.value_or(std::u16string()), url, |
| 4552 | frame_state.item_sequence_number, frame_state.document_sequence_number, |
| 4553 | frame_state.navigation_api_state); |
| 4554 | } |
| 4555 | |
| 4556 | } // namespace |
| 4557 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4558 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> |
| 4559 | NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4560 | Direction direction, |
| 4561 | int entry_index, |
| 4562 | const url::Origin& pending_origin, |
| 4563 | FrameTreeNode* node, |
| 4564 | SiteInstance* site_instance, |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4565 | int64_t pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4566 | int64_t pending_document_sequence_number, |
| 4567 | int& last_index_checked) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4568 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries; |
Rakina Zata Amni | e2d3131 | 2022-11-18 03:38:45 | [diff] [blame] | 4569 | if (GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4570 | // Don't process the initial entry. |
| 4571 | DCHECK_EQ(GetEntryCount(), 1); |
| 4572 | return entries; |
| 4573 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4574 | int offset = direction == Direction::kForward ? 1 : -1; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4575 | int64_t previous_item_sequence_number = pending_item_sequence_number; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4576 | for (int i = entry_index + offset; i >= 0 && i < GetEntryCount(); |
| 4577 | i += offset) { |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4578 | last_index_checked = i; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4579 | FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node); |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4580 | if (!frame_entry) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4581 | break; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4582 | // An entry should only appear in the navigation API entries if it is for |
| 4583 | // the same origin as the document being committed. Check the committed |
| 4584 | // origin, or if that is not available (during restore), check against the |
| 4585 | // FNE's url. |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4586 | // TODO(crbug.com/1209092): Move this into ToNavigationApiHistoryEntry() |
| 4587 | // once we can be sure that entries with the same ISN will never be |
| 4588 | // cross-origin. |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4589 | url::Origin frame_entry_origin = |
| 4590 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4591 | frame_entry->url(), |
| 4592 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 4593 | if (!pending_origin.IsSameOriginWith(frame_entry_origin)) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4594 | break; |
| 4595 | if (previous_item_sequence_number == frame_entry->item_sequence_number()) |
| 4596 | continue; |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4597 | if (blink::mojom::NavigationApiHistoryEntryPtr entry = |
| 4598 | ToNavigationApiHistoryEntry(frame_entry, |
| 4599 | pending_document_sequence_number)) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4600 | DCHECK(entry->url.empty() || |
| 4601 | pending_origin.CanBeDerivedFrom(GURL(entry->url))); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4602 | entries.push_back(std::move(entry)); |
| 4603 | previous_item_sequence_number = frame_entry->item_sequence_number(); |
| 4604 | } |
| 4605 | } |
| 4606 | // If |entries| was constructed by iterating backwards from |
| 4607 | // |entry_index|, it's latest-at-the-front, but the renderer will want it |
| 4608 | // earliest-at-the-front. Reverse it. |
| 4609 | if (direction == Direction::kBack) |
| 4610 | std::reverse(entries.begin(), entries.end()); |
| 4611 | return entries; |
| 4612 | } |
| 4613 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4614 | blink::mojom::NavigationApiHistoryEntryArraysPtr |
| 4615 | NavigationControllerImpl::GetNavigationApiHistoryEntryVectors( |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4616 | FrameTreeNode* node, |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4617 | NavigationRequest* request) { |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4618 | url::Origin pending_origin = request |
Lukasz Anforowicz | 435e68d | 2022-11-09 21:47:44 | [diff] [blame] | 4619 | ? request->GetOriginToCommit().value() |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 4620 | : url::Origin::Create(node->current_url()); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4621 | |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4622 | scoped_refptr<SiteInstance> site_instance = |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4623 | node->current_frame_host()->GetSiteInstance(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4624 | |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4625 | // NOTE: |entry_index| is the index where this entry will commit if no |
| 4626 | // modifications are made between now and DidCommitNavigation. This is used to |
| 4627 | // walk |entries_| and determine which entries should be exposed by the |
| 4628 | // navigation API. It is important to calculate this correctly, because blink |
| 4629 | // will cancel a same-document history commit if it's not present in the |
| 4630 | // entries blink knows about. |
| 4631 | int entry_index = GetLastCommittedEntryIndex(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4632 | int64_t pending_item_sequence_number = 0; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4633 | int64_t pending_document_sequence_number = 0; |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4634 | bool will_create_new_entry = false; |
| 4635 | if (GetPendingEntryIndex() != -1) { |
| 4636 | entry_index = GetPendingEntryIndex(); |
| 4637 | if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) { |
| 4638 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
| 4639 | pending_document_sequence_number = |
| 4640 | frame_entry->document_sequence_number(); |
| 4641 | } |
| 4642 | } else if (request && |
| 4643 | !NavigationTypeUtils::IsReload( |
| 4644 | request->common_params().navigation_type) && |
| 4645 | !NavigationTypeUtils::IsHistory( |
| 4646 | request->common_params().navigation_type) && |
| 4647 | !request->common_params().should_replace_current_entry && |
| 4648 | !request->common_params() |
| 4649 | .is_history_navigation_in_new_child_frame) { |
| 4650 | will_create_new_entry = true; |
| 4651 | entry_index = GetLastCommittedEntryIndex() + 1; |
| 4652 | // Don't set pending_item_sequence_number or |
| 4653 | // pending_document_sequence_number in this case - a new unique isn/dsn will |
| 4654 | // be calculated in the renderer later. |
| 4655 | } else if (GetLastCommittedEntryIndex() != -1) { |
| 4656 | entry_index = GetLastCommittedEntryIndex(); |
| 4657 | if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) { |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4658 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4659 | pending_document_sequence_number = |
| 4660 | frame_entry->document_sequence_number(); |
| 4661 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4662 | } |
| 4663 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4664 | auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New(); |
Nate Chapin | e82339d0 | 2022-05-03 23:48:25 | [diff] [blame] | 4665 | if (entry_index == -1) { |
| 4666 | // TODO(rakina): Exit early when there is no last committed entry. |
| 4667 | // Remove when InitialNavigationEntry ships. |
| 4668 | return entry_arrays; |
| 4669 | } |
| 4670 | |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4671 | int backmost_index = entry_index; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4672 | entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4673 | Direction::kBack, entry_index, pending_origin, node, site_instance.get(), |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4674 | pending_item_sequence_number, pending_document_sequence_number, |
| 4675 | backmost_index); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4676 | |
| 4677 | // 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] | 4678 | int forwardmost_index = entry_index; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4679 | if (!will_create_new_entry) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4680 | entry_arrays->forward_entries = |
| 4681 | PopulateSingleNavigationApiHistoryEntryVector( |
| 4682 | Direction::kForward, entry_index, pending_origin, node, |
| 4683 | site_instance.get(), pending_item_sequence_number, |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4684 | pending_document_sequence_number, forwardmost_index); |
| 4685 | } |
| 4686 | |
| 4687 | // If the previous entry is within the block of contiguous entries being |
| 4688 | // provided, then report it as the `previous_entry`. |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4689 | FrameNavigationEntry* previous_entry = nullptr; |
| 4690 | if (frame_tree_->is_prerendering()) { |
| 4691 | int initiator_id = PrerenderHost::GetFromFrameTreeNode(*node) |
| 4692 | .initiator_frame_tree_node_id(); |
| 4693 | if (initiator_id != RenderFrameHost::kNoFrameTreeNodeId) { |
| 4694 | auto* initiator_node = FrameTreeNode::GloballyFindByID(initiator_id); |
| 4695 | previous_entry = initiator_node->frame_tree() |
| 4696 | .controller() |
| 4697 | .GetLastCommittedEntry() |
| 4698 | ->GetFrameEntry(initiator_node); |
| 4699 | } |
| 4700 | } else if (GetLastCommittedEntryIndex() != -1 && |
| 4701 | GetLastCommittedEntryIndex() >= backmost_index && |
| 4702 | GetLastCommittedEntryIndex() <= forwardmost_index) { |
| 4703 | previous_entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 4704 | } |
| 4705 | if (previous_entry) { |
| 4706 | url::Origin previous_entry_origin = |
| 4707 | previous_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4708 | previous_entry->url(), |
| 4709 | previous_entry->initiator_origin().value_or(url::Origin()))); |
| 4710 | // TODO(crbug.com/1209092): Move this into ToNavigationApiHistoryEntry() |
| 4711 | // once we can be sure that entries with the same ISN will never be |
| 4712 | // cross-origin. |
| 4713 | if (pending_origin.IsSameOriginWith(previous_entry_origin)) { |
| 4714 | entry_arrays->previous_entry = ToNavigationApiHistoryEntry( |
| 4715 | previous_entry, pending_document_sequence_number); |
Nate Chapin | a2c881f5 | 2023-11-07 17:02:09 | [diff] [blame] | 4716 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4717 | } |
Nate Chapin | c06cf80a | 2023-11-18 01:12:25 | [diff] [blame] | 4718 | |
Nate Chapin | 4e657a47 | 2022-02-06 03:38:37 | [diff] [blame] | 4719 | return entry_arrays; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4720 | } |
| 4721 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4722 | NavigationControllerImpl::HistoryNavigationAction |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4723 | NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4724 | FrameNavigationEntry* current_entry, |
| 4725 | FrameNavigationEntry* target_entry, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4726 | const std::string& navigation_api_key) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4727 | if (!target_entry || !target_entry->committed_origin()) |
| 4728 | return HistoryNavigationAction::kStopLooking; |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4729 | if (!current_entry->committed_origin()->IsSameOriginWith( |
| 4730 | *target_entry->committed_origin())) { |
| 4731 | return HistoryNavigationAction::kStopLooking; |
| 4732 | } |
| 4733 | |
| 4734 | // NOTE: We don't actually care between kSameDocument and |
| 4735 | // kDifferentDocument, so always use kDifferentDocument by convention. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4736 | if (target_entry->navigation_api_key() == navigation_api_key) |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4737 | return HistoryNavigationAction::kDifferentDocument; |
| 4738 | return HistoryNavigationAction::kKeepLooking; |
| 4739 | } |
| 4740 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4741 | void NavigationControllerImpl::NavigateToNavigationApiKey( |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4742 | RenderFrameHostImpl* initiator_rfh, |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4743 | absl::optional<blink::scheduler::TaskAttributionId> |
| 4744 | soft_navigation_heuristics_task_id, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4745 | const std::string& key) { |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4746 | FrameTreeNode* node = initiator_rfh->frame_tree_node(); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4747 | FrameNavigationEntry* current_entry = |
| 4748 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 4749 | if (!current_entry) |
| 4750 | return; |
| 4751 | |
Yoav Weiss | 8c57395 | 2022-11-17 17:35:13 | [diff] [blame] | 4752 | // TODO(https://crbug.com/1383704): Make sure that the right task ID is passed |
| 4753 | // when `navigation.traverseTo()` is called. |
| 4754 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4755 | // We want to find the nearest matching entry that is contiguously |
| 4756 | // same-instance and same-origin. Check back first, then forward. |
| 4757 | // TODO(japhet): Link spec here once it exists. |
| 4758 | for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4759 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4760 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4761 | if (result == HistoryNavigationAction::kStopLooking) |
| 4762 | break; |
| 4763 | if (result != HistoryNavigationAction::kKeepLooking) { |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4764 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4765 | return; |
| 4766 | } |
| 4767 | } |
| 4768 | for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4769 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4770 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4771 | if (result == HistoryNavigationAction::kStopLooking) |
| 4772 | break; |
| 4773 | if (result != HistoryNavigationAction::kKeepLooking) { |
Yoav Weiss | a7449c3b | 2022-11-22 15:15:14 | [diff] [blame] | 4774 | GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4775 | return; |
| 4776 | } |
| 4777 | } |
Nate Chapin | bf682fa3 | 2022-09-26 22:41:20 | [diff] [blame] | 4778 | |
| 4779 | // If we fall through to here, a matching NavigationEntry couldn't be found. |
| 4780 | // Notify the renderer that the navigation was cancelled. |
| 4781 | initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled( |
| 4782 | key, blink::mojom::TraverseCancelledReason::kNotFound); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4783 | } |
| 4784 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame] | 4785 | bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4786 | network::mojom::ReferrerPolicy referrer_policy) { |
| 4787 | return referrer_policy == network::mojom::ReferrerPolicy::kNever || |
| 4788 | referrer_policy == network::mojom::ReferrerPolicy::kOrigin; |
| 4789 | } |
| 4790 | |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4791 | bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory( |
| 4792 | const FrameTreeNode* frame_tree_node) const { |
| 4793 | // TODO(https://crbug.com/1197384): We may have to add portals in addition to |
| 4794 | // prerender and fenced frames. This should be kept in sync with |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4795 | // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory. |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 4796 | // The preview mode appears as prerendered page in renderers, and |
| 4797 | // GetDocument()->IsPrerendering() covers the case together. |
Ali Hijazi | d87307d | 2022-11-07 20:15:03 | [diff] [blame] | 4798 | return frame_tree_->is_prerendering() || |
Takashi Toyoshima | 4dad2c1 | 2023-11-13 10:04:46 | [diff] [blame] | 4799 | frame_tree_->page_delegate()->IsInPreviewMode() || |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4800 | frame_tree_node->IsInFencedFrameTree(); |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4801 | } |
| 4802 | |
Julie Jeongeun Kim | 0e24224 | 2022-11-30 10:45:09 | [diff] [blame] | 4803 | void NavigationControllerImpl::DidChangeReferrerPolicy( |
| 4804 | FrameTreeNode* node, |
| 4805 | network::mojom::ReferrerPolicy referrer_policy) { |
| 4806 | FrameNavigationEntry* entry = GetLastCommittedEntry()->GetFrameEntry(node); |
| 4807 | if (!entry) |
| 4808 | return; |
| 4809 | |
| 4810 | // The FrameNavigationEntry may want to change whether to protect its url |
| 4811 | // in the navigation API when the referrer policy changes. |
| 4812 | entry->set_protect_url_in_navigation_api( |
| 4813 | ShouldProtectUrlInNavigationApi(referrer_policy)); |
| 4814 | } |
| 4815 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4816 | } // namespace content |