blob: 5127f9ce6376674f2b5a0f4b869649ee4cb6f57b [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"
danakjc492bf82020-09-09 20:02:4419#include "content/browser/renderer_host/navigator.h"
20#include "content/browser/renderer_host/render_frame_host_impl.h"
21#include "content/browser/renderer_host/render_frame_host_manager.h"
22#include "content/common/content_export.h"
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:5523#include "content/public/browser/frame_type.h"
danakjc492bf82020-09-09 20:02:4424#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Lei Zhang698df03c2021-05-21 04:23:3425#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin McNee43fe8292021-10-04 22:59:4126#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4427#include "third_party/blink/public/common/frame/frame_policy.h"
28#include "third_party/blink/public/common/frame/user_activation_state.h"
danakjc492bf82020-09-09 20:02:4429#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3730#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0131#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
David Sanders2c1194d92022-04-19 23:32:3232#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom-forward.h"
danakjc492bf82020-09-09 20:02:4433
Gabriel Charetted87f10f2022-03-31 00:44:2234#include "base/time/time.h"
danakjc492bf82020-09-09 20:02:4435#include "url/gurl.h"
36#include "url/origin.h"
37
38namespace content {
39
40class NavigationRequest;
41class RenderFrameHostImpl;
42class NavigationEntryImpl;
43
Abhijeet Kandalkar3f29bc42022-09-23 12:39:5844using FencedFrameStatus = RenderFrameHostImpl::FencedFrameStatus;
45
danakjc492bf82020-09-09 20:02:4446// When a page contains iframes, its renderer process maintains a tree structure
47// of those frames. We are mirroring this tree in the browser process. This
48// class represents a node in this tree and is a wrapper for all objects that
49// are frame-specific (as opposed to page-specific).
50//
51// Each FrameTreeNode has a current RenderFrameHost, which can change over
52// time as the frame is navigated. Any immediate subframes of the current
53// document are tracked using FrameTreeNodes owned by the current
54// RenderFrameHost, rather than as children of FrameTreeNode itself. This
55// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
56// still alive - for example while pending deletion, after a new current
57// RenderFrameHost has replaced it.
58class CONTENT_EXPORT FrameTreeNode {
59 public:
60 class Observer {
61 public:
62 // Invoked when a FrameTreeNode is being destroyed.
63 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
64
65 // Invoked when a FrameTreeNode becomes focused.
66 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
67
Arthur Hemerye4659282022-03-28 08:36:1568 // Invoked when a FrameTreeNode moves to a different BrowsingInstance and
69 // the popups it opened should be disowned.
70 virtual void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) {}
71
Fergal Dalya1d569972021-03-16 03:24:5372 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4473 };
74
75 static const int kFrameTreeNodeInvalidId;
76
77 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
78 // regardless of which FrameTree it is in.
79 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
80
81 // Returns the FrameTreeNode for the given |rfh|. Same as
82 // rfh->frame_tree_node(), but also supports nullptrs.
83 static FrameTreeNode* From(RenderFrameHost* rfh);
84
85 // Callers are are expected to initialize sandbox flags separately after
86 // calling the constructor.
87 FrameTreeNode(
88 FrameTree* frame_tree,
89 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0190 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4491 bool is_created_by_script,
92 const base::UnguessableToken& devtools_frame_token,
93 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4194 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3495 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4496
Peter Boström828b9022021-09-21 02:28:4397 FrameTreeNode(const FrameTreeNode&) = delete;
98 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
99
danakjc492bf82020-09-09 20:02:44100 ~FrameTreeNode();
101
102 void AddObserver(Observer* observer);
103 void RemoveObserver(Observer* observer);
104
Ian Vollick25a9d032022-04-12 23:20:17105 // Frame trees may be nested so it can be the case that IsMainFrame() is true,
106 // but is not the outermost main frame. In particular, !IsMainFrame() cannot
107 // be used to check if the frame is an embedded frame -- use
108 // !IsOutermostMainFrame() instead. NB: this does not escape guest views;
109 // IsOutermostMainFrame will be true for the outermost main frame in an inner
110 // guest view.
danakjc492bf82020-09-09 20:02:44111 bool IsMainFrame() const;
Ian Vollick25a9d032022-04-12 23:20:17112 bool IsOutermostMainFrame();
danakjc492bf82020-09-09 20:02:44113
arthursonzogni76098e52020-11-25 14:18:45114 // Clears any state in this node which was set by the document itself (CSP &
115 // UserActivationState) and notifies proxies as appropriate. Invoked after
116 // committing navigation to a new document (since the new document comes with
117 // a fresh set of CSP).
118 // TODO(arthursonzogni): Remove this function. The frame/document must not be
119 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13120 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44121
122 FrameTree* frame_tree() const { return frame_tree_; }
123 Navigator& navigator() { return frame_tree()->navigator(); }
124
125 RenderFrameHostManager* render_manager() { return &render_manager_; }
Alexander Timin33e2e2c12022-03-03 04:21:33126 const RenderFrameHostManager* render_manager() const {
127 return &render_manager_;
128 }
danakjc492bf82020-09-09 20:02:44129 int frame_tree_node_id() const { return frame_tree_node_id_; }
Yuzu Saijo03dbf9b2022-07-22 04:29:45130 // This reflects window.name, which is initially set to the the "name"
131 // attribute. But this won't reflect changes of 'name' attribute and instead
132 // reflect changes to the Window object's name property.
133 // This is different from IframeAttributes' name in that this will not get
134 // updated when 'name' attribute gets updated.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47135 const std::string& frame_name() const {
136 return render_manager_.current_replication_state().name;
137 }
danakjc492bf82020-09-09 20:02:44138
139 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47140 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44141 }
142
143 // See comment on the member declaration.
144 const base::UnguessableToken& devtools_frame_token() const {
145 return devtools_frame_token_;
146 }
147
Andrey Kosyakov6e82c3b2022-09-26 22:43:46148 // This may only change when an RFH changes host frame tree nodes
149 // during prerender activation.
150 // TODO(caseq,dsv): see if we can get rid of it.
151 void set_devtools_frame_token(const base::UnguessableToken& token) {
152 devtools_frame_token_ = token;
153 }
danakjc492bf82020-09-09 20:02:44154 size_t child_count() const { return current_frame_host()->child_count(); }
155
danakjc492bf82020-09-09 20:02:44156 RenderFrameHostImpl* parent() const { return parent_; }
157
Dave Tapuskac8de3b02021-12-03 21:51:01158 // See `RenderFrameHost::GetParentOrOuterDocument()` for
159 // documentation.
160 RenderFrameHostImpl* GetParentOrOuterDocument();
161
162 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
163 // documentation.
164 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
165
danakjc492bf82020-09-09 20:02:44166 FrameTreeNode* opener() const { return opener_; }
167
Rakina Zata Amni3a48ae42022-05-05 03:39:56168 FrameTreeNode* first_live_main_frame_in_original_opener_chain() const {
169 return first_live_main_frame_in_original_opener_chain_;
170 }
danakjc492bf82020-09-09 20:02:44171
Anton Bikineevf62d1bf2021-05-15 17:56:07172 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39173 return opener_devtools_frame_token_;
174 }
175
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55176 // Returns the type of the frame. Refer to frame_type.h for the details.
177 FrameType GetFrameType() const;
178
danakjc492bf82020-09-09 20:02:44179 // Assigns a new opener for this node and, if |opener| is non-null, registers
180 // an observer that will clear this node's opener if |opener| is ever
181 // destroyed.
182 void SetOpener(FrameTreeNode* opener);
183
184 // Assigns the initial opener for this node, and if |opener| is non-null,
185 // registers an observer that will clear this node's opener if |opener| is
186 // ever destroyed. The value set here is the root of the tree.
187 //
188 // It is not possible to change the opener once it was set.
189 void SetOriginalOpener(FrameTreeNode* opener);
190
Wolfgang Beyerd8809db2020-09-30 15:29:39191 // Assigns an opener frame id for this node. This string id is only set once
192 // and cannot be changed. It persists, even if the |opener| is destroyed. It
193 // is used for attribution in the DevTools frontend.
194 void SetOpenerDevtoolsFrameToken(
195 base::UnguessableToken opener_devtools_frame_token);
196
danakjc492bf82020-09-09 20:02:44197 FrameTreeNode* child_at(size_t index) const {
198 return current_frame_host()->child_at(index);
199 }
200
201 // Returns the URL of the last committed page in the current frame.
202 const GURL& current_url() const {
203 return current_frame_host()->GetLastCommittedURL();
204 }
205
Rakina Zata Amni90555282022-01-21 07:35:54206 // Sets `is_on_initial_empty_document_` to false.
207 void SetNotOnInitialEmptyDocument() { is_on_initial_empty_document_ = false; }
Rakina Zata Amni86c88fa2021-11-01 01:27:30208
Rakina Zata Amni91d485b42021-12-08 02:50:13209 // Returns false if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30210 // empty document, or if the current document's input stream has been opened
211 // with document.open(), causing the document to lose its "initial empty
212 // document" status. For more details, see the definition of
213 // `is_on_initial_empty_document_`.
214 bool is_on_initial_empty_document() const {
215 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56216 }
217
Rakina Zata Amni86c88fa2021-11-01 01:27:30218 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56219 // false. Must only be called after the current document's input stream has
220 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30221 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14222
danakjc492bf82020-09-09 20:02:44223 // Returns whether the frame's owner element in the parent document is
224 // collapsed, that is, removed from the layout as if it did not exist, as per
225 // request by the embedder (of the content/ layer).
226 bool is_collapsed() const { return is_collapsed_; }
227
228 // Sets whether to collapse the frame's owner element in the parent document,
229 // that is, to remove it from the layout as if it did not exist, as per
230 // request by the embedder (of the content/ layer). Cannot be called for main
231 // frames.
232 //
233 // This only has an effect for <iframe> owner elements, and is a no-op when
234 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
235 void SetCollapsed(bool collapsed);
236
237 // Returns the origin of the last committed page in this frame.
238 // WARNING: To get the last committed origin for a particular
239 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
240 // which will behave correctly even when the RenderFrameHost is not the
241 // current one for this frame (such as when it's pending deletion).
242 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47243 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44244 }
245
danakjc492bf82020-09-09 20:02:44246 // Returns the latest frame policy (sandbox flags and container policy) for
247 // this frame. This includes flags inherited from parent frames and the latest
248 // flags from the <iframe> element hosting this frame. The returned policies
249 // may not yet have taken effect, since "sandbox" and "allow" attribute
250 // updates in an <iframe> element take effect on next navigation. To retrieve
251 // the currently active policy for this frame, use effective_frame_policy().
252 const blink::FramePolicy& pending_frame_policy() const {
253 return pending_frame_policy_;
254 }
255
256 // Update this frame's sandbox flags and container policy. This is called
257 // when a parent frame updates the "sandbox" attribute in the <iframe> element
258 // for this frame, or any of the attributes which affect the container policy
259 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
260 // These policies won't take effect until next navigation. If this frame's
261 // parent is itself sandboxed, the parent's sandbox flags are combined with
262 // those in |frame_policy|.
263 // Attempting to change the container policy on the main frame will have no
264 // effect.
265 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
266
267 // Returns the currently active frame policy for this frame, including the
268 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39269 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44270 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
271 // origin of the iframe's src attribute (which may be different from the URL
272 // of the document currently loaded into the frame). This does not include
273 // policy changes that have been made by updating the containing iframe
274 // element attributes since the frame was last navigated; use
275 // pending_frame_policy() for those.
276 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47277 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44278 }
279
danakjc492bf82020-09-09 20:02:44280 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
281 return frame_owner_properties_;
282 }
283
284 void set_frame_owner_properties(
285 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
286 frame_owner_properties_ = frame_owner_properties;
287 }
288
Yuzu Saijo03dbf9b2022-07-22 04:29:45289 // Reflects the attributes of the corresponding iframe html element, such
290 // as 'anonymous', 'id', 'name' and 'src'. These values should not be
291 // exposed to cross-origin renderers.
292 const network::mojom::ContentSecurityPolicy* csp_attribute() const {
293 return attributes_->parsed_csp_attribute.get();
danakjc492bf82020-09-09 20:02:44294 }
Yuzu Saijo03dbf9b2022-07-22 04:29:45295 bool anonymous() const { return attributes_->anonymous; }
296 const std::string& html_id() const { return attributes_->id; }
297 // This tracks iframe's 'name' attribute instead of window.name, which is
298 // tracked in FrameReplicationState. See the comment for frame_name() for
299 // more details.
300 const std::string& html_name() const { return attributes_->name; }
301 const std::string& html_src() const { return attributes_->src; }
danakjc492bf82020-09-09 20:02:44302
Yuzu Saijo03dbf9b2022-07-22 04:29:45303 void SetAttributes(blink::mojom::IframeAttributesPtr attributes);
Antonio Sartori5abc8de2021-07-13 08:42:47304
danakjc492bf82020-09-09 20:02:44305 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47306 return render_manager_.current_replication_state().origin.IsSameOriginWith(
307 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44308 }
309
Gyuyoung Kimc16e52e92021-03-19 02:45:37310 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47311 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44312 }
313
314 RenderFrameHostImpl* current_frame_host() const {
315 return render_manager_.current_frame_host();
316 }
317
danakjc492bf82020-09-09 20:02:44318 // Returns true if this node is in a loading state.
319 bool IsLoading() const;
320
Alex Moshchuk9b0fd822020-10-26 23:08:15321 // Returns true if this node has a cross-document navigation in progress.
322 bool HasPendingCrossDocumentNavigation() const;
323
danakjc492bf82020-09-09 20:02:44324 NavigationRequest* navigation_request() { return navigation_request_.get(); }
325
326 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
327 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
328 // RenderFrameHost that is committing the navigation.
329 void TransferNavigationRequestOwnership(
330 RenderFrameHostImpl* render_frame_host);
331
332 // Takes ownership of |navigation_request| and makes it the current
333 // NavigationRequest of this frame. This corresponds to the start of a new
334 // navigation. If there was an ongoing navigation request before calling this
335 // function, it is canceled. |navigation_request| should not be null.
336 void CreatedNavigationRequest(
337 std::unique_ptr<NavigationRequest> navigation_request);
338
339 // Resets the current navigation request. If |keep_state| is true, any state
340 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
341 // loading state) will not be reset by the function.
342 void ResetNavigationRequest(bool keep_state);
343
344 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19345 // |should_show_loading_ui| indicates whether this navigation should be
346 // visible in the UI. True for cross-document navigations and navigations
Domenic Denicola30810742022-03-17 20:11:23347 // intercepted by the navigation API's transitionWhile().
danakjc492bf82020-09-09 20:02:44348 // |was_previously_loading| is false if the FrameTree was not loading before.
349 // The caller is required to provide this boolean as the delegate should only
350 // be notified if the FrameTree went from non-loading to loading state.
351 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19352 void DidStartLoading(bool should_show_loading_ui,
353 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44354
355 // A RenderFrameHost in this node stopped loading.
356 void DidStopLoading();
357
358 // The load progress for a RenderFrameHost in this node was updated to
359 // |load_progress|. This will notify the FrameTree which will in turn notify
360 // the WebContents.
361 void DidChangeLoadProgress(double load_progress);
362
363 // Called when the user directed the page to stop loading. Stops all loads
364 // happening in the FrameTreeNode. This method should be used with
365 // FrameTree::ForEach to stop all loads in the entire FrameTree.
366 bool StopLoading();
367
368 // Returns the time this frame was last focused.
369 base::TimeTicks last_focus_time() const { return last_focus_time_; }
370
371 // Called when this node becomes focused. Updates the node's last focused
372 // time and notifies observers.
373 void DidFocus();
374
375 // Called when the user closed the modal dialogue for BeforeUnload and
376 // cancelled the navigation. This should stop any load happening in the
377 // FrameTreeNode.
378 void BeforeUnloadCanceled();
379
danakjc492bf82020-09-09 20:02:44380 // Updates the user activation state in the browser frame tree and in the
381 // frame trees in all renderer processes except the renderer for this node
382 // (which initiated the update). Returns |false| if the update tries to
383 // consume an already consumed/expired transient state, |true| otherwise. See
384 // the comment on user_activation_state_ below.
385 //
386 // The |notification_type| parameter is used for histograms, only for the case
387 // |update_state == kNotifyActivation|.
388 bool UpdateUserActivationState(
389 blink::mojom::UserActivationUpdateType update_type,
390 blink::mojom::UserActivationNotificationType notification_type);
391
danakjc492bf82020-09-09 20:02:44392 // Returns the sandbox flags currently in effect for this frame. This includes
393 // flags inherited from parent frames, the currently active flags from the
394 // <iframe> element hosting this frame, as well as any flags set from a
395 // Content-Security-Policy HTTP header. This does not include flags that have
396 // have been updated in an <iframe> element but have not taken effect yet; use
397 // pending_frame_policy() for those. To see the flags which will take effect
398 // on navigation (which does not include the CSP-set flags), use
399 // effective_frame_policy().
400 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47401 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44402 }
403
danakjc492bf82020-09-09 20:02:44404 // Returns whether the frame received a user gesture on a previous navigation
405 // on the same eTLD+1.
406 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47407 return render_manager_.current_replication_state()
408 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44409 }
410
411 // When a tab is discarded, WebContents sets was_discarded on its
412 // root FrameTreeNode.
413 // In addition, when a child frame is created, this bit is passed on from
414 // parent to child.
415 // When a navigation request is created, was_discarded is passed on to the
416 // request and reset to false in FrameTreeNode.
417 void set_was_discarded() { was_discarded_ = true; }
418 bool was_discarded() const { return was_discarded_; }
419
420 // Returns the sticky bit of the User Activation v2 state of the
421 // |FrameTreeNode|.
422 bool HasStickyUserActivation() const {
423 return user_activation_state_.HasBeenActive();
424 }
425
426 // Returns the transient bit of the User Activation v2 state of the
427 // |FrameTreeNode|.
428 bool HasTransientUserActivation() {
429 return user_activation_state_.IsActive();
430 }
431
432 // Remove history entries for all frames created by script in this frame's
433 // subtree. If a frame created by a script is removed, then its history entry
434 // will never be reused - this saves memory.
435 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
436
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58437 FencedFrameStatus fenced_frame_status() const { return fenced_frame_status_; }
438
Kevin McNee43fe8292021-10-04 22:59:41439 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45440 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44441 }
danakjc492bf82020-09-09 20:02:44442
Daniel Cheng6ac128172021-05-25 18:49:01443 blink::mojom::TreeScopeType tree_scope_type() const {
444 return tree_scope_type_;
445 }
446
arthursonzogni034bb9c2020-10-01 08:29:56447 // The initial popup URL for new window opened using:
448 // `window.open(initial_popup_url)`.
449 // An empty GURL otherwise.
450 //
451 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
452 // document served from this URL. The FrameTreeNode always starts hosting the
453 // initial empty document and attempts a navigation toward this URL. However
454 // the navigation might be delayed, redirected and even cancelled.
455 void SetInitialPopupURL(const GURL& initial_popup_url);
456 const GURL& initial_popup_url() const { return initial_popup_url_; }
457
458 // The origin of the document that used window.open() to create this frame.
459 // Otherwise, an opaque Origin with a nonce different from all previously
460 // existing Origins.
461 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
462 const url::Origin& popup_creator_origin() const {
463 return popup_creator_origin_;
464 }
465
Harkiran Bolaria59290d62021-03-17 01:53:01466 // Sets the associated FrameTree for this node. The node can change FrameTrees
467 // when blink::features::Prerender2 is enabled, which allows a page loaded in
468 // the prerendered FrameTree to be used for a navigation in the primary frame
469 // tree.
470 void SetFrameTree(FrameTree& frame_tree);
471
Alexander Timin074cd182022-03-23 18:11:22472 using TraceProto = perfetto::protos::pbzero::FrameTreeNodeInfo;
Alexander Timinf785f342021-03-18 00:00:56473 // Write a representation of this object into a trace.
Alexander Timin074cd182022-03-23 18:11:22474 void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
Alexander Timinf785f342021-03-18 00:00:56475
Carlos Caballero76711352021-03-24 17:38:21476 // Returns true the node is navigating, i.e. it has an associated
477 // NavigationRequest.
478 bool HasNavigation();
479
shivanigithubf3ddff52021-07-03 22:06:30480 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30481 // Note that these two functions cannot be invoked from a FrameTree's or
482 // its root node's constructor since they require the frame tree and the
483 // root node to be completely constructed.
484 //
shivanigithubf3ddff52021-07-03 22:06:30485 // Returns false if fenced frames are disabled. Returns true if the feature is
486 // enabled and if |this| is a fenced frame. Returns false for
487 // iframes embedded in a fenced frame. To clarify: for the MPArch
488 // implementation this only returns true if |this| is the actual
489 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
490 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36491 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30492
493 // Returns false if fenced frames are disabled. Returns true if the
494 // feature is enabled and if |this| or any of its ancestor nodes is a
495 // fenced frame.
496 bool IsInFencedFrameTree() const;
497
shivanigithub4cd016a2021-09-20 21:10:30498 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
499 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
500 // details.
501 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
502 return fenced_frame_nonce_;
503 }
504
505 // If applicable, set the fenced frame nonce. See comment on
506 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
507 // by FrameTree::Init() or FrameTree::AddFrame().
508 void SetFencedFrameNonceIfNeeded();
509
Garrett Tanzera42fdef2022-06-13 16:09:14510 // Returns the mode attribute set on the fenced frame root if this frame is
511 // in a fenced frame tree, otherwise returns `absl::nullopt`.
Nan Line376738a2022-03-25 22:05:41512 absl::optional<blink::mojom::FencedFrameMode> GetFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16513
Dave Tapuskac8de3b02021-12-03 21:51:01514 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
515 // Do not use directly.
516 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
517
Harkiran Bolariab4437fd2021-08-11 17:51:22518 // Sets the unique_name and name fields on replication_state_. To be used in
519 // prerender activation to make sure the FrameTreeNode replication state is
520 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
521 // renderers should already have the correct value, so unlike
522 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47523 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
524 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22525 void set_frame_name_for_activation(const std::string& unique_name,
526 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40527 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
528 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22529 }
530
Nan Linaaf84f72021-12-02 22:31:56531 // Returns true if error page isolation is enabled.
532 bool IsErrorPageIsolationEnabled() const;
533
W. James MacLean81b8d01f2022-01-25 20:50:59534 // Functions to store and retrieve a frame's srcdoc value on this
535 // FrameTreeNode.
536 void SetSrcdocValue(const std::string& srcdoc_value);
537 const std::string& srcdoc_value() const { return srcdoc_value_; }
538
Garrett Tanzerc69f4642022-08-15 22:15:14539 void set_fenced_frame_properties(
540 absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
541 fenced_frame_properties) {
Garrett Tanzer2975eeac2022-08-22 16:34:01542 // TODO(crbug.com/1262022): Reenable this DCHECK once ShadowDOM and
543 // loading urns in iframes (for FLEDGE OT) are gone.
544 // DCHECK_EQ(fenced_frame_status_,
545 // RenderFrameHostImpl::FencedFrameStatus::kFencedFrameRoot);
Garrett Tanzerc69f4642022-08-15 22:15:14546 fenced_frame_properties_ = fenced_frame_properties;
547 }
548
549 const absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
550 fenced_frame_properties() {
551 return fenced_frame_properties_;
552 }
553
Yao Xiao1ac702d2022-06-08 17:20:49554 // Traverse up from this node. The `shared_storage_budget_metadata()` of the
555 // first seen node with a non-null budget metadata will be returned (i.e. this
556 // node inherits that budget metadata), and this node is expected to be an
557 // outermost fenced frame root. Return nullptr if not found (i.e. this node is
558 // not subjected to shared storage budgeting).
Garrett Tanzer2975eeac2022-08-22 16:34:01559 absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49560 FindSharedStorageBudgetMetadata();
561
Harkiran Bolariaebbe7702022-02-22 19:19:03562 // Accessor to BrowsingContextState for subframes only. Only main frame
563 // navigations can change BrowsingInstances and BrowsingContextStates,
564 // therefore for subframes associated BrowsingContextState never changes. This
565 // helper method makes this more explicit and guards against calling this on
566 // main frames (there an appropriate BrowsingContextState should be obtained
567 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
568 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
569 // the same frame).
570 const scoped_refptr<BrowsingContextState>&
571 GetBrowsingContextStateForSubframe() const;
572
Arthur Hemerye4659282022-03-28 08:36:15573 // Clears the opener property of popups referencing this FrameTreeNode as
574 // their opener.
575 void ClearOpenerReferences();
576
Liam Bradyd2a41e152022-07-19 13:58:48577 // Calculates whether one of the ancestor frames or this frame has a CSPEE
578 // in place. This is eventually sent over to LocalFrame in the renderer where
579 // it will be used by HTMLFencedFrameElement::canLoadOpaqueURL for information
580 // it can't get on its own.
581 bool AncestorOrSelfHasCSPEE() const;
582
danakjc492bf82020-09-09 20:02:44583 private:
Yuzu Saijo03dbf9b2022-07-22 04:29:45584 friend class CSPEmbeddedEnforcementUnitTest;
Charlie Hubb5943d2021-03-09 19:46:12585 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44586 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12587 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44588 ContainerPolicySandboxDynamic);
Yuzu Saijo03dbf9b2022-07-22 04:29:45589 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest, StorageKeyToCommit);
590 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest,
591 NavigationToAnonymousDocumentNetworkIsolationInfo);
592 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplTest,
593 ChildOfAnonymousIsAnonymous);
Yifan Luo86a79f42022-08-16 18:38:27594 FRIEND_TEST_ALL_PREFIXES(ContentPasswordManagerDriverTest,
595 PasswordAutofillDisabledOnAnonymousIframe);
danakjc492bf82020-09-09 20:02:44596
Dominic Farolino8a2187b2021-12-24 20:44:21597 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
598 // representing an inner FrameTree, this method destroys said inner FrameTree.
599 void DestroyInnerFrameTreeIfExists();
600
danakjc492bf82020-09-09 20:02:44601 class OpenerDestroyedObserver;
602
danakjc492bf82020-09-09 20:02:44603 // The |notification_type| parameter is used for histograms only.
604 bool NotifyUserActivation(
605 blink::mojom::UserActivationNotificationType notification_type);
606
607 bool ConsumeTransientUserActivation();
608
609 bool ClearUserActivation();
610
611 // Verify that the renderer process is allowed to set user activation on this
612 // frame by checking whether this frame's RenderWidgetHost had previously seen
613 // an input event that might lead to user activation. If user activation
614 // should be allowed, this returns true and also clears corresponding pending
615 // user activation state in the widget. Otherwise, this returns false.
616 bool VerifyUserActivation();
617
618 // The next available browser-global FrameTreeNode ID.
619 static int next_frame_tree_node_id_;
620
621 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52622 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44623
danakjc492bf82020-09-09 20:02:44624 // A browser-global identifier for the frame in the page, which stays stable
625 // even if the frame does a cross-process navigation.
626 const int frame_tree_node_id_;
627
628 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
629 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52630 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44631
danakjc492bf82020-09-09 20:02:44632 // The frame that opened this frame, if any. Will be set to null if the
633 // opener is closed, or if this frame disowns its opener by setting its
634 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52635 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44636
637 // An observer that clears this node's |opener_| if the opener is destroyed.
638 // This observer is added to the |opener_|'s observer list when the |opener_|
639 // is set to a non-null node, and it is removed from that list when |opener_|
640 // changes or when this node is destroyed. It is also cleared if |opener_|
641 // is disowned.
642 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
643
Rakina Zata Amni3a48ae42022-05-05 03:39:56644 // Unlike `opener_`, the "original opener chain" doesn't reflect
645 // window.opener, which can be suppressed or updated. The "original opener"
646 // is the main frame of the actual opener of this frame. This traces the all
647 // the way back, so if the original opener was closed (deleted or severed due
648 // to COOP), but _it_ had an original opener, this will return the original
649 // opener's original opener, etc. So this value will always be set as long as
650 // there is at least one live frame in the chain whose connection is not
651 // severed due to COOP.
652 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
653 nullptr;
danakjc492bf82020-09-09 20:02:44654
Wolfgang Beyerd8809db2020-09-30 15:29:39655 // The devtools frame token of the frame which opened this frame. This is
656 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07657 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39658
Rakina Zata Amni3a48ae42022-05-05 03:39:56659 // An observer that updates this node's
660 // |first_live_main_frame_in_original_opener_chain_| to the next original
661 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44662 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
663
arthursonzogni034bb9c2020-10-01 08:29:56664 // When created by an opener, the URL specified in window.open(url)
665 // Please refer to {Get,Set}InitialPopupURL() documentation.
666 GURL initial_popup_url_;
667
668 // When created using window.open, the origin of the creator.
669 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
670 url::Origin popup_creator_origin_;
671
W. James MacLean81b8d01f2022-01-25 20:50:59672 // If the url from the the last BeginNavigation is about:srcdoc, this value
673 // stores the srcdoc_attribute's value for re-use in history navigations.
674 std::string srcdoc_value_;
675
Rakina Zata Amni86c88fa2021-11-01 01:27:30676 // Whether this frame is still on the initial about:blank document or the
677 // synchronously committed about:blank document committed at frame creation,
678 // and its "initial empty document"-ness is still true.
679 // This will be false if either of these has happened:
Arthur Sonzogni47c79cc2022-08-30 15:25:27680 // - The current RenderFrameHost commits a cross-document navigation that is
681 // not the synchronously committed about:blank document per:
Rakina Zata Amni86c88fa2021-11-01 01:27:30682 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
683 // - The document's input stream has been opened with document.open(), per
684 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
685 // NOTE: we treat both the "initial about:blank document" and the
686 // "synchronously committed about:blank document" as the initial empty
687 // document. In the future, we plan to remove the synchronous about:blank
688 // commit so that this state will only be true if the frame is on the
689 // "initial about:blank document". See also:
690 // - https://github.com/whatwg/html/issues/6863
691 // - https://crbug.com/1215096
692 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56693
danakjc492bf82020-09-09 20:02:44694 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19695 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44696
Daniel Cheng6ac128172021-05-25 18:49:01697 // The type of frame owner for this frame. This is only relevant for non-main
698 // frames.
Kevin McNee43fe8292021-10-04 22:59:41699 const blink::FrameOwnerElementType frame_owner_element_type_ =
700 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45701
Daniel Cheng6ac128172021-05-25 18:49:01702 // The tree scope type of frame owner element, i.e. whether the element is in
703 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
704 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
705 // relevant for non-main frames.
706 const blink::mojom::TreeScopeType tree_scope_type_ =
707 blink::mojom::TreeScopeType::kDocument;
708
danakjc492bf82020-09-09 20:02:44709 // Track the pending sandbox flags and container policy for this frame. When a
710 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
711 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47712 // is stored here, and transferred into
713 // render_manager_.current_replication_state().frame_policy when they take
714 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44715 blink::FramePolicy pending_frame_policy_;
716
717 // Whether the frame was created by javascript. This is useful to prune
718 // history entries when the frame is removed (because frames created by
719 // scripts are never recreated with the same unique name - see
720 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19721 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44722
723 // Used for devtools instrumentation and trace-ability. The token is
724 // propagated to Blink's LocalFrame and both Blink and content/
725 // can tag calls and requests with this token in order to attribute them
726 // to the context frame.
727 // |devtools_frame_token_| is only defined by the browser process and is never
728 // sent back from the renderer in the control calls. It should be never used
729 // to look up the FrameTreeNode instance.
Andrey Kosyakov6e82c3b2022-09-26 22:43:46730 base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44731
732 // Tracks the scrolling and margin properties for this frame. These
733 // properties affect the child renderer but are stored on its parent's
734 // frame element. When this frame's parent dynamically updates these
735 // properties, we update them here too.
736 //
737 // Note that dynamic updates only take effect on the next frame navigation.
738 blink::mojom::FrameOwnerProperties frame_owner_properties_;
739
Yuzu Saijo03dbf9b2022-07-22 04:29:45740 // Contains the tracked HTML attributes of the corresponding iframe element,
741 // such as 'id' and 'src'.
742 blink::mojom::IframeAttributesPtr attributes_;
Antonio Sartori5abc8de2021-07-13 08:42:47743
danakjc492bf82020-09-09 20:02:44744 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
745 // be reset and a RenderFrameHost will be responsible for the navigation.
746 std::unique_ptr<NavigationRequest> navigation_request_;
747
748 // List of objects observing this FrameTreeNode.
749 base::ObserverList<Observer>::Unchecked observers_;
750
751 base::TimeTicks last_focus_time_;
752
arthursonzogni9816b9192021-03-29 16:09:19753 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44754
755 // The user activation state of the current frame. See |UserActivationState|
756 // for details on how this state is maintained.
757 blink::UserActivationState user_activation_state_;
758
shivanigithub4cd016a2021-09-20 21:10:30759 // Fenced Frames:
760 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
761 // frame and any iframes nested within it. Not set if this frame is not in a
762 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
763 // the MPArch version but for the shadow DOM version we need to keep it here
764 // since the fenced frame root is not a main frame for the latter. The value
shivanigithub14182aa2022-05-24 19:29:49765 // of the nonce will be the same for all of the the iframes inside a fenced
shivanigithub4cd016a2021-09-20 21:10:30766 // frame tree. If there is a nested fenced frame it will have a different
767 // nonce than its parent fenced frame. The nonce will stay the same across
shivanigithub14182aa2022-05-24 19:29:49768 // navigations initiated from the fenced frame tree because it is always used
769 // in conjunction with other fields of the keys and would be good to access
770 // the same storage across same-origin navigations. If the navigation is
771 // same-origin/site then the same network stack partition/storage will be
772 // reused and if it's cross-origin/site then other parts of the key will
773 // change and so, even with the same nonce, another partition will be used.
774 // But if the navigation is initiated from the embedder, the nonce will be
775 // reinitialized irrespective of same or cross origin such that there is no
776 // privacy leak via storage shared between two embedder initiated navigations.
777 // Note that this reinitialization is only implemented for MPArch.
shivanigithub4cd016a2021-09-20 21:10:30778 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
779
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58780 const FencedFrameStatus fenced_frame_status_ =
781 FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57782
Garrett Tanzerc69f4642022-08-15 22:15:14783 // If this is a fenced frame resulting from a urn:uuid navigation, this
784 // contains all the metadata specifying the resulting context.
Garrett Tanzerc69f4642022-08-15 22:15:14785 absl::optional<FencedFrameURLMapping::FencedFrameProperties>
786 fenced_frame_properties_;
787
Lukasz Anforowicz147141962020-12-16 18:03:24788 // Manages creation and swapping of RenderFrameHosts for this frame.
789 //
790 // This field needs to be declared last, because destruction of
791 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
792 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
793 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
794 // may try to use FrameTreeNode's fields above - this would be an undefined
795 // behavior if the fields (even trivially-destructible ones) were destructed
796 // before the RenderFrameHostManager's destructor runs. See also
797 // https://crbug.com/1157988.
798 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44799};
800
801} // namespace content
802
803#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_