blob: 10c584b3bf6bb8a9db1582e66013ead09bd171c4 [file] [log] [blame]
danakjc492bf82020-09-09 20:02:441// Copyright 2013 The Chromium Authors. All rights reserved.
2// 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
15#include "base/callback.h"
16#include "base/containers/queue.h"
Carlos Caballero101ac26b2021-03-24 11:54:0517#include "base/dcheck_is_on.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"
danakjc492bf82020-09-09 20:02:4422#include "content/browser/renderer_host/navigator.h"
23#include "content/browser/renderer_host/navigator_delegate.h"
24#include "content/browser/renderer_host/render_frame_host_manager.h"
25#include "content/common/content_export.h"
26#include "mojo/public/cpp/bindings/pending_receiver.h"
27#include "services/service_manager/public/mojom/interface_provider.mojom.h"
David Bokanc3fb5fa2022-07-04 14:55:3128#include "third_party/blink/public/common/features.h"
Kevin McNee43fe8292021-10-04 22:59:4129#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Chris Hamilton3ff6ed0e2021-02-19 03:54:0430#include "third_party/blink/public/common/tokens/tokens.h"
danakjc492bf82020-09-09 20:02:4431#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom-forward.h"
32
33namespace blink {
Lei Zhang4a867672021-07-19 06:30:1434namespace mojom {
35class BrowserInterfaceBroker;
36enum class TreeScopeType;
37} // namespace mojom
38
danakjc492bf82020-09-09 20:02:4439struct FramePolicy;
40} // namespace blink
41
42namespace content {
43
Carlos Caballero40b0efd2021-01-26 11:55:0044class BrowserContext;
Jeremy Roman2d8dfe132021-07-06 20:51:2645class PageDelegate;
Lei Zhang4a867672021-07-19 06:30:1446class PageImpl;
danakjc492bf82020-09-09 20:02:4447class RenderFrameHostDelegate;
48class RenderViewHostDelegate;
49class RenderViewHostImpl;
50class RenderFrameHostManager;
51class RenderWidgetHostDelegate;
Carlos Caballero40b0efd2021-01-26 11:55:0052class SiteInstance;
Sharon Yang57bde122022-03-01 20:01:1253class SiteInstanceGroup;
danakjc492bf82020-09-09 20:02:4454
55// Represents the frame tree for a page. With the exception of the main frame,
56// all FrameTreeNodes will be created/deleted in response to frame attach and
57// detach events in the DOM.
58//
59// The main frame's FrameTreeNode is special in that it is reused. This allows
60// it to serve as an anchor for state that needs to persist across top-level
61// page navigations.
62//
63// TODO(ajwong): Move NavigationController ownership to the main frame
64// FrameTreeNode. Possibly expose access to it from here.
65//
66// This object is only used on the UI thread.
67class CONTENT_EXPORT FrameTree {
68 public:
69 class NodeRange;
70
Alan Zhao8a882d12022-05-18 01:32:1571 class CONTENT_EXPORT NodeIterator {
danakjc492bf82020-09-09 20:02:4472 public:
Alan Zhao8a882d12022-05-18 01:32:1573 using iterator_category = std::forward_iterator_tag;
74 using value_type = FrameTreeNode*;
75 using difference_type = std::ptrdiff_t;
76 using pointer = FrameTreeNode**;
77 using reference = FrameTreeNode*&;
78
danakjc492bf82020-09-09 20:02:4479 NodeIterator(const NodeIterator& other);
80 ~NodeIterator();
81
82 NodeIterator& operator++();
Kevin McNee5f594382021-05-06 23:18:2383 // Advances the iterator and excludes the children of the current node
84 NodeIterator& AdvanceSkippingChildren();
danakjc492bf82020-09-09 20:02:4485
86 bool operator==(const NodeIterator& rhs) const;
87 bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); }
88
89 FrameTreeNode* operator*() { return current_node_; }
90
91 private:
Jayson Adams4db0bfe22021-07-15 19:24:0792 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4493 friend class NodeRange;
94
Kevin McNee5f594382021-05-06 23:18:2395 NodeIterator(const std::vector<FrameTreeNode*>& starting_nodes,
96 const FrameTreeNode* root_of_subtree_to_skip,
97 bool should_descend_into_inner_trees);
98
99 void AdvanceNode();
danakjc492bf82020-09-09 20:02:44100
Lukasz Anforowicz877e6222021-11-26 00:03:23101 // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for
102 // performance reasons (based on analysis of sampling profiler data and
103 // tab_search:top100:2020).
Keishi Hattori488b7602022-05-02 13:09:31104 RAW_PTR_EXCLUSION FrameTreeNode* current_node_;
105 RAW_PTR_EXCLUSION const FrameTreeNode* const root_of_subtree_to_skip_;
Lukasz Anforowicz877e6222021-11-26 00:03:23106
Kevin McNee5f594382021-05-06 23:18:23107 const bool should_descend_into_inner_trees_;
Jayson Adams4db0bfe22021-07-15 19:24:07108 base::circular_deque<FrameTreeNode*> queue_;
danakjc492bf82020-09-09 20:02:44109 };
110
111 class CONTENT_EXPORT NodeRange {
112 public:
Kevin McNee5f594382021-05-06 23:18:23113 NodeRange(const NodeRange&);
114 ~NodeRange();
115
danakjc492bf82020-09-09 20:02:44116 NodeIterator begin();
117 NodeIterator end();
118
119 private:
120 friend class FrameTree;
121
Kevin McNee5f594382021-05-06 23:18:23122 NodeRange(const std::vector<FrameTreeNode*>& starting_nodes,
123 const FrameTreeNode* root_of_subtree_to_skip,
124 bool should_descend_into_inner_trees);
danakjc492bf82020-09-09 20:02:44125
Kevin McNee5f594382021-05-06 23:18:23126 const std::vector<FrameTreeNode*> starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52127 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23128 const bool should_descend_into_inner_trees_;
danakjc492bf82020-09-09 20:02:44129 };
130
Carlos Caballero03262522021-02-05 14:49:58131 class CONTENT_EXPORT Delegate {
132 public:
133 // A RenderFrameHost in the specified |frame_tree_node| started loading a
134 // new document. This corresponds to browser UI starting to show a spinner
135 // or other visual indicator for loading. This method is only invoked if the
Nate Chapin9aabf5f2021-11-12 00:31:19136 // FrameTree hadn't been previously loading. |should_show_loading_ui| will
137 // be true unless the load is a fragment navigation, or triggered by
Carlos Caballero03262522021-02-05 14:49:58138 // history.pushState/replaceState.
139 virtual void DidStartLoading(FrameTreeNode* frame_tree_node,
Nate Chapin9aabf5f2021-11-12 00:31:19140 bool should_show_loading_ui) = 0;
Carlos Caballero03262522021-02-05 14:49:58141
Takashi Toyoshima74090df62021-03-09 14:34:57142 // This is called when all nodes in the FrameTree stopped loading. This
Carlos Caballero03262522021-02-05 14:49:58143 // corresponds to the browser UI stop showing a spinner or other visual
144 // indicator for loading.
145 virtual void DidStopLoading() = 0;
146
147 // The load progress was changed.
148 virtual void DidChangeLoadProgress() = 0;
Carlos Caballero6ff6ace2021-02-05 16:53:00149
Sreeja Kamishettyd64b993d2022-02-14 12:04:42150 // Returns the delegate's top loading tree, which should be used to infer
Yu Gaoc8c18552022-06-22 14:38:45151 // the values of loading-related states. The state of
152 // IsLoadingIncludingInnerFrameTrees() is a WebContents level concept and
153 // LoadingTree would return the frame tree to which loading events should be
154 // directed.
Sreeja Kamishettyd64b993d2022-02-14 12:04:42155 //
156 // TODO(crbug.com/1261928): Remove this method and directly rely on
157 // GetOutermostMainFrame() once portals and guest views are migrated to
158 // MPArch.
159 virtual FrameTree* LoadingTree() = 0;
160
Carlos Caballero6ff6ace2021-02-05 16:53:00161 // Returns true when the active RenderWidgetHostView should be hidden.
162 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25163
Sreeja Kamishetty53468972021-07-13 11:53:32164 // Called when current Page of this frame tree changes to `page`.
165 virtual void NotifyPageChanged(PageImpl& page) = 0;
Dominic Farolinoedf44ee2021-07-20 23:50:59166
167 // If the FrameTree using this delegate is an inner/nested FrameTree, then
168 // there may be a FrameTreeNode in the outer FrameTree that is considered
169 // our outer delegate FrameTreeNode. This method returns the outer delegate
170 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
171 // FrameTreeNode, this method returns
172 // FrameTreeNode::kFrameTreeNodeInvalidId.
173 virtual int GetOuterDelegateFrameTreeNodeId() = 0;
Dave Tapuskac8de3b02021-12-03 21:51:01174
175 // Returns if this FrameTree represents a portal.
176 virtual bool IsPortal() = 0;
Carlos Caballero03262522021-02-05 14:49:58177 };
178
Sreeja Kamishetty74bacd522021-03-22 17:04:24179 // Type of FrameTree instance.
180 enum class Type {
181 // This FrameTree is the primary frame tree for the WebContents, whose main
182 // document URL is shown in the Omnibox.
183 kPrimary,
184
185 // This FrameTree is used to prerender a page in the background which is
186 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36187 kPrerender,
188
189 // This FrameTree is used to host the contents of a <fencedframe> element.
190 // Even for <fencedframe>s hosted inside a prerendered page, the FrameTree
191 // associated with the fenced frame will be kFencedFrame, but the
192 // RenderFrameHosts inside of it will have their lifecycle state set to
193 // `RenderFrameHost::LifecycleState::kActive`.
194 //
195 // TODO(crbug.com/1232528, crbug.com/1244274): We should either:
196 // * Fully support nested FrameTrees inside prerendered pages, in which
197 // case all of the RenderFrameHosts inside the nested trees should have
198 // their lifecycle state set to kPrerendering, or...
199 // * Ban nested FrameTrees in prerendered pages, and therefore obsolete the
200 // above paragraph about <fencedframe>s hosted in prerendered pages.
201 kFencedFrame
Sreeja Kamishetty74bacd522021-03-22 17:04:24202 };
Sreeja Kamishetty837a10402021-04-23 12:41:59203
204 // A set of delegates are remembered here so that we can create
205 // RenderFrameHostManagers.
Dave Tapuska144570102022-08-02 19:28:27206 // `dev_tools_frame_token` is the devtools token for the root `FrameTreeNode`
207 // of this new `FrameTree`.
Sreeja Kamishetty837a10402021-04-23 12:41:59208 FrameTree(BrowserContext* browser_context,
209 Delegate* delegate,
210 NavigationControllerDelegate* navigation_controller_delegate,
211 NavigatorDelegate* navigator_delegate,
212 RenderFrameHostDelegate* render_frame_delegate,
213 RenderViewHostDelegate* render_view_delegate,
214 RenderWidgetHostDelegate* render_widget_delegate,
215 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26216 PageDelegate* page_delegate,
Dave Tapuska144570102022-08-02 19:28:27217 Type type,
218 const base::UnguessableToken& devtools_frame_token);
Peter Boström828b9022021-09-21 02:28:43219
220 FrameTree(const FrameTree&) = delete;
221 FrameTree& operator=(const FrameTree&) = delete;
222
Sreeja Kamishetty837a10402021-04-23 12:41:59223 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24224
Carlos Caballero40b0efd2021-01-26 11:55:00225 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17226 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni2322f4f82022-01-24 13:24:24227 // creates an initial NavigationEntry (if the InitialNavigationEntry feature
Rakina Zata Amni4eb716e2022-04-05 21:32:46228 // is enabled) that potentially inherits `opener_for_origin`'s origin in its
Rakina Zata Amni2322f4f82022-01-24 13:24:24229 // NavigationController. This method will call back into the delegates so it
230 // should only be called once they have completed their initialization. Pass
231 // in frame_policy so that it can be set in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00232 // TODO(carlscab): It would be great if initialization could happened in the
233 // constructor so we do not leave objects in a half initialized state.
234 void Init(SiteInstance* main_frame_site_instance,
235 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17236 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46237 RenderFrameHostImpl* opener_for_origin,
Harkiran Bolaria5ce27632022-01-20 15:05:05238 const blink::FramePolicy& frame_policy);
Sreeja Kamishetty837a10402021-04-23 12:41:59239
240 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00241
danakjc492bf82020-09-09 20:02:44242 FrameTreeNode* root() const { return root_; }
243
Sreeja Kamishetty74bacd522021-03-22 17:04:24244 bool is_prerendering() const { return type_ == FrameTree::Type::kPrerender; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46245
Sreeja Kamishetty60e47132022-02-08 12:51:53246 // Returns true if this frame tree is a portal.
247 //
248 // TODO(crbug.com/1254770): Once portals are migrated to MPArch, portals will
249 // have their own FrameTree::Type.
250 bool IsPortal();
251
Carlos Caballero03262522021-02-05 14:49:58252 Delegate* delegate() { return delegate_; }
253
Jeremy Roman2d8dfe132021-07-06 20:51:26254 // Delegates for various objects. These can be kept centrally on the
255 // FrameTree because they are expected to be the same for all frames on a
256 // given FrameTree.
danakjc492bf82020-09-09 20:02:44257 RenderFrameHostDelegate* render_frame_delegate() {
258 return render_frame_delegate_;
259 }
260 RenderViewHostDelegate* render_view_delegate() {
261 return render_view_delegate_;
262 }
263 RenderWidgetHostDelegate* render_widget_delegate() {
264 return render_widget_delegate_;
265 }
266 RenderFrameHostManager::Delegate* manager_delegate() {
267 return manager_delegate_;
268 }
Jeremy Roman2d8dfe132021-07-06 20:51:26269 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09270
Albert J. Wong1b6dc962021-07-09 18:06:57271 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
Sharon Yangc581a0c2021-11-02 18:09:39272
273 // SiteInstanceGroup IDs are used to look up RenderViewHosts, since there is
274 // one RenderViewHost per SiteInstanceGroup in a given FrameTree.
Aaron Colwell78b4bde2021-03-16 16:16:09275 using RenderViewHostMap = std::unordered_map<RenderViewHostMapId,
276 RenderViewHostImpl*,
277 RenderViewHostMapId::Hasher>;
278 const RenderViewHostMap& render_view_hosts() const {
danakjc492bf82020-09-09 20:02:44279 return render_view_host_map_;
280 }
281
282 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
283 // of this FrameTree.
284 FrameTreeNode* FindByID(int frame_tree_node_id);
285
286 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
287 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
288
289 // Returns the first frame in this tree with the given |name|, or the main
290 // frame if |name| is empty.
291 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
292 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
293 FrameTreeNode* FindByName(const std::string& name);
294
295 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
296 // breadth-first traversal order.
297 NodeRange Nodes();
298
299 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
300 // frame tree, starting from |subtree_root|.
301 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
302
Kevin McNee53f0b2d2021-11-02 18:00:45303 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
304 // well as any FrameTreeNodes of inner frame trees. Note that this includes
305 // inner frame trees of inner WebContents as well.
306 NodeRange NodesIncludingInnerTreeNodes();
307
Kevin McNee5f594382021-05-06 23:18:23308 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
309 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45310 // frame trees. Note that this includes inner frame trees of inner WebContents
311 // as well.
Kevin McNee5f594382021-05-06 23:18:23312 static NodeRange SubtreeAndInnerTreeNodes(RenderFrameHostImpl* parent);
313
danakjc492bf82020-09-09 20:02:44314 // Adds a new child frame to the frame tree. |process_id| is required to
315 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41316 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44317 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
318 // interface through which the child RenderFrame can access Mojo services
319 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30320 // sending the client end of the interface down to the
321 // RenderFrame. |policy_container_bind_params|, if not null, is used for
322 // binding Blink's policy container to the new RenderFrameHost's
323 // PolicyContainerHost. This is only needed if this frame is the result of the
324 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41325 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
326 // the added frame is only to serve as a placeholder for an inner frame tree
327 // (e.g. fenced frames, portals) and will not have a live RenderFrame of its
328 // own.
danakjc492bf82020-09-09 20:02:44329 FrameTreeNode* AddFrame(
330 RenderFrameHostImpl* parent,
331 int process_id,
332 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18333 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44334 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
335 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30336 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
Dominic Farolino12e06d72022-08-05 02:29:49337 mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
338 associated_interface_provider_receiver,
danakjc492bf82020-09-09 20:02:44339 blink::mojom::TreeScopeType scope,
340 const std::string& frame_name,
341 const std::string& frame_unique_name,
342 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04343 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44344 const base::UnguessableToken& devtools_frame_token,
345 const blink::FramePolicy& frame_policy,
346 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
347 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41348 blink::FrameOwnerElementType owner_type,
349 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44350
351 // Removes a frame from the frame tree. |child|, its children, and objects
352 // owned by their RenderFrameHostManagers are immediately deleted. The root
353 // node cannot be removed this way.
354 void RemoveFrame(FrameTreeNode* child);
355
356 // This method walks the entire frame tree and creates a RenderFrameProxyHost
357 // for the given |site_instance| in each node except the |source| one --
358 // the source will have a RenderFrameHost. |source| may be null if there is
359 // no node navigating in this frame tree (such as when this is called
360 // for an opener's frame tree), in which case no nodes are skipped for
Harkiran Bolaria2912a6b32022-02-22 16:43:45361 // RenderFrameProxyHost creation. |source_new_browsing_context_state| is the
362 // BrowsingContextState used by the speculative frame host, which may differ
363 // from the BrowsingContextState in |source| during cross-origin cross-
364 // browsing-instance navigations.
danakjc492bf82020-09-09 20:02:44365 void CreateProxiesForSiteInstance(FrameTreeNode* source,
Harkiran Bolaria2912a6b32022-02-22 16:43:45366 SiteInstance* site_instance,
367 const scoped_refptr<BrowsingContextState>&
368 source_new_browsing_context_state);
danakjc492bf82020-09-09 20:02:44369
370 // Convenience accessor for the main frame's RenderFrameHostImpl.
371 RenderFrameHostImpl* GetMainFrame() const;
372
373 // Returns the focused frame.
374 FrameTreeNode* GetFocusedFrame();
375
Sharon Yangefe52632022-03-08 23:06:06376 // Sets the focused frame to |node|. |source| identifies the
377 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
378 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
379 // notified about the new focused frame. Note that |source| may differ from
380 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
381 // window.focus() calls).
382 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44383
danakjc492bf82020-09-09 20:02:44384 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
385 //
386 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
387 // of this object.
388 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
389 SiteInstance* site_instance,
390 int32_t main_frame_routing_id,
danakj08eb51d2020-12-30 20:15:23391 bool swapped_out,
Harkiran Bolaria57e2b062022-03-14 10:27:58392 bool renderer_initiated_creation,
393 scoped_refptr<BrowsingContextState> main_browsing_context_state);
danakjc492bf82020-09-09 20:02:44394
395 // Returns the existing RenderViewHost for a new RenderFrameHost.
396 // There should always be such a RenderViewHost, because the main frame
397 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang57bde122022-03-01 20:01:12398 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44399
Sharon Yangc581a0c2021-11-02 18:09:39400 // Returns the ID used for the RenderViewHost associated with
401 // |site_instance_group|.
402 RenderViewHostMapId GetRenderViewHostMapId(
403 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09404
danakjc492bf82020-09-09 20:02:44405 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09406 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44407 //
408 // This method does not take ownership of|rvh|.
409 //
410 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
411 // |rvh|'s SiteInstance.
412 //
413 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
414 // *must* be called for |rvh| when it is destroyed or put into the
415 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
416 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09417 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44418
419 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09420 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
421 // called for a |render_view_host| that is not currently set for reuse.
422 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13423 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44424
425 // This is called when the frame is about to be removed and started to run
426 // unload handlers.
427 void FrameUnloading(FrameTreeNode* frame);
428
429 // This is only meant to be called by FrameTreeNode. Triggers calling
430 // the listener installed by SetFrameRemoveListener.
431 void FrameRemoved(FrameTreeNode* frame);
432
Carlos Caballero03262522021-02-05 14:49:58433 void DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19434 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58435 bool was_previously_loading);
436 void DidStopLoadingNode(FrameTreeNode& node);
Carlos Caballero03262522021-02-05 14:49:58437 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44438
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15439 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
440 // is navigating returns `blink::kInitialLoadProgress`.
441 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44442
Sreeja Kamishetty15f9944a22022-03-10 10:16:08443 // Returns true if at least one of the nodes in this frame tree or nodes in
444 // any inner frame tree of the same WebContents is loading.
Yu Gaoc8c18552022-06-22 14:38:45445 bool IsLoadingIncludingInnerFrameTrees() const;
danakjc492bf82020-09-09 20:02:44446
447 // Set page-level focus in all SiteInstances involved in rendering
448 // this FrameTree, not including the current main frame's
449 // SiteInstance. The focus update will be sent via the main frame's proxies
450 // in those SiteInstances.
451 void ReplicatePageFocus(bool is_focused);
452
453 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06454 // identified by |group|.
455 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44456
W. James MacLeanc07dc41b2022-07-25 18:52:16457 // Walks the current frame tree and registers any origins matching
458 // `previously_visited_origin`, either the last committed origin of a
459 // RenderFrameHost or the origin associated with a NavigationRequest that has
460 // been assigned to a SiteInstance, as having the default origin isolation
461 // state. This is only necessary when `previously_visited_origin` is seen with
462 // an OriginAgentCluster header explicitly requesting something other than the
463 // default.
464 void RegisterExistingOriginAsHavingDefaultIsolation(
danakjc492bf82020-09-09 20:02:44465 const url::Origin& previously_visited_origin,
466 NavigationRequest* navigation_request_to_exclude);
467
Carlos Caballero40b0efd2021-01-26 11:55:00468 NavigationControllerImpl& controller() { return navigator_.controller(); }
danakjc492bf82020-09-09 20:02:44469 Navigator& navigator() { return navigator_; }
470
Carlos Caballeroede6f8c2021-01-28 11:01:50471 // Another page accessed the initial empty main document, which means it
472 // is no longer safe to display a pending URL without risking a URL spoof.
473 void DidAccessInitialMainDocument();
474
475 bool has_accessed_initial_main_document() const {
476 return has_accessed_initial_main_document_;
477 }
478
479 void ResetHasAccessedInitialMainDocument() {
480 has_accessed_initial_main_document_ = false;
481 }
482
Carlos Caballero6ff6ace2021-02-05 16:53:00483 bool IsHidden() const { return delegate_->IsHidden(); }
484
Sreeja Kamishettyd64b993d2022-02-14 12:04:42485 // LoadingTree returns the following for different frame trees to direct
486 // loading related events. Please see FrameTree::Delegate::LoadingTree for
487 // more comments.
488 // - For prerender frame tree -> returns the frame tree itself.
489 // - For fenced frame and primary frame tree (including portal) -> returns
490 // the delegate's primary frame tree.
491 FrameTree* LoadingTree();
492
Carlos Caballero03262522021-02-05 14:49:58493 // Stops all ongoing navigations in each of the nodes of this FrameTree.
494 void StopLoading();
495
Carlos Caballero101ac26b2021-03-24 11:54:05496 // Prepares this frame tree for destruction, cleaning up the internal state
497 // and firing the appropriate events like FrameDeleted.
498 // Must be called before FrameTree is destroyed.
499 void Shutdown();
500
Takashi Toyoshimaea534ef22021-07-21 03:27:59501 bool IsBeingDestroyed() const { return is_being_destroyed_; }
502
Dave Tapuskad8b0530f2021-10-19 15:12:31503 base::SafeRef<FrameTree> GetSafeRef();
504
Dave Tapuska54c76a032021-10-27 22:10:42505 // Walks up the FrameTree chain and focuses the FrameTreeNode where
506 // each inner FrameTree is attached.
507 void FocusOuterFrameTrees();
508
David Bokanc3fb5fa2022-07-04 14:55:31509 absl::optional<blink::features::FencedFramesImplementationType>
510 FencedFramesImplementationType() const {
511 return fenced_frames_impl_;
512 }
513
514 bool IsFencedFramesMPArchBased() const {
515 return fenced_frames_impl_.has_value() &&
516 fenced_frames_impl_.value() ==
517 blink::features::FencedFramesImplementationType::kMPArch;
518 }
519 bool IsFencedFramesShadowDOMBased() const {
520 return fenced_frames_impl_.has_value() &&
521 fenced_frames_impl_.value() ==
522 blink::features::FencedFramesImplementationType::kShadowDOM;
523 }
524
danakjc492bf82020-09-09 20:02:44525 private:
526 friend class FrameTreeTest;
527 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Sreeja Kamishettyd64b993d2022-02-14 12:04:42528 FRIEND_TEST_ALL_PREFIXES(PortalBrowserTest, NodesForIsLoading);
529 FRIEND_TEST_ALL_PREFIXES(FencedFrameBrowserTest, NodesForIsLoading);
Sharon Yang2f3304a2022-05-13 02:24:25530 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
531 CreateRenderViewAfterProcessKillAndClosedProxy);
danakjc492bf82020-09-09 20:02:44532
533 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
534 // breadth-first traversal order, skipping the subtree rooted at
535 // |node|, but including |node| itself.
536 NodeRange NodesExceptSubtree(FrameTreeNode* node);
537
Sreeja Kamishettyd64b993d2022-02-14 12:04:42538 // Returns all FrameTreeNodes in this frame tree, as well as any
539 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
540 // frame trees of inner delegates. This is used to find the aggregate
541 // IsLoading value for a frame tree.
542 //
543 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
544 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
545 // portals and guest views are migrated to MPArch.
546 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
547
Keishi Hattori0e45c022021-11-27 09:25:52548 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58549
danakjc492bf82020-09-09 20:02:44550 // These delegates are installed into all the RenderViewHosts and
551 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52552 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
553 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
554 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
555 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
556 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44557
558 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00559 // tree. Each FrameTreeNode will default to using it for navigation tasks in
560 // the frame.
danakjc492bf82020-09-09 20:02:44561 Navigator navigator_;
562
Aaron Colwell78b4bde2021-03-16 16:16:09563 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44564 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
565 // Each RenderViewHost maintains a refcount and is deleted when there are no
566 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09567 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44568
Harkiran Bolaria16f2c48d2022-04-22 12:39:57569 // Indicates type of frame tree.
570 const Type type_;
571
danakjc492bf82020-09-09 20:02:44572 // This is an owned ptr to the root FrameTreeNode, which never changes over
573 // the lifetime of the FrameTree. It is not a scoped_ptr because we need the
574 // pointer to remain valid even while the FrameTreeNode is being destroyed,
575 // since it's common for a node to test whether it's the root node.
Kalvin Lee75d70302022-07-20 19:22:03576 //
577 // TODO(crbug.com/1298696): content_browsertests breaks with MTECheckedPtr
578 // enabled. Triage.
579 raw_ptr<FrameTreeNode, DanglingUntriagedDegradeToNoOpWhenMTE> root_;
danakjc492bf82020-09-09 20:02:44580
581 int focused_frame_tree_node_id_;
582
danakjc492bf82020-09-09 20:02:44583 // Overall load progress.
584 double load_progress_;
585
David Bokanc3fb5fa2022-07-04 14:55:31586 absl::optional<blink::features::FencedFramesImplementationType>
587 fenced_frames_impl_;
588
Carlos Caballeroede6f8c2021-01-28 11:01:50589 // Whether the initial empty page has been accessed by another page, making it
590 // unsafe to show the pending URL. Usually false unless another window tries
591 // to modify the blank page. Always false after the first commit.
592 bool has_accessed_initial_main_document_ = false;
593
Takashi Toyoshimaea534ef22021-07-21 03:27:59594 bool is_being_destroyed_ = false;
595
Carlos Caballero101ac26b2021-03-24 11:54:05596#if DCHECK_IS_ON()
597 // Whether Shutdown() was called.
598 bool was_shut_down_ = false;
599#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31600
601 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44602};
603
604} // namespace content
605
606#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_