blob: 6b80d081c1853fba4991622e445f97744baa6c27 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]9b159a52013-10-03 17:24:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakjc492bf82020-09-09 20:02:445#include "content/browser/renderer_host/frame_tree.h"
[email protected]9b159a52013-10-03 17:24:556
avib7348942015-12-25 20:57:107#include <stddef.h>
8
Peter Kasting1557e5f2025-01-28 01:14:089#include <algorithm>
[email protected]9b159a52013-10-03 17:24:5510#include <queue>
Lukasz Anforowicz7bfb2e92017-11-22 17:19:4511#include <set>
dcheng29f5a6c2015-08-31 21:43:2712#include <utility>
[email protected]9b159a52013-10-03 17:24:5513
Lei Zhangde197672021-04-29 08:11:2414#include "base/containers/contains.h"
Lei Zhang302bc3fa2025-06-04 20:10:3415#include "base/debug/crash_logging.h"
Rakina Zata Amni80700402021-09-20 17:18:0316#include "base/debug/dump_without_crashing.h"
Avi Drissmanadac21992023-01-11 23:46:3917#include "base/functional/bind.h"
18#include "base/functional/callback.h"
[email protected]20edca72014-08-14 10:27:5319#include "base/lazy_instance.h"
dcheng9bfa5162016-04-09 01:00:5720#include "base/memory/ptr_util.h"
Ali Hijazie63cbaf62023-12-20 19:29:3521#include "base/memory/raw_ptr.h"
Peilin Wang427aa6a2023-03-22 17:58:3322#include "base/memory/safe_ref.h"
Thomas Lukaszewicz0ef0f45f2025-02-18 17:59:4223#include "base/metrics/histogram_functions.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5024#include "base/trace_event/optional_trace_event.h"
Rakina Zata Amni4b1968d2021-09-09 03:29:4725#include "base/trace_event/typed_macros.h"
Peter Kasting3b811ffd2025-01-29 22:20:1626#include "base/types/cxx23_from_range.h"
Pavel Feldman25234722017-10-11 02:49:0627#include "base/unguessable_token.h"
Peilin Wang427aa6a2023-03-22 17:58:3328#include "content/browser/renderer_host/batched_proxy_ipc_sender.h"
danakjc492bf82020-09-09 20:02:4429#include "content/browser/renderer_host/navigation_controller_impl.h"
30#include "content/browser/renderer_host/navigation_entry_impl.h"
31#include "content/browser/renderer_host/navigation_request.h"
32#include "content/browser/renderer_host/navigator.h"
33#include "content/browser/renderer_host/navigator_delegate.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1534#include "content/browser/renderer_host/page_impl.h"
Kevin McNee5f594382021-05-06 23:18:2335#include "content/browser/renderer_host/render_frame_host_delegate.h"
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/render_frame_host_factory.h"
37#include "content/browser/renderer_host/render_frame_host_impl.h"
38#include "content/browser/renderer_host/render_frame_proxy_host.h"
Carlos Caballero101ac26b2021-03-24 11:54:0539#include "content/browser/renderer_host/render_view_host_delegate.h"
[email protected]94d0cc12013-12-18 00:07:4140#include "content/browser/renderer_host/render_view_host_factory.h"
41#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yang7ce309e2023-01-19 21:39:5742#include "content/common/content_navigation_policy.h"
japhet98e9bd82016-06-28 23:48:4543#include "content/common/content_switches_internal.h"
Peilin Wang427aa6a2023-03-22 17:58:3344#include "content/common/features.h"
Sreeja Kamishetty46f762c2021-02-05 07:52:4645#include "third_party/blink/public/common/features.h"
Kevin McNee43fe8292021-10-04 22:59:4146#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Blink Reformata30d4232018-04-07 15:31:0647#include "third_party/blink/public/common/frame/frame_policy.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1548#include "third_party/blink/public/common/loader/loader_constants.h"
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:5449#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
[email protected]9b159a52013-10-03 17:24:5550
51namespace content {
52
53namespace {
[email protected]20edca72014-08-14 10:27:5354
Rakina Zata Amni4b1968d2021-09-09 03:29:4755using perfetto::protos::pbzero::ChromeTrackEvent;
56
Sharon Yangefe52632022-03-08 23:06:0657// Helper function to collect SiteInstanceGroups involved in rendering a single
58// FrameTree (which is a subset of SiteInstanceGroups in main frame's
59// proxy_hosts_ because of openers).
60std::set<SiteInstanceGroup*> CollectSiteInstanceGroups(FrameTree* tree) {
61 std::set<SiteInstanceGroup*> groups;
Adithya Srinivasan0e9808342022-12-13 18:25:0762 for (FrameTreeNode* node : tree->Nodes())
Sharon Yangefe52632022-03-08 23:06:0663 groups.insert(node->current_frame_host()->GetSiteInstance()->group());
64 return groups;
alexmos3fcd0ca2015-10-23 18:18:3365}
66
Kevin McNee5f594382021-05-06 23:18:2367// If |node| is the placeholder FrameTreeNode for an embedded frame tree,
68// returns the inner tree's main frame's FrameTreeNode. Otherwise, returns null.
69FrameTreeNode* GetInnerTreeMainFrameNode(FrameTreeNode* node) {
Dominic Farolino377edb302021-07-29 05:57:1870 FrameTreeNode* inner_main_frame_tree_node = FrameTreeNode::GloballyFindByID(
71 node->current_frame_host()->inner_tree_main_frame_tree_node_id());
Kevin McNee5f594382021-05-06 23:18:2372
Kevin McNeed459ad42022-09-12 19:24:5973 if (inner_main_frame_tree_node) {
Arthur Sonzognif6785ec2022-12-05 10:11:5074 DCHECK_NE(&node->frame_tree(), &inner_main_frame_tree_node->frame_tree());
Kevin McNee5f594382021-05-06 23:18:2375 }
76
Kevin McNeed459ad42022-09-12 19:24:5977 return inner_main_frame_tree_node;
Kevin McNee5f594382021-05-06 23:18:2378}
79
[email protected]9b159a52013-10-03 17:24:5580} // namespace
81
vmpstr33895d992016-02-24 20:55:2182FrameTree::NodeIterator::NodeIterator(const NodeIterator& other) = default;
83
Fergal Daly55b6d722020-09-11 07:56:3384FrameTree::NodeIterator::~NodeIterator() = default;
dcheng57e39e22016-01-21 00:25:3885
86FrameTree::NodeIterator& FrameTree::NodeIterator::operator++() {
Alex Moshchuk27caae82017-09-11 23:11:1887 if (current_node_ != root_of_subtree_to_skip_) {
Jayson Adams4db0bfe22021-07-15 19:24:0788 // Reserve enough space in the queue to accommodate the nodes we're
89 // going to add, to avoid repeated resize calls.
90 queue_.reserve(queue_.size() + current_node_->child_count());
91
Alex Moshchuk27caae82017-09-11 23:11:1892 for (size_t i = 0; i < current_node_->child_count(); ++i) {
93 FrameTreeNode* child = current_node_->child_at(i);
Kevin McNee5f594382021-05-06 23:18:2394 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:4895 if (should_descend_into_inner_trees_ && inner_tree_main_ftn) {
96 if (include_delegate_nodes_for_inner_frame_trees_)
97 queue_.push_back(child);
98 queue_.push_back(inner_tree_main_ftn);
99 } else {
100 queue_.push_back(child);
101 }
Kevin McNee5f594382021-05-06 23:18:23102 }
103
104 if (should_descend_into_inner_trees_) {
Jayson Adams4db0bfe22021-07-15 19:24:07105 auto unattached_nodes =
106 current_node_->current_frame_host()
107 ->delegate()
108 ->GetUnattachedOwnedNodes(current_node_->current_frame_host());
109
110 // Reserve enough space in the queue to accommodate the nodes we're
111 // going to add.
112 queue_.reserve(queue_.size() + unattached_nodes.size());
113
114 for (auto* unattached_node : unattached_nodes) {
115 queue_.push_back(unattached_node);
Kevin McNee5f594382021-05-06 23:18:23116 }
Alex Moshchuk27caae82017-09-11 23:11:18117 }
dcheng57e39e22016-01-21 00:25:38118 }
119
Kevin McNee5f594382021-05-06 23:18:23120 AdvanceNode();
121 return *this;
122}
dcheng57e39e22016-01-21 00:25:38123
Kevin McNee5f594382021-05-06 23:18:23124FrameTree::NodeIterator& FrameTree::NodeIterator::AdvanceSkippingChildren() {
125 AdvanceNode();
dcheng57e39e22016-01-21 00:25:38126 return *this;
127}
128
129bool FrameTree::NodeIterator::operator==(const NodeIterator& rhs) const {
130 return current_node_ == rhs.current_node_;
131}
132
Kevin McNee5f594382021-05-06 23:18:23133void FrameTree::NodeIterator::AdvanceNode() {
134 if (!queue_.empty()) {
135 current_node_ = queue_.front();
Jayson Adams4db0bfe22021-07-15 19:24:07136 queue_.pop_front();
Kevin McNee5f594382021-05-06 23:18:23137 } else {
138 current_node_ = nullptr;
139 }
140}
141
142FrameTree::NodeIterator::NodeIterator(
Ali Hijazie63cbaf62023-12-20 19:29:35143 const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
144 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:23145 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48146 bool should_descend_into_inner_trees,
147 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23148 : current_node_(nullptr),
149 root_of_subtree_to_skip_(root_of_subtree_to_skip),
150 should_descend_into_inner_trees_(should_descend_into_inner_trees),
Dave Tapuskadda303d2022-10-04 16:56:48151 include_delegate_nodes_for_inner_frame_trees_(
152 include_delegate_nodes_for_inner_frame_trees),
Alan Zhaoadbc4f72024-11-08 01:59:36153 queue_(base::from_range, starting_nodes) {
Dave Tapuskadda303d2022-10-04 16:56:48154 // If `include_delegate_nodes_for_inner_frame_trees_` is true then
155 // `should_descend_into_inner_trees_` must be true.
156 DCHECK(!include_delegate_nodes_for_inner_frame_trees_ ||
157 should_descend_into_inner_trees_);
Kevin McNee5f594382021-05-06 23:18:23158 AdvanceNode();
159}
dcheng57e39e22016-01-21 00:25:38160
161FrameTree::NodeIterator FrameTree::NodeRange::begin() {
W. James MacLeance3176d2019-10-18 04:01:45162 // We shouldn't be attempting a frame tree traversal while the tree is
Kevin McNee5f594382021-05-06 23:18:23163 // being constructed or destructed.
Peter Kasting1557e5f2025-01-28 01:14:08164 DCHECK(std::ranges::all_of(starting_nodes_, [](FrameTreeNode* ftn) {
Peter Kastingd5685942022-09-02 17:52:17165 return ftn->current_frame_host();
166 }));
Kevin McNee5f594382021-05-06 23:18:23167
168 return NodeIterator(starting_nodes_, root_of_subtree_to_skip_,
Dave Tapuskadda303d2022-10-04 16:56:48169 should_descend_into_inner_trees_,
170 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38171}
172
173FrameTree::NodeIterator FrameTree::NodeRange::end() {
Dave Tapuskadda303d2022-10-04 16:56:48174 return NodeIterator({}, nullptr, should_descend_into_inner_trees_,
175 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38176}
177
Kevin McNee5f594382021-05-06 23:18:23178FrameTree::NodeRange::NodeRange(
Ali Hijazie63cbaf62023-12-20 19:29:35179 const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
180 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:23181 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48182 bool should_descend_into_inner_trees,
183 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23184 : starting_nodes_(starting_nodes),
185 root_of_subtree_to_skip_(root_of_subtree_to_skip),
Dave Tapuskadda303d2022-10-04 16:56:48186 should_descend_into_inner_trees_(should_descend_into_inner_trees),
187 include_delegate_nodes_for_inner_frame_trees_(
188 include_delegate_nodes_for_inner_frame_trees) {}
Kevin McNee5f594382021-05-06 23:18:23189
190FrameTree::NodeRange::NodeRange(const NodeRange&) = default;
191FrameTree::NodeRange::~NodeRange() = default;
dcheng57e39e22016-01-21 00:25:38192
Carlos Caballero40b0efd2021-01-26 11:55:00193FrameTree::FrameTree(
194 BrowserContext* browser_context,
Carlos Caballero03262522021-02-05 14:49:58195 Delegate* delegate,
Carlos Caballero40b0efd2021-01-26 11:55:00196 NavigationControllerDelegate* navigation_controller_delegate,
197 NavigatorDelegate* navigator_delegate,
198 RenderFrameHostDelegate* render_frame_delegate,
199 RenderViewHostDelegate* render_view_delegate,
200 RenderWidgetHostDelegate* render_widget_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59201 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26202 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44203 Type type)
Carlos Caballero03262522021-02-05 14:49:58204 : delegate_(delegate),
205 render_frame_delegate_(render_frame_delegate),
[email protected]92404c62013-12-04 16:40:46206 render_view_delegate_(render_view_delegate),
[email protected]fa944cb82013-11-15 17:51:21207 render_widget_delegate_(render_widget_delegate),
208 manager_delegate_(manager_delegate),
Jeremy Roman2d8dfe132021-07-06 20:51:26209 page_delegate_(page_delegate),
Carlos Caballero40b0efd2021-01-26 11:55:00210 navigator_(browser_context,
211 *this,
212 navigator_delegate,
213 navigation_controller_delegate),
Harkiran Bolaria16f2c48d2022-04-22 12:39:57214 type_(type),
Arthur Sonzognif6785ec2022-12-05 10:11:50215 root_(*this,
Paul Semel3e241042022-10-11 12:57:31216 nullptr,
217 // The top-level frame must always be in a
218 // document scope.
219 blink::mojom::TreeScopeType::kDocument,
220 false,
Paul Semel3e241042022-10-11 12:57:31221 blink::mojom::FrameOwnerProperties(),
222 blink::FrameOwnerElementType::kNone,
223 blink::FramePolicy()) {}
[email protected]9b159a52013-10-03 17:24:55224
225FrameTree::~FrameTree() {
Takashi Toyoshimaea534ef22021-07-21 03:27:59226 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05227#if DCHECK_IS_ON()
228 DCHECK(was_shut_down_);
229#endif
[email protected]9b159a52013-10-03 17:24:55230}
231
Sharon Yanged884542023-02-02 17:33:44232void FrameTree::ForEachRenderViewHost(
233 base::FunctionRef<void(RenderViewHostImpl*)> on_host) {
234 if (speculative_render_view_host_) {
235 on_host(speculative_render_view_host_.get());
236 }
237
238 for (auto& rvh : render_view_host_map_) {
239 on_host(rvh.second);
240 }
241}
242
Sharon Yang7ce309e2023-01-19 21:39:57243void FrameTree::MakeSpeculativeRVHCurrent() {
244 CHECK(speculative_render_view_host_);
245
246 // The existing RenderViewHost needs to be unregistered first.
247 // Speculative RenderViewHosts are only used for same-SiteInstanceGroup
248 // navigations, so there should be a RenderViewHost of the same
249 // SiteInstanceGroup already in the tree.
250 RenderViewHostMapId speculative_id =
251 speculative_render_view_host_->rvh_map_id();
252 auto it = render_view_host_map_.find(speculative_id);
253 CHECK(it != render_view_host_map_.end());
254 UnregisterRenderViewHost(speculative_id, it->second);
255
256 speculative_render_view_host_->set_is_speculative(false);
257 RegisterRenderViewHost(speculative_id, speculative_render_view_host_.get());
258 speculative_render_view_host_.reset();
259}
260
Avi Drissmanbd153642024-09-03 18:58:05261FrameTreeNode* FrameTree::FindByID(FrameTreeNodeId frame_tree_node_id) {
dcheng57e39e22016-01-21 00:25:38262 for (FrameTreeNode* node : Nodes()) {
263 if (node->frame_tree_node_id() == frame_tree_node_id)
264 return node;
265 }
266 return nullptr;
[email protected]9b159a52013-10-03 17:24:55267}
268
nasko479ea5a2015-02-14 00:03:04269FrameTreeNode* FrameTree::FindByRoutingID(int process_id, int routing_id) {
dmazzoni0b5d2482014-09-10 19:45:57270 RenderFrameHostImpl* render_frame_host =
271 RenderFrameHostImpl::FromID(process_id, routing_id);
272 if (render_frame_host) {
273 FrameTreeNode* result = render_frame_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50274 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57275 return result;
276 }
277
278 RenderFrameProxyHost* render_frame_proxy_host =
279 RenderFrameProxyHost::FromID(process_id, routing_id);
280 if (render_frame_proxy_host) {
281 FrameTreeNode* result = render_frame_proxy_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50282 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57283 return result;
284 }
285
creis6a93a812015-04-24 23:13:17286 return nullptr;
287}
288
289FrameTreeNode* FrameTree::FindByName(const std::string& name) {
290 if (name.empty())
Paul Semel3e241042022-10-11 12:57:31291 return &root_;
creis6a93a812015-04-24 23:13:17292
dcheng57e39e22016-01-21 00:25:38293 for (FrameTreeNode* node : Nodes()) {
294 if (node->frame_name() == name)
295 return node;
[email protected]9b159a52013-10-03 17:24:55296 }
dcheng57e39e22016-01-21 00:25:38297
298 return nullptr;
299}
300
301FrameTree::NodeRange FrameTree::Nodes() {
Alex Moshchuk27caae82017-09-11 23:11:18302 return NodesExceptSubtree(nullptr);
dcheng57e39e22016-01-21 00:25:38303}
304
kenrb61b6c252016-03-22 17:37:15305FrameTree::NodeRange FrameTree::SubtreeNodes(FrameTreeNode* subtree_root) {
Kevin McNee5f594382021-05-06 23:18:23306 return NodeRange({subtree_root}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48307 /*should_descend_into_inner_trees=*/false,
308 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee5f594382021-05-06 23:18:23309}
310
Kevin McNee53f0b2d2021-11-02 18:00:45311FrameTree::NodeRange FrameTree::NodesIncludingInnerTreeNodes() {
Paul Semel3e241042022-10-11 12:57:31312 return NodeRange({&root_}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48313 /*should_descend_into_inner_trees=*/true,
314 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee53f0b2d2021-11-02 18:00:45315}
316
Sreeja Kamishettyd64b993d2022-02-14 12:04:42317std::vector<FrameTreeNode*> FrameTree::CollectNodesForIsLoading() {
318 FrameTree::NodeRange node_range = NodesIncludingInnerTreeNodes();
319 FrameTree::NodeIterator node_iter = node_range.begin();
320 std::vector<FrameTreeNode*> nodes;
321
Daniel Cheng4d54f0a2025-05-26 22:59:12322 CHECK(node_iter != node_range.end());
Arthur Sonzognif6785ec2022-12-05 10:11:50323 FrameTree* root_loading_tree = root_.frame_tree().LoadingTree();
Sreeja Kamishettyd64b993d2022-02-14 12:04:42324 while (node_iter != node_range.end()) {
325 // Skip over frame trees and children which belong to inner web contents
326 // i.e., when nodes doesn't point to the same loading frame tree.
Arthur Sonzognif6785ec2022-12-05 10:11:50327 if ((*node_iter)->frame_tree().LoadingTree() != root_loading_tree) {
Sreeja Kamishettyd64b993d2022-02-14 12:04:42328 node_iter.AdvanceSkippingChildren();
329 } else {
330 nodes.push_back(*node_iter);
331 ++node_iter;
332 }
333 }
334 return nodes;
335}
336
Kevin McNee5f594382021-05-06 23:18:23337FrameTree::NodeRange FrameTree::SubtreeAndInnerTreeNodes(
Dave Tapuskadda303d2022-10-04 16:56:48338 RenderFrameHostImpl* parent,
339 bool include_delegate_nodes_for_inner_frame_trees) {
Ali Hijazie63cbaf62023-12-20 19:29:35340 std::vector<raw_ptr<FrameTreeNode, VectorExperimental>> starting_nodes;
Kevin McNee5f594382021-05-06 23:18:23341 starting_nodes.reserve(parent->child_count());
342 for (size_t i = 0; i < parent->child_count(); ++i) {
343 FrameTreeNode* child = parent->child_at(i);
344 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:48345 if (inner_tree_main_ftn) {
346 if (include_delegate_nodes_for_inner_frame_trees)
347 starting_nodes.push_back(child);
348 starting_nodes.push_back(inner_tree_main_ftn);
349 } else {
350 starting_nodes.push_back(child);
351 }
Kevin McNee5f594382021-05-06 23:18:23352 }
353 const std::vector<FrameTreeNode*> unattached_owned_nodes =
354 parent->delegate()->GetUnattachedOwnedNodes(parent);
355 starting_nodes.insert(starting_nodes.end(), unattached_owned_nodes.begin(),
356 unattached_owned_nodes.end());
357 return NodeRange(starting_nodes, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48358 /* should_descend_into_inner_trees */ true,
359 include_delegate_nodes_for_inner_frame_trees);
kenrb61b6c252016-03-22 17:37:15360}
361
Alex Moshchuk27caae82017-09-11 23:11:18362FrameTree::NodeRange FrameTree::NodesExceptSubtree(FrameTreeNode* node) {
Paul Semel3e241042022-10-11 12:57:31363 return NodeRange({&root_}, node, /*should_descend_into_inner_trees=*/false,
Dave Tapuskadda303d2022-10-04 16:56:48364 /*include_delegate_nodes_for_inner_frame_trees=*/false);
[email protected]9b159a52013-10-03 17:24:55365}
366
Sreeja Kamishettyd64b993d2022-02-14 12:04:42367FrameTree* FrameTree::LoadingTree() {
368 // We return the delegate's loading frame tree to infer loading related
369 // states.
370 return delegate_->LoadingTree();
371}
372
Lucas Furukawa Gadani99125822019-01-03 15:41:49373FrameTreeNode* FrameTree::AddFrame(
Alexander Timin381e7e182020-04-28 19:04:03374 RenderFrameHostImpl* parent,
Balazs Engedyba034e72017-10-27 22:26:28375 int process_id,
376 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18377 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
Oksana Zhuravlovafee097c2019-07-26 17:01:30378 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
379 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30380 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49381 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
382 associated_interface_provider_receiver,
Antonio Gomes9d5c1ef2020-04-30 20:56:41383 blink::mojom::TreeScopeType scope,
Balazs Engedyba034e72017-10-27 22:26:28384 const std::string& frame_name,
385 const std::string& frame_unique_name,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45386 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04387 const blink::LocalFrameToken& frame_token,
Balazs Engedyba034e72017-10-27 22:26:28388 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34389 const blink::DocumentToken& document_token,
Luna Luc3fdacdf2017-11-08 04:48:53390 const blink::FramePolicy& frame_policy,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54391 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Ehsan Karamad192a8da2018-10-21 03:48:08392 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41393 blink::FrameOwnerElementType owner_type,
394 bool is_dummy_frame_for_inner_tree) {
nick8814e652015-12-18 01:44:12395 CHECK_NE(new_routing_id, MSG_ROUTING_NONE);
Jeremy Romanc0c69be2023-11-21 19:14:52396 // Normally this path is for blink adding a child local frame. But fenced
397 // frames add a dummy child frame that never gets a corresponding
398 // RenderFrameImpl in any renderer process, and therefore its `frame_remote`
399 // is invalid. Also its RenderFrameHostImpl is exempt from having
400 // `RenderFrameCreated()` called on it (see later in this method, as well as
401 // `WebContentsObserverConsistencyChecker::RenderFrameHostChanged()`).
Kevin McNee43fe8292021-10-04 22:59:41402 DCHECK_NE(frame_remote.is_valid(), is_dummy_frame_for_inner_tree);
403 DCHECK_NE(browser_interface_broker_receiver.is_valid(),
404 is_dummy_frame_for_inner_tree);
Dominic Farolino12e06d72022-08-05 02:29:49405 DCHECK_NE(associated_interface_provider_receiver.is_valid(),
406 is_dummy_frame_for_inner_tree);
nick8814e652015-12-18 01:44:12407
dgroganfb22f9a2014-10-20 21:32:32408 // A child frame always starts with an initial empty document, which means
409 // it is in the same SiteInstance as the parent frame. Ensure that the process
410 // which requested a child frame to be added is the same as the process of the
411 // parent node.
Emily Andrewsd15fd762024-12-10 20:41:54412 CHECK_EQ(parent->GetProcess()->GetDeprecatedID(), process_id);
dgroganfb22f9a2014-10-20 21:32:32413
Danil Somsikov259aa65f2022-11-11 20:49:44414 std::unique_ptr<FrameTreeNode> new_node = base::WrapUnique(
Arthur Sonzognif6785ec2022-12-05 10:11:50415 new FrameTreeNode(*this, parent, scope, is_created_by_script,
Danil Somsikov259aa65f2022-11-11 20:49:44416 frame_owner_properties, owner_type, frame_policy));
iclelland098da752017-06-28 13:46:50417
418 // Set sandbox flags and container policy and make them effective immediately,
Charlie Hu5130d25e2021-03-05 21:53:39419 // since initial sandbox flags and permissions policy should apply to the
420 // initial empty document in the frame. This needs to happen before the call
421 // to AddChild so that the effective policy is sent to any newly-created
Dave Tapuska2402595f2022-08-03 16:24:21422 // `blink::RemoteFrame` objects when the RenderFrameHost is created.
Charlie Hu5ffc0152019-12-06 15:59:53423 // SetPendingFramePolicy is necessary here because next navigation on this
424 // frame will need the value of pending frame policy instead of effective
425 // frame policy.
Ian Clellandcdc4f312017-10-13 22:24:12426 new_node->SetPendingFramePolicy(frame_policy);
iclelland098da752017-06-28 13:46:50427
Shubhie Panickerddf2a4e2018-03-06 00:09:06428 if (was_discarded)
429 new_node->set_was_discarded();
430
iclelland098da752017-06-28 13:46:50431 // Add the new node to the FrameTree, creating the RenderFrameHost.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40432 FrameTreeNode* added_node = parent->AddChild(
433 std::move(new_node), new_routing_id, std::move(frame_remote), frame_token,
Danil Somsikov259aa65f2022-11-11 20:49:44434 document_token, devtools_frame_token, frame_policy, frame_name,
435 frame_unique_name);
nick8814e652015-12-18 01:44:12436
Garrett Tanzer34cb92fe2022-09-28 17:50:54437 added_node->SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30438
Kevin McNee43fe8292021-10-04 22:59:41439 if (browser_interface_broker_receiver.is_valid()) {
440 added_node->current_frame_host()->BindBrowserInterfaceBrokerReceiver(
441 std::move(browser_interface_broker_receiver));
442 }
Oksana Zhuravlovafee097c2019-07-26 17:01:30443
Antonio Sartoridb967c52021-01-20 09:54:30444 if (policy_container_bind_params) {
Antonio Sartoria1fd1432020-11-25 09:10:20445 added_node->current_frame_host()->policy_container_host()->Bind(
Antonio Sartoridb967c52021-01-20 09:54:30446 std::move(policy_container_bind_params));
Antonio Sartoria1fd1432020-11-25 09:10:20447 }
448
Dominic Farolino12e06d72022-08-05 02:29:49449 if (associated_interface_provider_receiver.is_valid()) {
450 added_node->current_frame_host()->BindAssociatedInterfaceProviderReceiver(
451 std::move(associated_interface_provider_receiver));
452 }
453
nasko03ecfad2016-08-02 00:54:06454 // The last committed NavigationEntry may have a FrameNavigationEntry with the
455 // same |frame_unique_name|, since we don't remove FrameNavigationEntries if
456 // their frames are deleted. If there is a stale one, remove it to avoid
457 // conflicts on future updates.
Fergal Daly09d6c762020-05-29 02:05:18458 NavigationEntryImpl* last_committed_entry = static_cast<NavigationEntryImpl*>(
Carlos Caballero40b0efd2021-01-26 11:55:00459 navigator_.controller().GetLastCommittedEntry());
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45460 if (last_committed_entry) {
461 last_committed_entry->RemoveEntryForFrame(
462 added_node, /* only_if_different_position = */ true);
463 }
nasko03ecfad2016-08-02 00:54:06464
nick8814e652015-12-18 01:44:12465 // Now that the new node is part of the FrameTree and has a RenderFrameHost,
466 // we can announce the creation of the initial RenderFrame which already
467 // exists in the renderer process.
Sreeja Kamishetty5b699622021-01-22 12:54:08468 // For consistency with navigating to a new RenderFrameHost case, we dispatch
469 // RenderFrameCreated before RenderFrameHostChanged.
Kevin McNee43fe8292021-10-04 22:59:41470 if (!is_dummy_frame_for_inner_tree) {
Jeremy Romanc0c69be2023-11-21 19:14:52471 // The outer dummy FrameTreeNode for fenced frames does not have a live
472 // RenderFrame in the renderer process.
Fergal Dalyf9ea35c2020-12-11 15:26:01473 added_node->current_frame_host()->RenderFrameCreated();
Ehsan Karamad44fc72112019-02-26 18:15:47474 }
Sreeja Kamishetty5b699622021-01-22 12:54:08475
476 // Notify the delegate of the creation of the current RenderFrameHost.
477 // This is only for subframes, as the main frame case is taken care of by
478 // WebContentsImpl::Init.
479 manager_delegate_->NotifySwappedFromRenderManager(
Dave Tapuskae45d6fd2021-09-29 17:03:59480 nullptr, added_node->current_frame_host());
Lucas Furukawa Gadani99125822019-01-03 15:41:49481 return added_node;
[email protected]9b159a52013-10-03 17:24:55482}
483
[email protected]58faf942014-02-20 21:03:58484void FrameTree::RemoveFrame(FrameTreeNode* child) {
Alexander Timin381e7e182020-04-28 19:04:03485 RenderFrameHostImpl* parent = child->parent();
[email protected]58faf942014-02-20 21:03:58486 if (!parent) {
Peter Boströmfc7ddc182024-10-31 19:37:21487 NOTREACHED() << "Unexpected RemoveFrame call for main frame.";
[email protected]9b159a52013-10-03 17:24:55488 }
489
Alexander Timin381e7e182020-04-28 19:04:03490 parent->RemoveChild(child);
[email protected]9b159a52013-10-03 17:24:55491}
492
Sharon Yangdcc5f252024-05-09 18:27:23493void FrameTree::CreateProxiesForSiteInstanceGroup(
Harkiran Bolaria2912a6b32022-02-22 16:43:45494 FrameTreeNode* source,
Sharon Yangdcc5f252024-05-09 18:27:23495 SiteInstanceGroup* site_instance_group,
Harkiran Bolaria2912a6b32022-02-22 16:43:45496 const scoped_refptr<BrowsingContextState>&
Alex Moshchukd0d759c22025-05-09 18:18:35497 source_new_browsing_context_state,
498 const std::optional<base::UnguessableToken>& navigation_metrics_token) {
Peilin Wangfc543212024-03-27 16:17:21499 // Will be instantiated with the root proxy later and passed to
500 // `CreateRenderFrameProxy()` to batch create proxies for child frames.
Peilin Wang427aa6a2023-03-22 17:58:33501 std::unique_ptr<BatchedProxyIPCSender> batched_proxy_ipc_sender;
502
alexmos58729042015-06-18 23:20:00503 if (!source || !source->IsMainFrame()) {
Sharon Yangdcc5f252024-05-09 18:27:23504 RenderViewHostImpl* render_view_host =
505 GetRenderViewHost(site_instance_group).get();
arthursonzognic5be3842019-07-09 11:49:14506 if (render_view_host) {
Sharon Yangdcc5f252024-05-09 18:27:23507 root()->render_manager()->EnsureRenderViewInitialized(
Alex Moshchukd0d759c22025-05-09 18:18:35508 render_view_host, site_instance_group, navigation_metrics_token);
arthursonzognic5be3842019-07-09 11:49:14509 } else {
Harkiran Bolaria2912a6b32022-02-22 16:43:45510 // Due to the check above, we are creating either an opener proxy (when
511 // source is null) or a main frame proxy due to a subframe navigation
512 // (when source is not a main frame). In the former case, we should use
513 // root's current BrowsingContextState, while in the latter case we should
514 // use BrowsingContextState from the main RenderFrameHost of the subframe
Dave Tapuska2cf1f532022-08-10 15:30:49515 // being navigated. We want to ensure that the `blink::WebView` is created
Sharon Yangdcc5f252024-05-09 18:27:23516 // in the right SiteInstanceGroup if it doesn't exist, before creating the
Dave Tapuska2cf1f532022-08-10 15:30:49517 // other proxies; if the `blink::WebView` doesn't exist, the only way to
518 // do this is to also create a proxy for the main frame as well.
Peilin Wang427aa6a2023-03-22 17:58:33519 const scoped_refptr<BrowsingContextState>& root_browsing_context_state =
Harkiran Bolaria2912a6b32022-02-22 16:43:45520 source ? source->parent()->GetMainFrame()->browsing_context_state()
Peilin Wang427aa6a2023-03-22 17:58:33521 : root()->current_frame_host()->browsing_context_state();
522
Alison Gale770f3fc2024-04-27 00:39:58523 // TODO(crbug.com/40248300): Batch main frame proxy creation and
Peilin Wang427aa6a2023-03-22 17:58:33524 // pass an instance of `BatchedProxyIPCSender` here instead of nullptr.
525 root()->render_manager()->CreateRenderFrameProxy(
Sharon Yangdcc5f252024-05-09 18:27:23526 site_instance_group, root_browsing_context_state,
Alex Moshchukd0d759c22025-05-09 18:18:35527 navigation_metrics_token,
Peilin Wang427aa6a2023-03-22 17:58:33528 /*batched_proxy_ipc_sender=*/nullptr);
529
530 // We only need to use `BatchedProxyIPCSender` when navigating to a new
Sharon Yangdcc5f252024-05-09 18:27:23531 // SiteInstanceGroup. Proxies do not need to be created when navigating to
532 // a SiteInstanceGroup that has already been encountered, because site
Peilin Wang427aa6a2023-03-22 17:58:33533 // isolation would guarantee that all nodes already have either proxies
534 // or real frames. Due to the check above, the `render_view_host` does
Sharon Yangdcc5f252024-05-09 18:27:23535 // not exist here, which means we have not seen this SiteInstanceGroup
Peilin Wang427aa6a2023-03-22 17:58:33536 // before, so we instantiate `batched_proxy_ipc_sender` to consolidate
537 // IPCs for proxy creation.
Peilin Wangfc543212024-03-27 16:17:21538 base::SafeRef<RenderFrameProxyHost> root_proxy =
539 root_browsing_context_state
Sharon Yangdcc5f252024-05-09 18:27:23540 ->GetRenderFrameProxyHost(site_instance_group)
Peilin Wangfc543212024-03-27 16:17:21541 ->GetSafeRef();
Alex Moshchukd0d759c22025-05-09 18:18:35542 batched_proxy_ipc_sender = std::make_unique<BatchedProxyIPCSender>(
543 std::move(root_proxy), navigation_metrics_token);
[email protected]82307f6b2014-08-07 03:30:12544 }
545 }
546
Sharon Yangdcc5f252024-05-09 18:27:23547 // Check whether we're in an inner delegate and the |site_instance_group|
548 // corresponds to the outer delegate. Subframe proxies aren't needed if this
549 // is the case.
Sharon Yang88f064ca2023-03-24 16:25:18550 bool is_site_instance_group_for_outer_delegate = false;
Alex Moshchuk1226b152019-11-08 18:23:45551 RenderFrameProxyHost* outer_delegate_proxy =
552 root()->render_manager()->GetProxyToOuterDelegate();
553 if (outer_delegate_proxy) {
Sharon Yang88f064ca2023-03-24 16:25:18554 is_site_instance_group_for_outer_delegate =
Sharon Yangdcc5f252024-05-09 18:27:23555 (site_instance_group == outer_delegate_proxy->site_instance_group());
Alex Moshchuk1226b152019-11-08 18:23:45556 }
557
naskoe6edde32014-10-17 15:36:48558 // Proxies are created in the FrameTree in response to a node navigating to a
Sharon Yangdcc5f252024-05-09 18:27:23559 // new SiteInstanceGroup. Since |source|'s navigation will replace the
560 // currently loaded document, the entire subtree under |source| will be
561 // removed, and thus proxy creation is skipped for all nodes in that subtree.
Alex Moshchuk27caae82017-09-11 23:11:18562 //
Sharon Yangdcc5f252024-05-09 18:27:23563 // However, a proxy *is* needed for the |source| node if it is
564 // cross-SiteInstanceGroup from the current node. This lets cross-process
565 // navigations in |source| start with a proxy and follow a remote-to-local
566 // transition, which avoids race conditions in cases where other navigations
567 // need to reference |source| before it commits. See https://crbug.com/756790
568 // for more background. Therefore, NodesExceptSubtree(source) will include
569 // |source| in the nodes traversed (see NodeIterator::operator++).
Alex Moshchuk27caae82017-09-11 23:11:18570 for (FrameTreeNode* node : NodesExceptSubtree(source)) {
Sharon Yangdcc5f252024-05-09 18:27:23571 // If a new frame is created in the current SiteInstanceGroup, other frames
572 // in that SiteInstanceGroup don't need a proxy for the new frame.
Alex Moshchuk27caae82017-09-11 23:11:18573 RenderFrameHostImpl* current_host =
574 node->render_manager()->current_frame_host();
Sharon Yangdcc5f252024-05-09 18:27:23575 SiteInstanceGroup* current_group = current_host->GetSiteInstance()->group();
576
577 // Check that the proxy is for a different SiteInstanceGroup. This ensures
578 // that a navigation within a SiteInstanceGroup does not cause proxies to be
579 // created. That then allows the Blink side to do a local to local frame
580 // transition within the same process.
581 if (current_group != site_instance_group) {
Alex Moshchuk27caae82017-09-11 23:11:18582 if (node == source && !current_host->IsRenderFrameLive()) {
Fergal Daly6de62f52020-10-14 01:56:44583 // We don't create a proxy at |source| when the current RenderFrameHost
584 // isn't live. This is because either (1) the speculative
Alex Moshchuk27caae82017-09-11 23:11:18585 // RenderFrameHost will be committed immediately, and the proxy
Fergal Daly6de62f52020-10-14 01:56:44586 // destroyed right away, in GetFrameHostForNavigation, which makes the
587 // races above impossible, or (2) the early commit will be skipped due
588 // to ShouldSkipEarlyCommitPendingForCrashedFrame, in which case the
589 // proxy for |source| *is* needed, but it will be created later in
590 // CreateProxiesForNewRenderFrameHost.
591 //
592 // TODO(fergal): Consider creating a proxy for |source| here rather than
593 // in CreateProxiesForNewRenderFrameHost for case (2) above.
Alex Moshchuk27caae82017-09-11 23:11:18594 continue;
595 }
Alex Moshchuk1226b152019-11-08 18:23:45596
597 // Do not create proxies for subframes in the outer delegate's
Sharon Yang88f064ca2023-03-24 16:25:18598 // SiteInstanceGroup, since there is no need to expose these subframes to
599 // the outer delegate. See also comments in CreateProxiesForChildFrame()
600 // and https://crbug.com/1013553.
601 if (!node->IsMainFrame() && is_site_instance_group_for_outer_delegate) {
Alex Moshchuk1226b152019-11-08 18:23:45602 continue;
Sharon Yang88f064ca2023-03-24 16:25:18603 }
Alex Moshchuk1226b152019-11-08 18:23:45604
Harkiran Bolaria2912a6b32022-02-22 16:43:45605 // If |node| is the FrameTreeNode being navigated, we use
606 // |browsing_context_state| (as BrowsingContextState might change for
607 // cross-BrowsingInstance navigations). Otherwise, we should use the
608 // |node|'s current BrowsingContextState.
609 node->render_manager()->CreateRenderFrameProxy(
Sharon Yangdcc5f252024-05-09 18:27:23610 site_instance_group,
Peilin Wang427aa6a2023-03-22 17:58:33611 node == source ? source_new_browsing_context_state
612 : node->current_frame_host()->browsing_context_state(),
Alex Moshchukd0d759c22025-05-09 18:18:35613 navigation_metrics_token, batched_proxy_ipc_sender.get());
Alex Moshchuk27caae82017-09-11 23:11:18614 }
dcheng57e39e22016-01-21 00:25:38615 }
Peilin Wang427aa6a2023-03-22 17:58:33616
617 if (batched_proxy_ipc_sender) {
618 batched_proxy_ipc_sender->CreateAllProxies();
619 }
[email protected]82307f6b2014-08-07 03:30:12620}
621
[email protected]9b159a52013-10-03 17:24:55622RenderFrameHostImpl* FrameTree::GetMainFrame() const {
Paul Semel3e241042022-10-11 12:57:31623 return root_.current_frame_host();
[email protected]9b159a52013-10-03 17:24:55624}
625
[email protected]9c9343b2014-03-08 02:56:07626FrameTreeNode* FrameTree::GetFocusedFrame() {
627 return FindByID(focused_frame_tree_node_id_);
628}
629
Sharon Yangefe52632022-03-08 23:06:06630void FrameTree::SetFocusedFrame(FrameTreeNode* node,
631 SiteInstanceGroup* source) {
Rakina Zata Amnicca4889e2021-09-28 23:25:55632 CHECK(node->current_frame_host()->IsActive());
alexmos5357efb2015-12-16 21:44:00633 if (node == GetFocusedFrame())
634 return;
635
Sharon Yangefe52632022-03-08 23:06:06636 std::set<SiteInstanceGroup*> frame_tree_groups =
637 CollectSiteInstanceGroups(this);
alexmosb1dc2162015-11-05 00:59:20638
Sharon Yangefe52632022-03-08 23:06:06639 SiteInstanceGroup* current_group =
640 node->current_frame_host()->GetSiteInstance()->group();
alexmos5357efb2015-12-16 21:44:00641
Sharon Yang7424bda2021-11-04 20:27:43642 // Update the focused frame in all other SiteInstanceGroups. If focus changes
643 // to a cross-group frame, this allows the old focused frame's renderer
alexmosb1dc2162015-11-05 00:59:20644 // process to clear focus from that frame and fire blur events. It also
645 // ensures that the latest focused frame is available in all renderers to
646 // compute document.activeElement.
alexmos5357efb2015-12-16 21:44:00647 //
Sharon Yangefe52632022-03-08 23:06:06648 // We do not notify the |source| SiteInstanceGroup because it already knows
649 // the new focused frame (since it initiated the focus change), and we notify
650 // the new focused frame's SiteInstanceGroup (if it differs from |source|)
651 // separately below.
652 for (auto* group : frame_tree_groups) {
653 if (group != source && group != current_group) {
654 RenderFrameProxyHost* proxy = node->current_frame_host()
655 ->browsing_context_state()
656 ->GetRenderFrameProxyHost(group);
Sharon Yangf92842a2022-03-01 18:21:47657
Rakina Zata Amni80700402021-09-20 17:18:03658 if (proxy) {
659 proxy->SetFocusedFrame();
660 } else {
Sharon Yangf92842a2022-03-01 18:21:47661 base::debug::DumpWithoutCrashing();
Rakina Zata Amni80700402021-09-20 17:18:03662 }
alexmosb1dc2162015-11-05 00:59:20663 }
alexmosca2c6ba2015-10-01 21:52:25664 }
665
Sharon Yangefe52632022-03-08 23:06:06666 // If |node| was focused from a cross-group frame (i.e., via
alexmos5357efb2015-12-16 21:44:00667 // window.focus()), tell its RenderFrame that it should focus.
Sharon Yangefe52632022-03-08 23:06:06668 if (current_group != source)
alexmos5357efb2015-12-16 21:44:00669 node->current_frame_host()->SetFocusedFrame();
670
[email protected]9c9343b2014-03-08 02:56:07671 focused_frame_tree_node_id_ = node->frame_tree_node_id();
alexmos21acae52015-11-07 01:04:43672 node->DidFocus();
dmazzonif27bf892016-03-10 15:51:55673
674 // The accessibility tree data for the root of the frame tree keeps
675 // track of the focused frame too, so update that every time the
676 // focused frame changes.
Kevin McNee582a7d62021-10-12 21:42:22677 root()
678 ->current_frame_host()
679 ->GetOutermostMainFrameOrEmbedder()
680 ->UpdateAXTreeData();
[email protected]9c9343b2014-03-08 02:56:07681}
682
arthursonzognic5be3842019-07-09 11:49:14683scoped_refptr<RenderViewHostImpl> FrameTree::CreateRenderViewHost(
Sharon Yangeb76ee22023-11-29 00:42:09684 SiteInstanceGroup* site_instance_group,
avib7348942015-12-25 20:57:10685 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58686 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57687 scoped_refptr<BrowsingContextState> main_browsing_context_state,
Khushal Sagar402d1c9f2023-11-15 22:21:48688 CreateRenderViewHostCase create_case,
Arthur Sonzognic686e8f2024-01-11 08:36:37689 std::optional<viz::FrameSinkId> frame_sink_id) {
Harkiran Bolaria57e2b062022-03-14 10:27:58690 if (main_browsing_context_state) {
691 DCHECK(main_browsing_context_state->is_main_frame());
692 }
dcheng29f5a6c2015-08-31 21:43:27693 RenderViewHostImpl* rvh =
694 static_cast<RenderViewHostImpl*>(RenderViewHostFactory::Create(
Sharon Yangeb76ee22023-11-29 00:42:09695 this, site_instance_group,
696 site_instance_group->GetStoragePartitionConfig(),
697 render_view_delegate_, render_widget_delegate_, main_frame_routing_id,
Sharon Yang7ce309e2023-01-19 21:39:57698 renderer_initiated_creation, std::move(main_browsing_context_state),
Khushal Sagar402d1c9f2023-11-15 22:21:48699 create_case, frame_sink_id));
Sharon Yang7ce309e2023-01-19 21:39:57700
Rakina Zata Amnidf9988fa12024-07-30 02:00:50701 if (create_case == CreateRenderViewHostCase::kSpeculative) {
Sharon Yang7ce309e2023-01-19 21:39:57702 set_speculative_render_view_host(rvh->GetWeakPtr());
703 } else {
704 // Register non-speculative RenderViewHosts. If they are speculative, they
705 // will be registered when they become active.
706 RegisterRenderViewHost(rvh->rvh_map_id(), rvh);
707 }
708
arthursonzognic5be3842019-07-09 11:49:14709 return base::WrapRefCounted(rvh);
[email protected]94d0cc12013-12-18 00:07:41710}
711
arthursonzognic5be3842019-07-09 11:49:14712scoped_refptr<RenderViewHostImpl> FrameTree::GetRenderViewHost(
Sharon Yang57bde122022-03-01 20:01:12713 SiteInstanceGroup* group) {
Sharon Yanga2fe85e2022-02-09 21:38:29714 // When called from RenderFrameHostManager::CreateRenderFrameHost, it's
715 // possible that a RenderProcessHost hasn't yet been created, which means
Sharon Yang57bde122022-03-01 20:01:12716 // a SiteInstanceGroup won't have been created yet.
Sharon Yanga2fe85e2022-02-09 21:38:29717 if (!group)
718 return nullptr;
719
720 auto it = render_view_host_map_.find(GetRenderViewHostMapId(group));
arthursonzogni88e54ae2019-04-15 12:57:36721 if (it == render_view_host_map_.end())
722 return nullptr;
alexmos9aa61232016-04-26 21:54:02723
arthursonzognic5be3842019-07-09 11:49:14724 return base::WrapRefCounted(it->second);
[email protected]94d0cc12013-12-18 00:07:41725}
726
Aaron Colwell78b4bde2021-03-16 16:16:09727FrameTree::RenderViewHostMapId FrameTree::GetRenderViewHostMapId(
Sharon Yangc581a0c2021-11-02 18:09:39728 SiteInstanceGroup* site_instance_group) const {
729 return RenderViewHostMapId::FromUnsafeValue(
730 site_instance_group->GetId().value());
Lowell Manners75055a132019-10-11 10:30:29731}
732
Aaron Colwell78b4bde2021-03-16 16:16:09733void FrameTree::RegisterRenderViewHost(RenderViewHostMapId id,
734 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47735 TRACE_EVENT_INSTANT("navigation", "FrameTree::RegisterRenderViewHost",
736 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57737 CHECK(!rvh->is_speculative());
Rakina Zata Amni89b8aa32024-09-20 03:36:54738 bool rvh_id_already_in_map = base::Contains(render_view_host_map_, id);
739 bool rfh_in_bfcache =
740 controller()
741 .GetBackForwardCache()
742 .IsRenderFrameHostWithSIGInBackForwardCacheForDebugging(
743 rvh->site_instance_group()->GetId());
744 bool rfph_in_bfcache =
745 controller()
746 .GetBackForwardCache()
747 .IsRenderFrameProxyHostWithSIGInBackForwardCacheForDebugging(
748 rvh->site_instance_group()->GetId());
749 bool rvh_in_bfcache =
750 controller()
751 .GetBackForwardCache()
752 .IsRenderViewHostWithMapIdInBackForwardCacheForDebugging(*rvh);
753 // We're seeing cases where an RVH being restored from BFCache has the same
754 // ID as an RVH already in the map, where the 2 RVHs are different but one
755 // was in BFCache and one isn't.
756 // To investigate, detect if any of these cases happen:
757 // 1) A RenderViewHost with the same ID as `rvh` is already in the map
758 // 2) A RenderFrameHost with the same SIG ID as `rvh` is in BFCache
759 // 3) A RenderFrameProxyHost with the same SIG ID as `rvh` is in BFCache
760 // 4) A RenderViewHost with the same ID as `rvh` is in BFCache
761 // These cases shouldn't be possible. Note that when checking #2-#4 for
762 // a RenderViewHost that is getting out of BFCache, we are guaranteed to not
763 // accidentally match to the RVH/RFPH/RFH of the page being restored,
764 // because we can only get here after the StoredPage is taken out of the
765 // BFCache and thus won't be iterated over in the functions above.
766 // See the linked bug below for more details.
767 if (rvh_id_already_in_map || rfh_in_bfcache || rfph_in_bfcache ||
768 rvh_in_bfcache) {
Rakina Zata Amni9b7ea7052024-09-04 20:40:58769 // TODO(https://crbug.com/354382462): Remove crash keys once investigation
770 // is done.
Rakina Zata Amni89b8aa32024-09-20 03:36:54771 SCOPED_CRASH_KEY_BOOL("rvh-double", "in_map", rvh_id_already_in_map);
772 SCOPED_CRASH_KEY_BOOL("rvh-double", "rfh_in_bfcache", rfh_in_bfcache);
773 SCOPED_CRASH_KEY_BOOL("rvh-double", "rfph_in_bfcache", rfph_in_bfcache);
774 SCOPED_CRASH_KEY_BOOL("rvh-double", "rvh_in_bfcache", rvh_in_bfcache);
775 SCOPED_CRASH_KEY_BOOL("rvh-double", "passed_renderer_created",
Rakina Zata Amnid3006032024-09-12 00:53:53776 rvh->renderer_view_created());
Rakina Zata Amnid3006032024-09-12 00:53:53777 SCOPED_CRASH_KEY_NUMBER("rvh-double", "passed_rvh_main_id",
778 rvh->main_frame_routing_id());
779 SCOPED_CRASH_KEY_NUMBER("rvh-double", "root_routing_id",
780 root()->current_frame_host()->GetRoutingID());
Rakina Zata Amnid3006032024-09-12 00:53:53781 SCOPED_CRASH_KEY_NUMBER("rvh-double", "passed_rvh_ptr",
782 reinterpret_cast<size_t>(rvh));
Rakina Zata Amni89b8aa32024-09-20 03:36:54783 SCOPED_CRASH_KEY_BOOL("rvh-double", "passed_rvh_bfcache",
784 rvh->is_in_back_forward_cache());
785 SCOPED_CRASH_KEY_BOOL("rvh-double", "frame_tree_primary", is_primary());
786
787 if (rvh_id_already_in_map) {
788 SCOPED_CRASH_KEY_BOOL(
789 "rvh-double", "mapped_rvh_registered",
790 render_view_host_map_[id]->is_registered_with_frame_tree());
791 SCOPED_CRASH_KEY_NUMBER(
792 "rvh-double", "mapped_rvh_main_id",
793 render_view_host_map_[id]->main_frame_routing_id());
794 SCOPED_CRASH_KEY_NUMBER(
795 "rvh-double", "map_rvh_ptr",
796 reinterpret_cast<size_t>(render_view_host_map_[id]));
797 SCOPED_CRASH_KEY_BOOL(
798 "rvh-double", "map_rvh_bfcache",
799 render_view_host_map_[id]->is_in_back_forward_cache());
800 SCOPED_CRASH_KEY_BOOL("rvh-double", "mapped_renderer_created",
801 render_view_host_map_[id]->renderer_view_created());
802 CHECK_EQ(rvh, render_view_host_map_[id]);
803 }
Rakina Zata Amni9b7ea7052024-09-04 20:40:58804 }
Aaron Colwell78b4bde2021-03-16 16:16:09805 render_view_host_map_[id] = rvh;
Sharon Yang7ce309e2023-01-19 21:39:57806 rvh->set_is_registered_with_frame_tree(true);
Aaron Colwell78b4bde2021-03-16 16:16:09807}
808
809void FrameTree::UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13810 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47811 TRACE_EVENT_INSTANT("navigation", "FrameTree::UnregisterRenderViewHost",
812 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57813 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09814 auto it = render_view_host_map_.find(id);
arthursonzogni88e54ae2019-04-15 12:57:36815 CHECK(it != render_view_host_map_.end());
arthursonzognic5be3842019-07-09 11:49:14816 CHECK_EQ(it->second, rvh);
817 render_view_host_map_.erase(it);
Sharon Yang7ce309e2023-01-19 21:39:57818 rvh->set_is_registered_with_frame_tree(false);
[email protected]94d0cc12013-12-18 00:07:41819}
820
arthursonzogni0a2007792019-05-28 21:32:48821void FrameTree::FrameUnloading(FrameTreeNode* frame) {
822 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
Avi Drissmanbd153642024-09-03 18:58:05823 focused_frame_tree_node_id_ = FrameTreeNodeId();
arthursonzogni0a2007792019-05-28 21:32:48824
825 // Ensure frames that are about to be deleted aren't visible from the other
826 // processes anymore.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40827 frame->GetBrowsingContextStateForSubframe()->ResetProxyHosts();
arthursonzogni0a2007792019-05-28 21:32:48828}
829
dmazzonie950ea232015-03-13 21:39:45830void FrameTree::FrameRemoved(FrameTreeNode* frame) {
nick53d5cbf2015-04-23 22:50:14831 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
Avi Drissmanbd153642024-09-03 18:58:05832 focused_frame_tree_node_id_ = FrameTreeNodeId();
dmazzonie950ea232015-03-13 21:39:45833}
834
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15835double FrameTree::GetLoadProgress() {
Paul Semel3e241042022-10-11 12:57:31836 if (root_.HasNavigation())
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15837 return blink::kInitialLoadProgress;
838
Paul Semel3e241042022-10-11 12:57:31839 return root_.current_frame_host()->GetPage().load_progress();
fdegans1d16355162015-03-26 11:58:34840}
841
Yu Gaoc8c18552022-06-22 14:38:45842bool FrameTree::IsLoadingIncludingInnerFrameTrees() const {
Nate Chapin470dbc62023-04-25 16:34:38843 return GetLoadingState() != LoadingState::NONE;
844}
845
846LoadingState FrameTree::GetLoadingState() const {
847 // The overall loading state for the FrameTree matches the root node's loading
848 // state if the root is loading.
849 if (root_.GetLoadingState() != LoadingState::NONE) {
850 return root_.GetLoadingState();
dcheng57e39e22016-01-21 00:25:38851 }
Nate Chapin470dbc62023-04-25 16:34:38852
853 // Otherwise, check if a subframe is loading without an associated navigation
854 // in the root frame. If so, we are loading, but we don't want to show
855 // loading UI.
856 for (const FrameTreeNode* node_to_check :
857 const_cast<FrameTree*>(this)->CollectNodesForIsLoading()) {
858 if (node_to_check->IsLoading()) {
859 return LoadingState::LOADING_WITHOUT_UI;
860 }
861 }
862 return LoadingState::NONE;
fdegans1d16355162015-03-26 11:58:34863}
864
alexmos3fcd0ca2015-10-23 18:18:33865void FrameTree::ReplicatePageFocus(bool is_focused) {
Dave Tapuska53e6ce12021-08-06 16:08:24866 // Focus loss may occur while this FrameTree is being destroyed. Don't
867 // send the message in this case, as the main frame's RenderFrameHost and
868 // other state has already been cleared.
869 if (is_being_destroyed_)
870 return;
Sharon Yangefe52632022-03-08 23:06:06871 std::set<SiteInstanceGroup*> frame_tree_site_instance_groups =
872 CollectSiteInstanceGroups(this);
alexmos3fcd0ca2015-10-23 18:18:33873
Sharon Yangefe52632022-03-08 23:06:06874 // Send the focus update to main frame's proxies in all SiteInstanceGroups of
alexmos3fcd0ca2015-10-23 18:18:33875 // other frames in this FrameTree. Note that the main frame might also know
Sharon Yangefe52632022-03-08 23:06:06876 // about proxies in SiteInstanceGroups for frames in a different FrameTree
877 // (e.g., for window.open), so we can't just iterate over its proxy_hosts_ in
alexmos3fcd0ca2015-10-23 18:18:33878 // RenderFrameHostManager.
Sharon Yangefe52632022-03-08 23:06:06879 for (auto* group : frame_tree_site_instance_groups)
880 SetPageFocus(group, is_focused);
alexmos0d7e0b09b2015-10-29 22:11:48881}
alexmos3fcd0ca2015-10-23 18:18:33882
Sharon Yangefe52632022-03-08 23:06:06883void FrameTree::SetPageFocus(SiteInstanceGroup* group, bool is_focused) {
Paul Semel3e241042022-10-11 12:57:31884 RenderFrameHostManager* root_manager = root_.render_manager();
alexmos0d7e0b09b2015-10-29 22:11:48885
886 // This is only used to set page-level focus in cross-process subframes, and
Sharon Yangefe52632022-03-08 23:06:06887 // requests to set focus in main frame's SiteInstanceGroup are ignored.
888 if (group != root_manager->current_frame_host()->GetSiteInstance()->group()) {
889 RenderFrameProxyHost* proxy = root_manager->current_frame_host()
890 ->browsing_context_state()
891 ->GetRenderFrameProxyHost(group);
Dave Tapuska82b54012022-07-15 23:26:10892 if (proxy->is_render_frame_proxy_live())
893 proxy->GetAssociatedRemoteFrame()->SetPageFocus(is_focused);
alexmos3fcd0ca2015-10-23 18:18:33894 }
895}
896
W. James MacLeanc07dc41b2022-07-25 18:52:16897void FrameTree::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanb70fab82020-05-01 18:51:14898 const url::Origin& previously_visited_origin,
899 NavigationRequest* navigation_request_to_exclude) {
W. James MacLeanc07dc41b2022-07-25 18:52:16900 controller().RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanc6dc86c2021-08-12 13:58:37901 previously_visited_origin);
902
Charlie Reis37be2682023-01-10 17:04:47903 std::unordered_set<SiteInstanceImpl*> matching_site_instances;
W. James MacLeanb70fab82020-05-01 18:51:14904
905 // Be sure to visit all RenderFrameHosts associated with this frame that might
906 // have an origin that could script other frames. We skip RenderFrameHosts
907 // that are in the bfcache, assuming there's no way for a frame to join the
908 // BrowsingInstance of a bfcache RFH while it's in the cache.
909 for (auto* frame_tree_node : SubtreeNodes(root())) {
910 auto* frame_host = frame_tree_node->current_frame_host();
W. James MacLeanb70fab82020-05-01 18:51:14911 if (previously_visited_origin == frame_host->GetLastCommittedOrigin())
912 matching_site_instances.insert(frame_host->GetSiteInstance());
913
914 if (frame_host->HasCommittingNavigationRequestForOrigin(
915 previously_visited_origin, navigation_request_to_exclude)) {
916 matching_site_instances.insert(frame_host->GetSiteInstance());
917 }
918
919 auto* spec_frame_host =
920 frame_tree_node->render_manager()->speculative_frame_host();
921 if (spec_frame_host &&
922 spec_frame_host->HasCommittingNavigationRequestForOrigin(
923 previously_visited_origin, navigation_request_to_exclude)) {
924 matching_site_instances.insert(spec_frame_host->GetSiteInstance());
925 }
926
927 auto* navigation_request = frame_tree_node->navigation_request();
928 if (navigation_request &&
929 navigation_request != navigation_request_to_exclude &&
930 navigation_request->HasCommittingOrigin(previously_visited_origin)) {
931 matching_site_instances.insert(frame_host->GetSiteInstance());
932 }
933 }
934
935 // Update any SiteInstances found to contain |origin|.
936 for (auto* site_instance : matching_site_instances) {
Charlie Reis37be2682023-01-10 17:04:47937 site_instance->RegisterAsDefaultOriginIsolation(previously_visited_origin);
W. James MacLeanb70fab82020-05-01 18:51:14938 }
939}
940
Charlie Reis37be2682023-01-10 17:04:47941void FrameTree::Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00942 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17943 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46944 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44945 const blink::FramePolicy& frame_policy,
946 const base::UnguessableToken& devtools_frame_token) {
Carlos Caballero40b0efd2021-01-26 11:55:00947 // blink::FrameTree::SetName always keeps |unique_name| empty in case of a
948 // main frame - let's do the same thing here.
949 std::string unique_name;
Paul Semel3e241042022-10-11 12:57:31950 root_.render_manager()->InitRoot(main_frame_site_instance,
951 renderer_initiated_creation, frame_policy,
Danil Somsikov259aa65f2022-11-11 20:49:44952 main_frame_name, devtools_frame_token);
Paul Semel3e241042022-10-11 12:57:31953 root_.SetFencedFramePropertiesIfNeeded();
Rakina Zata Amniafd3c6582021-11-30 06:19:17954
sbingler1f758842023-02-22 00:23:32955 // The initial empty document should inherit the origin (the origin may
956 // change after the first commit) and other state (such as the
957 // RuntimeFeatureStateReadContext) from its opener, except when they are in
Rakina Zata Amni4eb716e2022-04-05 21:32:46958 // different browsing context groups (`renderer_initiated_creation` will be
sbingler1f758842023-02-22 00:23:32959 // false), where it should use a new opaque origin and default values for the
960 // other state, respectively.
Rakina Zata Amniafd3c6582021-11-30 06:19:17961 // See also https://crbug.com/932067.
962 //
963 // Note that the origin of the new frame might depend on sandbox flags.
964 // Checking sandbox flags of the new frame should be safe at this point,
965 // because the flags should be already inherited when creating the root node.
Rakina Zata Amni4eb716e2022-04-05 21:32:46966 DCHECK(!renderer_initiated_creation || opener_for_origin);
Paul Semel3e241042022-10-11 12:57:31967 root_.current_frame_host()->SetOriginDependentStateOfNewFrame(
sbingler1f758842023-02-22 00:23:32968 renderer_initiated_creation ? opener_for_origin : nullptr);
Rakina Zata Amni2322f4f82022-01-24 13:24:24969
Rakina Zata Amni46087a12022-11-11 08:28:38970 controller().CreateInitialEntry();
Carlos Caballero40b0efd2021-01-26 11:55:00971}
972
Carlos Caballeroede6f8c2021-01-28 11:01:50973void FrameTree::DidAccessInitialMainDocument() {
974 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidAccessInitialDocument");
975 has_accessed_initial_main_document_ = true;
976 controller().DidAccessInitialMainDocument();
977}
978
Nate Chapin470dbc62023-04-25 16:34:38979void FrameTree::NodeLoadingStateChanged(
980 FrameTreeNode& node,
981 LoadingState previous_frame_tree_loading_state) {
982 LoadingState new_frame_tree_loading_state = GetLoadingState();
983 if (previous_frame_tree_loading_state == new_frame_tree_loading_state) {
Carlos Caballero03262522021-02-05 14:49:58984 return;
Nate Chapin470dbc62023-04-25 16:34:38985 }
Carlos Caballero03262522021-02-05 14:49:58986
Nate Chapin470dbc62023-04-25 16:34:38987 root()->render_manager()->SetIsLoading(new_frame_tree_loading_state !=
988 LoadingState::NONE);
989 delegate_->LoadingStateChanged(new_frame_tree_loading_state);
990 if (previous_frame_tree_loading_state == LoadingState::NONE) {
991 delegate_->DidStartLoading(&node);
992 } else if (new_frame_tree_loading_state == LoadingState::NONE) {
993 delegate_->DidStopLoading();
994 }
Carlos Caballero03262522021-02-05 14:49:58995}
996
Carlos Caballero03262522021-02-05 14:49:58997void FrameTree::DidCancelLoading() {
998 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidCancelLoading");
999 navigator_.controller().DiscardNonCommittedEntries();
Carlos Caballero03262522021-02-05 14:49:581000}
1001
1002void FrameTree::StopLoading() {
1003 for (FrameTreeNode* node : Nodes())
1004 node->StopLoading();
1005}
1006
Carlos Caballero101ac26b2021-03-24 11:54:051007void FrameTree::Shutdown() {
Dave Tapuskae88286de2021-08-05 19:10:421008 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:051009#if DCHECK_IS_ON()
1010 DCHECK(!was_shut_down_);
1011 was_shut_down_ = true;
1012#endif
1013
Paul Semel3e241042022-10-11 12:57:311014 RenderFrameHostManager* root_manager = root_.render_manager();
Carlos Caballero101ac26b2021-03-24 11:54:051015
Carlos Caballerodb7a8f6e2021-04-09 18:17:471016 if (!root_manager->current_frame_host()) {
1017 // The page has been transferred out during an activation. There is little
1018 // left to do.
Alison Gale770f3fc2024-04-27 00:39:581019 // TODO(crbug.com/40177949): If we decide that pending delete RFHs
Carlos Caballerodb7a8f6e2021-04-09 18:17:471020 // need to be moved along during activation replace this line with a DCHECK
1021 // that there are no pending delete instances.
1022 root_manager->ClearRFHsPendingShutdown();
Paul Semel3e241042022-10-11 12:57:311023 DCHECK(!root_.navigation_request());
Carlos Caballerodb7a8f6e2021-04-09 18:17:471024 DCHECK(!root_manager->speculative_frame_host());
Paul Semel3e241042022-10-11 12:57:311025 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:051026 return;
Carlos Caballerodb7a8f6e2021-04-09 18:17:471027 }
Carlos Caballero101ac26b2021-03-24 11:54:051028
1029 for (FrameTreeNode* node : Nodes()) {
1030 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
1031 // to be shutdown and be deleted as well.
1032 node->render_manager()->ClearRFHsPendingShutdown();
Alison Gale47d1537d2024-04-19 21:31:461033 // TODO(crbug.com/40177939): Ban WebUI instance in Prerender pages.
Carlos Caballero101ac26b2021-03-24 11:54:051034 node->render_manager()->ClearWebUIInstances();
1035 }
1036
1037 // Destroy all subframes now. This notifies observers.
1038 root_manager->current_frame_host()->ResetChildren();
Harkiran Bolaria0b3bdef02022-03-10 13:04:401039 root_manager->current_frame_host()
1040 ->browsing_context_state()
1041 ->ResetProxyHosts();
Carlos Caballero101ac26b2021-03-24 11:54:051042
Carlos Caballero101ac26b2021-03-24 11:54:051043 // Manually call the observer methods for the root FrameTreeNode. It is
1044 // necessary to manually delete all objects tracking navigations
1045 // (NavigationHandle, NavigationRequest) for observers to be properly
1046 // notified of these navigations stopping before the WebContents is
1047 // destroyed.
1048
1049 root_manager->current_frame_host()->RenderFrameDeleted();
Rakina Zata Amnif8f2bb62022-11-23 05:54:321050 root_manager->current_frame_host()->ResetOwnedNavigationRequests(
1051 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:051052
1053 // Do not update state as the FrameTree::Delegate (possibly a WebContents) is
1054 // being destroyed.
Rakina Zata Amni58681c62024-06-25 06:32:131055 root_.ResetNavigationRequestButKeepState(
1056 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:051057 if (root_manager->speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:451058 root_manager->DiscardSpeculativeRenderFrameHostForShutdown();
Carlos Caballero101ac26b2021-03-24 11:54:051059 }
1060
Alexander Timin8aeee642021-05-12 08:39:331061 // NavigationRequests restoring the page from bfcache have a reference to the
1062 // RFHs stored in the cache, so the cache should be cleared after the
1063 // navigation request is reset.
1064 controller().GetBackForwardCache().Shutdown();
1065
Paul Semel3e241042022-10-11 12:57:311066 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:051067 render_view_delegate_->RenderViewDeleted(
1068 root_manager->current_frame_host()->render_view_host());
1069}
1070
Dave Tapuskad8b0530f2021-10-19 15:12:311071base::SafeRef<FrameTree> FrameTree::GetSafeRef() {
1072 return weak_ptr_factory_.GetSafeRef();
1073}
1074
Dave Tapuska54c76a032021-10-27 22:10:421075void FrameTree::FocusOuterFrameTrees() {
1076 OPTIONAL_TRACE_EVENT0("content", "FrameTree::FocusOuterFrameTrees");
1077
1078 FrameTree* frame_tree_to_focus = this;
1079 while (true) {
1080 FrameTreeNode* outer_node = FrameTreeNode::GloballyFindByID(
1081 frame_tree_to_focus->delegate()->GetOuterDelegateFrameTreeNodeId());
1082 if (!outer_node || !outer_node->current_frame_host()->IsActive()) {
1083 // Don't set focus on an inactive FrameTreeNode.
1084 return;
1085 }
Arthur Sonzognif6785ec2022-12-05 10:11:501086 outer_node->frame_tree().SetFocusedFrame(outer_node, nullptr);
Dave Tapuska54c76a032021-10-27 22:10:421087
1088 // For a browser initiated focus change, let embedding renderer know of the
1089 // change. Otherwise, if the currently focused element is just across a
1090 // process boundary in focus order, it will not be possible to move across
1091 // that boundary. This is because the target element will already be focused
1092 // (that renderer was not notified) and drop the event.
1093 if (auto* proxy_to_outer_delegate = frame_tree_to_focus->root()
1094 ->render_manager()
1095 ->GetProxyToOuterDelegate()) {
1096 proxy_to_outer_delegate->SetFocusedFrame();
1097 }
Arthur Sonzognif6785ec2022-12-05 10:11:501098 frame_tree_to_focus = &outer_node->frame_tree();
Dave Tapuska54c76a032021-10-27 22:10:421099 }
1100}
1101
Thomas Lukaszewicz8ada31b2024-09-01 19:18:161102void FrameTree::Discard() {
Thomas Lukaszewicz0ef0f45f2025-02-18 17:59:421103 const auto attempt_discard = [this]() {
1104 // A speculative pending-commit rfh should not be cancelled or deleted. In
1105 // this case ignore the discard request and allow the navigation to complete
1106 // as normal.
1107 if (const auto* speculative_rfh =
1108 root()->render_manager()->speculative_frame_host();
1109 speculative_rfh && speculative_rfh->HasPendingCommitNavigation()) {
1110 return false;
1111 }
Thomas Lukaszewicz1b672fe2024-09-17 08:35:031112
Thomas Lukaszewicz0ef0f45f2025-02-18 17:59:421113 root()->set_was_discarded();
1114 root()->current_frame_host()->DiscardFrame();
1115 NavigationControllerImpl& navigation_controller = controller();
1116 navigation_controller.SetNeedsReload();
1117 navigation_controller.GetBackForwardCache().Flush();
1118 return true;
1119 };
1120 base::UmaHistogramBoolean("Discarding.DiscardFrameTree", attempt_discard());
Thomas Lukaszewicz8ada31b2024-09-01 19:18:161121}
1122
[email protected]9b159a52013-10-03 17:24:551123} // namespace content