blob: ef4a5d3cb4e69eb159b19baf94575f6f33dcc2e7 [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>
12#include <vector>
13
14#include "base/gtest_prod_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
danakjc492bf82020-09-09 20:02:4416#include "base/memory/ref_counted.h"
17#include "content/browser/renderer_host/frame_tree.h"
18#include "content/browser/renderer_host/frame_tree_node_blame_context.h"
19#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"
danakjc492bf82020-09-09 20:02:4423#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Lei Zhang698df03c2021-05-21 04:23:3424#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin McNee43fe8292021-10-04 22:59:4125#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4426#include "third_party/blink/public/common/frame/frame_policy.h"
27#include "third_party/blink/public/common/frame/user_activation_state.h"
danakjc492bf82020-09-09 20:02:4428#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3729#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0130#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
danakjc492bf82020-09-09 20:02:4431#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
32#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-forward.h"
33
34#include "url/gurl.h"
35#include "url/origin.h"
36
37namespace content {
38
39class NavigationRequest;
40class RenderFrameHostImpl;
41class NavigationEntryImpl;
42
43// When a page contains iframes, its renderer process maintains a tree structure
44// of those frames. We are mirroring this tree in the browser process. This
45// class represents a node in this tree and is a wrapper for all objects that
46// are frame-specific (as opposed to page-specific).
47//
48// Each FrameTreeNode has a current RenderFrameHost, which can change over
49// time as the frame is navigated. Any immediate subframes of the current
50// document are tracked using FrameTreeNodes owned by the current
51// RenderFrameHost, rather than as children of FrameTreeNode itself. This
52// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
53// still alive - for example while pending deletion, after a new current
54// RenderFrameHost has replaced it.
55class CONTENT_EXPORT FrameTreeNode {
56 public:
57 class Observer {
58 public:
59 // Invoked when a FrameTreeNode is being destroyed.
60 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
61
62 // Invoked when a FrameTreeNode becomes focused.
63 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
64
Fergal Dalya1d569972021-03-16 03:24:5365 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4466 };
67
68 static const int kFrameTreeNodeInvalidId;
69
70 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
71 // regardless of which FrameTree it is in.
72 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
73
74 // Returns the FrameTreeNode for the given |rfh|. Same as
75 // rfh->frame_tree_node(), but also supports nullptrs.
76 static FrameTreeNode* From(RenderFrameHost* rfh);
77
78 // Callers are are expected to initialize sandbox flags separately after
79 // calling the constructor.
80 FrameTreeNode(
81 FrameTree* frame_tree,
82 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0183 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4484 const std::string& name,
85 const std::string& unique_name,
86 bool is_created_by_script,
87 const base::UnguessableToken& devtools_frame_token,
88 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4189 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3490 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4491
Peter Boström828b9022021-09-21 02:28:4392 FrameTreeNode(const FrameTreeNode&) = delete;
93 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
94
danakjc492bf82020-09-09 20:02:4495 ~FrameTreeNode();
96
97 void AddObserver(Observer* observer);
98 void RemoveObserver(Observer* observer);
99
100 bool IsMainFrame() const;
101
arthursonzogni76098e52020-11-25 14:18:45102 // Clears any state in this node which was set by the document itself (CSP &
103 // UserActivationState) and notifies proxies as appropriate. Invoked after
104 // committing navigation to a new document (since the new document comes with
105 // a fresh set of CSP).
106 // TODO(arthursonzogni): Remove this function. The frame/document must not be
107 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13108 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44109
110 FrameTree* frame_tree() const { return frame_tree_; }
111 Navigator& navigator() { return frame_tree()->navigator(); }
112
113 RenderFrameHostManager* render_manager() { return &render_manager_; }
114 int frame_tree_node_id() const { return frame_tree_node_id_; }
Antonio Sartori90f41212021-01-22 10:08:34115 const std::string& frame_name() const { return replication_state_->name; }
danakjc492bf82020-09-09 20:02:44116
117 const std::string& unique_name() const {
Antonio Sartori90f41212021-01-22 10:08:34118 return replication_state_->unique_name;
danakjc492bf82020-09-09 20:02:44119 }
120
121 // See comment on the member declaration.
122 const base::UnguessableToken& devtools_frame_token() const {
123 return devtools_frame_token_;
124 }
125
126 size_t child_count() const { return current_frame_host()->child_count(); }
127
danakjc492bf82020-09-09 20:02:44128 RenderFrameHostImpl* parent() const { return parent_; }
129
130 FrameTreeNode* opener() const { return opener_; }
131
132 FrameTreeNode* original_opener() const { return original_opener_; }
133
Anton Bikineevf62d1bf2021-05-15 17:56:07134 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39135 return opener_devtools_frame_token_;
136 }
137
danakjc492bf82020-09-09 20:02:44138 // Gets the total number of descendants to this FrameTreeNode in addition to
139 // this node.
140 size_t GetFrameTreeSize() const;
141
142 // Assigns a new opener for this node and, if |opener| is non-null, registers
143 // an observer that will clear this node's opener if |opener| is ever
144 // destroyed.
145 void SetOpener(FrameTreeNode* opener);
146
147 // Assigns the initial opener for this node, and if |opener| is non-null,
148 // registers an observer that will clear this node's opener if |opener| is
149 // ever destroyed. The value set here is the root of the tree.
150 //
151 // It is not possible to change the opener once it was set.
152 void SetOriginalOpener(FrameTreeNode* opener);
153
Wolfgang Beyerd8809db2020-09-30 15:29:39154 // Assigns an opener frame id for this node. This string id is only set once
155 // and cannot be changed. It persists, even if the |opener| is destroyed. It
156 // is used for attribution in the DevTools frontend.
157 void SetOpenerDevtoolsFrameToken(
158 base::UnguessableToken opener_devtools_frame_token);
159
danakjc492bf82020-09-09 20:02:44160 FrameTreeNode* child_at(size_t index) const {
161 return current_frame_host()->child_at(index);
162 }
163
164 // Returns the URL of the last committed page in the current frame.
165 const GURL& current_url() const {
166 return current_frame_host()->GetLastCommittedURL();
167 }
168
Rakina Zata Amni86c88fa2021-11-01 01:27:30169 // Sets the last committed URL for this frame.
danakjc492bf82020-09-09 20:02:44170 void SetCurrentURL(const GURL& url);
171
Rakina Zata Amni86c88fa2021-11-01 01:27:30172 // The frame committed a document that is not the initial empty document.
173 // Update `has_committed_real_load_` and `is_on_initial_empty_document_`
174 // accordingly.
175 void DidCommitNonInitialEmptyDocument();
176
177 // Returns true if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30178 // empty document, or if the current document's input stream has been opened
179 // with document.open(), causing the document to lose its "initial empty
180 // document" status. For more details, see the definition of
181 // `is_on_initial_empty_document_`.
182 bool is_on_initial_empty_document() const {
183 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56184 }
185
Rakina Zata Amni86c88fa2021-11-01 01:27:30186 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56187 // false. Must only be called after the current document's input stream has
188 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30189 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14190
danakjc492bf82020-09-09 20:02:44191 // Returns whether the frame's owner element in the parent document is
192 // collapsed, that is, removed from the layout as if it did not exist, as per
193 // request by the embedder (of the content/ layer).
194 bool is_collapsed() const { return is_collapsed_; }
195
196 // Sets whether to collapse the frame's owner element in the parent document,
197 // that is, to remove it from the layout as if it did not exist, as per
198 // request by the embedder (of the content/ layer). Cannot be called for main
199 // frames.
200 //
201 // This only has an effect for <iframe> owner elements, and is a no-op when
202 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
203 void SetCollapsed(bool collapsed);
204
205 // Returns the origin of the last committed page in this frame.
206 // WARNING: To get the last committed origin for a particular
207 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
208 // which will behave correctly even when the RenderFrameHost is not the
209 // current one for this frame (such as when it's pending deletion).
210 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34211 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44212 }
213
214 // Set the current origin and notify proxies about the update.
215 void SetCurrentOrigin(const url::Origin& origin,
216 bool is_potentially_trustworthy_unique_origin);
217
218 // Set the current name and notify proxies about the update.
219 void SetFrameName(const std::string& name, const std::string& unique_name);
220
danakjc492bf82020-09-09 20:02:44221 // Sets the current insecure request policy, and notifies proxies about the
222 // update.
223 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
224
225 // Sets the current set of insecure urls to upgrade, and notifies proxies
226 // about the update.
227 void SetInsecureNavigationsSet(
228 const std::vector<uint32_t>& insecure_navigations_set);
229
230 // Returns the latest frame policy (sandbox flags and container policy) for
231 // this frame. This includes flags inherited from parent frames and the latest
232 // flags from the <iframe> element hosting this frame. The returned policies
233 // may not yet have taken effect, since "sandbox" and "allow" attribute
234 // updates in an <iframe> element take effect on next navigation. To retrieve
235 // the currently active policy for this frame, use effective_frame_policy().
236 const blink::FramePolicy& pending_frame_policy() const {
237 return pending_frame_policy_;
238 }
239
240 // Update this frame's sandbox flags and container policy. This is called
241 // when a parent frame updates the "sandbox" attribute in the <iframe> element
242 // for this frame, or any of the attributes which affect the container policy
243 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
244 // These policies won't take effect until next navigation. If this frame's
245 // parent is itself sandboxed, the parent's sandbox flags are combined with
246 // those in |frame_policy|.
247 // Attempting to change the container policy on the main frame will have no
248 // effect.
249 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
250
251 // Returns the currently active frame policy for this frame, including the
252 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39253 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44254 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
255 // origin of the iframe's src attribute (which may be different from the URL
256 // of the document currently loaded into the frame). This does not include
257 // policy changes that have been made by updating the containing iframe
258 // element attributes since the frame was last navigated; use
259 // pending_frame_policy() for those.
260 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34261 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44262 }
263
264 // Set the frame_policy provided in function parameter as active frame policy,
265 // while leaving pending_frame_policy_ untouched.
266 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
267
268 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
269 return frame_owner_properties_;
270 }
271
272 void set_frame_owner_properties(
273 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
274 frame_owner_properties_ = frame_owner_properties;
275 }
276
277 const network::mojom::ContentSecurityPolicy* csp_attribute() {
278 return csp_attribute_.get();
279 }
280
281 void set_csp_attribute(
282 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
283 csp_attribute_ = std::move(parsed_csp_attribute);
284 }
285
Antonio Sartori5abc8de2021-07-13 08:42:47286 // Reflects the 'anonymous' attribute of the corresponding iframe html
287 // element.
288 bool anonymous() const { return anonymous_; }
289 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
290
danakjc492bf82020-09-09 20:02:44291 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34292 return replication_state_->origin.IsSameOriginWith(
293 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44294 }
295
Gyuyoung Kimc16e52e92021-03-19 02:45:37296 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34297 return *replication_state_;
danakjc492bf82020-09-09 20:02:44298 }
299
300 RenderFrameHostImpl* current_frame_host() const {
301 return render_manager_.current_frame_host();
302 }
303
danakjc492bf82020-09-09 20:02:44304 // Returns true if this node is in a loading state.
305 bool IsLoading() const;
306
Alex Moshchuk9b0fd822020-10-26 23:08:15307 // Returns true if this node has a cross-document navigation in progress.
308 bool HasPendingCrossDocumentNavigation() const;
309
danakjc492bf82020-09-09 20:02:44310 NavigationRequest* navigation_request() { return navigation_request_.get(); }
311
312 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
313 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
314 // RenderFrameHost that is committing the navigation.
315 void TransferNavigationRequestOwnership(
316 RenderFrameHostImpl* render_frame_host);
317
318 // Takes ownership of |navigation_request| and makes it the current
319 // NavigationRequest of this frame. This corresponds to the start of a new
320 // navigation. If there was an ongoing navigation request before calling this
321 // function, it is canceled. |navigation_request| should not be null.
322 void CreatedNavigationRequest(
323 std::unique_ptr<NavigationRequest> navigation_request);
324
325 // Resets the current navigation request. If |keep_state| is true, any state
326 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
327 // loading state) will not be reset by the function.
328 void ResetNavigationRequest(bool keep_state);
329
330 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19331 // |should_show_loading_ui| indicates whether this navigation should be
332 // visible in the UI. True for cross-document navigations and navigations
333 // intercepted by appHistory's transitionWhile().
danakjc492bf82020-09-09 20:02:44334 // |was_previously_loading| is false if the FrameTree was not loading before.
335 // The caller is required to provide this boolean as the delegate should only
336 // be notified if the FrameTree went from non-loading to loading state.
337 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19338 void DidStartLoading(bool should_show_loading_ui,
339 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44340
341 // A RenderFrameHost in this node stopped loading.
342 void DidStopLoading();
343
344 // The load progress for a RenderFrameHost in this node was updated to
345 // |load_progress|. This will notify the FrameTree which will in turn notify
346 // the WebContents.
347 void DidChangeLoadProgress(double load_progress);
348
349 // Called when the user directed the page to stop loading. Stops all loads
350 // happening in the FrameTreeNode. This method should be used with
351 // FrameTree::ForEach to stop all loads in the entire FrameTree.
352 bool StopLoading();
353
354 // Returns the time this frame was last focused.
355 base::TimeTicks last_focus_time() const { return last_focus_time_; }
356
357 // Called when this node becomes focused. Updates the node's last focused
358 // time and notifies observers.
359 void DidFocus();
360
361 // Called when the user closed the modal dialogue for BeforeUnload and
362 // cancelled the navigation. This should stop any load happening in the
363 // FrameTreeNode.
364 void BeforeUnloadCanceled();
365
366 // Returns the BlameContext associated with this node.
367 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
368
369 // Updates the user activation state in the browser frame tree and in the
370 // frame trees in all renderer processes except the renderer for this node
371 // (which initiated the update). Returns |false| if the update tries to
372 // consume an already consumed/expired transient state, |true| otherwise. See
373 // the comment on user_activation_state_ below.
374 //
375 // The |notification_type| parameter is used for histograms, only for the case
376 // |update_state == kNotifyActivation|.
377 bool UpdateUserActivationState(
378 blink::mojom::UserActivationUpdateType update_type,
379 blink::mojom::UserActivationNotificationType notification_type);
380
381 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
382
383 // 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 {
Antonio Sartori90f41212021-01-22 10:08:34392 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44393 }
394
395 // Updates the active sandbox flags in this frame, in response to a
396 // Content-Security-Policy header adding additional flags, in addition to
397 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39398 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44399 // will be cleared, and the flags in the pending frame policy will be applied
400 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31401 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39402 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31403 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44404 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06405 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44406
407 // Returns whether the frame received a user gesture on a previous navigation
408 // on the same eTLD+1.
409 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34410 return replication_state_->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
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
Alex Turner10d557a42021-06-01 19:06:49447 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44448
arthursonzogni034bb9c2020-10-01 08:29:56449 // The initial popup URL for new window opened using:
450 // `window.open(initial_popup_url)`.
451 // An empty GURL otherwise.
452 //
453 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
454 // document served from this URL. The FrameTreeNode always starts hosting the
455 // initial empty document and attempts a navigation toward this URL. However
456 // the navigation might be delayed, redirected and even cancelled.
457 void SetInitialPopupURL(const GURL& initial_popup_url);
458 const GURL& initial_popup_url() const { return initial_popup_url_; }
459
460 // The origin of the document that used window.open() to create this frame.
461 // Otherwise, an opaque Origin with a nonce different from all previously
462 // existing Origins.
463 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
464 const url::Origin& popup_creator_origin() const {
465 return popup_creator_origin_;
466 }
467
Harkiran Bolaria59290d62021-03-17 01:53:01468 // Sets the associated FrameTree for this node. The node can change FrameTrees
469 // when blink::features::Prerender2 is enabled, which allows a page loaded in
470 // the prerendered FrameTree to be used for a navigation in the primary frame
471 // tree.
472 void SetFrameTree(FrameTree& frame_tree);
473
Alexander Timinf785f342021-03-18 00:00:56474 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24475 void WriteIntoTrace(perfetto::TracedValue context) const;
Rakina Zata Amni4b1968d2021-09-09 03:29:47476 void WriteIntoTrace(
477 perfetto::TracedProto<perfetto::protos::pbzero::FrameTreeNodeInfo> proto);
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
Harkiran Bolariab4437fd2021-08-11 17:51:22513 // Sets the unique_name and name fields on replication_state_. To be used in
514 // prerender activation to make sure the FrameTreeNode replication state is
515 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
516 // renderers should already have the correct value, so unlike
517 // FrameTreeNode::SetFrameName, we do not notify them here.
518 // TODO(https://crbug.com/1237091): Remove this once the Browsing Instance
519 // Frame State is implemented.
520 void set_frame_name_for_activation(const std::string& unique_name,
521 const std::string& name) {
522 replication_state_->unique_name = unique_name;
523 replication_state_->name = name;
524 }
525
Nan Linaaf84f72021-12-02 22:31:56526 // Returns true if error page isolation is enabled.
527 bool IsErrorPageIsolationEnabled() const;
528
danakjc492bf82020-09-09 20:02:44529 private:
Charlie Hubb5943d2021-03-09 19:46:12530 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44531 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12532 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44533 ContainerPolicySandboxDynamic);
534
535 class OpenerDestroyedObserver;
536
danakjc492bf82020-09-09 20:02:44537 // The |notification_type| parameter is used for histograms only.
538 bool NotifyUserActivation(
539 blink::mojom::UserActivationNotificationType notification_type);
540
541 bool ConsumeTransientUserActivation();
542
543 bool ClearUserActivation();
544
545 // Verify that the renderer process is allowed to set user activation on this
546 // frame by checking whether this frame's RenderWidgetHost had previously seen
547 // an input event that might lead to user activation. If user activation
548 // should be allowed, this returns true and also clears corresponding pending
549 // user activation state in the widget. Otherwise, this returns false.
550 bool VerifyUserActivation();
551
552 // The next available browser-global FrameTreeNode ID.
553 static int next_frame_tree_node_id_;
554
555 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52556 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44557
danakjc492bf82020-09-09 20:02:44558 // A browser-global identifier for the frame in the page, which stays stable
559 // even if the frame does a cross-process navigation.
560 const int frame_tree_node_id_;
561
562 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
563 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52564 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44565
danakjc492bf82020-09-09 20:02:44566 // The frame that opened this frame, if any. Will be set to null if the
567 // opener is closed, or if this frame disowns its opener by setting its
568 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52569 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44570
571 // An observer that clears this node's |opener_| if the opener is destroyed.
572 // This observer is added to the |opener_|'s observer list when the |opener_|
573 // is set to a non-null node, and it is removed from that list when |opener_|
574 // changes or when this node is destroyed. It is also cleared if |opener_|
575 // is disowned.
576 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
577
578 // The frame that opened this frame, if any. Contrary to opener_, this
579 // cannot be changed unless the original opener is destroyed.
Keishi Hattori0e45c022021-11-27 09:25:52580 raw_ptr<FrameTreeNode> original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44581
Wolfgang Beyerd8809db2020-09-30 15:29:39582 // The devtools frame token of the frame which opened this frame. This is
583 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07584 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39585
danakjc492bf82020-09-09 20:02:44586 // An observer that clears this node's |original_opener_| if the opener is
587 // destroyed.
588 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
589
arthursonzogni034bb9c2020-10-01 08:29:56590 // When created by an opener, the URL specified in window.open(url)
591 // Please refer to {Get,Set}InitialPopupURL() documentation.
592 GURL initial_popup_url_;
593
594 // When created using window.open, the origin of the creator.
595 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
596 url::Origin popup_creator_origin_;
597
Rakina Zata Amni86c88fa2021-11-01 01:27:30598 // Whether this frame is still on the initial about:blank document or the
599 // synchronously committed about:blank document committed at frame creation,
600 // and its "initial empty document"-ness is still true.
601 // This will be false if either of these has happened:
602 // - SetCurrentUrl() was called after committing a document that is not the
603 // initial about:blank document or the synchronously committed about:blank
604 // document, per
605 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
606 // - The document's input stream has been opened with document.open(), per
607 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
608 // NOTE: we treat both the "initial about:blank document" and the
609 // "synchronously committed about:blank document" as the initial empty
610 // document. In the future, we plan to remove the synchronous about:blank
611 // commit so that this state will only be true if the frame is on the
612 // "initial about:blank document". See also:
613 // - https://github.com/whatwg/html/issues/6863
614 // - https://crbug.com/1215096
615 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56616
danakjc492bf82020-09-09 20:02:44617 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19618 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44619
Daniel Cheng6ac128172021-05-25 18:49:01620 // The type of frame owner for this frame. This is only relevant for non-main
621 // frames.
Kevin McNee43fe8292021-10-04 22:59:41622 const blink::FrameOwnerElementType frame_owner_element_type_ =
623 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45624
Daniel Cheng6ac128172021-05-25 18:49:01625 // The tree scope type of frame owner element, i.e. whether the element is in
626 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
627 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
628 // relevant for non-main frames.
629 const blink::mojom::TreeScopeType tree_scope_type_ =
630 blink::mojom::TreeScopeType::kDocument;
631
danakjc492bf82020-09-09 20:02:44632 // Track information that needs to be replicated to processes that have
633 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37634 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44635
636 // Track the pending sandbox flags and container policy for this frame. When a
637 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
638 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34639 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44640 // they take effect on the next frame navigation.
641 blink::FramePolicy pending_frame_policy_;
642
643 // Whether the frame was created by javascript. This is useful to prune
644 // history entries when the frame is removed (because frames created by
645 // scripts are never recreated with the same unique name - see
646 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19647 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44648
649 // Used for devtools instrumentation and trace-ability. The token is
650 // propagated to Blink's LocalFrame and both Blink and content/
651 // can tag calls and requests with this token in order to attribute them
652 // to the context frame.
653 // |devtools_frame_token_| is only defined by the browser process and is never
654 // sent back from the renderer in the control calls. It should be never used
655 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19656 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44657
658 // Tracks the scrolling and margin properties for this frame. These
659 // properties affect the child renderer but are stored on its parent's
660 // frame element. When this frame's parent dynamically updates these
661 // properties, we update them here too.
662 //
663 // Note that dynamic updates only take effect on the next frame navigation.
664 blink::mojom::FrameOwnerProperties frame_owner_properties_;
665
666 // Contains the current parsed value of the 'csp' attribute of this frame.
667 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
668
Antonio Sartori5abc8de2021-07-13 08:42:47669 // Reflects the 'anonymous' attribute of the corresponding iframe html
670 // element.
671 bool anonymous_ = false;
672
danakjc492bf82020-09-09 20:02:44673 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
674 // be reset and a RenderFrameHost will be responsible for the navigation.
675 std::unique_ptr<NavigationRequest> navigation_request_;
676
677 // List of objects observing this FrameTreeNode.
678 base::ObserverList<Observer>::Unchecked observers_;
679
680 base::TimeTicks last_focus_time_;
681
arthursonzogni9816b9192021-03-29 16:09:19682 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44683
684 // The user activation state of the current frame. See |UserActivationState|
685 // for details on how this state is maintained.
686 blink::UserActivationState user_activation_state_;
687
688 // A helper for tracing the snapshots of this FrameTreeNode and attributing
689 // browser process activities to this node (when possible). It is unrelated
690 // to the core logic of FrameTreeNode.
691 FrameTreeNodeBlameContext blame_context_;
692
shivanigithub4cd016a2021-09-20 21:10:30693 // Fenced Frames:
694 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
695 // frame and any iframes nested within it. Not set if this frame is not in a
696 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
697 // the MPArch version but for the shadow DOM version we need to keep it here
698 // since the fenced frame root is not a main frame for the latter. The value
699 // of the nonce will be the same for all of the the frames inside a fenced
700 // frame tree. If there is a nested fenced frame it will have a different
701 // nonce than its parent fenced frame. The nonce will stay the same across
702 // navigations because it is always used in conjunction with other fields of
703 // the keys. If the navigation is same-origin/site then the same network stack
704 // partition/storage will be reused and if it's cross-origin/site then other
705 // parts of the key will change and so, even with the same nonce, another
706 // partition will be used.
707 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
708
Lukasz Anforowicz147141962020-12-16 18:03:24709 // Manages creation and swapping of RenderFrameHosts for this frame.
710 //
711 // This field needs to be declared last, because destruction of
712 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
713 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
714 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
715 // may try to use FrameTreeNode's fields above - this would be an undefined
716 // behavior if the fields (even trivially-destructible ones) were destructed
717 // before the RenderFrameHostManager's destructor runs. See also
718 // https://crbug.com/1157988.
719 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44720};
721
722} // namespace content
723
724#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_