blob: 712de3f8769260bf1dc33d1241c227baaa187c99 [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`.
502 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
503 // details.
504 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
505 return fenced_frame_nonce_;
506 }
507
508 // If applicable, set the fenced frame nonce. See comment on
509 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
510 // by FrameTree::Init() or FrameTree::AddFrame().
511 void SetFencedFrameNonceIfNeeded();
512
Garrett Tanzera42fdef2022-06-13 16:09:14513 // Returns the mode attribute set on the fenced frame root if this frame is
514 // in a fenced frame tree, otherwise returns `absl::nullopt`.
Nan Line376738a2022-03-25 22:05:41515 absl::optional<blink::mojom::FencedFrameMode> GetFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16516
Dave Tapuskac8de3b02021-12-03 21:51:01517 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
518 // Do not use directly.
519 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
520
Harkiran Bolariab4437fd2021-08-11 17:51:22521 // Sets the unique_name and name fields on replication_state_. To be used in
522 // prerender activation to make sure the FrameTreeNode replication state is
523 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
524 // renderers should already have the correct value, so unlike
525 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47526 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
527 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22528 void set_frame_name_for_activation(const std::string& unique_name,
529 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40530 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
531 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22532 }
533
Nan Linaaf84f72021-12-02 22:31:56534 // Returns true if error page isolation is enabled.
535 bool IsErrorPageIsolationEnabled() const;
536
W. James MacLean81b8d01f2022-01-25 20:50:59537 // Functions to store and retrieve a frame's srcdoc value on this
538 // FrameTreeNode.
539 void SetSrcdocValue(const std::string& srcdoc_value);
540 const std::string& srcdoc_value() const { return srcdoc_value_; }
541
Garrett Tanzerc69f4642022-08-15 22:15:14542 void set_fenced_frame_properties(
543 absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
544 fenced_frame_properties) {
Garrett Tanzer2975eeac2022-08-22 16:34:01545 // TODO(crbug.com/1262022): Reenable this DCHECK once ShadowDOM and
546 // loading urns in iframes (for FLEDGE OT) are gone.
547 // DCHECK_EQ(fenced_frame_status_,
548 // RenderFrameHostImpl::FencedFrameStatus::kFencedFrameRoot);
Garrett Tanzerc69f4642022-08-15 22:15:14549 fenced_frame_properties_ = fenced_frame_properties;
550 }
551
552 const absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
553 fenced_frame_properties() {
554 return fenced_frame_properties_;
555 }
556
Yao Xiao1ac702d2022-06-08 17:20:49557 // Traverse up from this node. The `shared_storage_budget_metadata()` of the
558 // first seen node with a non-null budget metadata will be returned (i.e. this
559 // node inherits that budget metadata), and this node is expected to be an
560 // outermost fenced frame root. Return nullptr if not found (i.e. this node is
561 // not subjected to shared storage budgeting).
Garrett Tanzer2975eeac2022-08-22 16:34:01562 absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49563 FindSharedStorageBudgetMetadata();
564
Harkiran Bolariaebbe7702022-02-22 19:19:03565 // Accessor to BrowsingContextState for subframes only. Only main frame
566 // navigations can change BrowsingInstances and BrowsingContextStates,
567 // therefore for subframes associated BrowsingContextState never changes. This
568 // helper method makes this more explicit and guards against calling this on
569 // main frames (there an appropriate BrowsingContextState should be obtained
570 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
571 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
572 // the same frame).
573 const scoped_refptr<BrowsingContextState>&
574 GetBrowsingContextStateForSubframe() const;
575
Arthur Hemerye4659282022-03-28 08:36:15576 // Clears the opener property of popups referencing this FrameTreeNode as
577 // their opener.
578 void ClearOpenerReferences();
579
Liam Bradyd2a41e152022-07-19 13:58:48580 // Calculates whether one of the ancestor frames or this frame has a CSPEE
581 // in place. This is eventually sent over to LocalFrame in the renderer where
582 // it will be used by HTMLFencedFrameElement::canLoadOpaqueURL for information
583 // it can't get on its own.
584 bool AncestorOrSelfHasCSPEE() const;
585
danakjc492bf82020-09-09 20:02:44586 private:
Yuzu Saijo03dbf9b2022-07-22 04:29:45587 friend class CSPEmbeddedEnforcementUnitTest;
Charlie Hubb5943d2021-03-09 19:46:12588 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44589 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12590 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44591 ContainerPolicySandboxDynamic);
Yuzu Saijo03dbf9b2022-07-22 04:29:45592 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest, StorageKeyToCommit);
593 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest,
594 NavigationToAnonymousDocumentNetworkIsolationInfo);
595 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplTest,
596 ChildOfAnonymousIsAnonymous);
Yifan Luo86a79f42022-08-16 18:38:27597 FRIEND_TEST_ALL_PREFIXES(ContentPasswordManagerDriverTest,
598 PasswordAutofillDisabledOnAnonymousIframe);
danakjc492bf82020-09-09 20:02:44599
Dominic Farolino8a2187b2021-12-24 20:44:21600 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
601 // representing an inner FrameTree, this method destroys said inner FrameTree.
602 void DestroyInnerFrameTreeIfExists();
603
danakjc492bf82020-09-09 20:02:44604 class OpenerDestroyedObserver;
605
danakjc492bf82020-09-09 20:02:44606 // The |notification_type| parameter is used for histograms only.
607 bool NotifyUserActivation(
608 blink::mojom::UserActivationNotificationType notification_type);
609
610 bool ConsumeTransientUserActivation();
611
612 bool ClearUserActivation();
613
614 // Verify that the renderer process is allowed to set user activation on this
615 // frame by checking whether this frame's RenderWidgetHost had previously seen
616 // an input event that might lead to user activation. If user activation
617 // should be allowed, this returns true and also clears corresponding pending
618 // user activation state in the widget. Otherwise, this returns false.
619 bool VerifyUserActivation();
620
621 // The next available browser-global FrameTreeNode ID.
622 static int next_frame_tree_node_id_;
623
624 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52625 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44626
danakjc492bf82020-09-09 20:02:44627 // A browser-global identifier for the frame in the page, which stays stable
628 // even if the frame does a cross-process navigation.
629 const int frame_tree_node_id_;
630
631 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
632 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52633 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44634
danakjc492bf82020-09-09 20:02:44635 // The frame that opened this frame, if any. Will be set to null if the
636 // opener is closed, or if this frame disowns its opener by setting its
637 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52638 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44639
640 // An observer that clears this node's |opener_| if the opener is destroyed.
641 // This observer is added to the |opener_|'s observer list when the |opener_|
642 // is set to a non-null node, and it is removed from that list when |opener_|
643 // changes or when this node is destroyed. It is also cleared if |opener_|
644 // is disowned.
645 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
646
Rakina Zata Amni3a48ae42022-05-05 03:39:56647 // Unlike `opener_`, the "original opener chain" doesn't reflect
648 // window.opener, which can be suppressed or updated. The "original opener"
649 // is the main frame of the actual opener of this frame. This traces the all
650 // the way back, so if the original opener was closed (deleted or severed due
651 // to COOP), but _it_ had an original opener, this will return the original
652 // opener's original opener, etc. So this value will always be set as long as
653 // there is at least one live frame in the chain whose connection is not
654 // severed due to COOP.
655 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
656 nullptr;
danakjc492bf82020-09-09 20:02:44657
Wolfgang Beyerd8809db2020-09-30 15:29:39658 // The devtools frame token of the frame which opened this frame. This is
659 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07660 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39661
Rakina Zata Amni3a48ae42022-05-05 03:39:56662 // An observer that updates this node's
663 // |first_live_main_frame_in_original_opener_chain_| to the next original
664 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44665 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
666
arthursonzogni034bb9c2020-10-01 08:29:56667 // When created by an opener, the URL specified in window.open(url)
668 // Please refer to {Get,Set}InitialPopupURL() documentation.
669 GURL initial_popup_url_;
670
671 // When created using window.open, the origin of the creator.
672 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
673 url::Origin popup_creator_origin_;
674
W. James MacLean81b8d01f2022-01-25 20:50:59675 // If the url from the the last BeginNavigation is about:srcdoc, this value
676 // stores the srcdoc_attribute's value for re-use in history navigations.
677 std::string srcdoc_value_;
678
Rakina Zata Amni86c88fa2021-11-01 01:27:30679 // Whether this frame is still on the initial about:blank document or the
680 // synchronously committed about:blank document committed at frame creation,
681 // and its "initial empty document"-ness is still true.
682 // This will be false if either of these has happened:
Arthur Sonzogni47c79cc2022-08-30 15:25:27683 // - The current RenderFrameHost commits a cross-document navigation that is
684 // not the synchronously committed about:blank document per:
Rakina Zata Amni86c88fa2021-11-01 01:27:30685 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
686 // - The document's input stream has been opened with document.open(), per
687 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
688 // NOTE: we treat both the "initial about:blank document" and the
689 // "synchronously committed about:blank document" as the initial empty
690 // document. In the future, we plan to remove the synchronous about:blank
691 // commit so that this state will only be true if the frame is on the
692 // "initial about:blank document". See also:
693 // - https://github.com/whatwg/html/issues/6863
694 // - https://crbug.com/1215096
695 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56696
danakjc492bf82020-09-09 20:02:44697 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19698 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44699
Daniel Cheng6ac128172021-05-25 18:49:01700 // The type of frame owner for this frame. This is only relevant for non-main
701 // frames.
Kevin McNee43fe8292021-10-04 22:59:41702 const blink::FrameOwnerElementType frame_owner_element_type_ =
703 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45704
Daniel Cheng6ac128172021-05-25 18:49:01705 // The tree scope type of frame owner element, i.e. whether the element is in
706 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
707 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
708 // relevant for non-main frames.
709 const blink::mojom::TreeScopeType tree_scope_type_ =
710 blink::mojom::TreeScopeType::kDocument;
711
danakjc492bf82020-09-09 20:02:44712 // Track the pending sandbox flags and container policy for this frame. When a
713 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
714 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47715 // is stored here, and transferred into
716 // render_manager_.current_replication_state().frame_policy when they take
717 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44718 blink::FramePolicy pending_frame_policy_;
719
720 // Whether the frame was created by javascript. This is useful to prune
721 // history entries when the frame is removed (because frames created by
722 // scripts are never recreated with the same unique name - see
723 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19724 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44725
726 // Used for devtools instrumentation and trace-ability. The token is
727 // propagated to Blink's LocalFrame and both Blink and content/
728 // can tag calls and requests with this token in order to attribute them
729 // to the context frame.
730 // |devtools_frame_token_| is only defined by the browser process and is never
731 // sent back from the renderer in the control calls. It should be never used
732 // to look up the FrameTreeNode instance.
Andrey Kosyakov6e82c3b2022-09-26 22:43:46733 base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44734
735 // Tracks the scrolling and margin properties for this frame. These
736 // properties affect the child renderer but are stored on its parent's
737 // frame element. When this frame's parent dynamically updates these
738 // properties, we update them here too.
739 //
740 // Note that dynamic updates only take effect on the next frame navigation.
741 blink::mojom::FrameOwnerProperties frame_owner_properties_;
742
Yuzu Saijo03dbf9b2022-07-22 04:29:45743 // Contains the tracked HTML attributes of the corresponding iframe element,
744 // such as 'id' and 'src'.
745 blink::mojom::IframeAttributesPtr attributes_;
Antonio Sartori5abc8de2021-07-13 08:42:47746
danakjc492bf82020-09-09 20:02:44747 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
748 // be reset and a RenderFrameHost will be responsible for the navigation.
749 std::unique_ptr<NavigationRequest> navigation_request_;
750
751 // List of objects observing this FrameTreeNode.
752 base::ObserverList<Observer>::Unchecked observers_;
753
754 base::TimeTicks last_focus_time_;
755
arthursonzogni9816b9192021-03-29 16:09:19756 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44757
758 // The user activation state of the current frame. See |UserActivationState|
759 // for details on how this state is maintained.
760 blink::UserActivationState user_activation_state_;
761
shivanigithub4cd016a2021-09-20 21:10:30762 // Fenced Frames:
763 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
764 // frame and any iframes nested within it. Not set if this frame is not in a
765 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
766 // the MPArch version but for the shadow DOM version we need to keep it here
767 // since the fenced frame root is not a main frame for the latter. The value
shivanigithub14182aa2022-05-24 19:29:49768 // of the nonce will be the same for all of the the iframes inside a fenced
shivanigithub4cd016a2021-09-20 21:10:30769 // frame tree. If there is a nested fenced frame it will have a different
770 // nonce than its parent fenced frame. The nonce will stay the same across
shivanigithub14182aa2022-05-24 19:29:49771 // navigations initiated from the fenced frame tree because it is always used
772 // in conjunction with other fields of the keys and would be good to access
773 // the same storage across same-origin navigations. If the navigation is
774 // same-origin/site then the same network stack partition/storage will be
775 // reused and if it's cross-origin/site then other parts of the key will
776 // change and so, even with the same nonce, another partition will be used.
777 // But if the navigation is initiated from the embedder, the nonce will be
778 // reinitialized irrespective of same or cross origin such that there is no
779 // privacy leak via storage shared between two embedder initiated navigations.
780 // Note that this reinitialization is only implemented for MPArch.
shivanigithub4cd016a2021-09-20 21:10:30781 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
782
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58783 const FencedFrameStatus fenced_frame_status_ =
784 FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57785
Garrett Tanzerc69f4642022-08-15 22:15:14786 // If this is a fenced frame resulting from a urn:uuid navigation, this
787 // contains all the metadata specifying the resulting context.
Garrett Tanzerc69f4642022-08-15 22:15:14788 absl::optional<FencedFrameURLMapping::FencedFrameProperties>
789 fenced_frame_properties_;
790
Lukasz Anforowicz147141962020-12-16 18:03:24791 // Manages creation and swapping of RenderFrameHosts for this frame.
792 //
793 // This field needs to be declared last, because destruction of
794 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
795 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
796 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
797 // may try to use FrameTreeNode's fields above - this would be an undefined
798 // behavior if the fields (even trivially-destructible ones) were destructed
799 // before the RenderFrameHostManager's destructor runs. See also
800 // https://crbug.com/1157988.
801 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44802};
803
804} // namespace content
805
806#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_