blob: 641ce1f021b56a1fcd1d7264af86d2070e98eb7e [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 Yanged884542023-02-02 17:33:44226void FrameTree::ForEachRenderViewHost(
227 base::FunctionRef<void(RenderViewHostImpl*)> on_host) {
228 if (speculative_render_view_host_) {
229 on_host(speculative_render_view_host_.get());
230 }
231
232 for (auto& rvh : render_view_host_map_) {
233 on_host(rvh.second);
234 }
235}
236
Sharon Yang7ce309e2023-01-19 21:39:57237void FrameTree::MakeSpeculativeRVHCurrent() {
238 CHECK(speculative_render_view_host_);
239
240 // The existing RenderViewHost needs to be unregistered first.
241 // Speculative RenderViewHosts are only used for same-SiteInstanceGroup
242 // navigations, so there should be a RenderViewHost of the same
243 // SiteInstanceGroup already in the tree.
244 RenderViewHostMapId speculative_id =
245 speculative_render_view_host_->rvh_map_id();
246 auto it = render_view_host_map_.find(speculative_id);
247 CHECK(it != render_view_host_map_.end());
248 UnregisterRenderViewHost(speculative_id, it->second);
249
250 speculative_render_view_host_->set_is_speculative(false);
251 RegisterRenderViewHost(speculative_id, speculative_render_view_host_.get());
252 speculative_render_view_host_.reset();
253}
254
vishal.b782eb5d2015-04-29 12:22:57255FrameTreeNode* FrameTree::FindByID(int frame_tree_node_id) {
dcheng57e39e22016-01-21 00:25:38256 for (FrameTreeNode* node : Nodes()) {
257 if (node->frame_tree_node_id() == frame_tree_node_id)
258 return node;
259 }
260 return nullptr;
[email protected]9b159a52013-10-03 17:24:55261}
262
nasko479ea5a2015-02-14 00:03:04263FrameTreeNode* FrameTree::FindByRoutingID(int process_id, int routing_id) {
dmazzoni0b5d2482014-09-10 19:45:57264 RenderFrameHostImpl* render_frame_host =
265 RenderFrameHostImpl::FromID(process_id, routing_id);
266 if (render_frame_host) {
267 FrameTreeNode* result = render_frame_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50268 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57269 return result;
270 }
271
272 RenderFrameProxyHost* render_frame_proxy_host =
273 RenderFrameProxyHost::FromID(process_id, routing_id);
274 if (render_frame_proxy_host) {
275 FrameTreeNode* result = render_frame_proxy_host->frame_tree_node();
Arthur Sonzognif6785ec2022-12-05 10:11:50276 if (this == &result->frame_tree())
dmazzoni0b5d2482014-09-10 19:45:57277 return result;
278 }
279
creis6a93a812015-04-24 23:13:17280 return nullptr;
281}
282
283FrameTreeNode* FrameTree::FindByName(const std::string& name) {
284 if (name.empty())
Paul Semel3e241042022-10-11 12:57:31285 return &root_;
creis6a93a812015-04-24 23:13:17286
dcheng57e39e22016-01-21 00:25:38287 for (FrameTreeNode* node : Nodes()) {
288 if (node->frame_name() == name)
289 return node;
[email protected]9b159a52013-10-03 17:24:55290 }
dcheng57e39e22016-01-21 00:25:38291
292 return nullptr;
293}
294
295FrameTree::NodeRange FrameTree::Nodes() {
Alex Moshchuk27caae82017-09-11 23:11:18296 return NodesExceptSubtree(nullptr);
dcheng57e39e22016-01-21 00:25:38297}
298
kenrb61b6c252016-03-22 17:37:15299FrameTree::NodeRange FrameTree::SubtreeNodes(FrameTreeNode* subtree_root) {
Kevin McNee5f594382021-05-06 23:18:23300 return NodeRange({subtree_root}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48301 /*should_descend_into_inner_trees=*/false,
302 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee5f594382021-05-06 23:18:23303}
304
Kevin McNee53f0b2d2021-11-02 18:00:45305FrameTree::NodeRange FrameTree::NodesIncludingInnerTreeNodes() {
Paul Semel3e241042022-10-11 12:57:31306 return NodeRange({&root_}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48307 /*should_descend_into_inner_trees=*/true,
308 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee53f0b2d2021-11-02 18:00:45309}
310
Sreeja Kamishettyd64b993d2022-02-14 12:04:42311std::vector<FrameTreeNode*> FrameTree::CollectNodesForIsLoading() {
312 FrameTree::NodeRange node_range = NodesIncludingInnerTreeNodes();
313 FrameTree::NodeIterator node_iter = node_range.begin();
314 std::vector<FrameTreeNode*> nodes;
315
316 DCHECK(node_iter != node_range.end());
Arthur Sonzognif6785ec2022-12-05 10:11:50317 FrameTree* root_loading_tree = root_.frame_tree().LoadingTree();
Sreeja Kamishettyd64b993d2022-02-14 12:04:42318 while (node_iter != node_range.end()) {
319 // Skip over frame trees and children which belong to inner web contents
320 // i.e., when nodes doesn't point to the same loading frame tree.
Arthur Sonzognif6785ec2022-12-05 10:11:50321 if ((*node_iter)->frame_tree().LoadingTree() != root_loading_tree) {
Sreeja Kamishettyd64b993d2022-02-14 12:04:42322 node_iter.AdvanceSkippingChildren();
323 } else {
324 nodes.push_back(*node_iter);
325 ++node_iter;
326 }
327 }
328 return nodes;
329}
330
Kevin McNee5f594382021-05-06 23:18:23331FrameTree::NodeRange FrameTree::SubtreeAndInnerTreeNodes(
Dave Tapuskadda303d2022-10-04 16:56:48332 RenderFrameHostImpl* parent,
333 bool include_delegate_nodes_for_inner_frame_trees) {
Kevin McNee5f594382021-05-06 23:18:23334 std::vector<FrameTreeNode*> starting_nodes;
335 starting_nodes.reserve(parent->child_count());
336 for (size_t i = 0; i < parent->child_count(); ++i) {
337 FrameTreeNode* child = parent->child_at(i);
338 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:48339 if (inner_tree_main_ftn) {
340 if (include_delegate_nodes_for_inner_frame_trees)
341 starting_nodes.push_back(child);
342 starting_nodes.push_back(inner_tree_main_ftn);
343 } else {
344 starting_nodes.push_back(child);
345 }
Kevin McNee5f594382021-05-06 23:18:23346 }
347 const std::vector<FrameTreeNode*> unattached_owned_nodes =
348 parent->delegate()->GetUnattachedOwnedNodes(parent);
349 starting_nodes.insert(starting_nodes.end(), unattached_owned_nodes.begin(),
350 unattached_owned_nodes.end());
351 return NodeRange(starting_nodes, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48352 /* should_descend_into_inner_trees */ true,
353 include_delegate_nodes_for_inner_frame_trees);
kenrb61b6c252016-03-22 17:37:15354}
355
Alex Moshchuk27caae82017-09-11 23:11:18356FrameTree::NodeRange FrameTree::NodesExceptSubtree(FrameTreeNode* node) {
Paul Semel3e241042022-10-11 12:57:31357 return NodeRange({&root_}, node, /*should_descend_into_inner_trees=*/false,
Dave Tapuskadda303d2022-10-04 16:56:48358 /*include_delegate_nodes_for_inner_frame_trees=*/false);
[email protected]9b159a52013-10-03 17:24:55359}
360
Sreeja Kamishettyd64b993d2022-02-14 12:04:42361FrameTree* FrameTree::LoadingTree() {
362 // We return the delegate's loading frame tree to infer loading related
363 // states.
364 return delegate_->LoadingTree();
365}
366
Lucas Furukawa Gadani99125822019-01-03 15:41:49367FrameTreeNode* FrameTree::AddFrame(
Alexander Timin381e7e182020-04-28 19:04:03368 RenderFrameHostImpl* parent,
Balazs Engedyba034e72017-10-27 22:26:28369 int process_id,
370 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18371 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
Oksana Zhuravlovafee097c2019-07-26 17:01:30372 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
373 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30374 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49375 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
376 associated_interface_provider_receiver,
Antonio Gomes9d5c1ef2020-04-30 20:56:41377 blink::mojom::TreeScopeType scope,
Balazs Engedyba034e72017-10-27 22:26:28378 const std::string& frame_name,
379 const std::string& frame_unique_name,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45380 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04381 const blink::LocalFrameToken& frame_token,
Balazs Engedyba034e72017-10-27 22:26:28382 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34383 const blink::DocumentToken& document_token,
Luna Luc3fdacdf2017-11-08 04:48:53384 const blink::FramePolicy& frame_policy,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54385 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Ehsan Karamad192a8da2018-10-21 03:48:08386 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41387 blink::FrameOwnerElementType owner_type,
388 bool is_dummy_frame_for_inner_tree) {
nick8814e652015-12-18 01:44:12389 CHECK_NE(new_routing_id, MSG_ROUTING_NONE);
Dominic Farolino4bc10ee2021-08-31 00:37:36390 // Normally this path is for blink adding a child local frame. But both
391 // portals and fenced frames add a dummy child frame that never gets a
392 // corresponding RenderFrameImpl in any renderer process, and therefore its
393 // `frame_remote` is invalid. Also its RenderFrameHostImpl is exempt from
394 // having `RenderFrameCreated()` called on it (see later in this method, as
395 // well as `WebContentsObserverConsistencyChecker::RenderFrameHostChanged()`).
Kevin McNee43fe8292021-10-04 22:59:41396 DCHECK_NE(frame_remote.is_valid(), is_dummy_frame_for_inner_tree);
397 DCHECK_NE(browser_interface_broker_receiver.is_valid(),
398 is_dummy_frame_for_inner_tree);
Dominic Farolino12e06d72022-08-05 02:29:49399 DCHECK_NE(associated_interface_provider_receiver.is_valid(),
400 is_dummy_frame_for_inner_tree);
nick8814e652015-12-18 01:44:12401
dgroganfb22f9a2014-10-20 21:32:32402 // A child frame always starts with an initial empty document, which means
403 // it is in the same SiteInstance as the parent frame. Ensure that the process
404 // which requested a child frame to be added is the same as the process of the
405 // parent node.
Dominic Farolino6caf3032021-10-20 03:50:08406 CHECK_EQ(parent->GetProcess()->GetID(), process_id);
dgroganfb22f9a2014-10-20 21:32:32407
Danil Somsikov259aa65f2022-11-11 20:49:44408 std::unique_ptr<FrameTreeNode> new_node = base::WrapUnique(
Arthur Sonzognif6785ec2022-12-05 10:11:50409 new FrameTreeNode(*this, parent, scope, is_created_by_script,
Danil Somsikov259aa65f2022-11-11 20:49:44410 frame_owner_properties, owner_type, frame_policy));
iclelland098da752017-06-28 13:46:50411
412 // Set sandbox flags and container policy and make them effective immediately,
Charlie Hu5130d25e2021-03-05 21:53:39413 // since initial sandbox flags and permissions policy should apply to the
414 // initial empty document in the frame. This needs to happen before the call
415 // to AddChild so that the effective policy is sent to any newly-created
Dave Tapuska2402595f2022-08-03 16:24:21416 // `blink::RemoteFrame` objects when the RenderFrameHost is created.
Charlie Hu5ffc0152019-12-06 15:59:53417 // SetPendingFramePolicy is necessary here because next navigation on this
418 // frame will need the value of pending frame policy instead of effective
419 // frame policy.
Ian Clellandcdc4f312017-10-13 22:24:12420 new_node->SetPendingFramePolicy(frame_policy);
iclelland098da752017-06-28 13:46:50421
Shubhie Panickerddf2a4e2018-03-06 00:09:06422 if (was_discarded)
423 new_node->set_was_discarded();
424
iclelland098da752017-06-28 13:46:50425 // Add the new node to the FrameTree, creating the RenderFrameHost.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40426 FrameTreeNode* added_node = parent->AddChild(
427 std::move(new_node), new_routing_id, std::move(frame_remote), frame_token,
Danil Somsikov259aa65f2022-11-11 20:49:44428 document_token, devtools_frame_token, frame_policy, frame_name,
429 frame_unique_name);
nick8814e652015-12-18 01:44:12430
Garrett Tanzer34cb92fe2022-09-28 17:50:54431 added_node->SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30432
Kevin McNee43fe8292021-10-04 22:59:41433 if (browser_interface_broker_receiver.is_valid()) {
434 added_node->current_frame_host()->BindBrowserInterfaceBrokerReceiver(
435 std::move(browser_interface_broker_receiver));
436 }
Oksana Zhuravlovafee097c2019-07-26 17:01:30437
Antonio Sartoridb967c52021-01-20 09:54:30438 if (policy_container_bind_params) {
Antonio Sartoria1fd1432020-11-25 09:10:20439 added_node->current_frame_host()->policy_container_host()->Bind(
Antonio Sartoridb967c52021-01-20 09:54:30440 std::move(policy_container_bind_params));
Antonio Sartoria1fd1432020-11-25 09:10:20441 }
442
Dominic Farolino12e06d72022-08-05 02:29:49443 if (associated_interface_provider_receiver.is_valid()) {
444 added_node->current_frame_host()->BindAssociatedInterfaceProviderReceiver(
445 std::move(associated_interface_provider_receiver));
446 }
447
nasko03ecfad2016-08-02 00:54:06448 // The last committed NavigationEntry may have a FrameNavigationEntry with the
449 // same |frame_unique_name|, since we don't remove FrameNavigationEntries if
450 // their frames are deleted. If there is a stale one, remove it to avoid
451 // conflicts on future updates.
Fergal Daly09d6c762020-05-29 02:05:18452 NavigationEntryImpl* last_committed_entry = static_cast<NavigationEntryImpl*>(
Carlos Caballero40b0efd2021-01-26 11:55:00453 navigator_.controller().GetLastCommittedEntry());
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45454 if (last_committed_entry) {
455 last_committed_entry->RemoveEntryForFrame(
456 added_node, /* only_if_different_position = */ true);
457 }
nasko03ecfad2016-08-02 00:54:06458
nick8814e652015-12-18 01:44:12459 // Now that the new node is part of the FrameTree and has a RenderFrameHost,
460 // we can announce the creation of the initial RenderFrame which already
461 // exists in the renderer process.
Sreeja Kamishetty5b699622021-01-22 12:54:08462 // For consistency with navigating to a new RenderFrameHost case, we dispatch
463 // RenderFrameCreated before RenderFrameHostChanged.
Kevin McNee43fe8292021-10-04 22:59:41464 if (!is_dummy_frame_for_inner_tree) {
Dominic Farolino4bc10ee2021-08-31 00:37:36465 // The outer dummy FrameTreeNode for both portals and fenced frames does not
466 // have a live RenderFrame in the renderer process.
Fergal Dalyf9ea35c2020-12-11 15:26:01467 added_node->current_frame_host()->RenderFrameCreated();
Ehsan Karamad44fc72112019-02-26 18:15:47468 }
Sreeja Kamishetty5b699622021-01-22 12:54:08469
470 // Notify the delegate of the creation of the current RenderFrameHost.
471 // This is only for subframes, as the main frame case is taken care of by
472 // WebContentsImpl::Init.
473 manager_delegate_->NotifySwappedFromRenderManager(
Dave Tapuskae45d6fd2021-09-29 17:03:59474 nullptr, added_node->current_frame_host());
Lucas Furukawa Gadani99125822019-01-03 15:41:49475 return added_node;
[email protected]9b159a52013-10-03 17:24:55476}
477
[email protected]58faf942014-02-20 21:03:58478void FrameTree::RemoveFrame(FrameTreeNode* child) {
Alexander Timin381e7e182020-04-28 19:04:03479 RenderFrameHostImpl* parent = child->parent();
[email protected]58faf942014-02-20 21:03:58480 if (!parent) {
481 NOTREACHED() << "Unexpected RemoveFrame call for main frame.";
482 return;
[email protected]9b159a52013-10-03 17:24:55483 }
484
Alexander Timin381e7e182020-04-28 19:04:03485 parent->RemoveChild(child);
[email protected]9b159a52013-10-03 17:24:55486}
487
Harkiran Bolaria2912a6b32022-02-22 16:43:45488void FrameTree::CreateProxiesForSiteInstance(
489 FrameTreeNode* source,
Charlie Reis37be2682023-01-10 17:04:47490 SiteInstanceImpl* site_instance,
Harkiran Bolaria2912a6b32022-02-22 16:43:45491 const scoped_refptr<BrowsingContextState>&
492 source_new_browsing_context_state) {
Charlie Reis37be2682023-01-10 17:04:47493 SiteInstanceGroup* group = site_instance->group();
naskob3041b98a42016-03-12 04:43:06494 // Create the RenderFrameProxyHost for the new SiteInstance.
alexmos58729042015-06-18 23:20:00495 if (!source || !source->IsMainFrame()) {
Sharon Yang57bde122022-03-01 20:01:12496 RenderViewHostImpl* render_view_host = GetRenderViewHost(group).get();
arthursonzognic5be3842019-07-09 11:49:14497 if (render_view_host) {
Sharon Yang57bde122022-03-01 20:01:12498 root()->render_manager()->EnsureRenderViewInitialized(render_view_host,
499 group);
arthursonzognic5be3842019-07-09 11:49:14500 } else {
Harkiran Bolaria2912a6b32022-02-22 16:43:45501 // Due to the check above, we are creating either an opener proxy (when
502 // source is null) or a main frame proxy due to a subframe navigation
503 // (when source is not a main frame). In the former case, we should use
504 // root's current BrowsingContextState, while in the latter case we should
505 // use BrowsingContextState from the main RenderFrameHost of the subframe
Dave Tapuska2cf1f532022-08-10 15:30:49506 // being navigated. We want to ensure that the `blink::WebView` is created
507 // in the right SiteInstance if it doesn't exist, before creating the
508 // other proxies; if the `blink::WebView` doesn't exist, the only way to
509 // do this is to also create a proxy for the main frame as well.
Harkiran Bolaria2912a6b32022-02-22 16:43:45510 root()->render_manager()->CreateRenderFrameProxy(
511 site_instance,
512 source ? source->parent()->GetMainFrame()->browsing_context_state()
513 : root()->current_frame_host()->browsing_context_state());
[email protected]82307f6b2014-08-07 03:30:12514 }
515 }
516
Alex Moshchuk1226b152019-11-08 18:23:45517 // Check whether we're in an inner delegate and |site_instance| corresponds
518 // to the outer delegate. Subframe proxies aren't needed if this is the
519 // case.
520 bool is_site_instance_for_outer_delegate = false;
521 RenderFrameProxyHost* outer_delegate_proxy =
522 root()->render_manager()->GetProxyToOuterDelegate();
523 if (outer_delegate_proxy) {
524 is_site_instance_for_outer_delegate =
525 (site_instance == outer_delegate_proxy->GetSiteInstance());
526 }
527
naskoe6edde32014-10-17 15:36:48528 // Proxies are created in the FrameTree in response to a node navigating to a
529 // new SiteInstance. Since |source|'s navigation will replace the currently
Alex Moshchuk27caae82017-09-11 23:11:18530 // loaded document, the entire subtree under |source| will be removed, and
531 // thus proxy creation is skipped for all nodes in that subtree.
532 //
533 // However, a proxy *is* needed for the |source| node itself. This lets
534 // cross-process navigations in |source| start with a proxy and follow a
535 // remote-to-local transition, which avoids race conditions in cases where
536 // other navigations need to reference |source| before it commits. See
537 // https://crbug.com/756790 for more background. Therefore,
538 // NodesExceptSubtree(source) will include |source| in the nodes traversed
539 // (see NodeIterator::operator++).
540 for (FrameTreeNode* node : NodesExceptSubtree(source)) {
dcheng57e39e22016-01-21 00:25:38541 // If a new frame is created in the current SiteInstance, other frames in
542 // that SiteInstance don't need a proxy for the new frame.
Alex Moshchuk27caae82017-09-11 23:11:18543 RenderFrameHostImpl* current_host =
544 node->render_manager()->current_frame_host();
Charlie Reis37be2682023-01-10 17:04:47545 SiteInstanceImpl* current_instance = current_host->GetSiteInstance();
Alex Moshchuk27caae82017-09-11 23:11:18546 if (current_instance != site_instance) {
547 if (node == source && !current_host->IsRenderFrameLive()) {
Fergal Daly6de62f52020-10-14 01:56:44548 // We don't create a proxy at |source| when the current RenderFrameHost
549 // isn't live. This is because either (1) the speculative
Alex Moshchuk27caae82017-09-11 23:11:18550 // RenderFrameHost will be committed immediately, and the proxy
Fergal Daly6de62f52020-10-14 01:56:44551 // destroyed right away, in GetFrameHostForNavigation, which makes the
552 // races above impossible, or (2) the early commit will be skipped due
553 // to ShouldSkipEarlyCommitPendingForCrashedFrame, in which case the
554 // proxy for |source| *is* needed, but it will be created later in
555 // CreateProxiesForNewRenderFrameHost.
556 //
557 // TODO(fergal): Consider creating a proxy for |source| here rather than
558 // in CreateProxiesForNewRenderFrameHost for case (2) above.
Alex Moshchuk27caae82017-09-11 23:11:18559 continue;
560 }
Alex Moshchuk1226b152019-11-08 18:23:45561
562 // Do not create proxies for subframes in the outer delegate's
563 // SiteInstance, since there is no need to expose these subframes to the
564 // outer delegate. See also comments in CreateProxiesForChildFrame() and
565 // https://crbug.com/1013553.
566 if (!node->IsMainFrame() && is_site_instance_for_outer_delegate)
567 continue;
568
Harkiran Bolaria2912a6b32022-02-22 16:43:45569 // If |node| is the FrameTreeNode being navigated, we use
570 // |browsing_context_state| (as BrowsingContextState might change for
571 // cross-BrowsingInstance navigations). Otherwise, we should use the
572 // |node|'s current BrowsingContextState.
573 node->render_manager()->CreateRenderFrameProxy(
574 site_instance,
575 node == source
576 ? source_new_browsing_context_state
577 : node->current_frame_host()->browsing_context_state());
Alex Moshchuk27caae82017-09-11 23:11:18578 }
dcheng57e39e22016-01-21 00:25:38579 }
[email protected]82307f6b2014-08-07 03:30:12580}
581
[email protected]9b159a52013-10-03 17:24:55582RenderFrameHostImpl* FrameTree::GetMainFrame() const {
Paul Semel3e241042022-10-11 12:57:31583 return root_.current_frame_host();
[email protected]9b159a52013-10-03 17:24:55584}
585
[email protected]9c9343b2014-03-08 02:56:07586FrameTreeNode* FrameTree::GetFocusedFrame() {
587 return FindByID(focused_frame_tree_node_id_);
588}
589
Sharon Yangefe52632022-03-08 23:06:06590void FrameTree::SetFocusedFrame(FrameTreeNode* node,
591 SiteInstanceGroup* source) {
Rakina Zata Amnicca4889e2021-09-28 23:25:55592 CHECK(node->current_frame_host()->IsActive());
alexmos5357efb2015-12-16 21:44:00593 if (node == GetFocusedFrame())
594 return;
595
Sharon Yangefe52632022-03-08 23:06:06596 std::set<SiteInstanceGroup*> frame_tree_groups =
597 CollectSiteInstanceGroups(this);
alexmosb1dc2162015-11-05 00:59:20598
Sharon Yangefe52632022-03-08 23:06:06599 SiteInstanceGroup* current_group =
600 node->current_frame_host()->GetSiteInstance()->group();
alexmos5357efb2015-12-16 21:44:00601
Sharon Yang7424bda2021-11-04 20:27:43602 // Update the focused frame in all other SiteInstanceGroups. If focus changes
603 // to a cross-group frame, this allows the old focused frame's renderer
alexmosb1dc2162015-11-05 00:59:20604 // process to clear focus from that frame and fire blur events. It also
605 // ensures that the latest focused frame is available in all renderers to
606 // compute document.activeElement.
alexmos5357efb2015-12-16 21:44:00607 //
Sharon Yangefe52632022-03-08 23:06:06608 // We do not notify the |source| SiteInstanceGroup because it already knows
609 // the new focused frame (since it initiated the focus change), and we notify
610 // the new focused frame's SiteInstanceGroup (if it differs from |source|)
611 // separately below.
612 for (auto* group : frame_tree_groups) {
613 if (group != source && group != current_group) {
614 RenderFrameProxyHost* proxy = node->current_frame_host()
615 ->browsing_context_state()
616 ->GetRenderFrameProxyHost(group);
Sharon Yangf92842a2022-03-01 18:21:47617
Rakina Zata Amni80700402021-09-20 17:18:03618 if (proxy) {
619 proxy->SetFocusedFrame();
620 } else {
Sharon Yangf92842a2022-03-01 18:21:47621 base::debug::DumpWithoutCrashing();
Rakina Zata Amni80700402021-09-20 17:18:03622 }
alexmosb1dc2162015-11-05 00:59:20623 }
alexmosca2c6ba2015-10-01 21:52:25624 }
625
Sharon Yangefe52632022-03-08 23:06:06626 // If |node| was focused from a cross-group frame (i.e., via
alexmos5357efb2015-12-16 21:44:00627 // window.focus()), tell its RenderFrame that it should focus.
Sharon Yangefe52632022-03-08 23:06:06628 if (current_group != source)
alexmos5357efb2015-12-16 21:44:00629 node->current_frame_host()->SetFocusedFrame();
630
[email protected]9c9343b2014-03-08 02:56:07631 focused_frame_tree_node_id_ = node->frame_tree_node_id();
alexmos21acae52015-11-07 01:04:43632 node->DidFocus();
dmazzonif27bf892016-03-10 15:51:55633
634 // The accessibility tree data for the root of the frame tree keeps
635 // track of the focused frame too, so update that every time the
636 // focused frame changes.
Kevin McNee582a7d62021-10-12 21:42:22637 root()
638 ->current_frame_host()
639 ->GetOutermostMainFrameOrEmbedder()
640 ->UpdateAXTreeData();
[email protected]9c9343b2014-03-08 02:56:07641}
642
arthursonzognic5be3842019-07-09 11:49:14643scoped_refptr<RenderViewHostImpl> FrameTree::CreateRenderViewHost(
Charlie Reis37be2682023-01-10 17:04:47644 SiteInstanceImpl* site_instance,
avib7348942015-12-25 20:57:10645 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58646 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57647 scoped_refptr<BrowsingContextState> main_browsing_context_state,
648 CreateRenderViewHostCase create_case) {
Harkiran Bolaria57e2b062022-03-14 10:27:58649 if (main_browsing_context_state) {
650 DCHECK(main_browsing_context_state->is_main_frame());
651 }
dcheng29f5a6c2015-08-31 21:43:27652 RenderViewHostImpl* rvh =
653 static_cast<RenderViewHostImpl*>(RenderViewHostFactory::Create(
Charlie Reis37be2682023-01-10 17:04:47654 this, site_instance->group(),
Sharon Yangcadd00b82022-05-13 19:16:33655 site_instance->GetStoragePartitionConfig(), render_view_delegate_,
Dave Tapuska658bfec2022-08-05 14:27:20656 render_widget_delegate_, main_frame_routing_id,
Sharon Yang7ce309e2023-01-19 21:39:57657 renderer_initiated_creation, std::move(main_browsing_context_state),
658 create_case));
659
660 if (ShouldCreateNewHostForAllFrames() &&
661 create_case == CreateRenderViewHostCase::kSpeculative) {
662 set_speculative_render_view_host(rvh->GetWeakPtr());
663 } else {
664 // Register non-speculative RenderViewHosts. If they are speculative, they
665 // will be registered when they become active.
666 RegisterRenderViewHost(rvh->rvh_map_id(), rvh);
667 }
668
arthursonzognic5be3842019-07-09 11:49:14669 return base::WrapRefCounted(rvh);
[email protected]94d0cc12013-12-18 00:07:41670}
671
arthursonzognic5be3842019-07-09 11:49:14672scoped_refptr<RenderViewHostImpl> FrameTree::GetRenderViewHost(
Sharon Yang57bde122022-03-01 20:01:12673 SiteInstanceGroup* group) {
Sharon Yanga2fe85e2022-02-09 21:38:29674 // When called from RenderFrameHostManager::CreateRenderFrameHost, it's
675 // possible that a RenderProcessHost hasn't yet been created, which means
Sharon Yang57bde122022-03-01 20:01:12676 // a SiteInstanceGroup won't have been created yet.
Sharon Yanga2fe85e2022-02-09 21:38:29677 if (!group)
678 return nullptr;
679
680 auto it = render_view_host_map_.find(GetRenderViewHostMapId(group));
arthursonzogni88e54ae2019-04-15 12:57:36681 if (it == render_view_host_map_.end())
682 return nullptr;
alexmos9aa61232016-04-26 21:54:02683
arthursonzognic5be3842019-07-09 11:49:14684 return base::WrapRefCounted(it->second);
[email protected]94d0cc12013-12-18 00:07:41685}
686
Aaron Colwell78b4bde2021-03-16 16:16:09687FrameTree::RenderViewHostMapId FrameTree::GetRenderViewHostMapId(
Sharon Yangc581a0c2021-11-02 18:09:39688 SiteInstanceGroup* site_instance_group) const {
689 return RenderViewHostMapId::FromUnsafeValue(
690 site_instance_group->GetId().value());
Lowell Manners75055a132019-10-11 10:30:29691}
692
Aaron Colwell78b4bde2021-03-16 16:16:09693void FrameTree::RegisterRenderViewHost(RenderViewHostMapId id,
694 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47695 TRACE_EVENT_INSTANT("navigation", "FrameTree::RegisterRenderViewHost",
696 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57697 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09698 CHECK(!base::Contains(render_view_host_map_, id));
699 render_view_host_map_[id] = rvh;
Sharon Yang7ce309e2023-01-19 21:39:57700 rvh->set_is_registered_with_frame_tree(true);
Aaron Colwell78b4bde2021-03-16 16:16:09701}
702
703void FrameTree::UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13704 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47705 TRACE_EVENT_INSTANT("navigation", "FrameTree::UnregisterRenderViewHost",
706 ChromeTrackEvent::kRenderViewHost, *rvh);
Sharon Yang7ce309e2023-01-19 21:39:57707 CHECK(!rvh->is_speculative());
Aaron Colwell78b4bde2021-03-16 16:16:09708 auto it = render_view_host_map_.find(id);
arthursonzogni88e54ae2019-04-15 12:57:36709 CHECK(it != render_view_host_map_.end());
arthursonzognic5be3842019-07-09 11:49:14710 CHECK_EQ(it->second, rvh);
711 render_view_host_map_.erase(it);
Sharon Yang7ce309e2023-01-19 21:39:57712 rvh->set_is_registered_with_frame_tree(false);
[email protected]94d0cc12013-12-18 00:07:41713}
714
arthursonzogni0a2007792019-05-28 21:32:48715void FrameTree::FrameUnloading(FrameTreeNode* frame) {
716 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
717 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
718
719 // Ensure frames that are about to be deleted aren't visible from the other
720 // processes anymore.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40721 frame->GetBrowsingContextStateForSubframe()->ResetProxyHosts();
arthursonzogni0a2007792019-05-28 21:32:48722}
723
dmazzonie950ea232015-03-13 21:39:45724void FrameTree::FrameRemoved(FrameTreeNode* frame) {
nick53d5cbf2015-04-23 22:50:14725 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
thestige62f7382016-11-08 18:31:39726 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
dmazzonie950ea232015-03-13 21:39:45727}
728
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15729double FrameTree::GetLoadProgress() {
Paul Semel3e241042022-10-11 12:57:31730 if (root_.HasNavigation())
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15731 return blink::kInitialLoadProgress;
732
Paul Semel3e241042022-10-11 12:57:31733 return root_.current_frame_host()->GetPage().load_progress();
fdegans1d16355162015-03-26 11:58:34734}
735
Yu Gaoc8c18552022-06-22 14:38:45736bool FrameTree::IsLoadingIncludingInnerFrameTrees() const {
Sreeja Kamishetty15f9944a22022-03-10 10:16:08737 for (const FrameTreeNode* node :
738 const_cast<FrameTree*>(this)->CollectNodesForIsLoading()) {
dcheng57e39e22016-01-21 00:25:38739 if (node->IsLoading())
740 return true;
741 }
742 return false;
fdegans1d16355162015-03-26 11:58:34743}
744
alexmos3fcd0ca2015-10-23 18:18:33745void FrameTree::ReplicatePageFocus(bool is_focused) {
Dave Tapuska53e6ce12021-08-06 16:08:24746 // Focus loss may occur while this FrameTree is being destroyed. Don't
747 // send the message in this case, as the main frame's RenderFrameHost and
748 // other state has already been cleared.
749 if (is_being_destroyed_)
750 return;
Sharon Yangefe52632022-03-08 23:06:06751 std::set<SiteInstanceGroup*> frame_tree_site_instance_groups =
752 CollectSiteInstanceGroups(this);
alexmos3fcd0ca2015-10-23 18:18:33753
Sharon Yangefe52632022-03-08 23:06:06754 // Send the focus update to main frame's proxies in all SiteInstanceGroups of
alexmos3fcd0ca2015-10-23 18:18:33755 // other frames in this FrameTree. Note that the main frame might also know
Sharon Yangefe52632022-03-08 23:06:06756 // about proxies in SiteInstanceGroups for frames in a different FrameTree
757 // (e.g., for window.open), so we can't just iterate over its proxy_hosts_ in
alexmos3fcd0ca2015-10-23 18:18:33758 // RenderFrameHostManager.
Sharon Yangefe52632022-03-08 23:06:06759 for (auto* group : frame_tree_site_instance_groups)
760 SetPageFocus(group, is_focused);
alexmos0d7e0b09b2015-10-29 22:11:48761}
alexmos3fcd0ca2015-10-23 18:18:33762
Sreeja Kamishetty60e47132022-02-08 12:51:53763bool FrameTree::IsPortal() {
764 return delegate_->IsPortal();
765}
766
Sharon Yangefe52632022-03-08 23:06:06767void FrameTree::SetPageFocus(SiteInstanceGroup* group, bool is_focused) {
Paul Semel3e241042022-10-11 12:57:31768 RenderFrameHostManager* root_manager = root_.render_manager();
alexmos0d7e0b09b2015-10-29 22:11:48769
Adithya Srinivasan47731222021-01-22 15:02:42770 // Portal frame tree should not get page focus.
Sreeja Kamishetty60e47132022-02-08 12:51:53771 DCHECK(!IsPortal() || !is_focused);
Adithya Srinivasan47731222021-01-22 15:02:42772
alexmos0d7e0b09b2015-10-29 22:11:48773 // This is only used to set page-level focus in cross-process subframes, and
Sharon Yangefe52632022-03-08 23:06:06774 // requests to set focus in main frame's SiteInstanceGroup are ignored.
775 if (group != root_manager->current_frame_host()->GetSiteInstance()->group()) {
776 RenderFrameProxyHost* proxy = root_manager->current_frame_host()
777 ->browsing_context_state()
778 ->GetRenderFrameProxyHost(group);
Dave Tapuska82b54012022-07-15 23:26:10779 if (proxy->is_render_frame_proxy_live())
780 proxy->GetAssociatedRemoteFrame()->SetPageFocus(is_focused);
alexmos3fcd0ca2015-10-23 18:18:33781 }
782}
783
W. James MacLeanc07dc41b2022-07-25 18:52:16784void FrameTree::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanb70fab82020-05-01 18:51:14785 const url::Origin& previously_visited_origin,
786 NavigationRequest* navigation_request_to_exclude) {
W. James MacLeanc07dc41b2022-07-25 18:52:16787 controller().RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanc6dc86c2021-08-12 13:58:37788 previously_visited_origin);
789
Charlie Reis37be2682023-01-10 17:04:47790 std::unordered_set<SiteInstanceImpl*> matching_site_instances;
W. James MacLeanb70fab82020-05-01 18:51:14791
792 // Be sure to visit all RenderFrameHosts associated with this frame that might
793 // have an origin that could script other frames. We skip RenderFrameHosts
794 // that are in the bfcache, assuming there's no way for a frame to join the
795 // BrowsingInstance of a bfcache RFH while it's in the cache.
796 for (auto* frame_tree_node : SubtreeNodes(root())) {
797 auto* frame_host = frame_tree_node->current_frame_host();
W. James MacLeanb70fab82020-05-01 18:51:14798 if (previously_visited_origin == frame_host->GetLastCommittedOrigin())
799 matching_site_instances.insert(frame_host->GetSiteInstance());
800
801 if (frame_host->HasCommittingNavigationRequestForOrigin(
802 previously_visited_origin, navigation_request_to_exclude)) {
803 matching_site_instances.insert(frame_host->GetSiteInstance());
804 }
805
806 auto* spec_frame_host =
807 frame_tree_node->render_manager()->speculative_frame_host();
808 if (spec_frame_host &&
809 spec_frame_host->HasCommittingNavigationRequestForOrigin(
810 previously_visited_origin, navigation_request_to_exclude)) {
811 matching_site_instances.insert(spec_frame_host->GetSiteInstance());
812 }
813
814 auto* navigation_request = frame_tree_node->navigation_request();
815 if (navigation_request &&
816 navigation_request != navigation_request_to_exclude &&
817 navigation_request->HasCommittingOrigin(previously_visited_origin)) {
818 matching_site_instances.insert(frame_host->GetSiteInstance());
819 }
820 }
821
822 // Update any SiteInstances found to contain |origin|.
823 for (auto* site_instance : matching_site_instances) {
Charlie Reis37be2682023-01-10 17:04:47824 site_instance->RegisterAsDefaultOriginIsolation(previously_visited_origin);
W. James MacLeanb70fab82020-05-01 18:51:14825 }
826}
827
Charlie Reis37be2682023-01-10 17:04:47828void FrameTree::Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00829 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17830 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46831 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44832 const blink::FramePolicy& frame_policy,
833 const base::UnguessableToken& devtools_frame_token) {
Carlos Caballero40b0efd2021-01-26 11:55:00834 // blink::FrameTree::SetName always keeps |unique_name| empty in case of a
835 // main frame - let's do the same thing here.
836 std::string unique_name;
Paul Semel3e241042022-10-11 12:57:31837 root_.render_manager()->InitRoot(main_frame_site_instance,
838 renderer_initiated_creation, frame_policy,
Danil Somsikov259aa65f2022-11-11 20:49:44839 main_frame_name, devtools_frame_token);
Paul Semel3e241042022-10-11 12:57:31840 root_.SetFencedFramePropertiesIfNeeded();
Rakina Zata Amniafd3c6582021-11-30 06:19:17841
842 // The initial empty document should inherit the origin of its opener (the
843 // origin may change after the first commit), except when they are in
Rakina Zata Amni4eb716e2022-04-05 21:32:46844 // different browsing context groups (`renderer_initiated_creation` will be
845 // false), where it should use a new opaque origin.
Rakina Zata Amniafd3c6582021-11-30 06:19:17846 // See also https://crbug.com/932067.
847 //
848 // Note that the origin of the new frame might depend on sandbox flags.
849 // Checking sandbox flags of the new frame should be safe at this point,
850 // because the flags should be already inherited when creating the root node.
Rakina Zata Amni4eb716e2022-04-05 21:32:46851 DCHECK(!renderer_initiated_creation || opener_for_origin);
Paul Semel3e241042022-10-11 12:57:31852 root_.current_frame_host()->SetOriginDependentStateOfNewFrame(
Rakina Zata Amni4eb716e2022-04-05 21:32:46853 renderer_initiated_creation ? opener_for_origin->GetLastCommittedOrigin()
Rakina Zata Amniafd3c6582021-11-30 06:19:17854 : url::Origin());
Rakina Zata Amni2322f4f82022-01-24 13:24:24855
Rakina Zata Amni46087a12022-11-11 08:28:38856 controller().CreateInitialEntry();
Carlos Caballero40b0efd2021-01-26 11:55:00857}
858
Carlos Caballeroede6f8c2021-01-28 11:01:50859void FrameTree::DidAccessInitialMainDocument() {
860 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidAccessInitialDocument");
861 has_accessed_initial_main_document_ = true;
862 controller().DidAccessInitialMainDocument();
863}
864
Carlos Caballero03262522021-02-05 14:49:58865void FrameTree::DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19866 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58867 bool was_previously_loading) {
Carlos Caballero03262522021-02-05 14:49:58868 if (was_previously_loading)
869 return;
870
Yu Gaoc8c18552022-06-22 14:38:45871 root()->render_manager()->SetIsLoading(IsLoadingIncludingInnerFrameTrees());
Nate Chapin9aabf5f2021-11-12 00:31:19872 delegate_->DidStartLoading(&node, should_show_loading_ui);
Carlos Caballero03262522021-02-05 14:49:58873}
874
875void FrameTree::DidStopLoadingNode(FrameTreeNode& node) {
Yu Gaoc8c18552022-06-22 14:38:45876 if (IsLoadingIncludingInnerFrameTrees())
Carlos Caballero03262522021-02-05 14:49:58877 return;
878
879 root()->render_manager()->SetIsLoading(false);
880 delegate_->DidStopLoading();
881}
882
Carlos Caballero03262522021-02-05 14:49:58883void FrameTree::DidCancelLoading() {
884 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidCancelLoading");
885 navigator_.controller().DiscardNonCommittedEntries();
Carlos Caballero03262522021-02-05 14:49:58886}
887
888void FrameTree::StopLoading() {
889 for (FrameTreeNode* node : Nodes())
890 node->StopLoading();
891}
892
Carlos Caballero101ac26b2021-03-24 11:54:05893void FrameTree::Shutdown() {
Dave Tapuskae88286de2021-08-05 19:10:42894 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05895#if DCHECK_IS_ON()
896 DCHECK(!was_shut_down_);
897 was_shut_down_ = true;
898#endif
899
Paul Semel3e241042022-10-11 12:57:31900 RenderFrameHostManager* root_manager = root_.render_manager();
Carlos Caballero101ac26b2021-03-24 11:54:05901
Carlos Caballerodb7a8f6e2021-04-09 18:17:47902 if (!root_manager->current_frame_host()) {
903 // The page has been transferred out during an activation. There is little
904 // left to do.
Carlos Caballerod1c80432021-04-20 08:16:32905 // TODO(https://crbug.com/1199693): If we decide that pending delete RFHs
Carlos Caballerodb7a8f6e2021-04-09 18:17:47906 // need to be moved along during activation replace this line with a DCHECK
907 // that there are no pending delete instances.
908 root_manager->ClearRFHsPendingShutdown();
Paul Semel3e241042022-10-11 12:57:31909 DCHECK(!root_.navigation_request());
Carlos Caballerodb7a8f6e2021-04-09 18:17:47910 DCHECK(!root_manager->speculative_frame_host());
Paul Semel3e241042022-10-11 12:57:31911 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:05912 return;
Carlos Caballerodb7a8f6e2021-04-09 18:17:47913 }
Carlos Caballero101ac26b2021-03-24 11:54:05914
915 for (FrameTreeNode* node : Nodes()) {
916 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
917 // to be shutdown and be deleted as well.
918 node->render_manager()->ClearRFHsPendingShutdown();
Carlos Caballerod1c80432021-04-20 08:16:32919 // TODO(https://crbug.com/1199676): Ban WebUI instance in Prerender pages.
Carlos Caballero101ac26b2021-03-24 11:54:05920 node->render_manager()->ClearWebUIInstances();
921 }
922
923 // Destroy all subframes now. This notifies observers.
924 root_manager->current_frame_host()->ResetChildren();
Harkiran Bolaria0b3bdef02022-03-10 13:04:40925 root_manager->current_frame_host()
926 ->browsing_context_state()
927 ->ResetProxyHosts();
Carlos Caballero101ac26b2021-03-24 11:54:05928
Carlos Caballero101ac26b2021-03-24 11:54:05929 // Manually call the observer methods for the root FrameTreeNode. It is
930 // necessary to manually delete all objects tracking navigations
931 // (NavigationHandle, NavigationRequest) for observers to be properly
932 // notified of these navigations stopping before the WebContents is
933 // destroyed.
934
935 root_manager->current_frame_host()->RenderFrameDeleted();
Rakina Zata Amnif8f2bb62022-11-23 05:54:32936 root_manager->current_frame_host()->ResetOwnedNavigationRequests(
937 NavigationDiscardReason::kWillRemoveFrame);
Carlos Caballero101ac26b2021-03-24 11:54:05938
939 // Do not update state as the FrameTree::Delegate (possibly a WebContents) is
940 // being destroyed.
Paul Semel3e241042022-10-11 12:57:31941 root_.ResetNavigationRequestButKeepState();
Carlos Caballero101ac26b2021-03-24 11:54:05942 if (root_manager->speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:45943 root_manager->DiscardSpeculativeRenderFrameHostForShutdown();
Carlos Caballero101ac26b2021-03-24 11:54:05944 }
945
Alexander Timin8aeee642021-05-12 08:39:33946 // NavigationRequests restoring the page from bfcache have a reference to the
947 // RFHs stored in the cache, so the cache should be cleared after the
948 // navigation request is reset.
949 controller().GetBackForwardCache().Shutdown();
950
Paul Semel3e241042022-10-11 12:57:31951 manager_delegate_->OnFrameTreeNodeDestroyed(&root_);
Carlos Caballero101ac26b2021-03-24 11:54:05952 render_view_delegate_->RenderViewDeleted(
953 root_manager->current_frame_host()->render_view_host());
954}
955
Dave Tapuskad8b0530f2021-10-19 15:12:31956base::SafeRef<FrameTree> FrameTree::GetSafeRef() {
957 return weak_ptr_factory_.GetSafeRef();
958}
959
Dave Tapuska54c76a032021-10-27 22:10:42960void FrameTree::FocusOuterFrameTrees() {
961 OPTIONAL_TRACE_EVENT0("content", "FrameTree::FocusOuterFrameTrees");
962
963 FrameTree* frame_tree_to_focus = this;
964 while (true) {
965 FrameTreeNode* outer_node = FrameTreeNode::GloballyFindByID(
966 frame_tree_to_focus->delegate()->GetOuterDelegateFrameTreeNodeId());
967 if (!outer_node || !outer_node->current_frame_host()->IsActive()) {
968 // Don't set focus on an inactive FrameTreeNode.
969 return;
970 }
Arthur Sonzognif6785ec2022-12-05 10:11:50971 outer_node->frame_tree().SetFocusedFrame(outer_node, nullptr);
Dave Tapuska54c76a032021-10-27 22:10:42972
973 // For a browser initiated focus change, let embedding renderer know of the
974 // change. Otherwise, if the currently focused element is just across a
975 // process boundary in focus order, it will not be possible to move across
976 // that boundary. This is because the target element will already be focused
977 // (that renderer was not notified) and drop the event.
978 if (auto* proxy_to_outer_delegate = frame_tree_to_focus->root()
979 ->render_manager()
980 ->GetProxyToOuterDelegate()) {
981 proxy_to_outer_delegate->SetFocusedFrame();
982 }
Arthur Sonzognif6785ec2022-12-05 10:11:50983 frame_tree_to_focus = &outer_node->frame_tree();
Dave Tapuska54c76a032021-10-27 22:10:42984 }
985}
986
Ari Chivukula5d15efb2023-01-21 04:33:52987void FrameTree::RegisterOriginForUnpartitionedSessionStorageAccess(
988 const url::Origin& origin) {
989 if (origin.opaque()) {
990 return;
991 }
992 unpartitioned_session_storage_origins_.insert(origin);
993}
994
Ari Chivukula9a96a582023-02-03 21:02:29995void FrameTree::UnregisterOriginForUnpartitionedSessionStorageAccess(
996 const url::Origin& origin) {
997 unpartitioned_session_storage_origins_.erase(origin);
998}
999
Ari Chivukula5d15efb2023-01-21 04:33:521000const blink::StorageKey FrameTree::GetSessionStorageKey(
1001 const blink::StorageKey& storage_key) {
1002 if (unpartitioned_session_storage_origins_.find(storage_key.origin()) !=
1003 unpartitioned_session_storage_origins_.end()) {
1004 // If the storage key matches a participating origin we need to return the
1005 // first-party version for use in binding session storage.
1006 return blink::StorageKey(storage_key.origin());
1007 }
1008 return storage_key;
1009}
1010
[email protected]9b159a52013-10-03 17:24:551011} // namespace content