blob: 068b03989027e75f23fa3dbc174b8331cc1c071f [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"
15#include "base/macros.h"
16#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"
danakjc492bf82020-09-09 20:02:4425#include "third_party/blink/public/common/frame/frame_policy.h"
26#include "third_party/blink/public/common/frame/user_activation_state.h"
27#include "third_party/blink/public/mojom/frame/frame_owner_element_type.mojom.h"
28#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,
89 blink::mojom::FrameOwnerElementType owner_type);
90
91 ~FrameTreeNode();
92
93 void AddObserver(Observer* observer);
94 void RemoveObserver(Observer* observer);
95
96 bool IsMainFrame() const;
97
arthursonzogni76098e52020-11-25 14:18:4598 // Clears any state in this node which was set by the document itself (CSP &
99 // UserActivationState) and notifies proxies as appropriate. Invoked after
100 // committing navigation to a new document (since the new document comes with
101 // a fresh set of CSP).
102 // TODO(arthursonzogni): Remove this function. The frame/document must not be
103 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13104 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44105
106 FrameTree* frame_tree() const { return frame_tree_; }
107 Navigator& navigator() { return frame_tree()->navigator(); }
108
109 RenderFrameHostManager* render_manager() { return &render_manager_; }
110 int frame_tree_node_id() const { return frame_tree_node_id_; }
Antonio Sartori90f41212021-01-22 10:08:34111 const std::string& frame_name() const { return replication_state_->name; }
danakjc492bf82020-09-09 20:02:44112
113 const std::string& unique_name() const {
Antonio Sartori90f41212021-01-22 10:08:34114 return replication_state_->unique_name;
danakjc492bf82020-09-09 20:02:44115 }
116
117 // See comment on the member declaration.
118 const base::UnguessableToken& devtools_frame_token() const {
119 return devtools_frame_token_;
120 }
121
122 size_t child_count() const { return current_frame_host()->child_count(); }
123
124 unsigned int depth() const { return depth_; }
125
126 RenderFrameHostImpl* parent() const { return parent_; }
127
128 FrameTreeNode* opener() const { return opener_; }
129
130 FrameTreeNode* original_opener() const { return original_opener_; }
131
Anton Bikineevf62d1bf2021-05-15 17:56:07132 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39133 return opener_devtools_frame_token_;
134 }
135
danakjc492bf82020-09-09 20:02:44136 // Gets the total number of descendants to this FrameTreeNode in addition to
137 // this node.
138 size_t GetFrameTreeSize() const;
139
140 // Assigns a new opener for this node and, if |opener| is non-null, registers
141 // an observer that will clear this node's opener if |opener| is ever
142 // destroyed.
143 void SetOpener(FrameTreeNode* opener);
144
145 // Assigns the initial opener for this node, and if |opener| is non-null,
146 // registers an observer that will clear this node's opener if |opener| is
147 // ever destroyed. The value set here is the root of the tree.
148 //
149 // It is not possible to change the opener once it was set.
150 void SetOriginalOpener(FrameTreeNode* opener);
151
Wolfgang Beyerd8809db2020-09-30 15:29:39152 // Assigns an opener frame id for this node. This string id is only set once
153 // and cannot be changed. It persists, even if the |opener| is destroyed. It
154 // is used for attribution in the DevTools frontend.
155 void SetOpenerDevtoolsFrameToken(
156 base::UnguessableToken opener_devtools_frame_token);
157
danakjc492bf82020-09-09 20:02:44158 FrameTreeNode* child_at(size_t index) const {
159 return current_frame_host()->child_at(index);
160 }
161
162 // Returns the URL of the last committed page in the current frame.
163 const GURL& current_url() const {
164 return current_frame_host()->GetLastCommittedURL();
165 }
166
167 // Sets the last committed URL for this frame and updates
168 // has_committed_real_load accordingly.
169 void SetCurrentURL(const GURL& url);
170
Rakina Zata Amnid09b6112021-06-05 06:20:14171 // Returns true if SetCurrentURL has been called with a non-blank URL or
172 // if the current document's input stream has been opened with
173 // document.open(). See the definition of `has_committed_real_load_` for more
174 // details.
danakjc492bf82020-09-09 20:02:44175 bool has_committed_real_load() const { return has_committed_real_load_; }
176
Rakina Zata Amnid09b6112021-06-05 06:20:14177 // Sets has_committed_real_load to true. Must only be called after the current
178 // document's input stream has been opened with document.open().
179 void DidOpenDocumentInputStream() { has_committed_real_load_ = true; }
180
danakjc492bf82020-09-09 20:02:44181 // Returns whether the frame's owner element in the parent document is
182 // collapsed, that is, removed from the layout as if it did not exist, as per
183 // request by the embedder (of the content/ layer).
184 bool is_collapsed() const { return is_collapsed_; }
185
186 // Sets whether to collapse the frame's owner element in the parent document,
187 // that is, to remove it from the layout as if it did not exist, as per
188 // request by the embedder (of the content/ layer). Cannot be called for main
189 // frames.
190 //
191 // This only has an effect for <iframe> owner elements, and is a no-op when
192 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
193 void SetCollapsed(bool collapsed);
194
195 // Returns the origin of the last committed page in this frame.
196 // WARNING: To get the last committed origin for a particular
197 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
198 // which will behave correctly even when the RenderFrameHost is not the
199 // current one for this frame (such as when it's pending deletion).
200 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34201 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44202 }
203
204 // Set the current origin and notify proxies about the update.
205 void SetCurrentOrigin(const url::Origin& origin,
206 bool is_potentially_trustworthy_unique_origin);
207
208 // Set the current name and notify proxies about the update.
209 void SetFrameName(const std::string& name, const std::string& unique_name);
210
danakjc492bf82020-09-09 20:02:44211 // Sets the current insecure request policy, and notifies proxies about the
212 // update.
213 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
214
215 // Sets the current set of insecure urls to upgrade, and notifies proxies
216 // about the update.
217 void SetInsecureNavigationsSet(
218 const std::vector<uint32_t>& insecure_navigations_set);
219
220 // Returns the latest frame policy (sandbox flags and container policy) for
221 // this frame. This includes flags inherited from parent frames and the latest
222 // flags from the <iframe> element hosting this frame. The returned policies
223 // may not yet have taken effect, since "sandbox" and "allow" attribute
224 // updates in an <iframe> element take effect on next navigation. To retrieve
225 // the currently active policy for this frame, use effective_frame_policy().
226 const blink::FramePolicy& pending_frame_policy() const {
227 return pending_frame_policy_;
228 }
229
230 // Update this frame's sandbox flags and container policy. This is called
231 // when a parent frame updates the "sandbox" attribute in the <iframe> element
232 // for this frame, or any of the attributes which affect the container policy
233 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
234 // These policies won't take effect until next navigation. If this frame's
235 // parent is itself sandboxed, the parent's sandbox flags are combined with
236 // those in |frame_policy|.
237 // Attempting to change the container policy on the main frame will have no
238 // effect.
239 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
240
241 // Returns the currently active frame policy for this frame, including the
242 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39243 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44244 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
245 // origin of the iframe's src attribute (which may be different from the URL
246 // of the document currently loaded into the frame). This does not include
247 // policy changes that have been made by updating the containing iframe
248 // element attributes since the frame was last navigated; use
249 // pending_frame_policy() for those.
250 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34251 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44252 }
253
254 // Set the frame_policy provided in function parameter as active frame policy,
255 // while leaving pending_frame_policy_ untouched.
256 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
257
258 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
259 return frame_owner_properties_;
260 }
261
262 void set_frame_owner_properties(
263 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
264 frame_owner_properties_ = frame_owner_properties;
265 }
266
267 const network::mojom::ContentSecurityPolicy* csp_attribute() {
268 return csp_attribute_.get();
269 }
270
271 void set_csp_attribute(
272 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
273 csp_attribute_ = std::move(parsed_csp_attribute);
274 }
275
276 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34277 return replication_state_->origin.IsSameOriginWith(
278 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44279 }
280
Gyuyoung Kimc16e52e92021-03-19 02:45:37281 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34282 return *replication_state_;
danakjc492bf82020-09-09 20:02:44283 }
284
285 RenderFrameHostImpl* current_frame_host() const {
286 return render_manager_.current_frame_host();
287 }
288
289 // Return the node immediately preceding this node in its parent's children,
290 // or nullptr if there is no such node.
291 FrameTreeNode* PreviousSibling() const;
292
293 // Return the node immediately following this node in its parent's children,
294 // or nullptr if there is no such node.
295 FrameTreeNode* NextSibling() const;
296
297 // Returns true if this node is in a loading state.
298 bool IsLoading() const;
299
Alex Moshchuk9b0fd822020-10-26 23:08:15300 // Returns true if this node has a cross-document navigation in progress.
301 bool HasPendingCrossDocumentNavigation() const;
302
danakjc492bf82020-09-09 20:02:44303 NavigationRequest* navigation_request() { return navigation_request_.get(); }
304
305 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
306 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
307 // RenderFrameHost that is committing the navigation.
308 void TransferNavigationRequestOwnership(
309 RenderFrameHostImpl* render_frame_host);
310
311 // Takes ownership of |navigation_request| and makes it the current
312 // NavigationRequest of this frame. This corresponds to the start of a new
313 // navigation. If there was an ongoing navigation request before calling this
314 // function, it is canceled. |navigation_request| should not be null.
315 void CreatedNavigationRequest(
316 std::unique_ptr<NavigationRequest> navigation_request);
317
318 // Resets the current navigation request. If |keep_state| is true, any state
319 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
320 // loading state) will not be reset by the function.
321 void ResetNavigationRequest(bool keep_state);
322
323 // A RenderFrameHost in this node started loading.
324 // |to_different_document| will be true unless the load is a fragment
325 // navigation, or triggered by history.pushState/replaceState.
326 // |was_previously_loading| is false if the FrameTree was not loading before.
327 // The caller is required to provide this boolean as the delegate should only
328 // be notified if the FrameTree went from non-loading to loading state.
329 // However, when it is called, the FrameTree should be in a loading state.
330 void DidStartLoading(bool to_different_document, bool was_previously_loading);
331
332 // A RenderFrameHost in this node stopped loading.
333 void DidStopLoading();
334
335 // The load progress for a RenderFrameHost in this node was updated to
336 // |load_progress|. This will notify the FrameTree which will in turn notify
337 // the WebContents.
338 void DidChangeLoadProgress(double load_progress);
339
340 // Called when the user directed the page to stop loading. Stops all loads
341 // happening in the FrameTreeNode. This method should be used with
342 // FrameTree::ForEach to stop all loads in the entire FrameTree.
343 bool StopLoading();
344
345 // Returns the time this frame was last focused.
346 base::TimeTicks last_focus_time() const { return last_focus_time_; }
347
348 // Called when this node becomes focused. Updates the node's last focused
349 // time and notifies observers.
350 void DidFocus();
351
352 // Called when the user closed the modal dialogue for BeforeUnload and
353 // cancelled the navigation. This should stop any load happening in the
354 // FrameTreeNode.
355 void BeforeUnloadCanceled();
356
357 // Returns the BlameContext associated with this node.
358 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
359
360 // Updates the user activation state in the browser frame tree and in the
361 // frame trees in all renderer processes except the renderer for this node
362 // (which initiated the update). Returns |false| if the update tries to
363 // consume an already consumed/expired transient state, |true| otherwise. See
364 // the comment on user_activation_state_ below.
365 //
366 // The |notification_type| parameter is used for histograms, only for the case
367 // |update_state == kNotifyActivation|.
368 bool UpdateUserActivationState(
369 blink::mojom::UserActivationUpdateType update_type,
370 blink::mojom::UserActivationNotificationType notification_type);
371
372 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
373
374 // Returns the sandbox flags currently in effect for this frame. This includes
375 // flags inherited from parent frames, the currently active flags from the
376 // <iframe> element hosting this frame, as well as any flags set from a
377 // Content-Security-Policy HTTP header. This does not include flags that have
378 // have been updated in an <iframe> element but have not taken effect yet; use
379 // pending_frame_policy() for those. To see the flags which will take effect
380 // on navigation (which does not include the CSP-set flags), use
381 // effective_frame_policy().
382 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34383 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44384 }
385
386 // Updates the active sandbox flags in this frame, in response to a
387 // Content-Security-Policy header adding additional flags, in addition to
388 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39389 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44390 // will be cleared, and the flags in the pending frame policy will be applied
391 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31392 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39393 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31394 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44395 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06396 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44397
398 // Returns whether the frame received a user gesture on a previous navigation
399 // on the same eTLD+1.
400 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34401 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44402 }
403
404 // When a tab is discarded, WebContents sets was_discarded on its
405 // root FrameTreeNode.
406 // In addition, when a child frame is created, this bit is passed on from
407 // parent to child.
408 // When a navigation request is created, was_discarded is passed on to the
409 // request and reset to false in FrameTreeNode.
410 void set_was_discarded() { was_discarded_ = true; }
411 bool was_discarded() const { return was_discarded_; }
412
413 // Returns the sticky bit of the User Activation v2 state of the
414 // |FrameTreeNode|.
415 bool HasStickyUserActivation() const {
416 return user_activation_state_.HasBeenActive();
417 }
418
419 // Returns the transient bit of the User Activation v2 state of the
420 // |FrameTreeNode|.
421 bool HasTransientUserActivation() {
422 return user_activation_state_.IsActive();
423 }
424
425 // Remove history entries for all frames created by script in this frame's
426 // subtree. If a frame created by a script is removed, then its history entry
427 // will never be reused - this saves memory.
428 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
429
430 blink::mojom::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45431 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44432 }
danakjc492bf82020-09-09 20:02:44433
Daniel Cheng6ac128172021-05-25 18:49:01434 blink::mojom::TreeScopeType tree_scope_type() const {
435 return tree_scope_type_;
436 }
437
Alex Turner10d557a42021-06-01 19:06:49438 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44439
arthursonzogni034bb9c2020-10-01 08:29:56440 // The initial popup URL for new window opened using:
441 // `window.open(initial_popup_url)`.
442 // An empty GURL otherwise.
443 //
444 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
445 // document served from this URL. The FrameTreeNode always starts hosting the
446 // initial empty document and attempts a navigation toward this URL. However
447 // the navigation might be delayed, redirected and even cancelled.
448 void SetInitialPopupURL(const GURL& initial_popup_url);
449 const GURL& initial_popup_url() const { return initial_popup_url_; }
450
451 // The origin of the document that used window.open() to create this frame.
452 // Otherwise, an opaque Origin with a nonce different from all previously
453 // existing Origins.
454 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
455 const url::Origin& popup_creator_origin() const {
456 return popup_creator_origin_;
457 }
458
Harkiran Bolaria59290d62021-03-17 01:53:01459 // Sets the associated FrameTree for this node. The node can change FrameTrees
460 // when blink::features::Prerender2 is enabled, which allows a page loaded in
461 // the prerendered FrameTree to be used for a navigation in the primary frame
462 // tree.
463 void SetFrameTree(FrameTree& frame_tree);
464
Alexander Timinf785f342021-03-18 00:00:56465 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24466 void WriteIntoTrace(perfetto::TracedValue context) const;
Alexander Timinf785f342021-03-18 00:00:56467
Carlos Caballero76711352021-03-24 17:38:21468 // Returns true the node is navigating, i.e. it has an associated
469 // NavigationRequest.
470 bool HasNavigation();
471
danakjc492bf82020-09-09 20:02:44472 private:
Charlie Hubb5943d2021-03-09 19:46:12473 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44474 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12475 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44476 ContainerPolicySandboxDynamic);
477
478 class OpenerDestroyedObserver;
479
480 FrameTreeNode* GetSibling(int relative_offset) const;
481
482 // The |notification_type| parameter is used for histograms only.
483 bool NotifyUserActivation(
484 blink::mojom::UserActivationNotificationType notification_type);
485
486 bool ConsumeTransientUserActivation();
487
488 bool ClearUserActivation();
489
490 // Verify that the renderer process is allowed to set user activation on this
491 // frame by checking whether this frame's RenderWidgetHost had previously seen
492 // an input event that might lead to user activation. If user activation
493 // should be allowed, this returns true and also clears corresponding pending
494 // user activation state in the widget. Otherwise, this returns false.
495 bool VerifyUserActivation();
496
497 // The next available browser-global FrameTreeNode ID.
498 static int next_frame_tree_node_id_;
499
500 // The FrameTree that owns us.
501 FrameTree* frame_tree_; // not owned.
502
danakjc492bf82020-09-09 20:02:44503 // A browser-global identifier for the frame in the page, which stays stable
504 // even if the frame does a cross-process navigation.
505 const int frame_tree_node_id_;
506
507 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
508 // life of this FrameTreeNode. |nullptr| if this node is the root.
509 RenderFrameHostImpl* const parent_;
510
511 // Number of edges from this node to the root. 0 if this is the root.
512 const unsigned int depth_;
513
514 // The frame that opened this frame, if any. Will be set to null if the
515 // opener is closed, or if this frame disowns its opener by setting its
516 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19517 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44518
519 // An observer that clears this node's |opener_| if the opener is destroyed.
520 // This observer is added to the |opener_|'s observer list when the |opener_|
521 // is set to a non-null node, and it is removed from that list when |opener_|
522 // changes or when this node is destroyed. It is also cleared if |opener_|
523 // is disowned.
524 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
525
526 // The frame that opened this frame, if any. Contrary to opener_, this
527 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19528 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44529
Wolfgang Beyerd8809db2020-09-30 15:29:39530 // The devtools frame token of the frame which opened this frame. This is
531 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07532 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39533
danakjc492bf82020-09-09 20:02:44534 // An observer that clears this node's |original_opener_| if the opener is
535 // destroyed.
536 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
537
arthursonzogni034bb9c2020-10-01 08:29:56538 // When created by an opener, the URL specified in window.open(url)
539 // Please refer to {Get,Set}InitialPopupURL() documentation.
540 GURL initial_popup_url_;
541
542 // When created using window.open, the origin of the creator.
543 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
544 url::Origin popup_creator_origin_;
545
Rakina Zata Amnid09b6112021-06-05 06:20:14546 // Whether this frame has committed a "real load", replacing its initial
547 // about:blank document and possibly other subsequent about:blank documents
548 // after the initial about:blank document. This will be marked as true if
549 // either of these has happened:
550 // - SetCurrentUrl() has been called with a non about:blank URL.
551 // - The document's input stream has been opened with document.open().
552 // See:
553 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
554 // TODO(https://crbug.com/1215096): Make this true after non-initial
555 // about:blank commits as well, making this only track whether the current
556 // document is the initial empty document or not.
arthursonzogni9816b9192021-03-29 16:09:19557 bool has_committed_real_load_ = false;
danakjc492bf82020-09-09 20:02:44558
559 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19560 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44561
Daniel Cheng6ac128172021-05-25 18:49:01562 // The type of frame owner for this frame. This is only relevant for non-main
563 // frames.
Daniel Cheng9bd90f92021-04-23 20:49:45564 const blink::mojom::FrameOwnerElementType frame_owner_element_type_ =
565 blink::mojom::FrameOwnerElementType::kNone;
566
Daniel Cheng6ac128172021-05-25 18:49:01567 // The tree scope type of frame owner element, i.e. whether the element is in
568 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
569 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
570 // relevant for non-main frames.
571 const blink::mojom::TreeScopeType tree_scope_type_ =
572 blink::mojom::TreeScopeType::kDocument;
573
danakjc492bf82020-09-09 20:02:44574 // Track information that needs to be replicated to processes that have
575 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37576 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44577
578 // Track the pending sandbox flags and container policy for this frame. When a
579 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
580 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34581 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44582 // they take effect on the next frame navigation.
583 blink::FramePolicy pending_frame_policy_;
584
585 // Whether the frame was created by javascript. This is useful to prune
586 // history entries when the frame is removed (because frames created by
587 // scripts are never recreated with the same unique name - see
588 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19589 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44590
591 // Used for devtools instrumentation and trace-ability. The token is
592 // propagated to Blink's LocalFrame and both Blink and content/
593 // can tag calls and requests with this token in order to attribute them
594 // to the context frame.
595 // |devtools_frame_token_| is only defined by the browser process and is never
596 // sent back from the renderer in the control calls. It should be never used
597 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19598 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44599
600 // Tracks the scrolling and margin properties for this frame. These
601 // properties affect the child renderer but are stored on its parent's
602 // frame element. When this frame's parent dynamically updates these
603 // properties, we update them here too.
604 //
605 // Note that dynamic updates only take effect on the next frame navigation.
606 blink::mojom::FrameOwnerProperties frame_owner_properties_;
607
608 // Contains the current parsed value of the 'csp' attribute of this frame.
609 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
610
611 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
612 // be reset and a RenderFrameHost will be responsible for the navigation.
613 std::unique_ptr<NavigationRequest> navigation_request_;
614
615 // List of objects observing this FrameTreeNode.
616 base::ObserverList<Observer>::Unchecked observers_;
617
618 base::TimeTicks last_focus_time_;
619
arthursonzogni9816b9192021-03-29 16:09:19620 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44621
622 // The user activation state of the current frame. See |UserActivationState|
623 // for details on how this state is maintained.
624 blink::UserActivationState user_activation_state_;
625
626 // A helper for tracing the snapshots of this FrameTreeNode and attributing
627 // browser process activities to this node (when possible). It is unrelated
628 // to the core logic of FrameTreeNode.
629 FrameTreeNodeBlameContext blame_context_;
630
Lukasz Anforowicz147141962020-12-16 18:03:24631 // Manages creation and swapping of RenderFrameHosts for this frame.
632 //
633 // This field needs to be declared last, because destruction of
634 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
635 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
636 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
637 // may try to use FrameTreeNode's fields above - this would be an undefined
638 // behavior if the fields (even trivially-destructible ones) were destructed
639 // before the RenderFrameHostManager's destructor runs. See also
640 // https://crbug.com/1157988.
641 RenderFrameHostManager render_manager_;
642
danakjc492bf82020-09-09 20:02:44643 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
644};
645
646} // namespace content
647
648#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_