blob: a26e26dece5fcb9d2ae251c2e446693954a0dcbd [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"
Ari Chivukula5d15efb2023-01-21 04:33:5234#include "url/origin.h"
danakjc492bf82020-09-09 20:02:4435
36namespace blink {
Lei Zhang4a867672021-07-19 06:30:1437namespace mojom {
38class BrowserInterfaceBroker;
39enum class TreeScopeType;
40} // namespace mojom
41
danakjc492bf82020-09-09 20:02:4442struct FramePolicy;
Ari Chivukula5d15efb2023-01-21 04:33:5243class StorageKey;
danakjc492bf82020-09-09 20:02:4444} // namespace blink
45
46namespace content {
47
Carlos Caballero40b0efd2021-01-26 11:55:0048class BrowserContext;
Jeremy Roman2d8dfe132021-07-06 20:51:2649class PageDelegate;
danakjc492bf82020-09-09 20:02:4450class RenderFrameHostDelegate;
51class RenderViewHostDelegate;
52class RenderViewHostImpl;
53class RenderFrameHostManager;
54class RenderWidgetHostDelegate;
Carlos Caballero40b0efd2021-01-26 11:55:0055class SiteInstance;
Sharon Yang57bde122022-03-01 20:01:1256class SiteInstanceGroup;
danakjc492bf82020-09-09 20:02:4457
58// Represents the frame tree for a page. With the exception of the main frame,
59// all FrameTreeNodes will be created/deleted in response to frame attach and
60// detach events in the DOM.
61//
62// The main frame's FrameTreeNode is special in that it is reused. This allows
63// it to serve as an anchor for state that needs to persist across top-level
64// page navigations.
65//
66// TODO(ajwong): Move NavigationController ownership to the main frame
67// FrameTreeNode. Possibly expose access to it from here.
68//
69// This object is only used on the UI thread.
70class CONTENT_EXPORT FrameTree {
71 public:
72 class NodeRange;
73
Alan Zhao8a882d12022-05-18 01:32:1574 class CONTENT_EXPORT NodeIterator {
danakjc492bf82020-09-09 20:02:4475 public:
Alan Zhao8a882d12022-05-18 01:32:1576 using iterator_category = std::forward_iterator_tag;
77 using value_type = FrameTreeNode*;
78 using difference_type = std::ptrdiff_t;
79 using pointer = FrameTreeNode**;
80 using reference = FrameTreeNode*&;
81
danakjc492bf82020-09-09 20:02:4482 NodeIterator(const NodeIterator& other);
83 ~NodeIterator();
84
85 NodeIterator& operator++();
Kevin McNee5f594382021-05-06 23:18:2386 // Advances the iterator and excludes the children of the current node
87 NodeIterator& AdvanceSkippingChildren();
danakjc492bf82020-09-09 20:02:4488
89 bool operator==(const NodeIterator& rhs) const;
90 bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); }
91
92 FrameTreeNode* operator*() { return current_node_; }
93
94 private:
Jayson Adams4db0bfe22021-07-15 19:24:0795 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4496 friend class NodeRange;
97
Kevin McNee5f594382021-05-06 23:18:2398 NodeIterator(const std::vector<FrameTreeNode*>& starting_nodes,
99 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48100 bool should_descend_into_inner_trees,
101 bool include_delegate_nodes_for_inner_frame_trees);
Kevin McNee5f594382021-05-06 23:18:23102
103 void AdvanceNode();
danakjc492bf82020-09-09 20:02:44104
Lukasz Anforowicz877e6222021-11-26 00:03:23105 // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for
106 // performance reasons (based on analysis of sampling profiler data and
107 // tab_search:top100:2020).
Keishi Hattori488b7602022-05-02 13:09:31108 RAW_PTR_EXCLUSION FrameTreeNode* current_node_;
109 RAW_PTR_EXCLUSION const FrameTreeNode* const root_of_subtree_to_skip_;
Lukasz Anforowicz877e6222021-11-26 00:03:23110
Kevin McNee5f594382021-05-06 23:18:23111 const bool should_descend_into_inner_trees_;
Dave Tapuskadda303d2022-10-04 16:56:48112 const bool include_delegate_nodes_for_inner_frame_trees_;
Jayson Adams4db0bfe22021-07-15 19:24:07113 base::circular_deque<FrameTreeNode*> queue_;
danakjc492bf82020-09-09 20:02:44114 };
115
116 class CONTENT_EXPORT NodeRange {
117 public:
Kevin McNee5f594382021-05-06 23:18:23118 NodeRange(const NodeRange&);
119 ~NodeRange();
120
danakjc492bf82020-09-09 20:02:44121 NodeIterator begin();
122 NodeIterator end();
123
124 private:
125 friend class FrameTree;
126
Kevin McNee5f594382021-05-06 23:18:23127 NodeRange(const std::vector<FrameTreeNode*>& starting_nodes,
128 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48129 bool should_descend_into_inner_trees,
130 bool include_delegate_nodes_for_inner_frame_trees);
danakjc492bf82020-09-09 20:02:44131
Kevin McNee5f594382021-05-06 23:18:23132 const std::vector<FrameTreeNode*> starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52133 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23134 const bool should_descend_into_inner_trees_;
Dave Tapuskadda303d2022-10-04 16:56:48135 const bool include_delegate_nodes_for_inner_frame_trees_;
danakjc492bf82020-09-09 20:02:44136 };
137
Carlos Caballero03262522021-02-05 14:49:58138 class CONTENT_EXPORT Delegate {
139 public:
Nate Chapin470dbc62023-04-25 16:34:38140 // The FrameTree changed its LoadingState. This can be a transition between
141 // not-loading and loading (in which case it will be accompanied by either a
142 // DidStartLoading or DidStopLoading), or a transition between not showing
143 // loading UI and showing loading UI while a navigation is in progress (in
144 // which case it will be called without either DidStartLoading or
145 // DidStopLoading).
146 virtual void LoadingStateChanged(LoadingState new_state) = 0;
Carlos Caballero03262522021-02-05 14:49:58147
Nate Chapin470dbc62023-04-25 16:34:38148 // The FrameTree has started loading in `frame_tree_node`. Note that this
149 // is only called when the FrameTree as a whole goes from not-loading to
150 // loading. If a second FrameTreeNode begins loading, a new DidStartLoading
151 // message will not be sent.
152 virtual void DidStartLoading(FrameTreeNode* frame_tree_node) = 0;
153
154 // The FrameTree has stopped loading. Sent only when all FrameTreeNodes have
155 // stopped loading.
Carlos Caballero03262522021-02-05 14:49:58156 virtual void DidStopLoading() = 0;
157
Sreeja Kamishettyd64b993d2022-02-14 12:04:42158 // Returns the delegate's top loading tree, which should be used to infer
Yu Gaoc8c18552022-06-22 14:38:45159 // the values of loading-related states. The state of
160 // IsLoadingIncludingInnerFrameTrees() is a WebContents level concept and
161 // LoadingTree would return the frame tree to which loading events should be
162 // directed.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42163 //
164 // TODO(crbug.com/1261928): Remove this method and directly rely on
165 // GetOutermostMainFrame() once portals and guest views are migrated to
166 // MPArch.
167 virtual FrameTree* LoadingTree() = 0;
168
Carlos Caballero6ff6ace2021-02-05 16:53:00169 // Returns true when the active RenderWidgetHostView should be hidden.
170 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25171
Dominic Farolinoedf44ee2021-07-20 23:50:59172 // If the FrameTree using this delegate is an inner/nested FrameTree, then
173 // there may be a FrameTreeNode in the outer FrameTree that is considered
174 // our outer delegate FrameTreeNode. This method returns the outer delegate
175 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
176 // FrameTreeNode, this method returns
177 // FrameTreeNode::kFrameTreeNodeInvalidId.
178 virtual int GetOuterDelegateFrameTreeNodeId() = 0;
Dave Tapuskac8de3b02021-12-03 21:51:01179
Kevin McNee86e64ee2023-02-17 16:35:50180 // If the FrameTree using this delegate is an inner/nested FrameTree that
181 // has not yet been attached to an outer FrameTreeNode, returns the parent
182 // RenderFrameHost of the intended outer FrameTreeNode to which the inner
183 // frame tree will be attached. This is usually the RenderFrameHost that is
184 // the outer document once attachment occurs, however in the case of some
185 // kinds of GuestView, the outer document may end up being a same-origin
186 // subframe of the RenderFrameHost returned by this method (see the
187 // `testNewWindowAttachInSubFrame` webview test for an example of this).
188 // Otherwise, returns null.
189 virtual RenderFrameHostImpl* GetProspectiveOuterDocument() = 0;
190
Dave Tapuskac8de3b02021-12-03 21:51:01191 // Returns if this FrameTree represents a portal.
192 virtual bool IsPortal() = 0;
Julie Jeongeun Kim2132b37f82022-11-23 08:30:46193
194 // Set the `node` frame as focused in its own FrameTree as well as possibly
195 // changing the focused frame tree in the case of inner/outer FrameTrees.
196 virtual void SetFocusedFrame(FrameTreeNode* node,
197 SiteInstanceGroup* source) = 0;
Carlos Caballero03262522021-02-05 14:49:58198 };
199
Sreeja Kamishetty74bacd522021-03-22 17:04:24200 // Type of FrameTree instance.
201 enum class Type {
202 // This FrameTree is the primary frame tree for the WebContents, whose main
203 // document URL is shown in the Omnibox.
204 kPrimary,
205
206 // This FrameTree is used to prerender a page in the background which is
207 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36208 kPrerender,
209
210 // This FrameTree is used to host the contents of a <fencedframe> element.
211 // Even for <fencedframe>s hosted inside a prerendered page, the FrameTree
212 // associated with the fenced frame will be kFencedFrame, but the
213 // RenderFrameHosts inside of it will have their lifecycle state set to
214 // `RenderFrameHost::LifecycleState::kActive`.
215 //
216 // TODO(crbug.com/1232528, crbug.com/1244274): We should either:
217 // * Fully support nested FrameTrees inside prerendered pages, in which
218 // case all of the RenderFrameHosts inside the nested trees should have
219 // their lifecycle state set to kPrerendering, or...
220 // * Ban nested FrameTrees in prerendered pages, and therefore obsolete the
221 // above paragraph about <fencedframe>s hosted in prerendered pages.
222 kFencedFrame
Sreeja Kamishetty74bacd522021-03-22 17:04:24223 };
Sreeja Kamishetty837a10402021-04-23 12:41:59224
225 // A set of delegates are remembered here so that we can create
226 // RenderFrameHostManagers.
227 FrameTree(BrowserContext* browser_context,
228 Delegate* delegate,
229 NavigationControllerDelegate* navigation_controller_delegate,
230 NavigatorDelegate* navigator_delegate,
231 RenderFrameHostDelegate* render_frame_delegate,
232 RenderViewHostDelegate* render_view_delegate,
233 RenderWidgetHostDelegate* render_widget_delegate,
234 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26235 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44236 Type type);
Peter Boström828b9022021-09-21 02:28:43237
238 FrameTree(const FrameTree&) = delete;
239 FrameTree& operator=(const FrameTree&) = delete;
240
Sreeja Kamishetty837a10402021-04-23 12:41:59241 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24242
Carlos Caballero40b0efd2021-01-26 11:55:00243 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17244 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni46087a12022-11-11 08:28:38245 // creates an initial NavigationEntry that potentially inherits
246 // `opener_for_origin`'s origin in its NavigationController. This method will
247 // call back into the delegates so it should only be called once they have
248 // completed their initialization. Pass in frame_policy so that it can be set
249 // in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00250 // TODO(carlscab): It would be great if initialization could happened in the
251 // constructor so we do not leave objects in a half initialized state.
Charlie Reis37be2682023-01-10 17:04:47252 void Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00253 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17254 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46255 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44256 const blink::FramePolicy& frame_policy,
257 const base::UnguessableToken& devtools_frame_token);
Sreeja Kamishetty837a10402021-04-23 12:41:59258
259 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00260
Paul Semel3e241042022-10-11 12:57:31261 FrameTreeNode* root() { return &root_; }
262 const FrameTreeNode* root() const { return &root_; }
danakjc492bf82020-09-09 20:02:44263
Sreeja Kamishetty74bacd522021-03-22 17:04:24264 bool is_prerendering() const { return type_ == FrameTree::Type::kPrerender; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46265
Sreeja Kamishetty60e47132022-02-08 12:51:53266 // Returns true if this frame tree is a portal.
267 //
268 // TODO(crbug.com/1254770): Once portals are migrated to MPArch, portals will
269 // have their own FrameTree::Type.
270 bool IsPortal();
271
Carlos Caballero03262522021-02-05 14:49:58272 Delegate* delegate() { return delegate_; }
273
Jeremy Roman2d8dfe132021-07-06 20:51:26274 // Delegates for various objects. These can be kept centrally on the
275 // FrameTree because they are expected to be the same for all frames on a
276 // given FrameTree.
danakjc492bf82020-09-09 20:02:44277 RenderFrameHostDelegate* render_frame_delegate() {
278 return render_frame_delegate_;
279 }
280 RenderViewHostDelegate* render_view_delegate() {
281 return render_view_delegate_;
282 }
283 RenderWidgetHostDelegate* render_widget_delegate() {
284 return render_widget_delegate_;
285 }
286 RenderFrameHostManager::Delegate* manager_delegate() {
287 return manager_delegate_;
288 }
Jeremy Roman2d8dfe132021-07-06 20:51:26289 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09290
Sharon Yanged884542023-02-02 17:33:44291 // Iterate over all RenderViewHosts, including speculative RenderViewHosts.
292 // See `speculative_render_view_host_` for more details.
293 void ForEachRenderViewHost(
294 base::FunctionRef<void(RenderViewHostImpl*)> on_host);
danakjc492bf82020-09-09 20:02:44295
Sharon Yang7ce309e2023-01-19 21:39:57296 // Speculative RenderViewHost accessors.
297 RenderViewHostImpl* speculative_render_view_host() const {
298 return speculative_render_view_host_.get();
299 }
300 void set_speculative_render_view_host(
301 base::WeakPtr<RenderViewHostImpl> render_view_host) {
302 speculative_render_view_host_ = render_view_host;
303 }
304
305 // Moves `speculative_render_view_host_` to `render_view_host_map_`. This
306 // should be called every time a main-frame same-SiteInstanceGroup speculative
307 // RenderFrameHost gets swapped in and becomes the active RenderFrameHost.
308 // This overwrites the previous RenderViewHost for the SiteInstanceGroup in
309 // `render_view_host_map_`, if one exists.
310 void MakeSpeculativeRVHCurrent();
311
danakjc492bf82020-09-09 20:02:44312 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
313 // of this FrameTree.
314 FrameTreeNode* FindByID(int frame_tree_node_id);
315
316 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
317 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
318
319 // Returns the first frame in this tree with the given |name|, or the main
320 // frame if |name| is empty.
321 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
322 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
323 FrameTreeNode* FindByName(const std::string& name);
324
325 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
326 // breadth-first traversal order.
327 NodeRange Nodes();
328
329 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
330 // frame tree, starting from |subtree_root|.
331 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
332
Kevin McNee53f0b2d2021-11-02 18:00:45333 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
334 // well as any FrameTreeNodes of inner frame trees. Note that this includes
335 // inner frame trees of inner WebContents as well.
336 NodeRange NodesIncludingInnerTreeNodes();
337
Kevin McNee5f594382021-05-06 23:18:23338 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
339 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45340 // frame trees. Note that this includes inner frame trees of inner WebContents
Dave Tapuskadda303d2022-10-04 16:56:48341 // as well. If `include_delegate_nodes_for_inner_frame_trees` is true the
342 // delegate RenderFrameHost owning the inner frame tree will also be returned.
343 static NodeRange SubtreeAndInnerTreeNodes(
344 RenderFrameHostImpl* parent,
345 bool include_delegate_nodes_for_inner_frame_trees = false);
Kevin McNee5f594382021-05-06 23:18:23346
danakjc492bf82020-09-09 20:02:44347 // Adds a new child frame to the frame tree. |process_id| is required to
348 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41349 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44350 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
351 // interface through which the child RenderFrame can access Mojo services
352 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30353 // sending the client end of the interface down to the
354 // RenderFrame. |policy_container_bind_params|, if not null, is used for
355 // binding Blink's policy container to the new RenderFrameHost's
356 // PolicyContainerHost. This is only needed if this frame is the result of the
357 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41358 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
359 // the added frame is only to serve as a placeholder for an inner frame tree
360 // (e.g. fenced frames, portals) and will not have a live RenderFrame of its
361 // own.
danakjc492bf82020-09-09 20:02:44362 FrameTreeNode* AddFrame(
363 RenderFrameHostImpl* parent,
364 int process_id,
365 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18366 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44367 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
368 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30369 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49370 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
371 associated_interface_provider_receiver,
danakjc492bf82020-09-09 20:02:44372 blink::mojom::TreeScopeType scope,
373 const std::string& frame_name,
374 const std::string& frame_unique_name,
375 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04376 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44377 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34378 const blink::DocumentToken& document_token,
danakjc492bf82020-09-09 20:02:44379 const blink::FramePolicy& frame_policy,
380 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
381 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41382 blink::FrameOwnerElementType owner_type,
383 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44384
385 // Removes a frame from the frame tree. |child|, its children, and objects
386 // owned by their RenderFrameHostManagers are immediately deleted. The root
387 // node cannot be removed this way.
388 void RemoveFrame(FrameTreeNode* child);
389
390 // This method walks the entire frame tree and creates a RenderFrameProxyHost
391 // for the given |site_instance| in each node except the |source| one --
392 // the source will have a RenderFrameHost. |source| may be null if there is
393 // no node navigating in this frame tree (such as when this is called
394 // for an opener's frame tree), in which case no nodes are skipped for
Harkiran Bolaria2912a6b32022-02-22 16:43:45395 // RenderFrameProxyHost creation. |source_new_browsing_context_state| is the
396 // BrowsingContextState used by the speculative frame host, which may differ
397 // from the BrowsingContextState in |source| during cross-origin cross-
398 // browsing-instance navigations.
danakjc492bf82020-09-09 20:02:44399 void CreateProxiesForSiteInstance(FrameTreeNode* source,
Charlie Reis37be2682023-01-10 17:04:47400 SiteInstanceImpl* site_instance,
Harkiran Bolaria2912a6b32022-02-22 16:43:45401 const scoped_refptr<BrowsingContextState>&
402 source_new_browsing_context_state);
danakjc492bf82020-09-09 20:02:44403
404 // Convenience accessor for the main frame's RenderFrameHostImpl.
405 RenderFrameHostImpl* GetMainFrame() const;
406
407 // Returns the focused frame.
408 FrameTreeNode* GetFocusedFrame();
409
Sharon Yangefe52632022-03-08 23:06:06410 // Sets the focused frame to |node|. |source| identifies the
411 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
412 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
413 // notified about the new focused frame. Note that |source| may differ from
414 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
415 // window.focus() calls).
416 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44417
danakjc492bf82020-09-09 20:02:44418 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
419 //
420 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
421 // of this object.
Sharon Yang7ce309e2023-01-19 21:39:57422 // `create_case` indicates whether or not the RenderViewHost being created is
423 // speculative or not. It should only be registered with the FrameTree if it
424 // is not speculative.
danakjc492bf82020-09-09 20:02:44425 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
Charlie Reis37be2682023-01-10 17:04:47426 SiteInstanceImpl* site_instance,
danakjc492bf82020-09-09 20:02:44427 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58428 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57429 scoped_refptr<BrowsingContextState> main_browsing_context_state,
430 CreateRenderViewHostCase create_case);
danakjc492bf82020-09-09 20:02:44431
432 // Returns the existing RenderViewHost for a new RenderFrameHost.
433 // There should always be such a RenderViewHost, because the main frame
434 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang7ce309e2023-01-19 21:39:57435 // Note that this will never return `speculative_render_view_host_`. If that
436 // is needed, call `speculative_render_view_host()` instead.
Sharon Yang57bde122022-03-01 20:01:12437 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44438
Sharon Yanged884542023-02-02 17:33:44439 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
440
Sharon Yangc581a0c2021-11-02 18:09:39441 // Returns the ID used for the RenderViewHost associated with
442 // |site_instance_group|.
443 RenderViewHostMapId GetRenderViewHostMapId(
444 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09445
danakjc492bf82020-09-09 20:02:44446 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09447 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44448 //
449 // This method does not take ownership of|rvh|.
450 //
451 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
452 // |rvh|'s SiteInstance.
453 //
454 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
455 // *must* be called for |rvh| when it is destroyed or put into the
456 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
457 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09458 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44459
460 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09461 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
462 // called for a |render_view_host| that is not currently set for reuse.
463 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13464 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44465
466 // This is called when the frame is about to be removed and started to run
467 // unload handlers.
468 void FrameUnloading(FrameTreeNode* frame);
469
470 // This is only meant to be called by FrameTreeNode. Triggers calling
471 // the listener installed by SetFrameRemoveListener.
472 void FrameRemoved(FrameTreeNode* frame);
473
Nate Chapin470dbc62023-04-25 16:34:38474 void NodeLoadingStateChanged(FrameTreeNode& node,
475 LoadingState previous_frame_tree_loading_state);
Carlos Caballero03262522021-02-05 14:49:58476 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44477
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15478 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
479 // is navigating returns `blink::kInitialLoadProgress`.
480 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44481
Sreeja Kamishetty15f9944a22022-03-10 10:16:08482 // Returns true if at least one of the nodes in this frame tree or nodes in
483 // any inner frame tree of the same WebContents is loading.
Yu Gaoc8c18552022-06-22 14:38:45484 bool IsLoadingIncludingInnerFrameTrees() const;
danakjc492bf82020-09-09 20:02:44485
Nate Chapin470dbc62023-04-25 16:34:38486 // Returns the LoadingState for the FrameTree as a whole, indicating whether
487 // a load is in progress, as well as whether loading UI should be shown.
488 LoadingState GetLoadingState() const;
489
danakjc492bf82020-09-09 20:02:44490 // Set page-level focus in all SiteInstances involved in rendering
491 // this FrameTree, not including the current main frame's
492 // SiteInstance. The focus update will be sent via the main frame's proxies
493 // in those SiteInstances.
494 void ReplicatePageFocus(bool is_focused);
495
496 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06497 // identified by |group|.
498 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44499
W. James MacLeanc07dc41b2022-07-25 18:52:16500 // Walks the current frame tree and registers any origins matching
501 // `previously_visited_origin`, either the last committed origin of a
502 // RenderFrameHost or the origin associated with a NavigationRequest that has
503 // been assigned to a SiteInstance, as having the default origin isolation
504 // state. This is only necessary when `previously_visited_origin` is seen with
505 // an OriginAgentCluster header explicitly requesting something other than the
506 // default.
507 void RegisterExistingOriginAsHavingDefaultIsolation(
danakjc492bf82020-09-09 20:02:44508 const url::Origin& previously_visited_origin,
509 NavigationRequest* navigation_request_to_exclude);
510
Carlos Caballero40b0efd2021-01-26 11:55:00511 NavigationControllerImpl& controller() { return navigator_.controller(); }
danakjc492bf82020-09-09 20:02:44512 Navigator& navigator() { return navigator_; }
513
Carlos Caballeroede6f8c2021-01-28 11:01:50514 // Another page accessed the initial empty main document, which means it
515 // is no longer safe to display a pending URL without risking a URL spoof.
516 void DidAccessInitialMainDocument();
517
518 bool has_accessed_initial_main_document() const {
519 return has_accessed_initial_main_document_;
520 }
521
522 void ResetHasAccessedInitialMainDocument() {
523 has_accessed_initial_main_document_ = false;
524 }
525
Carlos Caballero6ff6ace2021-02-05 16:53:00526 bool IsHidden() const { return delegate_->IsHidden(); }
527
Sreeja Kamishettyd64b993d2022-02-14 12:04:42528 // LoadingTree returns the following for different frame trees to direct
529 // loading related events. Please see FrameTree::Delegate::LoadingTree for
530 // more comments.
531 // - For prerender frame tree -> returns the frame tree itself.
532 // - For fenced frame and primary frame tree (including portal) -> returns
533 // the delegate's primary frame tree.
534 FrameTree* LoadingTree();
535
Carlos Caballero03262522021-02-05 14:49:58536 // Stops all ongoing navigations in each of the nodes of this FrameTree.
537 void StopLoading();
538
Carlos Caballero101ac26b2021-03-24 11:54:05539 // Prepares this frame tree for destruction, cleaning up the internal state
540 // and firing the appropriate events like FrameDeleted.
541 // Must be called before FrameTree is destroyed.
542 void Shutdown();
543
Takashi Toyoshimaea534ef22021-07-21 03:27:59544 bool IsBeingDestroyed() const { return is_being_destroyed_; }
545
Dave Tapuskad8b0530f2021-10-19 15:12:31546 base::SafeRef<FrameTree> GetSafeRef();
547
Dave Tapuska54c76a032021-10-27 22:10:42548 // Walks up the FrameTree chain and focuses the FrameTreeNode where
549 // each inner FrameTree is attached.
550 void FocusOuterFrameTrees();
551
Ari Chivukula5d15efb2023-01-21 04:33:52552 // This should only be called by NavigationRequest when it detects that an
553 // origin is participating in the deprecation trial.
554 //
555 // TODO(crbug.com/1407150): Remove this when deprecation trial is complete.
556 void RegisterOriginForUnpartitionedSessionStorageAccess(
557 const url::Origin& origin);
558
Ari Chivukula9a96a582023-02-03 21:02:29559 // This should only be called by NavigationRequest when it detects that an
560 // origin is not participating in the deprecation trial.
561 //
562 // TODO(crbug.com/1407150): Remove this when deprecation trial is complete.
563 void UnregisterOriginForUnpartitionedSessionStorageAccess(
564 const url::Origin& origin);
565
Ari Chivukula5d15efb2023-01-21 04:33:52566 // This should be used for all session storage related bindings as it adjusts
567 // the storage key used depending on the deprecation trial.
568 //
569 // TODO(crbug.com/1407150): Remove this when deprecation trial is complete.
570 const blink::StorageKey GetSessionStorageKey(
571 const blink::StorageKey& storage_key);
572
danakjc492bf82020-09-09 20:02:44573 private:
574 friend class FrameTreeTest;
575 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Sreeja Kamishettyd64b993d2022-02-14 12:04:42576 FRIEND_TEST_ALL_PREFIXES(PortalBrowserTest, NodesForIsLoading);
Xiaochen Zhou6443f752022-08-10 16:40:46577 FRIEND_TEST_ALL_PREFIXES(FencedFrameMPArchBrowserTest, NodesForIsLoading);
Sharon Yang2f3304a2022-05-13 02:24:25578 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
579 CreateRenderViewAfterProcessKillAndClosedProxy);
danakjc492bf82020-09-09 20:02:44580
581 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
582 // breadth-first traversal order, skipping the subtree rooted at
583 // |node|, but including |node| itself.
584 NodeRange NodesExceptSubtree(FrameTreeNode* node);
585
Sreeja Kamishettyd64b993d2022-02-14 12:04:42586 // Returns all FrameTreeNodes in this frame tree, as well as any
587 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
588 // frame trees of inner delegates. This is used to find the aggregate
589 // IsLoading value for a frame tree.
590 //
591 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
592 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
593 // portals and guest views are migrated to MPArch.
594 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
595
Keishi Hattori0e45c022021-11-27 09:25:52596 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58597
danakjc492bf82020-09-09 20:02:44598 // These delegates are installed into all the RenderViewHosts and
599 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52600 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
601 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
602 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
603 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
604 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44605
606 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00607 // tree. Each FrameTreeNode will default to using it for navigation tasks in
608 // the frame.
danakjc492bf82020-09-09 20:02:44609 Navigator navigator_;
610
Sharon Yanged884542023-02-02 17:33:44611 // A map to store RenderViewHosts, keyed by SiteInstanceGroup ID.
612 // This map does not cover all RenderViewHosts in a FrameTree. See
613 // `speculative_render_view_host_`.
614 using RenderViewHostMap = std::unordered_map<RenderViewHostMapId,
615 RenderViewHostImpl*,
616 RenderViewHostMapId::Hasher>;
Aaron Colwell78b4bde2021-03-16 16:16:09617 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44618 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
619 // Each RenderViewHost maintains a refcount and is deleted when there are no
620 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09621 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44622
Sharon Yang7ce309e2023-01-19 21:39:57623 // A speculative RenderViewHost is created for all speculative cross-page
624 // same-SiteInstanceGroup RenderFrameHosts. When the corresponding
625 // RenderFrameHost gets committed and becomes the current RenderFrameHost,
626 // `speculative_render_view_host_` will be moved to `render_view_host_map_`,
627 // overwriting the previous RenderViewHost of the same SiteInstanceGroup, if
628 // applicable. This field will also be reset at that time, or if the
629 // speculative RenderFrameHost gets deleted.
630 //
631 // For any given FrameTree, there will be at most one
632 // `speculative_render_view_host_`, because only main-frame speculative
633 // RenderFrameHosts have speculative RenderViewHosts, and there is at most one
634 // such RenderFrameHost per FrameTree at a time.
635 // This is a WeakPtr, since the RenderViewHost is owned by the
636 // RenderFrameHostImpl, not the FrameTree. This implies that if the owning
637 // RenderFrameHostImpl gets deleted, this will too.
638 //
639 // This supports but is independent of RenderDocument, which introduces cases
640 // where there may be more than one RenderViewHost per SiteInstanceGroup, such
641 // as cross-page same-SiteInstanceGroup navigations. The speculative
642 // RenderFrameHost has an associated RenderViewHost, but it cannot be put in
643 // `render_view_host_map_` when it is created, as the existing RenderViewHost
644 // will be incorrectly overwritten.
645 // TODO(yangsharon, crbug.com/1336305): Expand support to include
646 // cross-SiteInstanceGroup main-frame navigations, so all main-frame
647 // navigations use speculative RenderViewHost.
648 base::WeakPtr<RenderViewHostImpl> speculative_render_view_host_;
649
Harkiran Bolaria16f2c48d2022-04-22 12:39:57650 // Indicates type of frame tree.
651 const Type type_;
652
danakjc492bf82020-09-09 20:02:44653 int focused_frame_tree_node_id_;
654
danakjc492bf82020-09-09 20:02:44655 // Overall load progress.
656 double load_progress_;
657
Carlos Caballeroede6f8c2021-01-28 11:01:50658 // Whether the initial empty page has been accessed by another page, making it
659 // unsafe to show the pending URL. Usually false unless another window tries
660 // to modify the blank page. Always false after the first commit.
661 bool has_accessed_initial_main_document_ = false;
662
Takashi Toyoshimaea534ef22021-07-21 03:27:59663 bool is_being_destroyed_ = false;
664
Carlos Caballero101ac26b2021-03-24 11:54:05665#if DCHECK_IS_ON()
666 // Whether Shutdown() was called.
667 bool was_shut_down_ = false;
668#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31669
Paul Semel3e241042022-10-11 12:57:31670 // The root FrameTreeNode.
671 //
672 // Note: It is common for a node to test whether it's the root node, via the
673 // `root()` method, even while `root_` is running its destructor.
674 // For that reason, we want to destroy |root_| before any other fields.
675 FrameTreeNode root_;
676
Ari Chivukula5d15efb2023-01-21 04:33:52677 // Origins in this set have enabled a deprecation trial that prevents the
678 // partitioning of session storage when embedded as a third-party iframe.
679 // This list persists for the lifetime of the associated tab.
680 //
681 // TODO(crbug.com/1407150): Remove this when deprecation trial is complete.
682 std::set<url::Origin> unpartitioned_session_storage_origins_;
683
Dave Tapuskad8b0530f2021-10-19 15:12:31684 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44685};
686
687} // namespace content
688
689#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_