blob: 798490ef3a453f2c02b49557947c9f1bf1333b2b [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
Alan Zhao8a882d12022-05-18 01:32:1570 class CONTENT_EXPORT NodeIterator {
danakjc492bf82020-09-09 20:02:4471 public:
Alan Zhao8a882d12022-05-18 01:32:1572 using iterator_category = std::forward_iterator_tag;
73 using value_type = FrameTreeNode*;
74 using difference_type = std::ptrdiff_t;
75 using pointer = FrameTreeNode**;
76 using reference = FrameTreeNode*&;
77
danakjc492bf82020-09-09 20:02:4478 NodeIterator(const NodeIterator& other);
79 ~NodeIterator();
80
81 NodeIterator& operator++();
Kevin McNee5f594382021-05-06 23:18:2382 // Advances the iterator and excludes the children of the current node
83 NodeIterator& AdvanceSkippingChildren();
danakjc492bf82020-09-09 20:02:4484
85 bool operator==(const NodeIterator& rhs) const;
86 bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); }
87
88 FrameTreeNode* operator*() { return current_node_; }
89
90 private:
Jayson Adams4db0bfe22021-07-15 19:24:0791 friend class FrameTreeTest;
danakjc492bf82020-09-09 20:02:4492 friend class NodeRange;
93
Kevin McNee5f594382021-05-06 23:18:2394 NodeIterator(const std::vector<FrameTreeNode*>& starting_nodes,
95 const FrameTreeNode* root_of_subtree_to_skip,
96 bool should_descend_into_inner_trees);
97
98 void AdvanceNode();
danakjc492bf82020-09-09 20:02:4499
Lukasz Anforowicz877e6222021-11-26 00:03:23100 // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for
101 // performance reasons (based on analysis of sampling profiler data and
102 // tab_search:top100:2020).
Keishi Hattori488b7602022-05-02 13:09:31103 RAW_PTR_EXCLUSION FrameTreeNode* current_node_;
104 RAW_PTR_EXCLUSION const FrameTreeNode* const root_of_subtree_to_skip_;
Lukasz Anforowicz877e6222021-11-26 00:03:23105
Kevin McNee5f594382021-05-06 23:18:23106 const bool should_descend_into_inner_trees_;
Jayson Adams4db0bfe22021-07-15 19:24:07107 base::circular_deque<FrameTreeNode*> queue_;
danakjc492bf82020-09-09 20:02:44108 };
109
110 class CONTENT_EXPORT NodeRange {
111 public:
Kevin McNee5f594382021-05-06 23:18:23112 NodeRange(const NodeRange&);
113 ~NodeRange();
114
danakjc492bf82020-09-09 20:02:44115 NodeIterator begin();
116 NodeIterator end();
117
118 private:
119 friend class FrameTree;
120
Kevin McNee5f594382021-05-06 23:18:23121 NodeRange(const std::vector<FrameTreeNode*>& starting_nodes,
122 const FrameTreeNode* root_of_subtree_to_skip,
123 bool should_descend_into_inner_trees);
danakjc492bf82020-09-09 20:02:44124
Kevin McNee5f594382021-05-06 23:18:23125 const std::vector<FrameTreeNode*> starting_nodes_;
Keishi Hattori0e45c022021-11-27 09:25:52126 const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_;
Kevin McNee5f594382021-05-06 23:18:23127 const bool should_descend_into_inner_trees_;
danakjc492bf82020-09-09 20:02:44128 };
129
Carlos Caballero03262522021-02-05 14:49:58130 class CONTENT_EXPORT Delegate {
131 public:
132 // A RenderFrameHost in the specified |frame_tree_node| started loading a
133 // new document. This corresponds to browser UI starting to show a spinner
134 // or other visual indicator for loading. This method is only invoked if the
Nate Chapin9aabf5f2021-11-12 00:31:19135 // FrameTree hadn't been previously loading. |should_show_loading_ui| will
136 // be true unless the load is a fragment navigation, or triggered by
Carlos Caballero03262522021-02-05 14:49:58137 // history.pushState/replaceState.
138 virtual void DidStartLoading(FrameTreeNode* frame_tree_node,
Nate Chapin9aabf5f2021-11-12 00:31:19139 bool should_show_loading_ui) = 0;
Carlos Caballero03262522021-02-05 14:49:58140
Takashi Toyoshima74090df62021-03-09 14:34:57141 // This is called when all nodes in the FrameTree stopped loading. This
Carlos Caballero03262522021-02-05 14:49:58142 // corresponds to the browser UI stop showing a spinner or other visual
143 // indicator for loading.
144 virtual void DidStopLoading() = 0;
145
146 // The load progress was changed.
147 virtual void DidChangeLoadProgress() = 0;
Carlos Caballero6ff6ace2021-02-05 16:53:00148
Sreeja Kamishettyd64b993d2022-02-14 12:04:42149 // Returns the delegate's top loading tree, which should be used to infer
150 // the values of loading-related states. The state of IsLoading() is a
151 // WebContents level concept and LoadingTree would return the frame tree to
152 // which loading events should be directed.
153 //
154 // TODO(crbug.com/1261928): Remove this method and directly rely on
155 // GetOutermostMainFrame() once portals and guest views are migrated to
156 // MPArch.
157 virtual FrameTree* LoadingTree() = 0;
158
Carlos Caballero6ff6ace2021-02-05 16:53:00159 // Returns true when the active RenderWidgetHostView should be hidden.
160 virtual bool IsHidden() = 0;
Sreeja Kamishettya21b4f62021-06-25 07:48:25161
Sreeja Kamishetty53468972021-07-13 11:53:32162 // Called when current Page of this frame tree changes to `page`.
163 virtual void NotifyPageChanged(PageImpl& page) = 0;
Dominic Farolinoedf44ee2021-07-20 23:50:59164
165 // If the FrameTree using this delegate is an inner/nested FrameTree, then
166 // there may be a FrameTreeNode in the outer FrameTree that is considered
167 // our outer delegate FrameTreeNode. This method returns the outer delegate
168 // FrameTreeNode ID if one exists. If we don't have a an outer delegate
169 // FrameTreeNode, this method returns
170 // FrameTreeNode::kFrameTreeNodeInvalidId.
171 virtual int GetOuterDelegateFrameTreeNodeId() = 0;
Dave Tapuskac8de3b02021-12-03 21:51:01172
173 // Returns if this FrameTree represents a portal.
174 virtual bool IsPortal() = 0;
Carlos Caballero03262522021-02-05 14:49:58175 };
176
Sreeja Kamishetty74bacd522021-03-22 17:04:24177 // Type of FrameTree instance.
178 enum class Type {
179 // This FrameTree is the primary frame tree for the WebContents, whose main
180 // document URL is shown in the Omnibox.
181 kPrimary,
182
183 // This FrameTree is used to prerender a page in the background which is
184 // invisible to the user.
Dominic Farolino4bc10ee2021-08-31 00:37:36185 kPrerender,
186
187 // This FrameTree is used to host the contents of a <fencedframe> element.
188 // Even for <fencedframe>s hosted inside a prerendered page, the FrameTree
189 // associated with the fenced frame will be kFencedFrame, but the
190 // RenderFrameHosts inside of it will have their lifecycle state set to
191 // `RenderFrameHost::LifecycleState::kActive`.
192 //
193 // TODO(crbug.com/1232528, crbug.com/1244274): We should either:
194 // * Fully support nested FrameTrees inside prerendered pages, in which
195 // case all of the RenderFrameHosts inside the nested trees should have
196 // their lifecycle state set to kPrerendering, or...
197 // * Ban nested FrameTrees in prerendered pages, and therefore obsolete the
198 // above paragraph about <fencedframe>s hosted in prerendered pages.
199 kFencedFrame
Sreeja Kamishetty74bacd522021-03-22 17:04:24200 };
Sreeja Kamishetty837a10402021-04-23 12:41:59201
202 // A set of delegates are remembered here so that we can create
203 // RenderFrameHostManagers.
204 FrameTree(BrowserContext* browser_context,
205 Delegate* delegate,
206 NavigationControllerDelegate* navigation_controller_delegate,
207 NavigatorDelegate* navigator_delegate,
208 RenderFrameHostDelegate* render_frame_delegate,
209 RenderViewHostDelegate* render_view_delegate,
210 RenderWidgetHostDelegate* render_widget_delegate,
211 RenderFrameHostManager::Delegate* manager_delegate,
Jeremy Roman2d8dfe132021-07-06 20:51:26212 PageDelegate* page_delegate,
Sreeja Kamishetty837a10402021-04-23 12:41:59213 Type type);
Peter Boström828b9022021-09-21 02:28:43214
215 FrameTree(const FrameTree&) = delete;
216 FrameTree& operator=(const FrameTree&) = delete;
217
Sreeja Kamishetty837a10402021-04-23 12:41:59218 ~FrameTree();
Sreeja Kamishetty74bacd522021-03-22 17:04:24219
Carlos Caballero40b0efd2021-01-26 11:55:00220 // Initializes the main frame for this FrameTree. That is it creates the
Rakina Zata Amniafd3c6582021-11-30 06:19:17221 // initial RenderFrameHost in the root node's RenderFrameHostManager, and also
Rakina Zata Amni2322f4f82022-01-24 13:24:24222 // creates an initial NavigationEntry (if the InitialNavigationEntry feature
Rakina Zata Amni4eb716e2022-04-05 21:32:46223 // is enabled) that potentially inherits `opener_for_origin`'s origin in its
Rakina Zata Amni2322f4f82022-01-24 13:24:24224 // NavigationController. This method will call back into the delegates so it
225 // should only be called once they have completed their initialization. Pass
226 // in frame_policy so that it can be set in the root node's replication_state.
Carlos Caballero40b0efd2021-01-26 11:55:00227 // TODO(carlscab): It would be great if initialization could happened in the
228 // constructor so we do not leave objects in a half initialized state.
229 void Init(SiteInstance* main_frame_site_instance,
230 bool renderer_initiated_creation,
Rakina Zata Amniafd3c6582021-11-30 06:19:17231 const std::string& main_frame_name,
Rakina Zata Amni4eb716e2022-04-05 21:32:46232 RenderFrameHostImpl* opener_for_origin,
Harkiran Bolaria5ce27632022-01-20 15:05:05233 const blink::FramePolicy& frame_policy);
Sreeja Kamishetty837a10402021-04-23 12:41:59234
235 Type type() const { return type_; }
Carlos Caballero40b0efd2021-01-26 11:55:00236
danakjc492bf82020-09-09 20:02:44237 FrameTreeNode* root() const { return root_; }
238
Sreeja Kamishetty74bacd522021-03-22 17:04:24239 bool is_prerendering() const { return type_ == FrameTree::Type::kPrerender; }
Sreeja Kamishetty46f762c2021-02-05 07:52:46240
Sreeja Kamishetty60e47132022-02-08 12:51:53241 // Returns true if this frame tree is a portal.
242 //
243 // TODO(crbug.com/1254770): Once portals are migrated to MPArch, portals will
244 // have their own FrameTree::Type.
245 bool IsPortal();
246
Carlos Caballero03262522021-02-05 14:49:58247 Delegate* delegate() { return delegate_; }
248
Jeremy Roman2d8dfe132021-07-06 20:51:26249 // Delegates for various objects. These can be kept centrally on the
250 // FrameTree because they are expected to be the same for all frames on a
251 // given FrameTree.
danakjc492bf82020-09-09 20:02:44252 RenderFrameHostDelegate* render_frame_delegate() {
253 return render_frame_delegate_;
254 }
255 RenderViewHostDelegate* render_view_delegate() {
256 return render_view_delegate_;
257 }
258 RenderWidgetHostDelegate* render_widget_delegate() {
259 return render_widget_delegate_;
260 }
261 RenderFrameHostManager::Delegate* manager_delegate() {
262 return manager_delegate_;
263 }
Jeremy Roman2d8dfe132021-07-06 20:51:26264 PageDelegate* page_delegate() { return page_delegate_; }
Aaron Colwell78b4bde2021-03-16 16:16:09265
Albert J. Wong1b6dc962021-07-09 18:06:57266 using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>;
Sharon Yangc581a0c2021-11-02 18:09:39267
268 // SiteInstanceGroup IDs are used to look up RenderViewHosts, since there is
269 // one RenderViewHost per SiteInstanceGroup in a given FrameTree.
Aaron Colwell78b4bde2021-03-16 16:16:09270 using RenderViewHostMap = std::unordered_map<RenderViewHostMapId,
271 RenderViewHostImpl*,
272 RenderViewHostMapId::Hasher>;
273 const RenderViewHostMap& render_view_hosts() const {
danakjc492bf82020-09-09 20:02:44274 return render_view_host_map_;
275 }
276
277 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
278 // of this FrameTree.
279 FrameTreeNode* FindByID(int frame_tree_node_id);
280
281 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
282 FrameTreeNode* FindByRoutingID(int process_id, int routing_id);
283
284 // Returns the first frame in this tree with the given |name|, or the main
285 // frame if |name| is empty.
286 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
287 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
288 FrameTreeNode* FindByName(const std::string& name);
289
290 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
291 // breadth-first traversal order.
292 NodeRange Nodes();
293
294 // Returns a range to iterate over all FrameTreeNodes in a subtree of the
295 // frame tree, starting from |subtree_root|.
296 NodeRange SubtreeNodes(FrameTreeNode* subtree_root);
297
Kevin McNee53f0b2d2021-11-02 18:00:45298 // Returns a range to iterate over all FrameTreeNodes in this frame tree, as
299 // well as any FrameTreeNodes of inner frame trees. Note that this includes
300 // inner frame trees of inner WebContents as well.
301 NodeRange NodesIncludingInnerTreeNodes();
302
Kevin McNee5f594382021-05-06 23:18:23303 // Returns a range to iterate over all FrameTreeNodes in a subtree, starting
304 // from, but not including |parent|, as well as any FrameTreeNodes of inner
Kevin McNee53f0b2d2021-11-02 18:00:45305 // frame trees. Note that this includes inner frame trees of inner WebContents
306 // as well.
Kevin McNee5f594382021-05-06 23:18:23307 static NodeRange SubtreeAndInnerTreeNodes(RenderFrameHostImpl* parent);
308
danakjc492bf82020-09-09 20:02:44309 // Adds a new child frame to the frame tree. |process_id| is required to
310 // disambiguate |new_routing_id|, and it must match the process of the
Kevin McNee43fe8292021-10-04 22:59:41311 // |parent| node. Otherwise no child is added and this method returns nullptr.
danakjc492bf82020-09-09 20:02:44312 // |interface_provider_receiver| is the receiver end of the InterfaceProvider
313 // interface through which the child RenderFrame can access Mojo services
314 // exposed by the corresponding RenderFrameHost. The caller takes care of
Antonio Sartoridb967c52021-01-20 09:54:30315 // sending the client end of the interface down to the
316 // RenderFrame. |policy_container_bind_params|, if not null, is used for
317 // binding Blink's policy container to the new RenderFrameHost's
318 // PolicyContainerHost. This is only needed if this frame is the result of the
319 // CreateChildFrame mojo call, which also delivers the
Kevin McNee43fe8292021-10-04 22:59:41320 // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if
321 // the added frame is only to serve as a placeholder for an inner frame tree
322 // (e.g. fenced frames, portals) and will not have a live RenderFrame of its
323 // own.
danakjc492bf82020-09-09 20:02:44324 FrameTreeNode* AddFrame(
325 RenderFrameHostImpl* parent,
326 int process_id,
327 int new_routing_id,
danakj0bdfacd2021-01-20 19:27:18328 mojo::PendingAssociatedRemote<mojom::Frame> frame_remote,
danakjc492bf82020-09-09 20:02:44329 mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
330 browser_interface_broker_receiver,
Antonio Sartoridb967c52021-01-20 09:54:30331 blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params,
danakjc492bf82020-09-09 20:02:44332 blink::mojom::TreeScopeType scope,
333 const std::string& frame_name,
334 const std::string& frame_unique_name,
335 bool is_created_by_script,
Chris Hamilton3ff6ed0e2021-02-19 03:54:04336 const blink::LocalFrameToken& frame_token,
danakjc492bf82020-09-09 20:02:44337 const base::UnguessableToken& devtools_frame_token,
338 const blink::FramePolicy& frame_policy,
339 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
340 bool was_discarded,
Kevin McNee43fe8292021-10-04 22:59:41341 blink::FrameOwnerElementType owner_type,
342 bool is_dummy_frame_for_inner_tree);
danakjc492bf82020-09-09 20:02:44343
344 // Removes a frame from the frame tree. |child|, its children, and objects
345 // owned by their RenderFrameHostManagers are immediately deleted. The root
346 // node cannot be removed this way.
347 void RemoveFrame(FrameTreeNode* child);
348
349 // This method walks the entire frame tree and creates a RenderFrameProxyHost
350 // for the given |site_instance| in each node except the |source| one --
351 // the source will have a RenderFrameHost. |source| may be null if there is
352 // no node navigating in this frame tree (such as when this is called
353 // for an opener's frame tree), in which case no nodes are skipped for
Harkiran Bolaria2912a6b32022-02-22 16:43:45354 // RenderFrameProxyHost creation. |source_new_browsing_context_state| is the
355 // BrowsingContextState used by the speculative frame host, which may differ
356 // from the BrowsingContextState in |source| during cross-origin cross-
357 // browsing-instance navigations.
danakjc492bf82020-09-09 20:02:44358 void CreateProxiesForSiteInstance(FrameTreeNode* source,
Harkiran Bolaria2912a6b32022-02-22 16:43:45359 SiteInstance* site_instance,
360 const scoped_refptr<BrowsingContextState>&
361 source_new_browsing_context_state);
danakjc492bf82020-09-09 20:02:44362
363 // Convenience accessor for the main frame's RenderFrameHostImpl.
364 RenderFrameHostImpl* GetMainFrame() const;
365
366 // Returns the focused frame.
367 FrameTreeNode* GetFocusedFrame();
368
Sharon Yangefe52632022-03-08 23:06:06369 // Sets the focused frame to |node|. |source| identifies the
370 // SiteInstanceGroup that initiated this focus change. If this FrameTree has
371 // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be
372 // notified about the new focused frame. Note that |source| may differ from
373 // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process
374 // window.focus() calls).
375 void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source);
danakjc492bf82020-09-09 20:02:44376
danakjc492bf82020-09-09 20:02:44377 // Creates a RenderViewHostImpl for a given |site_instance| in the tree.
378 //
379 // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership
380 // of this object.
381 scoped_refptr<RenderViewHostImpl> CreateRenderViewHost(
382 SiteInstance* site_instance,
383 int32_t main_frame_routing_id,
danakj08eb51d2020-12-30 20:15:23384 bool swapped_out,
Harkiran Bolaria57e2b062022-03-14 10:27:58385 bool renderer_initiated_creation,
386 scoped_refptr<BrowsingContextState> main_browsing_context_state);
danakjc492bf82020-09-09 20:02:44387
388 // Returns the existing RenderViewHost for a new RenderFrameHost.
389 // There should always be such a RenderViewHost, because the main frame
390 // RenderFrameHost for each SiteInstance should be created before subframes.
Sharon Yang57bde122022-03-01 20:01:12391 scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group);
danakjc492bf82020-09-09 20:02:44392
Sharon Yangc581a0c2021-11-02 18:09:39393 // Returns the ID used for the RenderViewHost associated with
394 // |site_instance_group|.
395 RenderViewHostMapId GetRenderViewHostMapId(
396 SiteInstanceGroup* site_instance_group) const;
Aaron Colwell78b4bde2021-03-16 16:16:09397
danakjc492bf82020-09-09 20:02:44398 // Registers a RenderViewHost so that it can be reused by other frames
Aaron Colwell78b4bde2021-03-16 16:16:09399 // whose SiteInstance maps to the same RenderViewHostMapId.
danakjc492bf82020-09-09 20:02:44400 //
401 // This method does not take ownership of|rvh|.
402 //
403 // NOTE: This method CHECK fails if a RenderViewHost is already registered for
404 // |rvh|'s SiteInstance.
405 //
406 // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost
407 // *must* be called for |rvh| when it is destroyed or put into the
408 // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to
409 // reuse it.
Aaron Colwell78b4bde2021-03-16 16:16:09410 void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh);
danakjc492bf82020-09-09 20:02:44411
412 // Unregisters the RenderViewHostImpl that's available for reuse for a
Aaron Colwell78b4bde2021-03-16 16:16:09413 // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is
414 // called for a |render_view_host| that is not currently set for reuse.
415 void UnregisterRenderViewHost(RenderViewHostMapId id,
Aaron Colwellc4bd7d62021-01-29 04:23:13416 RenderViewHostImpl* render_view_host);
danakjc492bf82020-09-09 20:02:44417
418 // This is called when the frame is about to be removed and started to run
419 // unload handlers.
420 void FrameUnloading(FrameTreeNode* frame);
421
422 // This is only meant to be called by FrameTreeNode. Triggers calling
423 // the listener installed by SetFrameRemoveListener.
424 void FrameRemoved(FrameTreeNode* frame);
425
Carlos Caballero03262522021-02-05 14:49:58426 void DidStartLoadingNode(FrameTreeNode& node,
Nate Chapin9aabf5f2021-11-12 00:31:19427 bool should_show_loading_ui,
Carlos Caballero03262522021-02-05 14:49:58428 bool was_previously_loading);
429 void DidStopLoadingNode(FrameTreeNode& node);
Carlos Caballero03262522021-02-05 14:49:58430 void DidCancelLoading();
danakjc492bf82020-09-09 20:02:44431
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15432 // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode
433 // is navigating returns `blink::kInitialLoadProgress`.
434 double GetLoadProgress();
danakjc492bf82020-09-09 20:02:44435
Sreeja Kamishetty15f9944a22022-03-10 10:16:08436 // Returns true if at least one of the nodes in this frame tree or nodes in
437 // any inner frame tree of the same WebContents is loading.
438 //
439 // TODO(crbug.com/1293846): Rename to IsLoadingIncludingInnerFrameTrees() to
440 // adapt to new logic.
danakjc492bf82020-09-09 20:02:44441 bool IsLoading() const;
442
443 // Set page-level focus in all SiteInstances involved in rendering
444 // this FrameTree, not including the current main frame's
445 // SiteInstance. The focus update will be sent via the main frame's proxies
446 // in those SiteInstances.
447 void ReplicatePageFocus(bool is_focused);
448
449 // Updates page-level focus for this FrameTree in the subframe renderer
Sharon Yangefe52632022-03-08 23:06:06450 // identified by |group|.
451 void SetPageFocus(SiteInstanceGroup* group, bool is_focused);
danakjc492bf82020-09-09 20:02:44452
453 // Walks the current frame tree and registers any origins matching |origin|,
454 // either the last committed origin of a RenderFrameHost or the origin
455 // associated with a NavigationRequest that has been assigned to a
456 // SiteInstance, as not-opted-in for origin isolation.
457 void RegisterExistingOriginToPreventOptInIsolation(
458 const url::Origin& previously_visited_origin,
459 NavigationRequest* navigation_request_to_exclude);
460
Carlos Caballero40b0efd2021-01-26 11:55:00461 NavigationControllerImpl& controller() { return navigator_.controller(); }
danakjc492bf82020-09-09 20:02:44462 Navigator& navigator() { return navigator_; }
463
Carlos Caballeroede6f8c2021-01-28 11:01:50464 // Another page accessed the initial empty main document, which means it
465 // is no longer safe to display a pending URL without risking a URL spoof.
466 void DidAccessInitialMainDocument();
467
468 bool has_accessed_initial_main_document() const {
469 return has_accessed_initial_main_document_;
470 }
471
472 void ResetHasAccessedInitialMainDocument() {
473 has_accessed_initial_main_document_ = false;
474 }
475
Carlos Caballero6ff6ace2021-02-05 16:53:00476 bool IsHidden() const { return delegate_->IsHidden(); }
477
Sreeja Kamishettyd64b993d2022-02-14 12:04:42478 // LoadingTree returns the following for different frame trees to direct
479 // loading related events. Please see FrameTree::Delegate::LoadingTree for
480 // more comments.
481 // - For prerender frame tree -> returns the frame tree itself.
482 // - For fenced frame and primary frame tree (including portal) -> returns
483 // the delegate's primary frame tree.
484 FrameTree* LoadingTree();
485
Carlos Caballero03262522021-02-05 14:49:58486 // Stops all ongoing navigations in each of the nodes of this FrameTree.
487 void StopLoading();
488
Carlos Caballero101ac26b2021-03-24 11:54:05489 // Prepares this frame tree for destruction, cleaning up the internal state
490 // and firing the appropriate events like FrameDeleted.
491 // Must be called before FrameTree is destroyed.
492 void Shutdown();
493
Takashi Toyoshimaea534ef22021-07-21 03:27:59494 bool IsBeingDestroyed() const { return is_being_destroyed_; }
495
Dave Tapuskad8b0530f2021-10-19 15:12:31496 base::SafeRef<FrameTree> GetSafeRef();
497
Dave Tapuska54c76a032021-10-27 22:10:42498 // Walks up the FrameTree chain and focuses the FrameTreeNode where
499 // each inner FrameTree is attached.
500 void FocusOuterFrameTrees();
501
danakjc492bf82020-09-09 20:02:44502 private:
503 friend class FrameTreeTest;
504 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame);
Sreeja Kamishettyd64b993d2022-02-14 12:04:42505 FRIEND_TEST_ALL_PREFIXES(PortalBrowserTest, NodesForIsLoading);
506 FRIEND_TEST_ALL_PREFIXES(FencedFrameBrowserTest, NodesForIsLoading);
Sharon Yang2f3304a2022-05-13 02:24:25507 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
508 CreateRenderViewAfterProcessKillAndClosedProxy);
danakjc492bf82020-09-09 20:02:44509
510 // Returns a range to iterate over all FrameTreeNodes in the frame tree in
511 // breadth-first traversal order, skipping the subtree rooted at
512 // |node|, but including |node| itself.
513 NodeRange NodesExceptSubtree(FrameTreeNode* node);
514
Sreeja Kamishettyd64b993d2022-02-14 12:04:42515 // Returns all FrameTreeNodes in this frame tree, as well as any
516 // FrameTreeNodes of inner frame trees. Note that this doesn't include inner
517 // frame trees of inner delegates. This is used to find the aggregate
518 // IsLoading value for a frame tree.
519 //
520 // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly
521 // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once
522 // portals and guest views are migrated to MPArch.
523 std::vector<FrameTreeNode*> CollectNodesForIsLoading();
524
Keishi Hattori0e45c022021-11-27 09:25:52525 const raw_ptr<Delegate> delegate_;
Carlos Caballero03262522021-02-05 14:49:58526
danakjc492bf82020-09-09 20:02:44527 // These delegates are installed into all the RenderViewHosts and
528 // RenderFrameHosts that we create.
Keishi Hattori0e45c022021-11-27 09:25:52529 raw_ptr<RenderFrameHostDelegate> render_frame_delegate_;
530 raw_ptr<RenderViewHostDelegate> render_view_delegate_;
531 raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_;
532 raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_;
533 raw_ptr<PageDelegate> page_delegate_;
danakjc492bf82020-09-09 20:02:44534
535 // The Navigator object responsible for managing navigations on this frame
Carlos Caballero40b0efd2021-01-26 11:55:00536 // tree. Each FrameTreeNode will default to using it for navigation tasks in
537 // the frame.
danakjc492bf82020-09-09 20:02:44538 Navigator navigator_;
539
Aaron Colwell78b4bde2021-03-16 16:16:09540 // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the
danakjc492bf82020-09-09 20:02:44541 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
542 // Each RenderViewHost maintains a refcount and is deleted when there are no
543 // more RenderFrameHosts or RenderFrameProxyHosts using it.
Aaron Colwell78b4bde2021-03-16 16:16:09544 RenderViewHostMap render_view_host_map_;
danakjc492bf82020-09-09 20:02:44545
Harkiran Bolaria16f2c48d2022-04-22 12:39:57546 // Indicates type of frame tree.
547 const Type type_;
548
danakjc492bf82020-09-09 20:02:44549 // This is an owned ptr to the root FrameTreeNode, which never changes over
550 // the lifetime of the FrameTree. It is not a scoped_ptr because we need the
551 // pointer to remain valid even while the FrameTreeNode is being destroyed,
552 // since it's common for a node to test whether it's the root node.
Keishi Hattori0e45c022021-11-27 09:25:52553 raw_ptr<FrameTreeNode> root_;
danakjc492bf82020-09-09 20:02:44554
555 int focused_frame_tree_node_id_;
556
danakjc492bf82020-09-09 20:02:44557 // Overall load progress.
558 double load_progress_;
559
Carlos Caballeroede6f8c2021-01-28 11:01:50560 // Whether the initial empty page has been accessed by another page, making it
561 // unsafe to show the pending URL. Usually false unless another window tries
562 // to modify the blank page. Always false after the first commit.
563 bool has_accessed_initial_main_document_ = false;
564
Takashi Toyoshimaea534ef22021-07-21 03:27:59565 bool is_being_destroyed_ = false;
566
Carlos Caballero101ac26b2021-03-24 11:54:05567#if DCHECK_IS_ON()
568 // Whether Shutdown() was called.
569 bool was_shut_down_ = false;
570#endif
Dave Tapuskad8b0530f2021-10-19 15:12:31571
572 base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this};
danakjc492bf82020-09-09 20:02:44573};
574
575} // namespace content
576
577#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_