blob: 793ef79f78e3491f7ecb729fed9362a96322a4af [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
danakjc492bf82020-09-09 20:02:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_
6#define CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_
7
8#include <stddef.h>
9
10#include <memory>
Arthur Sonzognic686e8f2024-01-11 08:36:3711#include <optional>
danakjc492bf82020-09-09 20:02:4412#include <string>
David Sanders2c1194d92022-04-19 23:32:3213#include <utility>
danakjc492bf82020-09-09 20:02:4414
15#include "base/gtest_prod_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5216#include "base/memory/raw_ptr.h"
Christian Biesingere1865c57c2023-10-20 15:19:2917#include "base/memory/safe_ref.h"
David Sanders2c1194d92022-04-19 23:32:3218#include "base/memory/scoped_refptr.h"
David Sandersd4bf5eb2022-03-17 07:12:0519#include "base/observer_list.h"
Mingyu Lei7956b8b2023-07-24 08:24:0820#include "base/task/cancelable_task_tracker.h"
Arthur Sonzognic686e8f2024-01-11 08:36:3721#include "base/time/time.h"
danakjc492bf82020-09-09 20:02:4422#include "content/browser/renderer_host/navigator.h"
23#include "content/browser/renderer_host/render_frame_host_impl.h"
24#include "content/browser/renderer_host/render_frame_host_manager.h"
Miyoung Shin7cf88b42022-11-07 13:22:3025#include "content/browser/renderer_host/render_frame_host_owner.h"
danakjc492bf82020-09-09 20:02:4426#include "content/common/content_export.h"
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:5527#include "content/public/browser/frame_type.h"
Rakina Zata Amni58681c62024-06-25 06:32:1328#include "content/public/browser/navigation_discard_reason.h"
Anguluri Aravind Kumara8aa5952025-02-23 02:53:0729#include "services/network/public/cpp/is_potentially_trustworthy.h"
danakjc492bf82020-09-09 20:02:4430#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Julie Jeongeun Kim0e242242022-11-30 10:45:0931#include "services/network/public/mojom/referrer_policy.mojom-forward.h"
Kevin McNee43fe8292021-10-04 22:59:4132#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4433#include "third_party/blink/public/common/frame/frame_policy.h"
danakjc492bf82020-09-09 20:02:4434#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3735#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0136#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
David Sanders2c1194d92022-04-19 23:32:3237#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom-forward.h"
danakjc492bf82020-09-09 20:02:4438#include "url/gurl.h"
39#include "url/origin.h"
40
41namespace content {
42
43class NavigationRequest;
danakjc492bf82020-09-09 20:02:4444class NavigationEntryImpl;
Paul Semel3e241042022-10-11 12:57:3145class FrameTree;
danakjc492bf82020-09-09 20:02:4446
47// When a page contains iframes, its renderer process maintains a tree structure
48// of those frames. We are mirroring this tree in the browser process. This
49// class represents a node in this tree and is a wrapper for all objects that
50// are frame-specific (as opposed to page-specific).
51//
52// Each FrameTreeNode has a current RenderFrameHost, which can change over
53// time as the frame is navigated. Any immediate subframes of the current
54// document are tracked using FrameTreeNodes owned by the current
55// RenderFrameHost, rather than as children of FrameTreeNode itself. This
56// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
57// still alive - for example while pending deletion, after a new current
58// RenderFrameHost has replaced it.
Miyoung Shin7cf88b42022-11-07 13:22:3059class CONTENT_EXPORT FrameTreeNode : public RenderFrameHostOwner {
danakjc492bf82020-09-09 20:02:4460 public:
61 class Observer {
62 public:
63 // Invoked when a FrameTreeNode is being destroyed.
64 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
65
66 // Invoked when a FrameTreeNode becomes focused.
67 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
68
Arthur Hemerye4659282022-03-28 08:36:1569 // Invoked when a FrameTreeNode moves to a different BrowsingInstance and
70 // the popups it opened should be disowned.
71 virtual void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) {}
72
Fergal Dalya1d569972021-03-16 03:24:5373 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4474 };
75
danakjc492bf82020-09-09 20:02:4476 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
77 // regardless of which FrameTree it is in.
Avi Drissmanbd153642024-09-03 18:58:0578 static FrameTreeNode* GloballyFindByID(FrameTreeNodeId frame_tree_node_id);
danakjc492bf82020-09-09 20:02:4479
80 // Returns the FrameTreeNode for the given |rfh|. Same as
81 // rfh->frame_tree_node(), but also supports nullptrs.
82 static FrameTreeNode* From(RenderFrameHost* rfh);
83
84 // Callers are are expected to initialize sandbox flags separately after
85 // calling the constructor.
86 FrameTreeNode(
Arthur Sonzognif6785ec2022-12-05 10:11:5087 FrameTree& frame_tree,
danakjc492bf82020-09-09 20:02:4488 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0189 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4490 bool is_created_by_script,
danakjc492bf82020-09-09 20:02:4491 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4192 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3493 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4494
Peter Boström828b9022021-09-21 02:28:4395 FrameTreeNode(const FrameTreeNode&) = delete;
96 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
97
Miyoung Shin7cf88b42022-11-07 13:22:3098 ~FrameTreeNode() override;
danakjc492bf82020-09-09 20:02:4499
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
102
Ian Vollick25a9d032022-04-12 23:20:17103 // Frame trees may be nested so it can be the case that IsMainFrame() is true,
104 // but is not the outermost main frame. In particular, !IsMainFrame() cannot
105 // be used to check if the frame is an embedded frame -- use
106 // !IsOutermostMainFrame() instead. NB: this does not escape guest views;
107 // IsOutermostMainFrame will be true for the outermost main frame in an inner
108 // guest view.
danakjc492bf82020-09-09 20:02:44109 bool IsMainFrame() const;
Arthur Hemerya06697f2023-03-14 09:20:57110 bool IsOutermostMainFrame() const;
danakjc492bf82020-09-09 20:02:44111
Anguluri Aravind Kumara8aa5952025-02-23 02:53:07112 // Returns true if all the ancestors of the current frame have a potentially
113 // trustworthy origin.
114 bool AreAncestorsSecure();
115
Arthur Sonzognif6785ec2022-12-05 10:11:50116 FrameTree& frame_tree() const { return frame_tree_.get(); }
Paul Semel3e241042022-10-11 12:57:31117 Navigator& navigator();
danakjc492bf82020-09-09 20:02:44118
119 RenderFrameHostManager* render_manager() { return &render_manager_; }
Alexander Timin33e2e2c12022-03-03 04:21:33120 const RenderFrameHostManager* render_manager() const {
121 return &render_manager_;
122 }
Avi Drissmanbd153642024-09-03 18:58:05123 FrameTreeNodeId frame_tree_node_id() const { return frame_tree_node_id_; }
Yuzu Saijo03dbf9b2022-07-22 04:29:45124 // This reflects window.name, which is initially set to the the "name"
125 // attribute. But this won't reflect changes of 'name' attribute and instead
126 // reflect changes to the Window object's name property.
127 // This is different from IframeAttributes' name in that this will not get
128 // updated when 'name' attribute gets updated.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47129 const std::string& frame_name() const {
130 return render_manager_.current_replication_state().name;
131 }
danakjc492bf82020-09-09 20:02:44132
133 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47134 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44135 }
136
danakjc492bf82020-09-09 20:02:44137 size_t child_count() const { return current_frame_host()->child_count(); }
138
danakjc492bf82020-09-09 20:02:44139 RenderFrameHostImpl* parent() const { return parent_; }
140
Dave Tapuskac8de3b02021-12-03 21:51:01141 // See `RenderFrameHost::GetParentOrOuterDocument()` for
142 // documentation.
Arthur Hemerya06697f2023-03-14 09:20:57143 RenderFrameHostImpl* GetParentOrOuterDocument() const;
Dave Tapuskac8de3b02021-12-03 21:51:01144
145 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
146 // documentation.
147 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
148
danakjc492bf82020-09-09 20:02:44149 FrameTreeNode* opener() const { return opener_; }
150
Rakina Zata Amni3a48ae42022-05-05 03:39:56151 FrameTreeNode* first_live_main_frame_in_original_opener_chain() const {
152 return first_live_main_frame_in_original_opener_chain_;
153 }
danakjc492bf82020-09-09 20:02:44154
Arthur Sonzognic686e8f2024-01-11 08:36:37155 const std::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39156 return opener_devtools_frame_token_;
157 }
158
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55159 // Returns the type of the frame. Refer to frame_type.h for the details.
160 FrameType GetFrameType() const;
161
danakjc492bf82020-09-09 20:02:44162 // Assigns a new opener for this node and, if |opener| is non-null, registers
163 // an observer that will clear this node's opener if |opener| is ever
164 // destroyed.
165 void SetOpener(FrameTreeNode* opener);
166
167 // Assigns the initial opener for this node, and if |opener| is non-null,
168 // registers an observer that will clear this node's opener if |opener| is
169 // ever destroyed. The value set here is the root of the tree.
170 //
171 // It is not possible to change the opener once it was set.
172 void SetOriginalOpener(FrameTreeNode* opener);
173
Wolfgang Beyerd8809db2020-09-30 15:29:39174 // Assigns an opener frame id for this node. This string id is only set once
175 // and cannot be changed. It persists, even if the |opener| is destroyed. It
176 // is used for attribution in the DevTools frontend.
177 void SetOpenerDevtoolsFrameToken(
178 base::UnguessableToken opener_devtools_frame_token);
179
danakjc492bf82020-09-09 20:02:44180 FrameTreeNode* child_at(size_t index) const {
181 return current_frame_host()->child_at(index);
182 }
183
184 // Returns the URL of the last committed page in the current frame.
185 const GURL& current_url() const {
186 return current_frame_host()->GetLastCommittedURL();
187 }
188
Charlie Reis734db662024-01-11 18:20:03189 // Moves this frame out of the initial empty document state, which is a
190 // one-way change for FrameTreeNode (i.e., it cannot go back into the initial
191 // empty document state).
192 void set_not_on_initial_empty_document() {
193 is_on_initial_empty_document_ = false;
194 }
195
196 // Returns false if the frame has committed a document that is not the initial
197 // empty document, or if the current document's input stream has been opened
198 // with document.open(), causing the document to lose its "initial empty
199 // document" status. For more details, see the definition of
200 // `is_on_initial_empty_document_`.
Rakina Zata Amni86c88fa2021-11-01 01:27:30201 bool is_on_initial_empty_document() const {
Charlie Reis734db662024-01-11 18:20:03202 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56203 }
204
danakjc492bf82020-09-09 20:02:44205 // Returns whether the frame's owner element in the parent document is
206 // collapsed, that is, removed from the layout as if it did not exist, as per
207 // request by the embedder (of the content/ layer).
208 bool is_collapsed() const { return is_collapsed_; }
209
210 // Sets whether to collapse the frame's owner element in the parent document,
211 // that is, to remove it from the layout as if it did not exist, as per
212 // request by the embedder (of the content/ layer). Cannot be called for main
213 // frames.
214 //
215 // This only has an effect for <iframe> owner elements, and is a no-op when
216 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
217 void SetCollapsed(bool collapsed);
218
219 // Returns the origin of the last committed page in this frame.
220 // WARNING: To get the last committed origin for a particular
221 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
222 // which will behave correctly even when the RenderFrameHost is not the
223 // current one for this frame (such as when it's pending deletion).
224 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47225 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44226 }
227
danakjc492bf82020-09-09 20:02:44228 // Returns the latest frame policy (sandbox flags and container policy) for
229 // this frame. This includes flags inherited from parent frames and the latest
230 // flags from the <iframe> element hosting this frame. The returned policies
231 // may not yet have taken effect, since "sandbox" and "allow" attribute
Liam Brady25a14162022-12-02 15:25:57232 // updates in an <iframe> element take effect on next navigation. For
233 // <fencedframe> elements, not everything in the frame policy might actually
234 // take effect after the navigation. To retrieve the currently active policy
235 // for this frame, use effective_frame_policy().
danakjc492bf82020-09-09 20:02:44236 const blink::FramePolicy& pending_frame_policy() const {
237 return pending_frame_policy_;
238 }
239
Ming-Ying Chung0430c592025-01-21 00:33:10240 // Update this frame's sandbox flags, container policy and deferred fetch
241 // policy.
242 // This is called when either
243 // - a parent frame updates the "sandbox" attribute in the <iframe> element
244 // for this frame
245 // - any of the attributes which affect the container policy
246 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
247 // - a frame begins navigation which leads to calculation of deferred fetch
248 // policy.
danakjc492bf82020-09-09 20:02:44249 // These policies won't take effect until next navigation. If this frame's
250 // parent is itself sandboxed, the parent's sandbox flags are combined with
251 // those in |frame_policy|.
252 // Attempting to change the container policy on the main frame will have no
253 // effect.
254 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
255
256 // Returns the currently active frame policy for this frame, including the
257 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39258 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44259 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
260 // origin of the iframe's src attribute (which may be different from the URL
261 // of the document currently loaded into the frame). This does not include
262 // policy changes that have been made by updating the containing iframe
263 // element attributes since the frame was last navigated; use
264 // pending_frame_policy() for those.
265 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47266 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44267 }
268
danakjc492bf82020-09-09 20:02:44269 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
270 return frame_owner_properties_;
271 }
272
273 void set_frame_owner_properties(
274 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
275 frame_owner_properties_ = frame_owner_properties;
276 }
277
Yuzu Saijo03dbf9b2022-07-22 04:29:45278 // Reflects the attributes of the corresponding iframe html element, such
Arthur Sonzogni64457592022-11-22 11:08:59279 // as 'credentialless', 'id', 'name' and 'src'. These values should not be
Yuzu Saijo03dbf9b2022-07-22 04:29:45280 // exposed to cross-origin renderers.
281 const network::mojom::ContentSecurityPolicy* csp_attribute() const {
282 return attributes_->parsed_csp_attribute.get();
danakjc492bf82020-09-09 20:02:44283 }
Yao Xiao9c54b3e2023-03-14 04:25:04284 // Tracks iframe's 'browsingtopics' attribute, indicating whether the
285 // navigation requests on this frame should calculate and send the
286 // `Sec-Browsing-Topics` header.
287 bool browsing_topics() const { return attributes_->browsing_topics; }
Camillia Smith Barnes6d2966c82023-08-23 21:16:18288
Orr Bernsteina0cc6792023-11-14 22:12:35289 // Tracks iframe's 'adauctionheaders' attribute, indicating whether the
290 // navigation request on this frame should calculate and send the
291 // 'Sec-Ad-Auction-Fetch` header.
292 bool ad_auction_headers() const { return attributes_->ad_auction_headers; }
293
Camillia Smith Barnes6d2966c82023-08-23 21:16:18294 // Tracks iframe's 'sharedstoragewritable' attribute, indicating what value
Camillia Smith Barnesc267be62023-11-01 20:01:02295 // the the corresponding
296 // `network::ResourceRequest::shared_storage_writable_eligible` should take
297 // for the navigation(s) on this frame, pending a permissions policy check. If
298 // true, and if the permissions policy check returns "enabled", the network
Camillia Smith Barnes6d2966c82023-08-23 21:16:18299 // service will send the `Shared-Storage-Write` request header.
Camillia Smith Barnesc267be62023-11-01 20:01:02300 bool shared_storage_writable_opted_in() const {
301 return attributes_->shared_storage_writable_opted_in;
Camillia Smith Barnes6d2966c82023-08-23 21:16:18302 }
Arthur Sonzognic686e8f2024-01-11 08:36:37303 const std::optional<std::string> html_id() const { return attributes_->id; }
Yuzu Saijo03dbf9b2022-07-22 04:29:45304 // This tracks iframe's 'name' attribute instead of window.name, which is
305 // tracked in FrameReplicationState. See the comment for frame_name() for
306 // more details.
Arthur Sonzognic686e8f2024-01-11 08:36:37307 const std::optional<std::string> html_name() const {
Yuzu Saijodc870f92023-01-20 03:39:11308 return attributes_->name;
309 }
Arthur Sonzognic686e8f2024-01-11 08:36:37310 const std::optional<std::string> html_src() const { return attributes_->src; }
danakjc492bf82020-09-09 20:02:44311
Yuzu Saijo03dbf9b2022-07-22 04:29:45312 void SetAttributes(blink::mojom::IframeAttributesPtr attributes);
Antonio Sartori5abc8de2021-07-13 08:42:47313
danakjc492bf82020-09-09 20:02:44314 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47315 return render_manager_.current_replication_state().origin.IsSameOriginWith(
316 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44317 }
318
Gyuyoung Kimc16e52e92021-03-19 02:45:37319 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47320 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44321 }
322
323 RenderFrameHostImpl* current_frame_host() const {
324 return render_manager_.current_frame_host();
325 }
326
danakjc492bf82020-09-09 20:02:44327 // Returns true if this node is in a loading state.
328 bool IsLoading() const;
Nate Chapin470dbc62023-04-25 16:34:38329 LoadingState GetLoadingState() const;
danakjc492bf82020-09-09 20:02:44330
Alex Moshchuk9b0fd822020-10-26 23:08:15331 // Returns true if this node has a cross-document navigation in progress.
332 bool HasPendingCrossDocumentNavigation() const;
333
danakjc492bf82020-09-09 20:02:44334 NavigationRequest* navigation_request() { return navigation_request_.get(); }
335
336 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
337 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
338 // RenderFrameHost that is committing the navigation.
339 void TransferNavigationRequestOwnership(
340 RenderFrameHostImpl* render_frame_host);
341
342 // Takes ownership of |navigation_request| and makes it the current
343 // NavigationRequest of this frame. This corresponds to the start of a new
344 // navigation. If there was an ongoing navigation request before calling this
345 // function, it is canceled. |navigation_request| should not be null.
Charlie Reis09952ee2022-12-08 16:35:07346 void TakeNavigationRequest(
danakjc492bf82020-09-09 20:02:44347 std::unique_ptr<NavigationRequest> navigation_request);
348
Rakina Zata Amnif8f2bb62022-11-23 05:54:32349 // Resets the navigation request owned by `this` (which shouldn't have reached
350 // the "pending commit" stage yet) and any state created by it, including the
Rakina Zata Amni33175cb92022-11-24 02:46:03351 // speculative RenderFrameHost (if there are no other navigations associated
352 // with it). Note that this does not affect navigations that have reached the
353 // "pending commit" stage, which are owned by their corresponding
354 // RenderFrameHosts instead.
Daniel Cheng390e2a72022-09-28 06:07:53355 void ResetNavigationRequest(NavigationDiscardReason reason);
356
Rakina Zata Amnif8f2bb62022-11-23 05:54:32357 // Similar to `ResetNavigationRequest()`, but keeps the state created by the
Daniel Cheng390e2a72022-09-28 06:07:53358 // NavigationRequest (e.g. speculative RenderFrameHost, loading state).
Rakina Zata Amni58681c62024-06-25 06:32:13359 void ResetNavigationRequestButKeepState(NavigationDiscardReason reason);
danakjc492bf82020-09-09 20:02:44360
danakjc492bf82020-09-09 20:02:44361 // The load progress for a RenderFrameHost in this node was updated to
362 // |load_progress|. This will notify the FrameTree which will in turn notify
363 // the WebContents.
364 void DidChangeLoadProgress(double load_progress);
365
366 // Called when the user directed the page to stop loading. Stops all loads
367 // happening in the FrameTreeNode. This method should be used with
368 // FrameTree::ForEach to stop all loads in the entire FrameTree.
369 bool StopLoading();
370
371 // Returns the time this frame was last focused.
372 base::TimeTicks last_focus_time() const { return last_focus_time_; }
373
374 // Called when this node becomes focused. Updates the node's last focused
375 // time and notifies observers.
376 void DidFocus();
377
378 // Called when the user closed the modal dialogue for BeforeUnload and
379 // cancelled the navigation. This should stop any load happening in the
380 // FrameTreeNode.
381 void BeforeUnloadCanceled();
382
danakjc492bf82020-09-09 20:02:44383 // Returns the sandbox flags currently in effect for this frame. This includes
384 // flags inherited from parent frames, the currently active flags from the
385 // <iframe> element hosting this frame, as well as any flags set from a
386 // Content-Security-Policy HTTP header. This does not include flags that have
387 // have been updated in an <iframe> element but have not taken effect yet; use
388 // pending_frame_policy() for those. To see the flags which will take effect
389 // on navigation (which does not include the CSP-set flags), use
390 // effective_frame_policy().
391 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47392 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44393 }
394
danakjc492bf82020-09-09 20:02:44395 // Returns whether the frame received a user gesture on a previous navigation
396 // on the same eTLD+1.
397 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47398 return render_manager_.current_replication_state()
399 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44400 }
401
402 // When a tab is discarded, WebContents sets was_discarded on its
403 // root FrameTreeNode.
404 // In addition, when a child frame is created, this bit is passed on from
405 // parent to child.
406 // When a navigation request is created, was_discarded is passed on to the
407 // request and reset to false in FrameTreeNode.
408 void set_was_discarded() { was_discarded_ = true; }
409 bool was_discarded() const { return was_discarded_; }
410
Miyoung Shin8a66ec022022-11-28 23:50:09411 // Deprecated. Use directly HasStickyUserActivation in RFHI.
danakjc492bf82020-09-09 20:02:44412 // Returns the sticky bit of the User Activation v2 state of the
413 // |FrameTreeNode|.
414 bool HasStickyUserActivation() const {
Miyoung Shin8a66ec022022-11-28 23:50:09415 return current_frame_host()->HasStickyUserActivation();
danakjc492bf82020-09-09 20:02:44416 }
417
Miyoung Shin8a66ec022022-11-28 23:50:09418 // Deprecated. Use directly HasStickyUserActivation in RFHI.
danakjc492bf82020-09-09 20:02:44419 // Returns the transient bit of the User Activation v2 state of the
420 // |FrameTreeNode|.
421 bool HasTransientUserActivation() {
Miyoung Shin8a66ec022022-11-28 23:50:09422 return current_frame_host()->HasTransientUserActivation();
danakjc492bf82020-09-09 20:02:44423 }
424
425 // Remove history entries for all frames created by script in this frame's
426 // subtree. If a frame created by a script is removed, then its history entry
427 // will never be reused - this saves memory.
428 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
429
Abhijeet Kandalkarb43affa72022-09-27 16:48:01430 using FencedFrameStatus = RenderFrameHostImpl::FencedFrameStatus;
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58431 FencedFrameStatus fenced_frame_status() const { return fenced_frame_status_; }
432
Kevin McNee43fe8292021-10-04 22:59:41433 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45434 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44435 }
danakjc492bf82020-09-09 20:02:44436
Daniel Cheng6ac128172021-05-25 18:49:01437 blink::mojom::TreeScopeType tree_scope_type() const {
438 return tree_scope_type_;
439 }
440
arthursonzogni034bb9c2020-10-01 08:29:56441 // The initial popup URL for new window opened using:
442 // `window.open(initial_popup_url)`.
443 // An empty GURL otherwise.
444 //
445 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
446 // document served from this URL. The FrameTreeNode always starts hosting the
447 // initial empty document and attempts a navigation toward this URL. However
448 // the navigation might be delayed, redirected and even cancelled.
449 void SetInitialPopupURL(const GURL& initial_popup_url);
450 const GURL& initial_popup_url() const { return initial_popup_url_; }
451
452 // The origin of the document that used window.open() to create this frame.
453 // Otherwise, an opaque Origin with a nonce different from all previously
454 // existing Origins.
455 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
456 const url::Origin& popup_creator_origin() const {
457 return popup_creator_origin_;
458 }
459
Harkiran Bolaria59290d62021-03-17 01:53:01460 // Sets the associated FrameTree for this node. The node can change FrameTrees
Domenic Denicola7767a9c2023-07-13 15:36:39461 // as part of prerendering, which allows a page loaded in the prerendered
462 // FrameTree to be used for a navigation in the primary frame tree.
Harkiran Bolaria59290d62021-03-17 01:53:01463 void SetFrameTree(FrameTree& frame_tree);
464
Alexander Timin074cd182022-03-23 18:11:22465 using TraceProto = perfetto::protos::pbzero::FrameTreeNodeInfo;
Alexander Timinf785f342021-03-18 00:00:56466 // Write a representation of this object into a trace.
Alexander Timin074cd182022-03-23 18:11:22467 void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
Alexander Timinf785f342021-03-18 00:00:56468
Carlos Caballero76711352021-03-24 17:38:21469 // Returns true the node is navigating, i.e. it has an associated
470 // NavigationRequest.
471 bool HasNavigation();
472
murakinonoka97a8f042024-01-10 09:17:07473 // Returns true if there are any navigations happening in FrameTreeNode that
474 // is pending commit (i.e. between ReadyToCommit and DidCommit). Note that
475 // those navigations won't live in the FrameTreeNode itself, as they will
476 // already be owned by the committing RenderFrameHost (either the current
477 // RenderFrameHost or the speculative RenderFrameHost).
478 bool HasPendingCommitNavigation();
479
shivanigithubf3ddff52021-07-03 22:06:30480 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30481 // Note that these two functions cannot be invoked from a FrameTree's or
482 // its root node's constructor since they require the frame tree and the
483 // root node to be completely constructed.
484 //
shivanigithubf3ddff52021-07-03 22:06:30485 // Returns false if fenced frames are disabled. Returns true if the feature is
486 // enabled and if |this| is a fenced frame. Returns false for
487 // iframes embedded in a fenced frame. To clarify: for the MPArch
488 // implementation this only returns true if |this| is the actual
489 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
490 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36491 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30492
493 // Returns false if fenced frames are disabled. Returns true if the
494 // feature is enabled and if |this| or any of its ancestor nodes is a
495 // fenced frame.
496 bool IsInFencedFrameTree() const;
497
shivanigithub4cd016a2021-09-20 21:10:30498 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
Garrett Tanzer34cb92fe2022-09-28 17:50:54499 // Returns nullopt otherwise.
500 //
501 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
502 // frame and any iframes nested within it. Not set if this frame is not in a
503 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
504 // the MPArch version but for the shadow DOM version we need to keep it here
505 // since the fenced frame root is not a main frame for the latter. The value
506 // of the nonce will be the same for all of the the iframes inside a fenced
507 // frame tree. If there is a nested fenced frame it will have a different
508 // nonce than its parent fenced frame. The nonce will stay the same across
509 // navigations initiated from the fenced frame tree because it is always used
510 // in conjunction with other fields of the keys and would be good to access
511 // the same storage across same-origin navigations. If the navigation is
512 // same-origin/site then the same network stack partition/storage will be
513 // reused and if it's cross-origin/site then other parts of the key will
514 // change and so, even with the same nonce, another partition will be used.
515 // But if the navigation is initiated from the embedder, the nonce will be
516 // reinitialized irrespective of same or cross origin such that there is no
517 // privacy leak via storage shared between two embedder initiated navigations.
518 // Note that this reinitialization is implemented for all embedder-initiated
519 // navigations in MPArch, but only urn:uuid navigations in ShadowDOM.
Arthur Sonzognic686e8f2024-01-11 08:36:37520 std::optional<base::UnguessableToken> GetFencedFrameNonce();
shivanigithub4cd016a2021-09-20 21:10:30521
Garrett Tanzer34cb92fe2022-09-28 17:50:54522 // If applicable, initialize the default fenced frame properties. Right now,
523 // this means setting a new fenced frame nonce. See comment on
shivanigithub4cd016a2021-09-20 21:10:30524 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
525 // by FrameTree::Init() or FrameTree::AddFrame().
Garrett Tanzer34cb92fe2022-09-28 17:50:54526 void SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30527
Garrett Tanzer291a2d52023-03-20 22:41:57528 // Set the current FencedFrameProperties to have "opaque ads mode".
529 // This should only be used during tests, when the proper embedder-initiated
530 // fenced frame root urn/config navigation flow isn't available.
Alison Gale770f3fc2024-04-27 00:39:58531 // TODO(crbug.com/40233168): Refactor and expand use of test utils so there is
Garrett Tanzer291a2d52023-03-20 22:41:57532 // a consistent way to do this properly everywhere. Consider removing
533 // arbitrary restrictions in "default mode" so that using opaque ads mode is
534 // less necessary.
535 void SetFencedFramePropertiesOpaqueAdsModeForTesting() {
536 if (fenced_frame_properties_.has_value()) {
Garrett Tanzer06980702023-12-12 19:48:20537 fenced_frame_properties_
538 ->SetFencedFramePropertiesOpaqueAdsModeForTesting();
Garrett Tanzer291a2d52023-03-20 22:41:57539 }
540 }
541
542 // Returns the mode attribute from the `FencedFrameProperties` if this frame
543 // is in a fenced frame tree, otherwise returns `kDefault`.
544 blink::FencedFrame::DeprecatedFencedFrameMode GetDeprecatedFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16545
Dave Tapuskac8de3b02021-12-03 21:51:01546 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
547 // Do not use directly.
Kevin McNee86e64ee2023-02-17 16:35:50548 // `escape_guest_view` determines whether to iterate out of guest views and is
549 // the behaviour distinction between GetParentOrOuterDocument and
550 // GetParentOrOuterDocumentOrEmbedder. See the comment on
551 // GetParentOrOuterDocumentOrEmbedder for details.
552 // `include_prospective` includes embedders which own our frame tree, but have
553 // not yet attached it to the outer frame tree.
Arthur Hemerya06697f2023-03-14 09:20:57554 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(
555 bool escape_guest_view,
556 bool include_prospective) const;
Dave Tapuskac8de3b02021-12-03 21:51:01557
Harkiran Bolariab4437fd2021-08-11 17:51:22558 // Sets the unique_name and name fields on replication_state_. To be used in
559 // prerender activation to make sure the FrameTreeNode replication state is
560 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
561 // renderers should already have the correct value, so unlike
562 // FrameTreeNode::SetFrameName, we do not notify them here.
Alison Gale770f3fc2024-04-27 00:39:58563 // TODO(crbug.com/40192974): Remove this once the BrowsingContextState
Harkiran Bolaria4eacb3a2021-12-13 20:03:47564 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22565 void set_frame_name_for_activation(const std::string& unique_name,
566 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40567 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
568 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22569 }
570
Nan Linaaf84f72021-12-02 22:31:56571 // Returns true if error page isolation is enabled.
572 bool IsErrorPageIsolationEnabled() const;
573
W. James MacLean81b8d01f2022-01-25 20:50:59574 // Functions to store and retrieve a frame's srcdoc value on this
575 // FrameTreeNode.
576 void SetSrcdocValue(const std::string& srcdoc_value);
577 const std::string& srcdoc_value() const { return srcdoc_value_; }
578
Garrett Tanzerc69f4642022-08-15 22:15:14579 void set_fenced_frame_properties(
Arthur Sonzognic686e8f2024-01-11 08:36:37580 const std::optional<FencedFrameProperties>& fenced_frame_properties) {
Alison Gale770f3fc2024-04-27 00:39:58581 // TODO(crbug.com/40202462): Reenable this DCHECK once ShadowDOM and
Garrett Tanzer2975eea2022-08-22 16:34:01582 // loading urns in iframes (for FLEDGE OT) are gone.
583 // DCHECK_EQ(fenced_frame_status_,
584 // RenderFrameHostImpl::FencedFrameStatus::kFencedFrameRoot);
Garrett Tanzerc69f4642022-08-15 22:15:14585 fenced_frame_properties_ = fenced_frame_properties;
586 }
587
Xiaochen Zhou86f2e712023-09-13 19:55:04588 // This function returns the fenced frame properties associated with the given
589 // source.
590 // - If `source_node` is set to `kClosestAncestor`, the fenced frame
591 // properties are obtained by a bottom-up traversal from this node.
592 // - If `source_node` is set tp `kFrameTreeRoot`, the fenced frame properties
593 // from the fenced frame tree root are returned.
594 // For example, for an urn iframe that is nested inside a fenced frame.
595 // Calling this function from the nested urn iframe with `source_node` set to:
596 // - `kClosestAncestor`: returns the fenced frame properties from the urn
597 // iframe.
598 // - `kFrameTreeRoot`: returns the fenced frame properties from the fenced
599 // frame.
600 // Clients should decide which one to use depending on how the application of
601 // the fenced frame properties interact with urn iframes.
Alison Gale770f3fc2024-04-27 00:39:58602 // TODO(crbug.com/40060657): Once navigation support for urn::uuid in iframes
Xiaochen Zhou86f2e712023-09-13 19:55:04603 // is deprecated, remove the parameter `node_source`.
Arthur Sonzognic686e8f2024-01-11 08:36:37604 std::optional<FencedFrameProperties>& GetFencedFrameProperties(
Xiaochen Zhou86f2e712023-09-13 19:55:04605 FencedFramePropertiesNodeSource node_source =
606 FencedFramePropertiesNodeSource::kClosestAncestor);
Garrett Tanzerc69f4642022-08-15 22:15:14607
Liam Brady27da6a22024-06-05 16:35:34608 // Helper function for getting the FrameTreeNode that houses the relevant
609 // FencedFrameProperties when GetFencedFrameProperties() is called with
610 // kClosestAncestor.
611 FrameTreeNode* GetClosestAncestorWithFencedFrameProperties();
612
Liam Brady86ca0482023-12-06 19:49:25613 bool HasFencedFrameProperties() const {
614 return fenced_frame_properties_.has_value();
615 }
616
Yao Xiaof9ae90a2023-03-01 20:52:44617 // Returns the number of fenced frame boundaries above this frame. The
Yao Xiaoa2337ad2022-10-12 20:59:29618 // outermost main frame's frame tree has fenced frame depth 0, a topmost
619 // fenced frame tree embedded in the outermost main frame has fenced frame
620 // depth 1, etc.
Yao Xiaof9ae90a2023-03-01 20:52:44621 //
622 // Also, sets `shared_storage_fenced_frame_root_count` to the
623 // number of fenced frame boundaries (roots) above this frame that originate
624 // from shared storage. This is used to check whether a fenced frame
625 // originates from shared storage only (i.e. not from FLEDGE).
Alison Gale770f3fc2024-04-27 00:39:58626 // TODO(crbug.com/40233168): Remove this check once we put permissions inside
Yao Xiaof9ae90a2023-03-01 20:52:44627 // FencedFrameConfig.
628 size_t GetFencedFrameDepth(size_t& shared_storage_fenced_frame_root_count);
Yao Xiaoa2337ad2022-10-12 20:59:29629
630 // Traverse up from this node. Return all valid
631 // `node->fenced_frame_properties_->shared_storage_budget_metadata` (i.e. this
632 // node is subjected to the shared storage budgeting associated with those
633 // metadata). Every node that originates from sharedStorage.selectURL() will
634 // have an associated metadata. This indicates that the metadata can only
635 // possibly be associated with a fenced frame root, unless when
636 // `kAllowURNsInIframes` is enabled in which case they could be be associated
637 // with any node.
Garrett Tanzer29de7112022-12-06 21:26:32638 std::vector<const SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49639 FindSharedStorageBudgetMetadata();
640
Camillia Smith Barnes7218518c2023-03-06 19:02:17641 // Returns any shared storage context string that was written to a
642 // `blink::FencedFrameConfig` before navigation via
643 // `setSharedStorageContext()`, as long as the request is for a same-origin
644 // frame within the config's fenced frame tree (or a same-origin descendant of
645 // a URN iframe).
Arthur Sonzognic686e8f2024-01-11 08:36:37646 std::optional<std::u16string> GetEmbedderSharedStorageContextIfAllowed();
Camillia Smith Barnes7218518c2023-03-06 19:02:17647
Harkiran Bolariaebbe7702022-02-22 19:19:03648 // Accessor to BrowsingContextState for subframes only. Only main frame
649 // navigations can change BrowsingInstances and BrowsingContextStates,
650 // therefore for subframes associated BrowsingContextState never changes. This
651 // helper method makes this more explicit and guards against calling this on
652 // main frames (there an appropriate BrowsingContextState should be obtained
653 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
654 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
655 // the same frame).
656 const scoped_refptr<BrowsingContextState>&
657 GetBrowsingContextStateForSubframe() const;
658
Arthur Hemerye4659282022-03-28 08:36:15659 // Clears the opener property of popups referencing this FrameTreeNode as
660 // their opener.
661 void ClearOpenerReferences();
662
Liam Brady95d36d12023-03-13 21:13:06663 // Calculates whether one of the ancestor frames or this frame has a CSPEE in
664 // place. This is eventually sent over to LocalFrame in the renderer where it
665 // will be used by NavigatorAuction::canLoadAdAuctionFencedFrame for
666 // information it can't get on its own.
Liam Bradyd2a41e152022-07-19 13:58:48667 bool AncestorOrSelfHasCSPEE() const;
668
Arthur Sonzogni8e8eb1f2023-01-10 14:51:01669 // Reset every navigation in this frame, and its descendants. This is called
670 // after the <iframe> element has been removed, or after the document owning
671 // this frame has been navigated away.
672 //
673 // This takes into account:
674 // - Non-pending commit NavigationRequest owned by the FrameTreeNode
675 // - Pending commit NavigationRequest owned by the current RenderFrameHost
676 // - Speculative RenderFrameHost and its pending commit NavigationRequests.
677 void ResetAllNavigationsForFrameDetach();
678
Miyoung Shin7cf88b42022-11-07 13:22:30679 // RenderFrameHostOwner implementation:
Nate Chapin470dbc62023-04-25 16:34:38680 void DidStartLoading(LoadingState previous_frame_tree_loading_state) override;
Julie Jeongeun Kim07c077bd2022-12-05 08:40:31681 void DidStopLoading() override;
Miyoung Shin7cf88b42022-11-07 13:22:30682 void RestartNavigationAsCrossDocument(
683 std::unique_ptr<NavigationRequest> navigation_request) override;
Miyoung Shin1504eb712022-12-07 10:32:18684 bool Reload() override;
Julie Jeongeun Kimc1b07c32022-11-11 10:26:32685 Navigator& GetCurrentNavigator() override;
Miyoung Shine16cd2262022-11-30 05:52:16686 RenderFrameHostManager& GetRenderFrameHostManager() override;
Miyoung Shin64fd1bea2023-01-04 04:22:08687 FrameTreeNode* GetOpener() const override;
Julie Jeongeun Kim2132b37f82022-11-23 08:30:46688 void SetFocusedFrame(SiteInstanceGroup* source) override;
Julie Jeongeun Kim0e242242022-11-30 10:45:09689 void DidChangeReferrerPolicy(
690 network::mojom::ReferrerPolicy referrer_policy) override;
Miyoung Shin8a66ec022022-11-28 23:50:09691 // Updates the user activation state in the browser frame tree and in the
692 // frame trees in all renderer processes except the renderer for this node
693 // (which initiated the update). Returns |false| if the update tries to
694 // consume an already consumed/expired transient state, |true| otherwise. See
695 // the comment on `user_activation_state_` in RenderFrameHostImpl.
696 //
697 // The |notification_type| parameter is used for histograms, only for the case
698 // |update_state == kNotifyActivation|.
699 bool UpdateUserActivationState(
700 blink::mojom::UserActivationUpdateType update_type,
701 blink::mojom::UserActivationNotificationType notification_type) override;
Nate Chapin47276a62023-02-16 16:53:44702 void DidConsumeHistoryUserActivation() override;
Charlie Reis734db662024-01-11 18:20:03703 void DidOpenDocumentInputStream() override;
Miyoung Shinff13ed22022-11-30 09:21:47704 std::unique_ptr<NavigationRequest>
705 CreateNavigationRequestForSynchronousRendererCommit(
706 RenderFrameHostImpl* render_frame_host,
707 bool is_same_document,
708 const GURL& url,
709 const url::Origin& origin,
Arthur Sonzognic686e8f2024-01-11 08:36:37710 const std::optional<GURL>& initiator_base_url,
Miyoung Shinff13ed22022-11-30 09:21:47711 const net::IsolationInfo& isolation_info_for_subresources,
712 blink::mojom::ReferrerPtr referrer,
713 const ui::PageTransition& transition,
714 bool should_replace_current_entry,
715 const std::string& method,
716 bool has_transient_activation,
717 bool is_overriding_user_agent,
718 const std::vector<GURL>& redirects,
719 const GURL& original_url,
720 std::unique_ptr<CrossOriginEmbedderPolicyReporter> coep_reporter,
Camille Lamy36afacd2025-01-16 14:25:18721 std::unique_ptr<DocumentIsolationPolicyReporter> dip_reporter,
Charlie Reise1d9b8182025-04-02 04:32:12722 int http_response_code,
723 base::TimeTicks actual_navigation_start) override;
Rakina Zata Amni58681c62024-06-25 06:32:13724 void CancelNavigation(NavigationDiscardReason reason) override;
Thomas Lukaszewicz1b672fe2024-09-17 08:35:03725 void ResetNavigationsForDiscard() override;
Miyoung Shinc9ff4812023-01-05 08:58:05726 bool Credentialless() const override;
Kevin McNeef1b0f0b2024-09-17 21:49:41727 FrameType GetCurrentFrameType() const override;
Miyoung Shinff13ed22022-11-30 09:21:47728
Mingyu Lei7956b8b2023-07-24 08:24:08729 // Restart the navigation restoring the page from the back-forward cache
730 // as a regular non-BFCached history navigation.
731 //
732 // The restart itself is asynchronous as it's dangerous to restart navigation
733 // with arbitrary state on the stack (another navigation might be starting),
734 // so this function only posts the actual task to do all the work (See
735 // `RestartBackForwardCachedNavigationImpl()`).
736 void RestartBackForwardCachedNavigationAsync(int nav_entry_id);
737
738 // Cancel the asynchronous task that would restart the BFCache navigation.
739 // This should be called whenever a FrameTreeNode's NavigationRequest would
740 // normally get cancelled, including when another NavigationRequest starts.
741 // This preserves the previous behavior where a restarting BFCache
742 // NavigationRequest is kept around until the task to create the new
743 // navigation is run, or until that NavigationRequest gets deleted (which
744 // cancels the task).
745 void CancelRestartingBackForwardCacheNavigation();
746
Christian Biesingere1865c57c2023-10-20 15:19:29747 base::SafeRef<FrameTreeNode> GetSafeRef() {
748 return weak_factory_.GetSafeRef();
749 }
750
danakjc492bf82020-09-09 20:02:44751 private:
Yuzu Saijo03dbf9b2022-07-22 04:29:45752 friend class CSPEmbeddedEnforcementUnitTest;
Charlie Hubb5943d2021-03-09 19:46:12753 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44754 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12755 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44756 ContainerPolicySandboxDynamic);
Yuzu Saijo03dbf9b2022-07-22 04:29:45757 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest, StorageKeyToCommit);
Arthur Sonzogni64457592022-11-22 11:08:59758 FRIEND_TEST_ALL_PREFIXES(
759 NavigationRequestTest,
760 NavigationToCredentiallessDocumentNetworkIsolationInfo);
Yuzu Saijo03dbf9b2022-07-22 04:29:45761 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplTest,
Arthur Sonzogni64457592022-11-22 11:08:59762 ChildOfCredentiallessIsCredentialless);
Yifan Luo86a79f42022-08-16 18:38:27763 FRIEND_TEST_ALL_PREFIXES(ContentPasswordManagerDriverTest,
Arthur Sonzogni64457592022-11-22 11:08:59764 PasswordAutofillDisabledOnCredentiallessIframe);
danakjc492bf82020-09-09 20:02:44765
Dominic Farolino8a2187b2021-12-24 20:44:21766 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
767 // representing an inner FrameTree, this method destroys said inner FrameTree.
768 void DestroyInnerFrameTreeIfExists();
769
danakjc492bf82020-09-09 20:02:44770 class OpenerDestroyedObserver;
771
danakjc492bf82020-09-09 20:02:44772 // The |notification_type| parameter is used for histograms only.
Liam Brady38b84562024-03-07 22:11:26773 // |sticky_only| is set to true when propagating sticky user activation during
774 // cross-document navigations. The transient state remains unchanged.
danakjc492bf82020-09-09 20:02:44775 bool NotifyUserActivation(
Liam Brady38b84562024-03-07 22:11:26776 blink::mojom::UserActivationNotificationType notification_type,
777 bool sticky_only = false);
778
779 bool NotifyUserActivationStickyOnly();
danakjc492bf82020-09-09 20:02:44780
781 bool ConsumeTransientUserActivation();
782
783 bool ClearUserActivation();
784
785 // Verify that the renderer process is allowed to set user activation on this
786 // frame by checking whether this frame's RenderWidgetHost had previously seen
787 // an input event that might lead to user activation. If user activation
788 // should be allowed, this returns true and also clears corresponding pending
789 // user activation state in the widget. Otherwise, this returns false.
790 bool VerifyUserActivation();
791
Mingyu Lei7956b8b2023-07-24 08:24:08792 // See `RestartBackForwardCachedNavigationAsync()`.
793 void RestartBackForwardCachedNavigationImpl(int nav_entry_id);
794
Avi Drissmanbd153642024-09-03 18:58:05795 // The browser-global FrameTreeNodeId generator.
796 static FrameTreeNodeId::Generator frame_tree_node_id_generator_;
danakjc492bf82020-09-09 20:02:44797
Arthur Sonzognif6785ec2022-12-05 10:11:50798 // The FrameTree owning |this|. It can change with Prerender2 during
799 // activation.
800 raw_ref<FrameTree> frame_tree_;
danakjc492bf82020-09-09 20:02:44801
danakjc492bf82020-09-09 20:02:44802 // A browser-global identifier for the frame in the page, which stays stable
803 // even if the frame does a cross-process navigation.
Avi Drissmanbd153642024-09-03 18:58:05804 const FrameTreeNodeId frame_tree_node_id_;
danakjc492bf82020-09-09 20:02:44805
806 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
807 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52808 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44809
danakjc492bf82020-09-09 20:02:44810 // The frame that opened this frame, if any. Will be set to null if the
811 // opener is closed, or if this frame disowns its opener by setting its
812 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52813 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44814
815 // An observer that clears this node's |opener_| if the opener is destroyed.
816 // This observer is added to the |opener_|'s observer list when the |opener_|
817 // is set to a non-null node, and it is removed from that list when |opener_|
818 // changes or when this node is destroyed. It is also cleared if |opener_|
819 // is disowned.
820 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
821
Rakina Zata Amni3a48ae42022-05-05 03:39:56822 // Unlike `opener_`, the "original opener chain" doesn't reflect
823 // window.opener, which can be suppressed or updated. The "original opener"
824 // is the main frame of the actual opener of this frame. This traces the all
825 // the way back, so if the original opener was closed (deleted or severed due
826 // to COOP), but _it_ had an original opener, this will return the original
827 // opener's original opener, etc. So this value will always be set as long as
828 // there is at least one live frame in the chain whose connection is not
829 // severed due to COOP.
830 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
831 nullptr;
danakjc492bf82020-09-09 20:02:44832
Wolfgang Beyerd8809db2020-09-30 15:29:39833 // The devtools frame token of the frame which opened this frame. This is
834 // not cleared even if the opener is destroyed or disowns the frame.
Arthur Sonzognic686e8f2024-01-11 08:36:37835 std::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39836
Rakina Zata Amni3a48ae42022-05-05 03:39:56837 // An observer that updates this node's
838 // |first_live_main_frame_in_original_opener_chain_| to the next original
839 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44840 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
841
arthursonzogni034bb9c2020-10-01 08:29:56842 // When created by an opener, the URL specified in window.open(url)
843 // Please refer to {Get,Set}InitialPopupURL() documentation.
844 GURL initial_popup_url_;
845
846 // When created using window.open, the origin of the creator.
847 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
848 url::Origin popup_creator_origin_;
849
W. James MacLean81b8d01f2022-01-25 20:50:59850 // If the url from the the last BeginNavigation is about:srcdoc, this value
851 // stores the srcdoc_attribute's value for re-use in history navigations.
852 std::string srcdoc_value_;
853
Charlie Reis734db662024-01-11 18:20:03854 // Whether this frame is still on the initial about:blank document or the
855 // synchronously committed about:blank document committed at frame creation,
856 // and its "initial empty document"-ness is still true.
857 // This will be false if either of these has happened:
858 // - The current RenderFrameHost commits a cross-document navigation that is
859 // not the synchronously committed about:blank document per:
860 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
861 // - The document's input stream has been opened with document.open(), per
862 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
863 // NOTE: we treat both the "initial about:blank document" and the
864 // "synchronously committed about:blank document" as the initial empty
865 // document. In the future, we plan to remove the synchronous about:blank
866 // commit so that this state will only be true if the frame is on the
867 // "initial about:blank document". See also:
868 // - https://github.com/whatwg/html/issues/6863
869 // - https://crbug.com/1215096
870 //
871 // Note that cross-document navigations update this state at
872 // DidCommitNavigation() time. Thus, this is still true when a cross-document
873 // navigation from an initial empty document is in the pending-commit window,
874 // after sending the CommitNavigation IPC but before receiving
875 // DidCommitNavigation(). This is in contrast to
876 // has_committed_any_navigation(), which is updated in CommitNavigation().
877 // TODO(alexmos): Consider updating this at CommitNavigation() time as well to
878 // match the has_committed_any_navigation() behavior.
879 bool is_on_initial_empty_document_ = true;
880
danakjc492bf82020-09-09 20:02:44881 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19882 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44883
Daniel Cheng6ac128172021-05-25 18:49:01884 // The type of frame owner for this frame. This is only relevant for non-main
885 // frames.
Kevin McNee43fe8292021-10-04 22:59:41886 const blink::FrameOwnerElementType frame_owner_element_type_ =
887 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45888
Daniel Cheng6ac128172021-05-25 18:49:01889 // The tree scope type of frame owner element, i.e. whether the element is in
890 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
891 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
892 // relevant for non-main frames.
893 const blink::mojom::TreeScopeType tree_scope_type_ =
894 blink::mojom::TreeScopeType::kDocument;
895
Ming-Ying Chung0430c592025-01-21 00:33:10896 // Track the pending sandbox flags, container policy, and deferred fetch
897 // policy for this frame.
898 // When a parent frame dynamically updates 'sandbox', 'allow',
899 // 'allowfullscreen', 'allowpaymentrequest' or 'src' attributes, the updated
900 // policy for the frame is stored here, and transferred into
Harkiran Bolaria4eacb3a2021-12-13 20:03:47901 // render_manager_.current_replication_state().frame_policy when they take
902 // effect on the next frame navigation.
Ming-Ying Chung0430c592025-01-21 00:33:10903 //
904 // Note that updates to FramePolicy from the renderer side must be explicitly
905 // set in this field via `SetPendingFramePolicy()`; Otherwise, the browser
906 // side won't have it saved and can't pass it to new RenderFrameHost.
danakjc492bf82020-09-09 20:02:44907 blink::FramePolicy pending_frame_policy_;
908
909 // Whether the frame was created by javascript. This is useful to prune
910 // history entries when the frame is removed (because frames created by
911 // scripts are never recreated with the same unique name - see
912 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19913 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44914
danakjc492bf82020-09-09 20:02:44915 // Tracks the scrolling and margin properties for this frame. These
916 // properties affect the child renderer but are stored on its parent's
917 // frame element. When this frame's parent dynamically updates these
918 // properties, we update them here too.
919 //
920 // Note that dynamic updates only take effect on the next frame navigation.
921 blink::mojom::FrameOwnerProperties frame_owner_properties_;
922
Yuzu Saijo03dbf9b2022-07-22 04:29:45923 // Contains the tracked HTML attributes of the corresponding iframe element,
924 // such as 'id' and 'src'.
925 blink::mojom::IframeAttributesPtr attributes_;
Antonio Sartori5abc8de2021-07-13 08:42:47926
danakjc492bf82020-09-09 20:02:44927 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
928 // be reset and a RenderFrameHost will be responsible for the navigation.
929 std::unique_ptr<NavigationRequest> navigation_request_;
930
931 // List of objects observing this FrameTreeNode.
932 base::ObserverList<Observer>::Unchecked observers_;
933
934 base::TimeTicks last_focus_time_;
935
arthursonzogni9816b9192021-03-29 16:09:19936 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44937
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58938 const FencedFrameStatus fenced_frame_status_ =
939 FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57940
Garrett Tanzerc69f4642022-08-15 22:15:14941 // If this is a fenced frame resulting from a urn:uuid navigation, this
942 // contains all the metadata specifying the resulting context.
Alison Gale770f3fc2024-04-27 00:39:58943 // TODO(crbug.com/40202462): Move this into the FrameTree once ShadowDOM
Garrett Tanzer34cb92fe2022-09-28 17:50:54944 // and urn iframes are gone.
Arthur Sonzognic686e8f2024-01-11 08:36:37945 std::optional<FencedFrameProperties> fenced_frame_properties_;
Garrett Tanzerc69f4642022-08-15 22:15:14946
Mingyu Lei7956b8b2023-07-24 08:24:08947 // The tracker of the task that restarts the BFCache navigation. It might be
948 // used to cancel the task.
949 // See `CancelRestartingBackForwardCacheNavigation()`.
950 base::CancelableTaskTracker restart_back_forward_cached_navigation_tracker_;
951
Lukasz Anforowicz147141962020-12-16 18:03:24952 // Manages creation and swapping of RenderFrameHosts for this frame.
953 //
954 // This field needs to be declared last, because destruction of
955 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
956 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
957 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
958 // may try to use FrameTreeNode's fields above - this would be an undefined
959 // behavior if the fields (even trivially-destructible ones) were destructed
960 // before the RenderFrameHostManager's destructor runs. See also
961 // https://crbug.com/1157988.
962 RenderFrameHostManager render_manager_;
Mingyu Lei7956b8b2023-07-24 08:24:08963
964 base::WeakPtrFactory<FrameTreeNode> weak_factory_{this};
danakjc492bf82020-09-09 20:02:44965};
966
967} // namespace content
968
969#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_