blob: 04606c959134a68d0380818e6bf45c5813927ed9 [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>
11#include <string>
David Sanders2c1194d92022-04-19 23:32:3212#include <utility>
danakjc492bf82020-09-09 20:02:4413
14#include "base/gtest_prod_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
David Sanders2c1194d92022-04-19 23:32:3216#include "base/memory/scoped_refptr.h"
David Sandersd4bf5eb2022-03-17 07:12:0517#include "base/observer_list.h"
danakjc492bf82020-09-09 20:02:4418#include "content/browser/renderer_host/frame_tree.h"
Daniel Cheng390e2a72022-09-28 06:07:5319#include "content/browser/renderer_host/navigation_discard_reason.h"
danakjc492bf82020-09-09 20:02:4420#include "content/browser/renderer_host/navigator.h"
21#include "content/browser/renderer_host/render_frame_host_impl.h"
22#include "content/browser/renderer_host/render_frame_host_manager.h"
23#include "content/common/content_export.h"
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:5524#include "content/public/browser/frame_type.h"
danakjc492bf82020-09-09 20:02:4425#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Lei Zhang698df03c2021-05-21 04:23:3426#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin McNee43fe8292021-10-04 22:59:4127#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4428#include "third_party/blink/public/common/frame/frame_policy.h"
29#include "third_party/blink/public/common/frame/user_activation_state.h"
danakjc492bf82020-09-09 20:02:4430#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3731#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0132#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
David Sanders2c1194d92022-04-19 23:32:3233#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom-forward.h"
danakjc492bf82020-09-09 20:02:4434
Gabriel Charetted87f10f2022-03-31 00:44:2235#include "base/time/time.h"
danakjc492bf82020-09-09 20:02:4436#include "url/gurl.h"
37#include "url/origin.h"
38
39namespace content {
40
41class NavigationRequest;
42class RenderFrameHostImpl;
43class NavigationEntryImpl;
44
45// When a page contains iframes, its renderer process maintains a tree structure
46// of those frames. We are mirroring this tree in the browser process. This
47// class represents a node in this tree and is a wrapper for all objects that
48// are frame-specific (as opposed to page-specific).
49//
50// Each FrameTreeNode has a current RenderFrameHost, which can change over
51// time as the frame is navigated. Any immediate subframes of the current
52// document are tracked using FrameTreeNodes owned by the current
53// RenderFrameHost, rather than as children of FrameTreeNode itself. This
54// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
55// still alive - for example while pending deletion, after a new current
56// RenderFrameHost has replaced it.
57class CONTENT_EXPORT FrameTreeNode {
58 public:
59 class Observer {
60 public:
61 // Invoked when a FrameTreeNode is being destroyed.
62 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
63
64 // Invoked when a FrameTreeNode becomes focused.
65 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
66
Arthur Hemerye4659282022-03-28 08:36:1567 // Invoked when a FrameTreeNode moves to a different BrowsingInstance and
68 // the popups it opened should be disowned.
69 virtual void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) {}
70
Fergal Dalya1d569972021-03-16 03:24:5371 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4472 };
73
74 static const int kFrameTreeNodeInvalidId;
75
76 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
77 // regardless of which FrameTree it is in.
78 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
79
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(
87 FrameTree* frame_tree,
88 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,
91 const base::UnguessableToken& devtools_frame_token,
92 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4193 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3494 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4495
Peter Boström828b9022021-09-21 02:28:4396 FrameTreeNode(const FrameTreeNode&) = delete;
97 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
98
danakjc492bf82020-09-09 20:02:4499 ~FrameTreeNode();
100
101 void AddObserver(Observer* observer);
102 void RemoveObserver(Observer* observer);
103
Ian Vollick25a9d032022-04-12 23:20:17104 // Frame trees may be nested so it can be the case that IsMainFrame() is true,
105 // but is not the outermost main frame. In particular, !IsMainFrame() cannot
106 // be used to check if the frame is an embedded frame -- use
107 // !IsOutermostMainFrame() instead. NB: this does not escape guest views;
108 // IsOutermostMainFrame will be true for the outermost main frame in an inner
109 // guest view.
danakjc492bf82020-09-09 20:02:44110 bool IsMainFrame() const;
Ian Vollick25a9d032022-04-12 23:20:17111 bool IsOutermostMainFrame();
danakjc492bf82020-09-09 20:02:44112
arthursonzogni76098e52020-11-25 14:18:45113 // Clears any state in this node which was set by the document itself (CSP &
114 // UserActivationState) and notifies proxies as appropriate. Invoked after
115 // committing navigation to a new document (since the new document comes with
116 // a fresh set of CSP).
117 // TODO(arthursonzogni): Remove this function. The frame/document must not be
118 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13119 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44120
121 FrameTree* frame_tree() const { return frame_tree_; }
122 Navigator& navigator() { return frame_tree()->navigator(); }
123
124 RenderFrameHostManager* render_manager() { return &render_manager_; }
Alexander Timin33e2e2c12022-03-03 04:21:33125 const RenderFrameHostManager* render_manager() const {
126 return &render_manager_;
127 }
danakjc492bf82020-09-09 20:02:44128 int frame_tree_node_id() const { return frame_tree_node_id_; }
Yuzu Saijo03dbf9b2022-07-22 04:29:45129 // This reflects window.name, which is initially set to the the "name"
130 // attribute. But this won't reflect changes of 'name' attribute and instead
131 // reflect changes to the Window object's name property.
132 // This is different from IframeAttributes' name in that this will not get
133 // updated when 'name' attribute gets updated.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47134 const std::string& frame_name() const {
135 return render_manager_.current_replication_state().name;
136 }
danakjc492bf82020-09-09 20:02:44137
138 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47139 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44140 }
141
142 // See comment on the member declaration.
143 const base::UnguessableToken& devtools_frame_token() const {
144 return devtools_frame_token_;
145 }
146
Andrey Kosyakov6e82c3b2022-09-26 22:43:46147 // This may only change when an RFH changes host frame tree nodes
148 // during prerender activation.
149 // TODO(caseq,dsv): see if we can get rid of it.
150 void set_devtools_frame_token(const base::UnguessableToken& token) {
151 devtools_frame_token_ = token;
152 }
danakjc492bf82020-09-09 20:02:44153 size_t child_count() const { return current_frame_host()->child_count(); }
154
danakjc492bf82020-09-09 20:02:44155 RenderFrameHostImpl* parent() const { return parent_; }
156
Dave Tapuskac8de3b02021-12-03 21:51:01157 // See `RenderFrameHost::GetParentOrOuterDocument()` for
158 // documentation.
159 RenderFrameHostImpl* GetParentOrOuterDocument();
160
161 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
162 // documentation.
163 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
164
danakjc492bf82020-09-09 20:02:44165 FrameTreeNode* opener() const { return opener_; }
166
Rakina Zata Amni3a48ae42022-05-05 03:39:56167 FrameTreeNode* first_live_main_frame_in_original_opener_chain() const {
168 return first_live_main_frame_in_original_opener_chain_;
169 }
danakjc492bf82020-09-09 20:02:44170
Anton Bikineevf62d1bf2021-05-15 17:56:07171 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39172 return opener_devtools_frame_token_;
173 }
174
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55175 // Returns the type of the frame. Refer to frame_type.h for the details.
176 FrameType GetFrameType() const;
177
danakjc492bf82020-09-09 20:02:44178 // Assigns a new opener for this node and, if |opener| is non-null, registers
179 // an observer that will clear this node's opener if |opener| is ever
180 // destroyed.
181 void SetOpener(FrameTreeNode* opener);
182
183 // Assigns the initial opener for this node, and if |opener| is non-null,
184 // registers an observer that will clear this node's opener if |opener| is
185 // ever destroyed. The value set here is the root of the tree.
186 //
187 // It is not possible to change the opener once it was set.
188 void SetOriginalOpener(FrameTreeNode* opener);
189
Wolfgang Beyerd8809db2020-09-30 15:29:39190 // Assigns an opener frame id for this node. This string id is only set once
191 // and cannot be changed. It persists, even if the |opener| is destroyed. It
192 // is used for attribution in the DevTools frontend.
193 void SetOpenerDevtoolsFrameToken(
194 base::UnguessableToken opener_devtools_frame_token);
195
danakjc492bf82020-09-09 20:02:44196 FrameTreeNode* child_at(size_t index) const {
197 return current_frame_host()->child_at(index);
198 }
199
200 // Returns the URL of the last committed page in the current frame.
201 const GURL& current_url() const {
202 return current_frame_host()->GetLastCommittedURL();
203 }
204
Rakina Zata Amni90555282022-01-21 07:35:54205 // Sets `is_on_initial_empty_document_` to false.
206 void SetNotOnInitialEmptyDocument() { is_on_initial_empty_document_ = false; }
Rakina Zata Amni86c88fa2021-11-01 01:27:30207
Rakina Zata Amni91d485b42021-12-08 02:50:13208 // Returns false if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30209 // empty document, or if the current document's input stream has been opened
210 // with document.open(), causing the document to lose its "initial empty
211 // document" status. For more details, see the definition of
212 // `is_on_initial_empty_document_`.
213 bool is_on_initial_empty_document() const {
214 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56215 }
216
Rakina Zata Amni86c88fa2021-11-01 01:27:30217 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56218 // false. Must only be called after the current document's input stream has
219 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30220 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14221
danakjc492bf82020-09-09 20:02:44222 // Returns whether the frame's owner element in the parent document is
223 // collapsed, that is, removed from the layout as if it did not exist, as per
224 // request by the embedder (of the content/ layer).
225 bool is_collapsed() const { return is_collapsed_; }
226
227 // Sets whether to collapse the frame's owner element in the parent document,
228 // that is, to remove it from the layout as if it did not exist, as per
229 // request by the embedder (of the content/ layer). Cannot be called for main
230 // frames.
231 //
232 // This only has an effect for <iframe> owner elements, and is a no-op when
233 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
234 void SetCollapsed(bool collapsed);
235
236 // Returns the origin of the last committed page in this frame.
237 // WARNING: To get the last committed origin for a particular
238 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
239 // which will behave correctly even when the RenderFrameHost is not the
240 // current one for this frame (such as when it's pending deletion).
241 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47242 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44243 }
244
danakjc492bf82020-09-09 20:02:44245 // Returns the latest frame policy (sandbox flags and container policy) for
246 // this frame. This includes flags inherited from parent frames and the latest
247 // flags from the <iframe> element hosting this frame. The returned policies
248 // may not yet have taken effect, since "sandbox" and "allow" attribute
249 // updates in an <iframe> element take effect on next navigation. To retrieve
250 // the currently active policy for this frame, use effective_frame_policy().
251 const blink::FramePolicy& pending_frame_policy() const {
252 return pending_frame_policy_;
253 }
254
255 // Update this frame's sandbox flags and container policy. This is called
256 // when a parent frame updates the "sandbox" attribute in the <iframe> element
257 // for this frame, or any of the attributes which affect the container policy
258 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
259 // These policies won't take effect until next navigation. If this frame's
260 // parent is itself sandboxed, the parent's sandbox flags are combined with
261 // those in |frame_policy|.
262 // Attempting to change the container policy on the main frame will have no
263 // effect.
264 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
265
266 // Returns the currently active frame policy for this frame, including the
267 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39268 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44269 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
270 // origin of the iframe's src attribute (which may be different from the URL
271 // of the document currently loaded into the frame). This does not include
272 // policy changes that have been made by updating the containing iframe
273 // element attributes since the frame was last navigated; use
274 // pending_frame_policy() for those.
275 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47276 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44277 }
278
danakjc492bf82020-09-09 20:02:44279 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
280 return frame_owner_properties_;
281 }
282
283 void set_frame_owner_properties(
284 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
285 frame_owner_properties_ = frame_owner_properties;
286 }
287
Yuzu Saijo03dbf9b2022-07-22 04:29:45288 // Reflects the attributes of the corresponding iframe html element, such
289 // as 'anonymous', 'id', 'name' and 'src'. These values should not be
290 // exposed to cross-origin renderers.
291 const network::mojom::ContentSecurityPolicy* csp_attribute() const {
292 return attributes_->parsed_csp_attribute.get();
danakjc492bf82020-09-09 20:02:44293 }
Yuzu Saijo03dbf9b2022-07-22 04:29:45294 bool anonymous() const { return attributes_->anonymous; }
295 const std::string& html_id() const { return attributes_->id; }
296 // This tracks iframe's 'name' attribute instead of window.name, which is
297 // tracked in FrameReplicationState. See the comment for frame_name() for
298 // more details.
299 const std::string& html_name() const { return attributes_->name; }
300 const std::string& html_src() const { return attributes_->src; }
danakjc492bf82020-09-09 20:02:44301
Yuzu Saijo03dbf9b2022-07-22 04:29:45302 void SetAttributes(blink::mojom::IframeAttributesPtr attributes);
Antonio Sartori5abc8de2021-07-13 08:42:47303
danakjc492bf82020-09-09 20:02:44304 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47305 return render_manager_.current_replication_state().origin.IsSameOriginWith(
306 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44307 }
308
Gyuyoung Kimc16e52e92021-03-19 02:45:37309 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47310 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44311 }
312
313 RenderFrameHostImpl* current_frame_host() const {
314 return render_manager_.current_frame_host();
315 }
316
danakjc492bf82020-09-09 20:02:44317 // Returns true if this node is in a loading state.
318 bool IsLoading() const;
319
Alex Moshchuk9b0fd822020-10-26 23:08:15320 // Returns true if this node has a cross-document navigation in progress.
321 bool HasPendingCrossDocumentNavigation() const;
322
danakjc492bf82020-09-09 20:02:44323 NavigationRequest* navigation_request() { return navigation_request_.get(); }
324
325 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
326 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
327 // RenderFrameHost that is committing the navigation.
328 void TransferNavigationRequestOwnership(
329 RenderFrameHostImpl* render_frame_host);
330
331 // Takes ownership of |navigation_request| and makes it the current
332 // NavigationRequest of this frame. This corresponds to the start of a new
333 // navigation. If there was an ongoing navigation request before calling this
334 // function, it is canceled. |navigation_request| should not be null.
335 void CreatedNavigationRequest(
336 std::unique_ptr<NavigationRequest> navigation_request);
337
Daniel Cheng390e2a72022-09-28 06:07:53338 // Resets the current navigation request and any state created by it,
339 // including the speculative RenderFrameHost.
340 void ResetNavigationRequest(NavigationDiscardReason reason);
341
342 // Resets the current navigation request, but keeping state created by the
343 // NavigationRequest (e.g. speculative RenderFrameHost, loading state).
344 void ResetNavigationRequestButKeepState();
danakjc492bf82020-09-09 20:02:44345
346 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19347 // |should_show_loading_ui| indicates whether this navigation should be
348 // visible in the UI. True for cross-document navigations and navigations
Nate Chapin24295252022-09-27 18:31:08349 // intercepted by the navigation API's intercept().
danakjc492bf82020-09-09 20:02:44350 // |was_previously_loading| is false if the FrameTree was not loading before.
351 // The caller is required to provide this boolean as the delegate should only
352 // be notified if the FrameTree went from non-loading to loading state.
353 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19354 void DidStartLoading(bool should_show_loading_ui,
355 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44356
357 // A RenderFrameHost in this node stopped loading.
358 void DidStopLoading();
359
360 // The load progress for a RenderFrameHost in this node was updated to
361 // |load_progress|. This will notify the FrameTree which will in turn notify
362 // the WebContents.
363 void DidChangeLoadProgress(double load_progress);
364
365 // Called when the user directed the page to stop loading. Stops all loads
366 // happening in the FrameTreeNode. This method should be used with
367 // FrameTree::ForEach to stop all loads in the entire FrameTree.
368 bool StopLoading();
369
370 // Returns the time this frame was last focused.
371 base::TimeTicks last_focus_time() const { return last_focus_time_; }
372
373 // Called when this node becomes focused. Updates the node's last focused
374 // time and notifies observers.
375 void DidFocus();
376
377 // Called when the user closed the modal dialogue for BeforeUnload and
378 // cancelled the navigation. This should stop any load happening in the
379 // FrameTreeNode.
380 void BeforeUnloadCanceled();
381
danakjc492bf82020-09-09 20:02:44382 // Updates the user activation state in the browser frame tree and in the
383 // frame trees in all renderer processes except the renderer for this node
384 // (which initiated the update). Returns |false| if the update tries to
385 // consume an already consumed/expired transient state, |true| otherwise. See
386 // the comment on user_activation_state_ below.
387 //
388 // The |notification_type| parameter is used for histograms, only for the case
389 // |update_state == kNotifyActivation|.
390 bool UpdateUserActivationState(
391 blink::mojom::UserActivationUpdateType update_type,
392 blink::mojom::UserActivationNotificationType notification_type);
393
danakjc492bf82020-09-09 20:02:44394 // Returns the sandbox flags currently in effect for this frame. This includes
395 // flags inherited from parent frames, the currently active flags from the
396 // <iframe> element hosting this frame, as well as any flags set from a
397 // Content-Security-Policy HTTP header. This does not include flags that have
398 // have been updated in an <iframe> element but have not taken effect yet; use
399 // pending_frame_policy() for those. To see the flags which will take effect
400 // on navigation (which does not include the CSP-set flags), use
401 // effective_frame_policy().
402 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47403 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44404 }
405
danakjc492bf82020-09-09 20:02:44406 // Returns whether the frame received a user gesture on a previous navigation
407 // on the same eTLD+1.
408 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47409 return render_manager_.current_replication_state()
410 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44411 }
412
413 // When a tab is discarded, WebContents sets was_discarded on its
414 // root FrameTreeNode.
415 // In addition, when a child frame is created, this bit is passed on from
416 // parent to child.
417 // When a navigation request is created, was_discarded is passed on to the
418 // request and reset to false in FrameTreeNode.
419 void set_was_discarded() { was_discarded_ = true; }
420 bool was_discarded() const { return was_discarded_; }
421
422 // Returns the sticky bit of the User Activation v2 state of the
423 // |FrameTreeNode|.
424 bool HasStickyUserActivation() const {
425 return user_activation_state_.HasBeenActive();
426 }
427
428 // Returns the transient bit of the User Activation v2 state of the
429 // |FrameTreeNode|.
430 bool HasTransientUserActivation() {
431 return user_activation_state_.IsActive();
432 }
433
434 // Remove history entries for all frames created by script in this frame's
435 // subtree. If a frame created by a script is removed, then its history entry
436 // will never be reused - this saves memory.
437 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
438
Abhijeet Kandalkarb43affa72022-09-27 16:48:01439 using FencedFrameStatus = RenderFrameHostImpl::FencedFrameStatus;
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58440 FencedFrameStatus fenced_frame_status() const { return fenced_frame_status_; }
441
Kevin McNee43fe8292021-10-04 22:59:41442 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45443 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44444 }
danakjc492bf82020-09-09 20:02:44445
Daniel Cheng6ac128172021-05-25 18:49:01446 blink::mojom::TreeScopeType tree_scope_type() const {
447 return tree_scope_type_;
448 }
449
arthursonzogni034bb9c2020-10-01 08:29:56450 // The initial popup URL for new window opened using:
451 // `window.open(initial_popup_url)`.
452 // An empty GURL otherwise.
453 //
454 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
455 // document served from this URL. The FrameTreeNode always starts hosting the
456 // initial empty document and attempts a navigation toward this URL. However
457 // the navigation might be delayed, redirected and even cancelled.
458 void SetInitialPopupURL(const GURL& initial_popup_url);
459 const GURL& initial_popup_url() const { return initial_popup_url_; }
460
461 // The origin of the document that used window.open() to create this frame.
462 // Otherwise, an opaque Origin with a nonce different from all previously
463 // existing Origins.
464 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
465 const url::Origin& popup_creator_origin() const {
466 return popup_creator_origin_;
467 }
468
Harkiran Bolaria59290d62021-03-17 01:53:01469 // Sets the associated FrameTree for this node. The node can change FrameTrees
470 // when blink::features::Prerender2 is enabled, which allows a page loaded in
471 // the prerendered FrameTree to be used for a navigation in the primary frame
472 // tree.
473 void SetFrameTree(FrameTree& frame_tree);
474
Alexander Timin074cd182022-03-23 18:11:22475 using TraceProto = perfetto::protos::pbzero::FrameTreeNodeInfo;
Alexander Timinf785f342021-03-18 00:00:56476 // Write a representation of this object into a trace.
Alexander Timin074cd182022-03-23 18:11:22477 void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
Alexander Timinf785f342021-03-18 00:00:56478
Carlos Caballero76711352021-03-24 17:38:21479 // Returns true the node is navigating, i.e. it has an associated
480 // NavigationRequest.
481 bool HasNavigation();
482
shivanigithubf3ddff52021-07-03 22:06:30483 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30484 // Note that these two functions cannot be invoked from a FrameTree's or
485 // its root node's constructor since they require the frame tree and the
486 // root node to be completely constructed.
487 //
shivanigithubf3ddff52021-07-03 22:06:30488 // Returns false if fenced frames are disabled. Returns true if the feature is
489 // enabled and if |this| is a fenced frame. Returns false for
490 // iframes embedded in a fenced frame. To clarify: for the MPArch
491 // implementation this only returns true if |this| is the actual
492 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
493 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36494 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30495
496 // Returns false if fenced frames are disabled. Returns true if the
497 // feature is enabled and if |this| or any of its ancestor nodes is a
498 // fenced frame.
499 bool IsInFencedFrameTree() const;
500
shivanigithub4cd016a2021-09-20 21:10:30501 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
Garrett Tanzer34cb92fe2022-09-28 17:50:54502 // Returns nullopt otherwise.
503 //
504 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
505 // frame and any iframes nested within it. Not set if this frame is not in a
506 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
507 // the MPArch version but for the shadow DOM version we need to keep it here
508 // since the fenced frame root is not a main frame for the latter. The value
509 // of the nonce will be the same for all of the the iframes inside a fenced
510 // frame tree. If there is a nested fenced frame it will have a different
511 // nonce than its parent fenced frame. The nonce will stay the same across
512 // navigations initiated from the fenced frame tree because it is always used
513 // in conjunction with other fields of the keys and would be good to access
514 // the same storage across same-origin navigations. If the navigation is
515 // same-origin/site then the same network stack partition/storage will be
516 // reused and if it's cross-origin/site then other parts of the key will
517 // change and so, even with the same nonce, another partition will be used.
518 // But if the navigation is initiated from the embedder, the nonce will be
519 // reinitialized irrespective of same or cross origin such that there is no
520 // privacy leak via storage shared between two embedder initiated navigations.
521 // Note that this reinitialization is implemented for all embedder-initiated
522 // navigations in MPArch, but only urn:uuid navigations in ShadowDOM.
523 absl::optional<base::UnguessableToken> GetFencedFrameNonce();
shivanigithub4cd016a2021-09-20 21:10:30524
Garrett Tanzer34cb92fe2022-09-28 17:50:54525 // If applicable, initialize the default fenced frame properties. Right now,
526 // this means setting a new fenced frame nonce. See comment on
shivanigithub4cd016a2021-09-20 21:10:30527 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
528 // by FrameTree::Init() or FrameTree::AddFrame().
Garrett Tanzer34cb92fe2022-09-28 17:50:54529 void SetFencedFramePropertiesIfNeeded();
shivanigithub4cd016a2021-09-20 21:10:30530
Garrett Tanzera42fdef2022-06-13 16:09:14531 // Returns the mode attribute set on the fenced frame root if this frame is
532 // in a fenced frame tree, otherwise returns `absl::nullopt`.
Nan Line376738a2022-03-25 22:05:41533 absl::optional<blink::mojom::FencedFrameMode> GetFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16534
Dave Tapuskac8de3b02021-12-03 21:51:01535 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
536 // Do not use directly.
537 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
538
Harkiran Bolariab4437fd2021-08-11 17:51:22539 // Sets the unique_name and name fields on replication_state_. To be used in
540 // prerender activation to make sure the FrameTreeNode replication state is
541 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
542 // renderers should already have the correct value, so unlike
543 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47544 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
545 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22546 void set_frame_name_for_activation(const std::string& unique_name,
547 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40548 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
549 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22550 }
551
Nan Linaaf84f72021-12-02 22:31:56552 // Returns true if error page isolation is enabled.
553 bool IsErrorPageIsolationEnabled() const;
554
W. James MacLean81b8d01f2022-01-25 20:50:59555 // Functions to store and retrieve a frame's srcdoc value on this
556 // FrameTreeNode.
557 void SetSrcdocValue(const std::string& srcdoc_value);
558 const std::string& srcdoc_value() const { return srcdoc_value_; }
559
Garrett Tanzerc69f4642022-08-15 22:15:14560 void set_fenced_frame_properties(
561 absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
562 fenced_frame_properties) {
Garrett Tanzer2975eeac2022-08-22 16:34:01563 // TODO(crbug.com/1262022): Reenable this DCHECK once ShadowDOM and
564 // loading urns in iframes (for FLEDGE OT) are gone.
565 // DCHECK_EQ(fenced_frame_status_,
566 // RenderFrameHostImpl::FencedFrameStatus::kFencedFrameRoot);
Garrett Tanzerc69f4642022-08-15 22:15:14567 fenced_frame_properties_ = fenced_frame_properties;
568 }
569
Garrett Tanzer34cb92fe2022-09-28 17:50:54570 // Return the fenced frame properties for this fenced frame tree (if any).
571 // That is to say, this function returns the `fenced_frame_properties_`
572 // variable attached to the fenced frame root FrameTreeNode, which may be
573 // either this node or an ancestor of it.
Garrett Tanzerc69f4642022-08-15 22:15:14574 const absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
Garrett Tanzer34cb92fe2022-09-28 17:50:54575 GetFencedFrameProperties();
Garrett Tanzerc69f4642022-08-15 22:15:14576
Yao Xiao1ac702d2022-06-08 17:20:49577 // Traverse up from this node. The `shared_storage_budget_metadata()` of the
578 // first seen node with a non-null budget metadata will be returned (i.e. this
579 // node inherits that budget metadata), and this node is expected to be an
580 // outermost fenced frame root. Return nullptr if not found (i.e. this node is
581 // not subjected to shared storage budgeting).
Garrett Tanzer2975eeac2022-08-22 16:34:01582 absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49583 FindSharedStorageBudgetMetadata();
584
Harkiran Bolariaebbe7702022-02-22 19:19:03585 // Accessor to BrowsingContextState for subframes only. Only main frame
586 // navigations can change BrowsingInstances and BrowsingContextStates,
587 // therefore for subframes associated BrowsingContextState never changes. This
588 // helper method makes this more explicit and guards against calling this on
589 // main frames (there an appropriate BrowsingContextState should be obtained
590 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
591 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
592 // the same frame).
593 const scoped_refptr<BrowsingContextState>&
594 GetBrowsingContextStateForSubframe() const;
595
Arthur Hemerye4659282022-03-28 08:36:15596 // Clears the opener property of popups referencing this FrameTreeNode as
597 // their opener.
598 void ClearOpenerReferences();
599
Liam Bradyd2a41e152022-07-19 13:58:48600 // Calculates whether one of the ancestor frames or this frame has a CSPEE
601 // in place. This is eventually sent over to LocalFrame in the renderer where
602 // it will be used by HTMLFencedFrameElement::canLoadOpaqueURL for information
603 // it can't get on its own.
604 bool AncestorOrSelfHasCSPEE() const;
605
danakjc492bf82020-09-09 20:02:44606 private:
Yuzu Saijo03dbf9b2022-07-22 04:29:45607 friend class CSPEmbeddedEnforcementUnitTest;
Charlie Hubb5943d2021-03-09 19:46:12608 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44609 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12610 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44611 ContainerPolicySandboxDynamic);
Yuzu Saijo03dbf9b2022-07-22 04:29:45612 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest, StorageKeyToCommit);
613 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest,
614 NavigationToAnonymousDocumentNetworkIsolationInfo);
615 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplTest,
616 ChildOfAnonymousIsAnonymous);
Yifan Luo86a79f42022-08-16 18:38:27617 FRIEND_TEST_ALL_PREFIXES(ContentPasswordManagerDriverTest,
618 PasswordAutofillDisabledOnAnonymousIframe);
danakjc492bf82020-09-09 20:02:44619
Dominic Farolino8a2187b2021-12-24 20:44:21620 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
621 // representing an inner FrameTree, this method destroys said inner FrameTree.
622 void DestroyInnerFrameTreeIfExists();
623
danakjc492bf82020-09-09 20:02:44624 class OpenerDestroyedObserver;
625
danakjc492bf82020-09-09 20:02:44626 // The |notification_type| parameter is used for histograms only.
627 bool NotifyUserActivation(
628 blink::mojom::UserActivationNotificationType notification_type);
629
630 bool ConsumeTransientUserActivation();
631
632 bool ClearUserActivation();
633
634 // Verify that the renderer process is allowed to set user activation on this
635 // frame by checking whether this frame's RenderWidgetHost had previously seen
636 // an input event that might lead to user activation. If user activation
637 // should be allowed, this returns true and also clears corresponding pending
638 // user activation state in the widget. Otherwise, this returns false.
639 bool VerifyUserActivation();
640
641 // The next available browser-global FrameTreeNode ID.
642 static int next_frame_tree_node_id_;
643
644 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52645 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44646
danakjc492bf82020-09-09 20:02:44647 // A browser-global identifier for the frame in the page, which stays stable
648 // even if the frame does a cross-process navigation.
649 const int frame_tree_node_id_;
650
651 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
652 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52653 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44654
danakjc492bf82020-09-09 20:02:44655 // The frame that opened this frame, if any. Will be set to null if the
656 // opener is closed, or if this frame disowns its opener by setting its
657 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52658 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44659
660 // An observer that clears this node's |opener_| if the opener is destroyed.
661 // This observer is added to the |opener_|'s observer list when the |opener_|
662 // is set to a non-null node, and it is removed from that list when |opener_|
663 // changes or when this node is destroyed. It is also cleared if |opener_|
664 // is disowned.
665 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
666
Rakina Zata Amni3a48ae42022-05-05 03:39:56667 // Unlike `opener_`, the "original opener chain" doesn't reflect
668 // window.opener, which can be suppressed or updated. The "original opener"
669 // is the main frame of the actual opener of this frame. This traces the all
670 // the way back, so if the original opener was closed (deleted or severed due
671 // to COOP), but _it_ had an original opener, this will return the original
672 // opener's original opener, etc. So this value will always be set as long as
673 // there is at least one live frame in the chain whose connection is not
674 // severed due to COOP.
675 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
676 nullptr;
danakjc492bf82020-09-09 20:02:44677
Wolfgang Beyerd8809db2020-09-30 15:29:39678 // The devtools frame token of the frame which opened this frame. This is
679 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07680 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39681
Rakina Zata Amni3a48ae42022-05-05 03:39:56682 // An observer that updates this node's
683 // |first_live_main_frame_in_original_opener_chain_| to the next original
684 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44685 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
686
arthursonzogni034bb9c2020-10-01 08:29:56687 // When created by an opener, the URL specified in window.open(url)
688 // Please refer to {Get,Set}InitialPopupURL() documentation.
689 GURL initial_popup_url_;
690
691 // When created using window.open, the origin of the creator.
692 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
693 url::Origin popup_creator_origin_;
694
W. James MacLean81b8d01f2022-01-25 20:50:59695 // If the url from the the last BeginNavigation is about:srcdoc, this value
696 // stores the srcdoc_attribute's value for re-use in history navigations.
697 std::string srcdoc_value_;
698
Rakina Zata Amni86c88fa2021-11-01 01:27:30699 // Whether this frame is still on the initial about:blank document or the
700 // synchronously committed about:blank document committed at frame creation,
701 // and its "initial empty document"-ness is still true.
702 // This will be false if either of these has happened:
Arthur Sonzogni47c79cc2022-08-30 15:25:27703 // - The current RenderFrameHost commits a cross-document navigation that is
704 // not the synchronously committed about:blank document per:
Rakina Zata Amni86c88fa2021-11-01 01:27:30705 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
706 // - The document's input stream has been opened with document.open(), per
707 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
708 // NOTE: we treat both the "initial about:blank document" and the
709 // "synchronously committed about:blank document" as the initial empty
710 // document. In the future, we plan to remove the synchronous about:blank
711 // commit so that this state will only be true if the frame is on the
712 // "initial about:blank document". See also:
713 // - https://github.com/whatwg/html/issues/6863
714 // - https://crbug.com/1215096
715 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56716
danakjc492bf82020-09-09 20:02:44717 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19718 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44719
Daniel Cheng6ac128172021-05-25 18:49:01720 // The type of frame owner for this frame. This is only relevant for non-main
721 // frames.
Kevin McNee43fe8292021-10-04 22:59:41722 const blink::FrameOwnerElementType frame_owner_element_type_ =
723 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45724
Daniel Cheng6ac128172021-05-25 18:49:01725 // The tree scope type of frame owner element, i.e. whether the element is in
726 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
727 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
728 // relevant for non-main frames.
729 const blink::mojom::TreeScopeType tree_scope_type_ =
730 blink::mojom::TreeScopeType::kDocument;
731
danakjc492bf82020-09-09 20:02:44732 // Track the pending sandbox flags and container policy for this frame. When a
733 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
734 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47735 // is stored here, and transferred into
736 // render_manager_.current_replication_state().frame_policy when they take
737 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44738 blink::FramePolicy pending_frame_policy_;
739
740 // Whether the frame was created by javascript. This is useful to prune
741 // history entries when the frame is removed (because frames created by
742 // scripts are never recreated with the same unique name - see
743 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19744 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44745
746 // Used for devtools instrumentation and trace-ability. The token is
747 // propagated to Blink's LocalFrame and both Blink and content/
748 // can tag calls and requests with this token in order to attribute them
749 // to the context frame.
750 // |devtools_frame_token_| is only defined by the browser process and is never
751 // sent back from the renderer in the control calls. It should be never used
752 // to look up the FrameTreeNode instance.
Andrey Kosyakov6e82c3b2022-09-26 22:43:46753 base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44754
755 // Tracks the scrolling and margin properties for this frame. These
756 // properties affect the child renderer but are stored on its parent's
757 // frame element. When this frame's parent dynamically updates these
758 // properties, we update them here too.
759 //
760 // Note that dynamic updates only take effect on the next frame navigation.
761 blink::mojom::FrameOwnerProperties frame_owner_properties_;
762
Yuzu Saijo03dbf9b2022-07-22 04:29:45763 // Contains the tracked HTML attributes of the corresponding iframe element,
764 // such as 'id' and 'src'.
765 blink::mojom::IframeAttributesPtr attributes_;
Antonio Sartori5abc8de2021-07-13 08:42:47766
danakjc492bf82020-09-09 20:02:44767 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
768 // be reset and a RenderFrameHost will be responsible for the navigation.
769 std::unique_ptr<NavigationRequest> navigation_request_;
770
771 // List of objects observing this FrameTreeNode.
772 base::ObserverList<Observer>::Unchecked observers_;
773
774 base::TimeTicks last_focus_time_;
775
arthursonzogni9816b9192021-03-29 16:09:19776 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44777
778 // The user activation state of the current frame. See |UserActivationState|
779 // for details on how this state is maintained.
780 blink::UserActivationState user_activation_state_;
781
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58782 const FencedFrameStatus fenced_frame_status_ =
783 FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57784
Garrett Tanzerc69f4642022-08-15 22:15:14785 // If this is a fenced frame resulting from a urn:uuid navigation, this
786 // contains all the metadata specifying the resulting context.
Garrett Tanzer34cb92fe2022-09-28 17:50:54787 // TODO(crbug.com/1262022): Move this into the FrameTree once ShadowDOM
788 // and urn iframes are gone.
Garrett Tanzerc69f4642022-08-15 22:15:14789 absl::optional<FencedFrameURLMapping::FencedFrameProperties>
790 fenced_frame_properties_;
791
Lukasz Anforowicz147141962020-12-16 18:03:24792 // Manages creation and swapping of RenderFrameHosts for this frame.
793 //
794 // This field needs to be declared last, because destruction of
795 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
796 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
797 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
798 // may try to use FrameTreeNode's fields above - this would be an undefined
799 // behavior if the fields (even trivially-destructible ones) were destructed
800 // before the RenderFrameHostManager's destructor runs. See also
801 // https://crbug.com/1157988.
802 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44803};
804
805} // namespace content
806
807#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_