blob: 4fa6c98f9fdf5be498dd56618f9bc15ea38f08a5 [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"
Kevin McNee43fe8292021-10-04 22:59:4128#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
Chris Hamilton3ff6ed0e2021-02-19 03:54:0429#include "third_party/blink/public/common/tokens/tokens.h"
danakjc492bf82020-09-09 20:02:4430#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom-forward.h"
31
32namespace blink {
Lei Zhang4a867672021-07-19 06:30:1433namespace mojom {
34class BrowserInterfaceBroker;
35enum class TreeScopeType;
36} // namespace mojom
37
danakjc492bf82020-09-09 20:02:4438struct FramePolicy;
39} // namespace blink
40
41namespace content {
42
Carlos Caballero40b0efd2021-01-26 11:55:0043class BrowserContext;
Jeremy Roman2d8dfe132021-07-06 20:51:2644class PageDelegate;
Lei Zhang4a867672021-07-19 06:30:1445class PageImpl;
danakjc492bf82020-09-09 20:02:4446class RenderFrameHostDelegate;
47class RenderViewHostDelegate;
48class RenderViewHostImpl;
49class RenderFrameHostManager;
50class RenderWidgetHostDelegate;
Carlos Caballero40b0efd2021-01-26 11:55:0051class SiteInstance;
Sharon Yang57bde122022-03-01 20:01:1252class SiteInstanceGroup;
danakjc492bf82020-09-09 20:02:4453
54// Represents the frame tree for a page. With the exception of the main frame,
55// all FrameTreeNodes will be created/deleted in response to frame attach and
56// detach events in the DOM.
57//
58// The main frame's FrameTreeNode is special in that it is reused. This allows
59// it to serve as an anchor for state that needs to persist across top-level
60// page navigations.
61//
62// TODO(ajwong): Move NavigationController ownership to the main frame
63// FrameTreeNode. Possibly expose access to it from here.
64//
65// This object is only used on the UI thread.
66class CONTENT_EXPORT FrameTree {
67 public:
68 class NodeRange;
69
70 class CONTENT_EXPORT NodeIterator
Kevin McNeeb110d0c2021-10-26 15:53:0071 : public std::iterator<std::forward_iterator_tag, FrameTreeNode*> {
danakjc492bf82020-09-09 20:02:4472 public:
73 NodeIterator(const NodeIterator& other);
74 ~NodeIterator();
75
76 NodeIterator& operator++();
Kevin McNee5f594382021-05-06 23:18:2377 // Advances the iterator and excludes the children of the current node
78 NodeIterator& AdvanceSkippingChildren();
danakjc492bf82020-09-09 20:02:4479
80 bool operator==(const NodeIterator& rhs) const;
81 bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); }
82
83 FrameTreeNode* operator*() { return current_node_; }
84
85 private:
Jayson Adams4db0bfe22021-07-15 19:24:0786 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4487 friend class NodeRange;
88
Kevin McNee5f594382021-05-06 23:18:2389 NodeIterator(const std::vector<FrameTreeNode*>& starting_nodes,
90 const FrameTreeNode* root_of_subtree_to_skip,
91 bool should_descend_into_inner_trees);
92
93 void AdvanceNode();
danakjc492bf82020-09-09 20:02:4494
Lukasz Anforowicz877e6222021-11-26 00:03:2395 // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for
96 // performance reasons (based on analysis of sampling profiler data and
97 // tab_search:top100:2020).
danakjc492bf82020-09-09 20:02:4498 FrameTreeNode* current_node_;
Kevin McNee5f594382021-05-06 23:18:2399 const FrameTreeNode* const root_of_subtree_to_skip_;
Lukasz Anforowicz877e6222021-11-26 00:03:23100
Kevin McNee5f594382021-05-06 23:18:23101 const bool should_descend_into_inner_trees_;
Jayson Adams4db0bfe22021-07-15 19:24:07102 base::circular_deque<FrameTreeNode*> queue_;
danakjc492bf82020-09-09 20:02:44103 };
104
105 class CONTENT_EXPORT NodeRange {
106 public:
Kevin McNee5f594382021-05-06 23:18:23107 NodeRange(const NodeRange&);
108 ~NodeRange();
109
danakjc492bf82020-09-09 20:02:44110 NodeIterator begin();
111 NodeIterator end();
112
113 private:
114 friend class FrameTree;
115
Kevin McNee5f594382021-05-06 23:18:23116 NodeRange(const std::vector<FrameTreeNode*>& starting_nodes,
117 const FrameTreeNode* root_of_subtree_to_skip,
118 bool should_descend_into_inner_trees);
danakjc492bf82020-09-09 20:02:44119
Kevin McNee5f594382021-05-06 23:18:23120 const std::vector<FrameTreeNode*> starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52121 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23122 const bool should_descend_into_inner_trees_;
danakjc492bf82020-09-09 20:02:44123 };
124
Carlos Caballero03262522021-02-05 14:49:58125 class CONTENT_EXPORT Delegate {
126 public:
127 // A RenderFrameHost in the specified |frame_tree_node| started loading a
128 // new document. This corresponds to browser UI starting to show a spinner
129 // or other visual indicator for loading. This method is only invoked if the
Nate Chapin9aabf5f2021-11-12 00:31:19130 // FrameTree hadn't been previously loading. |should_show_loading_ui| will
131 // be true unless the load is a fragment navigation, or triggered by
Carlos Caballero03262522021-02-05 14:49:58132 // history.pushState/replaceState.
133 virtual void DidStartLoading(FrameTreeNode* frame_tree_node,
Nate Chapin9aabf5f2021-11-12 00:31:19134 bool should_show_loading_ui) = 0;
Carlos Caballero03262522021-02-05 14:49:58135
Takashi Toyoshima74090df62021-03-09 14:34:57136 // This is called when all nodes in the FrameTree stopped loading. This
Carlos Caballero03262522021-02-05 14:49:58137 // corresponds to the browser UI stop showing a spinner or other visual
138 // indicator for loading.
139 virtual void DidStopLoading() = 0;
140
141 // The load progress was changed.
142 virtual void DidChangeLoadProgress() = 0;
Carlos Caballero6ff6ace2021-02-05 16:53:00143
Sreeja Kamishettyd64b993d2022-02-14 12:04:42144 // Returns the delegate's top loading tree, which should be used to infer
145 // the values of loading-related states. The state of IsLoading() is a
146 // WebContents level concept and LoadingTree would return the frame tree to
147 // which loading events should be directed.
148 //
149 // TODO(crbug.com/1261928): Remove this method and directly rely on
150 // GetOutermostMainFrame() once portals and guest views are migrated to
151 // MPArch.
152 virtual FrameTree* LoadingTree() = 0;
153
Carlos Caballero6ff6ace2021-02-05 16:53:00154 // Returns true when the active RenderWidgetHostView should be hidden.
155 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25156
Sreeja Kamishetty53468972021-07-13 11:53:32157 // Called when current Page of this frame tree changes to `page`.
158 virtual void NotifyPageChanged(PageImpl& page) = 0;
Dominic Farolinoedf44ee2021-07-20 23:50:59159
160 // If the FrameTree using this delegate is an inner/nested FrameTree, then
161 // there may be a FrameTreeNode in the outer FrameTree that is considered
162 // our outer delegate FrameTreeNode. This method returns the outer delegate
163 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
164 // FrameTreeNode, this method returns
165 // FrameTreeNode::kFrameTreeNodeInvalidId.
166 virtual int GetOuterDelegateFrameTreeNodeId() = 0;
Dave Tapuskac8de3b02021-12-03 21:51:01167
168 // Returns if this FrameTree represents a portal.
169 virtual bool IsPortal() = 0;
Carlos Caballero03262522021-02-05 14:49:58170 };
171
Sreeja Kamishetty74bacd522021-03-22 17:04:24172 // Type of FrameTree instance.
173 enum class Type {
174 // This FrameTree is the primary frame tree for the WebContents, whose main
175 // document URL is shown in the Omnibox.
176 kPrimary,
177
178 // This FrameTree is used to prerender a page in the background which is
179 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36180 kPrerender,
181
182 // This FrameTree is used to host the contents of a <fencedframe> element.
183 // Even for <fencedframe>s hosted inside a prerendered page, the FrameTree
184 // associated with the fenced frame will be kFencedFrame, but the
185 // RenderFrameHosts inside of it will have their lifecycle state set to
186 // `RenderFrameHost::LifecycleState::kActive`.
187 //
188 // TODO(crbug.com/1232528, crbug.com/1244274): We should either:
189 // * Fully support nested FrameTrees inside prerendered pages, in which
190 // case all of the RenderFrameHosts inside the nested trees should have
191 // their lifecycle state set to kPrerendering, or...
192 // * Ban nested FrameTrees in prerendered pages, and therefore obsolete the
193 // above paragraph about <fencedframe>s hosted in prerendered pages.
194 kFencedFrame
Sreeja Kamishetty74bacd522021-03-22 17:04:24195 };
Sreeja Kamishetty837a10402021-04-23 12:41:59196
197 // A set of delegates are remembered here so that we can create
198 // RenderFrameHostManagers.
199 FrameTree(BrowserContext* browser_context,
200 Delegate* delegate,
201 NavigationControllerDelegate* navigation_controller_delegate,
202 NavigatorDelegate* navigator_delegate,
203 RenderFrameHostDelegate* render_frame_delegate,
204 RenderViewHostDelegate* render_view_delegate,
205 RenderWidgetHostDelegate* render_widget_delegate,
206 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26207 PageDelegate* page_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59208 Type type);
Peter Boström828b9022021-09-21 02:28:43209
210 FrameTree(const FrameTree&) = delete;
211 FrameTree& operator=(const FrameTree&) = delete;
212
Sreeja Kamishetty837a10402021-04-23 12:41:59213 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24214
Carlos Caballero40b0efd2021-01-26 11:55:00215 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17216 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni2322f4f82022-01-24 13:24:24217 // creates an initial NavigationEntry (if the InitialNavigationEntry feature
Rakina Zata Amni4eb716e2022-04-05 21:32:46218 // is enabled) that potentially inherits `opener_for_origin`'s origin in its
Rakina Zata Amni2322f4f82022-01-24 13:24:24219 // NavigationController. This method will call back into the delegates so it
220 // should only be called once they have completed their initialization. Pass
221 // in frame_policy so that it can be set in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00222 // TODO(carlscab): It would be great if initialization could happened in the
223 // constructor so we do not leave objects in a half initialized state.
224 void Init(SiteInstance* main_frame_site_instance,
225 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17226 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46227 RenderFrameHostImpl* opener_for_origin,
Harkiran Bolaria5ce27632022-01-20 15:05:05228 const blink::FramePolicy& frame_policy);
Sreeja Kamishetty837a10402021-04-23 12:41:59229
230 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00231
danakjc492bf82020-09-09 20:02:44232 FrameTreeNode* root() const { return root_; }
233
Sreeja Kamishetty74bacd522021-03-22 17:04:24234 bool is_prerendering() const { return type_ == FrameTree::Type::kPrerender; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46235
Sreeja Kamishetty60e47132022-02-08 12:51:53236 // Returns true if this frame tree is a portal.
237 //
238 // TODO(crbug.com/1254770): Once portals are migrated to MPArch, portals will
239 // have their own FrameTree::Type.
240 bool IsPortal();
241
Carlos Caballero03262522021-02-05 14:49:58242 Delegate* delegate() { return delegate_; }
243
Jeremy Roman2d8dfe132021-07-06 20:51:26244 // Delegates for various objects. These can be kept centrally on the
245 // FrameTree because they are expected to be the same for all frames on a
246 // given FrameTree.
danakjc492bf82020-09-09 20:02:44247 RenderFrameHostDelegate* render_frame_delegate() {
248 return render_frame_delegate_;
249 }
250 RenderViewHostDelegate* render_view_delegate() {
251 return render_view_delegate_;
252 }
253 RenderWidgetHostDelegate* render_widget_delegate() {
254 return render_widget_delegate_;
255 }
256 RenderFrameHostManager::Delegate* manager_delegate() {
257 return manager_delegate_;
258 }
Jeremy Roman2d8dfe132021-07-06 20:51:26259 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09260
Albert J. Wong1b6dc962021-07-09 18:06:57261 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
Sharon Yangc581a0c2021-11-02 18:09:39262
263 // SiteInstanceGroup IDs are used to look up RenderViewHosts, since there is
264 // one RenderViewHost per SiteInstanceGroup in a given FrameTree.
Aaron Colwell78b4bde2021-03-16 16:16:09265 using RenderViewHostMap = std::unordered_map<RenderViewHostMapId,
266 RenderViewHostImpl*,
267 RenderViewHostMapId::Hasher>;
268 const RenderViewHostMap& render_view_hosts() const {
danakjc492bf82020-09-09 20:02:44269 return render_view_host_map_;
270 }
271
272 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
273 // of this FrameTree.
274 FrameTreeNode* FindByID(int frame_tree_node_id);
275
276 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
277 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
278
279 // Returns the first frame in this tree with the given |name|, or the main
280 // frame if |name| is empty.
281 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
282 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
283 FrameTreeNode* FindByName(const std::string& name);
284
285 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
286 // breadth-first traversal order.
287 NodeRange Nodes();
288
289 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
290 // frame tree, starting from |subtree_root|.
291 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
292
Kevin McNee53f0b2d2021-11-02 18:00:45293 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
294 // well as any FrameTreeNodes of inner frame trees. Note that this includes
295 // inner frame trees of inner WebContents as well.
296 NodeRange NodesIncludingInnerTreeNodes();
297
Kevin McNee5f594382021-05-06 23:18:23298 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
299 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45300 // frame trees. Note that this includes inner frame trees of inner WebContents
301 // as well.
Kevin McNee5f594382021-05-06 23:18:23302 static NodeRange SubtreeAndInnerTreeNodes(RenderFrameHostImpl* parent);
303
danakjc492bf82020-09-09 20:02:44304 // Adds a new child frame to the frame tree. |process_id| is required to
305 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41306 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44307 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
308 // interface through which the child RenderFrame can access Mojo services
309 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30310 // sending the client end of the interface down to the
311 // RenderFrame. |policy_container_bind_params|, if not null, is used for
312 // binding Blink's policy container to the new RenderFrameHost's
313 // PolicyContainerHost. This is only needed if this frame is the result of the
314 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41315 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
316 // the added frame is only to serve as a placeholder for an inner frame tree
317 // (e.g. fenced frames, portals) and will not have a live RenderFrame of its
318 // own.
danakjc492bf82020-09-09 20:02:44319 FrameTreeNode* AddFrame(
320 RenderFrameHostImpl* parent,
321 int process_id,
322 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18323 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44324 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
325 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30326 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
danakjc492bf82020-09-09 20:02:44327 blink::mojom::TreeScopeType scope,
328 const std::string& frame_name,
329 const std::string& frame_unique_name,
330 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04331 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44332 const base::UnguessableToken& devtools_frame_token,
333 const blink::FramePolicy& frame_policy,
334 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
335 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41336 blink::FrameOwnerElementType owner_type,
337 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44338
339 // Removes a frame from the frame tree. |child|, its children, and objects
340 // owned by their RenderFrameHostManagers are immediately deleted. The root
341 // node cannot be removed this way.
342 void RemoveFrame(FrameTreeNode* child);
343
344 // This method walks the entire frame tree and creates a RenderFrameProxyHost
345 // for the given |site_instance| in each node except the |source| one --
346 // the source will have a RenderFrameHost. |source| may be null if there is
347 // no node navigating in this frame tree (such as when this is called
348 // for an opener's frame tree), in which case no nodes are skipped for
Harkiran Bolaria2912a6b32022-02-22 16:43:45349 // RenderFrameProxyHost creation. |source_new_browsing_context_state| is the
350 // BrowsingContextState used by the speculative frame host, which may differ
351 // from the BrowsingContextState in |source| during cross-origin cross-
352 // browsing-instance navigations.
danakjc492bf82020-09-09 20:02:44353 void CreateProxiesForSiteInstance(FrameTreeNode* source,
Harkiran Bolaria2912a6b32022-02-22 16:43:45354 SiteInstance* site_instance,
355 const scoped_refptr<BrowsingContextState>&
356 source_new_browsing_context_state);
danakjc492bf82020-09-09 20:02:44357
358 // Convenience accessor for the main frame's RenderFrameHostImpl.
359 RenderFrameHostImpl* GetMainFrame() const;
360
361 // Returns the focused frame.
362 FrameTreeNode* GetFocusedFrame();
363
Sharon Yangefe52632022-03-08 23:06:06364 // Sets the focused frame to |node|. |source| identifies the
365 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
366 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
367 // notified about the new focused frame. Note that |source| may differ from
368 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
369 // window.focus() calls).
370 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44371
danakjc492bf82020-09-09 20:02:44372 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
373 //
374 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
375 // of this object.
376 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
377 SiteInstance* site_instance,
378 int32_t main_frame_routing_id,
danakj08eb51d2020-12-30 20:15:23379 bool swapped_out,
Harkiran Bolaria57e2b062022-03-14 10:27:58380 bool renderer_initiated_creation,
381 scoped_refptr<BrowsingContextState> main_browsing_context_state);
danakjc492bf82020-09-09 20:02:44382
383 // Returns the existing RenderViewHost for a new RenderFrameHost.
384 // There should always be such a RenderViewHost, because the main frame
385 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang57bde122022-03-01 20:01:12386 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44387
Sharon Yangc581a0c2021-11-02 18:09:39388 // Returns the ID used for the RenderViewHost associated with
389 // |site_instance_group|.
390 RenderViewHostMapId GetRenderViewHostMapId(
391 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09392
danakjc492bf82020-09-09 20:02:44393 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09394 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44395 //
396 // This method does not take ownership of|rvh|.
397 //
398 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
399 // |rvh|'s SiteInstance.
400 //
401 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
402 // *must* be called for |rvh| when it is destroyed or put into the
403 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
404 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09405 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44406
407 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09408 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
409 // called for a |render_view_host| that is not currently set for reuse.
410 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13411 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44412
413 // This is called when the frame is about to be removed and started to run
414 // unload handlers.
415 void FrameUnloading(FrameTreeNode* frame);
416
417 // This is only meant to be called by FrameTreeNode. Triggers calling
418 // the listener installed by SetFrameRemoveListener.
419 void FrameRemoved(FrameTreeNode* frame);
420
Carlos Caballero03262522021-02-05 14:49:58421 void DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19422 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58423 bool was_previously_loading);
424 void DidStopLoadingNode(FrameTreeNode& node);
Carlos Caballero03262522021-02-05 14:49:58425 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44426
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15427 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
428 // is navigating returns `blink::kInitialLoadProgress`.
429 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44430
Sreeja Kamishetty15f9944a22022-03-10 10:16:08431 // Returns true if at least one of the nodes in this frame tree or nodes in
432 // any inner frame tree of the same WebContents is loading.
433 //
434 // TODO(crbug.com/1293846): Rename to IsLoadingIncludingInnerFrameTrees() to
435 // adapt to new logic.
danakjc492bf82020-09-09 20:02:44436 bool IsLoading() const;
437
438 // Set page-level focus in all SiteInstances involved in rendering
439 // this FrameTree, not including the current main frame's
440 // SiteInstance. The focus update will be sent via the main frame's proxies
441 // in those SiteInstances.
442 void ReplicatePageFocus(bool is_focused);
443
444 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06445 // identified by |group|.
446 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44447
448 // Walks the current frame tree and registers any origins matching |origin|,
449 // either the last committed origin of a RenderFrameHost or the origin
450 // associated with a NavigationRequest that has been assigned to a
451 // SiteInstance, as not-opted-in for origin isolation.
452 void RegisterExistingOriginToPreventOptInIsolation(
453 const url::Origin& previously_visited_origin,
454 NavigationRequest* navigation_request_to_exclude);
455
Carlos Caballero40b0efd2021-01-26 11:55:00456 NavigationControllerImpl& controller() { return navigator_.controller(); }
danakjc492bf82020-09-09 20:02:44457 Navigator& navigator() { return navigator_; }
458
Carlos Caballeroede6f8c2021-01-28 11:01:50459 // Another page accessed the initial empty main document, which means it
460 // is no longer safe to display a pending URL without risking a URL spoof.
461 void DidAccessInitialMainDocument();
462
463 bool has_accessed_initial_main_document() const {
464 return has_accessed_initial_main_document_;
465 }
466
467 void ResetHasAccessedInitialMainDocument() {
468 has_accessed_initial_main_document_ = false;
469 }
470
Carlos Caballero6ff6ace2021-02-05 16:53:00471 bool IsHidden() const { return delegate_->IsHidden(); }
472
Sreeja Kamishettyd64b993d2022-02-14 12:04:42473 // LoadingTree returns the following for different frame trees to direct
474 // loading related events. Please see FrameTree::Delegate::LoadingTree for
475 // more comments.
476 // - For prerender frame tree -> returns the frame tree itself.
477 // - For fenced frame and primary frame tree (including portal) -> returns
478 // the delegate's primary frame tree.
479 FrameTree* LoadingTree();
480
Carlos Caballero03262522021-02-05 14:49:58481 // Stops all ongoing navigations in each of the nodes of this FrameTree.
482 void StopLoading();
483
Carlos Caballero101ac26b2021-03-24 11:54:05484 // Prepares this frame tree for destruction, cleaning up the internal state
485 // and firing the appropriate events like FrameDeleted.
486 // Must be called before FrameTree is destroyed.
487 void Shutdown();
488
Takashi Toyoshimaea534ef22021-07-21 03:27:59489 bool IsBeingDestroyed() const { return is_being_destroyed_; }
490
Dave Tapuskad8b0530f2021-10-19 15:12:31491 base::SafeRef<FrameTree> GetSafeRef();
492
Dave Tapuska54c76a032021-10-27 22:10:42493 // Walks up the FrameTree chain and focuses the FrameTreeNode where
494 // each inner FrameTree is attached.
495 void FocusOuterFrameTrees();
496
danakjc492bf82020-09-09 20:02:44497 private:
498 friend class FrameTreeTest;
499 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Sreeja Kamishettyd64b993d2022-02-14 12:04:42500 FRIEND_TEST_ALL_PREFIXES(PortalBrowserTest, NodesForIsLoading);
501 FRIEND_TEST_ALL_PREFIXES(FencedFrameBrowserTest, NodesForIsLoading);
danakjc492bf82020-09-09 20:02:44502
503 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
504 // breadth-first traversal order, skipping the subtree rooted at
505 // |node|, but including |node| itself.
506 NodeRange NodesExceptSubtree(FrameTreeNode* node);
507
Sreeja Kamishettyd64b993d2022-02-14 12:04:42508 // Returns all FrameTreeNodes in this frame tree, as well as any
509 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
510 // frame trees of inner delegates. This is used to find the aggregate
511 // IsLoading value for a frame tree.
512 //
513 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
514 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
515 // portals and guest views are migrated to MPArch.
516 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
517
Keishi Hattori0e45c022021-11-27 09:25:52518 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58519
danakjc492bf82020-09-09 20:02:44520 // These delegates are installed into all the RenderViewHosts and
521 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52522 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
523 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
524 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
525 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
526 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44527
528 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00529 // tree. Each FrameTreeNode will default to using it for navigation tasks in
530 // the frame.
danakjc492bf82020-09-09 20:02:44531 Navigator navigator_;
532
Aaron Colwell78b4bde2021-03-16 16:16:09533 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44534 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
535 // Each RenderViewHost maintains a refcount and is deleted when there are no
536 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09537 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44538
Harkiran Bolaria16f2c48d2022-04-22 12:39:57539 // Indicates type of frame tree.
540 const Type type_;
541
danakjc492bf82020-09-09 20:02:44542 // This is an owned ptr to the root FrameTreeNode, which never changes over
543 // the lifetime of the FrameTree. It is not a scoped_ptr because we need the
544 // pointer to remain valid even while the FrameTreeNode is being destroyed,
545 // since it's common for a node to test whether it's the root node.
Keishi Hattori0e45c022021-11-27 09:25:52546 raw_ptr<FrameTreeNode> root_;
danakjc492bf82020-09-09 20:02:44547
548 int focused_frame_tree_node_id_;
549
danakjc492bf82020-09-09 20:02:44550 // Overall load progress.
551 double load_progress_;
552
Carlos Caballeroede6f8c2021-01-28 11:01:50553 // Whether the initial empty page has been accessed by another page, making it
554 // unsafe to show the pending URL. Usually false unless another window tries
555 // to modify the blank page. Always false after the first commit.
556 bool has_accessed_initial_main_document_ = false;
557
Takashi Toyoshimaea534ef22021-07-21 03:27:59558 bool is_being_destroyed_ = false;
559
Carlos Caballero101ac26b2021-03-24 11:54:05560#if DCHECK_IS_ON()
561 // Whether Shutdown() was called.
562 bool was_shut_down_ = false;
563#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31564
565 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44566};
567
568} // namespace content
569
570#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_