blob: 23921f3d58ac0dc84e93ded7b82880957461abe2 [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;
43} // namespace blink
44
45namespace content {
46
Carlos Caballero40b0efd2021-01-26 11:55:0047class BrowserContext;
Jeremy Roman2d8dfe132021-07-06 20:51:2648class PageDelegate;
danakjc492bf82020-09-09 20:02:4449class RenderFrameHostDelegate;
Dave Tapuska06a3361d2025-01-13 22:09:4350class RenderFrameProxyHost;
danakjc492bf82020-09-09 20:02:4451class 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;
danakjc492bf82020-09-09 20:02:4490
91 FrameTreeNode* operator*() { return current_node_; }
92
93 private:
Jayson Adams4db0bfe22021-07-15 19:24:0794 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4495 friend class NodeRange;
96
Ali Hijazie63cbaf62023-12-20 19:29:3597 NodeIterator(const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
98 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:2399 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
Ali Hijazie63cbaf62023-12-20 19:29:35127 NodeRange(const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>&
128 starting_nodes,
Kevin McNee5f594382021-05-06 23:18:23129 const FrameTreeNode* root_of_subtree_to_skip,
Dave Tapuskadda303d2022-10-04 16:56:48130 bool should_descend_into_inner_trees,
131 bool include_delegate_nodes_for_inner_frame_trees);
danakjc492bf82020-09-09 20:02:44132
Ali Hijazie63cbaf62023-12-20 19:29:35133 const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>
134 starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52135 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23136 const bool should_descend_into_inner_trees_;
Dave Tapuskadda303d2022-10-04 16:56:48137 const bool include_delegate_nodes_for_inner_frame_trees_;
danakjc492bf82020-09-09 20:02:44138 };
139
Carlos Caballero03262522021-02-05 14:49:58140 class CONTENT_EXPORT Delegate {
141 public:
Nate Chapin470dbc62023-04-25 16:34:38142 // The FrameTree changed its LoadingState. This can be a transition between
143 // not-loading and loading (in which case it will be accompanied by either a
144 // DidStartLoading or DidStopLoading), or a transition between not showing
145 // loading UI and showing loading UI while a navigation is in progress (in
146 // which case it will be called without either DidStartLoading or
147 // DidStopLoading).
148 virtual void LoadingStateChanged(LoadingState new_state) = 0;
Carlos Caballero03262522021-02-05 14:49:58149
Nate Chapin470dbc62023-04-25 16:34:38150 // The FrameTree has started loading in `frame_tree_node`. Note that this
151 // is only called when the FrameTree as a whole goes from not-loading to
152 // loading. If a second FrameTreeNode begins loading, a new DidStartLoading
153 // message will not be sent.
154 virtual void DidStartLoading(FrameTreeNode* frame_tree_node) = 0;
155
156 // The FrameTree has stopped loading. Sent only when all FrameTreeNodes have
157 // stopped loading.
Carlos Caballero03262522021-02-05 14:49:58158 virtual void DidStopLoading() = 0;
159
Sreeja Kamishettyd64b993d2022-02-14 12:04:42160 // Returns the delegate's top loading tree, which should be used to infer
Yu Gaoc8c18552022-06-22 14:38:45161 // the values of loading-related states. The state of
162 // IsLoadingIncludingInnerFrameTrees() is a WebContents level concept and
163 // LoadingTree would return the frame tree to which loading events should be
164 // directed.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42165 //
Alison Gale81f4f2c72024-04-22 19:33:31166 // TODO(crbug.com/40202416): Remove this method and directly rely on
Jeremy Romanc0c69be2023-11-21 19:14:52167 // GetOutermostMainFrame() once guest views are migrated to MPArch.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42168 virtual FrameTree* LoadingTree() = 0;
169
Carlos Caballero6ff6ace2021-02-05 16:53:00170 // Returns true when the active RenderWidgetHostView should be hidden.
171 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25172
Dominic Farolinoedf44ee2021-07-20 23:50:59173 // If the FrameTree using this delegate is an inner/nested FrameTree, then
174 // there may be a FrameTreeNode in the outer FrameTree that is considered
175 // our outer delegate FrameTreeNode. This method returns the outer delegate
176 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
Avi Drissmanbd153642024-09-03 18:58:05177 // FrameTreeNode, this method returns an invalid value.
178 virtual FrameTreeNodeId 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
Julie Jeongeun Kim2132b37f82022-11-23 08:30:46191 // Set the `node` frame as focused in its own FrameTree as well as possibly
192 // changing the focused frame tree in the case of inner/outer FrameTrees.
193 virtual void SetFocusedFrame(FrameTreeNode* node,
194 SiteInstanceGroup* source) = 0;
Tommy Steimel71f154462024-05-22 19:05:07195
196 // Returns this FrameTree's picture-in-picture FrameTree if it has one.
197 virtual FrameTree* GetOwnedPictureInPictureFrameTree() = 0;
198
199 // Returns this FrameTree's opener if this FrameTree represents a
200 // picture-in-picture window.
201 virtual FrameTree* GetPictureInPictureOpenerFrameTree() = 0;
Dave Tapuska06a3361d2025-01-13 22:09:43202
203 // Called when the visibility of the RenderFrameProxyHost changes.
204 // This method should only handle visibility for inner WebContents and
205 // will eventually notify all the RenderWidgetHostViews belonging to that
206 // WebContents. If this is not an inner WebContents or the inner WebContents
207 // FrameTree root does not match `render_frame_proxy_host` FrameTreeNode it
208 // should return false.
209 virtual bool OnRenderFrameProxyVisibilityChanged(
210 RenderFrameProxyHost* render_frame_proxy_host,
211 blink::mojom::FrameVisibility visibility) = 0;
Carlos Caballero03262522021-02-05 14:49:58212 };
213
Sreeja Kamishetty74bacd522021-03-22 17:04:24214 // Type of FrameTree instance.
215 enum class Type {
216 // This FrameTree is the primary frame tree for the WebContents, whose main
217 // document URL is shown in the Omnibox.
218 kPrimary,
219
220 // This FrameTree is used to prerender a page in the background which is
221 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36222 kPrerender,
223
224 // This FrameTree is used to host the contents of a <fencedframe> element.
Dominic Farolino4bc10ee2021-08-31 00:37:36225 //
Kevin McNeef1b0f0b2024-09-17 21:49:41226 // Note that the FrameTree's Type should not be confused for
227 // `RenderFrameHost::LifecycleState`. For example, when a <fencedframe> is
228 // nested in a page in the bfcache, the FrameTree associated with the fenced
229 // frame will be kFencedFrame, but the RenderFrameHosts inside of it will
230 // have their lifecycle state indicate that they are bfcached.
231 kFencedFrame,
Kevin McNee2585e732024-10-28 22:11:19232
233 // This FrameTree is used to host the contents of a guest page. Guests are
234 // kinds of embedded pages, but their semantics are mostly delegated outside
235 // of the content/ layer. See components/guest_view/README.md.
236 // The implementation of guests is being migrated from using a separate
237 // WebContents to using this FrameTree Type. This type is used with the
238 // `features::kGuestViewMPArch` flag.
239 kGuest,
Sreeja Kamishetty74bacd522021-03-22 17:04:24240 };
Sreeja Kamishetty837a10402021-04-23 12:41:59241
242 // A set of delegates are remembered here so that we can create
243 // RenderFrameHostManagers.
244 FrameTree(BrowserContext* browser_context,
245 Delegate* delegate,
246 NavigationControllerDelegate* navigation_controller_delegate,
247 NavigatorDelegate* navigator_delegate,
248 RenderFrameHostDelegate* render_frame_delegate,
249 RenderViewHostDelegate* render_view_delegate,
250 RenderWidgetHostDelegate* render_widget_delegate,
251 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26252 PageDelegate* page_delegate,
Danil Somsikov259aa65f2022-11-11 20:49:44253 Type type);
Peter Boström828b9022021-09-21 02:28:43254
255 FrameTree(const FrameTree&) = delete;
256 FrameTree& operator=(const FrameTree&) = delete;
257
Sreeja Kamishetty837a10402021-04-23 12:41:59258 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24259
Carlos Caballero40b0efd2021-01-26 11:55:00260 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17261 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni46087a12022-11-11 08:28:38262 // creates an initial NavigationEntry that potentially inherits
263 // `opener_for_origin`'s origin in its NavigationController. This method will
264 // call back into the delegates so it should only be called once they have
265 // completed their initialization. Pass in frame_policy so that it can be set
266 // in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00267 // TODO(carlscab): It would be great if initialization could happened in the
268 // constructor so we do not leave objects in a half initialized state.
Charlie Reis37be2682023-01-10 17:04:47269 void Init(SiteInstanceImpl* main_frame_site_instance,
Carlos Caballero40b0efd2021-01-26 11:55:00270 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17271 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46272 RenderFrameHostImpl* opener_for_origin,
Danil Somsikov259aa65f2022-11-11 20:49:44273 const blink::FramePolicy& frame_policy,
274 const base::UnguessableToken& devtools_frame_token);
Sreeja Kamishetty837a10402021-04-23 12:41:59275
276 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00277
Paul Semel3e241042022-10-11 12:57:31278 FrameTreeNode* root() { return &root_; }
279 const FrameTreeNode* root() const { return &root_; }
danakjc492bf82020-09-09 20:02:44280
Takashi Toyoshima7c041d82023-09-26 16:09:21281 bool is_primary() const { return type_ == Type::kPrimary; }
282 bool is_prerendering() const { return type_ == Type::kPrerender; }
283 bool is_fenced_frame() const { return type_ == Type::kFencedFrame; }
Kevin McNee2585e732024-10-28 22:11:19284 bool is_guest() const { return type_ == Type::kGuest; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46285
Carlos Caballero03262522021-02-05 14:49:58286 Delegate* delegate() { return delegate_; }
287
Jeremy Roman2d8dfe132021-07-06 20:51:26288 // Delegates for various objects. These can be kept centrally on the
289 // FrameTree because they are expected to be the same for all frames on a
290 // given FrameTree.
danakjc492bf82020-09-09 20:02:44291 RenderFrameHostDelegate* render_frame_delegate() {
292 return render_frame_delegate_;
293 }
294 RenderViewHostDelegate* render_view_delegate() {
295 return render_view_delegate_;
296 }
297 RenderWidgetHostDelegate* render_widget_delegate() {
298 return render_widget_delegate_;
299 }
300 RenderFrameHostManager::Delegate* manager_delegate() {
301 return manager_delegate_;
302 }
Jeremy Roman2d8dfe132021-07-06 20:51:26303 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09304
Sharon Yanged884542023-02-02 17:33:44305 // Iterate over all RenderViewHosts, including speculative RenderViewHosts.
306 // See `speculative_render_view_host_` for more details.
307 void ForEachRenderViewHost(
308 base::FunctionRef<void(RenderViewHostImpl*)> on_host);
danakjc492bf82020-09-09 20:02:44309
Sharon Yang7ce309e2023-01-19 21:39:57310 // Speculative RenderViewHost accessors.
311 RenderViewHostImpl* speculative_render_view_host() const {
312 return speculative_render_view_host_.get();
313 }
314 void set_speculative_render_view_host(
315 base::WeakPtr<RenderViewHostImpl> render_view_host) {
316 speculative_render_view_host_ = render_view_host;
317 }
318
319 // Moves `speculative_render_view_host_` to `render_view_host_map_`. This
320 // should be called every time a main-frame same-SiteInstanceGroup speculative
321 // RenderFrameHost gets swapped in and becomes the active RenderFrameHost.
322 // This overwrites the previous RenderViewHost for the SiteInstanceGroup in
323 // `render_view_host_map_`, if one exists.
324 void MakeSpeculativeRVHCurrent();
325
danakjc492bf82020-09-09 20:02:44326 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
327 // of this FrameTree.
Avi Drissmanbd153642024-09-03 18:58:05328 FrameTreeNode* FindByID(FrameTreeNodeId frame_tree_node_id);
danakjc492bf82020-09-09 20:02:44329
330 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
331 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
332
333 // Returns the first frame in this tree with the given |name|, or the main
334 // frame if |name| is empty.
335 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
336 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
337 FrameTreeNode* FindByName(const std::string& name);
338
339 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
340 // breadth-first traversal order.
341 NodeRange Nodes();
342
343 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
344 // frame tree, starting from |subtree_root|.
345 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
346
Kevin McNee53f0b2d2021-11-02 18:00:45347 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
348 // well as any FrameTreeNodes of inner frame trees. Note that this includes
349 // inner frame trees of inner WebContents as well.
350 NodeRange NodesIncludingInnerTreeNodes();
351
Kevin McNee5f594382021-05-06 23:18:23352 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
353 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45354 // frame trees. Note that this includes inner frame trees of inner WebContents
Dave Tapuskadda303d2022-10-04 16:56:48355 // as well. If `include_delegate_nodes_for_inner_frame_trees` is true the
356 // delegate RenderFrameHost owning the inner frame tree will also be returned.
357 static NodeRange SubtreeAndInnerTreeNodes(
358 RenderFrameHostImpl* parent,
359 bool include_delegate_nodes_for_inner_frame_trees = false);
Kevin McNee5f594382021-05-06 23:18:23360
danakjc492bf82020-09-09 20:02:44361 // Adds a new child frame to the frame tree. |process_id| is required to
362 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41363 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44364 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
365 // interface through which the child RenderFrame can access Mojo services
366 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30367 // sending the client end of the interface down to the
368 // RenderFrame. |policy_container_bind_params|, if not null, is used for
369 // binding Blink's policy container to the new RenderFrameHost's
370 // PolicyContainerHost. This is only needed if this frame is the result of the
371 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41372 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
373 // the added frame is only to serve as a placeholder for an inner frame tree
Jeremy Romanc0c69be2023-11-21 19:14:52374 // (e.g. fenced frames) and will not have a live RenderFrame of its
Kevin McNee43fe8292021-10-04 22:59:41375 // own.
danakjc492bf82020-09-09 20:02:44376 FrameTreeNode* AddFrame(
377 RenderFrameHostImpl* parent,
378 int process_id,
379 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18380 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44381 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
382 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30383 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49384 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
385 associated_interface_provider_receiver,
danakjc492bf82020-09-09 20:02:44386 blink::mojom::TreeScopeType scope,
387 const std::string& frame_name,
388 const std::string& frame_unique_name,
389 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04390 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44391 const base::UnguessableToken& devtools_frame_token,
Daniel Cheng284c38942022-09-22 23:30:34392 const blink::DocumentToken& document_token,
danakjc492bf82020-09-09 20:02:44393 const blink::FramePolicy& frame_policy,
394 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
395 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41396 blink::FrameOwnerElementType owner_type,
397 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44398
399 // Removes a frame from the frame tree. |child|, its children, and objects
400 // owned by their RenderFrameHostManagers are immediately deleted. The root
401 // node cannot be removed this way.
402 void RemoveFrame(FrameTreeNode* child);
403
Sharon Yangdcc5f252024-05-09 18:27:23404 // This method walks the entire frame tree and creates RenderFrameProxyHosts
405 // as needed. Proxies are not created if suitable proxies already exist. See
406 // below for special handling of |source|. Otherwise proxies are created for
407 // the given |site_instance_group| in each node.
408 //
409 // |source| may be null if there is no node navigating in this frame tree
410 // (such as when this is called for an opener's frame tree), in which case no
411 // nodes are skipped for RenderFrameProxyHost creation. Otherwise, a proxy is
412 // temporarily created for |source| in cross-SiteInstanceGroup cases (to allow
413 // a remote-to-local swap to the new RenderFrameHost in |source|), but the
414 // subtree rooted at source is skipped.
Alex Moshchukd0d759c22025-05-09 18:18:35415 //
Sharon Yangdcc5f252024-05-09 18:27:23416 // |source_new_browsing_context_state| is the BrowsingContextState used by the
417 // speculative frame host, which may differ from the BrowsingContextState in
418 // |source| during cross-origin cross- browsing-instance navigations.
Alex Moshchukd0d759c22025-05-09 18:18:35419 //
420 // |navigation_metrics_token| is a token identifying the navigation for which
421 // these proxies are being created, if any. It allows metrics code and trace
422 // events to tie together different IPCs and events pertaining to a particular
423 // navigation. It's nullopt for non-navigation cases such as creating proxies
424 // for a new subframe.
Sharon Yangdcc5f252024-05-09 18:27:23425 void CreateProxiesForSiteInstanceGroup(
426 FrameTreeNode* source,
427 SiteInstanceGroup* site_instance_group,
428 const scoped_refptr<BrowsingContextState>&
Alex Moshchukd0d759c22025-05-09 18:18:35429 source_new_browsing_context_state,
430 const std::optional<base::UnguessableToken>& navigation_metrics_token);
danakjc492bf82020-09-09 20:02:44431
432 // Convenience accessor for the main frame's RenderFrameHostImpl.
433 RenderFrameHostImpl* GetMainFrame() const;
434
435 // Returns the focused frame.
436 FrameTreeNode* GetFocusedFrame();
437
Sharon Yangefe52632022-03-08 23:06:06438 // Sets the focused frame to |node|. |source| identifies the
439 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
440 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
441 // notified about the new focused frame. Note that |source| may differ from
442 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
443 // window.focus() calls).
444 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44445
danakjc492bf82020-09-09 20:02:44446 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
447 //
448 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
449 // of this object.
Sharon Yang7ce309e2023-01-19 21:39:57450 // `create_case` indicates whether or not the RenderViewHost being created is
451 // speculative or not. It should only be registered with the FrameTree if it
452 // is not speculative.
Khushal Sagar402d1c9f2023-11-15 22:21:48453 // `frame_sink_id` is optionally set only if we're creating a speculative
454 // RenderViewHost. If set, it implies we're reusing the compositor from the
455 // previous RenderViewHost.
danakjc492bf82020-09-09 20:02:44456 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
Sharon Yangeb76ee22023-11-29 00:42:09457 SiteInstanceGroup* site_instance_group,
danakjc492bf82020-09-09 20:02:44458 int32_t main_frame_routing_id,
Harkiran Bolaria57e2b062022-03-14 10:27:58459 bool renderer_initiated_creation,
Sharon Yang7ce309e2023-01-19 21:39:57460 scoped_refptr<BrowsingContextState> main_browsing_context_state,
Khushal Sagar402d1c9f2023-11-15 22:21:48461 CreateRenderViewHostCase create_case,
Arthur Sonzognic686e8f2024-01-11 08:36:37462 std::optional<viz::FrameSinkId> frame_sink_id);
danakjc492bf82020-09-09 20:02:44463
464 // Returns the existing RenderViewHost for a new RenderFrameHost.
465 // There should always be such a RenderViewHost, because the main frame
466 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang7ce309e2023-01-19 21:39:57467 // Note that this will never return `speculative_render_view_host_`. If that
468 // is needed, call `speculative_render_view_host()` instead.
Sharon Yang57bde122022-03-01 20:01:12469 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44470
Sharon Yanged884542023-02-02 17:33:44471 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
472
Sharon Yangc581a0c2021-11-02 18:09:39473 // Returns the ID used for the RenderViewHost associated with
474 // |site_instance_group|.
475 RenderViewHostMapId GetRenderViewHostMapId(
476 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09477
danakjc492bf82020-09-09 20:02:44478 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09479 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44480 //
481 // This method does not take ownership of|rvh|.
482 //
483 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
484 // |rvh|'s SiteInstance.
485 //
486 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
487 // *must* be called for |rvh| when it is destroyed or put into the
488 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
489 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09490 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44491
492 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09493 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
494 // called for a |render_view_host| that is not currently set for reuse.
495 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13496 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44497
498 // This is called when the frame is about to be removed and started to run
499 // unload handlers.
500 void FrameUnloading(FrameTreeNode* frame);
501
502 // This is only meant to be called by FrameTreeNode. Triggers calling
503 // the listener installed by SetFrameRemoveListener.
504 void FrameRemoved(FrameTreeNode* frame);
505
Nate Chapin470dbc62023-04-25 16:34:38506 void NodeLoadingStateChanged(FrameTreeNode& node,
507 LoadingState previous_frame_tree_loading_state);
Carlos Caballero03262522021-02-05 14:49:58508 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44509
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15510 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
511 // is navigating returns `blink::kInitialLoadProgress`.
512 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44513
Sreeja Kamishetty15f9944a22022-03-10 10:16:08514 // Returns true if at least one of the nodes in this frame tree or nodes in
515 // any inner frame tree of the same WebContents is loading.
Yu Gaoc8c18552022-06-22 14:38:45516 bool IsLoadingIncludingInnerFrameTrees() const;
danakjc492bf82020-09-09 20:02:44517
Nate Chapin470dbc62023-04-25 16:34:38518 // Returns the LoadingState for the FrameTree as a whole, indicating whether
519 // a load is in progress, as well as whether loading UI should be shown.
520 LoadingState GetLoadingState() const;
521
danakjc492bf82020-09-09 20:02:44522 // Set page-level focus in all SiteInstances involved in rendering
523 // this FrameTree, not including the current main frame's
524 // SiteInstance. The focus update will be sent via the main frame's proxies
525 // in those SiteInstances.
526 void ReplicatePageFocus(bool is_focused);
527
528 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06529 // identified by |group|.
530 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44531
W. James MacLeanc07dc41b2022-07-25 18:52:16532 // Walks the current frame tree and registers any origins matching
533 // `previously_visited_origin`, either the last committed origin of a
534 // RenderFrameHost or the origin associated with a NavigationRequest that has
535 // been assigned to a SiteInstance, as having the default origin isolation
536 // state. This is only necessary when `previously_visited_origin` is seen with
537 // an OriginAgentCluster header explicitly requesting something other than the
538 // default.
539 void RegisterExistingOriginAsHavingDefaultIsolation(
danakjc492bf82020-09-09 20:02:44540 const url::Origin& previously_visited_origin,
541 NavigationRequest* navigation_request_to_exclude);
542
Elad Alon32044f532025-03-04 22:16:03543 const NavigationControllerImpl& controller() const {
544 return navigator_.controller();
545 }
Carlos Caballero40b0efd2021-01-26 11:55:00546 NavigationControllerImpl& controller() { return navigator_.controller(); }
Elad Alon32044f532025-03-04 22:16:03547
danakjc492bf82020-09-09 20:02:44548 Navigator& navigator() { return navigator_; }
549
Carlos Caballeroede6f8c2021-01-28 11:01:50550 // Another page accessed the initial empty main document, which means it
551 // is no longer safe to display a pending URL without risking a URL spoof.
552 void DidAccessInitialMainDocument();
553
554 bool has_accessed_initial_main_document() const {
555 return has_accessed_initial_main_document_;
556 }
557
558 void ResetHasAccessedInitialMainDocument() {
559 has_accessed_initial_main_document_ = false;
560 }
561
Carlos Caballero6ff6ace2021-02-05 16:53:00562 bool IsHidden() const { return delegate_->IsHidden(); }
563
Sreeja Kamishettyd64b993d2022-02-14 12:04:42564 // LoadingTree returns the following for different frame trees to direct
565 // loading related events. Please see FrameTree::Delegate::LoadingTree for
566 // more comments.
567 // - For prerender frame tree -> returns the frame tree itself.
Jeremy Romanc0c69be2023-11-21 19:14:52568 // - For fenced frame and primary frame tree -> returns
Sreeja Kamishettyd64b993d2022-02-14 12:04:42569 // the delegate's primary frame tree.
570 FrameTree* LoadingTree();
571
Carlos Caballero03262522021-02-05 14:49:58572 // Stops all ongoing navigations in each of the nodes of this FrameTree.
573 void StopLoading();
574
Carlos Caballero101ac26b2021-03-24 11:54:05575 // Prepares this frame tree for destruction, cleaning up the internal state
576 // and firing the appropriate events like FrameDeleted.
577 // Must be called before FrameTree is destroyed.
578 void Shutdown();
579
Takashi Toyoshimaea534ef22021-07-21 03:27:59580 bool IsBeingDestroyed() const { return is_being_destroyed_; }
581
Dave Tapuskad8b0530f2021-10-19 15:12:31582 base::SafeRef<FrameTree> GetSafeRef();
583
Dave Tapuska54c76a032021-10-27 22:10:42584 // Walks up the FrameTree chain and focuses the FrameTreeNode where
585 // each inner FrameTree is attached.
586 void FocusOuterFrameTrees();
587
Thomas Lukaszewicz8ada31b2024-09-01 19:18:16588 // Discards the frame tree. The root frame is transitioned to an empty
589 // document in blink and BFCache entries are cleared. The tree is configured
590 // to reload when activated.
591 void Discard();
592
danakjc492bf82020-09-09 20:02:44593 private:
594 friend class FrameTreeTest;
595 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Xiaochen Zhou6443f752022-08-10 16:40:46596 FRIEND_TEST_ALL_PREFIXES(FencedFrameMPArchBrowserTest, NodesForIsLoading);
Sharon Yang2f3304a2022-05-13 02:24:25597 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
598 CreateRenderViewAfterProcessKillAndClosedProxy);
danakjc492bf82020-09-09 20:02:44599
600 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
601 // breadth-first traversal order, skipping the subtree rooted at
602 // |node|, but including |node| itself.
603 NodeRange NodesExceptSubtree(FrameTreeNode* node);
604
Sreeja Kamishettyd64b993d2022-02-14 12:04:42605 // Returns all FrameTreeNodes in this frame tree, as well as any
606 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
607 // frame trees of inner delegates. This is used to find the aggregate
608 // IsLoading value for a frame tree.
609 //
610 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
611 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
Jeremy Romanc0c69be2023-11-21 19:14:52612 // guest views are migrated to MPArch.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42613 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
614
Keishi Hattori0e45c022021-11-27 09:25:52615 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58616
danakjc492bf82020-09-09 20:02:44617 // These delegates are installed into all the RenderViewHosts and
618 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52619 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
620 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
621 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
622 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
623 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44624
625 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00626 // tree. Each FrameTreeNode will default to using it for navigation tasks in
627 // the frame.
danakjc492bf82020-09-09 20:02:44628 Navigator navigator_;
629
Sharon Yanged884542023-02-02 17:33:44630 // A map to store RenderViewHosts, keyed by SiteInstanceGroup ID.
631 // This map does not cover all RenderViewHosts in a FrameTree. See
632 // `speculative_render_view_host_`.
Devon Loehr92631c892025-01-02 20:15:15633 using RenderViewHostMap =
634 std::unordered_map<RenderViewHostMapId, RenderViewHostImpl*>;
Aaron Colwell78b4bde2021-03-16 16:16:09635 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44636 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
637 // Each RenderViewHost maintains a refcount and is deleted when there are no
638 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09639 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44640
Sharon Yang7ce309e2023-01-19 21:39:57641 // A speculative RenderViewHost is created for all speculative cross-page
642 // same-SiteInstanceGroup RenderFrameHosts. When the corresponding
643 // RenderFrameHost gets committed and becomes the current RenderFrameHost,
644 // `speculative_render_view_host_` will be moved to `render_view_host_map_`,
645 // overwriting the previous RenderViewHost of the same SiteInstanceGroup, if
646 // applicable. This field will also be reset at that time, or if the
647 // speculative RenderFrameHost gets deleted.
648 //
649 // For any given FrameTree, there will be at most one
650 // `speculative_render_view_host_`, because only main-frame speculative
651 // RenderFrameHosts have speculative RenderViewHosts, and there is at most one
652 // such RenderFrameHost per FrameTree at a time.
653 // This is a WeakPtr, since the RenderViewHost is owned by the
654 // RenderFrameHostImpl, not the FrameTree. This implies that if the owning
655 // RenderFrameHostImpl gets deleted, this will too.
656 //
657 // This supports but is independent of RenderDocument, which introduces cases
658 // where there may be more than one RenderViewHost per SiteInstanceGroup, such
659 // as cross-page same-SiteInstanceGroup navigations. The speculative
660 // RenderFrameHost has an associated RenderViewHost, but it cannot be put in
661 // `render_view_host_map_` when it is created, as the existing RenderViewHost
662 // will be incorrectly overwritten.
663 // TODO(yangsharon, crbug.com/1336305): Expand support to include
664 // cross-SiteInstanceGroup main-frame navigations, so all main-frame
665 // navigations use speculative RenderViewHost.
666 base::WeakPtr<RenderViewHostImpl> speculative_render_view_host_;
667
Harkiran Bolaria16f2c48d2022-04-22 12:39:57668 // Indicates type of frame tree.
669 const Type type_;
670
Avi Drissmanbd153642024-09-03 18:58:05671 FrameTreeNodeId focused_frame_tree_node_id_;
danakjc492bf82020-09-09 20:02:44672
Carlos Caballeroede6f8c2021-01-28 11:01:50673 // Whether the initial empty page has been accessed by another page, making it
674 // unsafe to show the pending URL. Usually false unless another window tries
675 // to modify the blank page. Always false after the first commit.
676 bool has_accessed_initial_main_document_ = false;
677
Takashi Toyoshimaea534ef22021-07-21 03:27:59678 bool is_being_destroyed_ = false;
679
Carlos Caballero101ac26b2021-03-24 11:54:05680#if DCHECK_IS_ON()
681 // Whether Shutdown() was called.
682 bool was_shut_down_ = false;
683#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31684
Paul Semel3e241042022-10-11 12:57:31685 // The root FrameTreeNode.
686 //
687 // Note: It is common for a node to test whether it's the root node, via the
688 // `root()` method, even while `root_` is running its destructor.
689 // For that reason, we want to destroy |root_| before any other fields.
690 FrameTreeNode root_;
691
Dave Tapuskad8b0530f2021-10-19 15:12:31692 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44693};
694
695} // namespace content
696
697#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_