blob: 8b053199c3f4e7b0ddb7da008a3c5087220c6406 [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
288 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34289 return replication_state_->origin.IsSameOriginWith(
290 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44291 }
292
Gyuyoung Kimc16e52e92021-03-19 02:45:37293 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34294 return *replication_state_;
danakjc492bf82020-09-09 20:02:44295 }
296
297 RenderFrameHostImpl* current_frame_host() const {
298 return render_manager_.current_frame_host();
299 }
300
301 // Return the node immediately preceding this node in its parent's children,
302 // or nullptr if there is no such node.
303 FrameTreeNode* PreviousSibling() const;
304
305 // Return the node immediately following this node in its parent's children,
306 // or nullptr if there is no such node.
307 FrameTreeNode* NextSibling() const;
308
309 // Returns true if this node is in a loading state.
310 bool IsLoading() const;
311
Alex Moshchuk9b0fd822020-10-26 23:08:15312 // Returns true if this node has a cross-document navigation in progress.
313 bool HasPendingCrossDocumentNavigation() const;
314
danakjc492bf82020-09-09 20:02:44315 NavigationRequest* navigation_request() { return navigation_request_.get(); }
316
317 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
318 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
319 // RenderFrameHost that is committing the navigation.
320 void TransferNavigationRequestOwnership(
321 RenderFrameHostImpl* render_frame_host);
322
323 // Takes ownership of |navigation_request| and makes it the current
324 // NavigationRequest of this frame. This corresponds to the start of a new
325 // navigation. If there was an ongoing navigation request before calling this
326 // function, it is canceled. |navigation_request| should not be null.
327 void CreatedNavigationRequest(
328 std::unique_ptr<NavigationRequest> navigation_request);
329
330 // Resets the current navigation request. If |keep_state| is true, any state
331 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
332 // loading state) will not be reset by the function.
333 void ResetNavigationRequest(bool keep_state);
334
335 // A RenderFrameHost in this node started loading.
336 // |to_different_document| will be true unless the load is a fragment
337 // navigation, or triggered by history.pushState/replaceState.
338 // |was_previously_loading| is false if the FrameTree was not loading before.
339 // The caller is required to provide this boolean as the delegate should only
340 // be notified if the FrameTree went from non-loading to loading state.
341 // However, when it is called, the FrameTree should be in a loading state.
342 void DidStartLoading(bool to_different_document, bool was_previously_loading);
343
344 // A RenderFrameHost in this node stopped loading.
345 void DidStopLoading();
346
347 // The load progress for a RenderFrameHost in this node was updated to
348 // |load_progress|. This will notify the FrameTree which will in turn notify
349 // the WebContents.
350 void DidChangeLoadProgress(double load_progress);
351
352 // Called when the user directed the page to stop loading. Stops all loads
353 // happening in the FrameTreeNode. This method should be used with
354 // FrameTree::ForEach to stop all loads in the entire FrameTree.
355 bool StopLoading();
356
357 // Returns the time this frame was last focused.
358 base::TimeTicks last_focus_time() const { return last_focus_time_; }
359
360 // Called when this node becomes focused. Updates the node's last focused
361 // time and notifies observers.
362 void DidFocus();
363
364 // Called when the user closed the modal dialogue for BeforeUnload and
365 // cancelled the navigation. This should stop any load happening in the
366 // FrameTreeNode.
367 void BeforeUnloadCanceled();
368
369 // Returns the BlameContext associated with this node.
370 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
371
372 // Updates the user activation state in the browser frame tree and in the
373 // frame trees in all renderer processes except the renderer for this node
374 // (which initiated the update). Returns |false| if the update tries to
375 // consume an already consumed/expired transient state, |true| otherwise. See
376 // the comment on user_activation_state_ below.
377 //
378 // The |notification_type| parameter is used for histograms, only for the case
379 // |update_state == kNotifyActivation|.
380 bool UpdateUserActivationState(
381 blink::mojom::UserActivationUpdateType update_type,
382 blink::mojom::UserActivationNotificationType notification_type);
383
384 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
385
386 // Returns the sandbox flags currently in effect for this frame. This includes
387 // flags inherited from parent frames, the currently active flags from the
388 // <iframe> element hosting this frame, as well as any flags set from a
389 // Content-Security-Policy HTTP header. This does not include flags that have
390 // have been updated in an <iframe> element but have not taken effect yet; use
391 // pending_frame_policy() for those. To see the flags which will take effect
392 // on navigation (which does not include the CSP-set flags), use
393 // effective_frame_policy().
394 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34395 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44396 }
397
398 // Updates the active sandbox flags in this frame, in response to a
399 // Content-Security-Policy header adding additional flags, in addition to
400 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39401 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44402 // will be cleared, and the flags in the pending frame policy will be applied
403 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31404 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39405 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31406 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44407 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06408 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44409
410 // Returns whether the frame received a user gesture on a previous navigation
411 // on the same eTLD+1.
412 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34413 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44414 }
415
416 // When a tab is discarded, WebContents sets was_discarded on its
417 // root FrameTreeNode.
418 // In addition, when a child frame is created, this bit is passed on from
419 // parent to child.
420 // When a navigation request is created, was_discarded is passed on to the
421 // request and reset to false in FrameTreeNode.
422 void set_was_discarded() { was_discarded_ = true; }
423 bool was_discarded() const { return was_discarded_; }
424
425 // Returns the sticky bit of the User Activation v2 state of the
426 // |FrameTreeNode|.
427 bool HasStickyUserActivation() const {
428 return user_activation_state_.HasBeenActive();
429 }
430
431 // Returns the transient bit of the User Activation v2 state of the
432 // |FrameTreeNode|.
433 bool HasTransientUserActivation() {
434 return user_activation_state_.IsActive();
435 }
436
437 // Remove history entries for all frames created by script in this frame's
438 // subtree. If a frame created by a script is removed, then its history entry
439 // will never be reused - this saves memory.
440 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
441
442 blink::mojom::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45443 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44444 }
danakjc492bf82020-09-09 20:02:44445
Daniel Cheng6ac128172021-05-25 18:49:01446 blink::mojom::TreeScopeType tree_scope_type() const {
447 return tree_scope_type_;
448 }
449
Alex Turner10d557a42021-06-01 19:06:49450 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44451
arthursonzogni034bb9c2020-10-01 08:29:56452 // The initial popup URL for new window opened using:
453 // `window.open(initial_popup_url)`.
454 // An empty GURL otherwise.
455 //
456 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
457 // document served from this URL. The FrameTreeNode always starts hosting the
458 // initial empty document and attempts a navigation toward this URL. However
459 // the navigation might be delayed, redirected and even cancelled.
460 void SetInitialPopupURL(const GURL& initial_popup_url);
461 const GURL& initial_popup_url() const { return initial_popup_url_; }
462
463 // The origin of the document that used window.open() to create this frame.
464 // Otherwise, an opaque Origin with a nonce different from all previously
465 // existing Origins.
466 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
467 const url::Origin& popup_creator_origin() const {
468 return popup_creator_origin_;
469 }
470
Harkiran Bolaria59290d62021-03-17 01:53:01471 // Sets the associated FrameTree for this node. The node can change FrameTrees
472 // when blink::features::Prerender2 is enabled, which allows a page loaded in
473 // the prerendered FrameTree to be used for a navigation in the primary frame
474 // tree.
475 void SetFrameTree(FrameTree& frame_tree);
476
Alexander Timinf785f342021-03-18 00:00:56477 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24478 void WriteIntoTrace(perfetto::TracedValue context) const;
Alexander Timinf785f342021-03-18 00:00:56479
Carlos Caballero76711352021-03-24 17:38:21480 // Returns true the node is navigating, i.e. it has an associated
481 // NavigationRequest.
482 bool HasNavigation();
483
shivanigithubf3ddff52021-07-03 22:06:30484 // Fenced frames (meta-bug crbug.com/1111084):
485 // Returns false if fenced frames are disabled. Returns true if the feature is
486 // enabled and if |this| is a fenced frame. Returns false for
487 // iframes embedded in a fenced frame. To clarify: for the MPArch
488 // implementation this only returns true if |this| is the actual
489 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
490 // outer FrameTree.
491 bool IsFencedFrame() const;
492
493 // Returns false if fenced frames are disabled. Returns true if the
494 // feature is enabled and if |this| or any of its ancestor nodes is a
495 // fenced frame.
496 bool IsInFencedFrameTree() const;
497
danakjc492bf82020-09-09 20:02:44498 private:
Charlie Hubb5943d2021-03-09 19:46:12499 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44500 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12501 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44502 ContainerPolicySandboxDynamic);
503
504 class OpenerDestroyedObserver;
505
506 FrameTreeNode* GetSibling(int relative_offset) const;
507
508 // The |notification_type| parameter is used for histograms only.
509 bool NotifyUserActivation(
510 blink::mojom::UserActivationNotificationType notification_type);
511
512 bool ConsumeTransientUserActivation();
513
514 bool ClearUserActivation();
515
516 // Verify that the renderer process is allowed to set user activation on this
517 // frame by checking whether this frame's RenderWidgetHost had previously seen
518 // an input event that might lead to user activation. If user activation
519 // should be allowed, this returns true and also clears corresponding pending
520 // user activation state in the widget. Otherwise, this returns false.
521 bool VerifyUserActivation();
522
523 // The next available browser-global FrameTreeNode ID.
524 static int next_frame_tree_node_id_;
525
526 // The FrameTree that owns us.
527 FrameTree* frame_tree_; // not owned.
528
danakjc492bf82020-09-09 20:02:44529 // A browser-global identifier for the frame in the page, which stays stable
530 // even if the frame does a cross-process navigation.
531 const int frame_tree_node_id_;
532
533 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
534 // life of this FrameTreeNode. |nullptr| if this node is the root.
535 RenderFrameHostImpl* const parent_;
536
537 // Number of edges from this node to the root. 0 if this is the root.
538 const unsigned int depth_;
539
540 // The frame that opened this frame, if any. Will be set to null if the
541 // opener is closed, or if this frame disowns its opener by setting its
542 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19543 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44544
545 // An observer that clears this node's |opener_| if the opener is destroyed.
546 // This observer is added to the |opener_|'s observer list when the |opener_|
547 // is set to a non-null node, and it is removed from that list when |opener_|
548 // changes or when this node is destroyed. It is also cleared if |opener_|
549 // is disowned.
550 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
551
552 // The frame that opened this frame, if any. Contrary to opener_, this
553 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19554 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44555
Wolfgang Beyerd8809db2020-09-30 15:29:39556 // The devtools frame token of the frame which opened this frame. This is
557 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07558 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39559
danakjc492bf82020-09-09 20:02:44560 // An observer that clears this node's |original_opener_| if the opener is
561 // destroyed.
562 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
563
arthursonzogni034bb9c2020-10-01 08:29:56564 // When created by an opener, the URL specified in window.open(url)
565 // Please refer to {Get,Set}InitialPopupURL() documentation.
566 GURL initial_popup_url_;
567
568 // When created using window.open, the origin of the creator.
569 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
570 url::Origin popup_creator_origin_;
571
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56572 // Returns true iff SetCurrentURL has been called with a non-blank URL.
573 // TODO(https://crbug.com/1215096): Migrate all current usage of this to
574 // use `is_on_initial_empty_document_or_subsequent_empty_documents_` instead.
575 bool has_committed_real_load_ = false;
576
577 // Whether this frame is still on the initial about:blank document or any
578 // subsequent about:blank documents committed after the initial about:blank
579 // document. This will be false if either of these has happened:
Rakina Zata Amnid09b6112021-06-05 06:20:14580 // - SetCurrentUrl() has been called with a non about:blank URL.
581 // - The document's input stream has been opened with document.open().
582 // See:
583 // 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:56584 // TODO(https://crbug.com/1215096): Make this false after non-initial
Rakina Zata Amnid09b6112021-06-05 06:20:14585 // about:blank commits as well, making this only track whether the current
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56586 // document is the initial empty document or not. Currently we are still
587 // preserving most of the old behavior of `has_committed_real_load_` (except
588 // for the document.open() bit here) due to our current handling of initial
589 // empty document for session history and navigation (where we treat the
590 // the initial about:blank document and subsequent about:blank documents the
591 // same way).
592 bool is_on_initial_empty_document_or_subsequent_empty_documents_ = true;
danakjc492bf82020-09-09 20:02:44593
594 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19595 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44596
Daniel Cheng6ac128172021-05-25 18:49:01597 // The type of frame owner for this frame. This is only relevant for non-main
598 // frames.
Daniel Cheng9bd90f92021-04-23 20:49:45599 const blink::mojom::FrameOwnerElementType frame_owner_element_type_ =
600 blink::mojom::FrameOwnerElementType::kNone;
601
Daniel Cheng6ac128172021-05-25 18:49:01602 // The tree scope type of frame owner element, i.e. whether the element is in
603 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
604 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
605 // relevant for non-main frames.
606 const blink::mojom::TreeScopeType tree_scope_type_ =
607 blink::mojom::TreeScopeType::kDocument;
608
danakjc492bf82020-09-09 20:02:44609 // Track information that needs to be replicated to processes that have
610 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37611 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44612
613 // Track the pending sandbox flags and container policy for this frame. When a
614 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
615 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34616 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44617 // they take effect on the next frame navigation.
618 blink::FramePolicy pending_frame_policy_;
619
620 // Whether the frame was created by javascript. This is useful to prune
621 // history entries when the frame is removed (because frames created by
622 // scripts are never recreated with the same unique name - see
623 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19624 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44625
626 // Used for devtools instrumentation and trace-ability. The token is
627 // propagated to Blink's LocalFrame and both Blink and content/
628 // can tag calls and requests with this token in order to attribute them
629 // to the context frame.
630 // |devtools_frame_token_| is only defined by the browser process and is never
631 // sent back from the renderer in the control calls. It should be never used
632 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19633 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44634
635 // Tracks the scrolling and margin properties for this frame. These
636 // properties affect the child renderer but are stored on its parent's
637 // frame element. When this frame's parent dynamically updates these
638 // properties, we update them here too.
639 //
640 // Note that dynamic updates only take effect on the next frame navigation.
641 blink::mojom::FrameOwnerProperties frame_owner_properties_;
642
643 // Contains the current parsed value of the 'csp' attribute of this frame.
644 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
645
646 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
647 // be reset and a RenderFrameHost will be responsible for the navigation.
648 std::unique_ptr<NavigationRequest> navigation_request_;
649
650 // List of objects observing this FrameTreeNode.
651 base::ObserverList<Observer>::Unchecked observers_;
652
653 base::TimeTicks last_focus_time_;
654
arthursonzogni9816b9192021-03-29 16:09:19655 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44656
657 // The user activation state of the current frame. See |UserActivationState|
658 // for details on how this state is maintained.
659 blink::UserActivationState user_activation_state_;
660
661 // A helper for tracing the snapshots of this FrameTreeNode and attributing
662 // browser process activities to this node (when possible). It is unrelated
663 // to the core logic of FrameTreeNode.
664 FrameTreeNodeBlameContext blame_context_;
665
Lukasz Anforowicz147141962020-12-16 18:03:24666 // Manages creation and swapping of RenderFrameHosts for this frame.
667 //
668 // This field needs to be declared last, because destruction of
669 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
670 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
671 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
672 // may try to use FrameTreeNode's fields above - this would be an undefined
673 // behavior if the fields (even trivially-destructible ones) were destructed
674 // before the RenderFrameHostManager's destructor runs. See also
675 // https://crbug.com/1157988.
676 RenderFrameHostManager render_manager_;
677
danakjc492bf82020-09-09 20:02:44678 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
679};
680
681} // namespace content
682
683#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_