blob: efb8e0745b5a3c78f5393c87b835f638e598fda7 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
danakjc492bf82020-09-09 20:02:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_
6#define CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_
7
8#include <stdint.h>
9
10#include <iterator>
11#include <memory>
12#include <string>
13#include <unordered_map>
14
danakjc492bf82020-09-09 20:02:4415#include "base/containers/queue.h"
Carlos Caballero101ac26b2021-03-24 11:54:0516#include "base/dcheck_is_on.h"
Avi Drissmanadac21992023-01-11 23:46:3917#include "base/functional/callback.h"
danakjc492bf82020-09-09 20:02:4418#include "base/gtest_prod_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5219#include "base/memory/raw_ptr.h"
Dave Tapuskad8b0530f2021-10-19 15:12:3120#include "base/memory/safe_ref.h"
21#include "base/memory/weak_ptr.h"
Paul Semel3e241042022-10-11 12:57:3122#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4423#include "content/browser/renderer_host/navigator.h"
24#include "content/browser/renderer_host/navigator_delegate.h"
25#include "content/browser/renderer_host/render_frame_host_manager.h"
Sharon Yang7ce309e2023-01-19 21:39:5726#include "content/browser/renderer_host/render_view_host_enums.h"
danakjc492bf82020-09-09 20:02:4427#include "content/common/content_export.h"
28#include "mojo/public/cpp/bindings/pending_receiver.h"
29#include "services/service_manager/public/mojom/interface_provider.mojom.h"
David Bokanc3fb5fa2022-07-04 14:55:3130#include "third_party/blink/public/common/features.h"
Kevin McNee43fe8292021-10-04 22:59:4131#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Chris Hamilton3ff6ed0e2021-02-19 03:54:0432#include "third_party/blink/public/common/tokens/tokens.h"
danakjc492bf82020-09-09 20:02:4433#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom-forward.h"
34
35namespace blink {
Lei Zhang4a867672021-07-19 06:30:1436namespace mojom {
37class BrowserInterfaceBroker;
38enum class TreeScopeType;
39} // namespace mojom
40
danakjc492bf82020-09-09 20:02:4441struct FramePolicy;
42} // namespace blink
43
44namespace content {
45
Carlos Caballero40b0efd2021-01-26 11:55:0046class BrowserContext;
Jeremy Roman2d8dfe132021-07-06 20:51:2647class PageDelegate;
danakjc492bf82020-09-09 20:02:4448class RenderFrameHostDelegate;
49class RenderViewHostDelegate;
50class RenderViewHostImpl;
51class RenderFrameHostManager;
52class RenderWidgetHostDelegate;
Carlos Caballero40b0efd2021-01-26 11:55:0053class SiteInstance;
Sharon Yang57bde122022-03-01 20:01:1254class SiteInstanceGroup;
danakjc492bf82020-09-09 20:02:4455
56// Represents the frame tree for a page. With the exception of the main frame,
57// all FrameTreeNodes will be created/deleted in response to frame attach and
58// detach events in the DOM.
59//
60// The main frame's FrameTreeNode is special in that it is reused. This allows
61// it to serve as an anchor for state that needs to persist across top-level
62// page navigations.
63//
64// TODO(ajwong): Move NavigationController ownership to the main frame
65// FrameTreeNode. Possibly expose access to it from here.
66//
67// This object is only used on the UI thread.
68class CONTENT_EXPORT FrameTree {
69 public:
70 class NodeRange;
71
Alan Zhao8a882d12022-05-18 01:32:1572 class CONTENT_EXPORT NodeIterator {
danakjc492bf82020-09-09 20:02:4473 public:
Alan Zhao8a882d12022-05-18 01:32:1574 using iterator_category = std::forward_iterator_tag;
75 using value_type = FrameTreeNode*;
76 using difference_type = std::ptrdiff_t;
77 using pointer = FrameTreeNode**;
78 using reference = FrameTreeNode*&;
79
danakjc492bf82020-09-09 20:02:4480 NodeIterator(const NodeIterator& other);
81 ~NodeIterator();
82
83 NodeIterator& operator++();
Kevin McNee5f594382021-05-06 23:18:2384 // Advances the iterator and excludes the children of the current node
85 NodeIterator& AdvanceSkippingChildren();
danakjc492bf82020-09-09 20:02:4486
87 bool operator==(const NodeIterator& rhs) const;
88 bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); }
89
90 FrameTreeNode* operator*() { return current_node_; }
91
92 private:
Jayson Adams4db0bfe22021-07-15 19:24:0793 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4494 friend class NodeRange;
95
Kevin McNee5f594382021-05-06 23:18:2396 NodeIterator(const std::vector<FrameTreeNode*>& starting_nodes,
97 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:4898 bool should_descend_into_inner_trees,
99 bool include_delegate_nodes_for_inner_frame_trees);
Kevin McNee5f594382021-05-06 23:18:23100
101 void AdvanceNode();
danakjc492bf82020-09-09 20:02:44102
Lukasz Anforowicz877e6222021-11-26 00:03:23103 // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for
104 // performance reasons (based on analysis of sampling profiler data and
105 // tab_search:top100:2020).
Keishi Hattori488b7602022-05-02 13:09:31106 RAW_PTR_EXCLUSION FrameTreeNode* current_node_;
107 RAW_PTR_EXCLUSION const FrameTreeNode* const root_of_subtree_to_skip_;
Lukasz Anforowicz877e6222021-11-26 00:03:23108
Kevin McNee5f594382021-05-06 23:18:23109 const bool should_descend_into_inner_trees_;
Dave Tapuskadda303d2022-10-04 16:56:48110 const bool include_delegate_nodes_for_inner_frame_trees_;
Jayson Adams4db0bfe22021-07-15 19:24:07111 base::circular_deque<FrameTreeNode*> queue_;
danakjc492bf82020-09-09 20:02:44112 };
113
114 class CONTENT_EXPORT NodeRange {
115 public:
Kevin McNee5f594382021-05-06 23:18:23116 NodeRange(const NodeRange&);
117 ~NodeRange();
118
danakjc492bf82020-09-09 20:02:44119 NodeIterator begin();
120 NodeIterator end();
121
122 private:
123 friend class FrameTree;
124
Kevin McNee5f594382021-05-06 23:18:23125 NodeRange(const std::vector<FrameTreeNode*>& starting_nodes,
126 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48127 bool should_descend_into_inner_trees,
128 bool include_delegate_nodes_for_inner_frame_trees);
danakjc492bf82020-09-09 20:02:44129
Kevin McNee5f594382021-05-06 23:18:23130 const std::vector<FrameTreeNode*> starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52131 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23132 const bool should_descend_into_inner_trees_;
Dave Tapuskadda303d2022-10-04 16:56:48133 const bool include_delegate_nodes_for_inner_frame_trees_;
danakjc492bf82020-09-09 20:02:44134 };
135
Carlos Caballero03262522021-02-05 14:49:58136 class CONTENT_EXPORT Delegate {
137 public:
138 // A RenderFrameHost in the specified |frame_tree_node| started loading a
139 // new document. This corresponds to browser UI starting to show a spinner
140 // or other visual indicator for loading. This method is only invoked if the
Nate Chapin9aabf5f2021-11-12 00:31:19141 // FrameTree hadn't been previously loading. |should_show_loading_ui| will
142 // be true unless the load is a fragment navigation, or triggered by
Carlos Caballero03262522021-02-05 14:49:58143 // history.pushState/replaceState.
144 virtual void DidStartLoading(FrameTreeNode* frame_tree_node,
Nate Chapin9aabf5f2021-11-12 00:31:19145 bool should_show_loading_ui) = 0;
Carlos Caballero03262522021-02-05 14:49:58146
Takashi Toyoshima74090df62021-03-09 14:34:57147 // This is called when all nodes in the FrameTree stopped loading. This
Carlos Caballero03262522021-02-05 14:49:58148 // corresponds to the browser UI stop showing a spinner or other visual
149 // indicator for loading.
150 virtual void DidStopLoading() = 0;
151
Sreeja Kamishettyd64b993d2022-02-14 12:04:42152 // Returns the delegate's top loading tree, which should be used to infer
Yu Gaoc8c18552022-06-22 14:38:45153 // the values of loading-related states. The state of
154 // IsLoadingIncludingInnerFrameTrees() is a WebContents level concept and
155 // LoadingTree would return the frame tree to which loading events should be
156 // directed.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42157 //
158 // TODO(crbug.com/1261928): Remove this method and directly rely on
159 // GetOutermostMainFrame() once portals and guest views are migrated to
160 // MPArch.
161 virtual FrameTree* LoadingTree() = 0;
162
Carlos Caballero6ff6ace2021-02-05 16:53:00163 // Returns true when the active RenderWidgetHostView should be hidden.
164 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25165
Dominic Farolinoedf44ee2021-07-20 23:50:59166 // If the FrameTree using this delegate is an inner/nested FrameTree, then
167 // there may be a FrameTreeNode in the outer FrameTree that is considered
168 // our outer delegate FrameTreeNode. This method returns the outer delegate
169 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
170 // FrameTreeNode, this method returns
171 // FrameTreeNode::kFrameTreeNodeInvalidId.
172 virtual int GetOuterDelegateFrameTreeNodeId() = 0;
Dave Tapuskac8de3b02021-12-03 21:51:01173
174 // Returns if this FrameTree represents a portal.
175 virtual bool IsPortal() = 0;
Julie Jeongeun Kim2132b37f82022-11-23 08:30:46176
177 // Set the `node` frame as focused in its own FrameTree as well as possibly
178 // changing the focused frame tree in the case of inner/outer FrameTrees.
179 virtual void SetFocusedFrame(FrameTreeNode* node,
180 SiteInstanceGroup* source) = 0;
Carlos Caballero03262522021-02-05 14:49:58181 };
182
Sreeja Kamishetty74bacd522021-03-22 17:04:24183 // Type of FrameTree instance.
184 enum class Type {
185 // This FrameTree is the primary frame tree for the WebContents, whose main
186 // document URL is shown in the Omnibox.
187 kPrimary,
188
189 // This FrameTree is used to prerender a page in the background which is
190 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36191 kPrerender,
192
193 // This FrameTree is used to host the contents of a <fencedframe> element.
194 // Even for <fencedframe>s hosted inside a prerendered page, the FrameTree
195 // associated with the fenced frame will be kFencedFrame, but the
196 // RenderFrameHosts inside of it will have their lifecycle state set to
197 // `RenderFrameHost::LifecycleState::kActive`.
198 //
199 // TODO(crbug.com/1232528, crbug.com/1244274): We should either:
200 // * Fully support nested FrameTrees inside prerendered pages, in which
201 // case all of the RenderFrameHosts inside the nested trees should have
202 // their lifecycle state set to kPrerendering, or...
203 // * Ban nested FrameTrees in prerendered pages, and therefore obsolete the
204 // above paragraph about <fencedframe>s hosted in prerendered pages.
205 kFencedFrame
Sreeja Kamishetty74bacd522021-03-22 17:04:24206 };
Sreeja Kamishetty837a10402021-04-23 12:41:59207
208 // A set of delegates are remembered here so that we can create
209 // RenderFrameHostManagers.
210 FrameTree(BrowserContext* browser_context,
211 Delegate* delegate,
212 NavigationControllerDelegate* navigation_controller_delegate,
213 NavigatorDelegate* navigator_delegate,
214 RenderFrameHostDelegate* render_frame_delegate,
215 RenderViewHostDelegate* render_view_delegate,
216 RenderWidgetHostDelegate* render_widget_delegate,
217 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26218 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44219 Type type);
Peter Boström828b9022021-09-21 02:28:43220
221 FrameTree(const FrameTree&) = delete;
222 FrameTree& operator=(const FrameTree&) = delete;
223
Sreeja Kamishetty837a10402021-04-23 12:41:59224 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24225
Carlos Caballero40b0efd2021-01-26 11:55:00226 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17227 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni46087a12022-11-11 08:28:38228 // creates an initial NavigationEntry that potentially inherits
229 // `opener_for_origin`'s origin in its NavigationController. This method will
230 // call back into the delegates so it should only be called once they have
231 // completed their initialization. Pass in frame_policy so that it can be set
232 // in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00233 // TODO(carlscab): It would be great if initialization could happened in the
234 // constructor so we do not leave objects in a half initialized state.
Charlie Reis37be2682023-01-10 17:04:47235 void Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00236 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17237 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46238 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44239 const blink::FramePolicy& frame_policy,
240 const base::UnguessableToken& devtools_frame_token);
Sreeja Kamishetty837a10402021-04-23 12:41:59241
242 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00243
Paul Semel3e241042022-10-11 12:57:31244 FrameTreeNode* root() { return &root_; }
245 const FrameTreeNode* root() const { return &root_; }
danakjc492bf82020-09-09 20:02:44246
Sreeja Kamishetty74bacd522021-03-22 17:04:24247 bool is_prerendering() const { return type_ == FrameTree::Type::kPrerender; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46248
Sreeja Kamishetty60e47132022-02-08 12:51:53249 // Returns true if this frame tree is a portal.
250 //
251 // TODO(crbug.com/1254770): Once portals are migrated to MPArch, portals will
252 // have their own FrameTree::Type.
253 bool IsPortal();
254
Carlos Caballero03262522021-02-05 14:49:58255 Delegate* delegate() { return delegate_; }
256
Jeremy Roman2d8dfe132021-07-06 20:51:26257 // Delegates for various objects. These can be kept centrally on the
258 // FrameTree because they are expected to be the same for all frames on a
259 // given FrameTree.
danakjc492bf82020-09-09 20:02:44260 RenderFrameHostDelegate* render_frame_delegate() {
261 return render_frame_delegate_;
262 }
263 RenderViewHostDelegate* render_view_delegate() {
264 return render_view_delegate_;
265 }
266 RenderWidgetHostDelegate* render_widget_delegate() {
267 return render_widget_delegate_;
268 }
269 RenderFrameHostManager::Delegate* manager_delegate() {
270 return manager_delegate_;
271 }
Jeremy Roman2d8dfe132021-07-06 20:51:26272 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09273
Albert J. Wong1b6dc962021-07-09 18:06:57274 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
Sharon Yangc581a0c2021-11-02 18:09:39275
Sharon Yang7ce309e2023-01-19 21:39:57276 // A map to store RenderViewHosts, keyed by SiteInstanceGroup ID.
277 // This map does not cover all RenderViewHosts in a FrameTree. See
278 // `speculative_render_view_host_`.
Aaron Colwell78b4bde2021-03-16 16:16:09279 using RenderViewHostMap = std::unordered_map<RenderViewHostMapId,
280 RenderViewHostImpl*,
281 RenderViewHostMapId::Hasher>;
Sharon Yang7ce309e2023-01-19 21:39:57282 // TODO(yangsharon, crbug.com/1336305): Change this to ForEachRenderViewHost
283 // to include `speculative_render_view_host_`.
Aaron Colwell78b4bde2021-03-16 16:16:09284 const RenderViewHostMap& render_view_hosts() const {
danakjc492bf82020-09-09 20:02:44285 return render_view_host_map_;
286 }
287
Sharon Yang7ce309e2023-01-19 21:39:57288 // Speculative RenderViewHost accessors.
289 RenderViewHostImpl* speculative_render_view_host() const {
290 return speculative_render_view_host_.get();
291 }
292 void set_speculative_render_view_host(
293 base::WeakPtr<RenderViewHostImpl> render_view_host) {
294 speculative_render_view_host_ = render_view_host;
295 }
296
297 // Moves `speculative_render_view_host_` to `render_view_host_map_`. This
298 // should be called every time a main-frame same-SiteInstanceGroup speculative
299 // RenderFrameHost gets swapped in and becomes the active RenderFrameHost.
300 // This overwrites the previous RenderViewHost for the SiteInstanceGroup in
301 // `render_view_host_map_`, if one exists.
302 void MakeSpeculativeRVHCurrent();
303
danakjc492bf82020-09-09 20:02:44304 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
305 // of this FrameTree.
306 FrameTreeNode* FindByID(int frame_tree_node_id);
307
308 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
309 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
310
311 // Returns the first frame in this tree with the given |name|, or the main
312 // frame if |name| is empty.
313 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
314 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
315 FrameTreeNode* FindByName(const std::string& name);
316
317 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
318 // breadth-first traversal order.
319 NodeRange Nodes();
320
321 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
322 // frame tree, starting from |subtree_root|.
323 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
324
Kevin McNee53f0b2d2021-11-02 18:00:45325 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
326 // well as any FrameTreeNodes of inner frame trees. Note that this includes
327 // inner frame trees of inner WebContents as well.
328 NodeRange NodesIncludingInnerTreeNodes();
329
Kevin McNee5f594382021-05-06 23:18:23330 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
331 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45332 // frame trees. Note that this includes inner frame trees of inner WebContents
Dave Tapuskadda303d2022-10-04 16:56:48333 // as well. If `include_delegate_nodes_for_inner_frame_trees` is true the
334 // delegate RenderFrameHost owning the inner frame tree will also be returned.
335 static NodeRange SubtreeAndInnerTreeNodes(
336 RenderFrameHostImpl* parent,
337 bool include_delegate_nodes_for_inner_frame_trees = false);
Kevin McNee5f594382021-05-06 23:18:23338
danakjc492bf82020-09-09 20:02:44339 // Adds a new child frame to the frame tree. |process_id| is required to
340 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41341 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44342 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
343 // interface through which the child RenderFrame can access Mojo services
344 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30345 // sending the client end of the interface down to the
346 // RenderFrame. |policy_container_bind_params|, if not null, is used for
347 // binding Blink's policy container to the new RenderFrameHost's
348 // PolicyContainerHost. This is only needed if this frame is the result of the
349 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41350 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
351 // the added frame is only to serve as a placeholder for an inner frame tree
352 // (e.g. fenced frames, portals) and will not have a live RenderFrame of its
353 // own.
danakjc492bf82020-09-09 20:02:44354 FrameTreeNode* AddFrame(
355 RenderFrameHostImpl* parent,
356 int process_id,
357 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18358 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44359 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
360 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30361 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49362 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
363 associated_interface_provider_receiver,
danakjc492bf82020-09-09 20:02:44364 blink::mojom::TreeScopeType scope,
365 const std::string& frame_name,
366 const std::string& frame_unique_name,
367 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04368 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44369 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34370 const blink::DocumentToken& document_token,
danakjc492bf82020-09-09 20:02:44371 const blink::FramePolicy& frame_policy,
372 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
373 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41374 blink::FrameOwnerElementType owner_type,
375 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44376
377 // Removes a frame from the frame tree. |child|, its children, and objects
378 // owned by their RenderFrameHostManagers are immediately deleted. The root
379 // node cannot be removed this way.
380 void RemoveFrame(FrameTreeNode* child);
381
382 // This method walks the entire frame tree and creates a RenderFrameProxyHost
383 // for the given |site_instance| in each node except the |source| one --
384 // the source will have a RenderFrameHost. |source| may be null if there is
385 // no node navigating in this frame tree (such as when this is called
386 // for an opener's frame tree), in which case no nodes are skipped for
Harkiran Bolaria2912a6b32022-02-22 16:43:45387 // RenderFrameProxyHost creation. |source_new_browsing_context_state| is the
388 // BrowsingContextState used by the speculative frame host, which may differ
389 // from the BrowsingContextState in |source| during cross-origin cross-
390 // browsing-instance navigations.
danakjc492bf82020-09-09 20:02:44391 void CreateProxiesForSiteInstance(FrameTreeNode* source,
Charlie Reis37be2682023-01-10 17:04:47392 SiteInstanceImpl* site_instance,
Harkiran Bolaria2912a6b32022-02-22 16:43:45393 const scoped_refptr<BrowsingContextState>&
394 source_new_browsing_context_state);
danakjc492bf82020-09-09 20:02:44395
396 // Convenience accessor for the main frame's RenderFrameHostImpl.
397 RenderFrameHostImpl* GetMainFrame() const;
398
399 // Returns the focused frame.
400 FrameTreeNode* GetFocusedFrame();
401
Sharon Yangefe52632022-03-08 23:06:06402 // Sets the focused frame to |node|. |source| identifies the
403 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
404 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
405 // notified about the new focused frame. Note that |source| may differ from
406 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
407 // window.focus() calls).
408 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44409
danakjc492bf82020-09-09 20:02:44410 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
411 //
412 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
413 // of this object.
Sharon Yang7ce309e2023-01-19 21:39:57414 // `create_case` indicates whether or not the RenderViewHost being created is
415 // speculative or not. It should only be registered with the FrameTree if it
416 // is not speculative.
danakjc492bf82020-09-09 20:02:44417 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
Charlie Reis37be2682023-01-10 17:04:47418 SiteInstanceImpl* site_instance,
danakjc492bf82020-09-09 20:02:44419 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58420 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57421 scoped_refptr<BrowsingContextState> main_browsing_context_state,
422 CreateRenderViewHostCase create_case);
danakjc492bf82020-09-09 20:02:44423
424 // Returns the existing RenderViewHost for a new RenderFrameHost.
425 // There should always be such a RenderViewHost, because the main frame
426 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang7ce309e2023-01-19 21:39:57427 // Note that this will never return `speculative_render_view_host_`. If that
428 // is needed, call `speculative_render_view_host()` instead.
Sharon Yang57bde122022-03-01 20:01:12429 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44430
Sharon Yangc581a0c2021-11-02 18:09:39431 // Returns the ID used for the RenderViewHost associated with
432 // |site_instance_group|.
433 RenderViewHostMapId GetRenderViewHostMapId(
434 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09435
danakjc492bf82020-09-09 20:02:44436 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09437 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44438 //
439 // This method does not take ownership of|rvh|.
440 //
441 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
442 // |rvh|'s SiteInstance.
443 //
444 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
445 // *must* be called for |rvh| when it is destroyed or put into the
446 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
447 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09448 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44449
450 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09451 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
452 // called for a |render_view_host| that is not currently set for reuse.
453 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13454 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44455
456 // This is called when the frame is about to be removed and started to run
457 // unload handlers.
458 void FrameUnloading(FrameTreeNode* frame);
459
460 // This is only meant to be called by FrameTreeNode. Triggers calling
461 // the listener installed by SetFrameRemoveListener.
462 void FrameRemoved(FrameTreeNode* frame);
463
Carlos Caballero03262522021-02-05 14:49:58464 void DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19465 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58466 bool was_previously_loading);
467 void DidStopLoadingNode(FrameTreeNode& node);
Carlos Caballero03262522021-02-05 14:49:58468 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44469
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15470 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
471 // is navigating returns `blink::kInitialLoadProgress`.
472 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44473
Sreeja Kamishetty15f9944a22022-03-10 10:16:08474 // Returns true if at least one of the nodes in this frame tree or nodes in
475 // any inner frame tree of the same WebContents is loading.
Yu Gaoc8c18552022-06-22 14:38:45476 bool IsLoadingIncludingInnerFrameTrees() const;
danakjc492bf82020-09-09 20:02:44477
478 // Set page-level focus in all SiteInstances involved in rendering
479 // this FrameTree, not including the current main frame's
480 // SiteInstance. The focus update will be sent via the main frame's proxies
481 // in those SiteInstances.
482 void ReplicatePageFocus(bool is_focused);
483
484 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06485 // identified by |group|.
486 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44487
W. James MacLeanc07dc41b2022-07-25 18:52:16488 // Walks the current frame tree and registers any origins matching
489 // `previously_visited_origin`, either the last committed origin of a
490 // RenderFrameHost or the origin associated with a NavigationRequest that has
491 // been assigned to a SiteInstance, as having the default origin isolation
492 // state. This is only necessary when `previously_visited_origin` is seen with
493 // an OriginAgentCluster header explicitly requesting something other than the
494 // default.
495 void RegisterExistingOriginAsHavingDefaultIsolation(
danakjc492bf82020-09-09 20:02:44496 const url::Origin& previously_visited_origin,
497 NavigationRequest* navigation_request_to_exclude);
498
Carlos Caballero40b0efd2021-01-26 11:55:00499 NavigationControllerImpl& controller() { return navigator_.controller(); }
danakjc492bf82020-09-09 20:02:44500 Navigator& navigator() { return navigator_; }
501
Carlos Caballeroede6f8c2021-01-28 11:01:50502 // Another page accessed the initial empty main document, which means it
503 // is no longer safe to display a pending URL without risking a URL spoof.
504 void DidAccessInitialMainDocument();
505
506 bool has_accessed_initial_main_document() const {
507 return has_accessed_initial_main_document_;
508 }
509
510 void ResetHasAccessedInitialMainDocument() {
511 has_accessed_initial_main_document_ = false;
512 }
513
Carlos Caballero6ff6ace2021-02-05 16:53:00514 bool IsHidden() const { return delegate_->IsHidden(); }
515
Sreeja Kamishettyd64b993d2022-02-14 12:04:42516 // LoadingTree returns the following for different frame trees to direct
517 // loading related events. Please see FrameTree::Delegate::LoadingTree for
518 // more comments.
519 // - For prerender frame tree -> returns the frame tree itself.
520 // - For fenced frame and primary frame tree (including portal) -> returns
521 // the delegate's primary frame tree.
522 FrameTree* LoadingTree();
523
Carlos Caballero03262522021-02-05 14:49:58524 // Stops all ongoing navigations in each of the nodes of this FrameTree.
525 void StopLoading();
526
Carlos Caballero101ac26b2021-03-24 11:54:05527 // Prepares this frame tree for destruction, cleaning up the internal state
528 // and firing the appropriate events like FrameDeleted.
529 // Must be called before FrameTree is destroyed.
530 void Shutdown();
531
Takashi Toyoshimaea534ef22021-07-21 03:27:59532 bool IsBeingDestroyed() const { return is_being_destroyed_; }
533
Dave Tapuskad8b0530f2021-10-19 15:12:31534 base::SafeRef<FrameTree> GetSafeRef();
535
Dave Tapuska54c76a032021-10-27 22:10:42536 // Walks up the FrameTree chain and focuses the FrameTreeNode where
537 // each inner FrameTree is attached.
538 void FocusOuterFrameTrees();
539
danakjc492bf82020-09-09 20:02:44540 private:
541 friend class FrameTreeTest;
542 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Sreeja Kamishettyd64b993d2022-02-14 12:04:42543 FRIEND_TEST_ALL_PREFIXES(PortalBrowserTest, NodesForIsLoading);
Xiaochen Zhou6443f752022-08-10 16:40:46544 FRIEND_TEST_ALL_PREFIXES(FencedFrameMPArchBrowserTest, NodesForIsLoading);
Sharon Yang2f3304a2022-05-13 02:24:25545 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
546 CreateRenderViewAfterProcessKillAndClosedProxy);
danakjc492bf82020-09-09 20:02:44547
548 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
549 // breadth-first traversal order, skipping the subtree rooted at
550 // |node|, but including |node| itself.
551 NodeRange NodesExceptSubtree(FrameTreeNode* node);
552
Sreeja Kamishettyd64b993d2022-02-14 12:04:42553 // Returns all FrameTreeNodes in this frame tree, as well as any
554 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
555 // frame trees of inner delegates. This is used to find the aggregate
556 // IsLoading value for a frame tree.
557 //
558 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
559 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
560 // portals and guest views are migrated to MPArch.
561 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
562
Keishi Hattori0e45c022021-11-27 09:25:52563 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58564
danakjc492bf82020-09-09 20:02:44565 // These delegates are installed into all the RenderViewHosts and
566 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52567 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
568 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
569 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
570 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
571 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44572
573 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00574 // tree. Each FrameTreeNode will default to using it for navigation tasks in
575 // the frame.
danakjc492bf82020-09-09 20:02:44576 Navigator navigator_;
577
Aaron Colwell78b4bde2021-03-16 16:16:09578 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44579 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
580 // Each RenderViewHost maintains a refcount and is deleted when there are no
581 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09582 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44583
Sharon Yang7ce309e2023-01-19 21:39:57584 // A speculative RenderViewHost is created for all speculative cross-page
585 // same-SiteInstanceGroup RenderFrameHosts. When the corresponding
586 // RenderFrameHost gets committed and becomes the current RenderFrameHost,
587 // `speculative_render_view_host_` will be moved to `render_view_host_map_`,
588 // overwriting the previous RenderViewHost of the same SiteInstanceGroup, if
589 // applicable. This field will also be reset at that time, or if the
590 // speculative RenderFrameHost gets deleted.
591 //
592 // For any given FrameTree, there will be at most one
593 // `speculative_render_view_host_`, because only main-frame speculative
594 // RenderFrameHosts have speculative RenderViewHosts, and there is at most one
595 // such RenderFrameHost per FrameTree at a time.
596 // This is a WeakPtr, since the RenderViewHost is owned by the
597 // RenderFrameHostImpl, not the FrameTree. This implies that if the owning
598 // RenderFrameHostImpl gets deleted, this will too.
599 //
600 // This supports but is independent of RenderDocument, which introduces cases
601 // where there may be more than one RenderViewHost per SiteInstanceGroup, such
602 // as cross-page same-SiteInstanceGroup navigations. The speculative
603 // RenderFrameHost has an associated RenderViewHost, but it cannot be put in
604 // `render_view_host_map_` when it is created, as the existing RenderViewHost
605 // will be incorrectly overwritten.
606 // TODO(yangsharon, crbug.com/1336305): Expand support to include
607 // cross-SiteInstanceGroup main-frame navigations, so all main-frame
608 // navigations use speculative RenderViewHost.
609 base::WeakPtr<RenderViewHostImpl> speculative_render_view_host_;
610
Harkiran Bolaria16f2c48d2022-04-22 12:39:57611 // Indicates type of frame tree.
612 const Type type_;
613
danakjc492bf82020-09-09 20:02:44614 int focused_frame_tree_node_id_;
615
danakjc492bf82020-09-09 20:02:44616 // Overall load progress.
617 double load_progress_;
618
Carlos Caballeroede6f8c2021-01-28 11:01:50619 // Whether the initial empty page has been accessed by another page, making it
620 // unsafe to show the pending URL. Usually false unless another window tries
621 // to modify the blank page. Always false after the first commit.
622 bool has_accessed_initial_main_document_ = false;
623
Takashi Toyoshimaea534ef22021-07-21 03:27:59624 bool is_being_destroyed_ = false;
625
Carlos Caballero101ac26b2021-03-24 11:54:05626#if DCHECK_IS_ON()
627 // Whether Shutdown() was called.
628 bool was_shut_down_ = false;
629#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31630
Paul Semel3e241042022-10-11 12:57:31631 // The root FrameTreeNode.
632 //
633 // Note: It is common for a node to test whether it's the root node, via the
634 // `root()` method, even while `root_` is running its destructor.
635 // For that reason, we want to destroy |root_| before any other fields.
636 FrameTreeNode root_;
637
Dave Tapuskad8b0530f2021-10-19 15:12:31638 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44639};
640
641} // namespace content
642
643#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_