blob: 1cfba9e73e8cf6b17d6a6638e890b8ee16ba0eea [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,
Dominic Farolino08662c82021-06-11 07:36:3489 blink::mojom::FrameOwnerElementType owner_type,
90 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4491
92 ~FrameTreeNode();
93
94 void AddObserver(Observer* observer);
95 void RemoveObserver(Observer* observer);
96
97 bool IsMainFrame() const;
98
arthursonzogni76098e52020-11-25 14:18:4599 // Clears any state in this node which was set by the document itself (CSP &
100 // UserActivationState) and notifies proxies as appropriate. Invoked after
101 // committing navigation to a new document (since the new document comes with
102 // a fresh set of CSP).
103 // TODO(arthursonzogni): Remove this function. The frame/document must not be
104 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13105 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44106
107 FrameTree* frame_tree() const { return frame_tree_; }
108 Navigator& navigator() { return frame_tree()->navigator(); }
109
110 RenderFrameHostManager* render_manager() { return &render_manager_; }
111 int frame_tree_node_id() const { return frame_tree_node_id_; }
Antonio Sartori90f41212021-01-22 10:08:34112 const std::string& frame_name() const { return replication_state_->name; }
danakjc492bf82020-09-09 20:02:44113
114 const std::string& unique_name() const {
Antonio Sartori90f41212021-01-22 10:08:34115 return replication_state_->unique_name;
danakjc492bf82020-09-09 20:02:44116 }
117
118 // See comment on the member declaration.
119 const base::UnguessableToken& devtools_frame_token() const {
120 return devtools_frame_token_;
121 }
122
123 size_t child_count() const { return current_frame_host()->child_count(); }
124
125 unsigned int depth() const { return depth_; }
126
127 RenderFrameHostImpl* parent() const { return parent_; }
128
129 FrameTreeNode* opener() const { return opener_; }
130
131 FrameTreeNode* original_opener() const { return original_opener_; }
132
Anton Bikineevf62d1bf2021-05-15 17:56:07133 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39134 return opener_devtools_frame_token_;
135 }
136
danakjc492bf82020-09-09 20:02:44137 // Gets the total number of descendants to this FrameTreeNode in addition to
138 // this node.
139 size_t GetFrameTreeSize() const;
140
141 // Assigns a new opener for this node and, if |opener| is non-null, registers
142 // an observer that will clear this node's opener if |opener| is ever
143 // destroyed.
144 void SetOpener(FrameTreeNode* opener);
145
146 // Assigns the initial opener for this node, and if |opener| is non-null,
147 // registers an observer that will clear this node's opener if |opener| is
148 // ever destroyed. The value set here is the root of the tree.
149 //
150 // It is not possible to change the opener once it was set.
151 void SetOriginalOpener(FrameTreeNode* opener);
152
Wolfgang Beyerd8809db2020-09-30 15:29:39153 // Assigns an opener frame id for this node. This string id is only set once
154 // and cannot be changed. It persists, even if the |opener| is destroyed. It
155 // is used for attribution in the DevTools frontend.
156 void SetOpenerDevtoolsFrameToken(
157 base::UnguessableToken opener_devtools_frame_token);
158
danakjc492bf82020-09-09 20:02:44159 FrameTreeNode* child_at(size_t index) const {
160 return current_frame_host()->child_at(index);
161 }
162
163 // Returns the URL of the last committed page in the current frame.
164 const GURL& current_url() const {
165 return current_frame_host()->GetLastCommittedURL();
166 }
167
168 // Sets the last committed URL for this frame and updates
169 // has_committed_real_load accordingly.
170 void SetCurrentURL(const GURL& url);
171
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56172 // Returns true if SetCurrentURL has been called with a non-blank URL.
173 // TODO(https://crbug.com/1215096): Migrate most usage of
174 // has_committed_real_load() to call
175 // is_on_initial_empty_document_or_subsequent_empty_documents() instead.
danakjc492bf82020-09-09 20:02:44176 bool has_committed_real_load() const { return has_committed_real_load_; }
177
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56178 // Returns true if SetCurrentURL has been called with a non-blank URL or
179 // if the current document's input stream has been opened with
180 // document.open(). For more details, see the definition of
181 // `is_on_initial_empty_document_or_subsequent_empty_documents_`.
182 bool is_on_initial_empty_document_or_subsequent_empty_documents() const {
183 return is_on_initial_empty_document_or_subsequent_empty_documents_;
184 }
185
186 // Sets `is_on_initial_empty_document_or_subsequent_empty_documents_` to
187 // false. Must only be called after the current document's input stream has
188 // been opened with document.open().
189 void DidOpenDocumentInputStream() {
190 is_on_initial_empty_document_or_subsequent_empty_documents_ = false;
191 }
Rakina Zata Amnid09b6112021-06-05 06:20:14192
danakjc492bf82020-09-09 20:02:44193 // Returns whether the frame's owner element in the parent document is
194 // collapsed, that is, removed from the layout as if it did not exist, as per
195 // request by the embedder (of the content/ layer).
196 bool is_collapsed() const { return is_collapsed_; }
197
198 // Sets whether to collapse the frame's owner element in the parent document,
199 // that is, to remove it from the layout as if it did not exist, as per
200 // request by the embedder (of the content/ layer). Cannot be called for main
201 // frames.
202 //
203 // This only has an effect for <iframe> owner elements, and is a no-op when
204 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
205 void SetCollapsed(bool collapsed);
206
207 // Returns the origin of the last committed page in this frame.
208 // WARNING: To get the last committed origin for a particular
209 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
210 // which will behave correctly even when the RenderFrameHost is not the
211 // current one for this frame (such as when it's pending deletion).
212 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34213 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44214 }
215
216 // Set the current origin and notify proxies about the update.
217 void SetCurrentOrigin(const url::Origin& origin,
218 bool is_potentially_trustworthy_unique_origin);
219
220 // Set the current name and notify proxies about the update.
221 void SetFrameName(const std::string& name, const std::string& unique_name);
222
danakjc492bf82020-09-09 20:02:44223 // Sets the current insecure request policy, and notifies proxies about the
224 // update.
225 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
226
227 // Sets the current set of insecure urls to upgrade, and notifies proxies
228 // about the update.
229 void SetInsecureNavigationsSet(
230 const std::vector<uint32_t>& insecure_navigations_set);
231
232 // Returns the latest frame policy (sandbox flags and container policy) for
233 // this frame. This includes flags inherited from parent frames and the latest
234 // flags from the <iframe> element hosting this frame. The returned policies
235 // may not yet have taken effect, since "sandbox" and "allow" attribute
236 // updates in an <iframe> element take effect on next navigation. To retrieve
237 // the currently active policy for this frame, use effective_frame_policy().
238 const blink::FramePolicy& pending_frame_policy() const {
239 return pending_frame_policy_;
240 }
241
242 // Update this frame's sandbox flags and container policy. This is called
243 // when a parent frame updates the "sandbox" attribute in the <iframe> element
244 // for this frame, or any of the attributes which affect the container policy
245 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
246 // These policies won't take effect until next navigation. If this frame's
247 // parent is itself sandboxed, the parent's sandbox flags are combined with
248 // those in |frame_policy|.
249 // Attempting to change the container policy on the main frame will have no
250 // effect.
251 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
252
253 // Returns the currently active frame policy for this frame, including the
254 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39255 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44256 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
257 // origin of the iframe's src attribute (which may be different from the URL
258 // of the document currently loaded into the frame). This does not include
259 // policy changes that have been made by updating the containing iframe
260 // element attributes since the frame was last navigated; use
261 // pending_frame_policy() for those.
262 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34263 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44264 }
265
266 // Set the frame_policy provided in function parameter as active frame policy,
267 // while leaving pending_frame_policy_ untouched.
268 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
269
270 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
271 return frame_owner_properties_;
272 }
273
274 void set_frame_owner_properties(
275 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
276 frame_owner_properties_ = frame_owner_properties;
277 }
278
279 const network::mojom::ContentSecurityPolicy* csp_attribute() {
280 return csp_attribute_.get();
281 }
282
283 void set_csp_attribute(
284 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
285 csp_attribute_ = std::move(parsed_csp_attribute);
286 }
287
Antonio Sartori5abc8de2021-07-13 08:42:47288 // Reflects the 'anonymous' attribute of the corresponding iframe html
289 // element.
290 bool anonymous() const { return anonymous_; }
291 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
292
danakjc492bf82020-09-09 20:02:44293 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34294 return replication_state_->origin.IsSameOriginWith(
295 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44296 }
297
Gyuyoung Kimc16e52e92021-03-19 02:45:37298 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34299 return *replication_state_;
danakjc492bf82020-09-09 20:02:44300 }
301
302 RenderFrameHostImpl* current_frame_host() const {
303 return render_manager_.current_frame_host();
304 }
305
306 // Return the node immediately preceding this node in its parent's children,
307 // or nullptr if there is no such node.
308 FrameTreeNode* PreviousSibling() const;
309
310 // Return the node immediately following this node in its parent's children,
311 // or nullptr if there is no such node.
312 FrameTreeNode* NextSibling() const;
313
314 // Returns true if this node is in a loading state.
315 bool IsLoading() const;
316
Alex Moshchuk9b0fd822020-10-26 23:08:15317 // Returns true if this node has a cross-document navigation in progress.
318 bool HasPendingCrossDocumentNavigation() const;
319
danakjc492bf82020-09-09 20:02:44320 NavigationRequest* navigation_request() { return navigation_request_.get(); }
321
322 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
323 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
324 // RenderFrameHost that is committing the navigation.
325 void TransferNavigationRequestOwnership(
326 RenderFrameHostImpl* render_frame_host);
327
328 // Takes ownership of |navigation_request| and makes it the current
329 // NavigationRequest of this frame. This corresponds to the start of a new
330 // navigation. If there was an ongoing navigation request before calling this
331 // function, it is canceled. |navigation_request| should not be null.
332 void CreatedNavigationRequest(
333 std::unique_ptr<NavigationRequest> navigation_request);
334
335 // Resets the current navigation request. If |keep_state| is true, any state
336 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
337 // loading state) will not be reset by the function.
338 void ResetNavigationRequest(bool keep_state);
339
340 // A RenderFrameHost in this node started loading.
341 // |to_different_document| will be true unless the load is a fragment
342 // navigation, or triggered by history.pushState/replaceState.
343 // |was_previously_loading| is false if the FrameTree was not loading before.
344 // The caller is required to provide this boolean as the delegate should only
345 // be notified if the FrameTree went from non-loading to loading state.
346 // However, when it is called, the FrameTree should be in a loading state.
347 void DidStartLoading(bool to_different_document, bool was_previously_loading);
348
349 // A RenderFrameHost in this node stopped loading.
350 void DidStopLoading();
351
352 // The load progress for a RenderFrameHost in this node was updated to
353 // |load_progress|. This will notify the FrameTree which will in turn notify
354 // the WebContents.
355 void DidChangeLoadProgress(double load_progress);
356
357 // Called when the user directed the page to stop loading. Stops all loads
358 // happening in the FrameTreeNode. This method should be used with
359 // FrameTree::ForEach to stop all loads in the entire FrameTree.
360 bool StopLoading();
361
362 // Returns the time this frame was last focused.
363 base::TimeTicks last_focus_time() const { return last_focus_time_; }
364
365 // Called when this node becomes focused. Updates the node's last focused
366 // time and notifies observers.
367 void DidFocus();
368
369 // Called when the user closed the modal dialogue for BeforeUnload and
370 // cancelled the navigation. This should stop any load happening in the
371 // FrameTreeNode.
372 void BeforeUnloadCanceled();
373
374 // Returns the BlameContext associated with this node.
375 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
376
377 // Updates the user activation state in the browser frame tree and in the
378 // frame trees in all renderer processes except the renderer for this node
379 // (which initiated the update). Returns |false| if the update tries to
380 // consume an already consumed/expired transient state, |true| otherwise. See
381 // the comment on user_activation_state_ below.
382 //
383 // The |notification_type| parameter is used for histograms, only for the case
384 // |update_state == kNotifyActivation|.
385 bool UpdateUserActivationState(
386 blink::mojom::UserActivationUpdateType update_type,
387 blink::mojom::UserActivationNotificationType notification_type);
388
389 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
390
391 // Returns the sandbox flags currently in effect for this frame. This includes
392 // flags inherited from parent frames, the currently active flags from the
393 // <iframe> element hosting this frame, as well as any flags set from a
394 // Content-Security-Policy HTTP header. This does not include flags that have
395 // have been updated in an <iframe> element but have not taken effect yet; use
396 // pending_frame_policy() for those. To see the flags which will take effect
397 // on navigation (which does not include the CSP-set flags), use
398 // effective_frame_policy().
399 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34400 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44401 }
402
403 // Updates the active sandbox flags in this frame, in response to a
404 // Content-Security-Policy header adding additional flags, in addition to
405 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39406 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44407 // will be cleared, and the flags in the pending frame policy will be applied
408 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31409 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39410 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31411 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44412 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06413 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44414
415 // Returns whether the frame received a user gesture on a previous navigation
416 // on the same eTLD+1.
417 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34418 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44419 }
420
421 // When a tab is discarded, WebContents sets was_discarded on its
422 // root FrameTreeNode.
423 // In addition, when a child frame is created, this bit is passed on from
424 // parent to child.
425 // When a navigation request is created, was_discarded is passed on to the
426 // request and reset to false in FrameTreeNode.
427 void set_was_discarded() { was_discarded_ = true; }
428 bool was_discarded() const { return was_discarded_; }
429
430 // Returns the sticky bit of the User Activation v2 state of the
431 // |FrameTreeNode|.
432 bool HasStickyUserActivation() const {
433 return user_activation_state_.HasBeenActive();
434 }
435
436 // Returns the transient bit of the User Activation v2 state of the
437 // |FrameTreeNode|.
438 bool HasTransientUserActivation() {
439 return user_activation_state_.IsActive();
440 }
441
442 // Remove history entries for all frames created by script in this frame's
443 // subtree. If a frame created by a script is removed, then its history entry
444 // will never be reused - this saves memory.
445 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
446
447 blink::mojom::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45448 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44449 }
danakjc492bf82020-09-09 20:02:44450
Daniel Cheng6ac128172021-05-25 18:49:01451 blink::mojom::TreeScopeType tree_scope_type() const {
452 return tree_scope_type_;
453 }
454
Alex Turner10d557a42021-06-01 19:06:49455 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44456
arthursonzogni034bb9c2020-10-01 08:29:56457 // The initial popup URL for new window opened using:
458 // `window.open(initial_popup_url)`.
459 // An empty GURL otherwise.
460 //
461 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
462 // document served from this URL. The FrameTreeNode always starts hosting the
463 // initial empty document and attempts a navigation toward this URL. However
464 // the navigation might be delayed, redirected and even cancelled.
465 void SetInitialPopupURL(const GURL& initial_popup_url);
466 const GURL& initial_popup_url() const { return initial_popup_url_; }
467
468 // The origin of the document that used window.open() to create this frame.
469 // Otherwise, an opaque Origin with a nonce different from all previously
470 // existing Origins.
471 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
472 const url::Origin& popup_creator_origin() const {
473 return popup_creator_origin_;
474 }
475
Harkiran Bolaria59290d62021-03-17 01:53:01476 // Sets the associated FrameTree for this node. The node can change FrameTrees
477 // when blink::features::Prerender2 is enabled, which allows a page loaded in
478 // the prerendered FrameTree to be used for a navigation in the primary frame
479 // tree.
480 void SetFrameTree(FrameTree& frame_tree);
481
Alexander Timinf785f342021-03-18 00:00:56482 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24483 void WriteIntoTrace(perfetto::TracedValue context) const;
Alexander Timinf785f342021-03-18 00:00:56484
Carlos Caballero76711352021-03-24 17:38:21485 // Returns true the node is navigating, i.e. it has an associated
486 // NavigationRequest.
487 bool HasNavigation();
488
shivanigithubf3ddff52021-07-03 22:06:30489 // Fenced frames (meta-bug crbug.com/1111084):
490 // Returns false if fenced frames are disabled. Returns true if the feature is
491 // enabled and if |this| is a fenced frame. Returns false for
492 // iframes embedded in a fenced frame. To clarify: for the MPArch
493 // implementation this only returns true if |this| is the actual
494 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
495 // outer FrameTree.
496 bool IsFencedFrame() const;
497
498 // Returns false if fenced frames are disabled. Returns true if the
499 // feature is enabled and if |this| or any of its ancestor nodes is a
500 // fenced frame.
501 bool IsInFencedFrameTree() const;
502
danakjc492bf82020-09-09 20:02:44503 private:
Charlie Hubb5943d2021-03-09 19:46:12504 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44505 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12506 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44507 ContainerPolicySandboxDynamic);
508
509 class OpenerDestroyedObserver;
510
511 FrameTreeNode* GetSibling(int relative_offset) const;
512
513 // The |notification_type| parameter is used for histograms only.
514 bool NotifyUserActivation(
515 blink::mojom::UserActivationNotificationType notification_type);
516
517 bool ConsumeTransientUserActivation();
518
519 bool ClearUserActivation();
520
521 // Verify that the renderer process is allowed to set user activation on this
522 // frame by checking whether this frame's RenderWidgetHost had previously seen
523 // an input event that might lead to user activation. If user activation
524 // should be allowed, this returns true and also clears corresponding pending
525 // user activation state in the widget. Otherwise, this returns false.
526 bool VerifyUserActivation();
527
528 // The next available browser-global FrameTreeNode ID.
529 static int next_frame_tree_node_id_;
530
531 // The FrameTree that owns us.
532 FrameTree* frame_tree_; // not owned.
533
danakjc492bf82020-09-09 20:02:44534 // A browser-global identifier for the frame in the page, which stays stable
535 // even if the frame does a cross-process navigation.
536 const int frame_tree_node_id_;
537
538 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
539 // life of this FrameTreeNode. |nullptr| if this node is the root.
540 RenderFrameHostImpl* const parent_;
541
542 // Number of edges from this node to the root. 0 if this is the root.
543 const unsigned int depth_;
544
545 // The frame that opened this frame, if any. Will be set to null if the
546 // opener is closed, or if this frame disowns its opener by setting its
547 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19548 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44549
550 // An observer that clears this node's |opener_| if the opener is destroyed.
551 // This observer is added to the |opener_|'s observer list when the |opener_|
552 // is set to a non-null node, and it is removed from that list when |opener_|
553 // changes or when this node is destroyed. It is also cleared if |opener_|
554 // is disowned.
555 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
556
557 // The frame that opened this frame, if any. Contrary to opener_, this
558 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19559 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44560
Wolfgang Beyerd8809db2020-09-30 15:29:39561 // The devtools frame token of the frame which opened this frame. This is
562 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07563 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39564
danakjc492bf82020-09-09 20:02:44565 // An observer that clears this node's |original_opener_| if the opener is
566 // destroyed.
567 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
568
arthursonzogni034bb9c2020-10-01 08:29:56569 // When created by an opener, the URL specified in window.open(url)
570 // Please refer to {Get,Set}InitialPopupURL() documentation.
571 GURL initial_popup_url_;
572
573 // When created using window.open, the origin of the creator.
574 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
575 url::Origin popup_creator_origin_;
576
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56577 // Returns true iff SetCurrentURL has been called with a non-blank URL.
578 // TODO(https://crbug.com/1215096): Migrate all current usage of this to
579 // use `is_on_initial_empty_document_or_subsequent_empty_documents_` instead.
580 bool has_committed_real_load_ = false;
581
582 // Whether this frame is still on the initial about:blank document or any
583 // subsequent about:blank documents committed after the initial about:blank
584 // document. This will be false if either of these has happened:
Rakina Zata Amnid09b6112021-06-05 06:20:14585 // - SetCurrentUrl() has been called with a non about:blank URL.
586 // - The document's input stream has been opened with document.open().
587 // See:
588 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56589 // TODO(https://crbug.com/1215096): Make this false after non-initial
Rakina Zata Amnid09b6112021-06-05 06:20:14590 // about:blank commits as well, making this only track whether the current
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56591 // document is the initial empty document or not. Currently we are still
592 // preserving most of the old behavior of `has_committed_real_load_` (except
593 // for the document.open() bit here) due to our current handling of initial
594 // empty document for session history and navigation (where we treat the
595 // the initial about:blank document and subsequent about:blank documents the
596 // same way).
597 bool is_on_initial_empty_document_or_subsequent_empty_documents_ = true;
danakjc492bf82020-09-09 20:02:44598
599 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19600 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44601
Daniel Cheng6ac128172021-05-25 18:49:01602 // The type of frame owner for this frame. This is only relevant for non-main
603 // frames.
Daniel Cheng9bd90f92021-04-23 20:49:45604 const blink::mojom::FrameOwnerElementType frame_owner_element_type_ =
605 blink::mojom::FrameOwnerElementType::kNone;
606
Daniel Cheng6ac128172021-05-25 18:49:01607 // The tree scope type of frame owner element, i.e. whether the element is in
608 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
609 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
610 // relevant for non-main frames.
611 const blink::mojom::TreeScopeType tree_scope_type_ =
612 blink::mojom::TreeScopeType::kDocument;
613
danakjc492bf82020-09-09 20:02:44614 // Track information that needs to be replicated to processes that have
615 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37616 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44617
618 // Track the pending sandbox flags and container policy for this frame. When a
619 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
620 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34621 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44622 // they take effect on the next frame navigation.
623 blink::FramePolicy pending_frame_policy_;
624
625 // Whether the frame was created by javascript. This is useful to prune
626 // history entries when the frame is removed (because frames created by
627 // scripts are never recreated with the same unique name - see
628 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19629 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44630
631 // Used for devtools instrumentation and trace-ability. The token is
632 // propagated to Blink's LocalFrame and both Blink and content/
633 // can tag calls and requests with this token in order to attribute them
634 // to the context frame.
635 // |devtools_frame_token_| is only defined by the browser process and is never
636 // sent back from the renderer in the control calls. It should be never used
637 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19638 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44639
640 // Tracks the scrolling and margin properties for this frame. These
641 // properties affect the child renderer but are stored on its parent's
642 // frame element. When this frame's parent dynamically updates these
643 // properties, we update them here too.
644 //
645 // Note that dynamic updates only take effect on the next frame navigation.
646 blink::mojom::FrameOwnerProperties frame_owner_properties_;
647
648 // Contains the current parsed value of the 'csp' attribute of this frame.
649 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
650
Antonio Sartori5abc8de2021-07-13 08:42:47651 // Reflects the 'anonymous' attribute of the corresponding iframe html
652 // element.
653 bool anonymous_ = false;
654
danakjc492bf82020-09-09 20:02:44655 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
656 // be reset and a RenderFrameHost will be responsible for the navigation.
657 std::unique_ptr<NavigationRequest> navigation_request_;
658
659 // List of objects observing this FrameTreeNode.
660 base::ObserverList<Observer>::Unchecked observers_;
661
662 base::TimeTicks last_focus_time_;
663
arthursonzogni9816b9192021-03-29 16:09:19664 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44665
666 // The user activation state of the current frame. See |UserActivationState|
667 // for details on how this state is maintained.
668 blink::UserActivationState user_activation_state_;
669
670 // A helper for tracing the snapshots of this FrameTreeNode and attributing
671 // browser process activities to this node (when possible). It is unrelated
672 // to the core logic of FrameTreeNode.
673 FrameTreeNodeBlameContext blame_context_;
674
Lukasz Anforowicz147141962020-12-16 18:03:24675 // Manages creation and swapping of RenderFrameHosts for this frame.
676 //
677 // This field needs to be declared last, because destruction of
678 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
679 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
680 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
681 // may try to use FrameTreeNode's fields above - this would be an undefined
682 // behavior if the fields (even trivially-destructible ones) were destructed
683 // before the RenderFrameHostManager's destructor runs. See also
684 // https://crbug.com/1157988.
685 RenderFrameHostManager render_manager_;
686
danakjc492bf82020-09-09 20:02:44687 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
688};
689
690} // namespace content
691
692#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_