blob: ffc01a3acf5207c8312619e380eadd09e3ce0a66 [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"
danakjc492bf82020-09-09 20:02:4415#include "base/memory/ref_counted.h"
16#include "content/browser/renderer_host/frame_tree.h"
17#include "content/browser/renderer_host/frame_tree_node_blame_context.h"
18#include "content/browser/renderer_host/navigator.h"
19#include "content/browser/renderer_host/render_frame_host_impl.h"
20#include "content/browser/renderer_host/render_frame_host_manager.h"
21#include "content/common/content_export.h"
danakjc492bf82020-09-09 20:02:4422#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Lei Zhang698df03c2021-05-21 04:23:3423#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin McNee43fe8292021-10-04 22:59:4124#include "third_party/blink/public/common/frame/frame_owner_element_type.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"
danakjc492bf82020-09-09 20:02:4427#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3728#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0129#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
danakjc492bf82020-09-09 20:02:4430#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
31#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-forward.h"
32
33#include "url/gurl.h"
34#include "url/origin.h"
35
36namespace content {
37
38class NavigationRequest;
39class RenderFrameHostImpl;
40class NavigationEntryImpl;
41
42// When a page contains iframes, its renderer process maintains a tree structure
43// of those frames. We are mirroring this tree in the browser process. This
44// class represents a node in this tree and is a wrapper for all objects that
45// are frame-specific (as opposed to page-specific).
46//
47// Each FrameTreeNode has a current RenderFrameHost, which can change over
48// time as the frame is navigated. Any immediate subframes of the current
49// document are tracked using FrameTreeNodes owned by the current
50// RenderFrameHost, rather than as children of FrameTreeNode itself. This
51// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
52// still alive - for example while pending deletion, after a new current
53// RenderFrameHost has replaced it.
54class CONTENT_EXPORT FrameTreeNode {
55 public:
56 class Observer {
57 public:
58 // Invoked when a FrameTreeNode is being destroyed.
59 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
60
61 // Invoked when a FrameTreeNode becomes focused.
62 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
63
Fergal Dalya1d569972021-03-16 03:24:5364 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4465 };
66
67 static const int kFrameTreeNodeInvalidId;
68
69 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
70 // regardless of which FrameTree it is in.
71 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
72
73 // Returns the FrameTreeNode for the given |rfh|. Same as
74 // rfh->frame_tree_node(), but also supports nullptrs.
75 static FrameTreeNode* From(RenderFrameHost* rfh);
76
77 // Callers are are expected to initialize sandbox flags separately after
78 // calling the constructor.
79 FrameTreeNode(
80 FrameTree* frame_tree,
81 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0182 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4483 const std::string& name,
84 const std::string& unique_name,
85 bool is_created_by_script,
86 const base::UnguessableToken& devtools_frame_token,
87 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4188 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3489 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4490
Peter Boström828b9022021-09-21 02:28:4391 FrameTreeNode(const FrameTreeNode&) = delete;
92 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
93
danakjc492bf82020-09-09 20:02:4494 ~FrameTreeNode();
95
96 void AddObserver(Observer* observer);
97 void RemoveObserver(Observer* observer);
98
99 bool IsMainFrame() const;
100
arthursonzogni76098e52020-11-25 14:18:45101 // Clears any state in this node which was set by the document itself (CSP &
102 // UserActivationState) and notifies proxies as appropriate. Invoked after
103 // committing navigation to a new document (since the new document comes with
104 // a fresh set of CSP).
105 // TODO(arthursonzogni): Remove this function. The frame/document must not be
106 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13107 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44108
109 FrameTree* frame_tree() const { return frame_tree_; }
110 Navigator& navigator() { return frame_tree()->navigator(); }
111
112 RenderFrameHostManager* render_manager() { return &render_manager_; }
113 int frame_tree_node_id() const { return frame_tree_node_id_; }
Antonio Sartori90f41212021-01-22 10:08:34114 const std::string& frame_name() const { return replication_state_->name; }
danakjc492bf82020-09-09 20:02:44115
116 const std::string& unique_name() const {
Antonio Sartori90f41212021-01-22 10:08:34117 return replication_state_->unique_name;
danakjc492bf82020-09-09 20:02:44118 }
119
120 // See comment on the member declaration.
121 const base::UnguessableToken& devtools_frame_token() const {
122 return devtools_frame_token_;
123 }
124
125 size_t child_count() const { return current_frame_host()->child_count(); }
126
danakjc492bf82020-09-09 20:02:44127 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
Rakina Zata Amni86c88fa2021-11-01 01:27:30168 // Sets the last committed URL for this frame.
danakjc492bf82020-09-09 20:02:44169 void SetCurrentURL(const GURL& url);
170
Rakina Zata Amni86c88fa2021-11-01 01:27:30171 // The frame committed a document that is not the initial empty document.
172 // Update `has_committed_real_load_` and `is_on_initial_empty_document_`
173 // accordingly.
174 void DidCommitNonInitialEmptyDocument();
175
176 // Returns true if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30177 // empty document, or if the current document's input stream has been opened
178 // with document.open(), causing the document to lose its "initial empty
179 // document" status. For more details, see the definition of
180 // `is_on_initial_empty_document_`.
181 bool is_on_initial_empty_document() const {
182 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56183 }
184
Rakina Zata Amni86c88fa2021-11-01 01:27:30185 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56186 // false. Must only be called after the current document's input stream has
187 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30188 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14189
danakjc492bf82020-09-09 20:02:44190 // Returns whether the frame's owner element in the parent document is
191 // collapsed, that is, removed from the layout as if it did not exist, as per
192 // request by the embedder (of the content/ layer).
193 bool is_collapsed() const { return is_collapsed_; }
194
195 // Sets whether to collapse the frame's owner element in the parent document,
196 // that is, to remove it from the layout as if it did not exist, as per
197 // request by the embedder (of the content/ layer). Cannot be called for main
198 // frames.
199 //
200 // This only has an effect for <iframe> owner elements, and is a no-op when
201 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
202 void SetCollapsed(bool collapsed);
203
204 // Returns the origin of the last committed page in this frame.
205 // WARNING: To get the last committed origin for a particular
206 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
207 // which will behave correctly even when the RenderFrameHost is not the
208 // current one for this frame (such as when it's pending deletion).
209 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34210 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44211 }
212
213 // Set the current origin and notify proxies about the update.
214 void SetCurrentOrigin(const url::Origin& origin,
215 bool is_potentially_trustworthy_unique_origin);
216
217 // Set the current name and notify proxies about the update.
218 void SetFrameName(const std::string& name, const std::string& unique_name);
219
danakjc492bf82020-09-09 20:02:44220 // Sets the current insecure request policy, and notifies proxies about the
221 // update.
222 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
223
224 // Sets the current set of insecure urls to upgrade, and notifies proxies
225 // about the update.
226 void SetInsecureNavigationsSet(
227 const std::vector<uint32_t>& insecure_navigations_set);
228
229 // Returns the latest frame policy (sandbox flags and container policy) for
230 // this frame. This includes flags inherited from parent frames and the latest
231 // flags from the <iframe> element hosting this frame. The returned policies
232 // may not yet have taken effect, since "sandbox" and "allow" attribute
233 // updates in an <iframe> element take effect on next navigation. To retrieve
234 // the currently active policy for this frame, use effective_frame_policy().
235 const blink::FramePolicy& pending_frame_policy() const {
236 return pending_frame_policy_;
237 }
238
239 // Update this frame's sandbox flags and container policy. This is called
240 // when a parent frame updates the "sandbox" attribute in the <iframe> element
241 // for this frame, or any of the attributes which affect the container policy
242 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
243 // These policies won't take effect until next navigation. If this frame's
244 // parent is itself sandboxed, the parent's sandbox flags are combined with
245 // those in |frame_policy|.
246 // Attempting to change the container policy on the main frame will have no
247 // effect.
248 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
249
250 // Returns the currently active frame policy for this frame, including the
251 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39252 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44253 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
254 // origin of the iframe's src attribute (which may be different from the URL
255 // of the document currently loaded into the frame). This does not include
256 // policy changes that have been made by updating the containing iframe
257 // element attributes since the frame was last navigated; use
258 // pending_frame_policy() for those.
259 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34260 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44261 }
262
263 // Set the frame_policy provided in function parameter as active frame policy,
264 // while leaving pending_frame_policy_ untouched.
265 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
266
267 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
268 return frame_owner_properties_;
269 }
270
271 void set_frame_owner_properties(
272 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
273 frame_owner_properties_ = frame_owner_properties;
274 }
275
276 const network::mojom::ContentSecurityPolicy* csp_attribute() {
277 return csp_attribute_.get();
278 }
279
280 void set_csp_attribute(
281 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
282 csp_attribute_ = std::move(parsed_csp_attribute);
283 }
284
Antonio Sartori5abc8de2021-07-13 08:42:47285 // Reflects the 'anonymous' attribute of the corresponding iframe html
286 // element.
287 bool anonymous() const { return anonymous_; }
288 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
289
danakjc492bf82020-09-09 20:02:44290 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34291 return replication_state_->origin.IsSameOriginWith(
292 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44293 }
294
Gyuyoung Kimc16e52e92021-03-19 02:45:37295 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34296 return *replication_state_;
danakjc492bf82020-09-09 20:02:44297 }
298
299 RenderFrameHostImpl* current_frame_host() const {
300 return render_manager_.current_frame_host();
301 }
302
danakjc492bf82020-09-09 20:02:44303 // Returns true if this node is in a loading state.
304 bool IsLoading() const;
305
Alex Moshchuk9b0fd822020-10-26 23:08:15306 // Returns true if this node has a cross-document navigation in progress.
307 bool HasPendingCrossDocumentNavigation() const;
308
danakjc492bf82020-09-09 20:02:44309 NavigationRequest* navigation_request() { return navigation_request_.get(); }
310
311 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
312 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
313 // RenderFrameHost that is committing the navigation.
314 void TransferNavigationRequestOwnership(
315 RenderFrameHostImpl* render_frame_host);
316
317 // Takes ownership of |navigation_request| and makes it the current
318 // NavigationRequest of this frame. This corresponds to the start of a new
319 // navigation. If there was an ongoing navigation request before calling this
320 // function, it is canceled. |navigation_request| should not be null.
321 void CreatedNavigationRequest(
322 std::unique_ptr<NavigationRequest> navigation_request);
323
324 // Resets the current navigation request. If |keep_state| is true, any state
325 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
326 // loading state) will not be reset by the function.
327 void ResetNavigationRequest(bool keep_state);
328
329 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19330 // |should_show_loading_ui| indicates whether this navigation should be
331 // visible in the UI. True for cross-document navigations and navigations
332 // intercepted by appHistory's transitionWhile().
danakjc492bf82020-09-09 20:02:44333 // |was_previously_loading| is false if the FrameTree was not loading before.
334 // The caller is required to provide this boolean as the delegate should only
335 // be notified if the FrameTree went from non-loading to loading state.
336 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19337 void DidStartLoading(bool should_show_loading_ui,
338 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44339
340 // A RenderFrameHost in this node stopped loading.
341 void DidStopLoading();
342
343 // The load progress for a RenderFrameHost in this node was updated to
344 // |load_progress|. This will notify the FrameTree which will in turn notify
345 // the WebContents.
346 void DidChangeLoadProgress(double load_progress);
347
348 // Called when the user directed the page to stop loading. Stops all loads
349 // happening in the FrameTreeNode. This method should be used with
350 // FrameTree::ForEach to stop all loads in the entire FrameTree.
351 bool StopLoading();
352
353 // Returns the time this frame was last focused.
354 base::TimeTicks last_focus_time() const { return last_focus_time_; }
355
356 // Called when this node becomes focused. Updates the node's last focused
357 // time and notifies observers.
358 void DidFocus();
359
360 // Called when the user closed the modal dialogue for BeforeUnload and
361 // cancelled the navigation. This should stop any load happening in the
362 // FrameTreeNode.
363 void BeforeUnloadCanceled();
364
365 // Returns the BlameContext associated with this node.
366 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
367
368 // Updates the user activation state in the browser frame tree and in the
369 // frame trees in all renderer processes except the renderer for this node
370 // (which initiated the update). Returns |false| if the update tries to
371 // consume an already consumed/expired transient state, |true| otherwise. See
372 // the comment on user_activation_state_ below.
373 //
374 // The |notification_type| parameter is used for histograms, only for the case
375 // |update_state == kNotifyActivation|.
376 bool UpdateUserActivationState(
377 blink::mojom::UserActivationUpdateType update_type,
378 blink::mojom::UserActivationNotificationType notification_type);
379
380 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
381
382 // Returns the sandbox flags currently in effect for this frame. This includes
383 // flags inherited from parent frames, the currently active flags from the
384 // <iframe> element hosting this frame, as well as any flags set from a
385 // Content-Security-Policy HTTP header. This does not include flags that have
386 // have been updated in an <iframe> element but have not taken effect yet; use
387 // pending_frame_policy() for those. To see the flags which will take effect
388 // on navigation (which does not include the CSP-set flags), use
389 // effective_frame_policy().
390 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34391 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44392 }
393
394 // Updates the active sandbox flags in this frame, in response to a
395 // Content-Security-Policy header adding additional flags, in addition to
396 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39397 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44398 // will be cleared, and the flags in the pending frame policy will be applied
399 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31400 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39401 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31402 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44403 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06404 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44405
406 // Returns whether the frame received a user gesture on a previous navigation
407 // on the same eTLD+1.
408 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34409 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44410 }
411
412 // When a tab is discarded, WebContents sets was_discarded on its
413 // root FrameTreeNode.
414 // In addition, when a child frame is created, this bit is passed on from
415 // parent to child.
416 // When a navigation request is created, was_discarded is passed on to the
417 // request and reset to false in FrameTreeNode.
418 void set_was_discarded() { was_discarded_ = true; }
419 bool was_discarded() const { return was_discarded_; }
420
421 // Returns the sticky bit of the User Activation v2 state of the
422 // |FrameTreeNode|.
423 bool HasStickyUserActivation() const {
424 return user_activation_state_.HasBeenActive();
425 }
426
427 // Returns the transient bit of the User Activation v2 state of the
428 // |FrameTreeNode|.
429 bool HasTransientUserActivation() {
430 return user_activation_state_.IsActive();
431 }
432
433 // Remove history entries for all frames created by script in this frame's
434 // subtree. If a frame created by a script is removed, then its history entry
435 // will never be reused - this saves memory.
436 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
437
Kevin McNee43fe8292021-10-04 22:59:41438 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45439 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44440 }
danakjc492bf82020-09-09 20:02:44441
Daniel Cheng6ac128172021-05-25 18:49:01442 blink::mojom::TreeScopeType tree_scope_type() const {
443 return tree_scope_type_;
444 }
445
Alex Turner10d557a42021-06-01 19:06:49446 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44447
arthursonzogni034bb9c2020-10-01 08:29:56448 // The initial popup URL for new window opened using:
449 // `window.open(initial_popup_url)`.
450 // An empty GURL otherwise.
451 //
452 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
453 // document served from this URL. The FrameTreeNode always starts hosting the
454 // initial empty document and attempts a navigation toward this URL. However
455 // the navigation might be delayed, redirected and even cancelled.
456 void SetInitialPopupURL(const GURL& initial_popup_url);
457 const GURL& initial_popup_url() const { return initial_popup_url_; }
458
459 // The origin of the document that used window.open() to create this frame.
460 // Otherwise, an opaque Origin with a nonce different from all previously
461 // existing Origins.
462 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
463 const url::Origin& popup_creator_origin() const {
464 return popup_creator_origin_;
465 }
466
Harkiran Bolaria59290d62021-03-17 01:53:01467 // Sets the associated FrameTree for this node. The node can change FrameTrees
468 // when blink::features::Prerender2 is enabled, which allows a page loaded in
469 // the prerendered FrameTree to be used for a navigation in the primary frame
470 // tree.
471 void SetFrameTree(FrameTree& frame_tree);
472
Alexander Timinf785f342021-03-18 00:00:56473 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24474 void WriteIntoTrace(perfetto::TracedValue context) const;
Rakina Zata Amni4b1968d2021-09-09 03:29:47475 void WriteIntoTrace(
476 perfetto::TracedProto<perfetto::protos::pbzero::FrameTreeNodeInfo> proto);
Alexander Timinf785f342021-03-18 00:00:56477
Carlos Caballero76711352021-03-24 17:38:21478 // Returns true the node is navigating, i.e. it has an associated
479 // NavigationRequest.
480 bool HasNavigation();
481
shivanigithubf3ddff52021-07-03 22:06:30482 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30483 // Note that these two functions cannot be invoked from a FrameTree's or
484 // its root node's constructor since they require the frame tree and the
485 // root node to be completely constructed.
486 //
shivanigithubf3ddff52021-07-03 22:06:30487 // Returns false if fenced frames are disabled. Returns true if the feature is
488 // enabled and if |this| is a fenced frame. Returns false for
489 // iframes embedded in a fenced frame. To clarify: for the MPArch
490 // implementation this only returns true if |this| is the actual
491 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
492 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36493 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30494
495 // Returns false if fenced frames are disabled. Returns true if the
496 // feature is enabled and if |this| or any of its ancestor nodes is a
497 // fenced frame.
498 bool IsInFencedFrameTree() const;
499
shivanigithub4cd016a2021-09-20 21:10:30500 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
501 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
502 // details.
503 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
504 return fenced_frame_nonce_;
505 }
506
507 // If applicable, set the fenced frame nonce. See comment on
508 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
509 // by FrameTree::Init() or FrameTree::AddFrame().
510 void SetFencedFrameNonceIfNeeded();
511
Harkiran Bolariab4437fd2021-08-11 17:51:22512 // Sets the unique_name and name fields on replication_state_. To be used in
513 // prerender activation to make sure the FrameTreeNode replication state is
514 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
515 // renderers should already have the correct value, so unlike
516 // FrameTreeNode::SetFrameName, we do not notify them here.
517 // TODO(https://crbug.com/1237091): Remove this once the Browsing Instance
518 // Frame State is implemented.
519 void set_frame_name_for_activation(const std::string& unique_name,
520 const std::string& name) {
521 replication_state_->unique_name = unique_name;
522 replication_state_->name = name;
523 }
524
danakjc492bf82020-09-09 20:02:44525 private:
Charlie Hubb5943d2021-03-09 19:46:12526 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44527 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12528 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44529 ContainerPolicySandboxDynamic);
530
531 class OpenerDestroyedObserver;
532
danakjc492bf82020-09-09 20:02:44533 // The |notification_type| parameter is used for histograms only.
534 bool NotifyUserActivation(
535 blink::mojom::UserActivationNotificationType notification_type);
536
537 bool ConsumeTransientUserActivation();
538
539 bool ClearUserActivation();
540
541 // Verify that the renderer process is allowed to set user activation on this
542 // frame by checking whether this frame's RenderWidgetHost had previously seen
543 // an input event that might lead to user activation. If user activation
544 // should be allowed, this returns true and also clears corresponding pending
545 // user activation state in the widget. Otherwise, this returns false.
546 bool VerifyUserActivation();
547
548 // The next available browser-global FrameTreeNode ID.
549 static int next_frame_tree_node_id_;
550
551 // The FrameTree that owns us.
552 FrameTree* frame_tree_; // not owned.
553
danakjc492bf82020-09-09 20:02:44554 // A browser-global identifier for the frame in the page, which stays stable
555 // even if the frame does a cross-process navigation.
556 const int frame_tree_node_id_;
557
558 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
559 // life of this FrameTreeNode. |nullptr| if this node is the root.
560 RenderFrameHostImpl* const parent_;
561
danakjc492bf82020-09-09 20:02:44562 // The frame that opened this frame, if any. Will be set to null if the
563 // opener is closed, or if this frame disowns its opener by setting its
564 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19565 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44566
567 // An observer that clears this node's |opener_| if the opener is destroyed.
568 // This observer is added to the |opener_|'s observer list when the |opener_|
569 // is set to a non-null node, and it is removed from that list when |opener_|
570 // changes or when this node is destroyed. It is also cleared if |opener_|
571 // is disowned.
572 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
573
574 // The frame that opened this frame, if any. Contrary to opener_, this
575 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19576 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44577
Wolfgang Beyerd8809db2020-09-30 15:29:39578 // The devtools frame token of the frame which opened this frame. This is
579 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07580 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39581
danakjc492bf82020-09-09 20:02:44582 // An observer that clears this node's |original_opener_| if the opener is
583 // destroyed.
584 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
585
arthursonzogni034bb9c2020-10-01 08:29:56586 // When created by an opener, the URL specified in window.open(url)
587 // Please refer to {Get,Set}InitialPopupURL() documentation.
588 GURL initial_popup_url_;
589
590 // When created using window.open, the origin of the creator.
591 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
592 url::Origin popup_creator_origin_;
593
Rakina Zata Amni86c88fa2021-11-01 01:27:30594 // Whether this frame is still on the initial about:blank document or the
595 // synchronously committed about:blank document committed at frame creation,
596 // and its "initial empty document"-ness is still true.
597 // This will be false if either of these has happened:
598 // - SetCurrentUrl() was called after committing a document that is not the
599 // initial about:blank document or the synchronously committed about:blank
600 // document, per
601 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
602 // - The document's input stream has been opened with document.open(), per
603 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
604 // NOTE: we treat both the "initial about:blank document" and the
605 // "synchronously committed about:blank document" as the initial empty
606 // document. In the future, we plan to remove the synchronous about:blank
607 // commit so that this state will only be true if the frame is on the
608 // "initial about:blank document". See also:
609 // - https://github.com/whatwg/html/issues/6863
610 // - https://crbug.com/1215096
611 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56612
danakjc492bf82020-09-09 20:02:44613 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19614 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44615
Daniel Cheng6ac128172021-05-25 18:49:01616 // The type of frame owner for this frame. This is only relevant for non-main
617 // frames.
Kevin McNee43fe8292021-10-04 22:59:41618 const blink::FrameOwnerElementType frame_owner_element_type_ =
619 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45620
Daniel Cheng6ac128172021-05-25 18:49:01621 // The tree scope type of frame owner element, i.e. whether the element is in
622 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
623 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
624 // relevant for non-main frames.
625 const blink::mojom::TreeScopeType tree_scope_type_ =
626 blink::mojom::TreeScopeType::kDocument;
627
danakjc492bf82020-09-09 20:02:44628 // Track information that needs to be replicated to processes that have
629 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37630 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44631
632 // Track the pending sandbox flags and container policy for this frame. When a
633 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
634 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34635 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44636 // they take effect on the next frame navigation.
637 blink::FramePolicy pending_frame_policy_;
638
639 // Whether the frame was created by javascript. This is useful to prune
640 // history entries when the frame is removed (because frames created by
641 // scripts are never recreated with the same unique name - see
642 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19643 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44644
645 // Used for devtools instrumentation and trace-ability. The token is
646 // propagated to Blink's LocalFrame and both Blink and content/
647 // can tag calls and requests with this token in order to attribute them
648 // to the context frame.
649 // |devtools_frame_token_| is only defined by the browser process and is never
650 // sent back from the renderer in the control calls. It should be never used
651 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19652 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44653
654 // Tracks the scrolling and margin properties for this frame. These
655 // properties affect the child renderer but are stored on its parent's
656 // frame element. When this frame's parent dynamically updates these
657 // properties, we update them here too.
658 //
659 // Note that dynamic updates only take effect on the next frame navigation.
660 blink::mojom::FrameOwnerProperties frame_owner_properties_;
661
662 // Contains the current parsed value of the 'csp' attribute of this frame.
663 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
664
Antonio Sartori5abc8de2021-07-13 08:42:47665 // Reflects the 'anonymous' attribute of the corresponding iframe html
666 // element.
667 bool anonymous_ = false;
668
danakjc492bf82020-09-09 20:02:44669 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
670 // be reset and a RenderFrameHost will be responsible for the navigation.
671 std::unique_ptr<NavigationRequest> navigation_request_;
672
673 // List of objects observing this FrameTreeNode.
674 base::ObserverList<Observer>::Unchecked observers_;
675
676 base::TimeTicks last_focus_time_;
677
arthursonzogni9816b9192021-03-29 16:09:19678 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44679
680 // The user activation state of the current frame. See |UserActivationState|
681 // for details on how this state is maintained.
682 blink::UserActivationState user_activation_state_;
683
684 // A helper for tracing the snapshots of this FrameTreeNode and attributing
685 // browser process activities to this node (when possible). It is unrelated
686 // to the core logic of FrameTreeNode.
687 FrameTreeNodeBlameContext blame_context_;
688
shivanigithub4cd016a2021-09-20 21:10:30689 // Fenced Frames:
690 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
691 // frame and any iframes nested within it. Not set if this frame is not in a
692 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
693 // the MPArch version but for the shadow DOM version we need to keep it here
694 // since the fenced frame root is not a main frame for the latter. The value
695 // of the nonce will be the same for all of the the frames inside a fenced
696 // frame tree. If there is a nested fenced frame it will have a different
697 // nonce than its parent fenced frame. The nonce will stay the same across
698 // navigations because it is always used in conjunction with other fields of
699 // the keys. If the navigation is same-origin/site then the same network stack
700 // partition/storage will be reused and if it's cross-origin/site then other
701 // parts of the key will change and so, even with the same nonce, another
702 // partition will be used.
703 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
704
Lukasz Anforowicz147141962020-12-16 18:03:24705 // Manages creation and swapping of RenderFrameHosts for this frame.
706 //
707 // This field needs to be declared last, because destruction of
708 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
709 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
710 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
711 // may try to use FrameTreeNode's fields above - this would be an undefined
712 // behavior if the fields (even trivially-destructible ones) were destructed
713 // before the RenderFrameHostManager's destructor runs. See also
714 // https://crbug.com/1157988.
715 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44716};
717
718} // namespace content
719
720#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_