blob: fc4785fcff9b50297ad0836a4f99dfb62f54f02e [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"
Peter Kastingd5685942022-09-02 17:52:1719#include "base/ranges/algorithm.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5020#include "base/trace_event/optional_trace_event.h"
Rakina Zata Amni4b1968d2021-09-09 03:29:4721#include "base/trace_event/typed_macros.h"
Pavel Feldman25234722017-10-11 02:49:0622#include "base/unguessable_token.h"
danakjc492bf82020-09-09 20:02:4423#include "content/browser/renderer_host/navigation_controller_impl.h"
24#include "content/browser/renderer_host/navigation_entry_impl.h"
25#include "content/browser/renderer_host/navigation_request.h"
26#include "content/browser/renderer_host/navigator.h"
27#include "content/browser/renderer_host/navigator_delegate.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1528#include "content/browser/renderer_host/page_impl.h"
Kevin McNee5f594382021-05-06 23:18:2329#include "content/browser/renderer_host/render_frame_host_delegate.h"
danakjc492bf82020-09-09 20:02:4430#include "content/browser/renderer_host/render_frame_host_factory.h"
31#include "content/browser/renderer_host/render_frame_host_impl.h"
32#include "content/browser/renderer_host/render_frame_proxy_host.h"
Carlos Caballero101ac26b2021-03-24 11:54:0533#include "content/browser/renderer_host/render_view_host_delegate.h"
[email protected]94d0cc12013-12-18 00:07:4134#include "content/browser/renderer_host/render_view_host_factory.h"
35#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yang7ce309e2023-01-19 21:39:5736#include "content/common/content_navigation_policy.h"
japhet98e9bd82016-06-28 23:48:4537#include "content/common/content_switches_internal.h"
Sreeja Kamishetty46f762c2021-02-05 07:52:4638#include "third_party/blink/public/common/features.h"
Kevin McNee43fe8292021-10-04 22:59:4139#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Blink Reformata30d4232018-04-07 15:31:0640#include "third_party/blink/public/common/frame/frame_policy.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1541#include "third_party/blink/public/common/loader/loader_constants.h"
Ari Chivukula5d15efb2023-01-21 04:33:5242#include "third_party/blink/public/common/storage_key/storage_key.h"
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:5443#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
[email protected]9b159a52013-10-03 17:24:5544
45namespace content {
46
47namespace {
[email protected]20edca72014-08-14 10:27:5348
Rakina Zata Amni4b1968d2021-09-09 03:29:4749using perfetto::protos::pbzero::ChromeTrackEvent;
50
Sharon Yangefe52632022-03-08 23:06:0651// Helper function to collect SiteInstanceGroups involved in rendering a single
52// FrameTree (which is a subset of SiteInstanceGroups in main frame's
53// proxy_hosts_ because of openers).
54std::set<SiteInstanceGroup*> CollectSiteInstanceGroups(FrameTree* tree) {
55 std::set<SiteInstanceGroup*> groups;
Adithya Srinivasan0e9808342022-12-13 18:25:0756 for (FrameTreeNode* node : tree->Nodes())
Sharon Yangefe52632022-03-08 23:06:0657 groups.insert(node->current_frame_host()->GetSiteInstance()->group());
Sharon Yangefe52632022-03-08 23:06:0658 return groups;
alexmos3fcd0ca2015-10-23 18:18:3359}
60
Kevin McNee5f594382021-05-06 23:18:2361// If |node| is the placeholder FrameTreeNode for an embedded frame tree,
62// returns the inner tree's main frame's FrameTreeNode. Otherwise, returns null.
63FrameTreeNode* GetInnerTreeMainFrameNode(FrameTreeNode* node) {
Dominic Farolino377edb302021-07-29 05:57:1864 FrameTreeNode* inner_main_frame_tree_node = FrameTreeNode::GloballyFindByID(
65 node->current_frame_host()->inner_tree_main_frame_tree_node_id());
Kevin McNee5f594382021-05-06 23:18:2366
Kevin McNeed459ad42022-09-12 19:24:5967 if (inner_main_frame_tree_node) {
Arthur Sonzognif6785ec2022-12-05 10:11:5068 DCHECK_NE(&node->frame_tree(), &inner_main_frame_tree_node->frame_tree());
Kevin McNee5f594382021-05-06 23:18:2369 }
70
Kevin McNeed459ad42022-09-12 19:24:5971 return inner_main_frame_tree_node;
Kevin McNee5f594382021-05-06 23:18:2372}
73
[email protected]9b159a52013-10-03 17:24:5574} // namespace
75
vmpstr33895d992016-02-24 20:55:2176FrameTree::NodeIterator::NodeIterator(const NodeIterator& other) = default;
77
Fergal Daly55b6d722020-09-11 07:56:3378FrameTree::NodeIterator::~NodeIterator() = default;
dcheng57e39e22016-01-21 00:25:3879
80FrameTree::NodeIterator& FrameTree::NodeIterator::operator++() {
Alex Moshchuk27caae82017-09-11 23:11:1881 if (current_node_ != root_of_subtree_to_skip_) {
Jayson Adams4db0bfe22021-07-15 19:24:0782 // Reserve enough space in the queue to accommodate the nodes we're
83 // going to add, to avoid repeated resize calls.
84 queue_.reserve(queue_.size() + current_node_->child_count());
85
Alex Moshchuk27caae82017-09-11 23:11:1886 for (size_t i = 0; i < current_node_->child_count(); ++i) {
87 FrameTreeNode* child = current_node_->child_at(i);
Kevin McNee5f594382021-05-06 23:18:2388 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:4889 if (should_descend_into_inner_trees_ && inner_tree_main_ftn) {
90 if (include_delegate_nodes_for_inner_frame_trees_)
91 queue_.push_back(child);
92 queue_.push_back(inner_tree_main_ftn);
93 } else {
94 queue_.push_back(child);
95 }
Kevin McNee5f594382021-05-06 23:18:2396 }
97
98 if (should_descend_into_inner_trees_) {
Jayson Adams4db0bfe22021-07-15 19:24:0799 auto unattached_nodes =
100 current_node_->current_frame_host()
101 ->delegate()
102 ->GetUnattachedOwnedNodes(current_node_->current_frame_host());
103
104 // Reserve enough space in the queue to accommodate the nodes we're
105 // going to add.
106 queue_.reserve(queue_.size() + unattached_nodes.size());
107
108 for (auto* unattached_node : unattached_nodes) {
109 queue_.push_back(unattached_node);
Kevin McNee5f594382021-05-06 23:18:23110 }
Alex Moshchuk27caae82017-09-11 23:11:18111 }
dcheng57e39e22016-01-21 00:25:38112 }
113
Kevin McNee5f594382021-05-06 23:18:23114 AdvanceNode();
115 return *this;
116}
dcheng57e39e22016-01-21 00:25:38117
Kevin McNee5f594382021-05-06 23:18:23118FrameTree::NodeIterator& FrameTree::NodeIterator::AdvanceSkippingChildren() {
119 AdvanceNode();
dcheng57e39e22016-01-21 00:25:38120 return *this;
121}
122
123bool FrameTree::NodeIterator::operator==(const NodeIterator& rhs) const {
124 return current_node_ == rhs.current_node_;
125}
126
Kevin McNee5f594382021-05-06 23:18:23127void FrameTree::NodeIterator::AdvanceNode() {
128 if (!queue_.empty()) {
129 current_node_ = queue_.front();
Jayson Adams4db0bfe22021-07-15 19:24:07130 queue_.pop_front();
Kevin McNee5f594382021-05-06 23:18:23131 } else {
132 current_node_ = nullptr;
133 }
134}
135
136FrameTree::NodeIterator::NodeIterator(
137 const std::vector<FrameTreeNode*>& starting_nodes,
138 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48139 bool should_descend_into_inner_trees,
140 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23141 : current_node_(nullptr),
142 root_of_subtree_to_skip_(root_of_subtree_to_skip),
143 should_descend_into_inner_trees_(should_descend_into_inner_trees),
Dave Tapuskadda303d2022-10-04 16:56:48144 include_delegate_nodes_for_inner_frame_trees_(
145 include_delegate_nodes_for_inner_frame_trees),
Jayson Adams4db0bfe22021-07-15 19:24:07146 queue_(starting_nodes.begin(), starting_nodes.end()) {
Dave Tapuskadda303d2022-10-04 16:56:48147 // If `include_delegate_nodes_for_inner_frame_trees_` is true then
148 // `should_descend_into_inner_trees_` must be true.
149 DCHECK(!include_delegate_nodes_for_inner_frame_trees_ ||
150 should_descend_into_inner_trees_);
Kevin McNee5f594382021-05-06 23:18:23151 AdvanceNode();
152}
dcheng57e39e22016-01-21 00:25:38153
154FrameTree::NodeIterator FrameTree::NodeRange::begin() {
W. James MacLeance3176d2019-10-18 04:01:45155 // We shouldn't be attempting a frame tree traversal while the tree is
Kevin McNee5f594382021-05-06 23:18:23156 // being constructed or destructed.
Peter Kastingd5685942022-09-02 17:52:17157 DCHECK(base::ranges::all_of(starting_nodes_, [](FrameTreeNode* ftn) {
158 return ftn->current_frame_host();
159 }));
Kevin McNee5f594382021-05-06 23:18:23160
161 return NodeIterator(starting_nodes_, root_of_subtree_to_skip_,
Dave Tapuskadda303d2022-10-04 16:56:48162 should_descend_into_inner_trees_,
163 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38164}
165
166FrameTree::NodeIterator FrameTree::NodeRange::end() {
Dave Tapuskadda303d2022-10-04 16:56:48167 return NodeIterator({}, nullptr, should_descend_into_inner_trees_,
168 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38169}
170
Kevin McNee5f594382021-05-06 23:18:23171FrameTree::NodeRange::NodeRange(
172 const std::vector<FrameTreeNode*>& starting_nodes,
173 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48174 bool should_descend_into_inner_trees,
175 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23176 : starting_nodes_(starting_nodes),
177 root_of_subtree_to_skip_(root_of_subtree_to_skip),
Dave Tapuskadda303d2022-10-04 16:56:48178 should_descend_into_inner_trees_(should_descend_into_inner_trees),
179 include_delegate_nodes_for_inner_frame_trees_(
180 include_delegate_nodes_for_inner_frame_trees) {}
Kevin McNee5f594382021-05-06 23:18:23181
182FrameTree::NodeRange::NodeRange(const NodeRange&) = default;
183FrameTree::NodeRange::~NodeRange() = default;
dcheng57e39e22016-01-21 00:25:38184
Carlos Caballero40b0efd2021-01-26 11:55:00185FrameTree::FrameTree(
186 BrowserContext* browser_context,
Carlos Caballero03262522021-02-05 14:49:58187 Delegate* delegate,
Carlos Caballero40b0efd2021-01-26 11:55:00188 NavigationControllerDelegate* navigation_controller_delegate,
189 NavigatorDelegate* navigator_delegate,
190 RenderFrameHostDelegate* render_frame_delegate,
191 RenderViewHostDelegate* render_view_delegate,
192 RenderWidgetHostDelegate* render_widget_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59193 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26194 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44195 Type type)
Carlos Caballero03262522021-02-05 14:49:58196 : delegate_(delegate),
197 render_frame_delegate_(render_frame_delegate),
[email protected]92404c62013-12-04 16:40:46198 render_view_delegate_(render_view_delegate),
[email protected]fa944cb82013-11-15 17:51:21199 render_widget_delegate_(render_widget_delegate),
200 manager_delegate_(manager_delegate),
Jeremy Roman2d8dfe132021-07-06 20:51:26201 page_delegate_(page_delegate),
Carlos Caballero40b0efd2021-01-26 11:55:00202 navigator_(browser_context,
203 *this,
204 navigator_delegate,
205 navigation_controller_delegate),
Harkiran Bolaria16f2c48d2022-04-22 12:39:57206 type_(type),
thestige62f7382016-11-08 18:31:39207 focused_frame_tree_node_id_(FrameTreeNode::kFrameTreeNodeInvalidId),
David Bokanc3fb5fa2022-07-04 14:55:31208 load_progress_(0.0),
Arthur Sonzognif6785ec2022-12-05 10:11:50209 root_(*this,
Paul Semel3e241042022-10-11 12:57:31210 nullptr,
211 // The top-level frame must always be in a
212 // document scope.
213 blink::mojom::TreeScopeType::kDocument,
214 false,
Paul Semel3e241042022-10-11 12:57:31215 blink::mojom::FrameOwnerProperties(),
216 blink::FrameOwnerElementType::kNone,
217 blink::FramePolicy()) {}
[email protected]9b159a52013-10-03 17:24:55218
219FrameTree::~FrameTree() {
Takashi Toyoshimaea534ef22021-07-21 03:27:59220 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05221#if DCHECK_IS_ON()
222 DCHECK(was_shut_down_);
223#endif
[email protected]9b159a52013-10-03 17:24:55224}
225
Sharon Yang7ce309e2023-01-19 21:39:57226void FrameTree::MakeSpeculativeRVHCurrent() {
227 CHECK(speculative_render_view_host_);
228
229 // The existing RenderViewHost needs to be unregistered first.
230 // Speculative RenderViewHosts are only used for same-SiteInstanceGroup
231 // navigations, so there should be a RenderViewHost of the same
232 // SiteInstanceGroup already in the tree.
233 RenderViewHostMapId speculative_id =
234 speculative_render_view_host_->rvh_map_id();
235 auto it = render_view_host_map_.find(speculative_id);
236 CHECK(it != render_view_host_map_.end());
237 UnregisterRenderViewHost(speculative_id, it->second);
238
239 speculative_render_view_host_->set_is_speculative(false);
240 RegisterRenderViewHost(speculative_id, speculative_render_view_host_.get());
241 speculative_render_view_host_.reset();
242}
243
vishal.b782eb5d2015-04-29 12:22:57244FrameTreeNode* FrameTree::FindByID(int frame_tree_node_id) {
dcheng57e39e22016-01-21 00:25:38245 for (FrameTreeNode* node : Nodes()) {
246 if (node->frame_tree_node_id() == frame_tree_node_id)
247 return node;
248 }
249 return nullptr;
[email protected]9b159a52013-10-03 17:24:55250}
251
nasko479ea5a2015-02-14 00:03:04252FrameTreeNode* FrameTree::FindByRoutingID(int process_id, int routing_id) {
dmazzoni0b5d2482014-09-10 19:45:57253 RenderFrameHostImpl* render_frame_host =
254 RenderFrameHostImpl::FromID(process_id, routing_id);
255 if (render_frame_host) {
256 FrameTreeNode* result = render_frame_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50257 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57258 return result;
259 }
260
261 RenderFrameProxyHost* render_frame_proxy_host =
262 RenderFrameProxyHost::FromID(process_id, routing_id);
263 if (render_frame_proxy_host) {
264 FrameTreeNode* result = render_frame_proxy_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50265 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57266 return result;
267 }
268
creis6a93a812015-04-24 23:13:17269 return nullptr;
270}
271
272FrameTreeNode* FrameTree::FindByName(const std::string& name) {
273 if (name.empty())
Paul Semel3e241042022-10-11 12:57:31274 return &root_;
creis6a93a812015-04-24 23:13:17275
dcheng57e39e22016-01-21 00:25:38276 for (FrameTreeNode* node : Nodes()) {
277 if (node->frame_name() == name)
278 return node;
[email protected]9b159a52013-10-03 17:24:55279 }
dcheng57e39e22016-01-21 00:25:38280
281 return nullptr;
282}
283
284FrameTree::NodeRange FrameTree::Nodes() {
Alex Moshchuk27caae82017-09-11 23:11:18285 return NodesExceptSubtree(nullptr);
dcheng57e39e22016-01-21 00:25:38286}
287
kenrb61b6c252016-03-22 17:37:15288FrameTree::NodeRange FrameTree::SubtreeNodes(FrameTreeNode* subtree_root) {
Kevin McNee5f594382021-05-06 23:18:23289 return NodeRange({subtree_root}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48290 /*should_descend_into_inner_trees=*/false,
291 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee5f594382021-05-06 23:18:23292}
293
Kevin McNee53f0b2d2021-11-02 18:00:45294FrameTree::NodeRange FrameTree::NodesIncludingInnerTreeNodes() {
Paul Semel3e241042022-10-11 12:57:31295 return NodeRange({&root_}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48296 /*should_descend_into_inner_trees=*/true,
297 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee53f0b2d2021-11-02 18:00:45298}
299
Sreeja Kamishettyd64b993d2022-02-14 12:04:42300std::vector<FrameTreeNode*> FrameTree::CollectNodesForIsLoading() {
301 FrameTree::NodeRange node_range = NodesIncludingInnerTreeNodes();
302 FrameTree::NodeIterator node_iter = node_range.begin();
303 std::vector<FrameTreeNode*> nodes;
304
305 DCHECK(node_iter != node_range.end());
Arthur Sonzognif6785ec2022-12-05 10:11:50306 FrameTree* root_loading_tree = root_.frame_tree().LoadingTree();
Sreeja Kamishettyd64b993d2022-02-14 12:04:42307 while (node_iter != node_range.end()) {
308 // Skip over frame trees and children which belong to inner web contents
309 // i.e., when nodes doesn't point to the same loading frame tree.
Arthur Sonzognif6785ec2022-12-05 10:11:50310 if ((*node_iter)->frame_tree().LoadingTree() != root_loading_tree) {
Sreeja Kamishettyd64b993d2022-02-14 12:04:42311 node_iter.AdvanceSkippingChildren();
312 } else {
313 nodes.push_back(*node_iter);
314 ++node_iter;
315 }
316 }
317 return nodes;
318}
319
Kevin McNee5f594382021-05-06 23:18:23320FrameTree::NodeRange FrameTree::SubtreeAndInnerTreeNodes(
Dave Tapuskadda303d2022-10-04 16:56:48321 RenderFrameHostImpl* parent,
322 bool include_delegate_nodes_for_inner_frame_trees) {
Kevin McNee5f594382021-05-06 23:18:23323 std::vector<FrameTreeNode*> starting_nodes;
324 starting_nodes.reserve(parent->child_count());
325 for (size_t i = 0; i < parent->child_count(); ++i) {
326 FrameTreeNode* child = parent->child_at(i);
327 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:48328 if (inner_tree_main_ftn) {
329 if (include_delegate_nodes_for_inner_frame_trees)
330 starting_nodes.push_back(child);
331 starting_nodes.push_back(inner_tree_main_ftn);
332 } else {
333 starting_nodes.push_back(child);
334 }
Kevin McNee5f594382021-05-06 23:18:23335 }
336 const std::vector<FrameTreeNode*> unattached_owned_nodes =
337 parent->delegate()->GetUnattachedOwnedNodes(parent);
338 starting_nodes.insert(starting_nodes.end(), unattached_owned_nodes.begin(),
339 unattached_owned_nodes.end());
340 return NodeRange(starting_nodes, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48341 /* should_descend_into_inner_trees */ true,
342 include_delegate_nodes_for_inner_frame_trees);
kenrb61b6c252016-03-22 17:37:15343}
344
Alex Moshchuk27caae82017-09-11 23:11:18345FrameTree::NodeRange FrameTree::NodesExceptSubtree(FrameTreeNode* node) {
Paul Semel3e241042022-10-11 12:57:31346 return NodeRange({&root_}, node, /*should_descend_into_inner_trees=*/false,
Dave Tapuskadda303d2022-10-04 16:56:48347 /*include_delegate_nodes_for_inner_frame_trees=*/false);
[email protected]9b159a52013-10-03 17:24:55348}
349
Sreeja Kamishettyd64b993d2022-02-14 12:04:42350FrameTree* FrameTree::LoadingTree() {
351 // We return the delegate's loading frame tree to infer loading related
352 // states.
353 return delegate_->LoadingTree();
354}
355
Lucas Furukawa Gadani99125822019-01-03 15:41:49356FrameTreeNode* FrameTree::AddFrame(
Alexander Timin381e7e182020-04-28 19:04:03357 RenderFrameHostImpl* parent,
Balazs Engedyba034e72017-10-27 22:26:28358 int process_id,
359 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18360 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
Oksana Zhuravlovafee097c2019-07-26 17:01:30361 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
362 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30363 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49364 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
365 associated_interface_provider_receiver,
Antonio Gomes9d5c1ef2020-04-30 20:56:41366 blink::mojom::TreeScopeType scope,
Balazs Engedyba034e72017-10-27 22:26:28367 const std::string& frame_name,
368 const std::string& frame_unique_name,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45369 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04370 const blink::LocalFrameToken& frame_token,
Balazs Engedyba034e72017-10-27 22:26:28371 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34372 const blink::DocumentToken& document_token,
Luna Luc3fdacdf2017-11-08 04:48:53373 const blink::FramePolicy& frame_policy,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54374 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Ehsan Karamad192a8da2018-10-21 03:48:08375 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41376 blink::FrameOwnerElementType owner_type,
377 bool is_dummy_frame_for_inner_tree) {
nick8814e652015-12-18 01:44:12378 CHECK_NE(new_routing_id, MSG_ROUTING_NONE);
Dominic Farolino4bc10ee2021-08-31 00:37:36379 // Normally this path is for blink adding a child local frame. But both
380 // portals and fenced frames add a dummy child frame that never gets a
381 // corresponding RenderFrameImpl in any renderer process, and therefore its
382 // `frame_remote` is invalid. Also its RenderFrameHostImpl is exempt from
383 // having `RenderFrameCreated()` called on it (see later in this method, as
384 // well as `WebContentsObserverConsistencyChecker::RenderFrameHostChanged()`).
Kevin McNee43fe8292021-10-04 22:59:41385 DCHECK_NE(frame_remote.is_valid(), is_dummy_frame_for_inner_tree);
386 DCHECK_NE(browser_interface_broker_receiver.is_valid(),
387 is_dummy_frame_for_inner_tree);
Dominic Farolino12e06d72022-08-05 02:29:49388 DCHECK_NE(associated_interface_provider_receiver.is_valid(),
389 is_dummy_frame_for_inner_tree);
nick8814e652015-12-18 01:44:12390
dgroganfb22f9a2014-10-20 21:32:32391 // A child frame always starts with an initial empty document, which means
392 // it is in the same SiteInstance as the parent frame. Ensure that the process
393 // which requested a child frame to be added is the same as the process of the
394 // parent node.
Dominic Farolino6caf3032021-10-20 03:50:08395 CHECK_EQ(parent->GetProcess()->GetID(), process_id);
dgroganfb22f9a2014-10-20 21:32:32396
Danil Somsikov259aa65f2022-11-11 20:49:44397 std::unique_ptr<FrameTreeNode> new_node = base::WrapUnique(
Arthur Sonzognif6785ec2022-12-05 10:11:50398 new FrameTreeNode(*this, parent, scope, is_created_by_script,
Danil Somsikov259aa65f2022-11-11 20:49:44399 frame_owner_properties, owner_type, frame_policy));
iclelland098da752017-06-28 13:46:50400
401 // Set sandbox flags and container policy and make them effective immediately,
Charlie Hu5130d25e2021-03-05 21:53:39402 // since initial sandbox flags and permissions policy should apply to the
403 // initial empty document in the frame. This needs to happen before the call
404 // to AddChild so that the effective policy is sent to any newly-created
Dave Tapuska2402595f2022-08-03 16:24:21405 // `blink::RemoteFrame` objects when the RenderFrameHost is created.
Charlie Hu5ffc0152019-12-06 15:59:53406 // SetPendingFramePolicy is necessary here because next navigation on this
407 // frame will need the value of pending frame policy instead of effective
408 // frame policy.
Ian Clellandcdc4f312017-10-13 22:24:12409 new_node->SetPendingFramePolicy(frame_policy);
iclelland098da752017-06-28 13:46:50410
Shubhie Panickerddf2a4e2018-03-06 00:09:06411 if (was_discarded)
412 new_node->set_was_discarded();
413
iclelland098da752017-06-28 13:46:50414 // Add the new node to the FrameTree, creating the RenderFrameHost.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40415 FrameTreeNode* added_node = parent->AddChild(
416 std::move(new_node), new_routing_id, std::move(frame_remote), frame_token,
Danil Somsikov259aa65f2022-11-11 20:49:44417 document_token, devtools_frame_token, frame_policy, frame_name,
418 frame_unique_name);
nick8814e652015-12-18 01:44:12419
Garrett Tanzer34cb92fe2022-09-28 17:50:54420 added_node->SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30421
Kevin McNee43fe8292021-10-04 22:59:41422 if (browser_interface_broker_receiver.is_valid()) {
423 added_node->current_frame_host()->BindBrowserInterfaceBrokerReceiver(
424 std::move(browser_interface_broker_receiver));
425 }
Oksana Zhuravlovafee097c2019-07-26 17:01:30426
Antonio Sartoridb967c52021-01-20 09:54:30427 if (policy_container_bind_params) {
Antonio Sartoria1fd1432020-11-25 09:10:20428 added_node->current_frame_host()->policy_container_host()->Bind(
Antonio Sartoridb967c52021-01-20 09:54:30429 std::move(policy_container_bind_params));
Antonio Sartoria1fd1432020-11-25 09:10:20430 }
431
Dominic Farolino12e06d72022-08-05 02:29:49432 if (associated_interface_provider_receiver.is_valid()) {
433 added_node->current_frame_host()->BindAssociatedInterfaceProviderReceiver(
434 std::move(associated_interface_provider_receiver));
435 }
436
nasko03ecfad2016-08-02 00:54:06437 // The last committed NavigationEntry may have a FrameNavigationEntry with the
438 // same |frame_unique_name|, since we don't remove FrameNavigationEntries if
439 // their frames are deleted. If there is a stale one, remove it to avoid
440 // conflicts on future updates.
Fergal Daly09d6c762020-05-29 02:05:18441 NavigationEntryImpl* last_committed_entry = static_cast<NavigationEntryImpl*>(
Carlos Caballero40b0efd2021-01-26 11:55:00442 navigator_.controller().GetLastCommittedEntry());
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45443 if (last_committed_entry) {
444 last_committed_entry->RemoveEntryForFrame(
445 added_node, /* only_if_different_position = */ true);
446 }
nasko03ecfad2016-08-02 00:54:06447
nick8814e652015-12-18 01:44:12448 // Now that the new node is part of the FrameTree and has a RenderFrameHost,
449 // we can announce the creation of the initial RenderFrame which already
450 // exists in the renderer process.
Sreeja Kamishetty5b699622021-01-22 12:54:08451 // For consistency with navigating to a new RenderFrameHost case, we dispatch
452 // RenderFrameCreated before RenderFrameHostChanged.
Kevin McNee43fe8292021-10-04 22:59:41453 if (!is_dummy_frame_for_inner_tree) {
Dominic Farolino4bc10ee2021-08-31 00:37:36454 // The outer dummy FrameTreeNode for both portals and fenced frames does not
455 // have a live RenderFrame in the renderer process.
Fergal Dalyf9ea35c2020-12-11 15:26:01456 added_node->current_frame_host()->RenderFrameCreated();
Ehsan Karamad44fc72112019-02-26 18:15:47457 }
Sreeja Kamishetty5b699622021-01-22 12:54:08458
459 // Notify the delegate of the creation of the current RenderFrameHost.
460 // This is only for subframes, as the main frame case is taken care of by
461 // WebContentsImpl::Init.
462 manager_delegate_->NotifySwappedFromRenderManager(
Dave Tapuskae45d6fd2021-09-29 17:03:59463 nullptr, added_node->current_frame_host());
Lucas Furukawa Gadani99125822019-01-03 15:41:49464 return added_node;
[email protected]9b159a52013-10-03 17:24:55465}
466
[email protected]58faf942014-02-20 21:03:58467void FrameTree::RemoveFrame(FrameTreeNode* child) {
Alexander Timin381e7e182020-04-28 19:04:03468 RenderFrameHostImpl* parent = child->parent();
[email protected]58faf942014-02-20 21:03:58469 if (!parent) {
470 NOTREACHED() << "Unexpected RemoveFrame call for main frame.";
471 return;
[email protected]9b159a52013-10-03 17:24:55472 }
473
Alexander Timin381e7e182020-04-28 19:04:03474 parent->RemoveChild(child);
[email protected]9b159a52013-10-03 17:24:55475}
476
Harkiran Bolaria2912a6b32022-02-22 16:43:45477void FrameTree::CreateProxiesForSiteInstance(
478 FrameTreeNode* source,
Charlie Reis37be2682023-01-10 17:04:47479 SiteInstanceImpl* site_instance,
Harkiran Bolaria2912a6b32022-02-22 16:43:45480 const scoped_refptr<BrowsingContextState>&
481 source_new_browsing_context_state) {
Charlie Reis37be2682023-01-10 17:04:47482 SiteInstanceGroup* group = site_instance->group();
naskob3041b98a42016-03-12 04:43:06483 // Create the RenderFrameProxyHost for the new SiteInstance.
alexmos58729042015-06-18 23:20:00484 if (!source || !source->IsMainFrame()) {
Sharon Yang57bde122022-03-01 20:01:12485 RenderViewHostImpl* render_view_host = GetRenderViewHost(group).get();
arthursonzognic5be3842019-07-09 11:49:14486 if (render_view_host) {
Sharon Yang57bde122022-03-01 20:01:12487 root()->render_manager()->EnsureRenderViewInitialized(render_view_host,
488 group);
arthursonzognic5be3842019-07-09 11:49:14489 } else {
Harkiran Bolaria2912a6b32022-02-22 16:43:45490 // Due to the check above, we are creating either an opener proxy (when
491 // source is null) or a main frame proxy due to a subframe navigation
492 // (when source is not a main frame). In the former case, we should use
493 // root's current BrowsingContextState, while in the latter case we should
494 // use BrowsingContextState from the main RenderFrameHost of the subframe
Dave Tapuska2cf1f532022-08-10 15:30:49495 // being navigated. We want to ensure that the `blink::WebView` is created
496 // in the right SiteInstance if it doesn't exist, before creating the
497 // other proxies; if the `blink::WebView` doesn't exist, the only way to
498 // do this is to also create a proxy for the main frame as well.
Harkiran Bolaria2912a6b32022-02-22 16:43:45499 root()->render_manager()->CreateRenderFrameProxy(
500 site_instance,
501 source ? source->parent()->GetMainFrame()->browsing_context_state()
502 : root()->current_frame_host()->browsing_context_state());
[email protected]82307f6b2014-08-07 03:30:12503 }
504 }
505
Alex Moshchuk1226b152019-11-08 18:23:45506 // Check whether we're in an inner delegate and |site_instance| corresponds
507 // to the outer delegate. Subframe proxies aren't needed if this is the
508 // case.
509 bool is_site_instance_for_outer_delegate = false;
510 RenderFrameProxyHost* outer_delegate_proxy =
511 root()->render_manager()->GetProxyToOuterDelegate();
512 if (outer_delegate_proxy) {
513 is_site_instance_for_outer_delegate =
514 (site_instance == outer_delegate_proxy->GetSiteInstance());
515 }
516
naskoe6edde32014-10-17 15:36:48517 // Proxies are created in the FrameTree in response to a node navigating to a
518 // new SiteInstance. Since |source|'s navigation will replace the currently
Alex Moshchuk27caae82017-09-11 23:11:18519 // loaded document, the entire subtree under |source| will be removed, and
520 // thus proxy creation is skipped for all nodes in that subtree.
521 //
522 // However, a proxy *is* needed for the |source| node itself. This lets
523 // cross-process navigations in |source| start with a proxy and follow a
524 // remote-to-local transition, which avoids race conditions in cases where
525 // other navigations need to reference |source| before it commits. See
526 // https://crbug.com/756790 for more background. Therefore,
527 // NodesExceptSubtree(source) will include |source| in the nodes traversed
528 // (see NodeIterator::operator++).
529 for (FrameTreeNode* node : NodesExceptSubtree(source)) {
dcheng57e39e22016-01-21 00:25:38530 // If a new frame is created in the current SiteInstance, other frames in
531 // that SiteInstance don't need a proxy for the new frame.
Alex Moshchuk27caae82017-09-11 23:11:18532 RenderFrameHostImpl* current_host =
533 node->render_manager()->current_frame_host();
Charlie Reis37be2682023-01-10 17:04:47534 SiteInstanceImpl* current_instance = current_host->GetSiteInstance();
Alex Moshchuk27caae82017-09-11 23:11:18535 if (current_instance != site_instance) {
536 if (node == source && !current_host->IsRenderFrameLive()) {
Fergal Daly6de62f52020-10-14 01:56:44537 // We don't create a proxy at |source| when the current RenderFrameHost
538 // isn't live. This is because either (1) the speculative
Alex Moshchuk27caae82017-09-11 23:11:18539 // RenderFrameHost will be committed immediately, and the proxy
Fergal Daly6de62f52020-10-14 01:56:44540 // destroyed right away, in GetFrameHostForNavigation, which makes the
541 // races above impossible, or (2) the early commit will be skipped due
542 // to ShouldSkipEarlyCommitPendingForCrashedFrame, in which case the
543 // proxy for |source| *is* needed, but it will be created later in
544 // CreateProxiesForNewRenderFrameHost.
545 //
546 // TODO(fergal): Consider creating a proxy for |source| here rather than
547 // in CreateProxiesForNewRenderFrameHost for case (2) above.
Alex Moshchuk27caae82017-09-11 23:11:18548 continue;
549 }
Alex Moshchuk1226b152019-11-08 18:23:45550
551 // Do not create proxies for subframes in the outer delegate's
552 // SiteInstance, since there is no need to expose these subframes to the
553 // outer delegate. See also comments in CreateProxiesForChildFrame() and
554 // https://crbug.com/1013553.
555 if (!node->IsMainFrame() && is_site_instance_for_outer_delegate)
556 continue;
557
Harkiran Bolaria2912a6b32022-02-22 16:43:45558 // If |node| is the FrameTreeNode being navigated, we use
559 // |browsing_context_state| (as BrowsingContextState might change for
560 // cross-BrowsingInstance navigations). Otherwise, we should use the
561 // |node|'s current BrowsingContextState.
562 node->render_manager()->CreateRenderFrameProxy(
563 site_instance,
564 node == source
565 ? source_new_browsing_context_state
566 : node->current_frame_host()->browsing_context_state());
Alex Moshchuk27caae82017-09-11 23:11:18567 }
dcheng57e39e22016-01-21 00:25:38568 }
[email protected]82307f6b2014-08-07 03:30:12569}
570
[email protected]9b159a52013-10-03 17:24:55571RenderFrameHostImpl* FrameTree::GetMainFrame() const {
Paul Semel3e241042022-10-11 12:57:31572 return root_.current_frame_host();
[email protected]9b159a52013-10-03 17:24:55573}
574
[email protected]9c9343b2014-03-08 02:56:07575FrameTreeNode* FrameTree::GetFocusedFrame() {
576 return FindByID(focused_frame_tree_node_id_);
577}
578
Sharon Yangefe52632022-03-08 23:06:06579void FrameTree::SetFocusedFrame(FrameTreeNode* node,
580 SiteInstanceGroup* source) {
Rakina Zata Amnicca4889e2021-09-28 23:25:55581 CHECK(node->current_frame_host()->IsActive());
alexmos5357efb2015-12-16 21:44:00582 if (node == GetFocusedFrame())
583 return;
584
Sharon Yangefe52632022-03-08 23:06:06585 std::set<SiteInstanceGroup*> frame_tree_groups =
586 CollectSiteInstanceGroups(this);
alexmosb1dc2162015-11-05 00:59:20587
Sharon Yangefe52632022-03-08 23:06:06588 SiteInstanceGroup* current_group =
589 node->current_frame_host()->GetSiteInstance()->group();
alexmos5357efb2015-12-16 21:44:00590
Sharon Yang7424bda2021-11-04 20:27:43591 // Update the focused frame in all other SiteInstanceGroups. If focus changes
592 // to a cross-group frame, this allows the old focused frame's renderer
alexmosb1dc2162015-11-05 00:59:20593 // process to clear focus from that frame and fire blur events. It also
594 // ensures that the latest focused frame is available in all renderers to
595 // compute document.activeElement.
alexmos5357efb2015-12-16 21:44:00596 //
Sharon Yangefe52632022-03-08 23:06:06597 // We do not notify the |source| SiteInstanceGroup because it already knows
598 // the new focused frame (since it initiated the focus change), and we notify
599 // the new focused frame's SiteInstanceGroup (if it differs from |source|)
600 // separately below.
601 for (auto* group : frame_tree_groups) {
602 if (group != source && group != current_group) {
603 RenderFrameProxyHost* proxy = node->current_frame_host()
604 ->browsing_context_state()
605 ->GetRenderFrameProxyHost(group);
Sharon Yangf92842a2022-03-01 18:21:47606
Rakina Zata Amni80700402021-09-20 17:18:03607 if (proxy) {
608 proxy->SetFocusedFrame();
609 } else {
Sharon Yangf92842a2022-03-01 18:21:47610 base::debug::DumpWithoutCrashing();
Rakina Zata Amni80700402021-09-20 17:18:03611 }
alexmosb1dc2162015-11-05 00:59:20612 }
alexmosca2c6ba2015-10-01 21:52:25613 }
614
Sharon Yangefe52632022-03-08 23:06:06615 // If |node| was focused from a cross-group frame (i.e., via
alexmos5357efb2015-12-16 21:44:00616 // window.focus()), tell its RenderFrame that it should focus.
Sharon Yangefe52632022-03-08 23:06:06617 if (current_group != source)
alexmos5357efb2015-12-16 21:44:00618 node->current_frame_host()->SetFocusedFrame();
619
[email protected]9c9343b2014-03-08 02:56:07620 focused_frame_tree_node_id_ = node->frame_tree_node_id();
alexmos21acae52015-11-07 01:04:43621 node->DidFocus();
dmazzonif27bf892016-03-10 15:51:55622
623 // The accessibility tree data for the root of the frame tree keeps
624 // track of the focused frame too, so update that every time the
625 // focused frame changes.
Kevin McNee582a7d62021-10-12 21:42:22626 root()
627 ->current_frame_host()
628 ->GetOutermostMainFrameOrEmbedder()
629 ->UpdateAXTreeData();
[email protected]9c9343b2014-03-08 02:56:07630}
631
arthursonzognic5be3842019-07-09 11:49:14632scoped_refptr<RenderViewHostImpl> FrameTree::CreateRenderViewHost(
Charlie Reis37be2682023-01-10 17:04:47633 SiteInstanceImpl* site_instance,
avib7348942015-12-25 20:57:10634 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58635 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57636 scoped_refptr<BrowsingContextState> main_browsing_context_state,
637 CreateRenderViewHostCase create_case) {
Harkiran Bolaria57e2b062022-03-14 10:27:58638 if (main_browsing_context_state) {
639 DCHECK(main_browsing_context_state->is_main_frame());
640 }
dcheng29f5a6c2015-08-31 21:43:27641 RenderViewHostImpl* rvh =
642 static_cast<RenderViewHostImpl*>(RenderViewHostFactory::Create(
Charlie Reis37be2682023-01-10 17:04:47643 this, site_instance->group(),
Sharon Yangcadd00b82022-05-13 19:16:33644 site_instance->GetStoragePartitionConfig(), render_view_delegate_,
Dave Tapuska658bfec2022-08-05 14:27:20645 render_widget_delegate_, main_frame_routing_id,
Sharon Yang7ce309e2023-01-19 21:39:57646 renderer_initiated_creation, std::move(main_browsing_context_state),
647 create_case));
648
649 if (ShouldCreateNewHostForAllFrames() &&
650 create_case == CreateRenderViewHostCase::kSpeculative) {
651 set_speculative_render_view_host(rvh->GetWeakPtr());
652 } else {
653 // Register non-speculative RenderViewHosts. If they are speculative, they
654 // will be registered when they become active.
655 RegisterRenderViewHost(rvh->rvh_map_id(), rvh);
656 }
657
arthursonzognic5be3842019-07-09 11:49:14658 return base::WrapRefCounted(rvh);
[email protected]94d0cc12013-12-18 00:07:41659}
660
arthursonzognic5be3842019-07-09 11:49:14661scoped_refptr<RenderViewHostImpl> FrameTree::GetRenderViewHost(
Sharon Yang57bde122022-03-01 20:01:12662 SiteInstanceGroup* group) {
Sharon Yanga2fe85e2022-02-09 21:38:29663 // When called from RenderFrameHostManager::CreateRenderFrameHost, it's
664 // possible that a RenderProcessHost hasn't yet been created, which means
Sharon Yang57bde122022-03-01 20:01:12665 // a SiteInstanceGroup won't have been created yet.
Sharon Yanga2fe85e2022-02-09 21:38:29666 if (!group)
667 return nullptr;
668
669 auto it = render_view_host_map_.find(GetRenderViewHostMapId(group));
arthursonzogni88e54ae2019-04-15 12:57:36670 if (it == render_view_host_map_.end())
671 return nullptr;
alexmos9aa61232016-04-26 21:54:02672
arthursonzognic5be3842019-07-09 11:49:14673 return base::WrapRefCounted(it->second);
[email protected]94d0cc12013-12-18 00:07:41674}
675
Aaron Colwell78b4bde2021-03-16 16:16:09676FrameTree::RenderViewHostMapId FrameTree::GetRenderViewHostMapId(
Sharon Yangc581a0c2021-11-02 18:09:39677 SiteInstanceGroup* site_instance_group) const {
678 return RenderViewHostMapId::FromUnsafeValue(
679 site_instance_group->GetId().value());
Lowell Manners75055a132019-10-11 10:30:29680}
681
Aaron Colwell78b4bde2021-03-16 16:16:09682void FrameTree::RegisterRenderViewHost(RenderViewHostMapId id,
683 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47684 TRACE_EVENT_INSTANT("navigation", "FrameTree::RegisterRenderViewHost",
685 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57686 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09687 CHECK(!base::Contains(render_view_host_map_, id));
688 render_view_host_map_[id] = rvh;
Sharon Yang7ce309e2023-01-19 21:39:57689 rvh->set_is_registered_with_frame_tree(true);
Aaron Colwell78b4bde2021-03-16 16:16:09690}
691
692void FrameTree::UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13693 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47694 TRACE_EVENT_INSTANT("navigation", "FrameTree::UnregisterRenderViewHost",
695 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57696 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09697 auto it = render_view_host_map_.find(id);
arthursonzogni88e54ae2019-04-15 12:57:36698 CHECK(it != render_view_host_map_.end());
arthursonzognic5be3842019-07-09 11:49:14699 CHECK_EQ(it->second, rvh);
700 render_view_host_map_.erase(it);
Sharon Yang7ce309e2023-01-19 21:39:57701 rvh->set_is_registered_with_frame_tree(false);
[email protected]94d0cc12013-12-18 00:07:41702}
703
arthursonzogni0a2007792019-05-28 21:32:48704void FrameTree::FrameUnloading(FrameTreeNode* frame) {
705 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
706 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
707
708 // Ensure frames that are about to be deleted aren't visible from the other
709 // processes anymore.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40710 frame->GetBrowsingContextStateForSubframe()->ResetProxyHosts();
arthursonzogni0a2007792019-05-28 21:32:48711}
712
dmazzonie950ea232015-03-13 21:39:45713void FrameTree::FrameRemoved(FrameTreeNode* frame) {
nick53d5cbf2015-04-23 22:50:14714 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
thestige62f7382016-11-08 18:31:39715 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
dmazzonie950ea232015-03-13 21:39:45716}
717
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15718double FrameTree::GetLoadProgress() {
Paul Semel3e241042022-10-11 12:57:31719 if (root_.HasNavigation())
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15720 return blink::kInitialLoadProgress;
721
Paul Semel3e241042022-10-11 12:57:31722 return root_.current_frame_host()->GetPage().load_progress();
fdegans1d16355162015-03-26 11:58:34723}
724
Yu Gaoc8c18552022-06-22 14:38:45725bool FrameTree::IsLoadingIncludingInnerFrameTrees() const {
Sreeja Kamishetty15f9944a22022-03-10 10:16:08726 for (const FrameTreeNode* node :
727 const_cast<FrameTree*>(this)->CollectNodesForIsLoading()) {
dcheng57e39e22016-01-21 00:25:38728 if (node->IsLoading())
729 return true;
730 }
731 return false;
fdegans1d16355162015-03-26 11:58:34732}
733
alexmos3fcd0ca2015-10-23 18:18:33734void FrameTree::ReplicatePageFocus(bool is_focused) {
Dave Tapuska53e6ce12021-08-06 16:08:24735 // Focus loss may occur while this FrameTree is being destroyed. Don't
736 // send the message in this case, as the main frame's RenderFrameHost and
737 // other state has already been cleared.
738 if (is_being_destroyed_)
739 return;
Sharon Yangefe52632022-03-08 23:06:06740 std::set<SiteInstanceGroup*> frame_tree_site_instance_groups =
741 CollectSiteInstanceGroups(this);
alexmos3fcd0ca2015-10-23 18:18:33742
Sharon Yangefe52632022-03-08 23:06:06743 // Send the focus update to main frame's proxies in all SiteInstanceGroups of
alexmos3fcd0ca2015-10-23 18:18:33744 // other frames in this FrameTree. Note that the main frame might also know
Sharon Yangefe52632022-03-08 23:06:06745 // about proxies in SiteInstanceGroups for frames in a different FrameTree
746 // (e.g., for window.open), so we can't just iterate over its proxy_hosts_ in
alexmos3fcd0ca2015-10-23 18:18:33747 // RenderFrameHostManager.
Sharon Yangefe52632022-03-08 23:06:06748 for (auto* group : frame_tree_site_instance_groups)
749 SetPageFocus(group, is_focused);
alexmos0d7e0b09b2015-10-29 22:11:48750}
alexmos3fcd0ca2015-10-23 18:18:33751
Sreeja Kamishetty60e47132022-02-08 12:51:53752bool FrameTree::IsPortal() {
753 return delegate_->IsPortal();
754}
755
Sharon Yangefe52632022-03-08 23:06:06756void FrameTree::SetPageFocus(SiteInstanceGroup* group, bool is_focused) {
Paul Semel3e241042022-10-11 12:57:31757 RenderFrameHostManager* root_manager = root_.render_manager();
alexmos0d7e0b09b2015-10-29 22:11:48758
Adithya Srinivasan47731222021-01-22 15:02:42759 // Portal frame tree should not get page focus.
Sreeja Kamishetty60e47132022-02-08 12:51:53760 DCHECK(!IsPortal() || !is_focused);
Adithya Srinivasan47731222021-01-22 15:02:42761
alexmos0d7e0b09b2015-10-29 22:11:48762 // This is only used to set page-level focus in cross-process subframes, and
Sharon Yangefe52632022-03-08 23:06:06763 // requests to set focus in main frame's SiteInstanceGroup are ignored.
764 if (group != root_manager->current_frame_host()->GetSiteInstance()->group()) {
765 RenderFrameProxyHost* proxy = root_manager->current_frame_host()
766 ->browsing_context_state()
767 ->GetRenderFrameProxyHost(group);
Dave Tapuska82b54012022-07-15 23:26:10768 if (proxy->is_render_frame_proxy_live())
769 proxy->GetAssociatedRemoteFrame()->SetPageFocus(is_focused);
alexmos3fcd0ca2015-10-23 18:18:33770 }
771}
772
W. James MacLeanc07dc41b2022-07-25 18:52:16773void FrameTree::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanb70fab82020-05-01 18:51:14774 const url::Origin& previously_visited_origin,
775 NavigationRequest* navigation_request_to_exclude) {
W. James MacLeanc07dc41b2022-07-25 18:52:16776 controller().RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanc6dc86c2021-08-12 13:58:37777 previously_visited_origin);
778
Charlie Reis37be2682023-01-10 17:04:47779 std::unordered_set<SiteInstanceImpl*> matching_site_instances;
W. James MacLeanb70fab82020-05-01 18:51:14780
781 // Be sure to visit all RenderFrameHosts associated with this frame that might
782 // have an origin that could script other frames. We skip RenderFrameHosts
783 // that are in the bfcache, assuming there's no way for a frame to join the
784 // BrowsingInstance of a bfcache RFH while it's in the cache.
785 for (auto* frame_tree_node : SubtreeNodes(root())) {
786 auto* frame_host = frame_tree_node->current_frame_host();
W. James MacLeanb70fab82020-05-01 18:51:14787 if (previously_visited_origin == frame_host->GetLastCommittedOrigin())
788 matching_site_instances.insert(frame_host->GetSiteInstance());
789
790 if (frame_host->HasCommittingNavigationRequestForOrigin(
791 previously_visited_origin, navigation_request_to_exclude)) {
792 matching_site_instances.insert(frame_host->GetSiteInstance());
793 }
794
795 auto* spec_frame_host =
796 frame_tree_node->render_manager()->speculative_frame_host();
797 if (spec_frame_host &&
798 spec_frame_host->HasCommittingNavigationRequestForOrigin(
799 previously_visited_origin, navigation_request_to_exclude)) {
800 matching_site_instances.insert(spec_frame_host->GetSiteInstance());
801 }
802
803 auto* navigation_request = frame_tree_node->navigation_request();
804 if (navigation_request &&
805 navigation_request != navigation_request_to_exclude &&
806 navigation_request->HasCommittingOrigin(previously_visited_origin)) {
807 matching_site_instances.insert(frame_host->GetSiteInstance());
808 }
809 }
810
811 // Update any SiteInstances found to contain |origin|.
812 for (auto* site_instance : matching_site_instances) {
Charlie Reis37be2682023-01-10 17:04:47813 site_instance->RegisterAsDefaultOriginIsolation(previously_visited_origin);
W. James MacLeanb70fab82020-05-01 18:51:14814 }
815}
816
Charlie Reis37be2682023-01-10 17:04:47817void FrameTree::Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00818 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17819 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46820 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44821 const blink::FramePolicy& frame_policy,
822 const base::UnguessableToken& devtools_frame_token) {
Carlos Caballero40b0efd2021-01-26 11:55:00823 // blink::FrameTree::SetName always keeps |unique_name| empty in case of a
824 // main frame - let's do the same thing here.
825 std::string unique_name;
Paul Semel3e241042022-10-11 12:57:31826 root_.render_manager()->InitRoot(main_frame_site_instance,
827 renderer_initiated_creation, frame_policy,
Danil Somsikov259aa65f2022-11-11 20:49:44828 main_frame_name, devtools_frame_token);
Paul Semel3e241042022-10-11 12:57:31829 root_.SetFencedFramePropertiesIfNeeded();
Rakina Zata Amniafd3c6582021-11-30 06:19:17830
831 // The initial empty document should inherit the origin of its opener (the
832 // origin may change after the first commit), except when they are in
Rakina Zata Amni4eb716e2022-04-05 21:32:46833 // different browsing context groups (`renderer_initiated_creation` will be
834 // false), where it should use a new opaque origin.
Rakina Zata Amniafd3c6582021-11-30 06:19:17835 // See also https://crbug.com/932067.
836 //
837 // Note that the origin of the new frame might depend on sandbox flags.
838 // Checking sandbox flags of the new frame should be safe at this point,
839 // because the flags should be already inherited when creating the root node.
Rakina Zata Amni4eb716e2022-04-05 21:32:46840 DCHECK(!renderer_initiated_creation || opener_for_origin);
Paul Semel3e241042022-10-11 12:57:31841 root_.current_frame_host()->SetOriginDependentStateOfNewFrame(
Rakina Zata Amni4eb716e2022-04-05 21:32:46842 renderer_initiated_creation ? opener_for_origin->GetLastCommittedOrigin()
Rakina Zata Amniafd3c6582021-11-30 06:19:17843 : url::Origin());
Rakina Zata Amni2322f4f82022-01-24 13:24:24844
Rakina Zata Amni46087a12022-11-11 08:28:38845 controller().CreateInitialEntry();
Carlos Caballero40b0efd2021-01-26 11:55:00846}
847
Carlos Caballeroede6f8c2021-01-28 11:01:50848void FrameTree::DidAccessInitialMainDocument() {
849 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidAccessInitialDocument");
850 has_accessed_initial_main_document_ = true;
851 controller().DidAccessInitialMainDocument();
852}
853
Carlos Caballero03262522021-02-05 14:49:58854void FrameTree::DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19855 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58856 bool was_previously_loading) {
Carlos Caballero03262522021-02-05 14:49:58857 if (was_previously_loading)
858 return;
859
Yu Gaoc8c18552022-06-22 14:38:45860 root()->render_manager()->SetIsLoading(IsLoadingIncludingInnerFrameTrees());
Nate Chapin9aabf5f2021-11-12 00:31:19861 delegate_->DidStartLoading(&node, should_show_loading_ui);
Carlos Caballero03262522021-02-05 14:49:58862}
863
864void FrameTree::DidStopLoadingNode(FrameTreeNode& node) {
Yu Gaoc8c18552022-06-22 14:38:45865 if (IsLoadingIncludingInnerFrameTrees())
Carlos Caballero03262522021-02-05 14:49:58866 return;
867
868 root()->render_manager()->SetIsLoading(false);
869 delegate_->DidStopLoading();
870}
871
Carlos Caballero03262522021-02-05 14:49:58872void FrameTree::DidCancelLoading() {
873 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidCancelLoading");
874 navigator_.controller().DiscardNonCommittedEntries();
Carlos Caballero03262522021-02-05 14:49:58875}
876
877void FrameTree::StopLoading() {
878 for (FrameTreeNode* node : Nodes())
879 node->StopLoading();
880}
881
Carlos Caballero101ac26b2021-03-24 11:54:05882void FrameTree::Shutdown() {
Dave Tapuskae88286de2021-08-05 19:10:42883 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05884#if DCHECK_IS_ON()
885 DCHECK(!was_shut_down_);
886 was_shut_down_ = true;
887#endif
888
Paul Semel3e241042022-10-11 12:57:31889 RenderFrameHostManager* root_manager = root_.render_manager();
Carlos Caballero101ac26b2021-03-24 11:54:05890
Carlos Caballerodb7a8f6e2021-04-09 18:17:47891 if (!root_manager->current_frame_host()) {
892 // The page has been transferred out during an activation. There is little
893 // left to do.
Carlos Caballerod1c80432021-04-20 08:16:32894 // TODO(https://crbug.com/1199693): If we decide that pending delete RFHs
Carlos Caballerodb7a8f6e2021-04-09 18:17:47895 // need to be moved along during activation replace this line with a DCHECK
896 // that there are no pending delete instances.
897 root_manager->ClearRFHsPendingShutdown();
Paul Semel3e241042022-10-11 12:57:31898 DCHECK(!root_.navigation_request());
Carlos Caballerodb7a8f6e2021-04-09 18:17:47899 DCHECK(!root_manager->speculative_frame_host());
Paul Semel3e241042022-10-11 12:57:31900 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:05901 return;
Carlos Caballerodb7a8f6e2021-04-09 18:17:47902 }
Carlos Caballero101ac26b2021-03-24 11:54:05903
904 for (FrameTreeNode* node : Nodes()) {
905 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
906 // to be shutdown and be deleted as well.
907 node->render_manager()->ClearRFHsPendingShutdown();
Carlos Caballerod1c80432021-04-20 08:16:32908 // TODO(https://crbug.com/1199676): Ban WebUI instance in Prerender pages.
Carlos Caballero101ac26b2021-03-24 11:54:05909 node->render_manager()->ClearWebUIInstances();
910 }
911
912 // Destroy all subframes now. This notifies observers.
913 root_manager->current_frame_host()->ResetChildren();
Harkiran Bolaria0b3bdef02022-03-10 13:04:40914 root_manager->current_frame_host()
915 ->browsing_context_state()
916 ->ResetProxyHosts();
Carlos Caballero101ac26b2021-03-24 11:54:05917
Carlos Caballero101ac26b2021-03-24 11:54:05918 // Manually call the observer methods for the root FrameTreeNode. It is
919 // necessary to manually delete all objects tracking navigations
920 // (NavigationHandle, NavigationRequest) for observers to be properly
921 // notified of these navigations stopping before the WebContents is
922 // destroyed.
923
924 root_manager->current_frame_host()->RenderFrameDeleted();
Rakina Zata Amnif8f2bb62022-11-23 05:54:32925 root_manager->current_frame_host()->ResetOwnedNavigationRequests(
926 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:05927
928 // Do not update state as the FrameTree::Delegate (possibly a WebContents) is
929 // being destroyed.
Paul Semel3e241042022-10-11 12:57:31930 root_.ResetNavigationRequestButKeepState();
Carlos Caballero101ac26b2021-03-24 11:54:05931 if (root_manager->speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:45932 root_manager->DiscardSpeculativeRenderFrameHostForShutdown();
Carlos Caballero101ac26b2021-03-24 11:54:05933 }
934
Alexander Timin8aeee642021-05-12 08:39:33935 // NavigationRequests restoring the page from bfcache have a reference to the
936 // RFHs stored in the cache, so the cache should be cleared after the
937 // navigation request is reset.
938 controller().GetBackForwardCache().Shutdown();
939
Paul Semel3e241042022-10-11 12:57:31940 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:05941 render_view_delegate_->RenderViewDeleted(
942 root_manager->current_frame_host()->render_view_host());
943}
944
Dave Tapuskad8b0530f2021-10-19 15:12:31945base::SafeRef<FrameTree> FrameTree::GetSafeRef() {
946 return weak_ptr_factory_.GetSafeRef();
947}
948
Dave Tapuska54c76a032021-10-27 22:10:42949void FrameTree::FocusOuterFrameTrees() {
950 OPTIONAL_TRACE_EVENT0("content", "FrameTree::FocusOuterFrameTrees");
951
952 FrameTree* frame_tree_to_focus = this;
953 while (true) {
954 FrameTreeNode* outer_node = FrameTreeNode::GloballyFindByID(
955 frame_tree_to_focus->delegate()->GetOuterDelegateFrameTreeNodeId());
956 if (!outer_node || !outer_node->current_frame_host()->IsActive()) {
957 // Don't set focus on an inactive FrameTreeNode.
958 return;
959 }
Arthur Sonzognif6785ec2022-12-05 10:11:50960 outer_node->frame_tree().SetFocusedFrame(outer_node, nullptr);
Dave Tapuska54c76a032021-10-27 22:10:42961
962 // For a browser initiated focus change, let embedding renderer know of the
963 // change. Otherwise, if the currently focused element is just across a
964 // process boundary in focus order, it will not be possible to move across
965 // that boundary. This is because the target element will already be focused
966 // (that renderer was not notified) and drop the event.
967 if (auto* proxy_to_outer_delegate = frame_tree_to_focus->root()
968 ->render_manager()
969 ->GetProxyToOuterDelegate()) {
970 proxy_to_outer_delegate->SetFocusedFrame();
971 }
Arthur Sonzognif6785ec2022-12-05 10:11:50972 frame_tree_to_focus = &outer_node->frame_tree();
Dave Tapuska54c76a032021-10-27 22:10:42973 }
974}
975
Ari Chivukula5d15efb2023-01-21 04:33:52976void FrameTree::RegisterOriginForUnpartitionedSessionStorageAccess(
977 const url::Origin& origin) {
978 if (origin.opaque()) {
979 return;
980 }
981 unpartitioned_session_storage_origins_.insert(origin);
982}
983
984const blink::StorageKey FrameTree::GetSessionStorageKey(
985 const blink::StorageKey& storage_key) {
986 if (unpartitioned_session_storage_origins_.find(storage_key.origin()) !=
987 unpartitioned_session_storage_origins_.end()) {
988 // If the storage key matches a participating origin we need to return the
989 // first-party version for use in binding session storage.
990 return blink::StorageKey(storage_key.origin());
991 }
992 return storage_key;
993}
994
[email protected]9b159a52013-10-03 17:24:55995} // namespace content