blob: 2c960abc55947af57bd31fbdb3153a52ed308aae [file] [log] [blame]
danakjc492bf82020-09-09 20:02:441// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_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
44// When a page contains iframes, its renderer process maintains a tree structure
45// of those frames. We are mirroring this tree in the browser process. This
46// class represents a node in this tree and is a wrapper for all objects that
47// are frame-specific (as opposed to page-specific).
48//
49// Each FrameTreeNode has a current RenderFrameHost, which can change over
50// time as the frame is navigated. Any immediate subframes of the current
51// document are tracked using FrameTreeNodes owned by the current
52// RenderFrameHost, rather than as children of FrameTreeNode itself. This
53// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
54// still alive - for example while pending deletion, after a new current
55// RenderFrameHost has replaced it.
56class CONTENT_EXPORT FrameTreeNode {
57 public:
58 class Observer {
59 public:
60 // Invoked when a FrameTreeNode is being destroyed.
61 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
62
63 // Invoked when a FrameTreeNode becomes focused.
64 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
65
Arthur Hemerye4659282022-03-28 08:36:1566 // Invoked when a FrameTreeNode moves to a different BrowsingInstance and
67 // the popups it opened should be disowned.
68 virtual void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) {}
69
Fergal Dalya1d569972021-03-16 03:24:5370 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4471 };
72
73 static const int kFrameTreeNodeInvalidId;
74
75 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
76 // regardless of which FrameTree it is in.
77 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
78
79 // Returns the FrameTreeNode for the given |rfh|. Same as
80 // rfh->frame_tree_node(), but also supports nullptrs.
81 static FrameTreeNode* From(RenderFrameHost* rfh);
82
83 // Callers are are expected to initialize sandbox flags separately after
84 // calling the constructor.
85 FrameTreeNode(
86 FrameTree* frame_tree,
87 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0188 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4489 bool is_created_by_script,
90 const base::UnguessableToken& devtools_frame_token,
91 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4192 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3493 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4494
Peter Boström828b9022021-09-21 02:28:4395 FrameTreeNode(const FrameTreeNode&) = delete;
96 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
97
danakjc492bf82020-09-09 20:02:4498 ~FrameTreeNode();
99
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
102
Ian Vollick25a9d032022-04-12 23:20:17103 // Frame trees may be nested so it can be the case that IsMainFrame() is true,
104 // but is not the outermost main frame. In particular, !IsMainFrame() cannot
105 // be used to check if the frame is an embedded frame -- use
106 // !IsOutermostMainFrame() instead. NB: this does not escape guest views;
107 // IsOutermostMainFrame will be true for the outermost main frame in an inner
108 // guest view.
danakjc492bf82020-09-09 20:02:44109 bool IsMainFrame() const;
Ian Vollick25a9d032022-04-12 23:20:17110 bool IsOutermostMainFrame();
danakjc492bf82020-09-09 20:02:44111
arthursonzogni76098e52020-11-25 14:18:45112 // Clears any state in this node which was set by the document itself (CSP &
113 // UserActivationState) and notifies proxies as appropriate. Invoked after
114 // committing navigation to a new document (since the new document comes with
115 // a fresh set of CSP).
116 // TODO(arthursonzogni): Remove this function. The frame/document must not be
117 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13118 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44119
120 FrameTree* frame_tree() const { return frame_tree_; }
121 Navigator& navigator() { return frame_tree()->navigator(); }
122
123 RenderFrameHostManager* render_manager() { return &render_manager_; }
Alexander Timin33e2e2c12022-03-03 04:21:33124 const RenderFrameHostManager* render_manager() const {
125 return &render_manager_;
126 }
danakjc492bf82020-09-09 20:02:44127 int frame_tree_node_id() const { return frame_tree_node_id_; }
Harkiran Bolaria4eacb3a2021-12-13 20:03:47128 const std::string& frame_name() const {
129 return render_manager_.current_replication_state().name;
130 }
danakjc492bf82020-09-09 20:02:44131
132 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47133 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44134 }
135
136 // See comment on the member declaration.
137 const base::UnguessableToken& devtools_frame_token() const {
138 return devtools_frame_token_;
139 }
140
141 size_t child_count() const { return current_frame_host()->child_count(); }
142
danakjc492bf82020-09-09 20:02:44143 RenderFrameHostImpl* parent() const { return parent_; }
144
Dave Tapuskac8de3b02021-12-03 21:51:01145 // See `RenderFrameHost::GetParentOrOuterDocument()` for
146 // documentation.
147 RenderFrameHostImpl* GetParentOrOuterDocument();
148
149 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
150 // documentation.
151 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
152
danakjc492bf82020-09-09 20:02:44153 FrameTreeNode* opener() const { return opener_; }
154
Rakina Zata Amni3a48ae42022-05-05 03:39:56155 FrameTreeNode* first_live_main_frame_in_original_opener_chain() const {
156 return first_live_main_frame_in_original_opener_chain_;
157 }
danakjc492bf82020-09-09 20:02:44158
Anton Bikineevf62d1bf2021-05-15 17:56:07159 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39160 return opener_devtools_frame_token_;
161 }
162
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55163 // Returns the type of the frame. Refer to frame_type.h for the details.
164 FrameType GetFrameType() const;
165
danakjc492bf82020-09-09 20:02:44166 // Assigns a new opener for this node and, if |opener| is non-null, registers
167 // an observer that will clear this node's opener if |opener| is ever
168 // destroyed.
169 void SetOpener(FrameTreeNode* opener);
170
171 // Assigns the initial opener for this node, and if |opener| is non-null,
172 // registers an observer that will clear this node's opener if |opener| is
173 // ever destroyed. The value set here is the root of the tree.
174 //
175 // It is not possible to change the opener once it was set.
176 void SetOriginalOpener(FrameTreeNode* opener);
177
Wolfgang Beyerd8809db2020-09-30 15:29:39178 // Assigns an opener frame id for this node. This string id is only set once
179 // and cannot be changed. It persists, even if the |opener| is destroyed. It
180 // is used for attribution in the DevTools frontend.
181 void SetOpenerDevtoolsFrameToken(
182 base::UnguessableToken opener_devtools_frame_token);
183
danakjc492bf82020-09-09 20:02:44184 FrameTreeNode* child_at(size_t index) const {
185 return current_frame_host()->child_at(index);
186 }
187
188 // Returns the URL of the last committed page in the current frame.
189 const GURL& current_url() const {
190 return current_frame_host()->GetLastCommittedURL();
191 }
192
Rakina Zata Amni86c88fa2021-11-01 01:27:30193 // Sets the last committed URL for this frame.
danakjc492bf82020-09-09 20:02:44194 void SetCurrentURL(const GURL& url);
195
Rakina Zata Amni90555282022-01-21 07:35:54196 // Sets `is_on_initial_empty_document_` to false.
197 void SetNotOnInitialEmptyDocument() { is_on_initial_empty_document_ = false; }
Rakina Zata Amni86c88fa2021-11-01 01:27:30198
Rakina Zata Amni91d485b42021-12-08 02:50:13199 // Returns false if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30200 // empty document, or if the current document's input stream has been opened
201 // with document.open(), causing the document to lose its "initial empty
202 // document" status. For more details, see the definition of
203 // `is_on_initial_empty_document_`.
204 bool is_on_initial_empty_document() const {
205 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56206 }
207
Rakina Zata Amni86c88fa2021-11-01 01:27:30208 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56209 // false. Must only be called after the current document's input stream has
210 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30211 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14212
danakjc492bf82020-09-09 20:02:44213 // Returns whether the frame's owner element in the parent document is
214 // collapsed, that is, removed from the layout as if it did not exist, as per
215 // request by the embedder (of the content/ layer).
216 bool is_collapsed() const { return is_collapsed_; }
217
218 // Sets whether to collapse the frame's owner element in the parent document,
219 // that is, to remove it from the layout as if it did not exist, as per
220 // request by the embedder (of the content/ layer). Cannot be called for main
221 // frames.
222 //
223 // This only has an effect for <iframe> owner elements, and is a no-op when
224 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
225 void SetCollapsed(bool collapsed);
226
227 // Returns the origin of the last committed page in this frame.
228 // WARNING: To get the last committed origin for a particular
229 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
230 // which will behave correctly even when the RenderFrameHost is not the
231 // current one for this frame (such as when it's pending deletion).
232 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47233 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44234 }
235
Harkiran Bolaria0b3bdef02022-03-10 13:04:40236 // Set the current name and notify proxies about the update.
237 void SetFrameName(const std::string& name, const std::string& unique_name);
238
danakjc492bf82020-09-09 20:02:44239 // Returns the latest frame policy (sandbox flags and container policy) for
240 // this frame. This includes flags inherited from parent frames and the latest
241 // flags from the <iframe> element hosting this frame. The returned policies
242 // may not yet have taken effect, since "sandbox" and "allow" attribute
243 // updates in an <iframe> element take effect on next navigation. To retrieve
244 // the currently active policy for this frame, use effective_frame_policy().
245 const blink::FramePolicy& pending_frame_policy() const {
246 return pending_frame_policy_;
247 }
248
249 // Update this frame's sandbox flags and container policy. This is called
250 // when a parent frame updates the "sandbox" attribute in the <iframe> element
251 // for this frame, or any of the attributes which affect the container policy
252 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
253 // These policies won't take effect until next navigation. If this frame's
254 // parent is itself sandboxed, the parent's sandbox flags are combined with
255 // those in |frame_policy|.
256 // Attempting to change the container policy on the main frame will have no
257 // effect.
258 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
259
260 // Returns the currently active frame policy for this frame, including the
261 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39262 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44263 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
264 // origin of the iframe's src attribute (which may be different from the URL
265 // of the document currently loaded into the frame). This does not include
266 // policy changes that have been made by updating the containing iframe
267 // element attributes since the frame was last navigated; use
268 // pending_frame_policy() for those.
269 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47270 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44271 }
272
danakjc492bf82020-09-09 20:02:44273 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
274 return frame_owner_properties_;
275 }
276
277 void set_frame_owner_properties(
278 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
279 frame_owner_properties_ = frame_owner_properties;
280 }
281
282 const network::mojom::ContentSecurityPolicy* csp_attribute() {
283 return csp_attribute_.get();
284 }
285
286 void set_csp_attribute(
287 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
288 csp_attribute_ = std::move(parsed_csp_attribute);
289 }
290
Antonio Sartori5abc8de2021-07-13 08:42:47291 // Reflects the 'anonymous' attribute of the corresponding iframe html
292 // element.
293 bool anonymous() const { return anonymous_; }
Arthur Sonzogni2e9c6111392022-05-02 08:37:13294 void SetAnonymous(bool anonymous);
Antonio Sartori5abc8de2021-07-13 08:42:47295
danakjc492bf82020-09-09 20:02:44296 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47297 return render_manager_.current_replication_state().origin.IsSameOriginWith(
298 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44299 }
300
Gyuyoung Kimc16e52e92021-03-19 02:45:37301 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47302 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44303 }
304
305 RenderFrameHostImpl* current_frame_host() const {
306 return render_manager_.current_frame_host();
307 }
308
danakjc492bf82020-09-09 20:02:44309 // Returns true if this node is in a loading state.
310 bool IsLoading() const;
311
Alex Moshchuk9b0fd822020-10-26 23:08:15312 // Returns true if this node has a cross-document navigation in progress.
313 bool HasPendingCrossDocumentNavigation() const;
314
danakjc492bf82020-09-09 20:02:44315 NavigationRequest* navigation_request() { return navigation_request_.get(); }
316
317 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
318 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
319 // RenderFrameHost that is committing the navigation.
320 void TransferNavigationRequestOwnership(
321 RenderFrameHostImpl* render_frame_host);
322
323 // Takes ownership of |navigation_request| and makes it the current
324 // NavigationRequest of this frame. This corresponds to the start of a new
325 // navigation. If there was an ongoing navigation request before calling this
326 // function, it is canceled. |navigation_request| should not be null.
327 void CreatedNavigationRequest(
328 std::unique_ptr<NavigationRequest> navigation_request);
329
330 // Resets the current navigation request. If |keep_state| is true, any state
331 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
332 // loading state) will not be reset by the function.
333 void ResetNavigationRequest(bool keep_state);
334
335 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19336 // |should_show_loading_ui| indicates whether this navigation should be
337 // visible in the UI. True for cross-document navigations and navigations
Domenic Denicola30810742022-03-17 20:11:23338 // intercepted by the navigation API's transitionWhile().
danakjc492bf82020-09-09 20:02:44339 // |was_previously_loading| is false if the FrameTree was not loading before.
340 // The caller is required to provide this boolean as the delegate should only
341 // be notified if the FrameTree went from non-loading to loading state.
342 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19343 void DidStartLoading(bool should_show_loading_ui,
344 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44345
346 // A RenderFrameHost in this node stopped loading.
347 void DidStopLoading();
348
349 // The load progress for a RenderFrameHost in this node was updated to
350 // |load_progress|. This will notify the FrameTree which will in turn notify
351 // the WebContents.
352 void DidChangeLoadProgress(double load_progress);
353
354 // Called when the user directed the page to stop loading. Stops all loads
355 // happening in the FrameTreeNode. This method should be used with
356 // FrameTree::ForEach to stop all loads in the entire FrameTree.
357 bool StopLoading();
358
359 // Returns the time this frame was last focused.
360 base::TimeTicks last_focus_time() const { return last_focus_time_; }
361
362 // Called when this node becomes focused. Updates the node's last focused
363 // time and notifies observers.
364 void DidFocus();
365
366 // Called when the user closed the modal dialogue for BeforeUnload and
367 // cancelled the navigation. This should stop any load happening in the
368 // FrameTreeNode.
369 void BeforeUnloadCanceled();
370
danakjc492bf82020-09-09 20:02:44371 // Updates the user activation state in the browser frame tree and in the
372 // frame trees in all renderer processes except the renderer for this node
373 // (which initiated the update). Returns |false| if the update tries to
374 // consume an already consumed/expired transient state, |true| otherwise. See
375 // the comment on user_activation_state_ below.
376 //
377 // The |notification_type| parameter is used for histograms, only for the case
378 // |update_state == kNotifyActivation|.
379 bool UpdateUserActivationState(
380 blink::mojom::UserActivationUpdateType update_type,
381 blink::mojom::UserActivationNotificationType notification_type);
382
danakjc492bf82020-09-09 20:02:44383 // Returns the sandbox flags currently in effect for this frame. This includes
384 // flags inherited from parent frames, the currently active flags from the
385 // <iframe> element hosting this frame, as well as any flags set from a
386 // Content-Security-Policy HTTP header. This does not include flags that have
387 // have been updated in an <iframe> element but have not taken effect yet; use
388 // pending_frame_policy() for those. To see the flags which will take effect
389 // on navigation (which does not include the CSP-set flags), use
390 // effective_frame_policy().
391 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47392 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44393 }
394
danakjc492bf82020-09-09 20:02:44395 // Returns whether the frame received a user gesture on a previous navigation
396 // on the same eTLD+1.
397 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47398 return render_manager_.current_replication_state()
399 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44400 }
401
402 // When a tab is discarded, WebContents sets was_discarded on its
403 // root FrameTreeNode.
404 // In addition, when a child frame is created, this bit is passed on from
405 // parent to child.
406 // When a navigation request is created, was_discarded is passed on to the
407 // request and reset to false in FrameTreeNode.
408 void set_was_discarded() { was_discarded_ = true; }
409 bool was_discarded() const { return was_discarded_; }
410
411 // Returns the sticky bit of the User Activation v2 state of the
412 // |FrameTreeNode|.
413 bool HasStickyUserActivation() const {
414 return user_activation_state_.HasBeenActive();
415 }
416
417 // Returns the transient bit of the User Activation v2 state of the
418 // |FrameTreeNode|.
419 bool HasTransientUserActivation() {
420 return user_activation_state_.IsActive();
421 }
422
423 // Remove history entries for all frames created by script in this frame's
424 // subtree. If a frame created by a script is removed, then its history entry
425 // will never be reused - this saves memory.
426 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
427
Kevin McNee43fe8292021-10-04 22:59:41428 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45429 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44430 }
danakjc492bf82020-09-09 20:02:44431
Daniel Cheng6ac128172021-05-25 18:49:01432 blink::mojom::TreeScopeType tree_scope_type() const {
433 return tree_scope_type_;
434 }
435
arthursonzogni034bb9c2020-10-01 08:29:56436 // The initial popup URL for new window opened using:
437 // `window.open(initial_popup_url)`.
438 // An empty GURL otherwise.
439 //
440 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
441 // document served from this URL. The FrameTreeNode always starts hosting the
442 // initial empty document and attempts a navigation toward this URL. However
443 // the navigation might be delayed, redirected and even cancelled.
444 void SetInitialPopupURL(const GURL& initial_popup_url);
445 const GURL& initial_popup_url() const { return initial_popup_url_; }
446
447 // The origin of the document that used window.open() to create this frame.
448 // Otherwise, an opaque Origin with a nonce different from all previously
449 // existing Origins.
450 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
451 const url::Origin& popup_creator_origin() const {
452 return popup_creator_origin_;
453 }
454
Harkiran Bolaria59290d62021-03-17 01:53:01455 // Sets the associated FrameTree for this node. The node can change FrameTrees
456 // when blink::features::Prerender2 is enabled, which allows a page loaded in
457 // the prerendered FrameTree to be used for a navigation in the primary frame
458 // tree.
459 void SetFrameTree(FrameTree& frame_tree);
460
Alexander Timin074cd182022-03-23 18:11:22461 using TraceProto = perfetto::protos::pbzero::FrameTreeNodeInfo;
Alexander Timinf785f342021-03-18 00:00:56462 // Write a representation of this object into a trace.
Alexander Timin074cd182022-03-23 18:11:22463 void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
Alexander Timinf785f342021-03-18 00:00:56464
Carlos Caballero76711352021-03-24 17:38:21465 // Returns true the node is navigating, i.e. it has an associated
466 // NavigationRequest.
467 bool HasNavigation();
468
shivanigithubf3ddff52021-07-03 22:06:30469 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30470 // Note that these two functions cannot be invoked from a FrameTree's or
471 // its root node's constructor since they require the frame tree and the
472 // root node to be completely constructed.
473 //
shivanigithubf3ddff52021-07-03 22:06:30474 // Returns false if fenced frames are disabled. Returns true if the feature is
475 // enabled and if |this| is a fenced frame. Returns false for
476 // iframes embedded in a fenced frame. To clarify: for the MPArch
477 // implementation this only returns true if |this| is the actual
478 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
479 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36480 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30481
482 // Returns false if fenced frames are disabled. Returns true if the
483 // feature is enabled and if |this| or any of its ancestor nodes is a
484 // fenced frame.
485 bool IsInFencedFrameTree() const;
486
shivanigithub4cd016a2021-09-20 21:10:30487 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
488 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
489 // details.
490 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
491 return fenced_frame_nonce_;
492 }
493
494 // If applicable, set the fenced frame nonce. See comment on
495 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
496 // by FrameTree::Init() or FrameTree::AddFrame().
497 void SetFencedFrameNonceIfNeeded();
498
Nan Line376738a2022-03-25 22:05:41499 // Returns the mode attribute set on the fenced frame if this is a fenced
500 // frame root, otherwise returns `absl::nullopt`.
501 absl::optional<blink::mojom::FencedFrameMode> GetFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16502
Dave Tapuskac8de3b02021-12-03 21:51:01503 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
504 // Do not use directly.
505 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
506
Harkiran Bolariab4437fd2021-08-11 17:51:22507 // Sets the unique_name and name fields on replication_state_. To be used in
508 // prerender activation to make sure the FrameTreeNode replication state is
509 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
510 // renderers should already have the correct value, so unlike
511 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47512 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
513 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22514 void set_frame_name_for_activation(const std::string& unique_name,
515 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40516 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
517 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22518 }
519
Nan Linaaf84f72021-12-02 22:31:56520 // Returns true if error page isolation is enabled.
521 bool IsErrorPageIsolationEnabled() const;
522
W. James MacLean81b8d01f2022-01-25 20:50:59523 // Functions to store and retrieve a frame's srcdoc value on this
524 // FrameTreeNode.
525 void SetSrcdocValue(const std::string& srcdoc_value);
526 const std::string& srcdoc_value() const { return srcdoc_value_; }
527
Harkiran Bolariaebbe7702022-02-22 19:19:03528 // Accessor to BrowsingContextState for subframes only. Only main frame
529 // navigations can change BrowsingInstances and BrowsingContextStates,
530 // therefore for subframes associated BrowsingContextState never changes. This
531 // helper method makes this more explicit and guards against calling this on
532 // main frames (there an appropriate BrowsingContextState should be obtained
533 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
534 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
535 // the same frame).
536 const scoped_refptr<BrowsingContextState>&
537 GetBrowsingContextStateForSubframe() const;
538
Arthur Hemerye4659282022-03-28 08:36:15539 // Clears the opener property of popups referencing this FrameTreeNode as
540 // their opener.
541 void ClearOpenerReferences();
542
danakjc492bf82020-09-09 20:02:44543 private:
Charlie Hubb5943d2021-03-09 19:46:12544 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44545 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12546 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44547 ContainerPolicySandboxDynamic);
548
Dominic Farolino8a2187b2021-12-24 20:44:21549 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
550 // representing an inner FrameTree, this method destroys said inner FrameTree.
551 void DestroyInnerFrameTreeIfExists();
552
danakjc492bf82020-09-09 20:02:44553 class OpenerDestroyedObserver;
554
danakjc492bf82020-09-09 20:02:44555 // The |notification_type| parameter is used for histograms only.
556 bool NotifyUserActivation(
557 blink::mojom::UserActivationNotificationType notification_type);
558
559 bool ConsumeTransientUserActivation();
560
561 bool ClearUserActivation();
562
563 // Verify that the renderer process is allowed to set user activation on this
564 // frame by checking whether this frame's RenderWidgetHost had previously seen
565 // an input event that might lead to user activation. If user activation
566 // should be allowed, this returns true and also clears corresponding pending
567 // user activation state in the widget. Otherwise, this returns false.
568 bool VerifyUserActivation();
569
570 // The next available browser-global FrameTreeNode ID.
571 static int next_frame_tree_node_id_;
572
573 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52574 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44575
danakjc492bf82020-09-09 20:02:44576 // A browser-global identifier for the frame in the page, which stays stable
577 // even if the frame does a cross-process navigation.
578 const int frame_tree_node_id_;
579
580 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
581 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52582 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44583
danakjc492bf82020-09-09 20:02:44584 // The frame that opened this frame, if any. Will be set to null if the
585 // opener is closed, or if this frame disowns its opener by setting its
586 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52587 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44588
589 // An observer that clears this node's |opener_| if the opener is destroyed.
590 // This observer is added to the |opener_|'s observer list when the |opener_|
591 // is set to a non-null node, and it is removed from that list when |opener_|
592 // changes or when this node is destroyed. It is also cleared if |opener_|
593 // is disowned.
594 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
595
Rakina Zata Amni3a48ae42022-05-05 03:39:56596 // Unlike `opener_`, the "original opener chain" doesn't reflect
597 // window.opener, which can be suppressed or updated. The "original opener"
598 // is the main frame of the actual opener of this frame. This traces the all
599 // the way back, so if the original opener was closed (deleted or severed due
600 // to COOP), but _it_ had an original opener, this will return the original
601 // opener's original opener, etc. So this value will always be set as long as
602 // there is at least one live frame in the chain whose connection is not
603 // severed due to COOP.
604 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
605 nullptr;
danakjc492bf82020-09-09 20:02:44606
Wolfgang Beyerd8809db2020-09-30 15:29:39607 // The devtools frame token of the frame which opened this frame. This is
608 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07609 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39610
Rakina Zata Amni3a48ae42022-05-05 03:39:56611 // An observer that updates this node's
612 // |first_live_main_frame_in_original_opener_chain_| to the next original
613 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44614 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
615
arthursonzogni034bb9c2020-10-01 08:29:56616 // When created by an opener, the URL specified in window.open(url)
617 // Please refer to {Get,Set}InitialPopupURL() documentation.
618 GURL initial_popup_url_;
619
620 // When created using window.open, the origin of the creator.
621 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
622 url::Origin popup_creator_origin_;
623
W. James MacLean81b8d01f2022-01-25 20:50:59624 // If the url from the the last BeginNavigation is about:srcdoc, this value
625 // stores the srcdoc_attribute's value for re-use in history navigations.
626 std::string srcdoc_value_;
627
Rakina Zata Amni86c88fa2021-11-01 01:27:30628 // Whether this frame is still on the initial about:blank document or the
629 // synchronously committed about:blank document committed at frame creation,
630 // and its "initial empty document"-ness is still true.
631 // This will be false if either of these has happened:
632 // - SetCurrentUrl() was called after committing a document that is not the
633 // initial about:blank document or the synchronously committed about:blank
634 // document, per
635 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
636 // - The document's input stream has been opened with document.open(), per
637 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
638 // NOTE: we treat both the "initial about:blank document" and the
639 // "synchronously committed about:blank document" as the initial empty
640 // document. In the future, we plan to remove the synchronous about:blank
641 // commit so that this state will only be true if the frame is on the
642 // "initial about:blank document". See also:
643 // - https://github.com/whatwg/html/issues/6863
644 // - https://crbug.com/1215096
645 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56646
danakjc492bf82020-09-09 20:02:44647 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19648 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44649
Daniel Cheng6ac128172021-05-25 18:49:01650 // The type of frame owner for this frame. This is only relevant for non-main
651 // frames.
Kevin McNee43fe8292021-10-04 22:59:41652 const blink::FrameOwnerElementType frame_owner_element_type_ =
653 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45654
Daniel Cheng6ac128172021-05-25 18:49:01655 // The tree scope type of frame owner element, i.e. whether the element is in
656 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
657 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
658 // relevant for non-main frames.
659 const blink::mojom::TreeScopeType tree_scope_type_ =
660 blink::mojom::TreeScopeType::kDocument;
661
danakjc492bf82020-09-09 20:02:44662 // Track the pending sandbox flags and container policy for this frame. When a
663 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
664 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47665 // is stored here, and transferred into
666 // render_manager_.current_replication_state().frame_policy when they take
667 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44668 blink::FramePolicy pending_frame_policy_;
669
670 // Whether the frame was created by javascript. This is useful to prune
671 // history entries when the frame is removed (because frames created by
672 // scripts are never recreated with the same unique name - see
673 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19674 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44675
676 // Used for devtools instrumentation and trace-ability. The token is
677 // propagated to Blink's LocalFrame and both Blink and content/
678 // can tag calls and requests with this token in order to attribute them
679 // to the context frame.
680 // |devtools_frame_token_| is only defined by the browser process and is never
681 // sent back from the renderer in the control calls. It should be never used
682 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19683 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44684
685 // Tracks the scrolling and margin properties for this frame. These
686 // properties affect the child renderer but are stored on its parent's
687 // frame element. When this frame's parent dynamically updates these
688 // properties, we update them here too.
689 //
690 // Note that dynamic updates only take effect on the next frame navigation.
691 blink::mojom::FrameOwnerProperties frame_owner_properties_;
692
693 // Contains the current parsed value of the 'csp' attribute of this frame.
694 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
695
Antonio Sartori5abc8de2021-07-13 08:42:47696 // Reflects the 'anonymous' attribute of the corresponding iframe html
697 // element.
698 bool anonymous_ = false;
699
danakjc492bf82020-09-09 20:02:44700 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
701 // be reset and a RenderFrameHost will be responsible for the navigation.
702 std::unique_ptr<NavigationRequest> navigation_request_;
703
704 // List of objects observing this FrameTreeNode.
705 base::ObserverList<Observer>::Unchecked observers_;
706
707 base::TimeTicks last_focus_time_;
708
arthursonzogni9816b9192021-03-29 16:09:19709 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44710
711 // The user activation state of the current frame. See |UserActivationState|
712 // for details on how this state is maintained.
713 blink::UserActivationState user_activation_state_;
714
shivanigithub4cd016a2021-09-20 21:10:30715 // Fenced Frames:
716 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
717 // frame and any iframes nested within it. Not set if this frame is not in a
718 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
719 // the MPArch version but for the shadow DOM version we need to keep it here
720 // since the fenced frame root is not a main frame for the latter. The value
721 // of the nonce will be the same for all of the the frames inside a fenced
722 // frame tree. If there is a nested fenced frame it will have a different
723 // nonce than its parent fenced frame. The nonce will stay the same across
724 // navigations because it is always used in conjunction with other fields of
725 // the keys. If the navigation is same-origin/site then the same network stack
726 // partition/storage will be reused and if it's cross-origin/site then other
727 // parts of the key will change and so, even with the same nonce, another
728 // partition will be used.
729 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
730
Harkiran Bolaria16f2c48d2022-04-22 12:39:57731 const RenderFrameHostImpl::FencedFrameStatus fenced_frame_status_ =
732 RenderFrameHostImpl::FencedFrameStatus::kNotNestedInFencedFrame;
733
Lukasz Anforowicz147141962020-12-16 18:03:24734 // Manages creation and swapping of RenderFrameHosts for this frame.
735 //
736 // This field needs to be declared last, because destruction of
737 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
738 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
739 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
740 // may try to use FrameTreeNode's fields above - this would be an undefined
741 // behavior if the fields (even trivially-destructible ones) were destructed
742 // before the RenderFrameHostManager's destructor runs. See also
743 // https://crbug.com/1157988.
744 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44745};
746
747} // namespace content
748
749#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_