[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
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] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 41 | #include "base/bind.h" |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 42 | #include "base/command_line.h" |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 43 | #include "base/containers/adapters.h" |
Rakina Zata Amni | 627360d | 2022-02-24 00:53:40 | [diff] [blame] | 44 | #include "base/debug/dump_without_crashing.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" |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 48 | #include "base/stl_util.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" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 56 | #include "build/build_config.h" |
[email protected] | eabfe191 | 2014-05-12 10:07:28 | [diff] [blame] | 57 | #include "cc/base/switches.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 58 | #include "content/browser/bad_message.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 59 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 60 | #include "content/browser/browser_url_handler_impl.h" |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 61 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 62 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 63 | #include "content/browser/renderer_host/debug_urls.h" |
| 64 | #include "content/browser/renderer_host/frame_tree.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 65 | #include "content/browser/renderer_host/frame_tree_node.h" |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 66 | #include "content/browser/renderer_host/navigation_controller_delegate.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 67 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 68 | #include "content/browser/renderer_host/navigation_entry_restore_context_impl.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 69 | #include "content/browser/renderer_host/navigation_request.h" |
| 70 | #include "content/browser/renderer_host/navigator.h" |
| 71 | #include "content/browser/renderer_host/render_frame_host_delegate.h" |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 72 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Sharon Yang | d70a539 | 2021-10-26 23:06:32 | [diff] [blame] | 73 | #include "content/browser/site_info.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 74 | #include "content/browser/site_instance_impl.h" |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 75 | #include "content/browser/web_package/subresource_web_bundle_navigation_info.h" |
Tsuyoshi Horo | 37493af | 2019-11-21 23:43:14 | [diff] [blame] | 76 | #include "content/browser/web_package/web_bundle_navigation_info.h" |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 77 | #include "content/common/content_constants_internal.h" |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 78 | #include "content/common/navigation_params_utils.h" |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 79 | #include "content/common/trace_utils.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 80 | #include "content/public/browser/browser_context.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 81 | #include "content/public/browser/content_browser_client.h" |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 82 | #include "content/public/browser/disallow_activation_reason.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 83 | #include "content/public/browser/invalidate_type.h" |
[email protected] | 5b96836f | 2011-12-22 07:39:00 | [diff] [blame] | 84 | #include "content/public/browser/navigation_details.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 85 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 86 | #include "content/public/browser/notification_types.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 87 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 88 | #include "content/public/browser/render_widget_host.h" |
| 89 | #include "content/public/browser/render_widget_host_view.h" |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 90 | #include "content/public/browser/replaced_navigation_entry_data.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 91 | #include "content/public/browser/storage_partition.h" |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 92 | #include "content/public/common/content_client.h" |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 93 | #include "content/public/common/content_constants.h" |
toyoshim | 86e34ec | 2016-02-25 08:56:10 | [diff] [blame] | 94 | #include "content/public/common/content_features.h" |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 95 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 96 | #include "content/public/common/url_utils.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 97 | #include "media/base/mime_util.h" |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 98 | #include "net/base/escape.h" |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 99 | #include "net/http/http_status_code.h" |
Shivani Sharma | 9332910 | 2019-01-24 19:44:18 | [diff] [blame] | 100 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 101 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Yue Ru Sun | 12880493 | 2020-09-30 22:19:17 | [diff] [blame] | 102 | #include "services/metrics/public/cpp/ukm_source_id.h" |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 103 | #include "services/network/public/mojom/fetch_api.mojom.h" |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 104 | #include "skia/ext/platform_canvas.h" |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 105 | #include "third_party/blink/public/common/blob/blob_utils.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 106 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 107 | #include "third_party/blink/public/common/history/session_history_constants.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 108 | #include "third_party/blink/public/common/mime_util/mime_util.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 109 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 110 | #include "third_party/blink/public/common/page_state/page_state_serialization.h" |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 111 | #include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h" |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 112 | #include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 113 | #include "url/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 115 | namespace content { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 116 | namespace { |
| 117 | |
| 118 | // Invoked when entries have been pruned, or removed. For example, if the |
| 119 | // current entries are [google, digg, yahoo], with the current entry google, |
| 120 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 121 | void NotifyPrunedEntries(NavigationControllerImpl* nav_controller, |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 122 | int index, |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 123 | int count) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 124 | PrunedDetails details; |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 125 | details.index = index; |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 126 | details.count = count; |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 127 | nav_controller->delegate()->NotifyNavigationListPruned(details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 130 | // Configure all the NavigationEntries in entries for restore. This resets |
| 131 | // the transition type to reload and makes sure the content state isn't empty. |
| 132 | void ConfigureEntriesForRestore( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 133 | std::vector<std::unique_ptr<NavigationEntryImpl>>* entries, |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 134 | RestoreType type) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 135 | for (auto& entry : *entries) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 136 | // Use a transition type of reload so that we don't incorrectly increase |
| 137 | // the typed count. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 138 | entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 139 | entry->set_restore_type(type); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 143 | // Determines whether or not we should be carrying over a user agent override |
| 144 | // between two NavigationEntries. |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 145 | bool ShouldKeepOverride(NavigationEntry* last_entry) { |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 146 | return last_entry && last_entry->GetIsOverridingUserAgent(); |
| 147 | } |
| 148 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 149 | // Determines whether to override user agent for a navigation. |
| 150 | bool ShouldOverrideUserAgent( |
| 151 | NavigationController::UserAgentOverrideOption override_user_agent, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 152 | NavigationEntry* last_committed_entry) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 153 | switch (override_user_agent) { |
| 154 | case NavigationController::UA_OVERRIDE_INHERIT: |
| 155 | return ShouldKeepOverride(last_committed_entry); |
| 156 | case NavigationController::UA_OVERRIDE_TRUE: |
| 157 | return true; |
| 158 | case NavigationController::UA_OVERRIDE_FALSE: |
| 159 | return false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 160 | } |
| 161 | NOTREACHED(); |
| 162 | return false; |
| 163 | } |
| 164 | |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 165 | // Returns true if this navigation should be treated as a reload. For e.g. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 166 | // 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] | 167 | // link which results in a navigation to the last committed URL (but wasn't |
| 168 | // converted to do a replacement navigation in the renderer), etc. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 169 | // |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|, |
| 170 | // |base_url_for_data_url|, |transition_type| correspond to the new navigation |
| 171 | // (i.e. the pending NavigationEntry). |last_committed_entry| is the last |
| 172 | // navigation that committed. |
| 173 | bool ShouldTreatNavigationAsReload(FrameTreeNode* node, |
| 174 | const GURL& url, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 175 | const GURL& virtual_url, |
| 176 | const GURL& base_url_for_data_url, |
| 177 | ui::PageTransition transition_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 178 | bool is_post, |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 179 | bool should_replace_current_entry, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 180 | NavigationEntryImpl* last_committed_entry) { |
Rakina Zata Amni | 312822d7 | 2021-06-04 16:13:37 | [diff] [blame] | 181 | // Navigations intended to do a replacement shouldn't be converted to do a |
| 182 | // reload. |
| 183 | if (should_replace_current_entry) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 184 | return false; |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 185 | // Only convert to reload if at least one navigation committed. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 186 | if (!last_committed_entry || last_committed_entry->IsInitialEntry()) |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 187 | return false; |
| 188 | |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 189 | // Skip navigations initiated by external applications. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 190 | if (transition_type & ui::PAGE_TRANSITION_FROM_API) |
arthursonzogni | 7a824368 | 2017-12-14 16:41:42 | [diff] [blame] | 191 | return false; |
| 192 | |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 193 | // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR), |
| 194 | // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations |
| 195 | // which should be treated as reloads. |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 196 | bool transition_type_can_be_converted = false; |
| 197 | if (ui::PageTransitionCoreTypeIs(transition_type, |
| 198 | ui::PAGE_TRANSITION_RELOAD) && |
| 199 | (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) { |
| 200 | transition_type_can_be_converted = true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 201 | } |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 202 | if (ui::PageTransitionCoreTypeIs(transition_type, |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 203 | ui::PAGE_TRANSITION_TYPED)) { |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 204 | transition_type_can_be_converted = true; |
| 205 | } |
| 206 | if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK)) |
| 207 | transition_type_can_be_converted = true; |
| 208 | if (!transition_type_can_be_converted) |
| 209 | return false; |
| 210 | |
| 211 | // This check is required for cases like view-source:, etc. Here the URL of |
| 212 | // the navigation entry would contain the url of the page, while the virtual |
| 213 | // URL contains the full URL including the view-source prefix. |
| 214 | if (virtual_url != last_committed_entry->GetVirtualURL()) |
| 215 | return false; |
| 216 | |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 217 | // Check that the URLs match. |
| 218 | FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node); |
| 219 | // If there's no frame entry then by definition the URLs don't match. |
| 220 | if (!frame_entry) |
| 221 | return false; |
| 222 | |
| 223 | if (url != frame_entry->url()) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 224 | return false; |
| 225 | |
| 226 | // This check is required for Android WebView loadDataWithBaseURL. Apps |
| 227 | // can pass in anything in the base URL and we need to ensure that these |
| 228 | // match before classifying it as a reload. |
| 229 | if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) { |
| 230 | if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL()) |
| 231 | return false; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 232 | } |
| 233 | |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 234 | // Skip entries with SSL errors. |
| 235 | if (last_committed_entry->ssl_error()) |
| 236 | return false; |
| 237 | |
| 238 | // Don't convert to a reload when the last navigation was a POST or the new |
| 239 | // navigation is a POST. |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 240 | if (frame_entry->get_has_post_data() || is_post) |
clamy | 0a656e4 | 2018-02-06 18:18:28 | [diff] [blame] | 241 | return false; |
| 242 | |
| 243 | return true; |
ananta | 3bdd8ae | 2016-12-22 17:11:55 | [diff] [blame] | 244 | } |
| 245 | |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 246 | bool DoesURLMatchOriginForNavigation( |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 247 | const GURL& destination_url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 248 | const absl::optional<url::Origin>& origin, |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 249 | SubresourceWebBundleNavigationInfo* subresource_web_bundle_navigation_info, |
| 250 | NavigationEntry* entry, |
| 251 | bool is_main_frame) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 252 | // If there is no origin supplied there is nothing to match. This can happen |
| 253 | // for navigations to a pending entry and therefore it should be allowed. |
| 254 | if (!origin) |
| 255 | return true; |
| 256 | |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 257 | // For a history navigation to a document loaded with loadDataWithBaseURL, |
| 258 | // compare the origin to the entry's base URL instead of the data: URL used to |
| 259 | // commit. |
| 260 | const GURL& base_url_for_data_url = entry->GetBaseURLForDataURL(); |
| 261 | bool check_origin_against_base_url = |
| 262 | is_main_frame && !base_url_for_data_url.is_empty(); |
| 263 | |
| 264 | const GURL& url_for_origin = |
| 265 | check_origin_against_base_url ? base_url_for_data_url : destination_url; |
Kunihiko Sakamoto | 260ee0a | 2021-10-15 01:44:08 | [diff] [blame] | 266 | // Urn: and uuid-in-package: subframe from WebBundle has an opaque origin |
| 267 | // derived from the Bundle's origin. |
| 268 | // TODO(https://crbug.com/1257045): Remove urn: scheme support. |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 269 | if ((url_for_origin.SchemeIs(url::kUrnScheme) || |
| 270 | url_for_origin.SchemeIs(url::kUuidInPackageScheme)) && |
Kunihiko Sakamoto | 260ee0a | 2021-10-15 01:44:08 | [diff] [blame] | 271 | subresource_web_bundle_navigation_info) { |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 272 | return origin->CanBeDerivedFrom( |
| 273 | subresource_web_bundle_navigation_info->bundle_url()); |
| 274 | } |
| 275 | |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 276 | return origin->CanBeDerivedFrom(url_for_origin); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 277 | } |
| 278 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 279 | absl::optional<url::Origin> GetCommittedOriginForFrameEntry( |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 280 | const mojom::DidCommitProvisionalLoadParams& params, |
| 281 | NavigationRequest* request) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 282 | // Error pages commit in an opaque origin, yet have the real URL that resulted |
| 283 | // in an error as the |params.url|. Since successful reload of an error page |
| 284 | // should commit in the correct origin, setting the opaque origin on the |
| 285 | // FrameNavigationEntry will be incorrect. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 286 | if (request && request->DidEncounterError()) |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 287 | return absl::nullopt; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 288 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 289 | return absl::make_optional(params.origin); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 290 | } |
| 291 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 292 | bool IsValidURLForNavigation(bool is_main_frame, |
| 293 | const GURL& virtual_url, |
| 294 | const GURL& dest_url) { |
| 295 | // Don't attempt to navigate if the virtual URL is non-empty and invalid. |
| 296 | if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) { |
| 297 | LOG(WARNING) << "Refusing to load for invalid virtual URL: " |
| 298 | << virtual_url.possibly_invalid_spec(); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // Don't attempt to navigate to non-empty invalid URLs. |
| 303 | if (!dest_url.is_valid() && !dest_url.is_empty()) { |
| 304 | LOG(WARNING) << "Refusing to load invalid URL: " |
| 305 | << dest_url.possibly_invalid_spec(); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | // The renderer will reject IPC messages with URLs longer than |
| 310 | // this limit, so don't attempt to navigate with a longer URL. |
| 311 | if (dest_url.spec().size() > url::kMaxURLChars) { |
| 312 | LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars |
| 313 | << " characters."; |
| 314 | return false; |
| 315 | } |
| 316 | |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 317 | // Reject renderer debug URLs because they should have been handled before |
| 318 | // we get to this point. This check handles renderer debug URLs |
| 319 | // that are inside a view-source: URL (e.g. view-source:chrome://kill) and |
| 320 | // provides defense-in-depth if a renderer debug URL manages to get here via |
| 321 | // some other path. We want to reject the navigation here so it doesn't |
| 322 | // violate assumptions in downstream code. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 323 | if (blink::IsRendererDebugURL(dest_url)) { |
Aaron Colwell | 33109c59 | 2020-04-21 21:31:19 | [diff] [blame] | 324 | LOG(WARNING) << "Refusing to load renderer debug URL: " |
| 325 | << dest_url.possibly_invalid_spec(); |
| 326 | return false; |
| 327 | } |
| 328 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 332 | // See replaced_navigation_entry_data.h for details: this information is meant |
| 333 | // to ensure |*output_entry| keeps track of its original URL (landing page in |
| 334 | // case of server redirects) as it gets replaced (e.g. history.replaceState()), |
| 335 | // without overwriting it later, for main frames. |
| 336 | void CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 337 | NavigationEntryImpl* replaced_entry, |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 338 | NavigationEntryImpl* output_entry) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 339 | if (output_entry->GetReplacedEntryData().has_value() || |
| 340 | replaced_entry->IsInitialEntry()) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 341 | return; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 342 | } |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 343 | |
| 344 | ReplacedNavigationEntryData data; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 345 | data.first_committed_url = replaced_entry->GetURL(); |
| 346 | data.first_timestamp = replaced_entry->GetTimestamp(); |
| 347 | data.first_transition_type = replaced_entry->GetTransitionType(); |
Charlie Reis | b55438f | 2019-01-08 01:54:29 | [diff] [blame] | 348 | output_entry->set_replaced_entry_data(data); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 349 | } |
| 350 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 351 | blink::mojom::NavigationType GetNavigationType( |
| 352 | const GURL& old_url, |
| 353 | const GURL& new_url, |
| 354 | ReloadType reload_type, |
| 355 | NavigationEntryImpl* entry, |
| 356 | const FrameNavigationEntry& frame_entry, |
| 357 | bool has_pending_cross_document_commit, |
| 358 | bool is_currently_error_page, |
| 359 | bool is_same_document_history_load) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 360 | // Reload navigations |
| 361 | switch (reload_type) { |
| 362 | case ReloadType::NORMAL: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 363 | return blink::mojom::NavigationType::RELOAD; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 364 | case ReloadType::BYPASSING_CACHE: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 365 | return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 366 | case ReloadType::ORIGINAL_REQUEST_URL: |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 367 | return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 368 | case ReloadType::NONE: |
| 369 | break; // Fall through to rest of function. |
| 370 | } |
| 371 | |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 372 | if (entry->IsRestored()) { |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 373 | return entry->GetHasPostData() |
| 374 | ? blink::mojom::NavigationType::RESTORE_WITH_POST |
| 375 | : blink::mojom::NavigationType::RESTORE; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 376 | } |
| 377 | |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 378 | const bool can_be_same_document = |
| 379 | // A pending cross-document commit means this navigation will not occur in |
| 380 | // the current document, as that document would end up being replaced in |
| 381 | // the meantime. |
| 382 | !has_pending_cross_document_commit && |
| 383 | // If the current document is an error page, we should always treat it as |
| 384 | // a different-document navigation so that we'll attempt to load the |
| 385 | // document we're navigating to (and not stay in the current error page). |
| 386 | !is_currently_error_page; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 387 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 388 | // History navigations. |
| 389 | if (frame_entry.page_state().IsValid()) { |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 390 | return can_be_same_document && is_same_document_history_load |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 391 | ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT |
| 392 | : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 393 | } |
| 394 | DCHECK(!is_same_document_history_load); |
| 395 | |
| 396 | // A same-document fragment-navigation happens when the only part of the url |
| 397 | // that is modified is after the '#' character. |
| 398 | // |
| 399 | // When modifying this condition, please take a look at: |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 400 | // FrameLoader::ShouldPerformFragmentNavigation(). |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 401 | // |
| 402 | // Note: this check is only valid for navigations that are not history |
| 403 | // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a |
| 404 | // history navigation from 'A#foo' to 'A#bar' is not a same-document |
| 405 | // navigation, but a different-document one. This is why history navigation |
| 406 | // are classified before this check. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 407 | bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) && |
| 408 | frame_entry.method() == "GET"; |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 409 | |
| 410 | // The one case where we do the wrong thing here and incorrectly choose |
| 411 | // SAME_DOCUMENT is if the navigation is browser-initiated but the document in |
| 412 | // the renderer is a frameset. All frameset navigations should be |
| 413 | // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated |
| 414 | // navigation would do the right thing, as it would send it to the browser and |
| 415 | // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get |
| 416 | // into this method). But since we can't tell that case here for browser- |
| 417 | // initiated navigations, we have to get the renderer involved. In that case |
| 418 | // the navigation would be restarted due to the renderer spending a reply of |
| 419 | // mojom::CommitResult::RestartCrossDocument. |
| 420 | |
| 421 | return can_be_same_document && is_same_doc |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 422 | ? blink::mojom::NavigationType::SAME_DOCUMENT |
| 423 | : blink::mojom::NavigationType::DIFFERENT_DOCUMENT; |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 424 | } |
| 425 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 426 | // Adjusts the original input URL if needed, to get the URL to actually load and |
| 427 | // the virtual URL, which may differ. |
| 428 | void RewriteUrlForNavigation(const GURL& original_url, |
| 429 | BrowserContext* browser_context, |
| 430 | GURL* url_to_load, |
| 431 | GURL* virtual_url, |
| 432 | bool* reverse_on_redirect) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 433 | // Allow the browser URL handler to rewrite the URL. This will, for example, |
| 434 | // remove "view-source:" from the beginning of the URL to get the URL that |
| 435 | // will actually be loaded. This real URL won't be shown to the user, just |
| 436 | // used internally. |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 437 | *url_to_load = *virtual_url = original_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 438 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 439 | url_to_load, browser_context, reverse_on_redirect); |
| 440 | } |
| 441 | |
| 442 | #if DCHECK_IS_ON() |
| 443 | // Helper sanity check function used in debug mode. |
| 444 | void ValidateRequestMatchesEntry(NavigationRequest* request, |
| 445 | NavigationEntryImpl* entry) { |
| 446 | if (request->frame_tree_node()->IsMainFrame()) { |
| 447 | DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated()); |
| 448 | DCHECK(ui::PageTransitionTypeIncludingQualifiersIs( |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 449 | ui::PageTransitionFromInt(request->common_params().transition), |
| 450 | entry->GetTransitionType())); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 451 | } |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 452 | DCHECK_EQ(request->commit_params().should_clear_history_list, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 453 | entry->should_clear_history_list()); |
| 454 | DCHECK_EQ(request->common_params().has_user_gesture, |
| 455 | entry->has_user_gesture()); |
| 456 | DCHECK_EQ(request->common_params().base_url_for_data_url, |
| 457 | entry->GetBaseURLForDataURL()); |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 458 | DCHECK_EQ(request->commit_params().can_load_local_resources, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 459 | entry->GetCanLoadLocalResources()); |
| 460 | DCHECK_EQ(request->common_params().started_from_context_menu, |
| 461 | entry->has_started_from_context_menu()); |
| 462 | |
| 463 | FrameNavigationEntry* frame_entry = |
| 464 | entry->GetFrameEntry(request->frame_tree_node()); |
| 465 | if (!frame_entry) { |
| 466 | NOTREACHED(); |
| 467 | return; |
| 468 | } |
| 469 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 470 | DCHECK_EQ(request->common_params().method, frame_entry->method()); |
| 471 | |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 472 | size_t redirect_size = request->commit_params().redirects.size(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 473 | if (redirect_size == frame_entry->redirect_chain().size()) { |
| 474 | for (size_t i = 0; i < redirect_size; ++i) { |
Nasko Oskov | c36327d | 2019-01-03 23:23:04 | [diff] [blame] | 475 | DCHECK_EQ(request->commit_params().redirects[i], |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 476 | frame_entry->redirect_chain()[i]); |
| 477 | } |
| 478 | } else { |
| 479 | NOTREACHED(); |
| 480 | } |
| 481 | } |
| 482 | #endif // DCHECK_IS_ON() |
| 483 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 484 | // Returns whether the session history NavigationRequests in |navigations| |
| 485 | // would stay within the subtree of the sandboxed iframe in |
| 486 | // |sandbox_frame_tree_node_id|. |
| 487 | bool DoesSandboxNavigationStayWithinSubtree( |
| 488 | int sandbox_frame_tree_node_id, |
| 489 | const std::vector<std::unique_ptr<NavigationRequest>>& navigations) { |
| 490 | for (auto& item : navigations) { |
| 491 | bool within_subtree = false; |
| 492 | // Check whether this NavigationRequest affects a frame within the |
| 493 | // sandboxed frame's subtree by walking up the tree looking for the |
| 494 | // sandboxed frame. |
| 495 | for (auto* frame = item->frame_tree_node(); frame; |
Alexander Timin | 381e7e18 | 2020-04-28 19:04:03 | [diff] [blame] | 496 | frame = FrameTreeNode::From(frame->parent())) { |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 497 | if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) { |
| 498 | within_subtree = true; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | if (!within_subtree) |
| 503 | return false; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 508 | } // namespace |
| 509 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 510 | // NavigationControllerImpl::PendingEntryRef------------------------------------ |
| 511 | |
| 512 | NavigationControllerImpl::PendingEntryRef::PendingEntryRef( |
| 513 | base::WeakPtr<NavigationControllerImpl> controller) |
| 514 | : controller_(controller) {} |
| 515 | |
| 516 | NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() { |
| 517 | if (!controller_) // Can be null with interstitials. |
| 518 | return; |
| 519 | |
| 520 | controller_->PendingEntryRefDeleted(this); |
| 521 | } |
| 522 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 523 | // NavigationControllerImpl ---------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | |
[email protected] | 23a918b | 2014-07-15 09:51:36 | [diff] [blame] | 525 | const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1); |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 526 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 527 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 528 | size_t NavigationControllerImpl::max_entry_count_for_testing_ = |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 529 | kMaxEntryCountForTestingNotSet; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 530 | |
[email protected] | e6fec47 | 2013-05-14 05:29:02 | [diff] [blame] | 531 | // 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] | 532 | // when testing. |
| 533 | static bool g_check_for_repost = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 535 | // static |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 536 | std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry( |
| 537 | const GURL& url, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 538 | Referrer referrer, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 539 | absl::optional<url::Origin> initiator_origin, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 540 | ui::PageTransition transition, |
| 541 | bool is_renderer_initiated, |
| 542 | const std::string& extra_headers, |
| 543 | BrowserContext* browser_context, |
| 544 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { |
| 545 | return NavigationControllerImpl::CreateNavigationEntry( |
| 546 | url, referrer, std::move(initiator_origin), |
| 547 | nullptr /* source_site_instance */, transition, is_renderer_initiated, |
Hayato Ito | 303654c | 2021-06-30 09:07:54 | [diff] [blame] | 548 | extra_headers, browser_context, std::move(blob_url_loader_factory)); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | // static |
| 552 | std::unique_ptr<NavigationEntryImpl> |
| 553 | NavigationControllerImpl::CreateNavigationEntry( |
| 554 | const GURL& url, |
| 555 | Referrer referrer, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 556 | absl::optional<url::Origin> initiator_origin, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 557 | SiteInstance* source_site_instance, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 558 | ui::PageTransition transition, |
| 559 | bool is_renderer_initiated, |
| 560 | const std::string& extra_headers, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 561 | BrowserContext* browser_context, |
Hayato Ito | 303654c | 2021-06-30 09:07:54 | [diff] [blame] | 562 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 563 | GURL url_to_load; |
| 564 | GURL virtual_url; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 565 | bool reverse_on_redirect = false; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 566 | RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url, |
| 567 | &reverse_on_redirect); |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 568 | |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 569 | // Let the NTP override the navigation params and pretend that this is a |
| 570 | // browser-initiated, bookmark-like navigation. |
| 571 | GetContentClient()->browser()->OverrideNavigationParams( |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 572 | source_site_instance, &transition, &is_renderer_initiated, &referrer, |
| 573 | &initiator_origin); |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 574 | |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 575 | auto entry = std::make_unique<NavigationEntryImpl>( |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 576 | nullptr, // The site instance for tabs is sent on navigation |
| 577 | // (WebContents::GetSiteInstance). |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 578 | url_to_load, referrer, initiator_origin, std::u16string(), transition, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 579 | is_renderer_initiated, blob_url_loader_factory, |
| 580 | /* is_initial_entry = */ false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 581 | entry->SetVirtualURL(virtual_url); |
| 582 | entry->set_user_typed_url(virtual_url); |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 583 | entry->set_update_virtual_url_with_url(reverse_on_redirect); |
| 584 | entry->set_extra_headers(extra_headers); |
Patrick Monette | f507e98 | 2019-06-19 20:18:06 | [diff] [blame] | 585 | return entry; |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 586 | } |
| 587 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 588 | // static |
| 589 | void NavigationController::DisablePromptOnRepost() { |
| 590 | g_check_for_repost = false; |
| 591 | } |
| 592 | |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 593 | base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime( |
| 594 | base::Time t) { |
| 595 | // If |t| is between the water marks, we're in a run of duplicates |
| 596 | // or just getting out of it, so increase the high-water mark to get |
| 597 | // a time that probably hasn't been used before and return it. |
| 598 | if (low_water_mark_ <= t && t <= high_water_mark_) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 599 | high_water_mark_ += base::Microseconds(1); |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 600 | return high_water_mark_; |
| 601 | } |
| 602 | |
| 603 | // Otherwise, we're clear of the last duplicate run, so reset the |
| 604 | // water marks. |
| 605 | low_water_mark_ = high_water_mark_ = t; |
| 606 | return t; |
| 607 | } |
| 608 | |
ckitagawa | 0faa5e4 | 2020-06-17 17:30:54 | [diff] [blame] | 609 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 610 | ScopedShowRepostDialogForTesting() |
| 611 | : was_disallowed_(g_check_for_repost) { |
| 612 | g_check_for_repost = true; |
| 613 | } |
| 614 | |
| 615 | NavigationControllerImpl::ScopedShowRepostDialogForTesting:: |
| 616 | ~ScopedShowRepostDialogForTesting() { |
| 617 | g_check_for_repost = was_disallowed_; |
| 618 | } |
| 619 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 620 | NavigationControllerImpl::NavigationControllerImpl( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 621 | BrowserContext* browser_context, |
| 622 | FrameTree& frame_tree, |
| 623 | NavigationControllerDelegate* delegate) |
| 624 | : frame_tree_(frame_tree), |
| 625 | browser_context_(browser_context), |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 626 | delegate_(delegate), |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 627 | ssl_manager_(this), |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 628 | get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 629 | DCHECK(browser_context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 630 | } |
| 631 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 632 | NavigationControllerImpl::~NavigationControllerImpl() { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 633 | // The NavigationControllerImpl might be called inside its delegate |
| 634 | // destructor. Calling it is not valid anymore. |
| 635 | delegate_ = nullptr; |
| 636 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 637 | } |
| 638 | |
Matt Falkenhagen | 548ed156 | 2021-07-06 01:38:26 | [diff] [blame] | 639 | WebContents* NavigationControllerImpl::DeprecatedGetWebContents() { |
| 640 | return delegate_->DeprecatedGetWebContents(); |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 641 | } |
| 642 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 643 | BrowserContext* NavigationControllerImpl::GetBrowserContext() { |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 644 | return browser_context_; |
| 645 | } |
| 646 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 647 | void NavigationControllerImpl::Restore( |
[email protected] | 5e36967 | 2009-11-03 23:48:30 | [diff] [blame] | 648 | int selected_navigation, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 649 | RestoreType type, |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 650 | std::vector<std::unique_ptr<NavigationEntry>>* entries) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 651 | // Note that it's possible for `entries_` to contain multiple entries at this |
| 652 | // point, as Restore() might be called on a NavigationController that is |
| 653 | // already used (e.g. due to WebView's restoreState() API), not only for fresh |
| 654 | // NavigationControllers. These entries are not cleared to preserve legacy |
| 655 | // behavior and also because `pending_entry_` might point to one of the |
| 656 | // pre-existing entries. An exception for this is when `entries_` contains |
| 657 | // only the initial NavigationEntry, which must be removed. |
| 658 | |
| 659 | // Do not proceed if selected_navigation will be out of bounds for the updated |
| 660 | // entries_ list, since it will be assigned to last_committed_entry_index_ and |
| 661 | // used to index entries_. |
| 662 | // TODO(https://crbug.com/1287624): Consider also returning early if entries |
| 663 | // is empty, since there should be no work to do (rather than marking the |
| 664 | // existing entries as needing reload). Also consider returning early if the |
| 665 | // selected index is -1, which represents a no-committed-entry state. |
| 666 | if (selected_navigation < -1 || |
| 667 | selected_navigation >= |
| 668 | base::checked_cast<int>(entries->size() + entries_.size())) { |
| 669 | return; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 670 | } |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 671 | |
| 672 | if (blink::features::IsInitialNavigationEntryEnabled()) { |
| 673 | // In InitialNavigationEntry mode, there will always be at least one entry. |
| 674 | if (selected_navigation == -1) |
| 675 | selected_navigation = 0; |
| 676 | |
| 677 | if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) { |
| 678 | // If we are on the initial NavigationEntry, it must be the only entry in |
| 679 | // the list. Since it's impossible to do a history navigation to the |
| 680 | // initial NavigationEntry, `pending_entry_index_` must be -1 (but |
| 681 | // `pending_entry` might be set for a new non-history navigation). |
| 682 | // Note that we should not clear `entries_` if `entries` is empty (when |
| 683 | // InitialNavigationEntry mode is enabled), since that would leave us |
| 684 | // without any NavigationEntry. |
| 685 | CHECK_EQ(1, GetEntryCount()); |
| 686 | CHECK_EQ(-1, pending_entry_index_); |
| 687 | entries_.clear(); |
| 688 | } |
| 689 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 690 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 691 | needs_reload_ = true; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 692 | needs_reload_type_ = NeedsReloadType::kRestoreSession; |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 693 | entries_.reserve(entries->size()); |
Rakina Zata Amni | 627360d | 2022-02-24 00:53:40 | [diff] [blame] | 694 | int index = 0; |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 695 | for (auto& entry : *entries) { |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 696 | if (entry->GetURL().is_empty()) { |
| 697 | // 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] | 698 | // persisting the initial NavigationEntry [which is no longer possible but |
| 699 | // some old persisted sessions might still contain it] or when the |
| 700 | // serializer failed to write the URL because it's too long). Trying to |
| 701 | // restore and navigate to an entry with an empty URL will result in |
| 702 | // crashes, so change the URL to about:blank. See also |
| 703 | // https://crbug.com/1240138 and https://crbug.com/1299335. |
Rakina Zata Amni | 996ee41 | 2022-02-17 04:51:43 | [diff] [blame] | 704 | entry->SetURL(GURL(url::kAboutBlankURL)); |
| 705 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 706 | entries_.push_back( |
| 707 | NavigationEntryImpl::FromNavigationEntry(std::move(entry))); |
Rakina Zata Amni | 627360d | 2022-02-24 00:53:40 | [diff] [blame] | 708 | index++; |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 709 | } |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 710 | |
| 711 | // At this point, the |entries| is full of empty scoped_ptrs, so it can be |
| 712 | // cleared out safely. |
| 713 | entries->clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 714 | |
| 715 | // And finish the restore. |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 716 | FinishRestore(selected_navigation, type); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 717 | } |
| 718 | |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 719 | void NavigationControllerImpl::Reload(ReloadType reload_type, |
| 720 | bool check_for_repost) { |
liaoyuke | 9168fba | 2017-03-10 19:20:28 | [diff] [blame] | 721 | DCHECK_NE(ReloadType::NONE, reload_type); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 722 | NavigationEntryImpl* entry = nullptr; |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 723 | int current_index = -1; |
| 724 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 725 | if (entry_replaced_by_post_commit_error_) { |
| 726 | // If there is an entry that was replaced by a currently active post-commit |
| 727 | // error navigation, this can't be the initial navigation. |
| 728 | DCHECK(!IsInitialNavigation()); |
| 729 | // If the current entry is a post commit error, we reload the entry it |
| 730 | // replaced instead. We leave the error entry in place until a commit |
| 731 | // replaces it, but the pending entry points to the original entry in the |
| 732 | // meantime. Note that NavigateToExistingPendingEntry is able to handle the |
| 733 | // case that pending_entry_ != entries_[pending_entry_index_]. |
| 734 | entry = entry_replaced_by_post_commit_error_.get(); |
| 735 | current_index = GetCurrentEntryIndex(); |
| 736 | } else if (IsInitialNavigation() && pending_entry_) { |
| 737 | // If we are reloading the initial navigation, just use the current |
| 738 | // pending entry. Otherwise look up the current entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 739 | entry = pending_entry_; |
| 740 | // The pending entry might be in entries_ (e.g., after a Clone), so we |
| 741 | // should also update the current_index. |
| 742 | current_index = pending_entry_index_; |
| 743 | } else { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 744 | DiscardNonCommittedEntries(); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 745 | current_index = GetCurrentEntryIndex(); |
| 746 | if (current_index != -1) { |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 747 | entry = GetEntryAtIndex(current_index); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 748 | } |
[email protected] | 979a4bc | 2013-04-24 01:27:15 | [diff] [blame] | 749 | } |
[email protected] | 241db35 | 2013-04-22 18:04:05 | [diff] [blame] | 750 | |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 751 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 752 | // CanReload method. |
| 753 | if (!entry) |
| 754 | return; |
| 755 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 756 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 757 | // We should never navigate to an existing initial NavigationEntry that is |
| 758 | // the initial NavigationEntry for the initial empty document that hasn't |
| 759 | // been overridden by the synchronous about:blank commit, to preserve |
| 760 | // legacy behavior where trying to reload when the main frame is on the |
| 761 | // initial empty document won't result in a navigation. See also |
| 762 | // https://crbug.com/1277414. |
| 763 | return; |
| 764 | } |
| 765 | |
Takashi Toyoshima | c7df3c2 | 2019-06-25 14:18:47 | [diff] [blame] | 766 | // Set ReloadType for |entry|. |
toyoshim | a63c2a6 | 2016-09-29 09:03:26 | [diff] [blame] | 767 | entry->set_reload_type(reload_type); |
| 768 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 769 | if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) { |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 770 | // 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] | 771 | // they really want to do this. If they do, the dialog will call us back |
| 772 | // with check_for_repost = false. |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 773 | delegate_->NotifyBeforeFormRepostWarningShow(); |
[email protected] | 965bb09 | 2010-04-09 11:59:02 | [diff] [blame] | 774 | |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 775 | pending_reload_ = reload_type; |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 776 | delegate_->ActivateAndShowRepostFormWarningDialog(); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 777 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | } |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 779 | |
| 780 | if (!IsInitialNavigation()) |
| 781 | DiscardNonCommittedEntries(); |
| 782 | |
| 783 | pending_entry_ = entry; |
| 784 | pending_entry_index_ = current_index; |
| 785 | pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); |
| 786 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 787 | // location.reload() goes through BeginNavigation, so all reloads triggered |
| 788 | // via this codepath are browser initiated. |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 789 | NavigateToExistingPendingEntry(reload_type, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 790 | FrameTreeNode::kFrameTreeNodeInvalidId, |
| 791 | true /* is_browser_initiated */); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 792 | } |
| 793 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 794 | void NavigationControllerImpl::CancelPendingReload() { |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 795 | DCHECK(pending_reload_ != ReloadType::NONE); |
| 796 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 797 | } |
| 798 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 799 | void NavigationControllerImpl::ContinuePendingReload() { |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 800 | if (pending_reload_ == ReloadType::NONE) { |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 801 | NOTREACHED(); |
| 802 | } else { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 803 | Reload(pending_reload_, false); |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 804 | pending_reload_ = ReloadType::NONE; |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 808 | bool NavigationControllerImpl::IsInitialNavigation() { |
[email protected] | 27ba81c | 2012-08-21 17:04:09 | [diff] [blame] | 809 | return is_initial_navigation_; |
[email protected] | c70f9b8 | 2010-04-21 07:31:11 | [diff] [blame] | 810 | } |
| 811 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 812 | bool NavigationControllerImpl::IsInitialBlankNavigation() { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 813 | if (blink::features::IsInitialNavigationEntryEnabled()) { |
| 814 | // Check that we're on the initial NavigationEntry and that this is not a |
| 815 | // cloned tab. |
| 816 | return IsInitialNavigation() && GetEntryCount() == 1 && |
| 817 | GetLastCommittedEntry()->IsInitialEntry() && |
| 818 | GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored; |
| 819 | } else { |
| 820 | return IsInitialNavigation() && GetEntryCount() == 0; |
| 821 | } |
creis | 10a4ab7 | 2015-10-13 17:22:40 | [diff] [blame] | 822 | } |
| 823 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 824 | NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID( |
| 825 | int nav_entry_id) const { |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 826 | int index = GetEntryIndexWithUniqueID(nav_entry_id); |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 827 | return (index != -1) ? entries_[index].get() : nullptr; |
avi | 254eff0 | 2015-07-01 08:27:58 | [diff] [blame] | 828 | } |
| 829 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 830 | NavigationEntryImpl* |
| 831 | NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending( |
| 832 | int nav_entry_id) const { |
| 833 | NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id); |
| 834 | if (entry) |
| 835 | return entry; |
| 836 | return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 837 | ? pending_entry_.get() |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 838 | : nullptr; |
| 839 | } |
| 840 | |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 841 | void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation( |
| 842 | const url::Origin& origin) { |
| 843 | for (int i = 0; i < GetEntryCount(); i++) { |
| 844 | auto* entry = GetEntryAtIndex(i); |
| 845 | entry->RegisterExistingOriginToPreventOptInIsolation(origin); |
| 846 | } |
| 847 | if (entry_replaced_by_post_commit_error_) { |
| 848 | // It's possible we could come back to this entry if the error |
| 849 | // page/interstitial goes away. |
| 850 | entry_replaced_by_post_commit_error_ |
| 851 | ->RegisterExistingOriginToPreventOptInIsolation(origin); |
| 852 | } |
| 853 | // TODO(wjmaclean): Register pending commit NavigationRequests rather than |
| 854 | // visiting pending_entry_, which lacks a committed origin. This will be done |
| 855 | // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703. |
| 856 | } |
| 857 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 858 | void NavigationControllerImpl::SetPendingEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 859 | std::unique_ptr<NavigationEntryImpl> entry) { |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 860 | DiscardNonCommittedEntries(); |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 861 | pending_entry_ = entry.release(); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 862 | DCHECK_EQ(-1, pending_entry_index_); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 863 | } |
| 864 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 865 | NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 866 | if (pending_entry_) |
| 867 | return pending_entry_; |
| 868 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 869 | } |
| 870 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 871 | NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() { |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 872 | // The pending entry is safe to return for new (non-history), browser- |
| 873 | // initiated navigations. Most renderer-initiated navigations should not |
| 874 | // show the pending entry, to prevent URL spoof attacks. |
| 875 | // |
| 876 | // We make an exception for renderer-initiated navigations in new tabs, as |
| 877 | // long as no other page has tried to access the initial empty document in |
| 878 | // the new tab. If another page modifies this blank page, a URL spoof is |
| 879 | // possible, so we must stop showing the pending entry. |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 880 | bool safe_to_show_pending = |
| 881 | pending_entry_ && |
| 882 | // Require a new navigation. |
avi | 0dca04d | 2015-01-26 20:21:09 | [diff] [blame] | 883 | pending_entry_index_ == -1 && |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 884 | // Require either browser-initiated or an unmodified new tab. |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 885 | (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab()); |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 886 | |
| 887 | // Also allow showing the pending entry for history navigations in a new tab, |
| 888 | // such as Ctrl+Back. In this case, no existing page is visible and no one |
| 889 | // can script the new tab before it commits. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 890 | if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 && |
| 891 | IsInitialNavigation() && !pending_entry_->is_renderer_initiated()) |
[email protected] | 59167c2 | 2013-06-03 18:07:32 | [diff] [blame] | 892 | safe_to_show_pending = true; |
| 893 | |
| 894 | if (safe_to_show_pending) |
[email protected] | 867e1f9 | 2011-08-30 19:01:19 | [diff] [blame] | 895 | return pending_entry_; |
| 896 | return GetLastCommittedEntry(); |
| 897 | } |
| 898 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 899 | int NavigationControllerImpl::GetCurrentEntryIndex() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 900 | if (pending_entry_index_ != -1) |
| 901 | return pending_entry_index_; |
| 902 | return last_committed_entry_index_; |
| 903 | } |
| 904 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 905 | NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 906 | if (last_committed_entry_index_ == -1) { |
| 907 | // The last committed entry must always exist when InitialNavigationEntry |
| 908 | // is enabled. TODO(https://crbug.com/524208): Remove this case and all |
| 909 | // related nullchecks entirely. |
| 910 | DCHECK(!blink::features::IsInitialNavigationEntryEnabled()); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 911 | return nullptr; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 912 | } |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 913 | return entries_[last_committed_entry_index_].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 914 | } |
| 915 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 916 | bool NavigationControllerImpl::CanViewSource() { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 917 | const std::string& mime_type = |
| 918 | frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type(); |
Kinuko Yasuda | 74702f9 | 2017-07-31 03:27:53 | [diff] [blame] | 919 | bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) && |
| 920 | !media::IsSupportedMediaMimeType(mime_type); |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 921 | NavigationEntry* visible_entry = GetVisibleEntry(); |
| 922 | return visible_entry && !visible_entry->IsViewSourceMode() && |
Carlos IL | d51e770 | 2020-05-07 18:51:39 | [diff] [blame] | 923 | is_viewable_mime_type; |
[email protected] | 3168228 | 2010-01-15 18:05:16 | [diff] [blame] | 924 | } |
| 925 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 926 | int NavigationControllerImpl::GetLastCommittedEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 927 | // The last committed entry index must always be less than the number of |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 928 | // entries. If there are no entries, it must be -1. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 929 | DCHECK_LT(last_committed_entry_index_, GetEntryCount()); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 930 | DCHECK(GetEntryCount() || last_committed_entry_index_ == -1); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 931 | return last_committed_entry_index_; |
| 932 | } |
| 933 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 934 | int NavigationControllerImpl::GetEntryCount() { |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 935 | DCHECK_LE(entries_.size(), max_entry_count()); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 936 | return static_cast<int>(entries_.size()); |
| 937 | } |
| 938 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 939 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) { |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 940 | if (index < 0 || index >= GetEntryCount()) |
| 941 | return nullptr; |
| 942 | |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 943 | return entries_[index].get(); |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 944 | } |
| 945 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 946 | NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) { |
avi | 057ce149 | 2015-06-29 15:59:47 | [diff] [blame] | 947 | return GetEntryAtIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 948 | } |
| 949 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 950 | int NavigationControllerImpl::GetIndexForOffset(int offset) { |
[email protected] | 7bc2b03 | 2012-12-19 22:45:46 | [diff] [blame] | 951 | return GetCurrentEntryIndex() + offset; |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 952 | } |
| 953 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 954 | bool NavigationControllerImpl::CanGoBack() { |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 955 | for (int index = GetIndexForOffset(-1); index >= 0; index--) { |
| 956 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 957 | return true; |
| 958 | } |
| 959 | return false; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 960 | } |
| 961 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 962 | bool NavigationControllerImpl::CanGoForward() { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 963 | for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) { |
| 964 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 965 | return true; |
| 966 | } |
| 967 | return false; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 968 | } |
| 969 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 970 | bool NavigationControllerImpl::CanGoToOffset(int offset) { |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 971 | int index = GetIndexForOffset(offset); |
| 972 | return index >= 0 && index < GetEntryCount(); |
| 973 | } |
| 974 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 975 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 976 | bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 977 | if (offset == 0) |
| 978 | return true; |
| 979 | int increment = offset > 0 ? 1 : -1; |
| 980 | int non_skippable_entries = 0; |
| 981 | for (int index = GetIndexForOffset(increment); |
| 982 | index >= 0 && index < GetEntryCount(); index += increment) { |
| 983 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 984 | non_skippable_entries++; |
| 985 | |
| 986 | if (non_skippable_entries == std::abs(offset)) |
| 987 | return true; |
| 988 | } |
| 989 | return false; |
| 990 | } |
| 991 | #endif |
| 992 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 993 | void NavigationControllerImpl::GoBack() { |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 994 | int target_index = GetIndexForOffset(-1); |
| 995 | |
Elly Fong-Jones | 40ee811 | 2021-06-23 19:10:52 | [diff] [blame] | 996 | // Move the target index past the skippable entries. |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 997 | bool all_skippable_entries = true; |
Elly Fong-Jones | 40ee811 | 2021-06-23 19:10:52 | [diff] [blame] | 998 | while (target_index >= 0) { |
| 999 | if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) { |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1000 | all_skippable_entries = false; |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1001 | break; |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 1002 | } |
Elly Fong-Jones | 40ee811 | 2021-06-23 19:10:52 | [diff] [blame] | 1003 | target_index--; |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1004 | } |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 1005 | |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1006 | // Do nothing if all entries are skippable. Normally this path would not |
| 1007 | // happen as consumers would have already checked it in CanGoBack but a lot of |
| 1008 | // tests do not do that. |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 1009 | if (all_skippable_entries) |
Shivani Sharma | 298d1285 | 2019-01-22 20:04:03 | [diff] [blame] | 1010 | return; |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1011 | |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1012 | GoToIndex(target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1013 | } |
| 1014 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1015 | void NavigationControllerImpl::GoForward() { |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1016 | int target_index = GetIndexForOffset(1); |
| 1017 | |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 1018 | // Note that at least one entry (the last one) will be non-skippable since |
| 1019 | // entries are marked skippable only when they add another entry because of |
| 1020 | // redirect or pushState. |
Elly Fong-Jones | 40ee811 | 2021-06-23 19:10:52 | [diff] [blame] | 1021 | while (target_index < static_cast<int>(entries_.size())) { |
| 1022 | if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1023 | break; |
Elly Fong-Jones | 40ee811 | 2021-06-23 19:10:52 | [diff] [blame] | 1024 | target_index++; |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1025 | } |
shivanisha | 5520187 | 2018-12-13 04:29:06 | [diff] [blame] | 1026 | GoToIndex(target_index); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1027 | } |
| 1028 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1029 | void NavigationControllerImpl::GoToIndex(int index) { |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1030 | GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId, |
| 1031 | true /* is_browser_initiated */); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void NavigationControllerImpl::GoToIndex(int index, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1035 | int sandbox_frame_tree_node_id, |
| 1036 | bool is_browser_initiated) { |
| 1037 | DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId || |
| 1038 | !is_browser_initiated); |
sunjian | 30574a6 | 2017-03-21 21:39:44 | [diff] [blame] | 1039 | TRACE_EVENT0("browser,navigation,benchmark", |
| 1040 | "NavigationControllerImpl::GoToIndex"); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1041 | if (index < 0 || index >= static_cast<int>(entries_.size())) { |
| 1042 | NOTREACHED(); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1046 | if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 1047 | // We should never navigate to an existing initial NavigationEntry that is |
| 1048 | // the initial NavigationEntry for the initial empty document that hasn't |
| 1049 | // been overridden by the synchronous about:blank commit, to preserve |
| 1050 | // legacy behavior where trying to reload when the main frame is on the |
| 1051 | // initial empty document won't result in a navigation. See also |
| 1052 | // https://crbug.com/1277414. |
| 1053 | return; |
| 1054 | } |
| 1055 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1056 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1057 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1058 | DCHECK_EQ(nullptr, pending_entry_); |
| 1059 | DCHECK_EQ(-1, pending_entry_index_); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1060 | |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1061 | pending_entry_ = entries_[index].get(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1062 | pending_entry_index_ = index; |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 1063 | pending_entry_->SetTransitionType(ui::PageTransitionFromInt( |
| 1064 | pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK)); |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1065 | NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id, |
| 1066 | is_browser_initiated); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1067 | } |
| 1068 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1069 | void NavigationControllerImpl::GoToOffset(int offset) { |
toyoshim | 3af4d50 | 2016-03-30 12:38:12 | [diff] [blame] | 1070 | // Note: This is actually reached in unit tests. |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1071 | if (!CanGoToOffset(offset)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1072 | return; |
| 1073 | |
[email protected] | 9ba1405 | 2012-06-22 23:50:03 | [diff] [blame] | 1074 | GoToIndex(GetIndexForOffset(offset)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1075 | } |
| 1076 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 1077 | void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) { |
| 1078 | // Note: This is actually reached in unit tests. |
| 1079 | if (!CanGoToOffset(offset)) |
| 1080 | return; |
| 1081 | |
| 1082 | GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId, |
| 1083 | false /* is_browser_initiated */); |
| 1084 | } |
| 1085 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1086 | #if BUILDFLAG(IS_ANDROID) |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1087 | void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) { |
| 1088 | // Note: This is actually reached in unit tests. |
| 1089 | if (!CanGoToOffsetWithSkipping(offset)) |
| 1090 | return; |
| 1091 | |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 1092 | if (offset == 0) { |
WangHui | 74286d5 | 2021-03-31 16:17:15 | [diff] [blame] | 1093 | GoToIndex(GetIndexForOffset(offset)); |
| 1094 | return; |
| 1095 | } |
| 1096 | int increment = offset > 0 ? 1 : -1; |
| 1097 | // Find the offset without counting skippable entries. |
| 1098 | int target_index = GetIndexForOffset(increment); |
| 1099 | int non_skippable_entries = 0; |
| 1100 | for (int index = target_index; index >= 0 && index < GetEntryCount(); |
| 1101 | index += increment) { |
| 1102 | if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 1103 | non_skippable_entries++; |
| 1104 | |
| 1105 | if (non_skippable_entries == std::abs(offset)) { |
| 1106 | target_index = index; |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | GoToIndex(target_index); |
| 1112 | } |
| 1113 | #endif |
| 1114 | |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1115 | bool NavigationControllerImpl::RemoveEntryAtIndex(int index) { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1116 | if (index == last_committed_entry_index_ || index == pending_entry_index_) |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1117 | return false; |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 1118 | |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 1119 | RemoveEntryAtIndexInternal(index); |
[email protected] | 41374f1 | 2013-07-24 02:49:28 | [diff] [blame] | 1120 | return true; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1121 | } |
| 1122 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1123 | void NavigationControllerImpl::PruneForwardEntries() { |
| 1124 | DiscardNonCommittedEntries(); |
| 1125 | int remove_start_index = last_committed_entry_index_ + 1; |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 1126 | int num_removed = static_cast<int>(entries_.size()) - remove_start_index; |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 1127 | if (num_removed <= 0) |
| 1128 | return; |
| 1129 | entries_.erase(entries_.begin() + remove_start_index, entries_.end()); |
| 1130 | NotifyPrunedEntries(this, remove_start_index /* start index */, |
| 1131 | num_removed /* count */); |
| 1132 | } |
| 1133 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1134 | void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry, |
| 1135 | const GURL& new_url) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1136 | GURL new_virtual_url(new_url); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 1137 | if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite( |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 1138 | &new_virtual_url, entry->GetVirtualURL(), browser_context_)) { |
| 1139 | entry->SetVirtualURL(new_virtual_url); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1143 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL( |
| 1144 | const GURL& url, |
| 1145 | const Referrer& referrer, |
| 1146 | ui::PageTransition transition, |
| 1147 | const std::string& extra_headers) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1148 | LoadURLParams params(url); |
| 1149 | params.referrer = referrer; |
| 1150 | params.transition_type = transition; |
| 1151 | params.extra_headers = extra_headers; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1152 | return LoadURLWithParams(params); |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1153 | } |
| 1154 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1155 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams( |
| 1156 | const LoadURLParams& params) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1157 | if (params.is_renderer_initiated) |
| 1158 | DCHECK(params.initiator_origin.has_value()); |
| 1159 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 1160 | TRACE_EVENT1("browser,navigation", |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1161 | "NavigationControllerImpl::LoadURLWithParams", "url", |
| 1162 | params.url.possibly_invalid_spec()); |
Ian Vollick | 9dda052 | 2019-09-11 02:24:29 | [diff] [blame] | 1163 | bool is_explicit_navigation = |
| 1164 | GetContentClient()->browser()->IsExplicitNavigation( |
| 1165 | params.transition_type); |
| 1166 | if (HandleDebugURL(params.url, params.transition_type, |
| 1167 | is_explicit_navigation)) { |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1168 | // If Telemetry is running, allow the URL load to proceed as if it's |
| 1169 | // unhandled, otherwise Telemetry can't tell if Navigation completed. |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 1170 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1171 | cc::switches::kEnableGpuBenchmarking)) |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1172 | return nullptr; |
[email protected] | 4775298 | 2014-07-29 08:01:43 | [diff] [blame] | 1173 | } |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 1174 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1175 | // Checks based on params.load_type. |
| 1176 | switch (params.load_type) { |
| 1177 | case LOAD_TYPE_DEFAULT: |
lukasza | 477a5a2 | 2016-06-16 18:28:43 | [diff] [blame] | 1178 | case LOAD_TYPE_HTTP_POST: |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1179 | break; |
| 1180 | case LOAD_TYPE_DATA: |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 1181 | if (!params.url.SchemeIs(url::kDataScheme)) { |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1182 | NOTREACHED() << "Data load must use data scheme."; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1183 | return nullptr; |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 1184 | } |
| 1185 | break; |
Lukasz Anforowicz | bb0cfd5e | 2017-12-14 22:39:46 | [diff] [blame] | 1186 | } |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1187 | |
[email protected] | e47ae947 | 2011-10-13 19:48:34 | [diff] [blame] | 1188 | // The user initiated a load, we don't need to reload anymore. |
| 1189 | needs_reload_ = false; |
| 1190 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 1191 | return NavigateWithoutEntry(params); |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1192 | } |
| 1193 | |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1194 | bool NavigationControllerImpl::PendingEntryMatchesRequest( |
| 1195 | NavigationRequest* request) const { |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1196 | return pending_entry_ && |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1197 | pending_entry_->GetUniqueID() == request->nav_entry_id(); |
creis | b4dc933 | 2016-03-14 21:39:19 | [diff] [blame] | 1198 | } |
| 1199 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 1200 | bool NavigationControllerImpl::RendererDidNavigate( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1201 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1202 | const mojom::DidCommitProvisionalLoadParams& params, |
peary2 | 1b0f797b | 2016-09-28 17:28:33 | [diff] [blame] | 1203 | LoadCommittedDetails* details, |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1204 | bool is_same_document_navigation, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1205 | bool was_on_initial_empty_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1206 | bool previous_document_was_activated, |
Camille Lamy | 10aafcd3 | 2018-12-05 15:48:13 | [diff] [blame] | 1207 | NavigationRequest* navigation_request) { |
| 1208 | DCHECK(navigation_request); |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1209 | |
| 1210 | // Note: validation checks and renderer kills due to invalid commit messages |
| 1211 | // must happen before getting here, in |
| 1212 | // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some |
| 1213 | // effects of the navigation have already occurred. |
| 1214 | |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 1215 | if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) && |
| 1216 | GetLastCommittedEntry()) { |
Takashi Toyoshima | a9f6680 | 2021-08-18 02:40:17 | [diff] [blame] | 1217 | // Ensure that this navigation does not add a navigation entry, since |
| 1218 | // ShouldMaintainTrivialSessionHistory() means we should not add an entry |
| 1219 | // beyond the last committed one. Therefore, `should_replace_current_entry` |
| 1220 | // should be set, which replaces the current entry, or this should be a |
| 1221 | // reload, which does not create a new entry. |
| 1222 | DCHECK(params.should_replace_current_entry || |
| 1223 | navigation_request->GetReloadType() != ReloadType::NONE); |
| 1224 | } |
[email protected] | cd2e1574 | 2013-03-08 04:08:31 | [diff] [blame] | 1225 | is_initial_navigation_ = false; |
| 1226 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1227 | // Save the previous state before we clobber it. |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1228 | bool overriding_user_agent_changed = false; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1229 | if (GetLastCommittedEntry()) { |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 1230 | if (entry_replaced_by_post_commit_error_) { |
Chris Bookholt | 27faf8d | 2022-01-20 01:03:33 | [diff] [blame] | 1231 | // Same document navigation events with a post-commit error should already |
| 1232 | // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before |
| 1233 | // reaching here. |
| 1234 | CHECK(!is_same_document_navigation); |
| 1235 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 1236 | // Any commit while a post-commit error page is showing should put the |
| 1237 | // original entry back, replacing the error page's entry. This includes |
| 1238 | // reloads, where the original entry was used as the pending entry and |
| 1239 | // should now be at the correct index at commit time. |
| 1240 | entries_[last_committed_entry_index_] = |
| 1241 | std::move(entry_replaced_by_post_commit_error_); |
| 1242 | } |
Fergal Daly | 8e33cf6 | 2020-12-12 01:06:07 | [diff] [blame] | 1243 | details->previous_main_frame_url = GetLastCommittedEntry()->GetURL(); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 1244 | details->previous_entry_index = GetLastCommittedEntryIndex(); |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1245 | if (pending_entry_ && |
| 1246 | pending_entry_->GetIsOverridingUserAgent() != |
| 1247 | GetLastCommittedEntry()->GetIsOverridingUserAgent()) |
| 1248 | overriding_user_agent_changed = true; |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1249 | #if BUILDFLAG(IS_ANDROID) |
Shu Yang | 5756fa42 | 2021-11-05 23:02:54 | [diff] [blame] | 1250 | // TODO(crbug.com/1266277): Clean up the logic of setting |
| 1251 | // |overriding_user_agent_changed| post-launch. |
Shu Yang | 9eabbb90 | 2021-11-17 00:45:03 | [diff] [blame] | 1252 | if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) { |
Shu Yang | 5756fa42 | 2021-11-05 23:02:54 | [diff] [blame] | 1253 | // Must honor user agent overrides in the |navigation_request|, such as |
| 1254 | // from things like RequestDesktopSiteWebContentsObserverAndroid. As a |
| 1255 | // result, besides comparing |pending_entry_|'s user agent against |
| 1256 | // LastCommittedEntry's, also need to compare |navigation_request|'s user |
| 1257 | // agent against LastCommittedEntry's. |
| 1258 | if (navigation_request->is_overriding_user_agent() != |
| 1259 | GetLastCommittedEntry()->GetIsOverridingUserAgent()) { |
| 1260 | overriding_user_agent_changed = true; |
| 1261 | } |
| 1262 | } |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1263 | #endif // BUILDFLAG(IS_ANDROID) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1264 | } else { |
Gang Wu | 325f03f4 | 2021-02-25 20:00:46 | [diff] [blame] | 1265 | // GetLastCommittedEntry() is null, so this is the first entry. |
Fergal Daly | 8e33cf6 | 2020-12-12 01:06:07 | [diff] [blame] | 1266 | details->previous_main_frame_url = GURL(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1267 | details->previous_entry_index = -1; |
Gang Wu | 325f03f4 | 2021-02-25 20:00:46 | [diff] [blame] | 1268 | if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) { |
| 1269 | // Default setting is NOT override the user agent, so overriding the user |
| 1270 | // agent in first entry should be considered as user agent changed as |
| 1271 | // well. |
| 1272 | overriding_user_agent_changed = true; |
| 1273 | } |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1274 | #if BUILDFLAG(IS_ANDROID) |
Shu Yang | 5756fa42 | 2021-11-05 23:02:54 | [diff] [blame] | 1275 | // TODO(crbug.com/1266277): Clean up the logic of setting |
| 1276 | // |overriding_user_agent_changed| post-launch. |
Shu Yang | 9eabbb90 | 2021-11-17 00:45:03 | [diff] [blame] | 1277 | if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) { |
Shu Yang | 5756fa42 | 2021-11-05 23:02:54 | [diff] [blame] | 1278 | // Must honor user agent overrides in the |navigation_request|, such as |
| 1279 | // from things like RequestDesktopSiteWebContentsObserverAndroid. As a |
| 1280 | // result, besides checking |pending_entry_|'s user agent, also need to |
| 1281 | // check |navigation_request|'s. |
| 1282 | if (navigation_request->is_overriding_user_agent()) { |
| 1283 | overriding_user_agent_changed = true; |
| 1284 | } |
| 1285 | } |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 1286 | #endif // BUILDFLAG(IS_ANDROID) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1287 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1288 | |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1289 | bool is_main_frame_navigation = !rfh->GetParent(); |
| 1290 | |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1291 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1292 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1293 | // For primary frame tree navigations, choose an appropriate |
| 1294 | // BackForwardCacheMetrics to be associated with the NavigationEntry, by |
| 1295 | // either creating a new object or reusing the previous one depending on |
| 1296 | // whether it's a main frame navigation or not. |
| 1297 | scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics; |
| 1298 | if (navigation_request->frame_tree_node()->frame_tree()->type() == |
| 1299 | FrameTree::Type::kPrimary) { |
| 1300 | back_forward_cache_metrics = |
| 1301 | BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics( |
| 1302 | GetLastCommittedEntry(), is_main_frame_navigation, |
| 1303 | params.document_sequence_number); |
| 1304 | } |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1305 | // Notify the last active entry that we have navigated away. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1306 | if (is_main_frame_navigation && !is_same_document_navigation) { |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1307 | if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) { |
| 1308 | if (auto* metrics = navigation_entry->back_forward_cache_metrics()) { |
Hajime Hoshi | c760650 | 2021-04-14 02:42:16 | [diff] [blame] | 1309 | metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request); |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
fdegans | 9caf66a | 2015-07-30 21:10:42 | [diff] [blame] | 1314 | // If there is a pending entry at this point, it should have a SiteInstance, |
| 1315 | // except for restored entries. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1316 | bool was_restored = false; |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1317 | DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() || |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 1318 | pending_entry_->IsRestored()); |
| 1319 | if (pending_entry_ && pending_entry_->IsRestored()) { |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 1320 | pending_entry_->set_restore_type(RestoreType::kNotRestored); |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1321 | was_restored = true; |
toyoshim | 0df1d3a | 2016-09-09 09:52:48 | [diff] [blame] | 1322 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1323 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1324 | // If this is a navigation to a matching pending_entry_ and the SiteInstance |
| 1325 | // has changed, this must be treated as a new navigation with replacement. |
| 1326 | // Set the replacement bit here and ClassifyNavigation will identify this |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1327 | // case and return NEW_ENTRY. |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1328 | if (!rfh->GetParent() && pending_entry_ && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1329 | pending_entry_->GetUniqueID() == |
| 1330 | navigation_request->commit_params().nav_entry_id && |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1331 | pending_entry_->site_instance() && |
| 1332 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
| 1333 | DCHECK_NE(-1, pending_entry_index_); |
| 1334 | // TODO(nasko,creis): Instead of setting this value here, set |
| 1335 | // should_replace_current_entry on the parameters we send to the |
| 1336 | // renderer process as part of CommitNavigation. The renderer should |
| 1337 | // in turn send it back here as part of |params| and it can be just |
| 1338 | // enforced and renderer process terminated on mismatch. |
| 1339 | details->did_replace_entry = true; |
| 1340 | } else { |
| 1341 | // The renderer tells us whether the navigation replaces the current entry. |
| 1342 | details->did_replace_entry = params.should_replace_current_entry; |
| 1343 | } |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 1344 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1345 | // Do navigation-type specific actions. These will make and commit an entry. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1346 | details->type = ClassifyNavigation(rfh, params, navigation_request); |
| 1347 | if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1348 | if (rfh->GetParent()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1349 | // This is a subframe navigation on the initial empty document, which used |
| 1350 | // to not have a NavigationEntry to attach to. Now it can attach to the |
| 1351 | // initial NavigationEntry, and we must ensure that its NavigationEntry |
| 1352 | // will keep the "initial NavigationEntry" status and won't append a new |
| 1353 | // NavigationEntry (it should always do replacement instead). |
| 1354 | // See also https://crbug.com/1277414. |
| 1355 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1356 | // Subframe navigation on initial NavigationEntry must not append a new |
| 1357 | // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This |
| 1358 | // means every subframe navigation that happens while we're on the initial |
| 1359 | // NavigationEntry will always reuse the existing NavigationEntry and |
| 1360 | // just update the corresponding FrameNavigationEntry. |
| 1361 | DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME); |
| 1362 | } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) { |
| 1363 | // This is a navigation that modifies the initial NavigationEntry, either |
| 1364 | // for a replacement or a reload. The initial NavigationEntry should |
| 1365 | // retain its "initial NavigationEntry" status in this case. |
| 1366 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1367 | } else if (navigation_request->is_synchronous_renderer_commit() && |
| 1368 | !navigation_request->IsSameDocument() && !rfh->GetParent() && |
| 1369 | params.should_replace_current_entry) { |
| 1370 | // This is a synchronous about:blank navigation on the main frame, which |
| 1371 | // used to not create a NavigationEntry when we have no NavigationEntry on |
| 1372 | // FrameTree creation. We now have the initial NavigationEntry and are on |
| 1373 | // the initial NavigationEntry. To preserve old behavior, we should still |
| 1374 | // keep the "initial" status for the new NavigationEntry that we will |
| 1375 | // create for this navigation, so that subframe navigations under the |
| 1376 | // synchronously committed about:blank document will never append new |
| 1377 | // NavigationEntry, and instead will just reuse the initial |
| 1378 | // NavigationEntry and modify the corresponding FrameNavigationEntries. |
| 1379 | // See also https://crbug.com/1277414. |
| 1380 | details->should_stay_as_initial_entry = true; |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | DCHECK(!details->should_stay_as_initial_entry || |
| 1384 | GetLastCommittedEntry()->IsInitialEntry()); |
[email protected] | 4bf3522c | 2010-08-19 21:00:20 | [diff] [blame] | 1385 | |
eugenebut | ee08663a | 2017-04-27 17:43:12 | [diff] [blame] | 1386 | // is_same_document must be computed before the entry gets committed. |
Eugene But | 712f03d | 2018-05-22 16:03:44 | [diff] [blame] | 1387 | details->is_same_document = is_same_document_navigation; |
[email protected] | b9d4dfdc | 2013-08-08 00:25:12 | [diff] [blame] | 1388 | |
Lucas Furukawa Gadani | e3f7e79 | 2021-04-22 17:56:07 | [diff] [blame] | 1389 | details->is_prerender_activation = |
| 1390 | navigation_request->IsPrerenderedPageActivation(); |
| 1391 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1392 | // Make sure we do not discard the pending entry for a different ongoing |
| 1393 | // navigation when a same document commit comes in unexpectedly from the |
| 1394 | // renderer. Limit this to a very narrow set of conditions to avoid risks to |
| 1395 | // other navigation types. See https://crbug.com/900036. |
| 1396 | // TODO(crbug.com/926009): Handle history.pushState() as well. |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1397 | bool keep_pending_entry = |
| 1398 | is_same_document_navigation && |
| 1399 | details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY && |
| 1400 | pending_entry_ && !PendingEntryMatchesRequest(navigation_request); |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1401 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 1402 | switch (details->type) { |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1403 | case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1404 | RendererDidNavigateToNewEntry( |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 1405 | rfh, params, details->is_same_document, details->did_replace_entry, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1406 | previous_document_was_activated, navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1407 | break; |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1408 | case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY: |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1409 | RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document, |
| 1410 | was_restored, navigation_request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1411 | keep_pending_entry, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1412 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1413 | case NAVIGATION_TYPE_NEW_SUBFRAME: |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1414 | RendererDidNavigateNewSubframe( |
| 1415 | rfh, params, details->is_same_document, details->did_replace_entry, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1416 | previous_document_was_activated, navigation_request, details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1417 | break; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1418 | case NAVIGATION_TYPE_AUTO_SUBFRAME: |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1419 | if (!RendererDidNavigateAutoSubframe( |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 1420 | rfh, params, details->is_same_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1421 | was_on_initial_empty_document, navigation_request, details)) { |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1422 | // We don't send a notification about auto-subframe PageState during |
| 1423 | // UpdateStateForFrame, since it looks like nothing has changed. Send |
| 1424 | // it here at commit time instead. |
| 1425 | NotifyEntryChanged(GetLastCommittedEntry()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1426 | return false; |
creis | 59d5a47cb | 2016-08-24 23:57:19 | [diff] [blame] | 1427 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1428 | break; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1429 | case NAVIGATION_TYPE_NAV_IGNORE: |
| 1430 | // If a pending navigation was in progress, this canceled it. We should |
| 1431 | // discard it and make sure it is removed from the URL bar. After that, |
| 1432 | // there is nothing we can do with this navigation, so we just return to |
| 1433 | // the caller that nothing has happened. |
| 1434 | if (pending_entry_) |
| 1435 | DiscardNonCommittedEntries(); |
| 1436 | return false; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1437 | case NAVIGATION_TYPE_UNKNOWN: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1438 | NOTREACHED(); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 1439 | break; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1440 | } |
| 1441 | |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1442 | // At this point, we know that the navigation has just completed, so |
| 1443 | // record the time. |
| 1444 | // |
| 1445 | // TODO(akalin): Use "sane time" as described in |
Adam Langley | 4463fb83 | 2018-01-28 22:42:26 | [diff] [blame] | 1446 | // https://www.chromium.org/developers/design-documents/sane-time . |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 1447 | base::Time timestamp = |
| 1448 | time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run()); |
| 1449 | DVLOG(1) << "Navigation finished at (smoothed) timestamp " |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 1450 | << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1451 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 1452 | // If we aren't keeping the pending entry, there shouldn't be one at this |
| 1453 | // point. Clear it again in case any error cases above forgot to do so. |
| 1454 | // TODO(pbos): Consider a CHECK here that verifies that the pending entry has |
| 1455 | // been cleared instead of protecting against it. |
| 1456 | if (!keep_pending_entry) |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1457 | DiscardNonCommittedEntriesWithCommitDetails(details); |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 1458 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1459 | // All committed entries should have nonempty content state so WebKit doesn't |
| 1460 | // get confused when we go back to them (see the function for details). |
creis | 0cade2e | 2017-02-28 06:37:47 | [diff] [blame] | 1461 | DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState."; |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1462 | NavigationEntryImpl* active_entry = GetLastCommittedEntry(); |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 1463 | active_entry->SetTimestamp(timestamp); |
[email protected] | f49737b3 | 2013-08-28 07:51:44 | [diff] [blame] | 1464 | active_entry->SetHttpStatusCode(params.http_status_code); |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1465 | // TODO(altimin, crbug.com/933147): Remove this logic after we are done with |
| 1466 | // implementing back-forward cache. |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1467 | if (back_forward_cache_metrics && |
| 1468 | !active_entry->back_forward_cache_metrics()) { |
Alexander Timin | d2f2e4f2 | 2019-04-02 20:04:53 | [diff] [blame] | 1469 | active_entry->set_back_forward_cache_metrics( |
| 1470 | std::move(back_forward_cache_metrics)); |
| 1471 | } |
Dave Tapuska | a2ab4f25 | 2021-07-08 21:31:28 | [diff] [blame] | 1472 | |
| 1473 | // `back_forward_cache_metrics()` may return null as we do not record |
| 1474 | // back-forward cache metrics for navigations in non-primary frame trees. |
| 1475 | if (active_entry->back_forward_cache_metrics()) { |
| 1476 | active_entry->back_forward_cache_metrics()->DidCommitNavigation( |
| 1477 | navigation_request, |
| 1478 | back_forward_cache_.IsAllowed(navigation_request->GetURL())); |
| 1479 | } |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1480 | |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1481 | // Grab the corresponding FrameNavigationEntry for a few updates, but only if |
| 1482 | // the SiteInstance matches (to avoid updating the wrong entry by mistake). |
| 1483 | // A mismatch can occur if the renderer lies or due to a unique name collision |
| 1484 | // after a race with an OOPIF (see https://crbug.com/616820). |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1485 | FrameNavigationEntry* frame_entry = |
| 1486 | active_entry->GetFrameEntry(rfh->frame_tree_node()); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 1487 | if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance()) |
| 1488 | frame_entry = nullptr; |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1489 | // Make sure we've updated the PageState in one of the helper methods. |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 1490 | // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed. |
| 1491 | if (frame_entry) { |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1492 | DCHECK(params.page_state == frame_entry->page_state()); |
Nasko Oskov | bbcfc000 | 2019-11-20 20:03:20 | [diff] [blame] | 1493 | |
| 1494 | // Remember the bindings the renderer process has at this point, so that |
| 1495 | // we do not grant this entry additional bindings if we come back to it. |
| 1496 | frame_entry->SetBindings(rfh->GetEnabledBindings()); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 1497 | } |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 1498 | |
[email protected] | 97d8f0d | 2013-10-29 16:49:21 | [diff] [blame] | 1499 | // Once it is committed, we no longer need to track several pieces of state on |
| 1500 | // the entry. |
nasko | c753351 | 2016-05-06 17:01:12 | [diff] [blame] | 1501 | active_entry->ResetForCommit(frame_entry); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1502 | |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1503 | // The active entry's SiteInstance should match our SiteInstance. |
[email protected] | a1b9926 | 2013-12-27 21:56:22 | [diff] [blame] | 1504 | // TODO(creis): This check won't pass for subframes until we create entries |
| 1505 | // for subframe navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1506 | if (!rfh->GetParent()) |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1507 | CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance()); |
[email protected] | 49bd30e6 | 2011-03-22 20:12:59 | [diff] [blame] | 1508 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1509 | // Now prep the rest of the details for the notification and broadcast. |
[email protected] | 0f38dc455 | 2011-02-25 11:24:00 | [diff] [blame] | 1510 | details->entry = active_entry; |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1511 | details->is_main_frame = !rfh->GetParent(); |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame] | 1512 | details->http_status_code = params.http_status_code; |
estark | a5635c4 | 2015-07-14 00:06:53 | [diff] [blame] | 1513 | |
arthursonzogni | 7ddc654 | 2021-04-09 09:16:50 | [diff] [blame] | 1514 | active_entry->SetIsOverridingUserAgent( |
| 1515 | navigation_request->is_overriding_user_agent()); |
Scott Violet | c36f746 | 2020-05-06 23:13:03 | [diff] [blame] | 1516 | |
[email protected] | 93f230e0 | 2011-06-01 14:40:00 | [diff] [blame] | 1517 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1518 | |
aelias | 100c919 | 2017-01-13 00:01:43 | [diff] [blame] | 1519 | if (overriding_user_agent_changed) |
| 1520 | delegate_->UpdateOverridingUserAgent(); |
| 1521 | |
creis | 03b4800 | 2015-11-04 00:54:56 | [diff] [blame] | 1522 | // Update the nav_entry_id for each RenderFrameHost in the tree, so that each |
| 1523 | // one knows the latest NavigationEntry it is showing (whether it has |
| 1524 | // committed anything in this navigation or not). This allows things like |
| 1525 | // state and title updates from RenderFrames to apply to the latest relevant |
| 1526 | // NavigationEntry. |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1527 | int nav_entry_id = active_entry->GetUniqueID(); |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1528 | for (FrameTreeNode* node : frame_tree_.Nodes()) |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1529 | node->current_frame_host()->set_nav_entry_id(nav_entry_id); |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 1530 | |
| 1531 | if (navigation_request->IsPrerenderedPageActivation()) { |
| 1532 | BroadcastHistoryOffsetAndLength(); |
| 1533 | // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page |
| 1534 | // is activated. As a result, a "prerenderingchange" event listener sees the |
| 1535 | // history.length which is not updated yet. We should guarantee that |
| 1536 | // history's length and offset should be updated before a |
| 1537 | // "prerenderingchange" event listener runs. One possible approach is to use |
| 1538 | // the same IPC which "prerenderingchange" uses, and propagate history's |
| 1539 | // length and offset together with that. |
| 1540 | } |
| 1541 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1542 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1543 | } |
| 1544 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1545 | NavigationType NavigationControllerImpl::ClassifyNavigation( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1546 | RenderFrameHostImpl* rfh, |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1547 | const mojom::DidCommitProvisionalLoadParams& params, |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1548 | NavigationRequest* navigation_request) { |
Piotr Tworek | bad5128 | 2020-09-30 19:17:59 | [diff] [blame] | 1549 | TraceReturnReason<tracing_category::kNavigation> trace_return( |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1550 | "ClassifyNavigation"); |
| 1551 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1552 | if (params.did_create_new_entry) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1553 | // A new entry. We may or may not have a corresponding pending entry, and |
| 1554 | // this may or may not be the main frame. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1555 | if (!rfh->GetParent()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1556 | trace_return.set_return_reason("new entry, no parent, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1557 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1558 | } |
| 1559 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1560 | // When this is a new subframe navigation, we should have a committed page |
| 1561 | // in which it's a subframe. This may not be the case when an iframe is |
| 1562 | // navigated on a popup navigated to about:blank (the iframe would be |
| 1563 | // written into the popup by script on the main page). For these cases, |
| 1564 | // there isn't any navigation stuff we can do, so just ignore it. |
| 1565 | if (!GetLastCommittedEntry()) { |
| 1566 | trace_return.set_return_reason("new entry, no last committed, ignore"); |
| 1567 | return NAVIGATION_TYPE_NAV_IGNORE; |
| 1568 | } |
| 1569 | |
| 1570 | // Valid subframe navigation. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1571 | trace_return.set_return_reason("new entry, new subframe"); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1572 | return NAVIGATION_TYPE_NEW_SUBFRAME; |
| 1573 | } |
| 1574 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1575 | // We only clear the session history in tests when navigating to a new entry. |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1576 | DCHECK(!params.history_list_was_cleared); |
| 1577 | |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1578 | if (rfh->GetParent()) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1579 | // All manual subframes would be did_create_new_entry and handled above, so |
| 1580 | // we know this is auto. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1581 | if (GetLastCommittedEntry()) { |
| 1582 | trace_return.set_return_reason("subframe, last commmited, auto subframe"); |
| 1583 | return NAVIGATION_TYPE_AUTO_SUBFRAME; |
| 1584 | } |
| 1585 | |
| 1586 | // We ignore subframes created in non-committed pages; we'd appreciate if |
| 1587 | // people stopped doing that. |
| 1588 | trace_return.set_return_reason("subframe, no last commmited, ignore"); |
| 1589 | return NAVIGATION_TYPE_NAV_IGNORE; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1590 | } |
| 1591 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1592 | const int nav_entry_id = navigation_request->commit_params().nav_entry_id; |
| 1593 | if (nav_entry_id == 0) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1594 | // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't |
| 1595 | // create a new page. |
| 1596 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1597 | // Just like above in the did_create_new_entry case, it's possible to |
| 1598 | // scribble onto an uncommitted page. Again, there isn't any navigation |
| 1599 | // stuff that we can do, so ignore it here as well. |
| 1600 | NavigationEntry* last_committed = GetLastCommittedEntry(); |
| 1601 | if (!last_committed) { |
| 1602 | trace_return.set_return_reason("nav entry 0, no last committed, ignore"); |
| 1603 | return NAVIGATION_TYPE_NAV_IGNORE; |
| 1604 | } |
| 1605 | |
Hayato Ito | 2ae4944 | 2021-07-02 02:59:25 | [diff] [blame] | 1606 | // This main frame navigation is not a history navigation (since |
| 1607 | // nav_entry_id is 0), but didn't create a new entry. So this must be a |
| 1608 | // reload or a replacement navigation, which will modify the existing entry. |
| 1609 | // |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1610 | // TODO(nasko): With error page isolation, reloading an existing session |
| 1611 | // history entry can result in change of SiteInstance. Check for such a case |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1612 | // here and classify it as NEW_ENTRY, as such navigations should be treated |
Nasko Oskov | 332593c | 2018-08-16 17:21:34 | [diff] [blame] | 1613 | // as new with replacement. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1614 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1615 | "nav entry 0, last committed, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1616 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1617 | } |
| 1618 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1619 | if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1620 | // If the SiteInstance of the |pending_entry_| does not match the |
| 1621 | // SiteInstance that got committed, treat this as a new navigation with |
| 1622 | // replacement. This can happen if back/forward/reload encounters a server |
| 1623 | // redirect to a different site or an isolated error page gets successfully |
| 1624 | // reloaded into a different SiteInstance. |
| 1625 | if (pending_entry_->site_instance() && |
| 1626 | pending_entry_->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1627 | trace_return.set_return_reason("pending matching nav entry, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1628 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1629 | } |
creis | 77c9aa3 | 2015-09-25 19:59:42 | [diff] [blame] | 1630 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1631 | if (pending_entry_index_ == -1) { |
| 1632 | // In this case, we have a pending entry for a load of a new URL but Blink |
| 1633 | // didn't do a new navigation (params.did_create_new_entry). First check |
| 1634 | // to make sure Blink didn't treat a new cross-process navigation as |
| 1635 | // 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] | 1636 | // we must treat it as NEW rather than the converted reload case below, |
| 1637 | // since the new SiteInstance doesn't match the last committed entry. |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1638 | if (!GetLastCommittedEntry() || |
| 1639 | GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1640 | trace_return.set_return_reason("new pending, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1641 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | // Otherwise, this happens when you press enter in the URL bar to reload. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1645 | // We will create a pending entry, but NavigateWithoutEntry will convert |
| 1646 | // it to a reload since it's the same page and not create a new entry for |
| 1647 | // it. (The user doesn't want to have a new back/forward entry when they |
| 1648 | // do this.) Therefore we want to just ignore the pending entry and go |
| 1649 | // back to where we were (the "existing entry"). |
| 1650 | trace_return.set_return_reason("new pending, existing (same) entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1651 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1652 | } |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1653 | } |
| 1654 | |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 1655 | // Everything below here is assumed to be an existing entry, but if there is |
| 1656 | // no last committed entry, we must consider it a new navigation instead. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1657 | if (!GetLastCommittedEntry()) { |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1658 | trace_return.set_return_reason("no last committed, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1659 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1660 | } |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 1661 | |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 1662 | if (navigation_request->commit_params().intended_as_new_entry) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1663 | // 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] | 1664 | // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may |
| 1665 | // or may not have a pending entry. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1666 | trace_return.set_return_reason("intended as new entry, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1667 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1668 | } |
| 1669 | |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 1670 | if (navigation_request->DidEncounterError() && |
| 1671 | failed_pending_entry_id_ != 0 && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1672 | nav_entry_id == failed_pending_entry_id_) { |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1673 | // If the renderer was going to a new pending entry that got cleared because |
| 1674 | // 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] | 1675 | // by pressing return. Classify as EXISTING_ENTRY because we probably don't |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1676 | // have a pending entry. |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1677 | trace_return.set_return_reason( |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1678 | "unreachable, matching pending, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1679 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1680 | } |
| 1681 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1682 | // 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] | 1683 | int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
Nasko Oskov | ae49e29 | 2020-08-13 02:08:51 | [diff] [blame] | 1684 | trace_return.traced_value()->SetInteger("existing_entry_index", |
| 1685 | existing_entry_index); |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1686 | if (existing_entry_index == -1) { |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 1687 | // The renderer has committed a navigation to an entry that no longer |
| 1688 | // exists. Because the renderer is showing that page, resurrect that entry. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1689 | trace_return.set_return_reason("existing entry -1, new entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1690 | return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1691 | } |
| 1692 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1693 | // Since we weeded out "new" navigations above, we know this is an existing |
| 1694 | // (back/forward) navigation. |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1695 | trace_return.set_return_reason("default return, existing entry"); |
Yoshisato Yanagisawa | 2ccba60 | 2021-11-17 08:24:06 | [diff] [blame] | 1696 | return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY; |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 1697 | } |
| 1698 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1699 | void NavigationControllerImpl::UpdateNavigationEntryDetails( |
| 1700 | NavigationEntryImpl* entry, |
| 1701 | RenderFrameHostImpl* rfh, |
| 1702 | const mojom::DidCommitProvisionalLoadParams& params, |
| 1703 | NavigationRequest* request, |
| 1704 | NavigationEntryImpl::UpdatePolicy update_policy, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1705 | bool is_new_entry, |
| 1706 | LoadCommittedDetails* commit_details) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1707 | // Update the FrameNavigationEntry. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1708 | std::vector<GURL> redirects; |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1709 | entry->AddOrUpdateFrameEntry( |
| 1710 | rfh->frame_tree_node(), update_policy, params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 1711 | params.document_sequence_number, params.navigation_api_key, |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1712 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1713 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1714 | Referrer(*params.referrer), |
| 1715 | request ? request->common_params().initiator_origin : params.origin, |
| 1716 | request ? request->GetRedirectChain() : redirects, params.page_state, |
| 1717 | params.method, params.post_id, nullptr /* blob_url_loader_factory */, |
| 1718 | (request && request->web_bundle_navigation_info()) |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1719 | ? request->web_bundle_navigation_info()->Clone() |
| 1720 | : nullptr, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1721 | (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr), |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1722 | ComputePolicyContainerPoliciesForFrameEntry( |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1723 | rfh, request && request->IsSameDocument(), |
| 1724 | request ? request->common_params().url : params.url)); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1725 | |
| 1726 | if (rfh->GetParent()) { |
| 1727 | // Only modify the NavigationEntry for main frame navigations. |
| 1728 | return; |
| 1729 | } |
| 1730 | if (entry->update_virtual_url_with_url()) |
| 1731 | UpdateVirtualURLToURL(entry, params.url); |
| 1732 | // Don't use the page type from the pending entry. Some interstitial page |
| 1733 | // may have set the type to interstitial. Once we commit, however, the page |
| 1734 | // type must always be normal or error. |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1735 | entry->set_page_type((request && request->DidEncounterError()) |
| 1736 | ? PAGE_TYPE_ERROR |
| 1737 | : PAGE_TYPE_NORMAL); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 1738 | if (commit_details && commit_details->should_stay_as_initial_entry) { |
| 1739 | // Retain the "initial NavigationEntry" status. |
| 1740 | if (request->IsSameDocument()) { |
| 1741 | // If this is for a same-document navigation, the NavigationEntry must be |
| 1742 | // reused and should already be marked as the initial NavigationEntry. |
| 1743 | DCHECK(entry->IsInitialEntry()); |
| 1744 | } else { |
| 1745 | // If this is for a cross-document navigation, it can be caused by a |
| 1746 | // renderer-initiated reload, or the synchronous about:blank commit. Mark |
| 1747 | // "for synchronous about:blank" in the latter case, and also when it is |
| 1748 | // reloading a "for synchronous about:blank" entry. Otherwise, the entry |
| 1749 | // is not for a synchronous about:blank commit. |
| 1750 | NavigationEntryImpl::InitialNavigationEntryState new_state = |
| 1751 | NavigationEntryImpl::InitialNavigationEntryState:: |
| 1752 | kInitialNotForSynchronousAboutBlank; |
| 1753 | if (entry->IsInitialEntryForSynchronousAboutBlank() || |
| 1754 | request->is_synchronous_renderer_commit()) { |
| 1755 | new_state = NavigationEntryImpl::InitialNavigationEntryState:: |
| 1756 | kInitialForSynchronousAboutBlank; |
| 1757 | } |
| 1758 | entry->set_initial_navigation_entry_state(new_state); |
| 1759 | } |
| 1760 | } else if (commit_details && !commit_details->should_stay_as_initial_entry) { |
| 1761 | // Remove the "initial NavigationEntry" status. |
| 1762 | entry->set_initial_navigation_entry_state( |
| 1763 | NavigationEntryImpl::InitialNavigationEntryState::kNonInitial); |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1764 | } |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1765 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1766 | if (is_new_entry) { |
| 1767 | // Some properties of the NavigationEntry are only set when the entry is |
| 1768 | // new (we aren't reusing it). |
| 1769 | entry->SetTransitionType(params.transition); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1770 | entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL() |
| 1771 | : GURL::EmptyGURL()); |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1772 | DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent); |
| 1773 | entry->SetIsOverridingUserAgent(params.is_overriding_user_agent); |
| 1774 | } else { |
| 1775 | // We are reusing the NavigationEntry. The site instance will normally be |
| 1776 | // the same except for a few cases: |
| 1777 | // 1) session restore, when no site instance will be assigned or |
| 1778 | // 2) redirect, when the site instance is reset. |
| 1779 | DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() || |
| 1780 | entry->site_instance() == rfh->GetSiteInstance()); |
| 1781 | } |
| 1782 | } |
| 1783 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1784 | void NavigationControllerImpl::CreateInitialEntry() { |
| 1785 | DCHECK_EQ(entries_.size(), 0u); |
| 1786 | RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host(); |
| 1787 | auto params = mojom::DidCommitProvisionalLoadParams::New(); |
| 1788 | // The initial NavigationEntry's URL is the empty URL. This preserves the old |
| 1789 | // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from |
| 1790 | // before we have initial NavigationEntries. |
| 1791 | params->url = GURL::EmptyGURL(); |
| 1792 | params->http_status_code = 0; |
| 1793 | params->url_is_unreachable = false; |
| 1794 | params->method = "GET"; |
| 1795 | params->should_replace_current_entry = false; |
| 1796 | params->post_id = -1; |
| 1797 | params->embedding_token = base::UnguessableToken::Create(); |
| 1798 | params->navigation_token = base::UnguessableToken::Create(); |
| 1799 | params->did_create_new_entry = true; |
| 1800 | params->origin = rfh->GetLastCommittedOrigin(); |
| 1801 | params->should_update_history = true; |
| 1802 | params->item_sequence_number = 0; |
| 1803 | params->document_sequence_number = 0; |
| 1804 | bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree(); |
| 1805 | params->transition = is_in_fenced_frame_tree |
| 1806 | ? ui::PAGE_TRANSITION_AUTO_SUBFRAME |
| 1807 | : ui::PAGE_TRANSITION_LINK; |
| 1808 | params->referrer = blink::mojom::Referrer::New(); |
| 1809 | |
| 1810 | // Create and insert the initial NavigationEntry. |
| 1811 | auto new_entry = std::make_unique<NavigationEntryImpl>( |
| 1812 | rfh->GetSiteInstance(), params->url, Referrer(*params->referrer), |
| 1813 | rfh->GetLastCommittedOrigin(), std::u16string() /* title */, |
| 1814 | ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */, |
| 1815 | nullptr /* blob_url_loader_factory */, true /* is_initial_entry */); |
| 1816 | UpdateNavigationEntryDetails( |
| 1817 | new_entry.get(), rfh, *params, nullptr /* request */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1818 | NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */, |
| 1819 | nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1820 | |
| 1821 | InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */, |
| 1822 | false /* was_post_commit_error */, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1823 | is_in_fenced_frame_tree, nullptr /* commit_details */); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1824 | } |
| 1825 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1826 | void NavigationControllerImpl::RendererDidNavigateToNewEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1827 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1828 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 1829 | bool is_same_document, |
clamy | 3bf35e3c | 2016-11-10 15:59:44 | [diff] [blame] | 1830 | bool replace_entry, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 1831 | bool previous_document_was_activated, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1832 | NavigationRequest* request, |
| 1833 | LoadCommittedDetails* commit_details) { |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 1834 | std::unique_ptr<NavigationEntryImpl> new_entry; |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 1835 | const absl::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1836 | request->common_params().initiator_origin; |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1837 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1838 | // First check if this is an in-page navigation. If so, clone the current |
| 1839 | // entry instead of looking at the pending entry, because the pending entry |
| 1840 | // does not have any subframe history items. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 1841 | if (is_same_document && GetLastCommittedEntry()) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1842 | FrameNavigationEntry* previous_frame_entry = |
| 1843 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 1844 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 1845 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 1846 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 1847 | rfh->GetSiteInstance(), nullptr, params.url, |
| 1848 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | 82fafba | 2021-03-11 07:07:09 | [diff] [blame] | 1849 | Referrer(*params.referrer), initiator_origin, |
| 1850 | request->GetRedirectChain(), params.page_state, params.method, |
| 1851 | params.post_id, nullptr /* blob_url_loader_factory */, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1852 | nullptr /* web_bundle_navigation_info */, |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 1853 | request->GetSubresourceWebBundleNavigationInfo(), |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 1854 | // We will set the document policies later in this function. |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1855 | nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 1856 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 1857 | // FrameNavigationEntry. |
| 1858 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 1859 | previous_frame_entry->protect_url_in_navigation_api()); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 1860 | |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1861 | new_entry = GetLastCommittedEntry()->CloneAndReplace( |
Harkiran Bolaria | 081c5c2 | 2021-09-13 08:44:53 | [diff] [blame] | 1862 | frame_entry, true, request->frame_tree_node(), frame_tree_.root()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1863 | if (new_entry->GetURL().DeprecatedGetOriginAsURL() != |
| 1864 | params.url.DeprecatedGetOriginAsURL()) { |
jam | a78746e | 2017-02-22 17:21:57 | [diff] [blame] | 1865 | // TODO(jam): we had one report of this with a URL that was redirecting to |
| 1866 | // only tildes. Until we understand that better, don't copy the cert in |
| 1867 | // this case. |
| 1868 | new_entry->GetSSL() = SSLStatus(); |
jam | a78746e | 2017-02-22 17:21:57 | [diff] [blame] | 1869 | } |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1870 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 1871 | // It is expected that |frame_entry| is now owned by |new_entry|. This means |
| 1872 | // that |frame_entry| should now have a reference count of exactly 2: one |
| 1873 | // due to the local variable |frame_entry|, and another due to |new_entry| |
| 1874 | // also retaining one. This should never fail, because it's the main frame. |
| 1875 | CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef()); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1876 | } |
| 1877 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 1878 | // If this is an activation navigation from a prerendered page, transfer the |
| 1879 | // new entry from an entry already created and stored in the |
| 1880 | // NavigationRequest. |new_entry| will not have been set prior to this as |
| 1881 | // |is_same_document| is mutually exclusive with |
| 1882 | // |IsPrerenderedPageActivation|. |
| 1883 | if (request->IsPrerenderedPageActivation()) { |
| 1884 | DCHECK(!is_same_document); |
| 1885 | DCHECK(!new_entry); |
| 1886 | new_entry = request->TakePrerenderNavigationEntry(); |
| 1887 | DCHECK(new_entry); |
| 1888 | } |
| 1889 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1890 | // Only make a copy of the pending entry if it is appropriate for the new |
| 1891 | // document that just loaded. Verify this by checking if the entry corresponds |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1892 | // to the given NavigationRequest. Additionally, coarsely check that: |
csharrison | 9a9142bc4 | 2016-03-01 17:24:04 | [diff] [blame] | 1893 | // 1. The SiteInstance hasn't been assigned to something else. |
| 1894 | // 2. The pending entry was intended as a new entry, rather than being a |
| 1895 | // history navigation that was interrupted by an unrelated, |
| 1896 | // renderer-initiated navigation. |
| 1897 | // TODO(csharrison): Investigate whether we can remove some of the coarser |
| 1898 | // checks. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1899 | if (!new_entry && PendingEntryMatchesRequest(request) && |
| 1900 | pending_entry_index_ == -1 && |
[email protected] | 6dd86ab | 2013-02-27 00:30:34 | [diff] [blame] | 1901 | (!pending_entry_->site_instance() || |
[email protected] | 27dd82fd | 2014-03-03 22:11:43 | [diff] [blame] | 1902 | pending_entry_->site_instance() == rfh->GetSiteInstance())) { |
creis | ef4a0cb | 2015-03-12 19:14:35 | [diff] [blame] | 1903 | new_entry = pending_entry_->Clone(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1904 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 1905 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1906 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1907 | } |
| 1908 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1909 | // For cross-document commits with no matching pending entry, create a new |
| 1910 | // entry. |
creis | f49dfc9 | 2016-07-26 17:05:18 | [diff] [blame] | 1911 | if (!new_entry) { |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 1912 | new_entry = std::make_unique<NavigationEntryImpl>( |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1913 | rfh->GetSiteInstance(), params.url, Referrer(*params.referrer), |
| 1914 | initiator_origin, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 1915 | std::u16string(), // title |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1916 | params.transition, request->IsRendererInitiated(), |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 1917 | nullptr, // blob_url_loader_factory |
| 1918 | false); // is_initial_entry |
[email protected] | f8f93eb | 2012-09-25 03:06:24 | [diff] [blame] | 1919 | |
| 1920 | // Find out whether the new entry needs to update its virtual URL on URL |
| 1921 | // change and set up the entry accordingly. This is needed to correctly |
| 1922 | // update the virtual URL when replaceState is called after a pushState. |
| 1923 | GURL url = params.url; |
| 1924 | bool needs_update = false; |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1925 | // 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] | 1926 | // update the virtual URL based on the new URL. For example, this is needed |
| 1927 | // to show chrome://bookmarks/#1 when the bookmarks webui extension changes |
| 1928 | // the URL. |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1929 | BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( |
| 1930 | &url, browser_context_, &needs_update); |
| 1931 | new_entry->set_update_virtual_url_with_url(needs_update); |
| 1932 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 1933 | new_entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1934 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1935 | } |
| 1936 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 1937 | // TODO(crbug.com/1179428) - determine which parts of the entry need to be set |
| 1938 | // for prerendered contents, if any. This is because prerendering/activation |
| 1939 | // technically won't be creating a new document. Unlike BFCache, prerendering |
| 1940 | // creates a new NavigationEntry rather than using an existing one. |
| 1941 | if (!request->IsPrerenderedPageActivation()) { |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 1942 | UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request, |
| 1943 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1944 | true /* is_new_entry */, commit_details); |
creis | 8b5cd4c | 2015-06-19 00:11:08 | [diff] [blame] | 1945 | |
Harkiran Bolaria | 59290d6 | 2021-03-17 01:53:01 | [diff] [blame] | 1946 | // history.pushState() is classified as a navigation to a new page, but sets |
| 1947 | // is_same_document to true. In this case, we already have the title and |
| 1948 | // favicon available, so set them immediately. |
| 1949 | if (is_same_document && GetLastCommittedEntry()) { |
| 1950 | new_entry->SetTitle(GetLastCommittedEntry()->GetTitle()); |
| 1951 | new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon(); |
| 1952 | } |
[email protected] | 3a868f21 | 2014-08-09 10:41:19 | [diff] [blame] | 1953 | } |
[email protected] | ff64b3e | 2014-05-31 04:07:33 | [diff] [blame] | 1954 | |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1955 | DCHECK(!params.history_list_was_cleared || !replace_entry); |
| 1956 | // The browser requested to clear the session history when it initiated the |
| 1957 | // navigation. Now we know that the renderer has updated its state accordingly |
| 1958 | // and it is safe to also clear the browser side history. |
| 1959 | if (params.history_list_was_cleared) { |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 1960 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame] | 1961 | entries_.clear(); |
| 1962 | last_committed_entry_index_ = -1; |
| 1963 | } |
| 1964 | |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1965 | // If this is a new navigation with replacement and there is a |
| 1966 | // pending_entry_ which matches the navigation reported by the renderer |
| 1967 | // process, then it should be the one replaced, so update the |
| 1968 | // last_committed_entry_index_ to use it. |
| 1969 | if (replace_entry && pending_entry_index_ != -1 && |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 1970 | pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) { |
Nasko Oskov | aee2f86 | 2018-06-15 00:05:52 | [diff] [blame] | 1971 | last_committed_entry_index_ = pending_entry_index_; |
| 1972 | } |
| 1973 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 1974 | SetShouldSkipOnBackForwardUIIfNeeded( |
shivanigithub | e92c33da | 2020-09-14 13:01:41 | [diff] [blame] | 1975 | replace_entry, previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 1976 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
shivanisha | 41f04c5 | 2018-12-12 15:52:05 | [diff] [blame] | 1977 | |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 1978 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 1979 | !request->post_commit_error_page_html().empty(), |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1980 | rfh->frame_tree_node()->IsInFencedFrameTree(), |
| 1981 | commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1982 | } |
| 1983 | |
Charlie Reis | c0f17d2d | 2021-01-12 18:52:49 | [diff] [blame] | 1984 | void NavigationControllerImpl::RendererDidNavigateToExistingEntry( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 1985 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1986 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 1987 | bool is_same_document, |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 1988 | bool was_restored, |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 1989 | NavigationRequest* request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 1990 | bool keep_pending_entry, |
| 1991 | LoadCommittedDetails* commit_details) { |
creis | 26d2263 | 2017-04-21 20:23:56 | [diff] [blame] | 1992 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 1993 | << "that a last committed entry exists."; |
| 1994 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1995 | // We should only get here for main frame navigations. |
avi | 39c1edd3 | 2015-06-04 20:06:00 | [diff] [blame] | 1996 | DCHECK(!rfh->GetParent()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1997 | |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 1998 | NavigationEntryImpl* entry = nullptr; |
Rakina Zata Amni | 153d570 | 2021-09-13 22:48:00 | [diff] [blame] | 1999 | if (request->commit_params().intended_as_new_entry) { |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2000 | // We're guaranteed to have a last committed entry if intended_as_new_entry |
| 2001 | // is true. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2002 | entry = GetLastCommittedEntry(); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2003 | DCHECK(entry); |
| 2004 | |
| 2005 | // If the NavigationRequest matches a new pending entry and is classified as |
| 2006 | // EXISTING_ENTRY, then it is a navigation to the same URL that was |
| 2007 | // converted to a reload, such as a user pressing enter in the omnibox. |
| 2008 | if (pending_entry_ && pending_entry_index_ == -1 && |
| 2009 | pending_entry_->GetUniqueID() == |
| 2010 | request->commit_params().nav_entry_id) { |
| 2011 | // Note: The pending entry will usually have a real ReloadType here, but |
| 2012 | // it can still be ReloadType::NONE in cases that |
| 2013 | // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source). |
| 2014 | |
| 2015 | // If we classified this correctly, the SiteInstance should not have |
| 2016 | // changed. |
| 2017 | CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance()); |
| 2018 | |
| 2019 | // For converted reloads, we assign the entry's unique ID to be that of |
| 2020 | // the new one. Since this is always the result of a user action, we want |
| 2021 | // to dismiss infobars, etc. like a regular user-initiated navigation. |
| 2022 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
| 2023 | |
| 2024 | // The extra headers may have changed due to reloading with different |
| 2025 | // headers. |
| 2026 | entry->set_extra_headers(pending_entry_->extra_headers()); |
| 2027 | } |
| 2028 | // Otherwise, this was intended as a new entry but the pending entry was |
| 2029 | // lost in the meantime and no new entry was created. We are stuck at the |
| 2030 | // last committed entry. |
| 2031 | |
| 2032 | // Even if this is a converted reload from pressing enter in the omnibox, |
| 2033 | // the server could redirect, requiring an update to the SSL status. If this |
| 2034 | // is a same document navigation, though, there's no SSLStatus in the |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2035 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2036 | if (!is_same_document) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2037 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2038 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 2039 | } |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2040 | } else if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2041 | // This is a browser-initiated navigation (back/forward/reload). |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2042 | entry = GetEntryWithUniqueID(nav_entry_id); |
jam | d208b90 | 2016-09-01 16:58:16 | [diff] [blame] | 2043 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2044 | if (is_same_document) { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2045 | // There's no SSLStatus in the NavigationRequest for same document |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2046 | // navigations, so normally we leave |entry|'s SSLStatus as is. However if |
| 2047 | // this was a restored same document navigation entry, then it won't have |
| 2048 | // an SSLStatus. So we need to copy over the SSLStatus from the entry that |
| 2049 | // navigated it. |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2050 | NavigationEntryImpl* last_entry = GetLastCommittedEntry(); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2051 | if (entry->GetURL().DeprecatedGetOriginAsURL() == |
| 2052 | last_entry->GetURL().DeprecatedGetOriginAsURL() && |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2053 | last_entry->GetSSL().initialized && !entry->GetSSL().initialized && |
| 2054 | was_restored) { |
| 2055 | entry->GetSSL() = last_entry->GetSSL(); |
| 2056 | } |
| 2057 | } else { |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2058 | // In rapid back/forward navigations |request| sometimes won't have a cert |
| 2059 | // (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] | 2060 | // otherwise we only reuse the existing cert if the origins match. |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2061 | if (request->GetSSLInfo().has_value() && |
| 2062 | request->GetSSLInfo()->is_valid()) { |
| 2063 | entry->GetSSL() = SSLStatus(*(request->GetSSLInfo())); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2064 | } else if (entry->GetURL().DeprecatedGetOriginAsURL() != |
| 2065 | request->GetURL().DeprecatedGetOriginAsURL()) { |
John Abd-El-Malek | 3f24708 | 2017-12-07 19:02:19 | [diff] [blame] | 2066 | entry->GetSSL() = SSLStatus(); |
| 2067 | } |
jam | 48cea908 | 2017-02-15 06:13:29 | [diff] [blame] | 2068 | } |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2069 | } else { |
| 2070 | // This is renderer-initiated. The only kinds of renderer-initated |
Rakina Zata Amni | 557afb9 | 2021-07-17 04:39:57 | [diff] [blame] | 2071 | // navigations that are EXISTING_ENTRY are same-document navigations that |
| 2072 | // result in replacement (e.g. history.replaceState(), location.replace(), |
| 2073 | // forced replacements for trivial session history contexts). For these |
| 2074 | // cases, we reuse the last committed entry. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2075 | entry = GetLastCommittedEntry(); |
jam | 0576b13 | 2016-09-07 05:13:10 | [diff] [blame] | 2076 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2077 | // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK |
| 2078 | // to avoid misleading interpretations (e.g. URLs paired with |
| 2079 | // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix |
| 2080 | // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK |
| 2081 | // | PAGE_TRANSITION_CLIENT_REDIRECT). |
| 2082 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2083 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry); |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2084 | |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2085 | // 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] | 2086 | // NavigationRequest so don't overwrite the existing entry's SSLStatus. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2087 | if (!is_same_document) |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 2088 | entry->GetSSL() = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2089 | SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo())); |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2090 | } |
| 2091 | DCHECK(entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2092 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2093 | UpdateNavigationEntryDetails(entry, rfh, params, request, |
| 2094 | NavigationEntryImpl::UpdatePolicy::kUpdate, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2095 | false /* is_new_entry */, commit_details); |
creis | 22a7b4c | 2016-04-28 07:20:30 | [diff] [blame] | 2096 | |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2097 | // The redirected to page should not inherit the favicon from the previous |
| 2098 | // page. |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2099 | if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document) |
[email protected] | 91a4ff8 | 2012-10-29 20:29:48 | [diff] [blame] | 2100 | entry->GetFavicon() = FaviconStatus(); |
[email protected] | 5ccd4dc | 2012-10-24 02:28:14 | [diff] [blame] | 2101 | |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2102 | // We should also usually discard the pending entry if it corresponds to a |
| 2103 | // different navigation, since that one is now likely canceled. In rare |
| 2104 | // cases, we leave the pending entry for another navigation in place when we |
| 2105 | // know it is still ongoing, to avoid a flicker in the omnibox (see |
| 2106 | // https://crbug.com/900036). |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2107 | // |
| 2108 | // Note that we need to use the "internal" version since we don't want to |
| 2109 | // actually change any other state, just kill the pointer. |
Peter Boström | d759213 | 2019-01-30 04:50:31 | [diff] [blame] | 2110 | if (!keep_pending_entry) |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2111 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 2112 | |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 2113 | // Update the last committed index to reflect the committed entry. |
avi | cbdc4c1 | 2015-07-01 16:07:11 | [diff] [blame] | 2114 | last_committed_entry_index_ = GetIndexOfEntry(entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2115 | } |
| 2116 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2117 | void NavigationControllerImpl::RendererDidNavigateNewSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2118 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2119 | const mojom::DidCommitProvisionalLoadParams& params, |
eugenebut | 604866f | 2017-05-10 21:35:36 | [diff] [blame] | 2120 | bool is_same_document, |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2121 | bool replace_entry, |
| 2122 | bool previous_document_was_activated, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2123 | NavigationRequest* request, |
| 2124 | LoadCommittedDetails* commit_details) { |
avi | 25f5f9e | 2015-07-17 20:08:26 | [diff] [blame] | 2125 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2126 | ui::PAGE_TRANSITION_MANUAL_SUBFRAME)); |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 2127 | // The NEW_SUBFRAME path should never result in an initial NavigationEntry. |
| 2128 | DCHECK(!commit_details->should_stay_as_initial_entry); |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 2129 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2130 | // Manual subframe navigations just get the current entry cloned so the user |
| 2131 | // can go back or forward to it. The actual subframe information will be |
| 2132 | // stored in the page state for each of those entries. This happens out of |
| 2133 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 2134 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 2135 | << "that a last committed entry exists."; |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2136 | |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2137 | // The DCHECK below documents the fact that we don't know of any situation |
| 2138 | // where |replace_entry| is true for subframe navigations. This simplifies |
| 2139 | // reasoning about the replacement struct for subframes (see |
| 2140 | // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()). |
| 2141 | DCHECK(!replace_entry); |
| 2142 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2143 | // This FrameNavigationEntry might not end up being used in the |
| 2144 | // 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] | 2145 | const absl::optional<url::Origin>& initiator_origin = |
Mohamed Abdelhalim | 833de90 | 2019-09-16 17:41:45 | [diff] [blame] | 2146 | request->common_params().initiator_origin; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2147 | std::unique_ptr<PolicyContainerPolicies> policy_container_policies = |
| 2148 | ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document, |
| 2149 | request->GetURL()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2150 | bool protect_url_in_navigation_api = false; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2151 | if (is_same_document) { |
| 2152 | FrameNavigationEntry* previous_frame_entry = |
| 2153 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2154 | // Try to preserve protect_url_in_navigation_api from the previous |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2155 | // FrameNavigationEntry. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2156 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2157 | previous_frame_entry && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2158 | previous_frame_entry->protect_url_in_navigation_api(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2159 | } else { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2160 | protect_url_in_navigation_api = |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2161 | policy_container_policies && |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2162 | ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2163 | policy_container_policies->referrer_policy); |
| 2164 | } |
| 2165 | |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2166 | auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Charles Reis | d2a509f | 2017-09-27 23:47:48 | [diff] [blame] | 2167 | rfh->frame_tree_node()->unique_name(), params.item_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2168 | params.document_sequence_number, params.navigation_api_key, |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2169 | rfh->GetSiteInstance(), nullptr, params.url, |
| 2170 | GetCommittedOriginForFrameEntry(params, request), |
Rakina Zata Amni | 82fafba | 2021-03-11 07:07:09 | [diff] [blame] | 2171 | Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(), |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2172 | params.page_state, params.method, params.post_id, |
| 2173 | nullptr /* blob_url_loader_factory */, |
Tsuyoshi Horo | 0c60578 | 2020-05-27 00:21:03 | [diff] [blame] | 2174 | request->web_bundle_navigation_info() |
| 2175 | ? request->web_bundle_navigation_info()->Clone() |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2176 | : nullptr, |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 2177 | request->GetSubresourceWebBundleNavigationInfo(), |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2178 | std::move(policy_container_policies), protect_url_in_navigation_api); |
Charles Reis | f4448202 | 2017-10-13 21:15:03 | [diff] [blame] | 2179 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2180 | std::unique_ptr<NavigationEntryImpl> new_entry = |
| 2181 | GetLastCommittedEntry()->CloneAndReplace( |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2182 | std::move(frame_entry), is_same_document, rfh->frame_tree_node(), |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 2183 | frame_tree_.root()); |
creis | e062d54 | 2015-08-25 02:01:55 | [diff] [blame] | 2184 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2185 | SetShouldSkipOnBackForwardUIIfNeeded( |
shivanigithub | e92c33da | 2020-09-14 13:01:41 | [diff] [blame] | 2186 | replace_entry, previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 2187 | request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 2188 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2189 | // 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] | 2190 | // to replace, which can happen due to a unique name change. See |
| 2191 | // https://crbug.com/607205. For now, the call to CloneAndReplace() will |
| 2192 | // delete the |frame_entry| when the function exits if it doesn't get used. |
creis | 96fc5508 | 2015-06-13 06:42:38 | [diff] [blame] | 2193 | |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2194 | InsertOrReplaceEntry(std::move(new_entry), replace_entry, false, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2195 | rfh->frame_tree_node()->IsInFencedFrameTree(), |
| 2196 | commit_details); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2197 | } |
| 2198 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2199 | bool NavigationControllerImpl::RendererDidNavigateAutoSubframe( |
creis | 3da0387 | 2015-02-20 21:12:32 | [diff] [blame] | 2200 | RenderFrameHostImpl* rfh, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 2201 | const mojom::DidCommitProvisionalLoadParams& params, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2202 | bool is_same_document, |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2203 | bool was_on_initial_empty_document, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2204 | NavigationRequest* request, |
| 2205 | LoadCommittedDetails* commit_details) { |
avi | 9f07a0c | 2015-02-18 22:51:29 | [diff] [blame] | 2206 | DCHECK(ui::PageTransitionCoreTypeIs(params.transition, |
| 2207 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
| 2208 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2209 | // We're guaranteed to have a previously committed entry, and we now need to |
| 2210 | // handle navigation inside of a subframe in it without creating a new entry. |
| 2211 | DCHECK(GetLastCommittedEntry()); |
| 2212 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2213 | // For newly created subframes, we don't need to send a commit notification. |
| 2214 | // This is only necessary for history navigations in subframes. |
| 2215 | bool send_commit_notification = false; |
| 2216 | |
Rakina Zata Amni | f6950d55 | 2020-11-24 03:26:10 | [diff] [blame] | 2217 | // If |nav_entry_id| is non-zero and matches an existing entry, this |
| 2218 | // is a history navigation. Update the last committed index accordingly. If |
| 2219 | // we don't recognize the |nav_entry_id|, it might be a recently |
| 2220 | // pruned entry. We'll handle it below. |
| 2221 | if (const int nav_entry_id = request->commit_params().nav_entry_id) { |
| 2222 | int entry_index = GetEntryIndexWithUniqueID(nav_entry_id); |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2223 | if (entry_index != -1 && entry_index != last_committed_entry_index_) { |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2224 | // Make sure that a subframe commit isn't changing the main frame's |
| 2225 | // origin. Otherwise the renderer process may be confused, leading to a |
| 2226 | // URL spoof. We can't check the path since that may change |
| 2227 | // (https://crbug.com/373041). |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2228 | // TODO(creis): For now, restrict this check to HTTP(S) origins, because |
| 2229 | // about:blank, file, and unique origins are more subtle to get right. |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 2230 | // We should use checks similar to RenderFrameHostImpl's |
| 2231 | // CanCommitUrlAndOrigin on the main frame during subframe commits. |
| 2232 | // See https://crbug.com/1209092. |
creis | 37988b9 | 2016-06-10 18:03:57 | [diff] [blame] | 2233 | const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL(); |
| 2234 | const GURL& current_top_url = GetLastCommittedEntry()->GetURL(); |
| 2235 | if (current_top_url.SchemeIsHTTPOrHTTPS() && |
| 2236 | dest_top_url.SchemeIsHTTPOrHTTPS() && |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 2237 | current_top_url.DeprecatedGetOriginAsURL() != |
| 2238 | dest_top_url.DeprecatedGetOriginAsURL()) { |
Chris Bookholt | 10f4b733 | 2022-02-14 18:25:44 | [diff] [blame] | 2239 | bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(), |
creis | fb6eeb6 | 2016-05-10 19:01:51 | [diff] [blame] | 2240 | bad_message::NC_AUTO_SUBFRAME); |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2241 | } |
creis | 3cdc3b0 | 2015-05-29 23:00:47 | [diff] [blame] | 2242 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2243 | // We only need to discard the pending entry in this history navigation |
| 2244 | // case. For newly created subframes, there was no pending entry. |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2245 | last_committed_entry_index_ = entry_index; |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2246 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2247 | |
| 2248 | // History navigations should send a commit notification. |
| 2249 | send_commit_notification = true; |
avi | 98405c2 | 2015-05-21 20:47:06 | [diff] [blame] | 2250 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2251 | } |
[email protected] | f233e423 | 2013-02-23 00:55:14 | [diff] [blame] | 2252 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2253 | // This may be a "new auto" case where we add a new FrameNavigationEntry, or |
| 2254 | // it may be a "history auto" case where we update an existing one. |
| 2255 | NavigationEntryImpl* last_committed = GetLastCommittedEntry(); |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2256 | |
| 2257 | // We may want to update |last_committed|'s FrameNavigationEntry (if one |
| 2258 | // exists), or we may want to clobber it and create a new one. We update in |
| 2259 | // cases where the corresponding FrameNavigationEntry is conceptually similar |
| 2260 | // to the document described by the commit params: same-document |
| 2261 | // navigations, history traversal to an existing entry, and reloads (including |
| 2262 | // a "soft reload" where we navigate to the same url without flagging it as a |
| 2263 | // reload). But in the case of a different document that is not logically |
| 2264 | // related to the committed FrameNavigationEntry's document (cross-document, |
| 2265 | // not same url, not a reload, not a history traversal), we replace rather |
| 2266 | // than update. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2267 | // |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2268 | // In the case where we update, the FrameNavigationEntry will potentially be |
| 2269 | // shared across multiple NavigationEntries, and any updates will be reflected |
| 2270 | // in all of those NavigationEntries. In the replace case, any existing |
| 2271 | // sharing with other NavigationEntries will stop. |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2272 | // |
| 2273 | // When navigating away from the initial empty document, we also update rather |
| 2274 | // than replace. Either update or replace will overwrite the initial empty |
| 2275 | // document state for |last_committed|, but if the FrameNavigationEntry for |
| 2276 | // the initial empty document is shared across multiple NavigationEntries (due |
| 2277 | // to a navigation in another frame), we want to make sure we overwrite the |
| 2278 | // initial empty document state everywhere this FrameNavigationEntry is used, |
| 2279 | // which is accompished by updating the existing FrameNavigationEntry. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2280 | FrameNavigationEntry* last_committed_frame_entry = |
| 2281 | last_committed->GetFrameEntry(rfh->frame_tree_node()); |
| 2282 | NavigationEntryImpl::UpdatePolicy update_policy = |
| 2283 | NavigationEntryImpl::UpdatePolicy::kUpdate; |
| 2284 | if (request->common_params().navigation_type == |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 2285 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT && |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2286 | last_committed_frame_entry && |
Nate Chapin | c7019dd7d | 2021-06-25 18:29:25 | [diff] [blame] | 2287 | last_committed_frame_entry->url() != params.url && |
| 2288 | !was_on_initial_empty_document) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2289 | update_policy = NavigationEntryImpl::UpdatePolicy::kReplace; |
| 2290 | } |
| 2291 | |
Rakina Zata Amni | 3460d38 | 2021-10-29 00:43:37 | [diff] [blame] | 2292 | UpdateNavigationEntryDetails(last_committed, rfh, params, request, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2293 | update_policy, false /* is_new_entry */, |
| 2294 | commit_details); |
creis | 625a0c7d | 2015-03-24 23:17:12 | [diff] [blame] | 2295 | |
creis | 913c63ce | 2016-07-16 19:52:52 | [diff] [blame] | 2296 | return send_commit_notification; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 2297 | } |
| 2298 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2299 | int NavigationControllerImpl::GetIndexOfEntry( |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 2300 | const NavigationEntryImpl* entry) const { |
avi | f16f85a7 | 2015-11-13 18:25:03 | [diff] [blame] | 2301 | for (size_t i = 0; i < entries_.size(); ++i) { |
| 2302 | if (entries_[i].get() == entry) |
| 2303 | return i; |
| 2304 | } |
| 2305 | return -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2306 | } |
| 2307 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2308 | void NavigationControllerImpl::CopyStateFrom(NavigationController* temp, |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2309 | bool needs_reload) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2310 | NavigationControllerImpl* source = |
| 2311 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2312 | // Verify that we look new. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2313 | if (blink::features::IsInitialNavigationEntryEnabled()) { |
| 2314 | DCHECK_EQ(1, GetEntryCount()); |
| 2315 | DCHECK(GetLastCommittedEntry()->IsInitialEntry()); |
| 2316 | } else { |
| 2317 | DCHECK_EQ(0, GetEntryCount()); |
| 2318 | } |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 2319 | DCHECK(!GetPendingEntry()); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2320 | entries_.clear(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2321 | |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2322 | if (source->GetEntryCount() == 0) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2323 | return; |
| 2324 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2325 | |
Francois Doray | eaace78 | 2017-06-21 16:37:24 | [diff] [blame] | 2326 | needs_reload_ = needs_reload; |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 2327 | needs_reload_type_ = NeedsReloadType::kCopyStateFrom; |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2328 | InsertEntriesFrom(source, source->GetEntryCount()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2329 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2330 | for (auto& it : source->session_storage_namespace_map_) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2331 | SessionStorageNamespaceImpl* source_namespace = |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 2332 | static_cast<SessionStorageNamespaceImpl*>(it.second.get()); |
| 2333 | session_storage_namespace_map_[it.first] = source_namespace->Clone(); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2334 | } |
[email protected] | 4e6419c | 2010-01-15 04:50:34 | [diff] [blame] | 2335 | |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 2336 | FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2337 | } |
| 2338 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2339 | void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp, |
| 2340 | bool replace_entry) { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2341 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2342 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2343 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2344 | NavigationControllerImpl* source = |
| 2345 | static_cast<NavigationControllerImpl*>(temp); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2346 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2347 | // Remove all the entries leaving the last committed entry. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2348 | PruneAllButLastCommittedInternal(); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2349 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2350 | // We now have one entry, possibly with a new pending entry. Ensure that |
| 2351 | // adding the entries from source won't put us over the limit. |
| 2352 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | e78a685 | 2013-12-13 08:08:57 | [diff] [blame] | 2353 | if (!replace_entry) |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2354 | source->PruneOldestSkippableEntryIfFull(); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 2355 | |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 2356 | // Insert the entries from source. Ignore any pending entry, since it has not |
| 2357 | // committed in source. |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2358 | int max_source_index = source->last_committed_entry_index_; |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2359 | DCHECK_NE(max_source_index, -1); |
| 2360 | max_source_index++; |
[email protected] | e78a685 | 2013-12-13 08:08:57 | [diff] [blame] | 2361 | |
| 2362 | // Ignore the source's current entry if merging with replacement. |
| 2363 | // TODO(davidben): This should preserve entries forward of the current |
| 2364 | // too. http://crbug.com/317872 |
| 2365 | if (replace_entry && max_source_index > 0) |
| 2366 | max_source_index--; |
| 2367 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2368 | InsertEntriesFrom(source, max_source_index); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2369 | |
| 2370 | // 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] | 2371 | last_committed_entry_index_ = GetEntryCount() - 1; |
[email protected] | 796931a9 | 2011-08-10 01:32:14 | [diff] [blame] | 2372 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2373 | BroadcastHistoryOffsetAndLength(); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 2374 | } |
| 2375 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2376 | bool NavigationControllerImpl::CanPruneAllButLastCommitted() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2377 | // If there is no last committed entry, we cannot prune. Even if there is a |
| 2378 | // pending entry, it may not commit, leaving this WebContents blank, despite |
| 2379 | // possibly giving it new entries via CopyStateFromAndPrune. |
| 2380 | if (last_committed_entry_index_ == -1) |
| 2381 | return false; |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2382 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2383 | // We cannot prune if there is a pending entry at an existing entry index. |
| 2384 | // It may not commit, so we have to keep the last committed entry, and thus |
| 2385 | // there is no sensible place to keep the pending entry. It is ok to have |
| 2386 | // a new pending entry, which can optionally commit as a new navigation. |
| 2387 | if (pending_entry_index_ != -1) |
| 2388 | return false; |
| 2389 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2390 | return true; |
| 2391 | } |
| 2392 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2393 | void NavigationControllerImpl::PruneAllButLastCommitted() { |
| 2394 | PruneAllButLastCommittedInternal(); |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2395 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 2396 | DCHECK_EQ(0, last_committed_entry_index_); |
| 2397 | DCHECK_EQ(1, GetEntryCount()); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2398 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2399 | BroadcastHistoryOffsetAndLength(); |
[email protected] | 9350602e | 2013-02-26 23:27:44 | [diff] [blame] | 2400 | } |
| 2401 | |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2402 | void NavigationControllerImpl::PruneAllButLastCommittedInternal() { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2403 | // It is up to callers to check the invariants before calling this. |
[email protected] | 7936898 | 2013-11-13 01:11:01 | [diff] [blame] | 2404 | CHECK(CanPruneAllButLastCommitted()); |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2405 | |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 2406 | // Erase all entries but the last committed entry. There may still be a |
| 2407 | // new pending entry after this. |
| 2408 | entries_.erase(entries_.begin(), |
| 2409 | entries_.begin() + last_committed_entry_index_); |
| 2410 | entries_.erase(entries_.begin() + 1, entries_.end()); |
| 2411 | last_committed_entry_index_ = 0; |
[email protected] | 97b6c4f | 2010-09-27 19:31:26 | [diff] [blame] | 2412 | } |
| 2413 | |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2414 | void NavigationControllerImpl::DeleteNavigationEntries( |
| 2415 | const DeletionPredicate& deletionPredicate) { |
| 2416 | // It is up to callers to check the invariants before calling this. |
| 2417 | CHECK(CanPruneAllButLastCommitted()); |
| 2418 | std::vector<int> delete_indices; |
| 2419 | for (size_t i = 0; i < entries_.size(); i++) { |
| 2420 | if (i != static_cast<size_t>(last_committed_entry_index_) && |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2421 | deletionPredicate.Run(entries_[i].get())) { |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2422 | delete_indices.push_back(i); |
| 2423 | } |
| 2424 | } |
| 2425 | if (delete_indices.empty()) |
| 2426 | return; |
| 2427 | |
| 2428 | if (delete_indices.size() == GetEntryCount() - 1U) { |
| 2429 | PruneAllButLastCommitted(); |
| 2430 | } else { |
| 2431 | // Do the deletion in reverse to preserve indices. |
Ayu Ishii | 2f82585 | 2022-03-08 19:47:38 | [diff] [blame] | 2432 | for (const auto& index : base::Reversed(delete_indices)) { |
| 2433 | RemoveEntryAtIndex(index); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2434 | } |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 2435 | BroadcastHistoryOffsetAndLength(); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 2436 | } |
| 2437 | delegate()->NotifyNavigationEntriesDeleted(); |
| 2438 | } |
| 2439 | |
Shivani Sharma | 883f5f3 | 2019-02-12 18:20:01 | [diff] [blame] | 2440 | bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) { |
| 2441 | auto* entry = GetEntryAtIndex(index); |
| 2442 | return entry && entry->should_skip_on_back_forward_ui(); |
| 2443 | } |
| 2444 | |
Carlos Caballero | 35ce710c | 2019-09-19 10:59:45 | [diff] [blame] | 2445 | BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() { |
| 2446 | return back_forward_cache_; |
| 2447 | } |
| 2448 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2449 | void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) { |
| 2450 | // It is not safe to call DiscardPendingEntry while NavigateToEntry is in |
| 2451 | // progress, since this will cause a use-after-free. (We only allow this |
| 2452 | // when the tab is being destroyed for shutdown, since it won't return to |
| 2453 | // NavigateToEntry in that case.) http://crbug.com/347742. |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 2454 | CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed()); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2455 | |
| 2456 | if (was_failure && pending_entry_) { |
| 2457 | failed_pending_entry_id_ = pending_entry_->GetUniqueID(); |
| 2458 | } else { |
| 2459 | failed_pending_entry_id_ = 0; |
| 2460 | } |
| 2461 | |
| 2462 | if (pending_entry_) { |
| 2463 | if (pending_entry_index_ == -1) |
| 2464 | delete pending_entry_; |
| 2465 | pending_entry_index_ = -1; |
| 2466 | pending_entry_ = nullptr; |
| 2467 | } |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 2468 | |
| 2469 | // Ensure any refs to the current pending entry are ignored if they get |
| 2470 | // deleted, by clearing the set of known refs. All future pending entries will |
| 2471 | // only be affected by new refs. |
| 2472 | pending_entry_refs_.clear(); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) { |
| 2476 | if (pending_entry_) |
| 2477 | pending_entry_->set_ssl_error(error); |
| 2478 | } |
| 2479 | |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 2480 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2481 | // static |
| 2482 | bool NavigationControllerImpl::ValidateDataURLAsString( |
| 2483 | const scoped_refptr<const base::RefCountedString>& data_url_as_string) { |
| 2484 | if (!data_url_as_string) |
| 2485 | return false; |
| 2486 | |
| 2487 | if (data_url_as_string->size() > kMaxLengthOfDataURLString) |
| 2488 | return false; |
| 2489 | |
| 2490 | // The number of characters that is enough for validating a data: URI. |
| 2491 | // From the GURL's POV, the only important part here is scheme, it doesn't |
| 2492 | // check the actual content. Thus we can take only the prefix of the url, to |
| 2493 | // avoid unneeded copying of a potentially long string. |
| 2494 | const size_t kDataUriPrefixMaxLen = 64; |
| 2495 | GURL data_url( |
| 2496 | std::string(data_url_as_string->front_as<char>(), |
| 2497 | std::min(data_url_as_string->size(), kDataUriPrefixMaxLen))); |
| 2498 | if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme)) |
| 2499 | return false; |
| 2500 | |
| 2501 | return true; |
| 2502 | } |
| 2503 | #endif |
| 2504 | |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2505 | void NavigationControllerImpl::NotifyUserActivation() { |
| 2506 | // When a user activation occurs, ensure that all adjacent entries for the |
| 2507 | // same document clear their skippable bit, so that the history manipulation |
| 2508 | // intervention does not apply to them. |
| 2509 | auto* last_committed_entry = GetLastCommittedEntry(); |
| 2510 | if (!last_committed_entry) |
| 2511 | return; |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2512 | |
shivanigithub | 9936838 | 2021-06-16 18:33:37 | [diff] [blame] | 2513 | if (base::FeatureList::IsEnabled( |
| 2514 | features::kDebugHistoryInterventionNoUserActivation)) { |
| 2515 | return; |
| 2516 | } |
| 2517 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 2518 | SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false); |
Shivani Sharma | 19487703 | 2019-03-07 17:52:47 | [diff] [blame] | 2519 | } |
| 2520 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2521 | bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe( |
| 2522 | RenderFrameHostImpl* render_frame_host, |
Julie Jeongeun Kim | ed2e5ba7 | 2019-09-12 10:14:17 | [diff] [blame] | 2523 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2524 | NavigationEntryImpl* entry = |
| 2525 | GetEntryWithUniqueID(render_frame_host->nav_entry_id()); |
| 2526 | if (!entry) |
| 2527 | return false; |
| 2528 | |
| 2529 | FrameNavigationEntry* frame_entry = |
| 2530 | entry->GetFrameEntry(render_frame_host->frame_tree_node()); |
| 2531 | if (!frame_entry) |
| 2532 | return false; |
| 2533 | |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2534 | // |is_browser_initiated| is false here because a navigation in a new subframe |
| 2535 | // always begins with renderer action (i.e., an HTML element being inserted |
| 2536 | // into the DOM), so it is always renderer-initiated. |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2537 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2538 | render_frame_host->frame_tree_node(), entry, frame_entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2539 | ReloadType::NONE, false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2540 | true /* is_history_navigation_in_new_child */, |
| 2541 | false /* is_browser_initiated */); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2542 | |
| 2543 | if (!request) |
| 2544 | return false; |
| 2545 | |
arthursonzogni | f046d4a | 2019-12-12 19:08:10 | [diff] [blame] | 2546 | request->SetNavigationClient(std::move(*navigation_client)); |
Arthur Hemery | 06173ce | 2019-05-29 12:11:41 | [diff] [blame] | 2547 | |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2548 | render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request), |
| 2549 | ReloadType::NONE); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2550 | |
| 2551 | return true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2552 | } |
| 2553 | |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2554 | bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) { |
| 2555 | NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex()); |
| 2556 | if (!entry) |
| 2557 | return false; |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 2558 | |
| 2559 | if (entry->IsInitialEntryNotForSynchronousAboutBlank()) { |
| 2560 | // We should never navigate to an existing initial NavigationEntry that is |
| 2561 | // the initial NavigationEntry for the initial empty document that hasn't |
| 2562 | // been overridden by the synchronous about:blank commit, to preserve |
| 2563 | // legacy behavior where trying to reload when the main frame is on the |
| 2564 | // initial empty document won't result in a navigation. See also |
| 2565 | // https://crbug.com/1277414. |
| 2566 | return false; |
| 2567 | } |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2568 | FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node); |
| 2569 | if (!frame_entry) |
| 2570 | return false; |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2571 | ReloadType reload_type = ReloadType::NORMAL; |
| 2572 | entry->set_reload_type(reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2573 | std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry( |
John Abd-El-Malek | 5b66913 | 2020-07-14 01:04:14 | [diff] [blame] | 2574 | frame_tree_node, entry, frame_entry, reload_type, |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2575 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2576 | false /* is_history_navigation_in_new_child */, |
| 2577 | true /* is_browser_initiated */); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2578 | if (!request) |
| 2579 | return false; |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2580 | frame_tree_node->navigator().Navigate(std::move(request), reload_type); |
Tsuyoshi Horo | 52fd08e | 2020-07-07 07:03:45 | [diff] [blame] | 2581 | return true; |
| 2582 | } |
| 2583 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 2584 | void NavigationControllerImpl::GoToOffsetInSandboxedFrame( |
| 2585 | int offset, |
| 2586 | int sandbox_frame_tree_node_id) { |
| 2587 | if (!CanGoToOffset(offset)) |
| 2588 | return; |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2589 | GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id, |
| 2590 | false /* is_browser_initiated */); |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 2591 | } |
| 2592 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2593 | void NavigationControllerImpl::NavigateFromFrameProxy( |
| 2594 | RenderFrameHostImpl* render_frame_host, |
| 2595 | const GURL& url, |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2596 | const blink::LocalFrameToken* initiator_frame_token, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2597 | int initiator_process_id, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2598 | const absl::optional<url::Origin>& initiator_origin, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2599 | bool is_renderer_initiated, |
| 2600 | SiteInstance* source_site_instance, |
| 2601 | const Referrer& referrer, |
| 2602 | ui::PageTransition page_transition, |
| 2603 | bool should_replace_current_entry, |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 2604 | blink::NavigationDownloadPolicy download_policy, |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2605 | const std::string& method, |
| 2606 | scoped_refptr<network::ResourceRequestBody> post_body, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 2607 | const std::string& extra_headers, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2608 | network::mojom::SourceLocationPtr source_location, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2609 | scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 2610 | const absl::optional<blink::Impression>& impression, |
| 2611 | base::TimeTicks navigation_start_time) { |
Lukasz Anforowicz | 63f3b943 | 2019-05-30 05:42:58 | [diff] [blame] | 2612 | if (is_renderer_initiated) |
| 2613 | DCHECK(initiator_origin.has_value()); |
| 2614 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2615 | FrameTreeNode* node = render_frame_host->frame_tree_node(); |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2616 | |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2617 | // Don't allow an entry replacement if there is no entry to replace. |
| 2618 | // http://crbug.com/457149 |
| 2619 | if (GetEntryCount() == 0) |
| 2620 | should_replace_current_entry = false; |
| 2621 | |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2622 | // Create a NavigationEntry for the transfer, without making it the pending |
| 2623 | // entry. Subframe transfers should have a clone of the last committed entry |
| 2624 | // with a FrameNavigationEntry for the target frame. Main frame transfers |
| 2625 | // should have a new NavigationEntry. |
| 2626 | // TODO(creis): Make this unnecessary by creating (and validating) the params |
| 2627 | // directly, passing them to the destination RenderFrameHost. See |
| 2628 | // https://crbug.com/536906. |
| 2629 | std::unique_ptr<NavigationEntryImpl> entry; |
Harkiran Bolaria | e1b5158b | 2021-09-16 19:03:26 | [diff] [blame] | 2630 | if (!render_frame_host->is_main_frame()) { |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2631 | // Subframe case: create FrameNavigationEntry. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2632 | if (GetLastCommittedEntry()) { |
| 2633 | entry = GetLastCommittedEntry()->Clone(); |
| 2634 | entry->set_extra_headers(extra_headers); |
| 2635 | // TODO(arthursonzogni): What about |is_renderer_initiated|? |
| 2636 | // Renderer-initiated navigation that target a remote frame are currently |
| 2637 | // classified as browser-initiated when this one has already navigated. |
| 2638 | // See https://crbug.com/722251. |
| 2639 | } else { |
| 2640 | // If there's no last committed entry, create an entry for about:blank |
| 2641 | // with a subframe entry for our destination. |
| 2642 | // TODO(creis): Ensure this case can't exist in https://crbug.com/524208. |
| 2643 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
| 2644 | GURL(url::kAboutBlankURL), referrer, initiator_origin, |
| 2645 | source_site_instance, page_transition, is_renderer_initiated, |
| 2646 | extra_headers, browser_context_, |
| 2647 | nullptr /* blob_url_loader_factory */)); |
| 2648 | } |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 2649 | // TODO(arthursonzogni): What about |is_renderer_initiated|? |
| 2650 | // Renderer-initiated navigation that target a remote frame are currently |
| 2651 | // classified as browser-initiated when this one has already navigated. |
| 2652 | // See https://crbug.com/722251. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 2653 | // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter |
| 2654 | // to CreateNavigationRequestFromLoadParams(), so while kReplace might |
| 2655 | // remove child FrameNavigationEntries (e.g., if this is a cross-process |
| 2656 | // same-document navigation), they will still be present in the |
| 2657 | // committed NavigationEntry that will be referenced to construct the new |
| 2658 | // FrameNavigationEntry tree when this navigation commits. |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2659 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2660 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2661 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2662 | absl::nullopt /* commit_origin */, referrer, initiator_origin, |
Miyoung Shin | 5d77f7207 | 2020-10-09 15:14:20 | [diff] [blame] | 2663 | std::vector<GURL>(), blink::PageState(), method, -1, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 2664 | blob_url_loader_factory, nullptr /* web_bundle_navigation_info */, |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 2665 | nullptr /* subresource_web_bundle_navigation_info */, |
Antonio Sartori | 79d549d | 2021-02-18 12:59:54 | [diff] [blame] | 2666 | nullptr /* policy_container_policies */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2667 | } else { |
| 2668 | // Main frame case. |
| 2669 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 2670 | url, referrer, initiator_origin, source_site_instance, page_transition, |
| 2671 | is_renderer_initiated, extra_headers, browser_context_, |
Hayato Ito | 303654c | 2021-06-30 09:07:54 | [diff] [blame] | 2672 | blob_url_loader_factory)); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2673 | entry->root_node()->frame_entry->set_source_site_instance( |
| 2674 | static_cast<SiteInstanceImpl*>(source_site_instance)); |
| 2675 | entry->root_node()->frame_entry->set_method(method); |
| 2676 | } |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2677 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2678 | bool override_user_agent = false; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2679 | if (GetLastCommittedEntry() && |
| 2680 | GetLastCommittedEntry()->GetIsOverridingUserAgent()) { |
| 2681 | entry->SetIsOverridingUserAgent(true); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2682 | override_user_agent = true; |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2683 | } |
| 2684 | // TODO(creis): Set user gesture and intent received timestamp on Android. |
| 2685 | |
| 2686 | // We may not have successfully added the FrameNavigationEntry to |entry| |
| 2687 | // above (per https://crbug.com/608402), in which case we create it from |
| 2688 | // scratch. This works because we do not depend on |frame_entry| being inside |
| 2689 | // |entry| during NavigateToEntry. This will go away when we shortcut this |
| 2690 | // further in https://crbug.com/536906. |
| 2691 | scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node)); |
| 2692 | if (!frame_entry) { |
Patrick Monette | 50e8bd8 | 2019-06-13 22:40:45 | [diff] [blame] | 2693 | frame_entry = base::MakeRefCounted<FrameNavigationEntry>( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 2694 | node->unique_name(), -1, -1, "", nullptr, |
Nasko Oskov | 18006bc | 2018-12-06 02:53:58 | [diff] [blame] | 2695 | static_cast<SiteInstanceImpl*>(source_site_instance), url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 2696 | absl::nullopt /* origin */, referrer, initiator_origin, |
Rakina Zata Amni | 3a1c0ec | 2021-04-15 03:35:12 | [diff] [blame] | 2697 | std::vector<GURL>(), blink::PageState(), method, -1, |
| 2698 | blob_url_loader_factory, nullptr /* web_bundle_navigation_info */, |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 2699 | nullptr /* subresource_web_bundle_navigation_info */, |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 2700 | nullptr /* policy_container_policies */, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 2701 | false /* protect_url_in_navigation_api */); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2702 | } |
| 2703 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2704 | LoadURLParams params(url); |
Chris Hamilton | 83272dc | 2021-02-23 00:24:02 | [diff] [blame] | 2705 | params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token); |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 2706 | params.initiator_process_id = initiator_process_id; |
Nasko Oskov | 93e7c55c | 2018-12-19 01:59:29 | [diff] [blame] | 2707 | params.initiator_origin = initiator_origin; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2708 | params.source_site_instance = source_site_instance; |
| 2709 | params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT; |
| 2710 | params.transition_type = page_transition; |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2711 | params.frame_tree_node_id = node->frame_tree_node_id(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2712 | params.referrer = referrer; |
| 2713 | /* params.redirect_chain: skip */ |
| 2714 | params.extra_headers = extra_headers; |
| 2715 | params.is_renderer_initiated = is_renderer_initiated; |
| 2716 | params.override_user_agent = UA_OVERRIDE_INHERIT; |
| 2717 | /* params.base_url_for_data_url: skip */ |
| 2718 | /* params.virtual_url_for_data_url: skip */ |
| 2719 | /* params.data_url_as_string: skip */ |
| 2720 | params.post_data = post_body; |
| 2721 | params.can_load_local_resources = false; |
Kevin McNee | e60e76b | 2019-11-27 20:01:58 | [diff] [blame] | 2722 | /* params.should_replace_current_entry: skip */ |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2723 | /* params.frame_name: skip */ |
| 2724 | // TODO(clamy): See if user gesture should be propagated to this function. |
| 2725 | params.has_user_gesture = false; |
| 2726 | params.should_clear_history_list = false; |
| 2727 | params.started_from_context_menu = false; |
| 2728 | /* params.navigation_ui_data: skip */ |
| 2729 | /* params.input_start: skip */ |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 2730 | params.was_activated = blink::mojom::WasActivatedOption::kUnknown; |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 2731 | /* params.reload_type: skip */ |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 2732 | params.impression = impression; |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 2733 | params.download_policy = std::move(download_policy); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 2734 | |
| 2735 | std::unique_ptr<NavigationRequest> request = |
| 2736 | CreateNavigationRequestFromLoadParams( |
Dominic Farolino | 226226af | 2019-06-25 00:58:03 | [diff] [blame] | 2737 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 2738 | false /* has_user_gesture */, std::move(source_location), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 2739 | ReloadType::NONE, entry.get(), frame_entry.get(), |
| 2740 | navigation_start_time); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 2741 | |
| 2742 | if (!request) |
| 2743 | return; |
| 2744 | |
Arthur Hemery | 94874276 | 2019-09-18 10:06:24 | [diff] [blame] | 2745 | // At this stage we are proceeding with this navigation. If this was renderer |
| 2746 | // initiated with user gesture, we need to make sure we clear up potential |
| 2747 | // remains of a cancelled browser initiated navigation to avoid URL spoofs. |
| 2748 | DiscardNonCommittedEntries(); |
| 2749 | |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 2750 | node->navigator().Navigate(std::move(request), ReloadType::NONE); |
clamy | 987a375 | 2018-05-03 17:36:26 | [diff] [blame] | 2751 | } |
| 2752 | |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2753 | void NavigationControllerImpl::SetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2754 | const StoragePartitionConfig& partition_config, |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 2755 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2756 | if (!session_storage_namespace) |
| 2757 | return; |
| 2758 | |
| 2759 | // We can't overwrite an existing SessionStorage without violating spec. |
| 2760 | // Attempts to do so may give a tab access to another tab's session storage |
| 2761 | // so die hard on an error. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2762 | bool successful_insert = |
| 2763 | session_storage_namespace_map_ |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2764 | .insert(std::make_pair(partition_config, |
Aaron Colwell | f3b316e | 2021-03-11 20:17:05 | [diff] [blame] | 2765 | static_cast<SessionStorageNamespaceImpl*>( |
| 2766 | session_storage_namespace))) |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2767 | .second; |
| 2768 | CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace"; |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 2769 | } |
| 2770 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2771 | bool NavigationControllerImpl::IsUnmodifiedBlankTab() { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2772 | return IsInitialNavigation() && |
| 2773 | (!GetLastCommittedEntry() || |
| 2774 | GetLastCommittedEntry()->IsInitialEntry()) && |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 2775 | !frame_tree_.has_accessed_initial_main_document(); |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 2776 | } |
| 2777 | |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2778 | SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace( |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2779 | const StoragePartitionConfig& partition_config) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2780 | StoragePartition* partition = |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 2781 | browser_context_->GetStoragePartition(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2782 | DOMStorageContextWrapper* context_wrapper = |
| 2783 | static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext()); |
| 2784 | |
| 2785 | SessionStorageNamespaceMap::const_iterator it = |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2786 | session_storage_namespace_map_.find(partition_config); |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2787 | if (it != session_storage_namespace_map_.end()) { |
| 2788 | // Ensure that this namespace actually belongs to this partition. |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 2789 | DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get()) |
| 2790 | ->IsFromContext(context_wrapper)); |
Aaron Colwell | b731a0ae | 2021-03-19 19:14:47 | [diff] [blame] | 2791 | |
michaeln | bacbcbd | 2016-02-09 00:32:03 | [diff] [blame] | 2792 | return it->second.get(); |
| 2793 | } |
| 2794 | |
| 2795 | // Create one if no one has accessed session storage for this partition yet. |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2796 | scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace = |
| 2797 | SessionStorageNamespaceImpl::Create(context_wrapper); |
| 2798 | SessionStorageNamespaceImpl* session_storage_namespace_ptr = |
| 2799 | session_storage_namespace.get(); |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2800 | session_storage_namespace_map_[partition_config] = |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2801 | std::move(session_storage_namespace); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2802 | |
Daniel Murphy | 31bbb8b1 | 2018-02-07 21:44:10 | [diff] [blame] | 2803 | return session_storage_namespace_ptr; |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | SessionStorageNamespace* |
| 2807 | NavigationControllerImpl::GetDefaultSessionStorageNamespace() { |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 2808 | return GetSessionStorageNamespace( |
| 2809 | StoragePartitionConfig::CreateDefault(GetBrowserContext())); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | const SessionStorageNamespaceMap& |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2813 | NavigationControllerImpl::GetSessionStorageNamespaceMap() { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 2814 | return session_storage_namespace_map_; |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2815 | } |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2816 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2817 | bool NavigationControllerImpl::NeedsReload() { |
[email protected] | 71fde35 | 2011-12-29 03:29:56 | [diff] [blame] | 2818 | return needs_reload_; |
| 2819 | } |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2820 | |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2821 | void NavigationControllerImpl::SetNeedsReload() { |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 2822 | SetNeedsReload(NeedsReloadType::kRequestedByClient); |
| 2823 | } |
| 2824 | |
| 2825 | void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) { |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2826 | needs_reload_ = true; |
Alex Moshchuk | 7b4f065 | 2019-05-30 18:54:41 | [diff] [blame] | 2827 | needs_reload_type_ = type; |
jaekyun | c8cefa8 | 2015-01-09 20:14:54 | [diff] [blame] | 2828 | |
| 2829 | if (last_committed_entry_index_ != -1) { |
| 2830 | entries_[last_committed_entry_index_]->SetTransitionType( |
| 2831 | ui::PAGE_TRANSITION_RELOAD); |
| 2832 | } |
[email protected] | 46bb5e9c | 2013-10-03 22:16:47 | [diff] [blame] | 2833 | } |
| 2834 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 2835 | void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) { |
Kevin McNee | 0516477 | 2019-09-03 17:24:57 | [diff] [blame] | 2836 | DCHECK_LT(index, GetEntryCount()); |
| 2837 | DCHECK_NE(index, last_committed_entry_index_); |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 2838 | DiscardNonCommittedEntries(); |
| 2839 | |
| 2840 | entries_.erase(entries_.begin() + index); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 2841 | if (last_committed_entry_index_ > index) |
[email protected] | 4303234 | 2011-03-21 14:10:31 | [diff] [blame] | 2842 | last_committed_entry_index_--; |
| 2843 | } |
| 2844 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2845 | NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2846 | // If there is no pending_entry_, there should be no pending_entry_index_. |
| 2847 | DCHECK(pending_entry_ || pending_entry_index_ == -1); |
| 2848 | |
| 2849 | // 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] | 2850 | // at that index. An exception is while a reload of a post commit error page |
| 2851 | // is ongoing; in that case pending entry will point to the entry replaced |
| 2852 | // by the error. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2853 | DCHECK(pending_entry_index_ == -1 || |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2854 | pending_entry_ == GetEntryAtIndex(pending_entry_index_) || |
| 2855 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2856 | |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 2857 | return pending_entry_; |
| 2858 | } |
| 2859 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2860 | int NavigationControllerImpl::GetPendingEntryIndex() { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2861 | // The pending entry index must always be less than the number of entries. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2862 | // If there are no entries, it must be exactly -1. |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2863 | DCHECK_LT(pending_entry_index_, GetEntryCount()); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 2864 | DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 2865 | return pending_entry_index_; |
| 2866 | } |
| 2867 | |
avi | 2576470 | 2015-06-23 15:43:37 | [diff] [blame] | 2868 | void NavigationControllerImpl::InsertOrReplaceEntry( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 2869 | std::unique_ptr<NavigationEntryImpl> entry, |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2870 | bool replace, |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2871 | bool was_post_commit_error, |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2872 | bool in_fenced_frame_tree, |
| 2873 | LoadCommittedDetails* commit_details) { |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 2874 | // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME` |
| 2875 | // set because: |
| 2876 | // 1) They don't influence the history of the outer page. |
| 2877 | // 2) They are always replace only navigation (there is always only one entry |
| 2878 | // in their history stack). |
| 2879 | // 3) Are not top level navigations and appear similar to iframes. |
| 2880 | // Navigations of the fenced frame might create a new NavigationEntry, which |
| 2881 | // will call this function. Non fenced frame navigations will never have |
| 2882 | // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call |
| 2883 | // InsertOrReplaceEntry. |
| 2884 | DCHECK_EQ(in_fenced_frame_tree, |
| 2885 | ui::PageTransitionCoreTypeIs(entry->GetTransitionType(), |
| 2886 | ui::PAGE_TRANSITION_AUTO_SUBFRAME)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2887 | |
avi | 5cad491 | 2015-06-19 05:25:44 | [diff] [blame] | 2888 | // If the pending_entry_index_ is -1, the navigation was to a new page, and we |
| 2889 | // need to keep continuity with the pending entry, so copy the pending entry's |
| 2890 | // unique ID to the committed entry. If the pending_entry_index_ isn't -1, |
| 2891 | // then the renderer navigated on its own, independent of the pending entry, |
| 2892 | // so don't copy anything. |
| 2893 | if (pending_entry_ && pending_entry_index_ == -1) |
| 2894 | entry->set_unique_id(pending_entry_->GetUniqueID()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2895 | |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 2896 | DiscardNonCommittedEntriesWithCommitDetails(commit_details); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2897 | |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 2898 | // When replacing, don't prune the forward history. |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2899 | if ((replace || was_post_commit_error) && entries_.size() > 0) { |
Mikel Astiz | ba9cf2fd | 2017-12-17 10:38:10 | [diff] [blame] | 2900 | CopyReplacedNavigationEntryDataIfPreviouslyEmpty( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 2901 | entries_[last_committed_entry_index_].get(), entry.get()); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2902 | // If the new entry is a post-commit error page, we store the current last |
| 2903 | // committed entry to the side so that we can put it back when navigating |
| 2904 | // away from the error. |
| 2905 | if (was_post_commit_error) { |
| 2906 | DCHECK(!entry_replaced_by_post_commit_error_); |
| 2907 | entry_replaced_by_post_commit_error_ = |
| 2908 | std::move(entries_[last_committed_entry_index_]); |
| 2909 | } |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 2910 | entries_[last_committed_entry_index_] = std::move(entry); |
creis | ee17e93 | 2015-07-17 17:56:22 | [diff] [blame] | 2911 | return; |
| 2912 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2913 | |
creis | 37979a6 | 2015-08-04 19:48:18 | [diff] [blame] | 2914 | // We shouldn't see replace == true when there's no committed entries. |
| 2915 | DCHECK(!replace); |
| 2916 | |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 2917 | PruneForwardEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2918 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2919 | PruneOldestSkippableEntryIfFull(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2920 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 2921 | entries_.push_back(std::move(entry)); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 2922 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2923 | } |
| 2924 | |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2925 | void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() { |
Shivani Sharma | 2d5b4b6b | 2019-01-08 16:07:16 | [diff] [blame] | 2926 | if (entries_.size() < max_entry_count()) |
| 2927 | return; |
| 2928 | |
| 2929 | DCHECK_EQ(max_entry_count(), entries_.size()); |
| 2930 | DCHECK_GT(last_committed_entry_index_, 0); |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2931 | CHECK_EQ(pending_entry_index_, -1); |
| 2932 | |
| 2933 | int index = 0; |
Elly Fong-Jones | ccc6d1f | 2021-06-14 18:32:42 | [diff] [blame] | 2934 | // Retrieve the oldest skippable entry. |
| 2935 | for (; index < GetEntryCount(); index++) { |
| 2936 | if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) |
| 2937 | break; |
Shivani Sharma | d8c8d65 | 2019-02-13 17:27:57 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | // If there is no skippable entry or if it is the last committed entry then |
| 2941 | // fall back to pruning the oldest entry. It is not safe to prune the last |
| 2942 | // committed entry. |
| 2943 | if (index == GetEntryCount() || index == last_committed_entry_index_) |
| 2944 | index = 0; |
| 2945 | |
| 2946 | bool should_succeed = RemoveEntryAtIndex(index); |
| 2947 | DCHECK_EQ(true, should_succeed); |
| 2948 | |
| 2949 | NotifyPrunedEntries(this, index, 1); |
[email protected] | 944822b | 2012-03-02 20:57:25 | [diff] [blame] | 2950 | } |
| 2951 | |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 2952 | void NavigationControllerImpl::NavigateToExistingPendingEntry( |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 2953 | ReloadType reload_type, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2954 | int sandboxed_source_frame_tree_node_id, |
| 2955 | bool is_browser_initiated) { |
Alexander Timin | 3a92df7 | 2019-09-20 11:59:50 | [diff] [blame] | 2956 | TRACE_EVENT0("navigation", |
| 2957 | "NavigationControllerImpl::NavigateToExistingPendingEntry"); |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2958 | DCHECK(pending_entry_); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 2959 | DCHECK(IsInitialNavigation() || pending_entry_index_ != -1); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 2960 | if (pending_entry_index_ != -1) { |
| 2961 | // The pending entry may not be in entries_ if a post-commit error page is |
| 2962 | // showing. |
| 2963 | DCHECK(pending_entry_ == entries_[pending_entry_index_].get() || |
| 2964 | pending_entry_ == entry_replaced_by_post_commit_error_.get()); |
| 2965 | } |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 2966 | DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL())); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 2967 | bool is_forced_reload = needs_reload_; |
[email protected] | 72097fd0 | 2010-01-21 23:36:01 | [diff] [blame] | 2968 | needs_reload_ = false; |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 2969 | FrameTreeNode* root = frame_tree_.root(); |
Arthur Sonzogni | 620cec6 | 2018-12-13 13:08:57 | [diff] [blame] | 2970 | int nav_entry_id = pending_entry_->GetUniqueID(); |
| 2971 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 2972 | // If we were navigating to a slow-to-commit page, and the user performs |
| 2973 | // a session history navigation to the last committed page, RenderViewHost |
| 2974 | // will force the throbber to start, but WebKit will essentially ignore the |
| 2975 | // navigation, and won't send a message to stop the throbber. To prevent this |
| 2976 | // from happening, we drop the navigation here and stop the slow-to-commit |
| 2977 | // page from loading (which would normally happen during the navigation). |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 2978 | if (pending_entry_index_ == last_committed_entry_index_ && |
Lukasz Anforowicz | 6b75c0d | 2020-12-01 22:56:08 | [diff] [blame] | 2979 | !pending_entry_->IsRestored() && |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 2980 | pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) { |
Takashi Toyoshima | bf54947 | 2021-07-01 07:47:20 | [diff] [blame] | 2981 | frame_tree_.StopLoading(); |
[email protected] | 6a13a6c | 2011-12-20 21:47:12 | [diff] [blame] | 2982 | |
[email protected] | 83c2e23 | 2011-10-07 21:36:46 | [diff] [blame] | 2983 | DiscardNonCommittedEntries(); |
| 2984 | return; |
| 2985 | } |
| 2986 | |
creis | ce0ef357 | 2017-01-26 17:53:08 | [diff] [blame] | 2987 | // Compare FrameNavigationEntries to see which frames in the tree need to be |
| 2988 | // navigated. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 2989 | std::vector<std::unique_ptr<NavigationRequest>> same_document_loads; |
| 2990 | std::vector<std::unique_ptr<NavigationRequest>> different_document_loads; |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 2991 | FindFramesToNavigate(root, reload_type, is_browser_initiated, |
| 2992 | &same_document_loads, &different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 2993 | |
| 2994 | if (same_document_loads.empty() && different_document_loads.empty()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 2995 | // We were unable to match any frames to navigate. This can happen if a |
| 2996 | // history navigation targets a subframe that no longer exists |
| 2997 | // (https://crbug.com/705550). In this case, we need to update the current |
| 2998 | // history entry to the pending one but keep the main document loaded. We |
| 2999 | // also need to ensure that observers are informed about the updated |
| 3000 | // current history entry (e.g., for greying out back/forward buttons), and |
| 3001 | // that renderer processes update their history offsets. The easiest way |
| 3002 | // to do all that is to schedule a "redundant" same-document navigation in |
| 3003 | // the main frame. |
| 3004 | // |
| 3005 | // Note that we don't want to remove this history entry, as it might still |
| 3006 | // be valid later, since a frame that it's targeting may be recreated. |
| 3007 | // |
| 3008 | // TODO(alexmos, creis): This behavior isn't ideal, as the user would |
| 3009 | // need to repeat history navigations until finding the one that works. |
| 3010 | // Consider changing this behavior to keep looking for the first valid |
| 3011 | // history entry that finds frames to navigate. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3012 | std::unique_ptr<NavigationRequest> navigation_request = |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3013 | CreateNavigationRequestFromEntry( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3014 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3015 | ReloadType::NONE /* reload_type */, |
| 3016 | true /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3017 | false /* is_history_navigation_in_new_child */, |
| 3018 | is_browser_initiated); |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3019 | if (!navigation_request) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3020 | // If this navigation cannot start, delete the pending NavigationEntry. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3021 | DiscardPendingEntry(false); |
| 3022 | return; |
| 3023 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3024 | same_document_loads.push_back(std::move(navigation_request)); |
| 3025 | |
| 3026 | // Sanity check that we never take this branch for any kinds of reloads, |
| 3027 | // as those should've queued a different-document load in the main frame. |
| 3028 | DCHECK(!is_forced_reload); |
| 3029 | DCHECK_EQ(reload_type, ReloadType::NONE); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3030 | } |
| 3031 | |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3032 | // If |sandboxed_source_frame_node_id| is valid, then track whether this |
| 3033 | // navigation affects any frame outside the frame's subtree. |
| 3034 | if (sandboxed_source_frame_tree_node_id != |
| 3035 | FrameTreeNode::kFrameTreeNodeInvalidId) { |
| 3036 | bool navigates_inside_tree = |
| 3037 | DoesSandboxNavigationStayWithinSubtree( |
| 3038 | sandboxed_source_frame_tree_node_id, same_document_loads) && |
| 3039 | DoesSandboxNavigationStayWithinSubtree( |
| 3040 | sandboxed_source_frame_tree_node_id, different_document_loads); |
Dave Tapuska | 716ed3af | 2019-09-23 18:45:50 | [diff] [blame] | 3041 | // Count the navigations as web use counters so we can determine |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3042 | // the number of pages that trigger this. |
| 3043 | FrameTreeNode* sandbox_source_frame_tree_node = |
| 3044 | FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id); |
| 3045 | if (sandbox_source_frame_tree_node) { |
| 3046 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 3047 | sandbox_source_frame_tree_node->current_frame_host(), |
| 3048 | navigates_inside_tree |
| 3049 | ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree |
| 3050 | : blink::mojom::WebFeature:: |
| 3051 | kSandboxBackForwardAffectsFramesOutsideSubtree); |
| 3052 | } |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3053 | |
| 3054 | // If the navigation occurred outside the tree discard it because |
| 3055 | // the sandboxed frame didn't have permission to navigate outside |
| 3056 | // its tree. If it is possible that the navigation is both inside and |
| 3057 | // outside the frame tree and we discard it entirely because we don't |
| 3058 | // want to end up in a history state that didn't exist before. |
Dominic Farolino | 05744004 | 2022-01-19 18:18:14 | [diff] [blame] | 3059 | if (!navigates_inside_tree) { |
Dave Tapuska | 855c1e1 | 2019-08-23 20:45:52 | [diff] [blame] | 3060 | DiscardPendingEntry(false); |
| 3061 | return; |
| 3062 | } |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3063 | } |
| 3064 | |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3065 | // BackForwardCache: |
| 3066 | // Navigate immediately if the document is in the BackForwardCache. |
| 3067 | if (back_forward_cache_.GetEntry(nav_entry_id)) { |
| 3068 | TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest"); |
| 3069 | DCHECK_EQ(reload_type, ReloadType::NONE); |
| 3070 | auto navigation_request = CreateNavigationRequestFromEntry( |
| 3071 | root, pending_entry_, pending_entry_->GetFrameEntry(root), |
| 3072 | ReloadType::NONE, false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3073 | false /* is_history_navigation_in_new_child */, is_browser_initiated); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3074 | root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE); |
Carlos Caballero | 539a421c | 2020-07-06 10:25:57 | [diff] [blame] | 3075 | |
| 3076 | return; |
| 3077 | } |
| 3078 | |
| 3079 | // History navigation might try to reuse a specific BrowsingInstance, already |
| 3080 | // used by a page in the cache. To avoid having two different main frames that |
| 3081 | // live in the same BrowsingInstance, evict the all pages with this |
| 3082 | // BrowsingInstance from the cache. |
| 3083 | // |
| 3084 | // For example, take the following scenario: |
| 3085 | // |
| 3086 | // A1 = Some page on a.com |
| 3087 | // A2 = Some other page on a.com |
| 3088 | // B3 = An uncacheable page on b.com |
| 3089 | // |
| 3090 | // Then the following navigations occur: |
| 3091 | // A1->A2->B3->A1 |
| 3092 | // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't |
| 3093 | // take its place) and A1 will be created in the same BrowsingInstance (and |
| 3094 | // SiteInstance), as A2. |
| 3095 | // |
| 3096 | // If we didn't do anything, both A1 and A2 would remain alive in the same |
| 3097 | // BrowsingInstance/SiteInstance, which is unsupported by |
| 3098 | // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict |
| 3099 | // A2 from the cache. |
| 3100 | if (pending_entry_->site_instance()) { |
| 3101 | back_forward_cache_.EvictFramesInRelatedSiteInstances( |
| 3102 | pending_entry_->site_instance()); |
| 3103 | } |
| 3104 | |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3105 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3106 | CHECK(!in_navigate_to_pending_entry_); |
| 3107 | in_navigate_to_pending_entry_ = true; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3108 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3109 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3110 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3111 | // function. |
| 3112 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3113 | |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3114 | // Send all the same document frame loads before the different document loads. |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3115 | for (auto& item : same_document_loads) { |
| 3116 | FrameTreeNode* frame = item->frame_tree_node(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3117 | frame->navigator().Navigate(std::move(item), reload_type); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3118 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3119 | for (auto& item : different_document_loads) { |
| 3120 | FrameTreeNode* frame = item->frame_tree_node(); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3121 | frame->navigator().Navigate(std::move(item), reload_type); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3122 | } |
clamy | 3cb9bea9 | 2018-07-10 12:42:02 | [diff] [blame] | 3123 | |
| 3124 | in_navigate_to_pending_entry_ = false; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3125 | } |
| 3126 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3127 | NavigationControllerImpl::HistoryNavigationAction |
| 3128 | NavigationControllerImpl::DetermineActionForHistoryNavigation( |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3129 | FrameTreeNode* frame, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3130 | ReloadType reload_type) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3131 | RenderFrameHostImpl* render_frame_host = frame->current_frame_host(); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3132 | // Only active and prerendered documents are allowed to navigate in their |
| 3133 | // frame. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3134 | if (render_frame_host->lifecycle_state() != |
Sreeja Kamishetty | 299329ad | 2021-03-25 14:06:01 | [diff] [blame] | 3135 | RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) { |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3136 | // - If the document is in pending deletion, the browser already committed |
| 3137 | // to destroying this RenderFrameHost. See https://crbug.com/930278. |
| 3138 | // - If the document is in back-forward cache, it's not allowed to navigate |
| 3139 | // as it should remain frozen. Ignore the request and evict the document |
| 3140 | // from back-forward cache. |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3141 | // |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3142 | // If the document is inactive, there's no need to recurse into subframes, |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3143 | // which should all be inactive as well. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3144 | if (frame->current_frame_host()->IsInactiveAndDisallowActivation( |
| 3145 | DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) { |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3146 | return HistoryNavigationAction::kStopLooking; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3147 | } |
Sreeja Kamishetty | 8eacabb | 2021-03-09 11:45:42 | [diff] [blame] | 3148 | } |
arthursonzogni | 03f7615 | 2019-02-12 10:35:20 | [diff] [blame] | 3149 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3150 | // If there's no last committed entry, there is no previous history entry to |
| 3151 | // compare against, so fall back to a different-document load. Note that we |
| 3152 | // should only reach this case for the root frame and not descend further |
| 3153 | // into subframes. |
| 3154 | if (!GetLastCommittedEntry()) { |
| 3155 | DCHECK(frame->IsMainFrame()); |
| 3156 | return HistoryNavigationAction::kDifferentDocument; |
| 3157 | } |
| 3158 | |
| 3159 | // Reloads should result in a different-document load. Note that reloads may |
| 3160 | // also happen via the |needs_reload_| mechanism where the reload_type is |
| 3161 | // NONE, so detect this by comparing whether we're going to the same |
| 3162 | // entry that we're currently on. Similarly to above, only main frames |
| 3163 | // should reach this. Note that subframes support reloads, but that's done |
| 3164 | // via a different path that doesn't involve FindFramesToNavigate (see |
| 3165 | // RenderFrameHost::Reload()). |
| 3166 | if (reload_type != ReloadType::NONE || |
| 3167 | pending_entry_index_ == last_committed_entry_index_) { |
| 3168 | DCHECK(frame->IsMainFrame()); |
| 3169 | return HistoryNavigationAction::kDifferentDocument; |
| 3170 | } |
| 3171 | |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3172 | // If there is no new FrameNavigationEntry for the frame, ignore the |
| 3173 | // load. For example, this may happen when going back to an entry before a |
| 3174 | // frame was created. Suppose we commit a same-document navigation that also |
| 3175 | // results in adding a new subframe somewhere in the tree. If we go back, |
| 3176 | // the new subframe will be missing a FrameNavigationEntry in the previous |
| 3177 | // NavigationEntry, but we shouldn't delete or change what's loaded in |
| 3178 | // it. |
| 3179 | // |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3180 | // Note that in this case, there is no need to keep looking for navigations |
| 3181 | // in subframes, which would be missing FrameNavigationEntries as well. |
| 3182 | // |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3183 | // It's important to check this before checking |old_item| below, since both |
| 3184 | // might be null, and in that case we still shouldn't change what's loaded in |
| 3185 | // this frame. Note that scheduling any loads assumes that |new_item| is |
| 3186 | // non-null. See https://crbug.com/1088354. |
| 3187 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3188 | if (!new_item) |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3189 | return HistoryNavigationAction::kStopLooking; |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3190 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3191 | // If there is no old FrameNavigationEntry, schedule a different-document |
| 3192 | // load. |
| 3193 | // |
creis | 225a743 | 2016-06-03 22:56:27 | [diff] [blame] | 3194 | // TODO(creis): Store the last committed FrameNavigationEntry to use here, |
| 3195 | // rather than assuming the NavigationEntry has up to date info on subframes. |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3196 | FrameNavigationEntry* old_item = |
| 3197 | GetLastCommittedEntry()->GetFrameEntry(frame); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3198 | if (!old_item) |
| 3199 | return HistoryNavigationAction::kDifferentDocument; |
| 3200 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3201 | // If the new item is not in the same SiteInstance, schedule a |
| 3202 | // different-document load. Newly restored items may not have a SiteInstance |
| 3203 | // yet, in which case it will be assigned on first commit. |
| 3204 | if (new_item->site_instance() && |
| 3205 | new_item->site_instance() != old_item->site_instance()) |
| 3206 | return HistoryNavigationAction::kDifferentDocument; |
| 3207 | |
| 3208 | // Schedule a different-document load if the current RenderFrameHost is not |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3209 | // live. This case can happen for Ctrl+Back or after a renderer crash. Note |
| 3210 | // that we do this even if the history navigation would not be modifying this |
| 3211 | // frame were it live. |
| 3212 | if (!frame->current_frame_host()->IsRenderFrameLive()) |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3213 | return HistoryNavigationAction::kDifferentDocument; |
| 3214 | |
| 3215 | if (new_item->item_sequence_number() != old_item->item_sequence_number()) { |
danakj | 25c436d | 2021-04-01 16:35:31 | [diff] [blame] | 3216 | // Starting a navigation after a crash early-promotes the speculative |
| 3217 | // RenderFrameHost. Then we have a RenderFrameHost with no document in it |
| 3218 | // committed yet, so we can not possibly perform a same-document history |
| 3219 | // navigation. The frame would need to be reloaded with a cross-document |
| 3220 | // navigation. |
| 3221 | if (!frame->current_frame_host()->has_committed_any_navigation()) |
| 3222 | return HistoryNavigationAction::kDifferentDocument; |
| 3223 | |
creis | 5413169 | 2016-08-12 18:32:25 | [diff] [blame] | 3224 | // Same document loads happen if the previous item has the same document |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3225 | // sequence number but different item sequence number. |
| 3226 | if (new_item->document_sequence_number() == |
| 3227 | old_item->document_sequence_number()) { |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3228 | return HistoryNavigationAction::kSameDocument; |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3229 | } |
avi | b48cb31 | 2016-05-05 21:35:00 | [diff] [blame] | 3230 | |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3231 | // Otherwise, if both item and document sequence numbers differ, this |
| 3232 | // should be a different document load. |
| 3233 | return HistoryNavigationAction::kDifferentDocument; |
| 3234 | } |
| 3235 | |
| 3236 | // If the item sequence numbers match, there is no need to navigate this |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3237 | // frame. Keep looking for navigations in this frame's children. |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3238 | DCHECK_EQ(new_item->document_sequence_number(), |
| 3239 | old_item->document_sequence_number()); |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3240 | return HistoryNavigationAction::kKeepLooking; |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | void NavigationControllerImpl::FindFramesToNavigate( |
| 3244 | FrameTreeNode* frame, |
| 3245 | ReloadType reload_type, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3246 | bool is_browser_initiated, |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3247 | std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads, |
| 3248 | std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) { |
| 3249 | DCHECK(pending_entry_); |
| 3250 | FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame); |
| 3251 | |
| 3252 | auto action = DetermineActionForHistoryNavigation(frame, reload_type); |
| 3253 | |
| 3254 | if (action == HistoryNavigationAction::kSameDocument) { |
| 3255 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3256 | CreateNavigationRequestFromEntry( |
| 3257 | frame, pending_entry_, new_item, reload_type, |
| 3258 | true /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3259 | false /* is_history_navigation_in_new_child */, |
| 3260 | is_browser_initiated); |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3261 | if (navigation_request) { |
| 3262 | // Only add the request if was properly created. It's possible for the |
| 3263 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3264 | same_document_loads->push_back(std::move(navigation_request)); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3265 | } |
Alex Moshchuk | 3a4e77a | 2020-05-29 21:32:57 | [diff] [blame] | 3266 | } else if (action == HistoryNavigationAction::kDifferentDocument) { |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3267 | std::unique_ptr<NavigationRequest> navigation_request = |
| 3268 | CreateNavigationRequestFromEntry( |
| 3269 | frame, pending_entry_, new_item, reload_type, |
| 3270 | false /* is_same_document_history_load */, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3271 | false /* is_history_navigation_in_new_child */, |
| 3272 | is_browser_initiated); |
Lei Zhang | 9603153 | 2019-10-10 19:05:47 | [diff] [blame] | 3273 | if (navigation_request) { |
| 3274 | // Only add the request if was properly created. It's possible for the |
| 3275 | // creation to fail in certain cases, e.g. when the URL is invalid. |
| 3276 | different_document_loads->push_back(std::move(navigation_request)); |
| 3277 | } |
| 3278 | // For a different document, the subframes will be destroyed, so there's |
| 3279 | // no need to consider them. |
| 3280 | return; |
Alex Moshchuk | e65c3927 | 2020-06-03 17:55:37 | [diff] [blame] | 3281 | } else if (action == HistoryNavigationAction::kStopLooking) { |
| 3282 | return; |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | for (size_t i = 0; i < frame->child_count(); i++) { |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3286 | FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated, |
| 3287 | same_document_loads, different_document_loads); |
creis | 4e2ecb7 | 2015-06-20 00:46:30 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3291 | base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry( |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3292 | const LoadURLParams& params) { |
| 3293 | // Find the appropriate FrameTreeNode. |
| 3294 | FrameTreeNode* node = nullptr; |
| 3295 | if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId || |
| 3296 | !params.frame_name.empty()) { |
| 3297 | node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 3298 | ? frame_tree_.FindByID(params.frame_tree_node_id) |
| 3299 | : frame_tree_.FindByName(params.frame_name); |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3300 | DCHECK(!node || node->frame_tree() == &frame_tree_); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | // If no FrameTreeNode was specified, navigate the main frame. |
| 3304 | if (!node) |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 3305 | node = frame_tree_.root(); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3306 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3307 | // Compute overrides to the LoadURLParams for |override_user_agent|, |
| 3308 | // |should_replace_current_entry| and |has_user_gesture| that will be used |
| 3309 | // both in the creation of the NavigationEntry and the NavigationRequest. |
| 3310 | // Ideally, the LoadURLParams themselves would be updated, but since they are |
| 3311 | // passed as a const reference, this is not possible. |
| 3312 | // TODO(clamy): When we only create a NavigationRequest, move this to |
| 3313 | // CreateNavigationRequestFromLoadURLParams. |
| 3314 | bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent, |
| 3315 | GetLastCommittedEntry()); |
| 3316 | |
| 3317 | // Don't allow an entry replacement if there is no entry to replace. |
| 3318 | // http://crbug.com/457149 |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3319 | // |
| 3320 | // If there is an entry, an entry replacement must happen if the current |
| 3321 | // browsing context should maintain a trivial session history. |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 3322 | bool should_replace_current_entry = |
| 3323 | (params.should_replace_current_entry || |
| 3324 | ShouldMaintainTrivialSessionHistory(node)) && |
| 3325 | entries_.size(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3326 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3327 | // Javascript URLs should not create NavigationEntries. All other navigations |
| 3328 | // do, including navigations to chrome renderer debug URLs. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3329 | if (!params.url.SchemeIs(url::kJavaScriptScheme)) { |
Scott Violet | 5ae6c42e | 2020-10-28 02:47:37 | [diff] [blame] | 3330 | std::unique_ptr<NavigationEntryImpl> entry = |
| 3331 | CreateNavigationEntryFromLoadParams(node, params, override_user_agent, |
| 3332 | should_replace_current_entry, |
| 3333 | params.has_user_gesture); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3334 | DiscardPendingEntry(false); |
| 3335 | SetPendingEntry(std::move(entry)); |
| 3336 | } |
| 3337 | |
| 3338 | // Renderer-debug URLs are sent to the renderer process immediately for |
| 3339 | // processing and don't need to create a NavigationRequest. |
| 3340 | // Note: this includes navigations to JavaScript URLs, which are considered |
| 3341 | // renderer-debug URLs. |
| 3342 | // Note: we intentionally leave the pending entry in place for renderer debug |
| 3343 | // URLs, unlike the cases below where we clear it if the navigation doesn't |
| 3344 | // proceed. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 3345 | if (blink::IsRendererDebugURL(params.url)) { |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3346 | // Renderer-debug URLs won't go through NavigationThrottlers so we have to |
| 3347 | // check them explicitly. See bug 913334. |
Aaron Colwell | e1908d98 | 2020-06-26 22:08:15 | [diff] [blame] | 3348 | if (GetContentClient()->browser()->ShouldBlockRendererDebugURL( |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3349 | params.url, browser_context_)) { |
| 3350 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3351 | return nullptr; |
Oleg Davydov | 2cc0167b | 2019-02-05 14:32:48 | [diff] [blame] | 3352 | } |
| 3353 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3354 | HandleRendererDebugURL(node, params.url); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3355 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3356 | } |
| 3357 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3358 | DCHECK(pending_entry_); |
| 3359 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3360 | // Convert navigations to the current URL to a reload. |
| 3361 | // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of |
| 3362 | // relying on the frame tree node id from LoadURLParams. Unfortunately, |
| 3363 | // DevTools sometimes issues navigations to main frames that they do not |
| 3364 | // expect to see treated as reload, and it only works because they pass a |
| 3365 | // FrameTreeNode id in their LoadURLParams. Change this once they no longer do |
| 3366 | // that. See https://crbug.com/850926. |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3367 | ReloadType reload_type = params.reload_type; |
| 3368 | if (reload_type == ReloadType::NONE && |
| 3369 | ShouldTreatNavigationAsReload( |
Fergal Daly | 766177d | 2020-07-07 07:54:04 | [diff] [blame] | 3370 | node, params.url, pending_entry_->GetVirtualURL(), |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3371 | params.base_url_for_data_url, params.transition_type, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3372 | params.load_type == |
| 3373 | NavigationController::LOAD_TYPE_HTTP_POST /* is_post */, |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 3374 | should_replace_current_entry, GetLastCommittedEntry())) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3375 | reload_type = ReloadType::NORMAL; |
Alexander Timin | b70f6738 | 2020-12-10 00:03:47 | [diff] [blame] | 3376 | pending_entry_->set_reload_type(reload_type); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3377 | |
| 3378 | // If this is a reload of an existing FrameNavigationEntry and we had a |
| 3379 | // policy container for it, then we should copy it into the pending entry, |
| 3380 | // so that it is copied to the navigation request in |
| 3381 | // CreateNavigationRequestFromLoadParams later. |
| 3382 | if (GetLastCommittedEntry()) { |
| 3383 | FrameNavigationEntry* previous_frame_entry = |
| 3384 | GetLastCommittedEntry()->GetFrameEntry(node); |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3385 | if (previous_frame_entry && |
| 3386 | previous_frame_entry->policy_container_policies()) { |
| 3387 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 3388 | previous_frame_entry->policy_container_policies()->Clone()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | // 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] | 3394 | // document, then we should copy the document polices from RenderFrameHost's |
| 3395 | // PolicyContainerHost. The NavigationRequest will create a new |
| 3396 | // PolicyContainerHost with the document policies from the |pending_entry_|, |
| 3397 | // and that PolicyContainerHost will be put in the final RenderFrameHost for |
| 3398 | // the navigation. This way, we ensure that we keep enforcing the right |
| 3399 | // policies on the initial empty document after the reload. |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 3400 | if ((!GetLastCommittedEntry() || GetLastCommittedEntry()->IsInitialEntry()) && |
| 3401 | params.url.IsAboutBlank()) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3402 | if (node->current_frame_host() && |
| 3403 | node->current_frame_host()->policy_container_host()) { |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3404 | pending_entry_->GetFrameEntry(node)->set_policy_container_policies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 3405 | node->current_frame_host() |
| 3406 | ->policy_container_host() |
| 3407 | ->policies() |
| 3408 | .Clone()); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3409 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | // navigation_ui_data should only be present for main frame navigations. |
| 3413 | DCHECK(node->IsMainFrame() || !params.navigation_ui_data); |
| 3414 | |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3415 | // This will be used to set the Navigation Timing API navigationStart |
| 3416 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 3417 | // "Open link in new tab"). If the navigation must wait on the current |
| 3418 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 3419 | // will be updated when the BeforeUnload ack is received. |
| 3420 | const auto navigation_start_time = base::TimeTicks::Now(); |
| 3421 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3422 | std::unique_ptr<NavigationRequest> request = |
| 3423 | CreateNavigationRequestFromLoadParams( |
| 3424 | node, params, override_user_agent, should_replace_current_entry, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3425 | params.has_user_gesture, network::mojom::SourceLocation::New(), |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3426 | reload_type, pending_entry_, pending_entry_->GetFrameEntry(node), |
| 3427 | navigation_start_time); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3428 | |
| 3429 | // If the navigation couldn't start, return immediately and discard the |
| 3430 | // pending NavigationEntry. |
| 3431 | if (!request) { |
| 3432 | DiscardPendingEntry(false); |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3433 | return nullptr; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3434 | } |
| 3435 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3436 | #if DCHECK_IS_ON() |
| 3437 | // Safety check that NavigationRequest and NavigationEntry match. |
| 3438 | ValidateRequestMatchesEntry(request.get(), pending_entry_); |
| 3439 | #endif |
| 3440 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3441 | // This call does not support re-entrancy. See http://crbug.com/347742. |
| 3442 | CHECK(!in_navigate_to_pending_entry_); |
| 3443 | in_navigate_to_pending_entry_ = true; |
| 3444 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 3445 | // It is not possible to delete the pending NavigationEntry while navigating |
| 3446 | // to it. Grab a reference to delay potential deletion until the end of this |
| 3447 | // function. |
| 3448 | std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry(); |
| 3449 | |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3450 | base::WeakPtr<NavigationHandle> created_navigation_handle( |
| 3451 | request->GetWeakPtr()); |
Lukasz Anforowicz | 9ee83c27 | 2020-12-01 20:14:05 | [diff] [blame] | 3452 | node->navigator().Navigate(std::move(request), reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3453 | |
| 3454 | in_navigate_to_pending_entry_ = false; |
Harkiran Bolaria | ba823e4 | 2021-05-21 18:30:36 | [diff] [blame] | 3455 | return created_navigation_handle; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3456 | } |
| 3457 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3458 | void NavigationControllerImpl::HandleRendererDebugURL( |
| 3459 | FrameTreeNode* frame_tree_node, |
| 3460 | const GURL& url) { |
| 3461 | if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) { |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3462 | // Any renderer-side debug URLs or javascript: URLs should be ignored if |
| 3463 | // the renderer process is not live, unless it is the initial navigation |
| 3464 | // of the tab. |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3465 | if (!IsInitialNavigation()) { |
| 3466 | DiscardNonCommittedEntries(); |
| 3467 | return; |
| 3468 | } |
Fergal Daly | ecd3b020 | 2020-06-25 01:57:37 | [diff] [blame] | 3469 | // The current frame is always a main frame. If IsInitialNavigation() is |
| 3470 | // true then there have been no navigations and any frames of this tab must |
| 3471 | // be in the same renderer process. If that has crashed then the only frame |
| 3472 | // that can be revived is the main frame. |
| 3473 | frame_tree_node->render_manager() |
| 3474 | ->InitializeMainRenderFrameForImmediateUse(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3475 | } |
| 3476 | frame_tree_node->current_frame_host()->HandleRendererDebugURL(url); |
| 3477 | } |
| 3478 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3479 | std::unique_ptr<NavigationEntryImpl> |
| 3480 | NavigationControllerImpl::CreateNavigationEntryFromLoadParams( |
| 3481 | FrameTreeNode* node, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3482 | const LoadURLParams& params, |
| 3483 | bool override_user_agent, |
| 3484 | bool should_replace_current_entry, |
| 3485 | bool has_user_gesture) { |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3486 | // Browser initiated navigations might not have a blob_url_loader_factory set |
| 3487 | // in params even if the navigation is to a blob URL. If that happens, lookup |
| 3488 | // the correct url loader factory to use here. |
| 3489 | auto blob_url_loader_factory = params.blob_url_loader_factory; |
Kinuko Yasuda | 7d925ea2 | 2019-08-01 10:08:48 | [diff] [blame] | 3490 | if (!blob_url_loader_factory && params.url.SchemeIsBlob()) { |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3491 | // Resolve the blob URL in the storage partition associated with the target |
| 3492 | // frame. This is the storage partition the URL will be loaded in, and only |
| 3493 | // 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] | 3494 | blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl( |
Marijn Kruisselbrink | 8ffda44 | 2020-09-03 18:29:47 | [diff] [blame] | 3495 | node->current_frame_host()->GetStoragePartition(), params.url); |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 3496 | } |
| 3497 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3498 | std::unique_ptr<NavigationEntryImpl> entry; |
Tommy C. Li | 03eee77a | 2019-02-05 02:07:44 | [diff] [blame] | 3499 | // extra_headers in params are \n separated; navigation entries want \r\n. |
| 3500 | std::string extra_headers_crlf; |
| 3501 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3502 | |
| 3503 | // For subframes, create a pending entry with a corresponding frame entry. |
| 3504 | if (!node->IsMainFrame()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 3505 | if (GetLastCommittedEntry()) { |
| 3506 | entry = GetLastCommittedEntry()->Clone(); |
| 3507 | } else { |
| 3508 | // If there's no last committed entry, create an entry for about:blank |
| 3509 | // with a subframe entry for our destination. |
| 3510 | // TODO(creis): Ensure this case can't exist in https://crbug.com/524208. |
| 3511 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
| 3512 | GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin, |
| 3513 | params.source_site_instance.get(), params.transition_type, |
| 3514 | params.is_renderer_initiated, extra_headers_crlf, browser_context_, |
| 3515 | blob_url_loader_factory)); |
| 3516 | } |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3517 | entry->AddOrUpdateFrameEntry( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 3518 | node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr, |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3519 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get()), |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 3520 | params.url, absl::nullopt, params.referrer, params.initiator_origin, |
Miyoung Shin | 5d77f7207 | 2020-10-09 15:14:20 | [diff] [blame] | 3521 | params.redirect_chain, blink::PageState(), "GET", -1, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3522 | blob_url_loader_factory, nullptr /* web_bundle_navigation_info */, |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 3523 | nullptr /* subresource_web_bundle_navigation_info */, |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 3524 | // If in NavigateWithoutEntry we later determine that this navigation is |
Charlie Reis | 7e2cb6d | 2021-01-26 01:27:16 | [diff] [blame] | 3525 | // a conversion of a new navigation into a reload, we will set the right |
| 3526 | // document policies there. |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 3527 | nullptr /* policy_container_policies */); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3528 | } else { |
| 3529 | // Otherwise, create a pending entry for the main frame. |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3530 | entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry( |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 3531 | params.url, params.referrer, params.initiator_origin, |
Lukasz Anforowicz | 641234d5 | 2019-11-07 21:07:10 | [diff] [blame] | 3532 | params.source_site_instance.get(), params.transition_type, |
| 3533 | params.is_renderer_initiated, extra_headers_crlf, browser_context_, |
Hayato Ito | 303654c | 2021-06-30 09:07:54 | [diff] [blame] | 3534 | blob_url_loader_factory)); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3535 | entry->set_source_site_instance( |
| 3536 | static_cast<SiteInstanceImpl*>(params.source_site_instance.get())); |
| 3537 | entry->SetRedirectChain(params.redirect_chain); |
| 3538 | } |
| 3539 | |
| 3540 | // Set the FTN ID (only used in non-site-per-process, for tests). |
| 3541 | entry->set_frame_tree_node_id(node->frame_tree_node_id()); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3542 | entry->set_should_clear_history_list(params.should_clear_history_list); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3543 | entry->SetIsOverridingUserAgent(override_user_agent); |
| 3544 | entry->set_has_user_gesture(has_user_gesture); |
Robert Ogden | 011a808 | 2019-01-23 19:04:54 | [diff] [blame] | 3545 | entry->set_reload_type(params.reload_type); |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3546 | |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3547 | switch (params.load_type) { |
| 3548 | case LOAD_TYPE_DEFAULT: |
| 3549 | break; |
| 3550 | case LOAD_TYPE_HTTP_POST: |
| 3551 | entry->SetHasPostData(true); |
| 3552 | entry->SetPostData(params.post_data); |
| 3553 | break; |
| 3554 | case LOAD_TYPE_DATA: |
| 3555 | entry->SetBaseURLForDataURL(params.base_url_for_data_url); |
| 3556 | entry->SetVirtualURL(params.virtual_url_for_data_url); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3557 | #if BUILDFLAG(IS_ANDROID) |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3558 | entry->SetDataURLAsString(params.data_url_as_string); |
| 3559 | #endif |
| 3560 | entry->SetCanLoadLocalResources(params.can_load_local_resources); |
| 3561 | break; |
clamy | 21718cc2 | 2018-06-13 13:34:24 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | // TODO(clamy): NavigationEntry is meant for information that will be kept |
| 3565 | // after the navigation ended and therefore is not appropriate for |
| 3566 | // started_from_context_menu. Move started_from_context_menu to |
| 3567 | // NavigationUIData. |
| 3568 | entry->set_started_from_context_menu(params.started_from_context_menu); |
| 3569 | |
| 3570 | return entry; |
| 3571 | } |
| 3572 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3573 | std::unique_ptr<NavigationRequest> |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3574 | NavigationControllerImpl::CreateNavigationRequestFromLoadParams( |
| 3575 | FrameTreeNode* node, |
| 3576 | const LoadURLParams& params, |
| 3577 | bool override_user_agent, |
| 3578 | bool should_replace_current_entry, |
| 3579 | bool has_user_gesture, |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3580 | network::mojom::SourceLocationPtr source_location, |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3581 | ReloadType reload_type, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3582 | NavigationEntryImpl* entry, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3583 | FrameNavigationEntry* frame_entry, |
| 3584 | base::TimeTicks navigation_start_time) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3585 | DCHECK_EQ(-1, GetIndexOfEntry(entry)); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3586 | DCHECK(frame_entry); |
Nasko Oskov | 3c2f9e25 | 2019-01-10 17:45:53 | [diff] [blame] | 3587 | // All renderer-initiated navigations must have an initiator_origin. |
| 3588 | DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value()); |
Camille Lamy | ff7c482 | 2018-11-07 15:42:51 | [diff] [blame] | 3589 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3590 | GURL url_to_load; |
| 3591 | GURL virtual_url; |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 3592 | absl::optional<url::Origin> origin_to_commit = |
| 3593 | frame_entry ? frame_entry->committed_origin() : absl::nullopt; |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3594 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3595 | // For main frames, rewrite the URL if necessary and compute the virtual URL |
| 3596 | // that should be shown in the address bar. |
| 3597 | if (node->IsMainFrame()) { |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3598 | bool ignored_reverse_on_redirect = false; |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3599 | RewriteUrlForNavigation(params.url, browser_context_, &url_to_load, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3600 | &virtual_url, &ignored_reverse_on_redirect); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3601 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3602 | // For DATA loads, override the virtual URL. |
| 3603 | if (params.load_type == LOAD_TYPE_DATA) |
| 3604 | virtual_url = params.virtual_url_for_data_url; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3605 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3606 | if (virtual_url.is_empty()) |
| 3607 | virtual_url = url_to_load; |
| 3608 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3609 | CHECK(virtual_url == entry->GetVirtualURL()); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3610 | |
Aran Gilman | 249eb12 | 2019-12-02 23:32:46 | [diff] [blame] | 3611 | // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic |
| 3612 | // behavior: it is possible for two calls to RewriteUrlForNavigation to |
| 3613 | // return different results, leading to a different URL in the |
| 3614 | // NavigationRequest and FrameEntry. This will be fixed once we remove the |
| 3615 | // pending NavigationEntry, as we'll only make one call to |
| 3616 | // RewriteUrlForNavigation. |
| 3617 | VLOG_IF(1, (url_to_load != frame_entry->url())) |
| 3618 | << "NavigationRequest and FrameEntry have different URLs: " |
| 3619 | << url_to_load << " vs " << frame_entry->url(); |
Camille Lamy | b9ed3c5 | 2018-11-19 15:34:28 | [diff] [blame] | 3620 | |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3621 | // TODO(clamy): In order to remove the pending NavigationEntry, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 3622 | // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3623 | // NavigationRequest. |
| 3624 | } else { |
| 3625 | url_to_load = params.url; |
| 3626 | virtual_url = params.url; |
Camille Lamy | f664f762 | 2019-01-07 19:28:24 | [diff] [blame] | 3627 | CHECK(!frame_entry || url_to_load == frame_entry->url()); |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 3628 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3629 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 3630 | if (node->render_manager()->is_attaching_inner_delegate()) { |
| 3631 | // Avoid starting any new navigations since this node is now preparing for |
| 3632 | // attaching an inner delegate. |
| 3633 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 3634 | } |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3635 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3636 | if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load)) |
| 3637 | return nullptr; |
| 3638 | |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 3639 | if (!DoesURLMatchOriginForNavigation( |
| 3640 | url_to_load, origin_to_commit, |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 3641 | frame_entry->subresource_web_bundle_navigation_info(), entry, |
| 3642 | node->IsMainFrame())) { |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3643 | DCHECK(false) << " url:" << url_to_load |
| 3644 | << " origin:" << origin_to_commit.value(); |
| 3645 | return nullptr; |
| 3646 | } |
| 3647 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3648 | // Look for a pending commit that is to another document in this |
| 3649 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 3650 | // current URL by the time this navigation commits. |
| 3651 | bool has_pending_cross_document_commit = |
| 3652 | node->render_manager()->HasPendingCommitForCrossDocumentNavigation(); |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 3653 | bool is_currently_error_page = node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3654 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3655 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3656 | /*old_url=*/node->current_url(), |
| 3657 | /*new_url=*/url_to_load, reload_type, entry, *frame_entry, |
| 3658 | has_pending_cross_document_commit, is_currently_error_page, |
| 3659 | /*is_same_document_history_load=*/false); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3660 | |
| 3661 | // Create the NavigationParams based on |params|. |
| 3662 | |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 3663 | bool is_view_source_mode = entry->IsViewSourceMode(); |
| 3664 | DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme)); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3665 | |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 3666 | blink::NavigationDownloadPolicy download_policy = params.download_policy; |
Hiroki Nakagawa | 4ed6128 | 2021-06-18 05:37:23 | [diff] [blame] | 3667 | // Update |download_policy| if the virtual URL is view-source. |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3668 | if (is_view_source_mode) |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 3669 | download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource); |
Charlie Harrison | 8c113a3 | 2019-01-07 16:08:29 | [diff] [blame] | 3670 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3671 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 3672 | blink::mojom::CommonNavigationParams::New( |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3673 | url_to_load, params.initiator_origin, |
| 3674 | blink::mojom::Referrer::New(params.referrer.url, |
| 3675 | params.referrer.policy), |
Scott Violet | cf6ea7e | 2021-06-09 21:09:21 | [diff] [blame] | 3676 | params.transition_type, navigation_type, download_policy, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3677 | should_replace_current_entry, params.base_url_for_data_url, |
Tsuyoshi Horo | 167ca643 | 2022-03-09 05:16:39 | [diff] [blame] | 3678 | navigation_start_time, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3679 | params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET", |
Antonio Sartori | 2f763d9d | 2021-04-21 10:04:14 | [diff] [blame] | 3680 | params.post_data, std::move(source_location), |
arthursonzogni | af7c62c5 | 2020-02-12 10:49:41 | [diff] [blame] | 3681 | params.started_from_context_menu, has_user_gesture, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 3682 | false /* has_text_fragment_token */, |
| 3683 | network::mojom::CSPDisposition::CHECK, std::vector<int>(), |
| 3684 | params.href_translate, |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3685 | false /* is_history_navigation_in_new_child_frame */, |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 3686 | params.input_start, network::mojom::RequestDestination::kEmpty); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3687 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3688 | blink::mojom::CommitNavigationParamsPtr commit_params = |
| 3689 | blink::mojom::CommitNavigationParams::New( |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 3690 | frame_entry->committed_origin(), |
| 3691 | // The correct storage key will be computed before committing the |
| 3692 | // navigation. |
| 3693 | blink::StorageKey(), network::mojom::WebSandboxFlags(), |
arthursonzogni | d5a8d0b | 2021-03-11 17:36:43 | [diff] [blame] | 3694 | override_user_agent, params.redirect_chain, |
Lucas Furukawa Gadani | 81e294b | 2019-08-29 16:26:32 | [diff] [blame] | 3695 | std::vector<network::mojom::URLResponseHeadPtr>(), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3696 | std::vector<net::RedirectInfo>(), |
| 3697 | std::string() /* post_content_type */, common_params->url, |
| 3698 | common_params->method, params.can_load_local_resources, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3699 | frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(), |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3700 | entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */, |
| 3701 | -1 /* pending_history_list_offset */, |
| 3702 | params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(), |
| 3703 | params.should_clear_history_list ? 0 : GetEntryCount(), |
| 3704 | false /* was_discarded */, is_view_source_mode, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3705 | params.should_clear_history_list, |
| 3706 | blink::mojom::NavigationTiming::New(), |
Minggang Wang | f59db47b | 2021-06-16 01:56:22 | [diff] [blame] | 3707 | blink::mojom::WasActivatedOption::kUnknown, |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3708 | base::UnguessableToken::Create() /* navigation_token */, |
Minggang Wang | 7ee0c74 | 2021-06-16 16:16:51 | [diff] [blame] | 3709 | std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(), |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3710 | #if BUILDFLAG(IS_ANDROID) |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3711 | std::string(), /* data_url_as_string */ |
| 3712 | #endif |
arthursonzogni | 1437978 | 2020-05-15 09:09:27 | [diff] [blame] | 3713 | !params.is_renderer_initiated, /* is_browser_initiated */ |
Tsuyoshi Horo | e86d770 | 2019-11-29 01:52:47 | [diff] [blame] | 3714 | GURL() /* web_bundle_physical_url */, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 3715 | GURL() /* base_url_override_for_web_bundle */, |
Yue Ru Sun | 12880493 | 2020-09-30 22:19:17 | [diff] [blame] | 3716 | ukm::kInvalidSourceId /* document_ukm_source_id */, |
Jiewei Qian | 0406fc0 | 2020-03-09 06:02:07 | [diff] [blame] | 3717 | node->pending_frame_policy(), |
Domenic Denicola | 4778c3539 | 2020-06-25 21:25:16 | [diff] [blame] | 3718 | std::vector<std::string>() /* force_enabled_origin_trials */, |
Domenic Denicola | 55701ee | 2021-01-14 00:18:33 | [diff] [blame] | 3719 | false /* origin_agent_cluster */, |
Daniel Vogelheim | 243df9f | 2022-02-22 10:32:03 | [diff] [blame] | 3720 | true /* origin_agent_cluster_left_as_default */, |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 3721 | std::vector< |
Shuran Huang | a055ce7 | 2020-07-23 14:13:21 | [diff] [blame] | 3722 | network::mojom::WebClientHintsType>() /* enabled_client_hints */, |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 3723 | false /* is_cross_browsing_instance */, nullptr /* old_page_info */, |
| 3724 | -1 /* http_response_code */, |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 3725 | blink::mojom::NavigationApiHistoryEntryArrays::New(), |
Matt Menke | 4e20908 | 2021-11-09 04:59:31 | [diff] [blame] | 3726 | std::vector<GURL>() /* early_hints_preloaded_resources */, |
Clark DuVall | 8ee487a2 | 2021-11-10 02:25:58 | [diff] [blame] | 3727 | absl::nullopt /* ad_auction_components */, |
| 3728 | // This timestamp will be populated when the commit IPC is sent. |
W. James MacLean | 81b8d01f | 2022-01-25 20:50:59 | [diff] [blame] | 3729 | base::TimeTicks() /* commit_sent */, false /* anonymous */, |
| 3730 | std::string() /* srcdoc_value */); |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 3731 | #if BUILDFLAG(IS_ANDROID) |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3732 | if (ValidateDataURLAsString(params.data_url_as_string)) { |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3733 | commit_params->data_url_as_string = params.data_url_as_string->data(); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3734 | } |
| 3735 | #endif |
| 3736 | |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3737 | commit_params->was_activated = params.was_activated; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3738 | |
| 3739 | // A form submission may happen here if the navigation is a renderer-initiated |
| 3740 | // form submission that took the OpenURL path. |
| 3741 | scoped_refptr<network::ResourceRequestBody> request_body = params.post_data; |
| 3742 | |
| 3743 | // extra_headers in params are \n separated; NavigationRequests want \r\n. |
| 3744 | std::string extra_headers_crlf; |
| 3745 | base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3746 | |
| 3747 | auto navigation_request = NavigationRequest::CreateBrowserInitiated( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3748 | node, std::move(common_params), std::move(commit_params), |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 3749 | !params.is_renderer_initiated, params.was_opener_suppressed, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 3750 | params.initiator_frame_token.has_value() |
| 3751 | ? &(params.initiator_frame_token.value()) |
| 3752 | : nullptr, |
| 3753 | params.initiator_process_id, extra_headers_crlf, frame_entry, entry, |
| 3754 | request_body, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 3755 | params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 3756 | params.impression, params.is_pdf); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3757 | navigation_request->set_from_download_cross_origin_redirect( |
| 3758 | params.from_download_cross_origin_redirect); |
W. James MacLean | 00568d7 | 2022-02-24 19:36:55 | [diff] [blame] | 3759 | navigation_request->set_force_new_browsing_instance( |
| 3760 | params.force_new_browsing_instance); |
Yao Xiao | dc5ed10 | 2019-06-04 19:19:09 | [diff] [blame] | 3761 | return navigation_request; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | std::unique_ptr<NavigationRequest> |
| 3765 | NavigationControllerImpl::CreateNavigationRequestFromEntry( |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3766 | FrameTreeNode* frame_tree_node, |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3767 | NavigationEntryImpl* entry, |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3768 | FrameNavigationEntry* frame_entry, |
| 3769 | ReloadType reload_type, |
| 3770 | bool is_same_document_history_load, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3771 | bool is_history_navigation_in_new_child_frame, |
| 3772 | bool is_browser_initiated) { |
Alex Moshchuk | 47d1a4bd | 2020-06-01 22:15:34 | [diff] [blame] | 3773 | DCHECK(frame_entry); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3774 | GURL dest_url = frame_entry->url(); |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 3775 | // We should never navigate to an existing initial NavigationEntry that is the |
| 3776 | // initial NavigationEntry for the initial empty document that hasn't been |
| 3777 | // overridden by the synchronous about:blank commit, to preserve previous |
| 3778 | // behavior where trying to reload when the main frame is on the initial empty |
| 3779 | // document won't result in a navigation. |
| 3780 | // See also https://crbug.com/1277414. |
| 3781 | DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank()); |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 3782 | absl::optional<url::Origin> origin_to_commit = |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3783 | frame_entry->committed_origin(); |
| 3784 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3785 | Referrer dest_referrer = frame_entry->referrer(); |
Ryan Sturm | c4da199 | 2018-07-17 16:59:01 | [diff] [blame] | 3786 | if (reload_type == ReloadType::ORIGINAL_REQUEST_URL && |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3787 | entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3788 | // We may have been redirected when navigating to the current URL. |
| 3789 | // Use the URL the user originally intended to visit as signaled by the |
| 3790 | // ReloadType, if it's valid and if a POST wasn't involved; the latter |
Ryan Sturm | c4da199 | 2018-07-17 16:59:01 | [diff] [blame] | 3791 | // case avoids issues with sending data to the wrong page. |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3792 | dest_url = entry->GetOriginalRequestURL(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3793 | dest_referrer = Referrer(); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3794 | origin_to_commit.reset(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3795 | } |
| 3796 | |
Ehsan Karamad | 44fc7211 | 2019-02-26 18:15:47 | [diff] [blame] | 3797 | if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) { |
| 3798 | // Avoid starting any new navigations since this node is now preparing for |
| 3799 | // attaching an inner delegate. |
| 3800 | return nullptr; |
Ehsan Karamad | dd9a414 | 2018-12-04 20:38:20 | [diff] [blame] | 3801 | } |
| 3802 | |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3803 | if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(), |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 3804 | entry->GetVirtualURL(), dest_url)) { |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3805 | return nullptr; |
| 3806 | } |
| 3807 | |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 3808 | if (!DoesURLMatchOriginForNavigation( |
| 3809 | dest_url, origin_to_commit, |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 3810 | frame_entry->subresource_web_bundle_navigation_info(), entry, |
| 3811 | frame_tree_node->IsMainFrame())) { |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 3812 | DCHECK(false) << " url:" << dest_url |
Wang Hui | 14cac7a | 2022-01-28 06:07:47 | [diff] [blame] | 3813 | << " base_url_for_data_url: " << entry->GetBaseURLForDataURL() |
Kunihiko Sakamoto | 346a74e | 2021-03-10 08:57:48 | [diff] [blame] | 3814 | << " origin:" << origin_to_commit.value(); |
Nasko Oskov | 0391210 | 2019-01-11 00:21:32 | [diff] [blame] | 3815 | return nullptr; |
| 3816 | } |
| 3817 | |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3818 | // This will be used to set the Navigation Timing API navigationStart |
| 3819 | // parameter for browser navigations in new tabs (intents, tabs opened through |
| 3820 | // "Open link in new tab"). If the navigation must wait on the current |
| 3821 | // RenderFrameHost to execute its BeforeUnload event, the navigation start |
| 3822 | // will be updated when the BeforeUnload ack is received. |
| 3823 | base::TimeTicks navigation_start = base::TimeTicks::Now(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3824 | |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3825 | // Look for a pending commit that is to another document in this |
| 3826 | // FrameTreeNode. If one exists, then the last committed URL will not be the |
| 3827 | // current URL by the time this navigation commits. |
| 3828 | bool has_pending_cross_document_commit = |
| 3829 | frame_tree_node->render_manager() |
| 3830 | ->HasPendingCommitForCrossDocumentNavigation(); |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3831 | bool is_currently_error_page = |
Miyoung Shin | a2dd6a4 | 2021-10-07 12:19:21 | [diff] [blame] | 3832 | frame_tree_node->current_frame_host()->IsErrorDocument(); |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3833 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3834 | blink::mojom::NavigationType navigation_type = GetNavigationType( |
danakj | d83d706d | 2020-11-25 22:11:12 | [diff] [blame] | 3835 | /*old_url=*/frame_tree_node->current_url(), |
| 3836 | /*new_url=*/dest_url, reload_type, entry, *frame_entry, |
danakj | b952ef1 | 2021-01-14 19:58:49 | [diff] [blame] | 3837 | has_pending_cross_document_commit, is_currently_error_page, |
| 3838 | is_same_document_history_load); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3839 | |
| 3840 | // A form submission may happen here if the navigation is a |
| 3841 | // back/forward/reload navigation that does a form resubmission. |
| 3842 | scoped_refptr<network::ResourceRequestBody> request_body; |
| 3843 | std::string post_content_type; |
| 3844 | if (frame_entry->method() == "POST") { |
| 3845 | request_body = frame_entry->GetPostData(&post_content_type); |
| 3846 | // Might have a LF at end. |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 3847 | post_content_type = std::string( |
| 3848 | base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)); |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | // Create the NavigationParams based on |entry| and |frame_entry|. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3852 | blink::mojom::CommonNavigationParamsPtr common_params = |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3853 | entry->ConstructCommonNavigationParams( |
| 3854 | *frame_entry, request_body, dest_url, |
| 3855 | blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy), |
Tarun Bansal | bcd62c8 | 2022-01-18 17:27:38 | [diff] [blame] | 3856 | navigation_type, navigation_start, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 3857 | base::TimeTicks() /* input_start */); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 3858 | common_params->is_history_navigation_in_new_child_frame = |
Arthur Hemery | bee4a75 | 2019-05-29 10:50:55 | [diff] [blame] | 3859 | is_history_navigation_in_new_child_frame; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3860 | |
| 3861 | // TODO(clamy): |intended_as_new_entry| below should always be false once |
| 3862 | // Reload no longer leads to this being called for a pending NavigationEntry |
| 3863 | // of index -1. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3864 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3865 | entry->ConstructCommitNavigationParams( |
| 3866 | *frame_entry, common_params->url, origin_to_commit, |
| 3867 | common_params->method, entry->GetSubframeUniqueNames(frame_tree_node), |
| 3868 | GetPendingEntryIndex() == -1 /* intended_as_new_entry */, |
Charlie Hu | 5ffc015 | 2019-12-06 15:59:53 | [diff] [blame] | 3869 | GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(), |
| 3870 | frame_tree_node->pending_frame_policy()); |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3871 | commit_params->post_content_type = post_content_type; |
Camille Lamy | 5193caa | 2018-10-12 11:59:42 | [diff] [blame] | 3872 | |
W. James MacLean | 81b8d01f | 2022-01-25 20:50:59 | [diff] [blame] | 3873 | if (common_params->url.IsAboutSrcdoc()) |
| 3874 | commit_params->srcdoc_value = frame_tree_node->srcdoc_value(); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3875 | return NavigationRequest::CreateBrowserInitiated( |
Lucas Furukawa Gadani | a9c4568 | 2019-07-31 22:05:14 | [diff] [blame] | 3876 | frame_tree_node, std::move(common_params), std::move(commit_params), |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3877 | is_browser_initiated, false /* was_opener_suppressed */, |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 3878 | nullptr /* initiator_frame_token */, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 3879 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */, |
| 3880 | entry->extra_headers(), frame_entry, entry, request_body, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 3881 | nullptr /* navigation_ui_data */, absl::nullopt /* impression */, |
| 3882 | false /* is_pdf */); |
clamy | ea99ea1 | 2018-05-28 13:54:23 | [diff] [blame] | 3883 | } |
| 3884 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3885 | void NavigationControllerImpl::NotifyNavigationEntryCommitted( |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 3886 | LoadCommittedDetails* details) { |
[email protected] | 6286a379 | 2013-10-09 04:03:27 | [diff] [blame] | 3887 | details->entry = GetLastCommittedEntry(); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 3888 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 3889 | // We need to notify the ssl_manager_ before the WebContents so the |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 3890 | // location bar will have up-to-date information about the security style |
| 3891 | // when it wants to draw. See http://crbug.com/11157 |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 3892 | ssl_manager_.DidCommitProvisionalLoad(*details); |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 3893 | |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 3894 | bool should_fire_navigation_state_changed = true; |
| 3895 | #if BUILDFLAG(IS_ANDROID) |
| 3896 | if (details && details->should_stay_as_initial_entry) { |
| 3897 | // For initial NavigationEntries, only fire NavigationStateChanged() if the |
| 3898 | // embedder wants to hear about it. |
| 3899 | should_fire_navigation_state_changed = |
| 3900 | !GetContentClient() |
| 3901 | ->browser() |
| 3902 | ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport(); |
| 3903 | } |
| 3904 | #endif |
| 3905 | if (should_fire_navigation_state_changed) |
| 3906 | delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL); |
| 3907 | |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 3908 | delegate_->NotifyNavigationEntryCommitted(*details); |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 3909 | |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 3910 | // TODO(avi): Remove. http://crbug.com/170921 |
| 3911 | NotificationDetails notification_details = |
| 3912 | Details<LoadCommittedDetails>(details); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 3913 | NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 3914 | Source<NavigationController>(this), |
| 3915 | notification_details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3916 | } |
| 3917 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3918 | // static |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3919 | size_t NavigationControllerImpl::max_entry_count() { |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 3920 | if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 3921 | return max_entry_count_for_testing_; |
Miyoung Shin | 1c565c91 | 2021-03-17 12:11:21 | [diff] [blame] | 3922 | return blink::kMaxSessionHistoryEntries; |
[email protected] | 9b51970d | 2011-12-09 23:10:23 | [diff] [blame] | 3923 | } |
| 3924 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3925 | void NavigationControllerImpl::SetActive(bool is_active) { |
[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 3926 | if (is_active && needs_reload_) |
| 3927 | LoadIfNecessary(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3928 | } |
| 3929 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 3930 | void NavigationControllerImpl::LoadIfNecessary() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3931 | if (!needs_reload_) |
| 3932 | return; |
| 3933 | |
Bo Liu | cdfa4b1 | 2018-11-06 00:21:44 | [diff] [blame] | 3934 | UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType", |
| 3935 | needs_reload_type_); |
| 3936 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3937 | // Calling Reload() results in ignoring state, and not loading. |
| 3938 | // Explicitly use NavigateToPendingEntry so that the renderer uses the |
| 3939 | // cached state. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 3940 | if (pending_entry_) { |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3941 | NavigateToExistingPendingEntry(ReloadType::NONE, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3942 | FrameTreeNode::kFrameTreeNodeInvalidId, |
| 3943 | true /* is_browser_initiated */); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 3944 | } else if (last_committed_entry_index_ != -1 && |
| 3945 | !GetLastCommittedEntry() |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 3946 | ->IsInitialEntryNotForSynchronousAboutBlank()) { |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 3947 | pending_entry_ = entries_[last_committed_entry_index_].get(); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 3948 | pending_entry_index_ = last_committed_entry_index_; |
Dave Tapuska | 8bfd84c | 2019-03-26 20:47:16 | [diff] [blame] | 3949 | NavigateToExistingPendingEntry(ReloadType::NONE, |
Nate Chapin | 45f62058 | 2021-09-30 17:45:43 | [diff] [blame] | 3950 | FrameTreeNode::kFrameTreeNodeInvalidId, |
| 3951 | true /* is_browser_initiated */); |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 3952 | } else { |
Rakina Zata Amni | f297a80 | 2022-01-18 03:53:43 | [diff] [blame] | 3953 | // We should never navigate to an existing initial NavigationEntry that is |
| 3954 | // the initial NavigationEntry for the initial empty document that hasn't |
| 3955 | // been overridden by the synchronous about:blank commit, to preserve |
| 3956 | // legacy behavior where trying to reload when the main frame is on the |
| 3957 | // initial empty document won't result in a navigation. See also |
| 3958 | // https://crbug.com/1277414. If there is something to reload, the |
| 3959 | // successful reload will clear the |needs_reload_| flag. Otherwise, just do |
| 3960 | // it here. |
avi | cc872d724 | 2015-08-19 21:26:34 | [diff] [blame] | 3961 | needs_reload_ = false; |
| 3962 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 3963 | } |
| 3964 | |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 3965 | base::WeakPtr<NavigationHandle> |
| 3966 | NavigationControllerImpl::LoadPostCommitErrorPage( |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 3967 | RenderFrameHost* render_frame_host, |
| 3968 | const GURL& url, |
| 3969 | const std::string& error_page_html, |
| 3970 | net::Error error) { |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 3971 | RenderFrameHostImpl* rfhi = |
| 3972 | static_cast<RenderFrameHostImpl*>(render_frame_host); |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3973 | |
| 3974 | // Only active documents can load post-commit error pages: |
| 3975 | // - If the document is in pending deletion, the browser already committed to |
| 3976 | // destroying this RenderFrameHost so ignore loading the error page. |
| 3977 | // - If the document is in back-forward cache, it's not allowed to navigate as |
| 3978 | // it should remain frozen. Ignore the request and evict the document from |
| 3979 | // back-forward cache. |
| 3980 | // - If the document is prerendering, it can navigate but when loading error |
| 3981 | // pages, cancel prerendering. |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3982 | if (rfhi->IsInactiveAndDisallowActivation( |
| 3983 | DisallowActivationReasonId::kLoadPostCommitErrorPage)) { |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 3984 | return nullptr; |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 3985 | } |
Sreeja Kamishetty | db8e289 | 2021-03-10 09:30:58 | [diff] [blame] | 3986 | |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 3987 | FrameTreeNode* node = rfhi->frame_tree_node(); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 3988 | |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3989 | blink::mojom::CommonNavigationParamsPtr common_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 3990 | blink::CreateCommonNavigationParams(); |
Rakina Zata Amni | d2da154 | 2020-12-23 00:52:59 | [diff] [blame] | 3991 | // |url| might be empty, such as when LoadPostCommitErrorPage happens before |
| 3992 | // the frame actually committed (e.g. iframe with "src" set to a |
| 3993 | // slow-responding URL). We should rewrite the URL to about:blank in this |
| 3994 | // case, as the renderer will only think a page is an error page if it has a |
| 3995 | // non-empty unreachable URL. |
Rakina Zata Amni | 919b792 | 2020-12-11 09:03:13 | [diff] [blame] | 3996 | common_params->url = url.is_empty() ? GURL("about:blank") : url; |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 3997 | blink::mojom::CommitNavigationParamsPtr commit_params = |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 3998 | blink::CreateCommitNavigationParams(); |
Antonio Sartori | 58591c89 | 2021-04-21 06:54:33 | [diff] [blame] | 3999 | commit_params->original_url = common_params->url; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4000 | |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 4001 | // Error pages have a fully permissive FramePolicy. |
| 4002 | // TODO(arthursonzogni): Consider providing the minimal capabilities to the |
| 4003 | // error pages. |
| 4004 | commit_params->frame_policy = blink::FramePolicy(); |
| 4005 | |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4006 | std::unique_ptr<NavigationRequest> navigation_request = |
| 4007 | NavigationRequest::CreateBrowserInitiated( |
| 4008 | node, std::move(common_params), std::move(commit_params), |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 4009 | true /* browser_initiated */, false /* was_opener_suppressed */, |
Lingqi Chi | 82efa95e | 2020-12-29 05:31:19 | [diff] [blame] | 4010 | nullptr /* initiator_frame_token */, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 4011 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */, |
John Delaney | f43556d | 2020-05-04 23:19:06 | [diff] [blame] | 4012 | "" /* extra_headers */, nullptr /* frame_entry */, |
| 4013 | nullptr /* entry */, nullptr /* post_body */, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 4014 | nullptr /* navigation_ui_data */, absl::nullopt /* impression */, |
| 4015 | false /* is_pdf */); |
Carlos IL | 42b41659 | 2019-10-07 23:10:36 | [diff] [blame] | 4016 | navigation_request->set_post_commit_error_page_html(error_page_html); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4017 | navigation_request->set_net_error(error); |
| 4018 | node->CreatedNavigationRequest(std::move(navigation_request)); |
| 4019 | DCHECK(node->navigation_request()); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4020 | |
| 4021 | // Calling BeginNavigation may destroy the NavigationRequest. |
| 4022 | base::WeakPtr<NavigationRequest> created_navigation_request( |
| 4023 | node->navigation_request()->GetWeakPtr()); |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4024 | node->navigation_request()->BeginNavigation(); |
Kevin McNee | ccca617 | 2021-10-19 17:11:14 | [diff] [blame] | 4025 | return created_navigation_request; |
John Delaney | 131ad36 | 2019-08-08 21:57:41 | [diff] [blame] | 4026 | } |
| 4027 | |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4028 | void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) { |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4029 | EntryChangedDetails det; |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 4030 | det.changed_entry = entry; |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 4031 | det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry)); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 4032 | delegate_->NotifyNavigationEntryChanged(det); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4033 | } |
| 4034 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4035 | void NavigationControllerImpl::FinishRestore(int selected_index, |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4036 | RestoreType type) { |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4037 | // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would |
| 4038 | // represent a no-committed-entry state. |
| 4039 | DCHECK(selected_index >= -1 && selected_index < GetEntryCount()); |
[email protected] | 2ca1ea66 | 2012-10-04 02:26:36 | [diff] [blame] | 4040 | ConfigureEntriesForRestore(&entries_, type); |
Charlie Reis | 23c26da | 2022-01-29 00:57:47 | [diff] [blame] | 4041 | // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry |
| 4042 | // if `entries_` contains pre-existing entries from the NavigationController |
| 4043 | // before restore, which would not be removed and will be at the front of the |
| 4044 | // entries list, causing the index to be off by the amount of pre-existing |
| 4045 | // entries in the list. Fix this to point to the correct entry. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4046 | last_committed_entry_index_ = selected_index; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4047 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 4048 | |
arthursonzogni | 69a6a1b | 2019-09-17 09:23:00 | [diff] [blame] | 4049 | void NavigationControllerImpl::DiscardNonCommittedEntries() { |
Rakina Zata Amni | a4e2722 | 2021-12-22 01:05:00 | [diff] [blame] | 4050 | DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */); |
| 4051 | } |
| 4052 | |
| 4053 | void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails( |
| 4054 | LoadCommittedDetails* commit_details) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4055 | // Avoid sending a notification if there is nothing to discard. |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4056 | // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help |
| 4057 | // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570. |
Carlos IL | 4dea890 | 2020-05-26 15:14:29 | [diff] [blame] | 4058 | if (!pending_entry_ && failed_pending_entry_id_ == 0) { |
Michael Thiessen | 9b14d51 | 2019-09-23 21:19:47 | [diff] [blame] | 4059 | return; |
Michael Thiessen | c5676d2 | 2019-09-25 22:32:10 | [diff] [blame] | 4060 | } |
avi | 45a7253 | 2015-04-07 21:01:45 | [diff] [blame] | 4061 | DiscardPendingEntry(false); |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 4062 | |
| 4063 | if (!delegate_) |
| 4064 | return; |
| 4065 | |
| 4066 | bool should_fire_navigation_state_changed = true; |
| 4067 | #if BUILDFLAG(IS_ANDROID) |
| 4068 | if (commit_details && commit_details->should_stay_as_initial_entry) { |
| 4069 | // For initial NavigationEntries, only fire NavigationStateChanged() if the |
| 4070 | // embedder wants to hear about it. |
| 4071 | should_fire_navigation_state_changed = |
| 4072 | !GetContentClient() |
| 4073 | ->browser() |
| 4074 | ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport(); |
Rakina Zata Amni | ddf1050 | 2022-01-15 02:56:55 | [diff] [blame] | 4075 | } |
Rakina Zata Amni | daa84f6 | 2022-02-17 00:55:31 | [diff] [blame] | 4076 | #endif |
| 4077 | if (should_fire_navigation_state_changed) |
| 4078 | delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL); |
[email protected] | b12eb22 | 2013-09-10 00:11:48 | [diff] [blame] | 4079 | } |
| 4080 | |
avi | 7c6f35e | 2015-05-08 17:52:38 | [diff] [blame] | 4081 | int NavigationControllerImpl::GetEntryIndexWithUniqueID( |
| 4082 | int nav_entry_id) const { |
| 4083 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
| 4084 | if (entries_[i]->GetUniqueID() == nav_entry_id) |
| 4085 | return i; |
| 4086 | } |
| 4087 | return -1; |
| 4088 | } |
| 4089 | |
[email protected] | d202a7c | 2012-01-04 07:53:47 | [diff] [blame] | 4090 | void NavigationControllerImpl::InsertEntriesFrom( |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4091 | NavigationControllerImpl* source, |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4092 | int max_index) { |
Lucas Furukawa Gadani | 5553a158 | 2019-01-08 18:55:57 | [diff] [blame] | 4093 | DCHECK_LE(max_index, source->GetEntryCount()); |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 4094 | std::unique_ptr<NavigationEntryRestoreContextImpl> context = |
| 4095 | std::make_unique<NavigationEntryRestoreContextImpl>(); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4096 | for (int i = 0; i < max_index; i++) { |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4097 | // Normally, cloning a NavigationEntryImpl results in sharing |
| 4098 | // FrameNavigationEntries between the original and the clone. However, when |
| 4099 | // cloning from a different NavigationControllerImpl, we want to fork the |
| 4100 | // FrameNavigationEntries. |
Nate Chapin | 9f16907 | 2021-06-09 19:32:37 | [diff] [blame] | 4101 | entries_.insert(entries_.begin() + i, |
Nate Chapin | 214a86a | 2021-06-21 20:35:57 | [diff] [blame] | 4102 | source->entries_[i]->CloneWithoutSharing(context.get())); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4103 | } |
arthursonzogni | 5c4c202d | 2017-04-25 23:41:27 | [diff] [blame] | 4104 | DCHECK(pending_entry_index_ == -1 || |
| 4105 | pending_entry_ == GetEntryAtIndex(pending_entry_index_)); |
[email protected] | e1cd545 | 2010-08-26 18:03:25 | [diff] [blame] | 4106 | } |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4107 | |
| 4108 | void NavigationControllerImpl::SetGetTimestampCallbackForTest( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 4109 | const base::RepeatingCallback<base::Time()>& get_timestamp_callback) { |
[email protected] | c5b88d8 | 2012-10-06 17:03:33 | [diff] [blame] | 4110 | get_timestamp_callback_ = get_timestamp_callback; |
| 4111 | } |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4112 | |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4113 | // History manipulation intervention: |
| 4114 | void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded( |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4115 | bool replace_entry, |
| 4116 | bool previous_document_was_activated, |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4117 | bool is_renderer_initiated, |
| 4118 | ukm::SourceId previous_page_load_ukm_source_id) { |
Shivani Sharma | 712d5d7 | 2019-04-16 21:56:45 | [diff] [blame] | 4119 | // Note that for a subframe, previous_document_was_activated is true if the |
| 4120 | // gesture happened in any subframe (propagated to main frame) or in the main |
| 4121 | // frame itself. |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4122 | if (replace_entry || previous_document_was_activated || |
shivanigithub | cceeacf | 2020-03-06 20:00:27 | [diff] [blame] | 4123 | !is_renderer_initiated) { |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4124 | return; |
| 4125 | } |
| 4126 | if (last_committed_entry_index_ == -1) |
| 4127 | return; |
| 4128 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4129 | SetSkippableForSameDocumentEntries(last_committed_entry_index_, true); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4130 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 4131 | // Log UKM with the URL we are navigating away from. |
| 4132 | ukm::builders::HistoryManipulationIntervention( |
| 4133 | previous_page_load_ukm_source_id) |
| 4134 | .Record(ukm::UkmRecorder::Get()); |
Shivani Sharma | ffb32b8 | 2019-04-09 16:58:47 | [diff] [blame] | 4135 | } |
| 4136 | |
Shivani Sharma | c4cc892 | 2019-04-18 03:11:17 | [diff] [blame] | 4137 | void NavigationControllerImpl::SetSkippableForSameDocumentEntries( |
| 4138 | int reference_index, |
| 4139 | bool skippable) { |
| 4140 | auto* reference_entry = GetEntryAtIndex(reference_index); |
| 4141 | reference_entry->set_should_skip_on_back_forward_ui(skippable); |
| 4142 | |
| 4143 | int64_t document_sequence_number = |
| 4144 | reference_entry->root_node()->frame_entry->document_sequence_number(); |
| 4145 | for (int index = 0; index < GetEntryCount(); index++) { |
| 4146 | auto* entry = GetEntryAtIndex(index); |
| 4147 | if (entry->root_node()->frame_entry->document_sequence_number() == |
| 4148 | document_sequence_number) { |
| 4149 | entry->set_should_skip_on_back_forward_ui(skippable); |
| 4150 | } |
| 4151 | } |
| 4152 | } |
| 4153 | |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4154 | std::unique_ptr<NavigationControllerImpl::PendingEntryRef> |
| 4155 | NavigationControllerImpl::ReferencePendingEntry() { |
| 4156 | DCHECK(pending_entry_); |
| 4157 | auto pending_entry_ref = |
| 4158 | std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr()); |
| 4159 | pending_entry_refs_.insert(pending_entry_ref.get()); |
| 4160 | return pending_entry_ref; |
| 4161 | } |
| 4162 | |
| 4163 | void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) { |
| 4164 | // Ignore refs that don't correspond to the current pending entry. |
| 4165 | auto it = pending_entry_refs_.find(ref); |
| 4166 | if (it == pending_entry_refs_.end()) |
| 4167 | return; |
| 4168 | pending_entry_refs_.erase(it); |
| 4169 | |
| 4170 | if (!pending_entry_refs_.empty()) |
| 4171 | return; |
| 4172 | |
| 4173 | // The pending entry may be deleted before the last PendingEntryRef. |
| 4174 | if (!pending_entry_) |
| 4175 | return; |
| 4176 | |
| 4177 | // We usually clear the pending entry when the matching NavigationRequest |
| 4178 | // fails, so that an arbitrary URL isn't left visible above a committed page. |
| 4179 | // |
| 4180 | // However, we do preserve the pending entry in some cases, such as on the |
| 4181 | // initial navigation of an unmodified blank tab. We also allow the delegate |
| 4182 | // to say when it's safe to leave aborted URLs in the omnibox, to let the |
| 4183 | // user edit the URL and try again. This may be useful in cases that the |
| 4184 | // committed page cannot be attacker-controlled. In these cases, we still |
| 4185 | // allow the view to clear the pending entry and typed URL if the user |
| 4186 | // requests (e.g., hitting Escape with focus in the address bar). |
| 4187 | // |
| 4188 | // Do not leave the pending entry visible if it has an invalid URL, since this |
| 4189 | // might be formatted in an unexpected or unsafe way. |
| 4190 | // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824. |
arthursonzogni | 66f711c | 2019-10-08 14:40:36 | [diff] [blame] | 4191 | bool should_preserve_entry = |
| 4192 | (pending_entry_ == GetVisibleEntry()) && |
| 4193 | pending_entry_->GetURL().is_valid() && |
| 4194 | (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs()); |
| 4195 | if (should_preserve_entry) |
| 4196 | return; |
| 4197 | |
| 4198 | DiscardPendingEntry(true); |
| 4199 | delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL); |
| 4200 | } |
| 4201 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4202 | std::unique_ptr<PolicyContainerPolicies> |
| 4203 | NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry( |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4204 | RenderFrameHostImpl* rfh, |
| 4205 | bool is_same_document, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4206 | const GURL& url) { |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4207 | if (is_same_document) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 4208 | // TODO(https://crbug.com/524208): Remove this nullptr check when we can |
| 4209 | // ensure we always have a FrameNavigationEntry here. |
| 4210 | if (!GetLastCommittedEntry()) |
| 4211 | return nullptr; |
Charlie Reis | 73e35624 | 2021-04-02 17:10:31 | [diff] [blame] | 4212 | |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4213 | FrameNavigationEntry* previous_frame_entry = |
| 4214 | GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node()); |
| 4215 | |
| 4216 | // TODO(https://crbug.com/608402): Remove this nullptr check when we can |
| 4217 | // ensure we always have a FrameNavigationEntry here. |
| 4218 | if (!previous_frame_entry) |
| 4219 | return nullptr; |
| 4220 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4221 | const PolicyContainerPolicies* previous_policies = |
| 4222 | previous_frame_entry->policy_container_policies(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4223 | |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 4224 | if (!previous_policies) |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4225 | return nullptr; |
| 4226 | |
| 4227 | // Make a copy of the policy container for the new FrameNavigationEntry. |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 4228 | return previous_policies->Clone(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4229 | } |
| 4230 | |
Antonio Sartori | 4f537379 | 2021-05-31 10:56:47 | [diff] [blame] | 4231 | return rfh->policy_container_host()->policies().Clone(); |
Antonio Sartori | 78a749f | 2020-11-30 12:03:39 | [diff] [blame] | 4232 | } |
| 4233 | |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4234 | void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() { |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4235 | OPTIONAL_TRACE_EVENT2( |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4236 | "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength", |
| 4237 | "history_offset", GetLastCommittedEntryIndex(), "history_length", |
| 4238 | GetEntryCount()); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4239 | |
| 4240 | auto callback = base::BindRepeating( |
| 4241 | [](int history_offset, int history_length, RenderViewHostImpl* rvh) { |
| 4242 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) { |
| 4243 | broadcast->SetHistoryOffsetAndLength(history_offset, history_length); |
| 4244 | } |
| 4245 | }, |
Hayato Ito | 2c8c08d0 | 2021-06-23 03:38:43 | [diff] [blame] | 4246 | GetLastCommittedEntryIndex(), GetEntryCount()); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4247 | frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback); |
| 4248 | } |
| 4249 | |
| 4250 | void NavigationControllerImpl::DidAccessInitialMainDocument() { |
| 4251 | // We may have left a failed browser-initiated navigation in the address bar |
| 4252 | // to let the user edit it and try again. Clear it now that content might |
| 4253 | // show up underneath it. |
| 4254 | if (!frame_tree_.IsLoading() && GetPendingEntry()) |
| 4255 | DiscardPendingEntry(false); |
| 4256 | |
| 4257 | // Update the URL display. |
| 4258 | delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL); |
| 4259 | } |
| 4260 | |
| 4261 | void NavigationControllerImpl::UpdateStateForFrame( |
| 4262 | RenderFrameHostImpl* rfhi, |
| 4263 | const blink::PageState& page_state) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4264 | OPTIONAL_TRACE_EVENT1("content", |
| 4265 | "NavigationControllerImpl::UpdateStateForFrame", |
| 4266 | "render_frame_host", rfhi); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 4267 | // The state update affects the last NavigationEntry associated with the given |
| 4268 | // |render_frame_host|. This may not be the last committed NavigationEntry (as |
| 4269 | // in the case of an UpdateState from a frame being swapped out). We track |
| 4270 | // which entry this is in the RenderFrameHost's nav_entry_id. |
| 4271 | NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id()); |
| 4272 | if (!entry) |
| 4273 | return; |
| 4274 | |
| 4275 | FrameNavigationEntry* frame_entry = |
| 4276 | entry->GetFrameEntry(rfhi->frame_tree_node()); |
| 4277 | if (!frame_entry) |
| 4278 | return; |
| 4279 | |
| 4280 | // The SiteInstance might not match if we do a cross-process navigation with |
| 4281 | // replacement (e.g., auto-subframe), in which case the swap out of the old |
| 4282 | // RenderFrameHost runs in the background after the old FrameNavigationEntry |
| 4283 | // has already been replaced and destroyed. |
| 4284 | if (frame_entry->site_instance() != rfhi->GetSiteInstance()) |
| 4285 | return; |
| 4286 | |
| 4287 | if (page_state == frame_entry->page_state()) |
| 4288 | return; // Nothing to update. |
| 4289 | |
| 4290 | DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState."; |
| 4291 | |
| 4292 | // The document_sequence_number and item_sequence_number recorded in the |
| 4293 | // FrameNavigationEntry should not differ from the one coming with the update, |
| 4294 | // since it must come from the same document. Do not update it if a difference |
| 4295 | // is detected, as this indicates that |frame_entry| is not the correct one. |
| 4296 | blink::ExplodedPageState exploded_state; |
| 4297 | if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state)) |
| 4298 | return; |
| 4299 | |
| 4300 | if (exploded_state.top.document_sequence_number != |
| 4301 | frame_entry->document_sequence_number() || |
| 4302 | exploded_state.top.item_sequence_number != |
| 4303 | frame_entry->item_sequence_number()) { |
| 4304 | return; |
| 4305 | } |
| 4306 | |
| 4307 | frame_entry->SetPageState(page_state); |
| 4308 | NotifyEntryChanged(entry); |
| 4309 | } |
| 4310 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4311 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> |
| 4312 | NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4313 | Direction direction, |
| 4314 | int entry_index, |
| 4315 | const url::Origin& pending_origin, |
| 4316 | FrameTreeNode* node, |
| 4317 | SiteInstance* site_instance, |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4318 | int64_t pending_item_sequence_number, |
| 4319 | int64_t pending_document_sequence_number) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4320 | std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries; |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 4321 | if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) { |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4322 | // Don't process the initial entry. |
| 4323 | DCHECK_EQ(GetEntryCount(), 1); |
| 4324 | return entries; |
| 4325 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4326 | int offset = direction == Direction::kForward ? 1 : -1; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4327 | int64_t previous_item_sequence_number = pending_item_sequence_number; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4328 | for (int i = entry_index + offset; i >= 0 && i < GetEntryCount(); |
| 4329 | i += offset) { |
| 4330 | FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node); |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4331 | if (!frame_entry) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4332 | break; |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4333 | // An entry should only appear in the navigation API entries if it is for |
| 4334 | // the same origin as the document being committed. Check the committed |
| 4335 | // origin, or if that is not available (during restore), check against the |
| 4336 | // FNE's url. |
Nate Chapin | dedfa64 | 2022-01-28 23:59:41 | [diff] [blame] | 4337 | url::Origin frame_entry_origin = |
| 4338 | frame_entry->committed_origin().value_or(url::Origin::Resolve( |
| 4339 | frame_entry->url(), |
| 4340 | frame_entry->initiator_origin().value_or(url::Origin()))); |
| 4341 | if (!pending_origin.IsSameOriginWith(frame_entry_origin)) |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4342 | break; |
| 4343 | if (previous_item_sequence_number == frame_entry->item_sequence_number()) |
| 4344 | continue; |
| 4345 | blink::ExplodedPageState exploded_page_state; |
| 4346 | if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(), |
| 4347 | &exploded_page_state)) { |
| 4348 | blink::ExplodedFrameState frame_state = exploded_page_state.top; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4349 | |
| 4350 | // If the document represented by this FNE hid its full url from appearing |
| 4351 | // in a referrer via a "no-referrer" or "origin" referrer policy, censor |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4352 | // the url in the navigation API as well (unless we're navigating to that |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4353 | // document). |
| 4354 | std::u16string url; |
| 4355 | if (pending_document_sequence_number == |
| 4356 | frame_entry->document_sequence_number() || |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4357 | !frame_entry->protect_url_in_navigation_api()) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4358 | url = frame_state.url_string.value_or(std::u16string()); |
| 4359 | } |
| 4360 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4361 | blink::mojom::NavigationApiHistoryEntryPtr entry = |
| 4362 | blink::mojom::NavigationApiHistoryEntry::New( |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4363 | frame_state.navigation_api_key.value_or(std::u16string()), |
| 4364 | frame_state.navigation_api_id.value_or(std::u16string()), url, |
Nate Chapin | ab5c3a71 | 2021-11-18 22:17:09 | [diff] [blame] | 4365 | frame_state.item_sequence_number, |
| 4366 | frame_state.document_sequence_number, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4367 | frame_state.navigation_api_state.value_or(std::u16string())); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 4368 | |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4369 | DCHECK(entry->url.empty() || |
| 4370 | pending_origin.CanBeDerivedFrom(GURL(entry->url))); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4371 | entries.push_back(std::move(entry)); |
| 4372 | previous_item_sequence_number = frame_entry->item_sequence_number(); |
| 4373 | } |
| 4374 | } |
| 4375 | // If |entries| was constructed by iterating backwards from |
| 4376 | // |entry_index|, it's latest-at-the-front, but the renderer will want it |
| 4377 | // earliest-at-the-front. Reverse it. |
| 4378 | if (direction == Direction::kBack) |
| 4379 | std::reverse(entries.begin(), entries.end()); |
| 4380 | return entries; |
| 4381 | } |
| 4382 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4383 | blink::mojom::NavigationApiHistoryEntryArraysPtr |
| 4384 | NavigationControllerImpl::GetNavigationApiHistoryEntryVectors( |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4385 | FrameTreeNode* node, |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4386 | NavigationRequest* request) { |
| 4387 | url::Origin pending_origin = |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4388 | request ? request->commit_params().origin_to_commit |
| 4389 | ? *request->commit_params().origin_to_commit |
| 4390 | : url::Origin::Create(request->common_params().url) |
| 4391 | : url::Origin::Create(node->current_url()); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4392 | |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4393 | scoped_refptr<SiteInstance> site_instance = |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4394 | node->current_frame_host()->GetSiteInstance(); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4395 | |
| 4396 | // NOTE: |entry_index| is an estimate of the index where this entry will |
| 4397 | // commit, but it may be wrong in corner cases (e.g., if we are at the max |
| 4398 | // entry limit, the earliest entry will be dropped). This is ok because this |
| 4399 | // algorithm only uses |entry_index| to walk the entry list as it stands right |
| 4400 | // now, and it isn't saved for anything post-commit. |
| 4401 | int entry_index = GetPendingEntryIndex(); |
| 4402 | bool will_create_new_entry = false; |
Nate Chapin | 97d2f54 | 2022-02-18 01:34:55 | [diff] [blame] | 4403 | if (!request || |
| 4404 | NavigationTypeUtils::IsReload(request->common_params().navigation_type) || |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4405 | request->common_params().should_replace_current_entry) { |
| 4406 | entry_index = GetLastCommittedEntryIndex(); |
| 4407 | } else if (entry_index == -1) { |
| 4408 | will_create_new_entry = true; |
| 4409 | entry_index = GetLastCommittedEntryIndex() + 1; |
| 4410 | } |
| 4411 | |
| 4412 | int64_t pending_item_sequence_number = 0; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4413 | int64_t pending_document_sequence_number = 0; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4414 | if (auto* pending_entry = GetPendingEntry()) { |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4415 | if (auto* frame_entry = pending_entry->GetFrameEntry(node)) { |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4416 | pending_item_sequence_number = frame_entry->item_sequence_number(); |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4417 | pending_document_sequence_number = |
| 4418 | frame_entry->document_sequence_number(); |
| 4419 | } |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4420 | } |
| 4421 | |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4422 | auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New(); |
| 4423 | entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector( |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4424 | Direction::kBack, entry_index, pending_origin, node, site_instance.get(), |
Nate Chapin | 4e657a47 | 2022-02-06 03:38:37 | [diff] [blame] | 4425 | pending_item_sequence_number, pending_document_sequence_number); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4426 | |
| 4427 | // Don't populate forward entries if they will be truncated by a new entry. |
| 4428 | if (!will_create_new_entry) { |
Domenic Denicola | cd30f5f8 | 2022-03-16 21:48:01 | [diff] [blame] | 4429 | entry_arrays->forward_entries = |
| 4430 | PopulateSingleNavigationApiHistoryEntryVector( |
| 4431 | Direction::kForward, entry_index, pending_origin, node, |
| 4432 | site_instance.get(), pending_item_sequence_number, |
| 4433 | pending_document_sequence_number); |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4434 | } |
Nate Chapin | 4e657a47 | 2022-02-06 03:38:37 | [diff] [blame] | 4435 | return entry_arrays; |
Nate Chapin | d1fe361 | 2021-04-16 20:45:57 | [diff] [blame] | 4436 | } |
| 4437 | |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4438 | NavigationControllerImpl::HistoryNavigationAction |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4439 | NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4440 | FrameNavigationEntry* current_entry, |
| 4441 | FrameNavigationEntry* target_entry, |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4442 | const std::string& navigation_api_key) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4443 | if (!target_entry || !target_entry->committed_origin()) |
| 4444 | return HistoryNavigationAction::kStopLooking; |
| 4445 | if (current_entry->site_instance() != target_entry->site_instance()) |
| 4446 | return HistoryNavigationAction::kStopLooking; |
| 4447 | if (!current_entry->committed_origin()->IsSameOriginWith( |
| 4448 | *target_entry->committed_origin())) { |
| 4449 | return HistoryNavigationAction::kStopLooking; |
| 4450 | } |
| 4451 | |
| 4452 | // NOTE: We don't actually care between kSameDocument and |
| 4453 | // kDifferentDocument, so always use kDifferentDocument by convention. |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4454 | if (target_entry->navigation_api_key() == navigation_api_key) |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4455 | return HistoryNavigationAction::kDifferentDocument; |
| 4456 | return HistoryNavigationAction::kKeepLooking; |
| 4457 | } |
| 4458 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4459 | void NavigationControllerImpl::NavigateToNavigationApiKey( |
| 4460 | FrameTreeNode* node, |
| 4461 | const std::string& key) { |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4462 | FrameNavigationEntry* current_entry = |
| 4463 | GetLastCommittedEntry()->GetFrameEntry(node); |
| 4464 | if (!current_entry) |
| 4465 | return; |
| 4466 | |
| 4467 | // We want to find the nearest matching entry that is contiguously |
| 4468 | // same-instance and same-origin. Check back first, then forward. |
| 4469 | // TODO(japhet): Link spec here once it exists. |
| 4470 | for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4471 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4472 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4473 | if (result == HistoryNavigationAction::kStopLooking) |
| 4474 | break; |
| 4475 | if (result != HistoryNavigationAction::kKeepLooking) { |
Nate Chapin | 2892b37c | 2021-10-08 17:33:11 | [diff] [blame] | 4476 | GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId, |
| 4477 | false /* is_browser_initiated*/); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4478 | return; |
| 4479 | } |
| 4480 | } |
| 4481 | for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) { |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4482 | auto result = ShouldNavigateToEntryForNavigationApiKey( |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4483 | current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key); |
| 4484 | if (result == HistoryNavigationAction::kStopLooking) |
| 4485 | break; |
| 4486 | if (result != HistoryNavigationAction::kKeepLooking) { |
Nate Chapin | 2892b37c | 2021-10-08 17:33:11 | [diff] [blame] | 4487 | GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId, |
| 4488 | false /* is_browser_initiated*/); |
Nate Chapin | fbfe5af | 2021-06-10 17:22:08 | [diff] [blame] | 4489 | return; |
| 4490 | } |
| 4491 | } |
| 4492 | } |
| 4493 | |
Domenic Denicola | cc094fb | 2022-03-16 23:40:57 | [diff] [blame^] | 4494 | bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 4495 | network::mojom::ReferrerPolicy referrer_policy) { |
| 4496 | return referrer_policy == network::mojom::ReferrerPolicy::kNever || |
| 4497 | referrer_policy == network::mojom::ReferrerPolicy::kOrigin; |
| 4498 | } |
| 4499 | |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4500 | bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory( |
| 4501 | const FrameTreeNode* frame_tree_node) const { |
| 4502 | // TODO(https://crbug.com/1197384): We may have to add portals in addition to |
| 4503 | // prerender and fenced frames. This should be kept in sync with |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4504 | // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory. |
shivanigithub | f405bf0d | 2021-11-05 17:58:33 | [diff] [blame] | 4505 | return frame_tree_.is_prerendering() || |
| 4506 | frame_tree_node->IsInFencedFrameTree(); |
Hayato Ito | 7a80db4 | 2021-07-05 06:18:54 | [diff] [blame] | 4507 | } |
| 4508 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 4509 | } // namespace content |