Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 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 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 15 | #include "base/containers/queue.h" |
Carlos Caballero | 101ac26b | 2021-03-24 11:54:05 | [diff] [blame] | 16 | #include "base/dcheck_is_on.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 17 | #include "base/functional/callback.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 18 | #include "base/gtest_prod_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 19 | #include "base/memory/raw_ptr.h" |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 20 | #include "base/memory/safe_ref.h" |
| 21 | #include "base/memory/weak_ptr.h" |
Paul Semel | 3e24104 | 2022-10-11 12:57:31 | [diff] [blame] | 22 | #include "content/browser/renderer_host/frame_tree_node.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 23 | #include "content/browser/renderer_host/navigator.h" |
| 24 | #include "content/browser/renderer_host/navigator_delegate.h" |
| 25 | #include "content/browser/renderer_host/render_frame_host_manager.h" |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 26 | #include "content/browser/renderer_host/render_view_host_enums.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 27 | #include "content/common/content_export.h" |
| 28 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 29 | #include "services/service_manager/public/mojom/interface_provider.mojom.h" |
David Bokan | c3fb5fa | 2022-07-04 14:55:31 | [diff] [blame] | 30 | #include "third_party/blink/public/common/features.h" |
Kevin McNee | 43fe829 | 2021-10-04 22:59:41 | [diff] [blame] | 31 | #include "third_party/blink/public/common/frame/frame_owner_element_type.h" |
Chris Hamilton | 3ff6ed0e | 2021-02-19 03:54:04 | [diff] [blame] | 32 | #include "third_party/blink/public/common/tokens/tokens.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 33 | #include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom-forward.h" |
Ari Chivukula | 5d15efb | 2023-01-21 04:33:52 | [diff] [blame] | 34 | #include "url/origin.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 35 | |
| 36 | namespace blink { |
Lei Zhang | 4a86767 | 2021-07-19 06:30:14 | [diff] [blame] | 37 | namespace mojom { |
| 38 | class BrowserInterfaceBroker; |
| 39 | enum class TreeScopeType; |
| 40 | } // namespace mojom |
| 41 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 42 | struct FramePolicy; |
| 43 | } // namespace blink |
| 44 | |
| 45 | namespace content { |
| 46 | |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 47 | class BrowserContext; |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 48 | class PageDelegate; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 49 | class RenderFrameHostDelegate; |
Dave Tapuska | 06a3361d | 2025-01-13 22:09:43 | [diff] [blame] | 50 | class RenderFrameProxyHost; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 51 | class RenderViewHostDelegate; |
| 52 | class RenderViewHostImpl; |
| 53 | class RenderFrameHostManager; |
| 54 | class RenderWidgetHostDelegate; |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 55 | class SiteInstance; |
Sharon Yang | 57bde12 | 2022-03-01 20:01:12 | [diff] [blame] | 56 | class SiteInstanceGroup; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 57 | |
| 58 | // Represents the frame tree for a page. With the exception of the main frame, |
| 59 | // all FrameTreeNodes will be created/deleted in response to frame attach and |
| 60 | // detach events in the DOM. |
| 61 | // |
| 62 | // The main frame's FrameTreeNode is special in that it is reused. This allows |
| 63 | // it to serve as an anchor for state that needs to persist across top-level |
| 64 | // page navigations. |
| 65 | // |
| 66 | // TODO(ajwong): Move NavigationController ownership to the main frame |
| 67 | // FrameTreeNode. Possibly expose access to it from here. |
| 68 | // |
| 69 | // This object is only used on the UI thread. |
| 70 | class CONTENT_EXPORT FrameTree { |
| 71 | public: |
| 72 | class NodeRange; |
| 73 | |
Alan Zhao | 8a882d1 | 2022-05-18 01:32:15 | [diff] [blame] | 74 | class CONTENT_EXPORT NodeIterator { |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 75 | public: |
Alan Zhao | 8a882d1 | 2022-05-18 01:32:15 | [diff] [blame] | 76 | using iterator_category = std::forward_iterator_tag; |
| 77 | using value_type = FrameTreeNode*; |
| 78 | using difference_type = std::ptrdiff_t; |
| 79 | using pointer = FrameTreeNode**; |
| 80 | using reference = FrameTreeNode*&; |
| 81 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 82 | NodeIterator(const NodeIterator& other); |
| 83 | ~NodeIterator(); |
| 84 | |
| 85 | NodeIterator& operator++(); |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 86 | // Advances the iterator and excludes the children of the current node |
| 87 | NodeIterator& AdvanceSkippingChildren(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 88 | |
| 89 | bool operator==(const NodeIterator& rhs) const; |
| 90 | bool operator!=(const NodeIterator& rhs) const { return !(*this == rhs); } |
| 91 | |
| 92 | FrameTreeNode* operator*() { return current_node_; } |
| 93 | |
| 94 | private: |
Jayson Adams | 4db0bfe2 | 2021-07-15 19:24:07 | [diff] [blame] | 95 | friend class FrameTreeTest; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 96 | friend class NodeRange; |
| 97 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 98 | NodeIterator(const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>& |
| 99 | starting_nodes, |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 100 | const FrameTreeNode* root_of_subtree_to_skip, |
Dave Tapuska | dda303d | 2022-10-04 16:56:48 | [diff] [blame] | 101 | bool should_descend_into_inner_trees, |
| 102 | bool include_delegate_nodes_for_inner_frame_trees); |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 103 | |
| 104 | void AdvanceNode(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 105 | |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 106 | // `current_node_` and `root_of_subtree_to_skip_` are not a raw_ptr<...> for |
| 107 | // performance reasons (based on analysis of sampling profiler data and |
| 108 | // tab_search:top100:2020). |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 109 | RAW_PTR_EXCLUSION FrameTreeNode* current_node_; |
| 110 | RAW_PTR_EXCLUSION const FrameTreeNode* const root_of_subtree_to_skip_; |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 111 | |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 112 | const bool should_descend_into_inner_trees_; |
Dave Tapuska | dda303d | 2022-10-04 16:56:48 | [diff] [blame] | 113 | const bool include_delegate_nodes_for_inner_frame_trees_; |
Jayson Adams | 4db0bfe2 | 2021-07-15 19:24:07 | [diff] [blame] | 114 | base::circular_deque<FrameTreeNode*> queue_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class CONTENT_EXPORT NodeRange { |
| 118 | public: |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 119 | NodeRange(const NodeRange&); |
| 120 | ~NodeRange(); |
| 121 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 122 | NodeIterator begin(); |
| 123 | NodeIterator end(); |
| 124 | |
| 125 | private: |
| 126 | friend class FrameTree; |
| 127 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 128 | NodeRange(const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>>& |
| 129 | starting_nodes, |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 130 | const FrameTreeNode* root_of_subtree_to_skip, |
Dave Tapuska | dda303d | 2022-10-04 16:56:48 | [diff] [blame] | 131 | bool should_descend_into_inner_trees, |
| 132 | bool include_delegate_nodes_for_inner_frame_trees); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 133 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 134 | const std::vector<raw_ptr<FrameTreeNode, VectorExperimental>> |
| 135 | starting_nodes_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 136 | const raw_ptr<const FrameTreeNode> root_of_subtree_to_skip_; |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 137 | const bool should_descend_into_inner_trees_; |
Dave Tapuska | dda303d | 2022-10-04 16:56:48 | [diff] [blame] | 138 | const bool include_delegate_nodes_for_inner_frame_trees_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 139 | }; |
| 140 | |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 141 | class CONTENT_EXPORT Delegate { |
| 142 | public: |
Nate Chapin | 470dbc6 | 2023-04-25 16:34:38 | [diff] [blame] | 143 | // The FrameTree changed its LoadingState. This can be a transition between |
| 144 | // not-loading and loading (in which case it will be accompanied by either a |
| 145 | // DidStartLoading or DidStopLoading), or a transition between not showing |
| 146 | // loading UI and showing loading UI while a navigation is in progress (in |
| 147 | // which case it will be called without either DidStartLoading or |
| 148 | // DidStopLoading). |
| 149 | virtual void LoadingStateChanged(LoadingState new_state) = 0; |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 150 | |
Nate Chapin | 470dbc6 | 2023-04-25 16:34:38 | [diff] [blame] | 151 | // The FrameTree has started loading in `frame_tree_node`. Note that this |
| 152 | // is only called when the FrameTree as a whole goes from not-loading to |
| 153 | // loading. If a second FrameTreeNode begins loading, a new DidStartLoading |
| 154 | // message will not be sent. |
| 155 | virtual void DidStartLoading(FrameTreeNode* frame_tree_node) = 0; |
| 156 | |
| 157 | // The FrameTree has stopped loading. Sent only when all FrameTreeNodes have |
| 158 | // stopped loading. |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 159 | virtual void DidStopLoading() = 0; |
| 160 | |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 161 | // Returns the delegate's top loading tree, which should be used to infer |
Yu Gao | c8c1855 | 2022-06-22 14:38:45 | [diff] [blame] | 162 | // the values of loading-related states. The state of |
| 163 | // IsLoadingIncludingInnerFrameTrees() is a WebContents level concept and |
| 164 | // LoadingTree would return the frame tree to which loading events should be |
| 165 | // directed. |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 166 | // |
Alison Gale | 81f4f2c | 2024-04-22 19:33:31 | [diff] [blame] | 167 | // TODO(crbug.com/40202416): Remove this method and directly rely on |
Jeremy Roman | c0c69be | 2023-11-21 19:14:52 | [diff] [blame] | 168 | // GetOutermostMainFrame() once guest views are migrated to MPArch. |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 169 | virtual FrameTree* LoadingTree() = 0; |
| 170 | |
Carlos Caballero | 6ff6ace | 2021-02-05 16:53:00 | [diff] [blame] | 171 | // Returns true when the active RenderWidgetHostView should be hidden. |
| 172 | virtual bool IsHidden() = 0; |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 173 | |
Dominic Farolino | edf44ee | 2021-07-20 23:50:59 | [diff] [blame] | 174 | // If the FrameTree using this delegate is an inner/nested FrameTree, then |
| 175 | // there may be a FrameTreeNode in the outer FrameTree that is considered |
| 176 | // our outer delegate FrameTreeNode. This method returns the outer delegate |
| 177 | // FrameTreeNode ID if one exists. If we don't have a an outer delegate |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 178 | // FrameTreeNode, this method returns an invalid value. |
| 179 | virtual FrameTreeNodeId GetOuterDelegateFrameTreeNodeId() = 0; |
Dave Tapuska | c8de3b0 | 2021-12-03 21:51:01 | [diff] [blame] | 180 | |
Kevin McNee | 86e64ee | 2023-02-17 16:35:50 | [diff] [blame] | 181 | // If the FrameTree using this delegate is an inner/nested FrameTree that |
| 182 | // has not yet been attached to an outer FrameTreeNode, returns the parent |
| 183 | // RenderFrameHost of the intended outer FrameTreeNode to which the inner |
| 184 | // frame tree will be attached. This is usually the RenderFrameHost that is |
| 185 | // the outer document once attachment occurs, however in the case of some |
| 186 | // kinds of GuestView, the outer document may end up being a same-origin |
| 187 | // subframe of the RenderFrameHost returned by this method (see the |
| 188 | // `testNewWindowAttachInSubFrame` webview test for an example of this). |
| 189 | // Otherwise, returns null. |
| 190 | virtual RenderFrameHostImpl* GetProspectiveOuterDocument() = 0; |
| 191 | |
Julie Jeongeun Kim | 2132b37f8 | 2022-11-23 08:30:46 | [diff] [blame] | 192 | // Set the `node` frame as focused in its own FrameTree as well as possibly |
| 193 | // changing the focused frame tree in the case of inner/outer FrameTrees. |
| 194 | virtual void SetFocusedFrame(FrameTreeNode* node, |
| 195 | SiteInstanceGroup* source) = 0; |
Tommy Steimel | 71f15446 | 2024-05-22 19:05:07 | [diff] [blame] | 196 | |
| 197 | // Returns this FrameTree's picture-in-picture FrameTree if it has one. |
| 198 | virtual FrameTree* GetOwnedPictureInPictureFrameTree() = 0; |
| 199 | |
| 200 | // Returns this FrameTree's opener if this FrameTree represents a |
| 201 | // picture-in-picture window. |
| 202 | virtual FrameTree* GetPictureInPictureOpenerFrameTree() = 0; |
Dave Tapuska | 06a3361d | 2025-01-13 22:09:43 | [diff] [blame] | 203 | |
| 204 | // Called when the visibility of the RenderFrameProxyHost changes. |
| 205 | // This method should only handle visibility for inner WebContents and |
| 206 | // will eventually notify all the RenderWidgetHostViews belonging to that |
| 207 | // WebContents. If this is not an inner WebContents or the inner WebContents |
| 208 | // FrameTree root does not match `render_frame_proxy_host` FrameTreeNode it |
| 209 | // should return false. |
| 210 | virtual bool OnRenderFrameProxyVisibilityChanged( |
| 211 | RenderFrameProxyHost* render_frame_proxy_host, |
| 212 | blink::mojom::FrameVisibility visibility) = 0; |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 213 | }; |
| 214 | |
Sreeja Kamishetty | 74bacd52 | 2021-03-22 17:04:24 | [diff] [blame] | 215 | // Type of FrameTree instance. |
| 216 | enum class Type { |
| 217 | // This FrameTree is the primary frame tree for the WebContents, whose main |
| 218 | // document URL is shown in the Omnibox. |
| 219 | kPrimary, |
| 220 | |
| 221 | // This FrameTree is used to prerender a page in the background which is |
| 222 | // invisible to the user. |
Dominic Farolino | 4bc10ee | 2021-08-31 00:37:36 | [diff] [blame] | 223 | kPrerender, |
| 224 | |
| 225 | // This FrameTree is used to host the contents of a <fencedframe> element. |
Dominic Farolino | 4bc10ee | 2021-08-31 00:37:36 | [diff] [blame] | 226 | // |
Kevin McNee | f1b0f0b | 2024-09-17 21:49:41 | [diff] [blame] | 227 | // Note that the FrameTree's Type should not be confused for |
| 228 | // `RenderFrameHost::LifecycleState`. For example, when a <fencedframe> is |
| 229 | // nested in a page in the bfcache, the FrameTree associated with the fenced |
| 230 | // frame will be kFencedFrame, but the RenderFrameHosts inside of it will |
| 231 | // have their lifecycle state indicate that they are bfcached. |
| 232 | kFencedFrame, |
Kevin McNee | 2585e73 | 2024-10-28 22:11:19 | [diff] [blame] | 233 | |
| 234 | // This FrameTree is used to host the contents of a guest page. Guests are |
| 235 | // kinds of embedded pages, but their semantics are mostly delegated outside |
| 236 | // of the content/ layer. See components/guest_view/README.md. |
| 237 | // The implementation of guests is being migrated from using a separate |
| 238 | // WebContents to using this FrameTree Type. This type is used with the |
| 239 | // `features::kGuestViewMPArch` flag. |
| 240 | kGuest, |
Sreeja Kamishetty | 74bacd52 | 2021-03-22 17:04:24 | [diff] [blame] | 241 | }; |
Sreeja Kamishetty | 837a1040 | 2021-04-23 12:41:59 | [diff] [blame] | 242 | |
| 243 | // A set of delegates are remembered here so that we can create |
| 244 | // RenderFrameHostManagers. |
| 245 | FrameTree(BrowserContext* browser_context, |
| 246 | Delegate* delegate, |
| 247 | NavigationControllerDelegate* navigation_controller_delegate, |
| 248 | NavigatorDelegate* navigator_delegate, |
| 249 | RenderFrameHostDelegate* render_frame_delegate, |
| 250 | RenderViewHostDelegate* render_view_delegate, |
| 251 | RenderWidgetHostDelegate* render_widget_delegate, |
| 252 | RenderFrameHostManager::Delegate* manager_delegate, |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 253 | PageDelegate* page_delegate, |
Danil Somsikov | 259aa65f | 2022-11-11 20:49:44 | [diff] [blame] | 254 | Type type); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 255 | |
| 256 | FrameTree(const FrameTree&) = delete; |
| 257 | FrameTree& operator=(const FrameTree&) = delete; |
| 258 | |
Sreeja Kamishetty | 837a1040 | 2021-04-23 12:41:59 | [diff] [blame] | 259 | ~FrameTree(); |
Sreeja Kamishetty | 74bacd52 | 2021-03-22 17:04:24 | [diff] [blame] | 260 | |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 261 | // Initializes the main frame for this FrameTree. That is it creates the |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 262 | // initial RenderFrameHost in the root node's RenderFrameHostManager, and also |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 263 | // creates an initial NavigationEntry that potentially inherits |
| 264 | // `opener_for_origin`'s origin in its NavigationController. This method will |
| 265 | // call back into the delegates so it should only be called once they have |
| 266 | // completed their initialization. Pass in frame_policy so that it can be set |
| 267 | // in the root node's replication_state. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 268 | // TODO(carlscab): It would be great if initialization could happened in the |
| 269 | // constructor so we do not leave objects in a half initialized state. |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 270 | void Init(SiteInstanceImpl* main_frame_site_instance, |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 271 | bool renderer_initiated_creation, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 272 | const std::string& main_frame_name, |
Rakina Zata Amni | 4eb716e | 2022-04-05 21:32:46 | [diff] [blame] | 273 | RenderFrameHostImpl* opener_for_origin, |
Danil Somsikov | 259aa65f | 2022-11-11 20:49:44 | [diff] [blame] | 274 | const blink::FramePolicy& frame_policy, |
| 275 | const base::UnguessableToken& devtools_frame_token); |
Sreeja Kamishetty | 837a1040 | 2021-04-23 12:41:59 | [diff] [blame] | 276 | |
| 277 | Type type() const { return type_; } |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 278 | |
Paul Semel | 3e24104 | 2022-10-11 12:57:31 | [diff] [blame] | 279 | FrameTreeNode* root() { return &root_; } |
| 280 | const FrameTreeNode* root() const { return &root_; } |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 281 | |
Takashi Toyoshima | 7c041d8 | 2023-09-26 16:09:21 | [diff] [blame] | 282 | bool is_primary() const { return type_ == Type::kPrimary; } |
| 283 | bool is_prerendering() const { return type_ == Type::kPrerender; } |
| 284 | bool is_fenced_frame() const { return type_ == Type::kFencedFrame; } |
Kevin McNee | 2585e73 | 2024-10-28 22:11:19 | [diff] [blame] | 285 | bool is_guest() const { return type_ == Type::kGuest; } |
Sreeja Kamishetty | 46f762c | 2021-02-05 07:52:46 | [diff] [blame] | 286 | |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 287 | Delegate* delegate() { return delegate_; } |
| 288 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 289 | // Delegates for various objects. These can be kept centrally on the |
| 290 | // FrameTree because they are expected to be the same for all frames on a |
| 291 | // given FrameTree. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 292 | RenderFrameHostDelegate* render_frame_delegate() { |
| 293 | return render_frame_delegate_; |
| 294 | } |
| 295 | RenderViewHostDelegate* render_view_delegate() { |
| 296 | return render_view_delegate_; |
| 297 | } |
| 298 | RenderWidgetHostDelegate* render_widget_delegate() { |
| 299 | return render_widget_delegate_; |
| 300 | } |
| 301 | RenderFrameHostManager::Delegate* manager_delegate() { |
| 302 | return manager_delegate_; |
| 303 | } |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 304 | PageDelegate* page_delegate() { return page_delegate_; } |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 305 | |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 306 | // Iterate over all RenderViewHosts, including speculative RenderViewHosts. |
| 307 | // See `speculative_render_view_host_` for more details. |
| 308 | void ForEachRenderViewHost( |
| 309 | base::FunctionRef<void(RenderViewHostImpl*)> on_host); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 310 | |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 311 | // Speculative RenderViewHost accessors. |
| 312 | RenderViewHostImpl* speculative_render_view_host() const { |
| 313 | return speculative_render_view_host_.get(); |
| 314 | } |
| 315 | void set_speculative_render_view_host( |
| 316 | base::WeakPtr<RenderViewHostImpl> render_view_host) { |
| 317 | speculative_render_view_host_ = render_view_host; |
| 318 | } |
| 319 | |
| 320 | // Moves `speculative_render_view_host_` to `render_view_host_map_`. This |
| 321 | // should be called every time a main-frame same-SiteInstanceGroup speculative |
| 322 | // RenderFrameHost gets swapped in and becomes the active RenderFrameHost. |
| 323 | // This overwrites the previous RenderViewHost for the SiteInstanceGroup in |
| 324 | // `render_view_host_map_`, if one exists. |
| 325 | void MakeSpeculativeRVHCurrent(); |
| 326 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 327 | // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part |
| 328 | // of this FrameTree. |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 329 | FrameTreeNode* FindByID(FrameTreeNodeId frame_tree_node_id); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 330 | |
| 331 | // Returns the FrameTreeNode with the given renderer-specific |routing_id|. |
| 332 | FrameTreeNode* FindByRoutingID(int process_id, int routing_id); |
| 333 | |
| 334 | // Returns the first frame in this tree with the given |name|, or the main |
| 335 | // frame if |name| is empty. |
| 336 | // Note that this does NOT support pseudo-names like _self, _top, and _blank, |
| 337 | // nor searching other FrameTrees (unlike blink::WebView::findFrameByName). |
| 338 | FrameTreeNode* FindByName(const std::string& name); |
| 339 | |
| 340 | // Returns a range to iterate over all FrameTreeNodes in the frame tree in |
| 341 | // breadth-first traversal order. |
| 342 | NodeRange Nodes(); |
| 343 | |
| 344 | // Returns a range to iterate over all FrameTreeNodes in a subtree of the |
| 345 | // frame tree, starting from |subtree_root|. |
| 346 | NodeRange SubtreeNodes(FrameTreeNode* subtree_root); |
| 347 | |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 348 | // Returns a range to iterate over all FrameTreeNodes in this frame tree, as |
| 349 | // well as any FrameTreeNodes of inner frame trees. Note that this includes |
| 350 | // inner frame trees of inner WebContents as well. |
| 351 | NodeRange NodesIncludingInnerTreeNodes(); |
| 352 | |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 353 | // Returns a range to iterate over all FrameTreeNodes in a subtree, starting |
| 354 | // from, but not including |parent|, as well as any FrameTreeNodes of inner |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 355 | // frame trees. Note that this includes inner frame trees of inner WebContents |
Dave Tapuska | dda303d | 2022-10-04 16:56:48 | [diff] [blame] | 356 | // as well. If `include_delegate_nodes_for_inner_frame_trees` is true the |
| 357 | // delegate RenderFrameHost owning the inner frame tree will also be returned. |
| 358 | static NodeRange SubtreeAndInnerTreeNodes( |
| 359 | RenderFrameHostImpl* parent, |
| 360 | bool include_delegate_nodes_for_inner_frame_trees = false); |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 361 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 362 | // Adds a new child frame to the frame tree. |process_id| is required to |
| 363 | // disambiguate |new_routing_id|, and it must match the process of the |
Kevin McNee | 43fe829 | 2021-10-04 22:59:41 | [diff] [blame] | 364 | // |parent| node. Otherwise no child is added and this method returns nullptr. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 365 | // |interface_provider_receiver| is the receiver end of the InterfaceProvider |
| 366 | // interface through which the child RenderFrame can access Mojo services |
| 367 | // exposed by the corresponding RenderFrameHost. The caller takes care of |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 368 | // sending the client end of the interface down to the |
| 369 | // RenderFrame. |policy_container_bind_params|, if not null, is used for |
| 370 | // binding Blink's policy container to the new RenderFrameHost's |
| 371 | // PolicyContainerHost. This is only needed if this frame is the result of the |
| 372 | // CreateChildFrame mojo call, which also delivers the |
Kevin McNee | 43fe829 | 2021-10-04 22:59:41 | [diff] [blame] | 373 | // |policy_container_bind_params|. |is_dummy_frame_for_inner_tree| is true if |
| 374 | // the added frame is only to serve as a placeholder for an inner frame tree |
Jeremy Roman | c0c69be | 2023-11-21 19:14:52 | [diff] [blame] | 375 | // (e.g. fenced frames) and will not have a live RenderFrame of its |
Kevin McNee | 43fe829 | 2021-10-04 22:59:41 | [diff] [blame] | 376 | // own. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 377 | FrameTreeNode* AddFrame( |
| 378 | RenderFrameHostImpl* parent, |
| 379 | int process_id, |
| 380 | int new_routing_id, |
danakj | 0bdfacd | 2021-01-20 19:27:18 | [diff] [blame] | 381 | mojo::PendingAssociatedRemote<mojom::Frame> frame_remote, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 382 | mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker> |
| 383 | browser_interface_broker_receiver, |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 384 | blink::mojom::PolicyContainerBindParamsPtr policy_container_bind_params, |
Dominic Farolino | 12e06d7 | 2022-08-05 02:29:49 | [diff] [blame] | 385 | mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider> |
| 386 | associated_interface_provider_receiver, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 387 | blink::mojom::TreeScopeType scope, |
| 388 | const std::string& frame_name, |
| 389 | const std::string& frame_unique_name, |
| 390 | bool is_created_by_script, |
Chris Hamilton | 3ff6ed0e | 2021-02-19 03:54:04 | [diff] [blame] | 391 | const blink::LocalFrameToken& frame_token, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 392 | const base::UnguessableToken& devtools_frame_token, |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 393 | const blink::DocumentToken& document_token, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 394 | const blink::FramePolicy& frame_policy, |
| 395 | const blink::mojom::FrameOwnerProperties& frame_owner_properties, |
| 396 | bool was_discarded, |
Kevin McNee | 43fe829 | 2021-10-04 22:59:41 | [diff] [blame] | 397 | blink::FrameOwnerElementType owner_type, |
| 398 | bool is_dummy_frame_for_inner_tree); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 399 | |
| 400 | // Removes a frame from the frame tree. |child|, its children, and objects |
| 401 | // owned by their RenderFrameHostManagers are immediately deleted. The root |
| 402 | // node cannot be removed this way. |
| 403 | void RemoveFrame(FrameTreeNode* child); |
| 404 | |
Sharon Yang | dcc5f25 | 2024-05-09 18:27:23 | [diff] [blame] | 405 | // This method walks the entire frame tree and creates RenderFrameProxyHosts |
| 406 | // as needed. Proxies are not created if suitable proxies already exist. See |
| 407 | // below for special handling of |source|. Otherwise proxies are created for |
| 408 | // the given |site_instance_group| in each node. |
| 409 | // |
| 410 | // |source| may be null if there is no node navigating in this frame tree |
| 411 | // (such as when this is called for an opener's frame tree), in which case no |
| 412 | // nodes are skipped for RenderFrameProxyHost creation. Otherwise, a proxy is |
| 413 | // temporarily created for |source| in cross-SiteInstanceGroup cases (to allow |
| 414 | // a remote-to-local swap to the new RenderFrameHost in |source|), but the |
| 415 | // subtree rooted at source is skipped. |
| 416 | // |source_new_browsing_context_state| is the BrowsingContextState used by the |
| 417 | // speculative frame host, which may differ from the BrowsingContextState in |
| 418 | // |source| during cross-origin cross- browsing-instance navigations. |
| 419 | void CreateProxiesForSiteInstanceGroup( |
| 420 | FrameTreeNode* source, |
| 421 | SiteInstanceGroup* site_instance_group, |
| 422 | const scoped_refptr<BrowsingContextState>& |
| 423 | source_new_browsing_context_state); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 424 | |
| 425 | // Convenience accessor for the main frame's RenderFrameHostImpl. |
| 426 | RenderFrameHostImpl* GetMainFrame() const; |
| 427 | |
| 428 | // Returns the focused frame. |
| 429 | FrameTreeNode* GetFocusedFrame(); |
| 430 | |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 431 | // Sets the focused frame to |node|. |source| identifies the |
| 432 | // SiteInstanceGroup that initiated this focus change. If this FrameTree has |
| 433 | // SiteInstanceGroups other than |source|, those SiteInstanceGroups will be |
| 434 | // notified about the new focused frame. Note that |source| may differ from |
| 435 | // |node|'s current SiteInstanceGroup (e.g., this happens for cross-process |
| 436 | // window.focus() calls). |
| 437 | void SetFocusedFrame(FrameTreeNode* node, SiteInstanceGroup* source); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 438 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 439 | // Creates a RenderViewHostImpl for a given |site_instance| in the tree. |
| 440 | // |
| 441 | // The RenderFrameHostImpls and the RenderFrameProxyHosts will share ownership |
| 442 | // of this object. |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 443 | // `create_case` indicates whether or not the RenderViewHost being created is |
| 444 | // speculative or not. It should only be registered with the FrameTree if it |
| 445 | // is not speculative. |
Khushal Sagar | 402d1c9f | 2023-11-15 22:21:48 | [diff] [blame] | 446 | // `frame_sink_id` is optionally set only if we're creating a speculative |
| 447 | // RenderViewHost. If set, it implies we're reusing the compositor from the |
| 448 | // previous RenderViewHost. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 449 | scoped_refptr<RenderViewHostImpl> CreateRenderViewHost( |
Sharon Yang | eb76ee2 | 2023-11-29 00:42:09 | [diff] [blame] | 450 | SiteInstanceGroup* site_instance_group, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 451 | int32_t main_frame_routing_id, |
Harkiran Bolaria | 57e2b06 | 2022-03-14 10:27:58 | [diff] [blame] | 452 | bool renderer_initiated_creation, |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 453 | scoped_refptr<BrowsingContextState> main_browsing_context_state, |
Khushal Sagar | 402d1c9f | 2023-11-15 22:21:48 | [diff] [blame] | 454 | CreateRenderViewHostCase create_case, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 455 | std::optional<viz::FrameSinkId> frame_sink_id); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 456 | |
| 457 | // Returns the existing RenderViewHost for a new RenderFrameHost. |
| 458 | // There should always be such a RenderViewHost, because the main frame |
| 459 | // RenderFrameHost for each SiteInstance should be created before subframes. |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 460 | // Note that this will never return `speculative_render_view_host_`. If that |
| 461 | // is needed, call `speculative_render_view_host()` instead. |
Sharon Yang | 57bde12 | 2022-03-01 20:01:12 | [diff] [blame] | 462 | scoped_refptr<RenderViewHostImpl> GetRenderViewHost(SiteInstanceGroup* group); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 463 | |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 464 | using RenderViewHostMapId = base::IdType32<class RenderViewHostMap>; |
| 465 | |
Sharon Yang | c581a0c | 2021-11-02 18:09:39 | [diff] [blame] | 466 | // Returns the ID used for the RenderViewHost associated with |
| 467 | // |site_instance_group|. |
| 468 | RenderViewHostMapId GetRenderViewHostMapId( |
| 469 | SiteInstanceGroup* site_instance_group) const; |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 470 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 471 | // Registers a RenderViewHost so that it can be reused by other frames |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 472 | // whose SiteInstance maps to the same RenderViewHostMapId. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 473 | // |
| 474 | // This method does not take ownership of|rvh|. |
| 475 | // |
| 476 | // NOTE: This method CHECK fails if a RenderViewHost is already registered for |
| 477 | // |rvh|'s SiteInstance. |
| 478 | // |
| 479 | // ALSO NOTE: After calling RegisterRenderViewHost, UnregisterRenderViewHost |
| 480 | // *must* be called for |rvh| when it is destroyed or put into the |
| 481 | // BackForwardCache, to prevent FrameTree::CreateRenderViewHost from trying to |
| 482 | // reuse it. |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 483 | void RegisterRenderViewHost(RenderViewHostMapId id, RenderViewHostImpl* rvh); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 484 | |
| 485 | // Unregisters the RenderViewHostImpl that's available for reuse for a |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 486 | // particular RenderViewHostMapId. NOTE: This method CHECK fails if it is |
| 487 | // called for a |render_view_host| that is not currently set for reuse. |
| 488 | void UnregisterRenderViewHost(RenderViewHostMapId id, |
Aaron Colwell | c4bd7d6 | 2021-01-29 04:23:13 | [diff] [blame] | 489 | RenderViewHostImpl* render_view_host); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 490 | |
| 491 | // This is called when the frame is about to be removed and started to run |
| 492 | // unload handlers. |
| 493 | void FrameUnloading(FrameTreeNode* frame); |
| 494 | |
| 495 | // This is only meant to be called by FrameTreeNode. Triggers calling |
| 496 | // the listener installed by SetFrameRemoveListener. |
| 497 | void FrameRemoved(FrameTreeNode* frame); |
| 498 | |
Nate Chapin | 470dbc6 | 2023-04-25 16:34:38 | [diff] [blame] | 499 | void NodeLoadingStateChanged(FrameTreeNode& node, |
| 500 | LoadingState previous_frame_tree_loading_state); |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 501 | void DidCancelLoading(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 502 | |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 503 | // Returns this FrameTree's total load progress. If the `root_` FrameTreeNode |
| 504 | // is navigating returns `blink::kInitialLoadProgress`. |
| 505 | double GetLoadProgress(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 506 | |
Sreeja Kamishetty | 15f9944a2 | 2022-03-10 10:16:08 | [diff] [blame] | 507 | // Returns true if at least one of the nodes in this frame tree or nodes in |
| 508 | // any inner frame tree of the same WebContents is loading. |
Yu Gao | c8c1855 | 2022-06-22 14:38:45 | [diff] [blame] | 509 | bool IsLoadingIncludingInnerFrameTrees() const; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 510 | |
Nate Chapin | 470dbc6 | 2023-04-25 16:34:38 | [diff] [blame] | 511 | // Returns the LoadingState for the FrameTree as a whole, indicating whether |
| 512 | // a load is in progress, as well as whether loading UI should be shown. |
| 513 | LoadingState GetLoadingState() const; |
| 514 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 515 | // Set page-level focus in all SiteInstances involved in rendering |
| 516 | // this FrameTree, not including the current main frame's |
| 517 | // SiteInstance. The focus update will be sent via the main frame's proxies |
| 518 | // in those SiteInstances. |
| 519 | void ReplicatePageFocus(bool is_focused); |
| 520 | |
| 521 | // Updates page-level focus for this FrameTree in the subframe renderer |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 522 | // identified by |group|. |
| 523 | void SetPageFocus(SiteInstanceGroup* group, bool is_focused); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 524 | |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 525 | // Walks the current frame tree and registers any origins matching |
| 526 | // `previously_visited_origin`, either the last committed origin of a |
| 527 | // RenderFrameHost or the origin associated with a NavigationRequest that has |
| 528 | // been assigned to a SiteInstance, as having the default origin isolation |
| 529 | // state. This is only necessary when `previously_visited_origin` is seen with |
| 530 | // an OriginAgentCluster header explicitly requesting something other than the |
| 531 | // default. |
| 532 | void RegisterExistingOriginAsHavingDefaultIsolation( |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 533 | const url::Origin& previously_visited_origin, |
| 534 | NavigationRequest* navigation_request_to_exclude); |
| 535 | |
Elad Alon | 32044f53 | 2025-03-04 22:16:03 | [diff] [blame] | 536 | const NavigationControllerImpl& controller() const { |
| 537 | return navigator_.controller(); |
| 538 | } |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 539 | NavigationControllerImpl& controller() { return navigator_.controller(); } |
Elad Alon | 32044f53 | 2025-03-04 22:16:03 | [diff] [blame] | 540 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 541 | Navigator& navigator() { return navigator_; } |
| 542 | |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 543 | // Another page accessed the initial empty main document, which means it |
| 544 | // is no longer safe to display a pending URL without risking a URL spoof. |
| 545 | void DidAccessInitialMainDocument(); |
| 546 | |
| 547 | bool has_accessed_initial_main_document() const { |
| 548 | return has_accessed_initial_main_document_; |
| 549 | } |
| 550 | |
| 551 | void ResetHasAccessedInitialMainDocument() { |
| 552 | has_accessed_initial_main_document_ = false; |
| 553 | } |
| 554 | |
Carlos Caballero | 6ff6ace | 2021-02-05 16:53:00 | [diff] [blame] | 555 | bool IsHidden() const { return delegate_->IsHidden(); } |
| 556 | |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 557 | // LoadingTree returns the following for different frame trees to direct |
| 558 | // loading related events. Please see FrameTree::Delegate::LoadingTree for |
| 559 | // more comments. |
| 560 | // - For prerender frame tree -> returns the frame tree itself. |
Jeremy Roman | c0c69be | 2023-11-21 19:14:52 | [diff] [blame] | 561 | // - For fenced frame and primary frame tree -> returns |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 562 | // the delegate's primary frame tree. |
| 563 | FrameTree* LoadingTree(); |
| 564 | |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 565 | // Stops all ongoing navigations in each of the nodes of this FrameTree. |
| 566 | void StopLoading(); |
| 567 | |
Carlos Caballero | 101ac26b | 2021-03-24 11:54:05 | [diff] [blame] | 568 | // Prepares this frame tree for destruction, cleaning up the internal state |
| 569 | // and firing the appropriate events like FrameDeleted. |
| 570 | // Must be called before FrameTree is destroyed. |
| 571 | void Shutdown(); |
| 572 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 573 | bool IsBeingDestroyed() const { return is_being_destroyed_; } |
| 574 | |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 575 | base::SafeRef<FrameTree> GetSafeRef(); |
| 576 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 577 | // Walks up the FrameTree chain and focuses the FrameTreeNode where |
| 578 | // each inner FrameTree is attached. |
| 579 | void FocusOuterFrameTrees(); |
| 580 | |
Thomas Lukaszewicz | 8ada31b | 2024-09-01 19:18:16 | [diff] [blame] | 581 | // Discards the frame tree. The root frame is transitioned to an empty |
| 582 | // document in blink and BFCache entries are cleared. The tree is configured |
| 583 | // to reload when activated. |
| 584 | void Discard(); |
| 585 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 586 | private: |
| 587 | friend class FrameTreeTest; |
| 588 | FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest, RemoveFocusedFrame); |
Xiaochen Zhou | 6443f75 | 2022-08-10 16:40:46 | [diff] [blame] | 589 | FRIEND_TEST_ALL_PREFIXES(FencedFrameMPArchBrowserTest, NodesForIsLoading); |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 590 | FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest, |
| 591 | CreateRenderViewAfterProcessKillAndClosedProxy); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 592 | |
| 593 | // Returns a range to iterate over all FrameTreeNodes in the frame tree in |
| 594 | // breadth-first traversal order, skipping the subtree rooted at |
| 595 | // |node|, but including |node| itself. |
| 596 | NodeRange NodesExceptSubtree(FrameTreeNode* node); |
| 597 | |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 598 | // Returns all FrameTreeNodes in this frame tree, as well as any |
| 599 | // FrameTreeNodes of inner frame trees. Note that this doesn't include inner |
| 600 | // frame trees of inner delegates. This is used to find the aggregate |
| 601 | // IsLoading value for a frame tree. |
| 602 | // |
| 603 | // TODO(crbug.com/1261928, crbug.com/1261928): Remove this method and directly |
| 604 | // rely on GetOutermostMainFrame() and NodesIncludingInnerTreeNodes() once |
Jeremy Roman | c0c69be | 2023-11-21 19:14:52 | [diff] [blame] | 605 | // guest views are migrated to MPArch. |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 606 | std::vector<FrameTreeNode*> CollectNodesForIsLoading(); |
| 607 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 608 | const raw_ptr<Delegate> delegate_; |
Carlos Caballero | 0326252 | 2021-02-05 14:49:58 | [diff] [blame] | 609 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 610 | // These delegates are installed into all the RenderViewHosts and |
| 611 | // RenderFrameHosts that we create. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 612 | raw_ptr<RenderFrameHostDelegate> render_frame_delegate_; |
| 613 | raw_ptr<RenderViewHostDelegate> render_view_delegate_; |
| 614 | raw_ptr<RenderWidgetHostDelegate> render_widget_delegate_; |
| 615 | raw_ptr<RenderFrameHostManager::Delegate> manager_delegate_; |
| 616 | raw_ptr<PageDelegate> page_delegate_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 617 | |
| 618 | // The Navigator object responsible for managing navigations on this frame |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 619 | // tree. Each FrameTreeNode will default to using it for navigation tasks in |
| 620 | // the frame. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 621 | Navigator navigator_; |
| 622 | |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 623 | // A map to store RenderViewHosts, keyed by SiteInstanceGroup ID. |
| 624 | // This map does not cover all RenderViewHosts in a FrameTree. See |
| 625 | // `speculative_render_view_host_`. |
Devon Loehr | 92631c89 | 2025-01-02 20:15:15 | [diff] [blame] | 626 | using RenderViewHostMap = |
| 627 | std::unordered_map<RenderViewHostMapId, RenderViewHostImpl*>; |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 628 | // Map of RenderViewHostMapId to RenderViewHost. This allows us to look up the |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 629 | // RenderViewHost for a given SiteInstance when creating RenderFrameHosts. |
| 630 | // Each RenderViewHost maintains a refcount and is deleted when there are no |
| 631 | // more RenderFrameHosts or RenderFrameProxyHosts using it. |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 632 | RenderViewHostMap render_view_host_map_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 633 | |
Sharon Yang | 7ce309e | 2023-01-19 21:39:57 | [diff] [blame] | 634 | // A speculative RenderViewHost is created for all speculative cross-page |
| 635 | // same-SiteInstanceGroup RenderFrameHosts. When the corresponding |
| 636 | // RenderFrameHost gets committed and becomes the current RenderFrameHost, |
| 637 | // `speculative_render_view_host_` will be moved to `render_view_host_map_`, |
| 638 | // overwriting the previous RenderViewHost of the same SiteInstanceGroup, if |
| 639 | // applicable. This field will also be reset at that time, or if the |
| 640 | // speculative RenderFrameHost gets deleted. |
| 641 | // |
| 642 | // For any given FrameTree, there will be at most one |
| 643 | // `speculative_render_view_host_`, because only main-frame speculative |
| 644 | // RenderFrameHosts have speculative RenderViewHosts, and there is at most one |
| 645 | // such RenderFrameHost per FrameTree at a time. |
| 646 | // This is a WeakPtr, since the RenderViewHost is owned by the |
| 647 | // RenderFrameHostImpl, not the FrameTree. This implies that if the owning |
| 648 | // RenderFrameHostImpl gets deleted, this will too. |
| 649 | // |
| 650 | // This supports but is independent of RenderDocument, which introduces cases |
| 651 | // where there may be more than one RenderViewHost per SiteInstanceGroup, such |
| 652 | // as cross-page same-SiteInstanceGroup navigations. The speculative |
| 653 | // RenderFrameHost has an associated RenderViewHost, but it cannot be put in |
| 654 | // `render_view_host_map_` when it is created, as the existing RenderViewHost |
| 655 | // will be incorrectly overwritten. |
| 656 | // TODO(yangsharon, crbug.com/1336305): Expand support to include |
| 657 | // cross-SiteInstanceGroup main-frame navigations, so all main-frame |
| 658 | // navigations use speculative RenderViewHost. |
| 659 | base::WeakPtr<RenderViewHostImpl> speculative_render_view_host_; |
| 660 | |
Harkiran Bolaria | 16f2c48d | 2022-04-22 12:39:57 | [diff] [blame] | 661 | // Indicates type of frame tree. |
| 662 | const Type type_; |
| 663 | |
Avi Drissman | bd15364 | 2024-09-03 18:58:05 | [diff] [blame] | 664 | FrameTreeNodeId focused_frame_tree_node_id_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 665 | |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 666 | // Whether the initial empty page has been accessed by another page, making it |
| 667 | // unsafe to show the pending URL. Usually false unless another window tries |
| 668 | // to modify the blank page. Always false after the first commit. |
| 669 | bool has_accessed_initial_main_document_ = false; |
| 670 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 671 | bool is_being_destroyed_ = false; |
| 672 | |
Carlos Caballero | 101ac26b | 2021-03-24 11:54:05 | [diff] [blame] | 673 | #if DCHECK_IS_ON() |
| 674 | // Whether Shutdown() was called. |
| 675 | bool was_shut_down_ = false; |
| 676 | #endif |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 677 | |
Paul Semel | 3e24104 | 2022-10-11 12:57:31 | [diff] [blame] | 678 | // The root FrameTreeNode. |
| 679 | // |
| 680 | // Note: It is common for a node to test whether it's the root node, via the |
| 681 | // `root()` method, even while `root_` is running its destructor. |
| 682 | // For that reason, we want to destroy |root_| before any other fields. |
| 683 | FrameTreeNode root_; |
| 684 | |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 685 | base::WeakPtrFactory<FrameTree> weak_ptr_factory_{this}; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 686 | }; |
| 687 | |
| 688 | } // namespace content |
| 689 | |
| 690 | #endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_H_ |