blob: 5764d8d89ca9878c927dbd97ad6a9ff30d96fbdc [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
[email protected]9b159a52013-10-03 17:24:559#include <queue>
Lukasz Anforowicz7bfb2e92017-11-22 17:19:4510#include <set>
dcheng29f5a6c2015-08-31 21:43:2711#include <utility>
[email protected]9b159a52013-10-03 17:24:5512
Lei Zhangde197672021-04-29 08:11:2413#include "base/containers/contains.h"
Rakina Zata Amni80700402021-09-20 17:18:0314#include "base/debug/dump_without_crashing.h"
Avi Drissmanadac21992023-01-11 23:46:3915#include "base/functional/bind.h"
16#include "base/functional/callback.h"
[email protected]20edca72014-08-14 10:27:5317#include "base/lazy_instance.h"
dcheng9bfa5162016-04-09 01:00:5718#include "base/memory/ptr_util.h"
Ali Hijazie63cbaf62023-12-20 19:29:3519#include "base/memory/raw_ptr.h"
Peilin Wang427aa6a2023-03-22 17:58:3320#include "base/memory/safe_ref.h"
Alex Goughd836aa42024-07-15 23:56:5221#include "base/not_fatal_until.h"
Peter Kastingd5685942022-09-02 17:52:1722#include "base/ranges/algorithm.h"
Alan Zhaoadbc4f72024-11-08 01:59:3623#include "base/ranges/from_range.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"
Pavel Feldman25234722017-10-11 02:49:0626#include "base/unguessable_token.h"
Peilin Wang427aa6a2023-03-22 17:58:3327#include "content/browser/renderer_host/batched_proxy_ipc_sender.h"
danakjc492bf82020-09-09 20:02:4428#include "content/browser/renderer_host/navigation_controller_impl.h"
29#include "content/browser/renderer_host/navigation_entry_impl.h"
30#include "content/browser/renderer_host/navigation_request.h"
31#include "content/browser/renderer_host/navigator.h"
32#include "content/browser/renderer_host/navigator_delegate.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1533#include "content/browser/renderer_host/page_impl.h"
Kevin McNee5f594382021-05-06 23:18:2334#include "content/browser/renderer_host/render_frame_host_delegate.h"
danakjc492bf82020-09-09 20:02:4435#include "content/browser/renderer_host/render_frame_host_factory.h"
36#include "content/browser/renderer_host/render_frame_host_impl.h"
37#include "content/browser/renderer_host/render_frame_proxy_host.h"
Carlos Caballero101ac26b2021-03-24 11:54:0538#include "content/browser/renderer_host/render_view_host_delegate.h"
[email protected]94d0cc12013-12-18 00:07:4139#include "content/browser/renderer_host/render_view_host_factory.h"
40#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yang7ce309e2023-01-19 21:39:5741#include "content/common/content_navigation_policy.h"
japhet98e9bd82016-06-28 23:48:4542#include "content/common/content_switches_internal.h"
Peilin Wang427aa6a2023-03-22 17:58:3343#include "content/common/features.h"
Sreeja Kamishetty46f762c2021-02-05 07:52:4644#include "third_party/blink/public/common/features.h"
Kevin McNee43fe8292021-10-04 22:59:4145#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Blink Reformata30d4232018-04-07 15:31:0646#include "third_party/blink/public/common/frame/frame_policy.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1547#include "third_party/blink/public/common/loader/loader_constants.h"
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:5448#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
[email protected]9b159a52013-10-03 17:24:5549
50namespace content {
51
52namespace {
[email protected]20edca72014-08-14 10:27:5353
Rakina Zata Amni4b1968d2021-09-09 03:29:4754using perfetto::protos::pbzero::ChromeTrackEvent;
55
Sharon Yangefe52632022-03-08 23:06:0656// Helper function to collect SiteInstanceGroups involved in rendering a single
57// FrameTree (which is a subset of SiteInstanceGroups in main frame's
58// proxy_hosts_ because of openers).
59std::set<SiteInstanceGroup*> CollectSiteInstanceGroups(FrameTree* tree) {
60 std::set<SiteInstanceGroup*> groups;
Adithya Srinivasan0e9808342022-12-13 18:25:0761 for (FrameTreeNode* node : tree->Nodes())
Sharon Yangefe52632022-03-08 23:06:0662 groups.insert(node->current_frame_host()->GetSiteInstance()->group());
63 return groups;
alexmos3fcd0ca2015-10-23 18:18:3364}
65
Kevin McNee5f594382021-05-06 23:18:2366// If |node| is the placeholder FrameTreeNode for an embedded frame tree,
67// returns the inner tree's main frame's FrameTreeNode. Otherwise, returns null.
68FrameTreeNode* GetInnerTreeMainFrameNode(FrameTreeNode* node) {
Dominic Farolino377edb302021-07-29 05:57:1869 FrameTreeNode* inner_main_frame_tree_node = FrameTreeNode::GloballyFindByID(
70 node->current_frame_host()->inner_tree_main_frame_tree_node_id());
Kevin McNee5f594382021-05-06 23:18:2371
Kevin McNeed459ad42022-09-12 19:24:5972 if (inner_main_frame_tree_node) {
Arthur Sonzognif6785ec2022-12-05 10:11:5073 DCHECK_NE(&node->frame_tree(), &inner_main_frame_tree_node->frame_tree());
Kevin McNee5f594382021-05-06 23:18:2374 }
75
Kevin McNeed459ad42022-09-12 19:24:5976 return inner_main_frame_tree_node;
Kevin McNee5f594382021-05-06 23:18:2377}
78
[email protected]9b159a52013-10-03 17:24:5579} // namespace
80
vmpstr33895d992016-02-24 20:55:2181FrameTree::NodeIterator::NodeIterator(const NodeIterator& other) = default;
82
Fergal Daly55b6d722020-09-11 07:56:3383FrameTree::NodeIterator::~NodeIterator() = default;
dcheng57e39e22016-01-21 00:25:3884
85FrameTree::NodeIterator& FrameTree::NodeIterator::operator++() {
Alex Moshchuk27caae82017-09-11 23:11:1886 if (current_node_ != root_of_subtree_to_skip_) {
Jayson Adams4db0bfe22021-07-15 19:24:0787 // Reserve enough space in the queue to accommodate the nodes we're
88 // going to add, to avoid repeated resize calls.
89 queue_.reserve(queue_.size() + current_node_->child_count());
90
Alex Moshchuk27caae82017-09-11 23:11:1891 for (size_t i = 0; i < current_node_->child_count(); ++i) {
92 FrameTreeNode* child = current_node_->child_at(i);
Kevin McNee5f594382021-05-06 23:18:2393 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:4894 if (should_descend_into_inner_trees_ && inner_tree_main_ftn) {
95 if (include_delegate_nodes_for_inner_frame_trees_)
96 queue_.push_back(child);
97 queue_.push_back(inner_tree_main_ftn);
98 } else {
99 queue_.push_back(child);
100 }
Kevin McNee5f594382021-05-06 23:18:23101 }
102
103 if (should_descend_into_inner_trees_) {
Jayson Adams4db0bfe22021-07-15 19:24:07104 auto unattached_nodes =
105 current_node_->current_frame_host()
106 ->delegate()
107 ->GetUnattachedOwnedNodes(current_node_->current_frame_host());
108
109 // Reserve enough space in the queue to accommodate the nodes we're
110 // going to add.
111 queue_.reserve(queue_.size() + unattached_nodes.size());
112
113 for (auto* unattached_node : unattached_nodes) {
114 queue_.push_back(unattached_node);
Kevin McNee5f594382021-05-06 23:18:23115 }
Alex Moshchuk27caae82017-09-11 23:11:18116 }
dcheng57e39e22016-01-21 00:25:38117 }
118
Kevin McNee5f594382021-05-06 23:18:23119 AdvanceNode();
120 return *this;
121}
dcheng57e39e22016-01-21 00:25:38122
Kevin McNee5f594382021-05-06 23:18:23123FrameTree::NodeIterator& FrameTree::NodeIterator::AdvanceSkippingChildren() {
124 AdvanceNode();
dcheng57e39e22016-01-21 00:25:38125 return *this;
126}
127
128bool FrameTree::NodeIterator::operator==(const NodeIterator& rhs) const {
129 return current_node_ == rhs.current_node_;
130}
131
Kevin McNee5f594382021-05-06 23:18:23132void FrameTree::NodeIterator::AdvanceNode() {
133 if (!queue_.empty()) {
134 current_node_ = queue_.front();
Jayson Adams4db0bfe22021-07-15 19:24:07135 queue_.pop_front();
Kevin McNee5f594382021-05-06 23:18:23136 } else {
137 current_node_ = nullptr;
138 }
139}
140
141FrameTree::NodeIterator::NodeIterator(
Ali Hijazie63cbaf62023-12-20 19:29:35142 const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
143 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:23144 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48145 bool should_descend_into_inner_trees,
146 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23147 : current_node_(nullptr),
148 root_of_subtree_to_skip_(root_of_subtree_to_skip),
149 should_descend_into_inner_trees_(should_descend_into_inner_trees),
Dave Tapuskadda303d2022-10-04 16:56:48150 include_delegate_nodes_for_inner_frame_trees_(
151 include_delegate_nodes_for_inner_frame_trees),
Alan Zhaoadbc4f72024-11-08 01:59:36152 queue_(base::from_range, starting_nodes) {
Dave Tapuskadda303d2022-10-04 16:56:48153 // If `include_delegate_nodes_for_inner_frame_trees_` is true then
154 // `should_descend_into_inner_trees_` must be true.
155 DCHECK(!include_delegate_nodes_for_inner_frame_trees_ ||
156 should_descend_into_inner_trees_);
Kevin McNee5f594382021-05-06 23:18:23157 AdvanceNode();
158}
dcheng57e39e22016-01-21 00:25:38159
160FrameTree::NodeIterator FrameTree::NodeRange::begin() {
W. James MacLeance3176d2019-10-18 04:01:45161 // We shouldn't be attempting a frame tree traversal while the tree is
Kevin McNee5f594382021-05-06 23:18:23162 // being constructed or destructed.
Peter Kastingd5685942022-09-02 17:52:17163 DCHECK(base::ranges::all_of(starting_nodes_, [](FrameTreeNode* ftn) {
164 return ftn->current_frame_host();
165 }));
Kevin McNee5f594382021-05-06 23:18:23166
167 return NodeIterator(starting_nodes_, root_of_subtree_to_skip_,
Dave Tapuskadda303d2022-10-04 16:56:48168 should_descend_into_inner_trees_,
169 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38170}
171
172FrameTree::NodeIterator FrameTree::NodeRange::end() {
Dave Tapuskadda303d2022-10-04 16:56:48173 return NodeIterator({}, nullptr, should_descend_into_inner_trees_,
174 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38175}
176
Kevin McNee5f594382021-05-06 23:18:23177FrameTree::NodeRange::NodeRange(
Ali Hijazie63cbaf62023-12-20 19:29:35178 const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
179 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:23180 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48181 bool should_descend_into_inner_trees,
182 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23183 : starting_nodes_(starting_nodes),
184 root_of_subtree_to_skip_(root_of_subtree_to_skip),
Dave Tapuskadda303d2022-10-04 16:56:48185 should_descend_into_inner_trees_(should_descend_into_inner_trees),
186 include_delegate_nodes_for_inner_frame_trees_(
187 include_delegate_nodes_for_inner_frame_trees) {}
Kevin McNee5f594382021-05-06 23:18:23188
189FrameTree::NodeRange::NodeRange(const NodeRange&) = default;
190FrameTree::NodeRange::~NodeRange() = default;
dcheng57e39e22016-01-21 00:25:38191
Carlos Caballero40b0efd2021-01-26 11:55:00192FrameTree::FrameTree(
193 BrowserContext* browser_context,
Carlos Caballero03262522021-02-05 14:49:58194 Delegate* delegate,
Carlos Caballero40b0efd2021-01-26 11:55:00195 NavigationControllerDelegate* navigation_controller_delegate,
196 NavigatorDelegate* navigator_delegate,
197 RenderFrameHostDelegate* render_frame_delegate,
198 RenderViewHostDelegate* render_view_delegate,
199 RenderWidgetHostDelegate* render_widget_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59200 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26201 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44202 Type type)
Carlos Caballero03262522021-02-05 14:49:58203 : delegate_(delegate),
204 render_frame_delegate_(render_frame_delegate),
[email protected]92404c62013-12-04 16:40:46205 render_view_delegate_(render_view_delegate),
[email protected]fa944cb82013-11-15 17:51:21206 render_widget_delegate_(render_widget_delegate),
207 manager_delegate_(manager_delegate),
Jeremy Roman2d8dfe132021-07-06 20:51:26208 page_delegate_(page_delegate),
Carlos Caballero40b0efd2021-01-26 11:55:00209 navigator_(browser_context,
210 *this,
211 navigator_delegate,
212 navigation_controller_delegate),
Harkiran Bolaria16f2c48d2022-04-22 12:39:57213 type_(type),
David Bokanc3fb5fa2022-07-04 14:55:31214 load_progress_(0.0),
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
Alex Goughd836aa42024-07-15 23:56:52322 CHECK(node_iter != node_range.end(), base::NotFatalUntil::M130);
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.
Dominic Farolino6caf3032021-10-20 03:50:08412 CHECK_EQ(parent->GetProcess()->GetID(), 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>&
497 source_new_browsing_context_state) {
Peilin Wangfc543212024-03-27 16:17:21498 // Will be instantiated with the root proxy later and passed to
499 // `CreateRenderFrameProxy()` to batch create proxies for child frames.
Peilin Wang427aa6a2023-03-22 17:58:33500 std::unique_ptr<BatchedProxyIPCSender> batched_proxy_ipc_sender;
501
alexmos58729042015-06-18 23:20:00502 if (!source || !source->IsMainFrame()) {
Sharon Yangdcc5f252024-05-09 18:27:23503 RenderViewHostImpl* render_view_host =
504 GetRenderViewHost(site_instance_group).get();
arthursonzognic5be3842019-07-09 11:49:14505 if (render_view_host) {
Sharon Yangdcc5f252024-05-09 18:27:23506 root()->render_manager()->EnsureRenderViewInitialized(
507 render_view_host, site_instance_group);
arthursonzognic5be3842019-07-09 11:49:14508 } else {
Harkiran Bolaria2912a6b32022-02-22 16:43:45509 // Due to the check above, we are creating either an opener proxy (when
510 // source is null) or a main frame proxy due to a subframe navigation
511 // (when source is not a main frame). In the former case, we should use
512 // root's current BrowsingContextState, while in the latter case we should
513 // use BrowsingContextState from the main RenderFrameHost of the subframe
Dave Tapuska2cf1f532022-08-10 15:30:49514 // being navigated. We want to ensure that the `blink::WebView` is created
Sharon Yangdcc5f252024-05-09 18:27:23515 // in the right SiteInstanceGroup if it doesn't exist, before creating the
Dave Tapuska2cf1f532022-08-10 15:30:49516 // other proxies; if the `blink::WebView` doesn't exist, the only way to
517 // do this is to also create a proxy for the main frame as well.
Peilin Wang427aa6a2023-03-22 17:58:33518 const scoped_refptr<BrowsingContextState>& root_browsing_context_state =
Harkiran Bolaria2912a6b32022-02-22 16:43:45519 source ? source->parent()->GetMainFrame()->browsing_context_state()
Peilin Wang427aa6a2023-03-22 17:58:33520 : root()->current_frame_host()->browsing_context_state();
521
Alison Gale770f3fc2024-04-27 00:39:58522 // TODO(crbug.com/40248300): Batch main frame proxy creation and
Peilin Wang427aa6a2023-03-22 17:58:33523 // pass an instance of `BatchedProxyIPCSender` here instead of nullptr.
524 root()->render_manager()->CreateRenderFrameProxy(
Sharon Yangdcc5f252024-05-09 18:27:23525 site_instance_group, root_browsing_context_state,
Peilin Wang427aa6a2023-03-22 17:58:33526 /*batched_proxy_ipc_sender=*/nullptr);
527
528 // We only need to use `BatchedProxyIPCSender` when navigating to a new
Sharon Yangdcc5f252024-05-09 18:27:23529 // SiteInstanceGroup. Proxies do not need to be created when navigating to
530 // a SiteInstanceGroup that has already been encountered, because site
Peilin Wang427aa6a2023-03-22 17:58:33531 // isolation would guarantee that all nodes already have either proxies
532 // or real frames. Due to the check above, the `render_view_host` does
Sharon Yangdcc5f252024-05-09 18:27:23533 // not exist here, which means we have not seen this SiteInstanceGroup
Peilin Wang427aa6a2023-03-22 17:58:33534 // before, so we instantiate `batched_proxy_ipc_sender` to consolidate
535 // IPCs for proxy creation.
Peilin Wangfc543212024-03-27 16:17:21536 base::SafeRef<RenderFrameProxyHost> root_proxy =
537 root_browsing_context_state
Sharon Yangdcc5f252024-05-09 18:27:23538 ->GetRenderFrameProxyHost(site_instance_group)
Peilin Wangfc543212024-03-27 16:17:21539 ->GetSafeRef();
540 batched_proxy_ipc_sender =
541 std::make_unique<BatchedProxyIPCSender>(std::move(root_proxy));
[email protected]82307f6b2014-08-07 03:30:12542 }
543 }
544
Sharon Yangdcc5f252024-05-09 18:27:23545 // Check whether we're in an inner delegate and the |site_instance_group|
546 // corresponds to the outer delegate. Subframe proxies aren't needed if this
547 // is the case.
Sharon Yang88f064ca2023-03-24 16:25:18548 bool is_site_instance_group_for_outer_delegate = false;
Alex Moshchuk1226b152019-11-08 18:23:45549 RenderFrameProxyHost* outer_delegate_proxy =
550 root()->render_manager()->GetProxyToOuterDelegate();
551 if (outer_delegate_proxy) {
Sharon Yang88f064ca2023-03-24 16:25:18552 is_site_instance_group_for_outer_delegate =
Sharon Yangdcc5f252024-05-09 18:27:23553 (site_instance_group == outer_delegate_proxy->site_instance_group());
Alex Moshchuk1226b152019-11-08 18:23:45554 }
555
naskoe6edde32014-10-17 15:36:48556 // Proxies are created in the FrameTree in response to a node navigating to a
Sharon Yangdcc5f252024-05-09 18:27:23557 // new SiteInstanceGroup. Since |source|'s navigation will replace the
558 // currently loaded document, the entire subtree under |source| will be
559 // removed, and thus proxy creation is skipped for all nodes in that subtree.
Alex Moshchuk27caae82017-09-11 23:11:18560 //
Sharon Yangdcc5f252024-05-09 18:27:23561 // However, a proxy *is* needed for the |source| node if it is
562 // cross-SiteInstanceGroup from the current node. This lets cross-process
563 // navigations in |source| start with a proxy and follow a remote-to-local
564 // transition, which avoids race conditions in cases where other navigations
565 // need to reference |source| before it commits. See https://crbug.com/756790
566 // for more background. Therefore, NodesExceptSubtree(source) will include
567 // |source| in the nodes traversed (see NodeIterator::operator++).
Alex Moshchuk27caae82017-09-11 23:11:18568 for (FrameTreeNode* node : NodesExceptSubtree(source)) {
Sharon Yangdcc5f252024-05-09 18:27:23569 // If a new frame is created in the current SiteInstanceGroup, other frames
570 // in that SiteInstanceGroup don't need a proxy for the new frame.
Alex Moshchuk27caae82017-09-11 23:11:18571 RenderFrameHostImpl* current_host =
572 node->render_manager()->current_frame_host();
Sharon Yangdcc5f252024-05-09 18:27:23573 SiteInstanceGroup* current_group = current_host->GetSiteInstance()->group();
574
575 // Check that the proxy is for a different SiteInstanceGroup. This ensures
576 // that a navigation within a SiteInstanceGroup does not cause proxies to be
577 // created. That then allows the Blink side to do a local to local frame
578 // transition within the same process.
579 if (current_group != site_instance_group) {
Alex Moshchuk27caae82017-09-11 23:11:18580 if (node == source && !current_host->IsRenderFrameLive()) {
Fergal Daly6de62f52020-10-14 01:56:44581 // We don't create a proxy at |source| when the current RenderFrameHost
582 // isn't live. This is because either (1) the speculative
Alex Moshchuk27caae82017-09-11 23:11:18583 // RenderFrameHost will be committed immediately, and the proxy
Fergal Daly6de62f52020-10-14 01:56:44584 // destroyed right away, in GetFrameHostForNavigation, which makes the
585 // races above impossible, or (2) the early commit will be skipped due
586 // to ShouldSkipEarlyCommitPendingForCrashedFrame, in which case the
587 // proxy for |source| *is* needed, but it will be created later in
588 // CreateProxiesForNewRenderFrameHost.
589 //
590 // TODO(fergal): Consider creating a proxy for |source| here rather than
591 // in CreateProxiesForNewRenderFrameHost for case (2) above.
Alex Moshchuk27caae82017-09-11 23:11:18592 continue;
593 }
Alex Moshchuk1226b152019-11-08 18:23:45594
595 // Do not create proxies for subframes in the outer delegate's
Sharon Yang88f064ca2023-03-24 16:25:18596 // SiteInstanceGroup, since there is no need to expose these subframes to
597 // the outer delegate. See also comments in CreateProxiesForChildFrame()
598 // and https://crbug.com/1013553.
599 if (!node->IsMainFrame() && is_site_instance_group_for_outer_delegate) {
Alex Moshchuk1226b152019-11-08 18:23:45600 continue;
Sharon Yang88f064ca2023-03-24 16:25:18601 }
Alex Moshchuk1226b152019-11-08 18:23:45602
Harkiran Bolaria2912a6b32022-02-22 16:43:45603 // If |node| is the FrameTreeNode being navigated, we use
604 // |browsing_context_state| (as BrowsingContextState might change for
605 // cross-BrowsingInstance navigations). Otherwise, we should use the
606 // |node|'s current BrowsingContextState.
607 node->render_manager()->CreateRenderFrameProxy(
Sharon Yangdcc5f252024-05-09 18:27:23608 site_instance_group,
Peilin Wang427aa6a2023-03-22 17:58:33609 node == source ? source_new_browsing_context_state
610 : node->current_frame_host()->browsing_context_state(),
611 batched_proxy_ipc_sender.get());
Alex Moshchuk27caae82017-09-11 23:11:18612 }
dcheng57e39e22016-01-21 00:25:38613 }
Peilin Wang427aa6a2023-03-22 17:58:33614
615 if (batched_proxy_ipc_sender) {
616 batched_proxy_ipc_sender->CreateAllProxies();
617 }
[email protected]82307f6b2014-08-07 03:30:12618}
619
[email protected]9b159a52013-10-03 17:24:55620RenderFrameHostImpl* FrameTree::GetMainFrame() const {
Paul Semel3e241042022-10-11 12:57:31621 return root_.current_frame_host();
[email protected]9b159a52013-10-03 17:24:55622}
623
[email protected]9c9343b2014-03-08 02:56:07624FrameTreeNode* FrameTree::GetFocusedFrame() {
625 return FindByID(focused_frame_tree_node_id_);
626}
627
Sharon Yangefe52632022-03-08 23:06:06628void FrameTree::SetFocusedFrame(FrameTreeNode* node,
629 SiteInstanceGroup* source) {
Rakina Zata Amnicca4889e2021-09-28 23:25:55630 CHECK(node->current_frame_host()->IsActive());
alexmos5357efb2015-12-16 21:44:00631 if (node == GetFocusedFrame())
632 return;
633
Sharon Yangefe52632022-03-08 23:06:06634 std::set<SiteInstanceGroup*> frame_tree_groups =
635 CollectSiteInstanceGroups(this);
alexmosb1dc2162015-11-05 00:59:20636
Sharon Yangefe52632022-03-08 23:06:06637 SiteInstanceGroup* current_group =
638 node->current_frame_host()->GetSiteInstance()->group();
alexmos5357efb2015-12-16 21:44:00639
Sharon Yang7424bda2021-11-04 20:27:43640 // Update the focused frame in all other SiteInstanceGroups. If focus changes
641 // to a cross-group frame, this allows the old focused frame's renderer
alexmosb1dc2162015-11-05 00:59:20642 // process to clear focus from that frame and fire blur events. It also
643 // ensures that the latest focused frame is available in all renderers to
644 // compute document.activeElement.
alexmos5357efb2015-12-16 21:44:00645 //
Sharon Yangefe52632022-03-08 23:06:06646 // We do not notify the |source| SiteInstanceGroup because it already knows
647 // the new focused frame (since it initiated the focus change), and we notify
648 // the new focused frame's SiteInstanceGroup (if it differs from |source|)
649 // separately below.
650 for (auto* group : frame_tree_groups) {
651 if (group != source && group != current_group) {
652 RenderFrameProxyHost* proxy = node->current_frame_host()
653 ->browsing_context_state()
654 ->GetRenderFrameProxyHost(group);
Sharon Yangf92842a2022-03-01 18:21:47655
Rakina Zata Amni80700402021-09-20 17:18:03656 if (proxy) {
657 proxy->SetFocusedFrame();
658 } else {
Sharon Yangf92842a2022-03-01 18:21:47659 base::debug::DumpWithoutCrashing();
Rakina Zata Amni80700402021-09-20 17:18:03660 }
alexmosb1dc2162015-11-05 00:59:20661 }
alexmosca2c6ba2015-10-01 21:52:25662 }
663
Sharon Yangefe52632022-03-08 23:06:06664 // If |node| was focused from a cross-group frame (i.e., via
alexmos5357efb2015-12-16 21:44:00665 // window.focus()), tell its RenderFrame that it should focus.
Sharon Yangefe52632022-03-08 23:06:06666 if (current_group != source)
alexmos5357efb2015-12-16 21:44:00667 node->current_frame_host()->SetFocusedFrame();
668
[email protected]9c9343b2014-03-08 02:56:07669 focused_frame_tree_node_id_ = node->frame_tree_node_id();
alexmos21acae52015-11-07 01:04:43670 node->DidFocus();
dmazzonif27bf892016-03-10 15:51:55671
672 // The accessibility tree data for the root of the frame tree keeps
673 // track of the focused frame too, so update that every time the
674 // focused frame changes.
Kevin McNee582a7d62021-10-12 21:42:22675 root()
676 ->current_frame_host()
677 ->GetOutermostMainFrameOrEmbedder()
678 ->UpdateAXTreeData();
[email protected]9c9343b2014-03-08 02:56:07679}
680
arthursonzognic5be3842019-07-09 11:49:14681scoped_refptr<RenderViewHostImpl> FrameTree::CreateRenderViewHost(
Sharon Yangeb76ee22023-11-29 00:42:09682 SiteInstanceGroup* site_instance_group,
avib7348942015-12-25 20:57:10683 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58684 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57685 scoped_refptr<BrowsingContextState> main_browsing_context_state,
Khushal Sagar402d1c9f2023-11-15 22:21:48686 CreateRenderViewHostCase create_case,
Arthur Sonzognic686e8f2024-01-11 08:36:37687 std::optional<viz::FrameSinkId> frame_sink_id) {
Harkiran Bolaria57e2b062022-03-14 10:27:58688 if (main_browsing_context_state) {
689 DCHECK(main_browsing_context_state->is_main_frame());
690 }
dcheng29f5a6c2015-08-31 21:43:27691 RenderViewHostImpl* rvh =
692 static_cast<RenderViewHostImpl*>(RenderViewHostFactory::Create(
Sharon Yangeb76ee22023-11-29 00:42:09693 this, site_instance_group,
694 site_instance_group->GetStoragePartitionConfig(),
695 render_view_delegate_, render_widget_delegate_, main_frame_routing_id,
Sharon Yang7ce309e2023-01-19 21:39:57696 renderer_initiated_creation, std::move(main_browsing_context_state),
Khushal Sagar402d1c9f2023-11-15 22:21:48697 create_case, frame_sink_id));
Sharon Yang7ce309e2023-01-19 21:39:57698
Rakina Zata Amnidf9988fa12024-07-30 02:00:50699 if (create_case == CreateRenderViewHostCase::kSpeculative) {
Sharon Yang7ce309e2023-01-19 21:39:57700 set_speculative_render_view_host(rvh->GetWeakPtr());
701 } else {
702 // Register non-speculative RenderViewHosts. If they are speculative, they
703 // will be registered when they become active.
704 RegisterRenderViewHost(rvh->rvh_map_id(), rvh);
705 }
706
arthursonzognic5be3842019-07-09 11:49:14707 return base::WrapRefCounted(rvh);
[email protected]94d0cc12013-12-18 00:07:41708}
709
arthursonzognic5be3842019-07-09 11:49:14710scoped_refptr<RenderViewHostImpl> FrameTree::GetRenderViewHost(
Sharon Yang57bde122022-03-01 20:01:12711 SiteInstanceGroup* group) {
Sharon Yanga2fe85e2022-02-09 21:38:29712 // When called from RenderFrameHostManager::CreateRenderFrameHost, it's
713 // possible that a RenderProcessHost hasn't yet been created, which means
Sharon Yang57bde122022-03-01 20:01:12714 // a SiteInstanceGroup won't have been created yet.
Sharon Yanga2fe85e2022-02-09 21:38:29715 if (!group)
716 return nullptr;
717
718 auto it = render_view_host_map_.find(GetRenderViewHostMapId(group));
arthursonzogni88e54ae2019-04-15 12:57:36719 if (it == render_view_host_map_.end())
720 return nullptr;
alexmos9aa61232016-04-26 21:54:02721
arthursonzognic5be3842019-07-09 11:49:14722 return base::WrapRefCounted(it->second);
[email protected]94d0cc12013-12-18 00:07:41723}
724
Aaron Colwell78b4bde2021-03-16 16:16:09725FrameTree::RenderViewHostMapId FrameTree::GetRenderViewHostMapId(
Sharon Yangc581a0c2021-11-02 18:09:39726 SiteInstanceGroup* site_instance_group) const {
727 return RenderViewHostMapId::FromUnsafeValue(
728 site_instance_group->GetId().value());
Lowell Manners75055a132019-10-11 10:30:29729}
730
Aaron Colwell78b4bde2021-03-16 16:16:09731void FrameTree::RegisterRenderViewHost(RenderViewHostMapId id,
732 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47733 TRACE_EVENT_INSTANT("navigation", "FrameTree::RegisterRenderViewHost",
734 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57735 CHECK(!rvh->is_speculative());
Rakina Zata Amni89b8aa32024-09-20 03:36:54736 bool rvh_id_already_in_map = base::Contains(render_view_host_map_, id);
737 bool rfh_in_bfcache =
738 controller()
739 .GetBackForwardCache()
740 .IsRenderFrameHostWithSIGInBackForwardCacheForDebugging(
741 rvh->site_instance_group()->GetId());
742 bool rfph_in_bfcache =
743 controller()
744 .GetBackForwardCache()
745 .IsRenderFrameProxyHostWithSIGInBackForwardCacheForDebugging(
746 rvh->site_instance_group()->GetId());
747 bool rvh_in_bfcache =
748 controller()
749 .GetBackForwardCache()
750 .IsRenderViewHostWithMapIdInBackForwardCacheForDebugging(*rvh);
751 // We're seeing cases where an RVH being restored from BFCache has the same
752 // ID as an RVH already in the map, where the 2 RVHs are different but one
753 // was in BFCache and one isn't.
754 // To investigate, detect if any of these cases happen:
755 // 1) A RenderViewHost with the same ID as `rvh` is already in the map
756 // 2) A RenderFrameHost with the same SIG ID as `rvh` is in BFCache
757 // 3) A RenderFrameProxyHost with the same SIG ID as `rvh` is in BFCache
758 // 4) A RenderViewHost with the same ID as `rvh` is in BFCache
759 // These cases shouldn't be possible. Note that when checking #2-#4 for
760 // a RenderViewHost that is getting out of BFCache, we are guaranteed to not
761 // accidentally match to the RVH/RFPH/RFH of the page being restored,
762 // because we can only get here after the StoredPage is taken out of the
763 // BFCache and thus won't be iterated over in the functions above.
764 // See the linked bug below for more details.
765 if (rvh_id_already_in_map || rfh_in_bfcache || rfph_in_bfcache ||
766 rvh_in_bfcache) {
Rakina Zata Amni9b7ea7052024-09-04 20:40:58767 // TODO(https://crbug.com/354382462): Remove crash keys once investigation
768 // is done.
Rakina Zata Amni89b8aa32024-09-20 03:36:54769 SCOPED_CRASH_KEY_BOOL("rvh-double", "in_map", rvh_id_already_in_map);
770 SCOPED_CRASH_KEY_BOOL("rvh-double", "rfh_in_bfcache", rfh_in_bfcache);
771 SCOPED_CRASH_KEY_BOOL("rvh-double", "rfph_in_bfcache", rfph_in_bfcache);
772 SCOPED_CRASH_KEY_BOOL("rvh-double", "rvh_in_bfcache", rvh_in_bfcache);
773 SCOPED_CRASH_KEY_BOOL("rvh-double", "passed_renderer_created",
Rakina Zata Amnid3006032024-09-12 00:53:53774 rvh->renderer_view_created());
Rakina Zata Amnid3006032024-09-12 00:53:53775 SCOPED_CRASH_KEY_NUMBER("rvh-double", "passed_rvh_main_id",
776 rvh->main_frame_routing_id());
777 SCOPED_CRASH_KEY_NUMBER("rvh-double", "root_routing_id",
778 root()->current_frame_host()->GetRoutingID());
Rakina Zata Amnid3006032024-09-12 00:53:53779 SCOPED_CRASH_KEY_NUMBER("rvh-double", "passed_rvh_ptr",
780 reinterpret_cast<size_t>(rvh));
Rakina Zata Amni89b8aa32024-09-20 03:36:54781 SCOPED_CRASH_KEY_BOOL("rvh-double", "passed_rvh_bfcache",
782 rvh->is_in_back_forward_cache());
783 SCOPED_CRASH_KEY_BOOL("rvh-double", "frame_tree_primary", is_primary());
784
785 if (rvh_id_already_in_map) {
786 SCOPED_CRASH_KEY_BOOL(
787 "rvh-double", "mapped_rvh_registered",
788 render_view_host_map_[id]->is_registered_with_frame_tree());
789 SCOPED_CRASH_KEY_NUMBER(
790 "rvh-double", "mapped_rvh_main_id",
791 render_view_host_map_[id]->main_frame_routing_id());
792 SCOPED_CRASH_KEY_NUMBER(
793 "rvh-double", "map_rvh_ptr",
794 reinterpret_cast<size_t>(render_view_host_map_[id]));
795 SCOPED_CRASH_KEY_BOOL(
796 "rvh-double", "map_rvh_bfcache",
797 render_view_host_map_[id]->is_in_back_forward_cache());
798 SCOPED_CRASH_KEY_BOOL("rvh-double", "mapped_renderer_created",
799 render_view_host_map_[id]->renderer_view_created());
800 CHECK_EQ(rvh, render_view_host_map_[id]);
801 }
Rakina Zata Amni9b7ea7052024-09-04 20:40:58802 }
Aaron Colwell78b4bde2021-03-16 16:16:09803 render_view_host_map_[id] = rvh;
Sharon Yang7ce309e2023-01-19 21:39:57804 rvh->set_is_registered_with_frame_tree(true);
Aaron Colwell78b4bde2021-03-16 16:16:09805}
806
807void FrameTree::UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13808 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47809 TRACE_EVENT_INSTANT("navigation", "FrameTree::UnregisterRenderViewHost",
810 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57811 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09812 auto it = render_view_host_map_.find(id);
arthursonzogni88e54ae2019-04-15 12:57:36813 CHECK(it != render_view_host_map_.end());
arthursonzognic5be3842019-07-09 11:49:14814 CHECK_EQ(it->second, rvh);
815 render_view_host_map_.erase(it);
Sharon Yang7ce309e2023-01-19 21:39:57816 rvh->set_is_registered_with_frame_tree(false);
[email protected]94d0cc12013-12-18 00:07:41817}
818
arthursonzogni0a2007792019-05-28 21:32:48819void FrameTree::FrameUnloading(FrameTreeNode* frame) {
820 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
Avi Drissmanbd153642024-09-03 18:58:05821 focused_frame_tree_node_id_ = FrameTreeNodeId();
arthursonzogni0a2007792019-05-28 21:32:48822
823 // Ensure frames that are about to be deleted aren't visible from the other
824 // processes anymore.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40825 frame->GetBrowsingContextStateForSubframe()->ResetProxyHosts();
arthursonzogni0a2007792019-05-28 21:32:48826}
827
dmazzonie950ea232015-03-13 21:39:45828void FrameTree::FrameRemoved(FrameTreeNode* frame) {
nick53d5cbf2015-04-23 22:50:14829 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
Avi Drissmanbd153642024-09-03 18:58:05830 focused_frame_tree_node_id_ = FrameTreeNodeId();
dmazzonie950ea232015-03-13 21:39:45831}
832
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15833double FrameTree::GetLoadProgress() {
Paul Semel3e241042022-10-11 12:57:31834 if (root_.HasNavigation())
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15835 return blink::kInitialLoadProgress;
836
Paul Semel3e241042022-10-11 12:57:31837 return root_.current_frame_host()->GetPage().load_progress();
fdegans1d16355162015-03-26 11:58:34838}
839
Yu Gaoc8c18552022-06-22 14:38:45840bool FrameTree::IsLoadingIncludingInnerFrameTrees() const {
Nate Chapin470dbc62023-04-25 16:34:38841 return GetLoadingState() != LoadingState::NONE;
842}
843
844LoadingState FrameTree::GetLoadingState() const {
845 // The overall loading state for the FrameTree matches the root node's loading
846 // state if the root is loading.
847 if (root_.GetLoadingState() != LoadingState::NONE) {
848 return root_.GetLoadingState();
dcheng57e39e22016-01-21 00:25:38849 }
Nate Chapin470dbc62023-04-25 16:34:38850
851 // Otherwise, check if a subframe is loading without an associated navigation
852 // in the root frame. If so, we are loading, but we don't want to show
853 // loading UI.
854 for (const FrameTreeNode* node_to_check :
855 const_cast<FrameTree*>(this)->CollectNodesForIsLoading()) {
856 if (node_to_check->IsLoading()) {
857 return LoadingState::LOADING_WITHOUT_UI;
858 }
859 }
860 return LoadingState::NONE;
fdegans1d16355162015-03-26 11:58:34861}
862
alexmos3fcd0ca2015-10-23 18:18:33863void FrameTree::ReplicatePageFocus(bool is_focused) {
Dave Tapuska53e6ce12021-08-06 16:08:24864 // Focus loss may occur while this FrameTree is being destroyed. Don't
865 // send the message in this case, as the main frame's RenderFrameHost and
866 // other state has already been cleared.
867 if (is_being_destroyed_)
868 return;
Sharon Yangefe52632022-03-08 23:06:06869 std::set<SiteInstanceGroup*> frame_tree_site_instance_groups =
870 CollectSiteInstanceGroups(this);
alexmos3fcd0ca2015-10-23 18:18:33871
Sharon Yangefe52632022-03-08 23:06:06872 // Send the focus update to main frame's proxies in all SiteInstanceGroups of
alexmos3fcd0ca2015-10-23 18:18:33873 // other frames in this FrameTree. Note that the main frame might also know
Sharon Yangefe52632022-03-08 23:06:06874 // about proxies in SiteInstanceGroups for frames in a different FrameTree
875 // (e.g., for window.open), so we can't just iterate over its proxy_hosts_ in
alexmos3fcd0ca2015-10-23 18:18:33876 // RenderFrameHostManager.
Sharon Yangefe52632022-03-08 23:06:06877 for (auto* group : frame_tree_site_instance_groups)
878 SetPageFocus(group, is_focused);
alexmos0d7e0b09b2015-10-29 22:11:48879}
alexmos3fcd0ca2015-10-23 18:18:33880
Sharon Yangefe52632022-03-08 23:06:06881void FrameTree::SetPageFocus(SiteInstanceGroup* group, bool is_focused) {
Paul Semel3e241042022-10-11 12:57:31882 RenderFrameHostManager* root_manager = root_.render_manager();
alexmos0d7e0b09b2015-10-29 22:11:48883
884 // This is only used to set page-level focus in cross-process subframes, and
Sharon Yangefe52632022-03-08 23:06:06885 // requests to set focus in main frame's SiteInstanceGroup are ignored.
886 if (group != root_manager->current_frame_host()->GetSiteInstance()->group()) {
887 RenderFrameProxyHost* proxy = root_manager->current_frame_host()
888 ->browsing_context_state()
889 ->GetRenderFrameProxyHost(group);
Dave Tapuska82b54012022-07-15 23:26:10890 if (proxy->is_render_frame_proxy_live())
891 proxy->GetAssociatedRemoteFrame()->SetPageFocus(is_focused);
alexmos3fcd0ca2015-10-23 18:18:33892 }
893}
894
W. James MacLeanc07dc41b2022-07-25 18:52:16895void FrameTree::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanb70fab82020-05-01 18:51:14896 const url::Origin& previously_visited_origin,
897 NavigationRequest* navigation_request_to_exclude) {
W. James MacLeanc07dc41b2022-07-25 18:52:16898 controller().RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanc6dc86c2021-08-12 13:58:37899 previously_visited_origin);
900
Charlie Reis37be2682023-01-10 17:04:47901 std::unordered_set<SiteInstanceImpl*> matching_site_instances;
W. James MacLeanb70fab82020-05-01 18:51:14902
903 // Be sure to visit all RenderFrameHosts associated with this frame that might
904 // have an origin that could script other frames. We skip RenderFrameHosts
905 // that are in the bfcache, assuming there's no way for a frame to join the
906 // BrowsingInstance of a bfcache RFH while it's in the cache.
907 for (auto* frame_tree_node : SubtreeNodes(root())) {
908 auto* frame_host = frame_tree_node->current_frame_host();
W. James MacLeanb70fab82020-05-01 18:51:14909 if (previously_visited_origin == frame_host->GetLastCommittedOrigin())
910 matching_site_instances.insert(frame_host->GetSiteInstance());
911
912 if (frame_host->HasCommittingNavigationRequestForOrigin(
913 previously_visited_origin, navigation_request_to_exclude)) {
914 matching_site_instances.insert(frame_host->GetSiteInstance());
915 }
916
917 auto* spec_frame_host =
918 frame_tree_node->render_manager()->speculative_frame_host();
919 if (spec_frame_host &&
920 spec_frame_host->HasCommittingNavigationRequestForOrigin(
921 previously_visited_origin, navigation_request_to_exclude)) {
922 matching_site_instances.insert(spec_frame_host->GetSiteInstance());
923 }
924
925 auto* navigation_request = frame_tree_node->navigation_request();
926 if (navigation_request &&
927 navigation_request != navigation_request_to_exclude &&
928 navigation_request->HasCommittingOrigin(previously_visited_origin)) {
929 matching_site_instances.insert(frame_host->GetSiteInstance());
930 }
931 }
932
933 // Update any SiteInstances found to contain |origin|.
934 for (auto* site_instance : matching_site_instances) {
Charlie Reis37be2682023-01-10 17:04:47935 site_instance->RegisterAsDefaultOriginIsolation(previously_visited_origin);
W. James MacLeanb70fab82020-05-01 18:51:14936 }
937}
938
Charlie Reis37be2682023-01-10 17:04:47939void FrameTree::Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00940 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17941 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46942 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44943 const blink::FramePolicy& frame_policy,
944 const base::UnguessableToken& devtools_frame_token) {
Carlos Caballero40b0efd2021-01-26 11:55:00945 // blink::FrameTree::SetName always keeps |unique_name| empty in case of a
946 // main frame - let's do the same thing here.
947 std::string unique_name;
Paul Semel3e241042022-10-11 12:57:31948 root_.render_manager()->InitRoot(main_frame_site_instance,
949 renderer_initiated_creation, frame_policy,
Danil Somsikov259aa65f2022-11-11 20:49:44950 main_frame_name, devtools_frame_token);
Paul Semel3e241042022-10-11 12:57:31951 root_.SetFencedFramePropertiesIfNeeded();
Rakina Zata Amniafd3c6582021-11-30 06:19:17952
sbingler1f758842023-02-22 00:23:32953 // The initial empty document should inherit the origin (the origin may
954 // change after the first commit) and other state (such as the
955 // RuntimeFeatureStateReadContext) from its opener, except when they are in
Rakina Zata Amni4eb716e2022-04-05 21:32:46956 // different browsing context groups (`renderer_initiated_creation` will be
sbingler1f758842023-02-22 00:23:32957 // false), where it should use a new opaque origin and default values for the
958 // other state, respectively.
Rakina Zata Amniafd3c6582021-11-30 06:19:17959 // See also https://crbug.com/932067.
960 //
961 // Note that the origin of the new frame might depend on sandbox flags.
962 // Checking sandbox flags of the new frame should be safe at this point,
963 // because the flags should be already inherited when creating the root node.
Rakina Zata Amni4eb716e2022-04-05 21:32:46964 DCHECK(!renderer_initiated_creation || opener_for_origin);
Paul Semel3e241042022-10-11 12:57:31965 root_.current_frame_host()->SetOriginDependentStateOfNewFrame(
sbingler1f758842023-02-22 00:23:32966 renderer_initiated_creation ? opener_for_origin : nullptr);
Rakina Zata Amni2322f4f82022-01-24 13:24:24967
Rakina Zata Amni46087a12022-11-11 08:28:38968 controller().CreateInitialEntry();
Carlos Caballero40b0efd2021-01-26 11:55:00969}
970
Carlos Caballeroede6f8c2021-01-28 11:01:50971void FrameTree::DidAccessInitialMainDocument() {
972 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidAccessInitialDocument");
973 has_accessed_initial_main_document_ = true;
974 controller().DidAccessInitialMainDocument();
975}
976
Nate Chapin470dbc62023-04-25 16:34:38977void FrameTree::NodeLoadingStateChanged(
978 FrameTreeNode& node,
979 LoadingState previous_frame_tree_loading_state) {
980 LoadingState new_frame_tree_loading_state = GetLoadingState();
981 if (previous_frame_tree_loading_state == new_frame_tree_loading_state) {
Carlos Caballero03262522021-02-05 14:49:58982 return;
Nate Chapin470dbc62023-04-25 16:34:38983 }
Carlos Caballero03262522021-02-05 14:49:58984
Nate Chapin470dbc62023-04-25 16:34:38985 root()->render_manager()->SetIsLoading(new_frame_tree_loading_state !=
986 LoadingState::NONE);
987 delegate_->LoadingStateChanged(new_frame_tree_loading_state);
988 if (previous_frame_tree_loading_state == LoadingState::NONE) {
989 delegate_->DidStartLoading(&node);
990 } else if (new_frame_tree_loading_state == LoadingState::NONE) {
991 delegate_->DidStopLoading();
992 }
Carlos Caballero03262522021-02-05 14:49:58993}
994
Carlos Caballero03262522021-02-05 14:49:58995void FrameTree::DidCancelLoading() {
996 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidCancelLoading");
997 navigator_.controller().DiscardNonCommittedEntries();
Carlos Caballero03262522021-02-05 14:49:58998}
999
1000void FrameTree::StopLoading() {
1001 for (FrameTreeNode* node : Nodes())
1002 node->StopLoading();
1003}
1004
Carlos Caballero101ac26b2021-03-24 11:54:051005void FrameTree::Shutdown() {
Dave Tapuskae88286de2021-08-05 19:10:421006 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:051007#if DCHECK_IS_ON()
1008 DCHECK(!was_shut_down_);
1009 was_shut_down_ = true;
1010#endif
1011
Paul Semel3e241042022-10-11 12:57:311012 RenderFrameHostManager* root_manager = root_.render_manager();
Carlos Caballero101ac26b2021-03-24 11:54:051013
Carlos Caballerodb7a8f6e2021-04-09 18:17:471014 if (!root_manager->current_frame_host()) {
1015 // The page has been transferred out during an activation. There is little
1016 // left to do.
Alison Gale770f3fc2024-04-27 00:39:581017 // TODO(crbug.com/40177949): If we decide that pending delete RFHs
Carlos Caballerodb7a8f6e2021-04-09 18:17:471018 // need to be moved along during activation replace this line with a DCHECK
1019 // that there are no pending delete instances.
1020 root_manager->ClearRFHsPendingShutdown();
Paul Semel3e241042022-10-11 12:57:311021 DCHECK(!root_.navigation_request());
Carlos Caballerodb7a8f6e2021-04-09 18:17:471022 DCHECK(!root_manager->speculative_frame_host());
Paul Semel3e241042022-10-11 12:57:311023 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:051024 return;
Carlos Caballerodb7a8f6e2021-04-09 18:17:471025 }
Carlos Caballero101ac26b2021-03-24 11:54:051026
1027 for (FrameTreeNode* node : Nodes()) {
1028 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
1029 // to be shutdown and be deleted as well.
1030 node->render_manager()->ClearRFHsPendingShutdown();
Alison Gale47d1537d2024-04-19 21:31:461031 // TODO(crbug.com/40177939): Ban WebUI instance in Prerender pages.
Carlos Caballero101ac26b2021-03-24 11:54:051032 node->render_manager()->ClearWebUIInstances();
1033 }
1034
1035 // Destroy all subframes now. This notifies observers.
1036 root_manager->current_frame_host()->ResetChildren();
Harkiran Bolaria0b3bdef02022-03-10 13:04:401037 root_manager->current_frame_host()
1038 ->browsing_context_state()
1039 ->ResetProxyHosts();
Carlos Caballero101ac26b2021-03-24 11:54:051040
Carlos Caballero101ac26b2021-03-24 11:54:051041 // Manually call the observer methods for the root FrameTreeNode. It is
1042 // necessary to manually delete all objects tracking navigations
1043 // (NavigationHandle, NavigationRequest) for observers to be properly
1044 // notified of these navigations stopping before the WebContents is
1045 // destroyed.
1046
1047 root_manager->current_frame_host()->RenderFrameDeleted();
Rakina Zata Amnif8f2bb62022-11-23 05:54:321048 root_manager->current_frame_host()->ResetOwnedNavigationRequests(
1049 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:051050
1051 // Do not update state as the FrameTree::Delegate (possibly a WebContents) is
1052 // being destroyed.
Rakina Zata Amni58681c62024-06-25 06:32:131053 root_.ResetNavigationRequestButKeepState(
1054 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:051055 if (root_manager->speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:451056 root_manager->DiscardSpeculativeRenderFrameHostForShutdown();
Carlos Caballero101ac26b2021-03-24 11:54:051057 }
1058
Alexander Timin8aeee642021-05-12 08:39:331059 // NavigationRequests restoring the page from bfcache have a reference to the
1060 // RFHs stored in the cache, so the cache should be cleared after the
1061 // navigation request is reset.
1062 controller().GetBackForwardCache().Shutdown();
1063
Paul Semel3e241042022-10-11 12:57:311064 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:051065 render_view_delegate_->RenderViewDeleted(
1066 root_manager->current_frame_host()->render_view_host());
1067}
1068
Dave Tapuskad8b0530f2021-10-19 15:12:311069base::SafeRef<FrameTree> FrameTree::GetSafeRef() {
1070 return weak_ptr_factory_.GetSafeRef();
1071}
1072
Dave Tapuska54c76a032021-10-27 22:10:421073void FrameTree::FocusOuterFrameTrees() {
1074 OPTIONAL_TRACE_EVENT0("content", "FrameTree::FocusOuterFrameTrees");
1075
1076 FrameTree* frame_tree_to_focus = this;
1077 while (true) {
1078 FrameTreeNode* outer_node = FrameTreeNode::GloballyFindByID(
1079 frame_tree_to_focus->delegate()->GetOuterDelegateFrameTreeNodeId());
1080 if (!outer_node || !outer_node->current_frame_host()->IsActive()) {
1081 // Don't set focus on an inactive FrameTreeNode.
1082 return;
1083 }
Arthur Sonzognif6785ec2022-12-05 10:11:501084 outer_node->frame_tree().SetFocusedFrame(outer_node, nullptr);
Dave Tapuska54c76a032021-10-27 22:10:421085
1086 // For a browser initiated focus change, let embedding renderer know of the
1087 // change. Otherwise, if the currently focused element is just across a
1088 // process boundary in focus order, it will not be possible to move across
1089 // that boundary. This is because the target element will already be focused
1090 // (that renderer was not notified) and drop the event.
1091 if (auto* proxy_to_outer_delegate = frame_tree_to_focus->root()
1092 ->render_manager()
1093 ->GetProxyToOuterDelegate()) {
1094 proxy_to_outer_delegate->SetFocusedFrame();
1095 }
Arthur Sonzognif6785ec2022-12-05 10:11:501096 frame_tree_to_focus = &outer_node->frame_tree();
Dave Tapuska54c76a032021-10-27 22:10:421097 }
1098}
1099
Thomas Lukaszewicz8ada31b2024-09-01 19:18:161100void FrameTree::Discard() {
Thomas Lukaszewicz1b672fe2024-09-17 08:35:031101 // A speculative pending-commit rfh should not be cancelled or deleted. In
1102 // this case ignore the discard request and allow the navigation to complete
1103 // as normal.
1104 if (const auto* speculative_rfh =
1105 root()->render_manager()->speculative_frame_host();
1106 speculative_rfh && speculative_rfh->HasPendingCommitNavigation()) {
1107 return;
1108 }
1109
Thomas Lukaszewicz8ada31b2024-09-01 19:18:161110 root()->set_was_discarded();
1111 root()->current_frame_host()->DiscardFrame();
1112 NavigationControllerImpl& navigation_controller = controller();
1113 navigation_controller.SetNeedsReload();
1114 navigation_controller.GetBackForwardCache().Flush();
1115}
1116
[email protected]9b159a52013-10-03 17:24:551117} // namespace content