blob: cab0dae40fd55d8b5b22376a94da0c12db7855c7 [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
13#include "base/bind.h"
14#include "base/callback.h"
Lei Zhangde197672021-04-29 08:11:2415#include "base/containers/contains.h"
Rakina Zata Amni80700402021-09-20 17:18:0316#include "base/debug/dump_without_crashing.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/frame_tree_node.h"
24#include "content/browser/renderer_host/navigation_controller_impl.h"
25#include "content/browser/renderer_host/navigation_entry_impl.h"
26#include "content/browser/renderer_host/navigation_request.h"
27#include "content/browser/renderer_host/navigator.h"
28#include "content/browser/renderer_host/navigator_delegate.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1529#include "content/browser/renderer_host/page_impl.h"
Kevin McNee5f594382021-05-06 23:18:2330#include "content/browser/renderer_host/render_frame_host_delegate.h"
danakjc492bf82020-09-09 20:02:4431#include "content/browser/renderer_host/render_frame_host_factory.h"
32#include "content/browser/renderer_host/render_frame_host_impl.h"
33#include "content/browser/renderer_host/render_frame_proxy_host.h"
Carlos Caballero101ac26b2021-03-24 11:54:0534#include "content/browser/renderer_host/render_view_host_delegate.h"
[email protected]94d0cc12013-12-18 00:07:4135#include "content/browser/renderer_host/render_view_host_factory.h"
36#include "content/browser/renderer_host/render_view_host_impl.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"
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:5442#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
[email protected]9b159a52013-10-03 17:24:5543
44namespace content {
45
46namespace {
[email protected]20edca72014-08-14 10:27:5347
Rakina Zata Amni4b1968d2021-09-09 03:29:4748using perfetto::protos::pbzero::ChromeTrackEvent;
49
Sharon Yangefe52632022-03-08 23:06:0650// Helper function to collect SiteInstanceGroups involved in rendering a single
51// FrameTree (which is a subset of SiteInstanceGroups in main frame's
52// proxy_hosts_ because of openers).
53std::set<SiteInstanceGroup*> CollectSiteInstanceGroups(FrameTree* tree) {
54 std::set<SiteInstanceGroup*> groups;
dcheng57e39e22016-01-21 00:25:3855 for (FrameTreeNode* node : tree->Nodes())
Sharon Yangefe52632022-03-08 23:06:0656 groups.insert(node->current_frame_host()->GetSiteInstance()->group());
57 return groups;
alexmos3fcd0ca2015-10-23 18:18:3358}
59
Kevin McNee5f594382021-05-06 23:18:2360// If |node| is the placeholder FrameTreeNode for an embedded frame tree,
61// returns the inner tree's main frame's FrameTreeNode. Otherwise, returns null.
62FrameTreeNode* GetInnerTreeMainFrameNode(FrameTreeNode* node) {
Dominic Farolino377edb302021-07-29 05:57:1863 FrameTreeNode* inner_main_frame_tree_node = FrameTreeNode::GloballyFindByID(
64 node->current_frame_host()->inner_tree_main_frame_tree_node_id());
Kevin McNee5f594382021-05-06 23:18:2365
Kevin McNeed459ad42022-09-12 19:24:5966 if (inner_main_frame_tree_node) {
67 DCHECK_NE(node->frame_tree(), inner_main_frame_tree_node->frame_tree());
Kevin McNee5f594382021-05-06 23:18:2368 }
69
Kevin McNeed459ad42022-09-12 19:24:5970 return inner_main_frame_tree_node;
Kevin McNee5f594382021-05-06 23:18:2371}
72
[email protected]9b159a52013-10-03 17:24:5573} // namespace
74
vmpstr33895d992016-02-24 20:55:2175FrameTree::NodeIterator::NodeIterator(const NodeIterator& other) = default;
76
Fergal Daly55b6d722020-09-11 07:56:3377FrameTree::NodeIterator::~NodeIterator() = default;
dcheng57e39e22016-01-21 00:25:3878
79FrameTree::NodeIterator& FrameTree::NodeIterator::operator++() {
Alex Moshchuk27caae82017-09-11 23:11:1880 if (current_node_ != root_of_subtree_to_skip_) {
Jayson Adams4db0bfe22021-07-15 19:24:0781 // Reserve enough space in the queue to accommodate the nodes we're
82 // going to add, to avoid repeated resize calls.
83 queue_.reserve(queue_.size() + current_node_->child_count());
84
Alex Moshchuk27caae82017-09-11 23:11:1885 for (size_t i = 0; i < current_node_->child_count(); ++i) {
86 FrameTreeNode* child = current_node_->child_at(i);
Kevin McNee5f594382021-05-06 23:18:2387 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:4888 if (should_descend_into_inner_trees_ && inner_tree_main_ftn) {
89 if (include_delegate_nodes_for_inner_frame_trees_)
90 queue_.push_back(child);
91 queue_.push_back(inner_tree_main_ftn);
92 } else {
93 queue_.push_back(child);
94 }
Kevin McNee5f594382021-05-06 23:18:2395 }
96
97 if (should_descend_into_inner_trees_) {
Jayson Adams4db0bfe22021-07-15 19:24:0798 auto unattached_nodes =
99 current_node_->current_frame_host()
100 ->delegate()
101 ->GetUnattachedOwnedNodes(current_node_->current_frame_host());
102
103 // Reserve enough space in the queue to accommodate the nodes we're
104 // going to add.
105 queue_.reserve(queue_.size() + unattached_nodes.size());
106
107 for (auto* unattached_node : unattached_nodes) {
108 queue_.push_back(unattached_node);
Kevin McNee5f594382021-05-06 23:18:23109 }
Alex Moshchuk27caae82017-09-11 23:11:18110 }
dcheng57e39e22016-01-21 00:25:38111 }
112
Kevin McNee5f594382021-05-06 23:18:23113 AdvanceNode();
114 return *this;
115}
dcheng57e39e22016-01-21 00:25:38116
Kevin McNee5f594382021-05-06 23:18:23117FrameTree::NodeIterator& FrameTree::NodeIterator::AdvanceSkippingChildren() {
118 AdvanceNode();
dcheng57e39e22016-01-21 00:25:38119 return *this;
120}
121
122bool FrameTree::NodeIterator::operator==(const NodeIterator& rhs) const {
123 return current_node_ == rhs.current_node_;
124}
125
Kevin McNee5f594382021-05-06 23:18:23126void FrameTree::NodeIterator::AdvanceNode() {
127 if (!queue_.empty()) {
128 current_node_ = queue_.front();
Jayson Adams4db0bfe22021-07-15 19:24:07129 queue_.pop_front();
Kevin McNee5f594382021-05-06 23:18:23130 } else {
131 current_node_ = nullptr;
132 }
133}
134
135FrameTree::NodeIterator::NodeIterator(
136 const std::vector<FrameTreeNode*>& starting_nodes,
137 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48138 bool should_descend_into_inner_trees,
139 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23140 : current_node_(nullptr),
141 root_of_subtree_to_skip_(root_of_subtree_to_skip),
142 should_descend_into_inner_trees_(should_descend_into_inner_trees),
Dave Tapuskadda303d2022-10-04 16:56:48143 include_delegate_nodes_for_inner_frame_trees_(
144 include_delegate_nodes_for_inner_frame_trees),
Jayson Adams4db0bfe22021-07-15 19:24:07145 queue_(starting_nodes.begin(), starting_nodes.end()) {
Dave Tapuskadda303d2022-10-04 16:56:48146 // If `include_delegate_nodes_for_inner_frame_trees_` is true then
147 // `should_descend_into_inner_trees_` must be true.
148 DCHECK(!include_delegate_nodes_for_inner_frame_trees_ ||
149 should_descend_into_inner_trees_);
Kevin McNee5f594382021-05-06 23:18:23150 AdvanceNode();
151}
dcheng57e39e22016-01-21 00:25:38152
153FrameTree::NodeIterator FrameTree::NodeRange::begin() {
W. James MacLeance3176d2019-10-18 04:01:45154 // We shouldn't be attempting a frame tree traversal while the tree is
Kevin McNee5f594382021-05-06 23:18:23155 // being constructed or destructed.
Peter Kastingd5685942022-09-02 17:52:17156 DCHECK(base::ranges::all_of(starting_nodes_, [](FrameTreeNode* ftn) {
157 return ftn->current_frame_host();
158 }));
Kevin McNee5f594382021-05-06 23:18:23159
160 return NodeIterator(starting_nodes_, root_of_subtree_to_skip_,
Dave Tapuskadda303d2022-10-04 16:56:48161 should_descend_into_inner_trees_,
162 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38163}
164
165FrameTree::NodeIterator FrameTree::NodeRange::end() {
Dave Tapuskadda303d2022-10-04 16:56:48166 return NodeIterator({}, nullptr, should_descend_into_inner_trees_,
167 include_delegate_nodes_for_inner_frame_trees_);
dcheng57e39e22016-01-21 00:25:38168}
169
Kevin McNee5f594382021-05-06 23:18:23170FrameTree::NodeRange::NodeRange(
171 const std::vector<FrameTreeNode*>& starting_nodes,
172 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48173 bool should_descend_into_inner_trees,
174 bool include_delegate_nodes_for_inner_frame_trees)
Kevin McNee5f594382021-05-06 23:18:23175 : starting_nodes_(starting_nodes),
176 root_of_subtree_to_skip_(root_of_subtree_to_skip),
Dave Tapuskadda303d2022-10-04 16:56:48177 should_descend_into_inner_trees_(should_descend_into_inner_trees),
178 include_delegate_nodes_for_inner_frame_trees_(
179 include_delegate_nodes_for_inner_frame_trees) {}
Kevin McNee5f594382021-05-06 23:18:23180
181FrameTree::NodeRange::NodeRange(const NodeRange&) = default;
182FrameTree::NodeRange::~NodeRange() = default;
dcheng57e39e22016-01-21 00:25:38183
Carlos Caballero40b0efd2021-01-26 11:55:00184FrameTree::FrameTree(
185 BrowserContext* browser_context,
Carlos Caballero03262522021-02-05 14:49:58186 Delegate* delegate,
Carlos Caballero40b0efd2021-01-26 11:55:00187 NavigationControllerDelegate* navigation_controller_delegate,
188 NavigatorDelegate* navigator_delegate,
189 RenderFrameHostDelegate* render_frame_delegate,
190 RenderViewHostDelegate* render_view_delegate,
191 RenderWidgetHostDelegate* render_widget_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59192 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26193 PageDelegate* page_delegate,
Dave Tapuska144570102022-08-02 19:28:27194 Type type,
195 const base::UnguessableToken& devtools_frame_token)
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),
[email protected]94d0cc12013-12-18 00:07:41207 root_(new FrameTreeNode(this,
xiaochengh98488162016-05-19 15:17:59208 nullptr,
dcheng860817a2015-05-22 03:16:56209 // The top-level frame must always be in a
210 // document scope.
Antonio Gomes9d5c1ef2020-04-30 20:56:41211 blink::mojom::TreeScopeType::kDocument,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45212 false,
Dave Tapuska144570102022-08-02 19:28:27213 devtools_frame_token,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54214 blink::mojom::FrameOwnerProperties(),
Kevin McNee43fe8292021-10-04 22:59:41215 blink::FrameOwnerElementType::kNone,
Dominic Farolino08662c82021-06-11 07:36:34216 blink::FramePolicy())),
thestige62f7382016-11-08 18:31:39217 focused_frame_tree_node_id_(FrameTreeNode::kFrameTreeNodeInvalidId),
David Bokanc3fb5fa2022-07-04 14:55:31218 load_progress_(0.0),
219 fenced_frames_impl_(
220 blink::features::IsFencedFramesEnabled()
221 ? absl::optional<blink::features::FencedFramesImplementationType>(
222 blink::features::kFencedFramesImplementationTypeParam.Get())
223 : absl::nullopt) {}
[email protected]9b159a52013-10-03 17:24:55224
225FrameTree::~FrameTree() {
Takashi Toyoshimaea534ef22021-07-21 03:27:59226 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05227#if DCHECK_IS_ON()
228 DCHECK(was_shut_down_);
229#endif
nick70b782d2015-10-06 17:37:04230 delete root_;
231 root_ = nullptr;
[email protected]9b159a52013-10-03 17:24:55232}
233
vishal.b782eb5d2015-04-29 12:22:57234FrameTreeNode* FrameTree::FindByID(int frame_tree_node_id) {
dcheng57e39e22016-01-21 00:25:38235 for (FrameTreeNode* node : Nodes()) {
236 if (node->frame_tree_node_id() == frame_tree_node_id)
237 return node;
238 }
239 return nullptr;
[email protected]9b159a52013-10-03 17:24:55240}
241
nasko479ea5a2015-02-14 00:03:04242FrameTreeNode* FrameTree::FindByRoutingID(int process_id, int routing_id) {
dmazzoni0b5d2482014-09-10 19:45:57243 RenderFrameHostImpl* render_frame_host =
244 RenderFrameHostImpl::FromID(process_id, routing_id);
245 if (render_frame_host) {
246 FrameTreeNode* result = render_frame_host->frame_tree_node();
247 if (this == result->frame_tree())
248 return result;
249 }
250
251 RenderFrameProxyHost* render_frame_proxy_host =
252 RenderFrameProxyHost::FromID(process_id, routing_id);
253 if (render_frame_proxy_host) {
254 FrameTreeNode* result = render_frame_proxy_host->frame_tree_node();
255 if (this == result->frame_tree())
256 return result;
257 }
258
creis6a93a812015-04-24 23:13:17259 return nullptr;
260}
261
262FrameTreeNode* FrameTree::FindByName(const std::string& name) {
263 if (name.empty())
nick70b782d2015-10-06 17:37:04264 return root_;
creis6a93a812015-04-24 23:13:17265
dcheng57e39e22016-01-21 00:25:38266 for (FrameTreeNode* node : Nodes()) {
267 if (node->frame_name() == name)
268 return node;
[email protected]9b159a52013-10-03 17:24:55269 }
dcheng57e39e22016-01-21 00:25:38270
271 return nullptr;
272}
273
274FrameTree::NodeRange FrameTree::Nodes() {
Alex Moshchuk27caae82017-09-11 23:11:18275 return NodesExceptSubtree(nullptr);
dcheng57e39e22016-01-21 00:25:38276}
277
kenrb61b6c252016-03-22 17:37:15278FrameTree::NodeRange FrameTree::SubtreeNodes(FrameTreeNode* subtree_root) {
Kevin McNee5f594382021-05-06 23:18:23279 return NodeRange({subtree_root}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48280 /*should_descend_into_inner_trees=*/false,
281 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee5f594382021-05-06 23:18:23282}
283
Kevin McNee53f0b2d2021-11-02 18:00:45284FrameTree::NodeRange FrameTree::NodesIncludingInnerTreeNodes() {
285 return NodeRange({root_}, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48286 /*should_descend_into_inner_trees=*/true,
287 /*include_delegate_nodes_for_inner_frame_trees=*/false);
Kevin McNee53f0b2d2021-11-02 18:00:45288}
289
Sreeja Kamishettyd64b993d2022-02-14 12:04:42290std::vector<FrameTreeNode*> FrameTree::CollectNodesForIsLoading() {
291 FrameTree::NodeRange node_range = NodesIncludingInnerTreeNodes();
292 FrameTree::NodeIterator node_iter = node_range.begin();
293 std::vector<FrameTreeNode*> nodes;
294
295 DCHECK(node_iter != node_range.end());
296 FrameTree* root_loading_tree = root_->frame_tree()->LoadingTree();
297 while (node_iter != node_range.end()) {
298 // Skip over frame trees and children which belong to inner web contents
299 // i.e., when nodes doesn't point to the same loading frame tree.
300 if ((*node_iter)->frame_tree()->LoadingTree() != root_loading_tree) {
301 node_iter.AdvanceSkippingChildren();
302 } else {
303 nodes.push_back(*node_iter);
304 ++node_iter;
305 }
306 }
307 return nodes;
308}
309
Kevin McNee5f594382021-05-06 23:18:23310FrameTree::NodeRange FrameTree::SubtreeAndInnerTreeNodes(
Dave Tapuskadda303d2022-10-04 16:56:48311 RenderFrameHostImpl* parent,
312 bool include_delegate_nodes_for_inner_frame_trees) {
Kevin McNee5f594382021-05-06 23:18:23313 std::vector<FrameTreeNode*> starting_nodes;
314 starting_nodes.reserve(parent->child_count());
315 for (size_t i = 0; i < parent->child_count(); ++i) {
316 FrameTreeNode* child = parent->child_at(i);
317 FrameTreeNode* inner_tree_main_ftn = GetInnerTreeMainFrameNode(child);
Dave Tapuskadda303d2022-10-04 16:56:48318 if (inner_tree_main_ftn) {
319 if (include_delegate_nodes_for_inner_frame_trees)
320 starting_nodes.push_back(child);
321 starting_nodes.push_back(inner_tree_main_ftn);
322 } else {
323 starting_nodes.push_back(child);
324 }
Kevin McNee5f594382021-05-06 23:18:23325 }
326 const std::vector<FrameTreeNode*> unattached_owned_nodes =
327 parent->delegate()->GetUnattachedOwnedNodes(parent);
328 starting_nodes.insert(starting_nodes.end(), unattached_owned_nodes.begin(),
329 unattached_owned_nodes.end());
330 return NodeRange(starting_nodes, nullptr,
Dave Tapuskadda303d2022-10-04 16:56:48331 /* should_descend_into_inner_trees */ true,
332 include_delegate_nodes_for_inner_frame_trees);
kenrb61b6c252016-03-22 17:37:15333}
334
Alex Moshchuk27caae82017-09-11 23:11:18335FrameTree::NodeRange FrameTree::NodesExceptSubtree(FrameTreeNode* node) {
Dave Tapuskadda303d2022-10-04 16:56:48336 return NodeRange({root_}, node, /*should_descend_into_inner_trees=*/false,
337 /*include_delegate_nodes_for_inner_frame_trees=*/false);
[email protected]9b159a52013-10-03 17:24:55338}
339
Sreeja Kamishettyd64b993d2022-02-14 12:04:42340FrameTree* FrameTree::LoadingTree() {
341 // We return the delegate's loading frame tree to infer loading related
342 // states.
343 return delegate_->LoadingTree();
344}
345
Lucas Furukawa Gadani99125822019-01-03 15:41:49346FrameTreeNode* FrameTree::AddFrame(
Alexander Timin381e7e182020-04-28 19:04:03347 RenderFrameHostImpl* parent,
Balazs Engedyba034e72017-10-27 22:26:28348 int process_id,
349 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18350 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
Oksana Zhuravlovafee097c2019-07-26 17:01:30351 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
352 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30353 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49354 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
355 associated_interface_provider_receiver,
Antonio Gomes9d5c1ef2020-04-30 20:56:41356 blink::mojom::TreeScopeType scope,
Balazs Engedyba034e72017-10-27 22:26:28357 const std::string& frame_name,
358 const std::string& frame_unique_name,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45359 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04360 const blink::LocalFrameToken& frame_token,
Balazs Engedyba034e72017-10-27 22:26:28361 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34362 const blink::DocumentToken& document_token,
Luna Luc3fdacdf2017-11-08 04:48:53363 const blink::FramePolicy& frame_policy,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54364 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Ehsan Karamad192a8da2018-10-21 03:48:08365 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41366 blink::FrameOwnerElementType owner_type,
367 bool is_dummy_frame_for_inner_tree) {
nick8814e652015-12-18 01:44:12368 CHECK_NE(new_routing_id, MSG_ROUTING_NONE);
Dominic Farolino4bc10ee2021-08-31 00:37:36369 // Normally this path is for blink adding a child local frame. But both
370 // portals and fenced frames add a dummy child frame that never gets a
371 // corresponding RenderFrameImpl in any renderer process, and therefore its
372 // `frame_remote` is invalid. Also its RenderFrameHostImpl is exempt from
373 // having `RenderFrameCreated()` called on it (see later in this method, as
374 // well as `WebContentsObserverConsistencyChecker::RenderFrameHostChanged()`).
Kevin McNee43fe8292021-10-04 22:59:41375 DCHECK_NE(frame_remote.is_valid(), is_dummy_frame_for_inner_tree);
376 DCHECK_NE(browser_interface_broker_receiver.is_valid(),
377 is_dummy_frame_for_inner_tree);
Dominic Farolino12e06d72022-08-05 02:29:49378 DCHECK_NE(associated_interface_provider_receiver.is_valid(),
379 is_dummy_frame_for_inner_tree);
nick8814e652015-12-18 01:44:12380
dgroganfb22f9a2014-10-20 21:32:32381 // A child frame always starts with an initial empty document, which means
382 // it is in the same SiteInstance as the parent frame. Ensure that the process
383 // which requested a child frame to be added is the same as the process of the
384 // parent node.
Dominic Farolino6caf3032021-10-20 03:50:08385 CHECK_EQ(parent->GetProcess()->GetID(), process_id);
dgroganfb22f9a2014-10-20 21:32:32386
iclelland098da752017-06-28 13:46:50387 std::unique_ptr<FrameTreeNode> new_node = base::WrapUnique(new FrameTreeNode(
Harkiran Bolaria0b3bdef02022-03-10 13:04:40388 this, parent, scope, is_created_by_script, devtools_frame_token,
389 frame_owner_properties, owner_type, frame_policy));
iclelland098da752017-06-28 13:46:50390
391 // Set sandbox flags and container policy and make them effective immediately,
Charlie Hu5130d25e2021-03-05 21:53:39392 // since initial sandbox flags and permissions policy should apply to the
393 // initial empty document in the frame. This needs to happen before the call
394 // to AddChild so that the effective policy is sent to any newly-created
Dave Tapuska2402595f2022-08-03 16:24:21395 // `blink::RemoteFrame` objects when the RenderFrameHost is created.
Charlie Hu5ffc0152019-12-06 15:59:53396 // SetPendingFramePolicy is necessary here because next navigation on this
397 // frame will need the value of pending frame policy instead of effective
398 // frame policy.
Ian Clellandcdc4f312017-10-13 22:24:12399 new_node->SetPendingFramePolicy(frame_policy);
iclelland098da752017-06-28 13:46:50400
Shubhie Panickerddf2a4e2018-03-06 00:09:06401 if (was_discarded)
402 new_node->set_was_discarded();
403
iclelland098da752017-06-28 13:46:50404 // Add the new node to the FrameTree, creating the RenderFrameHost.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40405 FrameTreeNode* added_node = parent->AddChild(
406 std::move(new_node), new_routing_id, std::move(frame_remote), frame_token,
Daniel Cheng284c38942022-09-22 23:30:34407 document_token, frame_policy, frame_name, frame_unique_name);
nick8814e652015-12-18 01:44:12408
Garrett Tanzer34cb92fe2022-09-28 17:50:54409 added_node->SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30410
Kevin McNee43fe8292021-10-04 22:59:41411 if (browser_interface_broker_receiver.is_valid()) {
412 added_node->current_frame_host()->BindBrowserInterfaceBrokerReceiver(
413 std::move(browser_interface_broker_receiver));
414 }
Oksana Zhuravlovafee097c2019-07-26 17:01:30415
Antonio Sartoridb967c52021-01-20 09:54:30416 if (policy_container_bind_params) {
Antonio Sartoria1fd1432020-11-25 09:10:20417 added_node->current_frame_host()->policy_container_host()->Bind(
Antonio Sartoridb967c52021-01-20 09:54:30418 std::move(policy_container_bind_params));
Antonio Sartoria1fd1432020-11-25 09:10:20419 }
420
Dominic Farolino12e06d72022-08-05 02:29:49421 if (associated_interface_provider_receiver.is_valid()) {
422 added_node->current_frame_host()->BindAssociatedInterfaceProviderReceiver(
423 std::move(associated_interface_provider_receiver));
424 }
425
nasko03ecfad2016-08-02 00:54:06426 // The last committed NavigationEntry may have a FrameNavigationEntry with the
427 // same |frame_unique_name|, since we don't remove FrameNavigationEntries if
428 // their frames are deleted. If there is a stale one, remove it to avoid
429 // conflicts on future updates.
Fergal Daly09d6c762020-05-29 02:05:18430 NavigationEntryImpl* last_committed_entry = static_cast<NavigationEntryImpl*>(
Carlos Caballero40b0efd2021-01-26 11:55:00431 navigator_.controller().GetLastCommittedEntry());
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45432 if (last_committed_entry) {
433 last_committed_entry->RemoveEntryForFrame(
434 added_node, /* only_if_different_position = */ true);
435 }
nasko03ecfad2016-08-02 00:54:06436
nick8814e652015-12-18 01:44:12437 // Now that the new node is part of the FrameTree and has a RenderFrameHost,
438 // we can announce the creation of the initial RenderFrame which already
439 // exists in the renderer process.
Sreeja Kamishetty5b699622021-01-22 12:54:08440 // For consistency with navigating to a new RenderFrameHost case, we dispatch
441 // RenderFrameCreated before RenderFrameHostChanged.
Kevin McNee43fe8292021-10-04 22:59:41442 if (!is_dummy_frame_for_inner_tree) {
Dominic Farolino4bc10ee2021-08-31 00:37:36443 // The outer dummy FrameTreeNode for both portals and fenced frames does not
444 // have a live RenderFrame in the renderer process.
Fergal Dalyf9ea35c2020-12-11 15:26:01445 added_node->current_frame_host()->RenderFrameCreated();
Ehsan Karamad44fc72112019-02-26 18:15:47446 }
Sreeja Kamishetty5b699622021-01-22 12:54:08447
448 // Notify the delegate of the creation of the current RenderFrameHost.
449 // This is only for subframes, as the main frame case is taken care of by
450 // WebContentsImpl::Init.
451 manager_delegate_->NotifySwappedFromRenderManager(
Dave Tapuskae45d6fd2021-09-29 17:03:59452 nullptr, added_node->current_frame_host());
Lucas Furukawa Gadani99125822019-01-03 15:41:49453 return added_node;
[email protected]9b159a52013-10-03 17:24:55454}
455
[email protected]58faf942014-02-20 21:03:58456void FrameTree::RemoveFrame(FrameTreeNode* child) {
Alexander Timin381e7e182020-04-28 19:04:03457 RenderFrameHostImpl* parent = child->parent();
[email protected]58faf942014-02-20 21:03:58458 if (!parent) {
459 NOTREACHED() << "Unexpected RemoveFrame call for main frame.";
460 return;
[email protected]9b159a52013-10-03 17:24:55461 }
462
Alexander Timin381e7e182020-04-28 19:04:03463 parent->RemoveChild(child);
[email protected]9b159a52013-10-03 17:24:55464}
465
Harkiran Bolaria2912a6b32022-02-22 16:43:45466void FrameTree::CreateProxiesForSiteInstance(
467 FrameTreeNode* source,
468 SiteInstance* site_instance,
469 const scoped_refptr<BrowsingContextState>&
470 source_new_browsing_context_state) {
Sharon Yang57bde122022-03-01 20:01:12471 SiteInstanceGroup* group =
472 static_cast<SiteInstanceImpl*>(site_instance)->group();
naskob3041b98a42016-03-12 04:43:06473 // Create the RenderFrameProxyHost for the new SiteInstance.
alexmos58729042015-06-18 23:20:00474 if (!source || !source->IsMainFrame()) {
Sharon Yang57bde122022-03-01 20:01:12475 RenderViewHostImpl* render_view_host = GetRenderViewHost(group).get();
arthursonzognic5be3842019-07-09 11:49:14476 if (render_view_host) {
Sharon Yang57bde122022-03-01 20:01:12477 root()->render_manager()->EnsureRenderViewInitialized(render_view_host,
478 group);
arthursonzognic5be3842019-07-09 11:49:14479 } else {
Harkiran Bolaria2912a6b32022-02-22 16:43:45480 // Due to the check above, we are creating either an opener proxy (when
481 // source is null) or a main frame proxy due to a subframe navigation
482 // (when source is not a main frame). In the former case, we should use
483 // root's current BrowsingContextState, while in the latter case we should
484 // use BrowsingContextState from the main RenderFrameHost of the subframe
Dave Tapuska2cf1f532022-08-10 15:30:49485 // being navigated. We want to ensure that the `blink::WebView` is created
486 // in the right SiteInstance if it doesn't exist, before creating the
487 // other proxies; if the `blink::WebView` doesn't exist, the only way to
488 // do this is to also create a proxy for the main frame as well.
Harkiran Bolaria2912a6b32022-02-22 16:43:45489 root()->render_manager()->CreateRenderFrameProxy(
490 site_instance,
491 source ? source->parent()->GetMainFrame()->browsing_context_state()
492 : root()->current_frame_host()->browsing_context_state());
[email protected]82307f6b2014-08-07 03:30:12493 }
494 }
495
Alex Moshchuk1226b152019-11-08 18:23:45496 // Check whether we're in an inner delegate and |site_instance| corresponds
497 // to the outer delegate. Subframe proxies aren't needed if this is the
498 // case.
499 bool is_site_instance_for_outer_delegate = false;
500 RenderFrameProxyHost* outer_delegate_proxy =
501 root()->render_manager()->GetProxyToOuterDelegate();
502 if (outer_delegate_proxy) {
503 is_site_instance_for_outer_delegate =
504 (site_instance == outer_delegate_proxy->GetSiteInstance());
505 }
506
naskoe6edde32014-10-17 15:36:48507 // Proxies are created in the FrameTree in response to a node navigating to a
508 // new SiteInstance. Since |source|'s navigation will replace the currently
Alex Moshchuk27caae82017-09-11 23:11:18509 // loaded document, the entire subtree under |source| will be removed, and
510 // thus proxy creation is skipped for all nodes in that subtree.
511 //
512 // However, a proxy *is* needed for the |source| node itself. This lets
513 // cross-process navigations in |source| start with a proxy and follow a
514 // remote-to-local transition, which avoids race conditions in cases where
515 // other navigations need to reference |source| before it commits. See
516 // https://crbug.com/756790 for more background. Therefore,
517 // NodesExceptSubtree(source) will include |source| in the nodes traversed
518 // (see NodeIterator::operator++).
519 for (FrameTreeNode* node : NodesExceptSubtree(source)) {
dcheng57e39e22016-01-21 00:25:38520 // If a new frame is created in the current SiteInstance, other frames in
521 // that SiteInstance don't need a proxy for the new frame.
Alex Moshchuk27caae82017-09-11 23:11:18522 RenderFrameHostImpl* current_host =
523 node->render_manager()->current_frame_host();
524 SiteInstance* current_instance = current_host->GetSiteInstance();
525 if (current_instance != site_instance) {
526 if (node == source && !current_host->IsRenderFrameLive()) {
Fergal Daly6de62f52020-10-14 01:56:44527 // We don't create a proxy at |source| when the current RenderFrameHost
528 // isn't live. This is because either (1) the speculative
Alex Moshchuk27caae82017-09-11 23:11:18529 // RenderFrameHost will be committed immediately, and the proxy
Fergal Daly6de62f52020-10-14 01:56:44530 // destroyed right away, in GetFrameHostForNavigation, which makes the
531 // races above impossible, or (2) the early commit will be skipped due
532 // to ShouldSkipEarlyCommitPendingForCrashedFrame, in which case the
533 // proxy for |source| *is* needed, but it will be created later in
534 // CreateProxiesForNewRenderFrameHost.
535 //
536 // TODO(fergal): Consider creating a proxy for |source| here rather than
537 // in CreateProxiesForNewRenderFrameHost for case (2) above.
Alex Moshchuk27caae82017-09-11 23:11:18538 continue;
539 }
Alex Moshchuk1226b152019-11-08 18:23:45540
541 // Do not create proxies for subframes in the outer delegate's
542 // SiteInstance, since there is no need to expose these subframes to the
543 // outer delegate. See also comments in CreateProxiesForChildFrame() and
544 // https://crbug.com/1013553.
545 if (!node->IsMainFrame() && is_site_instance_for_outer_delegate)
546 continue;
547
Harkiran Bolaria2912a6b32022-02-22 16:43:45548 // If |node| is the FrameTreeNode being navigated, we use
549 // |browsing_context_state| (as BrowsingContextState might change for
550 // cross-BrowsingInstance navigations). Otherwise, we should use the
551 // |node|'s current BrowsingContextState.
552 node->render_manager()->CreateRenderFrameProxy(
553 site_instance,
554 node == source
555 ? source_new_browsing_context_state
556 : node->current_frame_host()->browsing_context_state());
Alex Moshchuk27caae82017-09-11 23:11:18557 }
dcheng57e39e22016-01-21 00:25:38558 }
[email protected]82307f6b2014-08-07 03:30:12559}
560
[email protected]9b159a52013-10-03 17:24:55561RenderFrameHostImpl* FrameTree::GetMainFrame() const {
[email protected]94d0cc12013-12-18 00:07:41562 return root_->current_frame_host();
[email protected]9b159a52013-10-03 17:24:55563}
564
[email protected]9c9343b2014-03-08 02:56:07565FrameTreeNode* FrameTree::GetFocusedFrame() {
566 return FindByID(focused_frame_tree_node_id_);
567}
568
Sharon Yangefe52632022-03-08 23:06:06569void FrameTree::SetFocusedFrame(FrameTreeNode* node,
570 SiteInstanceGroup* source) {
Rakina Zata Amnicca4889e2021-09-28 23:25:55571 CHECK(node->current_frame_host()->IsActive());
alexmos5357efb2015-12-16 21:44:00572 if (node == GetFocusedFrame())
573 return;
574
Sharon Yangefe52632022-03-08 23:06:06575 std::set<SiteInstanceGroup*> frame_tree_groups =
576 CollectSiteInstanceGroups(this);
alexmosb1dc2162015-11-05 00:59:20577
Sharon Yangefe52632022-03-08 23:06:06578 SiteInstanceGroup* current_group =
579 node->current_frame_host()->GetSiteInstance()->group();
alexmos5357efb2015-12-16 21:44:00580
Sharon Yang7424bda2021-11-04 20:27:43581 // Update the focused frame in all other SiteInstanceGroups. If focus changes
582 // to a cross-group frame, this allows the old focused frame's renderer
alexmosb1dc2162015-11-05 00:59:20583 // process to clear focus from that frame and fire blur events. It also
584 // ensures that the latest focused frame is available in all renderers to
585 // compute document.activeElement.
alexmos5357efb2015-12-16 21:44:00586 //
Sharon Yangefe52632022-03-08 23:06:06587 // We do not notify the |source| SiteInstanceGroup because it already knows
588 // the new focused frame (since it initiated the focus change), and we notify
589 // the new focused frame's SiteInstanceGroup (if it differs from |source|)
590 // separately below.
591 for (auto* group : frame_tree_groups) {
592 if (group != source && group != current_group) {
593 RenderFrameProxyHost* proxy = node->current_frame_host()
594 ->browsing_context_state()
595 ->GetRenderFrameProxyHost(group);
Sharon Yangf92842a2022-03-01 18:21:47596
Rakina Zata Amni80700402021-09-20 17:18:03597 if (proxy) {
598 proxy->SetFocusedFrame();
599 } else {
Sharon Yangf92842a2022-03-01 18:21:47600 base::debug::DumpWithoutCrashing();
Rakina Zata Amni80700402021-09-20 17:18:03601 }
alexmosb1dc2162015-11-05 00:59:20602 }
alexmosca2c6ba2015-10-01 21:52:25603 }
604
Sharon Yangefe52632022-03-08 23:06:06605 // If |node| was focused from a cross-group frame (i.e., via
alexmos5357efb2015-12-16 21:44:00606 // window.focus()), tell its RenderFrame that it should focus.
Sharon Yangefe52632022-03-08 23:06:06607 if (current_group != source)
alexmos5357efb2015-12-16 21:44:00608 node->current_frame_host()->SetFocusedFrame();
609
[email protected]9c9343b2014-03-08 02:56:07610 focused_frame_tree_node_id_ = node->frame_tree_node_id();
alexmos21acae52015-11-07 01:04:43611 node->DidFocus();
dmazzonif27bf892016-03-10 15:51:55612
613 // The accessibility tree data for the root of the frame tree keeps
614 // track of the focused frame too, so update that every time the
615 // focused frame changes.
Kevin McNee582a7d62021-10-12 21:42:22616 root()
617 ->current_frame_host()
618 ->GetOutermostMainFrameOrEmbedder()
619 ->UpdateAXTreeData();
[email protected]9c9343b2014-03-08 02:56:07620}
621
arthursonzognic5be3842019-07-09 11:49:14622scoped_refptr<RenderViewHostImpl> FrameTree::CreateRenderViewHost(
avib7348942015-12-25 20:57:10623 SiteInstance* site_instance,
avib7348942015-12-25 20:57:10624 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58625 bool renderer_initiated_creation,
626 scoped_refptr<BrowsingContextState> main_browsing_context_state) {
627 if (main_browsing_context_state) {
628 DCHECK(main_browsing_context_state->is_main_frame());
629 }
dcheng29f5a6c2015-08-31 21:43:27630 RenderViewHostImpl* rvh =
631 static_cast<RenderViewHostImpl*>(RenderViewHostFactory::Create(
Sharon Yangcadd00b82022-05-13 19:16:33632 this, static_cast<SiteInstanceImpl*>(site_instance)->group(),
633 site_instance->GetStoragePartitionConfig(), render_view_delegate_,
Dave Tapuska658bfec2022-08-05 14:27:20634 render_widget_delegate_, main_frame_routing_id,
Sharon Yangcadd00b82022-05-13 19:16:33635 renderer_initiated_creation, std::move(main_browsing_context_state)));
arthursonzognic5be3842019-07-09 11:49:14636 return base::WrapRefCounted(rvh);
[email protected]94d0cc12013-12-18 00:07:41637}
638
arthursonzognic5be3842019-07-09 11:49:14639scoped_refptr<RenderViewHostImpl> FrameTree::GetRenderViewHost(
Sharon Yang57bde122022-03-01 20:01:12640 SiteInstanceGroup* group) {
Sharon Yanga2fe85e2022-02-09 21:38:29641 // When called from RenderFrameHostManager::CreateRenderFrameHost, it's
642 // possible that a RenderProcessHost hasn't yet been created, which means
Sharon Yang57bde122022-03-01 20:01:12643 // a SiteInstanceGroup won't have been created yet.
Sharon Yanga2fe85e2022-02-09 21:38:29644 if (!group)
645 return nullptr;
646
647 auto it = render_view_host_map_.find(GetRenderViewHostMapId(group));
arthursonzogni88e54ae2019-04-15 12:57:36648 if (it == render_view_host_map_.end())
649 return nullptr;
alexmos9aa61232016-04-26 21:54:02650
arthursonzognic5be3842019-07-09 11:49:14651 return base::WrapRefCounted(it->second);
[email protected]94d0cc12013-12-18 00:07:41652}
653
Aaron Colwell78b4bde2021-03-16 16:16:09654FrameTree::RenderViewHostMapId FrameTree::GetRenderViewHostMapId(
Sharon Yangc581a0c2021-11-02 18:09:39655 SiteInstanceGroup* site_instance_group) const {
656 return RenderViewHostMapId::FromUnsafeValue(
657 site_instance_group->GetId().value());
Lowell Manners75055a132019-10-11 10:30:29658}
659
Aaron Colwell78b4bde2021-03-16 16:16:09660void FrameTree::RegisterRenderViewHost(RenderViewHostMapId id,
661 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47662 TRACE_EVENT_INSTANT("navigation", "FrameTree::RegisterRenderViewHost",
663 ChromeTrackEvent::kRenderViewHost, *rvh);
Aaron Colwell78b4bde2021-03-16 16:16:09664 CHECK(!base::Contains(render_view_host_map_, id));
665 render_view_host_map_[id] = rvh;
666}
667
668void FrameTree::UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13669 RenderViewHostImpl* rvh) {
Rakina Zata Amni4b1968d2021-09-09 03:29:47670 TRACE_EVENT_INSTANT("navigation", "FrameTree::UnregisterRenderViewHost",
671 ChromeTrackEvent::kRenderViewHost, *rvh);
Aaron Colwell78b4bde2021-03-16 16:16:09672 auto it = render_view_host_map_.find(id);
arthursonzogni88e54ae2019-04-15 12:57:36673 CHECK(it != render_view_host_map_.end());
arthursonzognic5be3842019-07-09 11:49:14674 CHECK_EQ(it->second, rvh);
675 render_view_host_map_.erase(it);
[email protected]94d0cc12013-12-18 00:07:41676}
677
arthursonzogni0a2007792019-05-28 21:32:48678void FrameTree::FrameUnloading(FrameTreeNode* frame) {
679 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
680 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
681
682 // Ensure frames that are about to be deleted aren't visible from the other
683 // processes anymore.
Harkiran Bolaria0b3bdef02022-03-10 13:04:40684 frame->GetBrowsingContextStateForSubframe()->ResetProxyHosts();
arthursonzogni0a2007792019-05-28 21:32:48685}
686
dmazzonie950ea232015-03-13 21:39:45687void FrameTree::FrameRemoved(FrameTreeNode* frame) {
nick53d5cbf2015-04-23 22:50:14688 if (frame->frame_tree_node_id() == focused_frame_tree_node_id_)
thestige62f7382016-11-08 18:31:39689 focused_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
dmazzonie950ea232015-03-13 21:39:45690}
691
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15692double FrameTree::GetLoadProgress() {
693 if (root_->HasNavigation())
694 return blink::kInitialLoadProgress;
695
696 return root_->current_frame_host()->GetPage().load_progress();
fdegans1d16355162015-03-26 11:58:34697}
698
Yu Gaoc8c18552022-06-22 14:38:45699bool FrameTree::IsLoadingIncludingInnerFrameTrees() const {
Sreeja Kamishetty15f9944a22022-03-10 10:16:08700 for (const FrameTreeNode* node :
701 const_cast<FrameTree*>(this)->CollectNodesForIsLoading()) {
dcheng57e39e22016-01-21 00:25:38702 if (node->IsLoading())
703 return true;
704 }
705 return false;
fdegans1d16355162015-03-26 11:58:34706}
707
alexmos3fcd0ca2015-10-23 18:18:33708void FrameTree::ReplicatePageFocus(bool is_focused) {
Dave Tapuska53e6ce12021-08-06 16:08:24709 // Focus loss may occur while this FrameTree is being destroyed. Don't
710 // send the message in this case, as the main frame's RenderFrameHost and
711 // other state has already been cleared.
712 if (is_being_destroyed_)
713 return;
Sharon Yangefe52632022-03-08 23:06:06714 std::set<SiteInstanceGroup*> frame_tree_site_instance_groups =
715 CollectSiteInstanceGroups(this);
alexmos3fcd0ca2015-10-23 18:18:33716
Sharon Yangefe52632022-03-08 23:06:06717 // Send the focus update to main frame's proxies in all SiteInstanceGroups of
alexmos3fcd0ca2015-10-23 18:18:33718 // other frames in this FrameTree. Note that the main frame might also know
Sharon Yangefe52632022-03-08 23:06:06719 // about proxies in SiteInstanceGroups for frames in a different FrameTree
720 // (e.g., for window.open), so we can't just iterate over its proxy_hosts_ in
alexmos3fcd0ca2015-10-23 18:18:33721 // RenderFrameHostManager.
Sharon Yangefe52632022-03-08 23:06:06722 for (auto* group : frame_tree_site_instance_groups)
723 SetPageFocus(group, is_focused);
alexmos0d7e0b09b2015-10-29 22:11:48724}
alexmos3fcd0ca2015-10-23 18:18:33725
Sreeja Kamishetty60e47132022-02-08 12:51:53726bool FrameTree::IsPortal() {
727 return delegate_->IsPortal();
728}
729
Sharon Yangefe52632022-03-08 23:06:06730void FrameTree::SetPageFocus(SiteInstanceGroup* group, bool is_focused) {
alexmos0d7e0b09b2015-10-29 22:11:48731 RenderFrameHostManager* root_manager = root_->render_manager();
732
Adithya Srinivasan47731222021-01-22 15:02:42733 // Portal frame tree should not get page focus.
Sreeja Kamishetty60e47132022-02-08 12:51:53734 DCHECK(!IsPortal() || !is_focused);
Adithya Srinivasan47731222021-01-22 15:02:42735
alexmos0d7e0b09b2015-10-29 22:11:48736 // This is only used to set page-level focus in cross-process subframes, and
Sharon Yangefe52632022-03-08 23:06:06737 // requests to set focus in main frame's SiteInstanceGroup are ignored.
738 if (group != root_manager->current_frame_host()->GetSiteInstance()->group()) {
739 RenderFrameProxyHost* proxy = root_manager->current_frame_host()
740 ->browsing_context_state()
741 ->GetRenderFrameProxyHost(group);
Dave Tapuska82b54012022-07-15 23:26:10742 if (proxy->is_render_frame_proxy_live())
743 proxy->GetAssociatedRemoteFrame()->SetPageFocus(is_focused);
alexmos3fcd0ca2015-10-23 18:18:33744 }
745}
746
W. James MacLeanc07dc41b2022-07-25 18:52:16747void FrameTree::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanb70fab82020-05-01 18:51:14748 const url::Origin& previously_visited_origin,
749 NavigationRequest* navigation_request_to_exclude) {
W. James MacLeanc07dc41b2022-07-25 18:52:16750 controller().RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLeanc6dc86c2021-08-12 13:58:37751 previously_visited_origin);
752
W. James MacLeanb70fab82020-05-01 18:51:14753 std::unordered_set<SiteInstance*> matching_site_instances;
754
755 // Be sure to visit all RenderFrameHosts associated with this frame that might
756 // have an origin that could script other frames. We skip RenderFrameHosts
757 // that are in the bfcache, assuming there's no way for a frame to join the
758 // BrowsingInstance of a bfcache RFH while it's in the cache.
759 for (auto* frame_tree_node : SubtreeNodes(root())) {
760 auto* frame_host = frame_tree_node->current_frame_host();
W. James MacLeanb70fab82020-05-01 18:51:14761 if (previously_visited_origin == frame_host->GetLastCommittedOrigin())
762 matching_site_instances.insert(frame_host->GetSiteInstance());
763
764 if (frame_host->HasCommittingNavigationRequestForOrigin(
765 previously_visited_origin, navigation_request_to_exclude)) {
766 matching_site_instances.insert(frame_host->GetSiteInstance());
767 }
768
769 auto* spec_frame_host =
770 frame_tree_node->render_manager()->speculative_frame_host();
771 if (spec_frame_host &&
772 spec_frame_host->HasCommittingNavigationRequestForOrigin(
773 previously_visited_origin, navigation_request_to_exclude)) {
774 matching_site_instances.insert(spec_frame_host->GetSiteInstance());
775 }
776
777 auto* navigation_request = frame_tree_node->navigation_request();
778 if (navigation_request &&
779 navigation_request != navigation_request_to_exclude &&
780 navigation_request->HasCommittingOrigin(previously_visited_origin)) {
781 matching_site_instances.insert(frame_host->GetSiteInstance());
782 }
783 }
784
785 // Update any SiteInstances found to contain |origin|.
786 for (auto* site_instance : matching_site_instances) {
787 static_cast<SiteInstanceImpl*>(site_instance)
W. James MacLeanc07dc41b2022-07-25 18:52:16788 ->RegisterAsDefaultOriginIsolation(previously_visited_origin);
W. James MacLeanb70fab82020-05-01 18:51:14789 }
790}
791
Carlos Caballero40b0efd2021-01-26 11:55:00792void FrameTree::Init(SiteInstance* main_frame_site_instance,
793 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17794 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46795 RenderFrameHostImpl* opener_for_origin,
Harkiran Bolaria5ce27632022-01-20 15:05:05796 const blink::FramePolicy& frame_policy) {
Carlos Caballero40b0efd2021-01-26 11:55:00797 // blink::FrameTree::SetName always keeps |unique_name| empty in case of a
798 // main frame - let's do the same thing here.
799 std::string unique_name;
Sreeja Kamishetty3699c35f2021-02-12 15:10:58800 root_->render_manager()->InitRoot(main_frame_site_instance,
Harkiran Bolaria880a7632022-02-28 16:02:50801 renderer_initiated_creation, frame_policy,
802 main_frame_name);
Garrett Tanzer34cb92fe2022-09-28 17:50:54803 root_->SetFencedFramePropertiesIfNeeded();
Rakina Zata Amniafd3c6582021-11-30 06:19:17804
805 // The initial empty document should inherit the origin of its opener (the
806 // origin may change after the first commit), except when they are in
Rakina Zata Amni4eb716e2022-04-05 21:32:46807 // different browsing context groups (`renderer_initiated_creation` will be
808 // false), where it should use a new opaque origin.
Rakina Zata Amniafd3c6582021-11-30 06:19:17809 // See also https://crbug.com/932067.
810 //
811 // Note that the origin of the new frame might depend on sandbox flags.
812 // Checking sandbox flags of the new frame should be safe at this point,
813 // because the flags should be already inherited when creating the root node.
Rakina Zata Amni4eb716e2022-04-05 21:32:46814 DCHECK(!renderer_initiated_creation || opener_for_origin);
Rakina Zata Amniafd3c6582021-11-30 06:19:17815 root_->current_frame_host()->SetOriginDependentStateOfNewFrame(
Rakina Zata Amni4eb716e2022-04-05 21:32:46816 renderer_initiated_creation ? opener_for_origin->GetLastCommittedOrigin()
Rakina Zata Amniafd3c6582021-11-30 06:19:17817 : url::Origin());
Rakina Zata Amni2322f4f82022-01-24 13:24:24818
819 if (blink::features::IsInitialNavigationEntryEnabled())
820 controller().CreateInitialEntry();
Carlos Caballero40b0efd2021-01-26 11:55:00821}
822
Carlos Caballeroede6f8c2021-01-28 11:01:50823void FrameTree::DidAccessInitialMainDocument() {
824 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidAccessInitialDocument");
825 has_accessed_initial_main_document_ = true;
826 controller().DidAccessInitialMainDocument();
827}
828
Carlos Caballero03262522021-02-05 14:49:58829void FrameTree::DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19830 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58831 bool was_previously_loading) {
Carlos Caballero03262522021-02-05 14:49:58832 if (was_previously_loading)
833 return;
834
Yu Gaoc8c18552022-06-22 14:38:45835 root()->render_manager()->SetIsLoading(IsLoadingIncludingInnerFrameTrees());
Nate Chapin9aabf5f2021-11-12 00:31:19836 delegate_->DidStartLoading(&node, should_show_loading_ui);
Carlos Caballero03262522021-02-05 14:49:58837}
838
839void FrameTree::DidStopLoadingNode(FrameTreeNode& node) {
Yu Gaoc8c18552022-06-22 14:38:45840 if (IsLoadingIncludingInnerFrameTrees())
Carlos Caballero03262522021-02-05 14:49:58841 return;
842
843 root()->render_manager()->SetIsLoading(false);
844 delegate_->DidStopLoading();
845}
846
Carlos Caballero03262522021-02-05 14:49:58847void FrameTree::DidCancelLoading() {
848 OPTIONAL_TRACE_EVENT0("content", "FrameTree::DidCancelLoading");
849 navigator_.controller().DiscardNonCommittedEntries();
Carlos Caballero03262522021-02-05 14:49:58850}
851
852void FrameTree::StopLoading() {
853 for (FrameTreeNode* node : Nodes())
854 node->StopLoading();
855}
856
Carlos Caballero101ac26b2021-03-24 11:54:05857void FrameTree::Shutdown() {
Dave Tapuskae88286de2021-08-05 19:10:42858 is_being_destroyed_ = true;
Carlos Caballero101ac26b2021-03-24 11:54:05859#if DCHECK_IS_ON()
860 DCHECK(!was_shut_down_);
861 was_shut_down_ = true;
862#endif
863
864 RenderFrameHostManager* root_manager = root_->render_manager();
865
Carlos Caballerodb7a8f6e2021-04-09 18:17:47866 if (!root_manager->current_frame_host()) {
867 // The page has been transferred out during an activation. There is little
868 // left to do.
Carlos Caballerod1c80432021-04-20 08:16:32869 // TODO(https://crbug.com/1199693): If we decide that pending delete RFHs
Carlos Caballerodb7a8f6e2021-04-09 18:17:47870 // need to be moved along during activation replace this line with a DCHECK
871 // that there are no pending delete instances.
872 root_manager->ClearRFHsPendingShutdown();
Carlos Caballerodb7a8f6e2021-04-09 18:17:47873 DCHECK(!root_->navigation_request());
874 DCHECK(!root_manager->speculative_frame_host());
875 manager_delegate_->OnFrameTreeNodeDestroyed(root_);
Carlos Caballero101ac26b2021-03-24 11:54:05876 return;
Carlos Caballerodb7a8f6e2021-04-09 18:17:47877 }
Carlos Caballero101ac26b2021-03-24 11:54:05878
879 for (FrameTreeNode* node : Nodes()) {
880 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
881 // to be shutdown and be deleted as well.
882 node->render_manager()->ClearRFHsPendingShutdown();
Carlos Caballerod1c80432021-04-20 08:16:32883 // TODO(https://crbug.com/1199676): Ban WebUI instance in Prerender pages.
Carlos Caballero101ac26b2021-03-24 11:54:05884 node->render_manager()->ClearWebUIInstances();
885 }
886
887 // Destroy all subframes now. This notifies observers.
888 root_manager->current_frame_host()->ResetChildren();
Harkiran Bolaria0b3bdef02022-03-10 13:04:40889 root_manager->current_frame_host()
890 ->browsing_context_state()
891 ->ResetProxyHosts();
Carlos Caballero101ac26b2021-03-24 11:54:05892
Carlos Caballero101ac26b2021-03-24 11:54:05893 // Manually call the observer methods for the root FrameTreeNode. It is
894 // necessary to manually delete all objects tracking navigations
895 // (NavigationHandle, NavigationRequest) for observers to be properly
896 // notified of these navigations stopping before the WebContents is
897 // destroyed.
898
899 root_manager->current_frame_host()->RenderFrameDeleted();
900 root_manager->current_frame_host()->ResetNavigationRequests();
901
902 // Do not update state as the FrameTree::Delegate (possibly a WebContents) is
903 // being destroyed.
Daniel Cheng390e2a72022-09-28 06:07:53904 root_->ResetNavigationRequestButKeepState();
Carlos Caballero101ac26b2021-03-24 11:54:05905 if (root_manager->speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:45906 root_manager->DiscardSpeculativeRenderFrameHostForShutdown();
Carlos Caballero101ac26b2021-03-24 11:54:05907 }
908
Alexander Timin8aeee642021-05-12 08:39:33909 // NavigationRequests restoring the page from bfcache have a reference to the
910 // RFHs stored in the cache, so the cache should be cleared after the
911 // navigation request is reset.
912 controller().GetBackForwardCache().Shutdown();
913
Carlos Caballero101ac26b2021-03-24 11:54:05914 manager_delegate_->OnFrameTreeNodeDestroyed(root_);
915 render_view_delegate_->RenderViewDeleted(
916 root_manager->current_frame_host()->render_view_host());
917}
918
Dave Tapuskad8b0530f2021-10-19 15:12:31919base::SafeRef<FrameTree> FrameTree::GetSafeRef() {
920 return weak_ptr_factory_.GetSafeRef();
921}
922
Dave Tapuska54c76a032021-10-27 22:10:42923void FrameTree::FocusOuterFrameTrees() {
924 OPTIONAL_TRACE_EVENT0("content", "FrameTree::FocusOuterFrameTrees");
925
926 FrameTree* frame_tree_to_focus = this;
927 while (true) {
928 FrameTreeNode* outer_node = FrameTreeNode::GloballyFindByID(
929 frame_tree_to_focus->delegate()->GetOuterDelegateFrameTreeNodeId());
930 if (!outer_node || !outer_node->current_frame_host()->IsActive()) {
931 // Don't set focus on an inactive FrameTreeNode.
932 return;
933 }
934 outer_node->frame_tree()->SetFocusedFrame(outer_node, nullptr);
935
936 // For a browser initiated focus change, let embedding renderer know of the
937 // change. Otherwise, if the currently focused element is just across a
938 // process boundary in focus order, it will not be possible to move across
939 // that boundary. This is because the target element will already be focused
940 // (that renderer was not notified) and drop the event.
941 if (auto* proxy_to_outer_delegate = frame_tree_to_focus->root()
942 ->render_manager()
943 ->GetProxyToOuterDelegate()) {
944 proxy_to_outer_delegate->SetFocusedFrame();
945 }
946 frame_tree_to_focus = outer_node->frame_tree();
947 }
948}
949
[email protected]9b159a52013-10-03 17:24:55950} // namespace content