blob: 4c4d7b96280119e3836a2a8d0e6d90a740c4831d [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
danakjc492bf82020-09-09 20:02:44125 RenderFrameHostImpl* parent() const { return parent_; }
126
127 FrameTreeNode* opener() const { return opener_; }
128
129 FrameTreeNode* original_opener() const { return original_opener_; }
130
Anton Bikineevf62d1bf2021-05-15 17:56:07131 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39132 return opener_devtools_frame_token_;
133 }
134
danakjc492bf82020-09-09 20:02:44135 // Gets the total number of descendants to this FrameTreeNode in addition to
136 // this node.
137 size_t GetFrameTreeSize() const;
138
139 // Assigns a new opener for this node and, if |opener| is non-null, registers
140 // an observer that will clear this node's opener if |opener| is ever
141 // destroyed.
142 void SetOpener(FrameTreeNode* opener);
143
144 // Assigns the initial opener for this node, and if |opener| is non-null,
145 // registers an observer that will clear this node's opener if |opener| is
146 // ever destroyed. The value set here is the root of the tree.
147 //
148 // It is not possible to change the opener once it was set.
149 void SetOriginalOpener(FrameTreeNode* opener);
150
Wolfgang Beyerd8809db2020-09-30 15:29:39151 // Assigns an opener frame id for this node. This string id is only set once
152 // and cannot be changed. It persists, even if the |opener| is destroyed. It
153 // is used for attribution in the DevTools frontend.
154 void SetOpenerDevtoolsFrameToken(
155 base::UnguessableToken opener_devtools_frame_token);
156
danakjc492bf82020-09-09 20:02:44157 FrameTreeNode* child_at(size_t index) const {
158 return current_frame_host()->child_at(index);
159 }
160
161 // Returns the URL of the last committed page in the current frame.
162 const GURL& current_url() const {
163 return current_frame_host()->GetLastCommittedURL();
164 }
165
166 // Sets the last committed URL for this frame and updates
167 // has_committed_real_load accordingly.
168 void SetCurrentURL(const GURL& url);
169
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56170 // Returns true if SetCurrentURL has been called with a non-blank URL.
171 // TODO(https://crbug.com/1215096): Migrate most usage of
172 // has_committed_real_load() to call
173 // is_on_initial_empty_document_or_subsequent_empty_documents() instead.
danakjc492bf82020-09-09 20:02:44174 bool has_committed_real_load() const { return has_committed_real_load_; }
175
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56176 // Returns true if SetCurrentURL has been called with a non-blank URL or
177 // if the current document's input stream has been opened with
178 // document.open(). For more details, see the definition of
179 // `is_on_initial_empty_document_or_subsequent_empty_documents_`.
180 bool is_on_initial_empty_document_or_subsequent_empty_documents() const {
181 return is_on_initial_empty_document_or_subsequent_empty_documents_;
182 }
183
184 // Sets `is_on_initial_empty_document_or_subsequent_empty_documents_` to
185 // false. Must only be called after the current document's input stream has
186 // been opened with document.open().
187 void DidOpenDocumentInputStream() {
188 is_on_initial_empty_document_or_subsequent_empty_documents_ = false;
189 }
Rakina Zata Amnid09b6112021-06-05 06:20:14190
danakjc492bf82020-09-09 20:02:44191 // Returns whether the frame's owner element in the parent document is
192 // collapsed, that is, removed from the layout as if it did not exist, as per
193 // request by the embedder (of the content/ layer).
194 bool is_collapsed() const { return is_collapsed_; }
195
196 // Sets whether to collapse the frame's owner element in the parent document,
197 // that is, to remove it from the layout as if it did not exist, as per
198 // request by the embedder (of the content/ layer). Cannot be called for main
199 // frames.
200 //
201 // This only has an effect for <iframe> owner elements, and is a no-op when
202 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
203 void SetCollapsed(bool collapsed);
204
205 // Returns the origin of the last committed page in this frame.
206 // WARNING: To get the last committed origin for a particular
207 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
208 // which will behave correctly even when the RenderFrameHost is not the
209 // current one for this frame (such as when it's pending deletion).
210 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34211 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44212 }
213
214 // Set the current origin and notify proxies about the update.
215 void SetCurrentOrigin(const url::Origin& origin,
216 bool is_potentially_trustworthy_unique_origin);
217
218 // Set the current name and notify proxies about the update.
219 void SetFrameName(const std::string& name, const std::string& unique_name);
220
danakjc492bf82020-09-09 20:02:44221 // Sets the current insecure request policy, and notifies proxies about the
222 // update.
223 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
224
225 // Sets the current set of insecure urls to upgrade, and notifies proxies
226 // about the update.
227 void SetInsecureNavigationsSet(
228 const std::vector<uint32_t>& insecure_navigations_set);
229
230 // Returns the latest frame policy (sandbox flags and container policy) for
231 // this frame. This includes flags inherited from parent frames and the latest
232 // flags from the <iframe> element hosting this frame. The returned policies
233 // may not yet have taken effect, since "sandbox" and "allow" attribute
234 // updates in an <iframe> element take effect on next navigation. To retrieve
235 // the currently active policy for this frame, use effective_frame_policy().
236 const blink::FramePolicy& pending_frame_policy() const {
237 return pending_frame_policy_;
238 }
239
240 // Update this frame's sandbox flags and container policy. This is called
241 // when a parent frame updates the "sandbox" attribute in the <iframe> element
242 // for this frame, or any of the attributes which affect the container policy
243 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
244 // These policies won't take effect until next navigation. If this frame's
245 // parent is itself sandboxed, the parent's sandbox flags are combined with
246 // those in |frame_policy|.
247 // Attempting to change the container policy on the main frame will have no
248 // effect.
249 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
250
251 // Returns the currently active frame policy for this frame, including the
252 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39253 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44254 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
255 // origin of the iframe's src attribute (which may be different from the URL
256 // of the document currently loaded into the frame). This does not include
257 // policy changes that have been made by updating the containing iframe
258 // element attributes since the frame was last navigated; use
259 // pending_frame_policy() for those.
260 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34261 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44262 }
263
264 // Set the frame_policy provided in function parameter as active frame policy,
265 // while leaving pending_frame_policy_ untouched.
266 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
267
268 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
269 return frame_owner_properties_;
270 }
271
272 void set_frame_owner_properties(
273 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
274 frame_owner_properties_ = frame_owner_properties;
275 }
276
277 const network::mojom::ContentSecurityPolicy* csp_attribute() {
278 return csp_attribute_.get();
279 }
280
281 void set_csp_attribute(
282 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
283 csp_attribute_ = std::move(parsed_csp_attribute);
284 }
285
Antonio Sartori5abc8de2021-07-13 08:42:47286 // Reflects the 'anonymous' attribute of the corresponding iframe html
287 // element.
288 bool anonymous() const { return anonymous_; }
289 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
290
danakjc492bf82020-09-09 20:02:44291 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34292 return replication_state_->origin.IsSameOriginWith(
293 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44294 }
295
Gyuyoung Kimc16e52e92021-03-19 02:45:37296 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34297 return *replication_state_;
danakjc492bf82020-09-09 20:02:44298 }
299
300 RenderFrameHostImpl* current_frame_host() const {
301 return render_manager_.current_frame_host();
302 }
303
304 // Return the node immediately preceding this node in its parent's children,
305 // or nullptr if there is no such node.
306 FrameTreeNode* PreviousSibling() const;
307
308 // Return the node immediately following this node in its parent's children,
309 // or nullptr if there is no such node.
310 FrameTreeNode* NextSibling() const;
311
312 // Returns true if this node is in a loading state.
313 bool IsLoading() const;
314
Alex Moshchuk9b0fd822020-10-26 23:08:15315 // Returns true if this node has a cross-document navigation in progress.
316 bool HasPendingCrossDocumentNavigation() const;
317
danakjc492bf82020-09-09 20:02:44318 NavigationRequest* navigation_request() { return navigation_request_.get(); }
319
320 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
321 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
322 // RenderFrameHost that is committing the navigation.
323 void TransferNavigationRequestOwnership(
324 RenderFrameHostImpl* render_frame_host);
325
326 // Takes ownership of |navigation_request| and makes it the current
327 // NavigationRequest of this frame. This corresponds to the start of a new
328 // navigation. If there was an ongoing navigation request before calling this
329 // function, it is canceled. |navigation_request| should not be null.
330 void CreatedNavigationRequest(
331 std::unique_ptr<NavigationRequest> navigation_request);
332
333 // Resets the current navigation request. If |keep_state| is true, any state
334 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
335 // loading state) will not be reset by the function.
336 void ResetNavigationRequest(bool keep_state);
337
338 // A RenderFrameHost in this node started loading.
339 // |to_different_document| will be true unless the load is a fragment
340 // navigation, or triggered by history.pushState/replaceState.
341 // |was_previously_loading| is false if the FrameTree was not loading before.
342 // The caller is required to provide this boolean as the delegate should only
343 // be notified if the FrameTree went from non-loading to loading state.
344 // However, when it is called, the FrameTree should be in a loading state.
345 void DidStartLoading(bool to_different_document, bool was_previously_loading);
346
347 // A RenderFrameHost in this node stopped loading.
348 void DidStopLoading();
349
350 // The load progress for a RenderFrameHost in this node was updated to
351 // |load_progress|. This will notify the FrameTree which will in turn notify
352 // the WebContents.
353 void DidChangeLoadProgress(double load_progress);
354
355 // Called when the user directed the page to stop loading. Stops all loads
356 // happening in the FrameTreeNode. This method should be used with
357 // FrameTree::ForEach to stop all loads in the entire FrameTree.
358 bool StopLoading();
359
360 // Returns the time this frame was last focused.
361 base::TimeTicks last_focus_time() const { return last_focus_time_; }
362
363 // Called when this node becomes focused. Updates the node's last focused
364 // time and notifies observers.
365 void DidFocus();
366
367 // Called when the user closed the modal dialogue for BeforeUnload and
368 // cancelled the navigation. This should stop any load happening in the
369 // FrameTreeNode.
370 void BeforeUnloadCanceled();
371
372 // Returns the BlameContext associated with this node.
373 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
374
375 // Updates the user activation state in the browser frame tree and in the
376 // frame trees in all renderer processes except the renderer for this node
377 // (which initiated the update). Returns |false| if the update tries to
378 // consume an already consumed/expired transient state, |true| otherwise. See
379 // the comment on user_activation_state_ below.
380 //
381 // The |notification_type| parameter is used for histograms, only for the case
382 // |update_state == kNotifyActivation|.
383 bool UpdateUserActivationState(
384 blink::mojom::UserActivationUpdateType update_type,
385 blink::mojom::UserActivationNotificationType notification_type);
386
387 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
388
389 // Returns the sandbox flags currently in effect for this frame. This includes
390 // flags inherited from parent frames, the currently active flags from the
391 // <iframe> element hosting this frame, as well as any flags set from a
392 // Content-Security-Policy HTTP header. This does not include flags that have
393 // have been updated in an <iframe> element but have not taken effect yet; use
394 // pending_frame_policy() for those. To see the flags which will take effect
395 // on navigation (which does not include the CSP-set flags), use
396 // effective_frame_policy().
397 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34398 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44399 }
400
401 // Updates the active sandbox flags in this frame, in response to a
402 // Content-Security-Policy header adding additional flags, in addition to
403 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39404 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44405 // will be cleared, and the flags in the pending frame policy will be applied
406 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31407 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39408 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31409 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44410 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06411 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44412
413 // Returns whether the frame received a user gesture on a previous navigation
414 // on the same eTLD+1.
415 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34416 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44417 }
418
419 // When a tab is discarded, WebContents sets was_discarded on its
420 // root FrameTreeNode.
421 // In addition, when a child frame is created, this bit is passed on from
422 // parent to child.
423 // When a navigation request is created, was_discarded is passed on to the
424 // request and reset to false in FrameTreeNode.
425 void set_was_discarded() { was_discarded_ = true; }
426 bool was_discarded() const { return was_discarded_; }
427
428 // Returns the sticky bit of the User Activation v2 state of the
429 // |FrameTreeNode|.
430 bool HasStickyUserActivation() const {
431 return user_activation_state_.HasBeenActive();
432 }
433
434 // Returns the transient bit of the User Activation v2 state of the
435 // |FrameTreeNode|.
436 bool HasTransientUserActivation() {
437 return user_activation_state_.IsActive();
438 }
439
440 // Remove history entries for all frames created by script in this frame's
441 // subtree. If a frame created by a script is removed, then its history entry
442 // will never be reused - this saves memory.
443 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
444
445 blink::mojom::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45446 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44447 }
danakjc492bf82020-09-09 20:02:44448
Daniel Cheng6ac128172021-05-25 18:49:01449 blink::mojom::TreeScopeType tree_scope_type() const {
450 return tree_scope_type_;
451 }
452
Alex Turner10d557a42021-06-01 19:06:49453 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44454
arthursonzogni034bb9c2020-10-01 08:29:56455 // The initial popup URL for new window opened using:
456 // `window.open(initial_popup_url)`.
457 // An empty GURL otherwise.
458 //
459 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
460 // document served from this URL. The FrameTreeNode always starts hosting the
461 // initial empty document and attempts a navigation toward this URL. However
462 // the navigation might be delayed, redirected and even cancelled.
463 void SetInitialPopupURL(const GURL& initial_popup_url);
464 const GURL& initial_popup_url() const { return initial_popup_url_; }
465
466 // The origin of the document that used window.open() to create this frame.
467 // Otherwise, an opaque Origin with a nonce different from all previously
468 // existing Origins.
469 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
470 const url::Origin& popup_creator_origin() const {
471 return popup_creator_origin_;
472 }
473
Harkiran Bolaria59290d62021-03-17 01:53:01474 // Sets the associated FrameTree for this node. The node can change FrameTrees
475 // when blink::features::Prerender2 is enabled, which allows a page loaded in
476 // the prerendered FrameTree to be used for a navigation in the primary frame
477 // tree.
478 void SetFrameTree(FrameTree& frame_tree);
479
Alexander Timinf785f342021-03-18 00:00:56480 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24481 void WriteIntoTrace(perfetto::TracedValue context) const;
Rakina Zata Amni4b1968d2021-09-09 03:29:47482 void WriteIntoTrace(
483 perfetto::TracedProto<perfetto::protos::pbzero::FrameTreeNodeInfo> proto);
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.
Dominic Farolino4bc10ee2021-08-31 00:37:36496 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30497
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
Harkiran Bolariab4437fd2021-08-11 17:51:22503 // Sets the unique_name and name fields on replication_state_. To be used in
504 // prerender activation to make sure the FrameTreeNode replication state is
505 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
506 // renderers should already have the correct value, so unlike
507 // FrameTreeNode::SetFrameName, we do not notify them here.
508 // TODO(https://crbug.com/1237091): Remove this once the Browsing Instance
509 // Frame State is implemented.
510 void set_frame_name_for_activation(const std::string& unique_name,
511 const std::string& name) {
512 replication_state_->unique_name = unique_name;
513 replication_state_->name = name;
514 }
515
danakjc492bf82020-09-09 20:02:44516 private:
Charlie Hubb5943d2021-03-09 19:46:12517 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44518 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12519 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44520 ContainerPolicySandboxDynamic);
521
522 class OpenerDestroyedObserver;
523
524 FrameTreeNode* GetSibling(int relative_offset) const;
525
526 // The |notification_type| parameter is used for histograms only.
527 bool NotifyUserActivation(
528 blink::mojom::UserActivationNotificationType notification_type);
529
530 bool ConsumeTransientUserActivation();
531
532 bool ClearUserActivation();
533
534 // Verify that the renderer process is allowed to set user activation on this
535 // frame by checking whether this frame's RenderWidgetHost had previously seen
536 // an input event that might lead to user activation. If user activation
537 // should be allowed, this returns true and also clears corresponding pending
538 // user activation state in the widget. Otherwise, this returns false.
539 bool VerifyUserActivation();
540
541 // The next available browser-global FrameTreeNode ID.
542 static int next_frame_tree_node_id_;
543
544 // The FrameTree that owns us.
545 FrameTree* frame_tree_; // not owned.
546
danakjc492bf82020-09-09 20:02:44547 // A browser-global identifier for the frame in the page, which stays stable
548 // even if the frame does a cross-process navigation.
549 const int frame_tree_node_id_;
550
551 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
552 // life of this FrameTreeNode. |nullptr| if this node is the root.
553 RenderFrameHostImpl* const parent_;
554
danakjc492bf82020-09-09 20:02:44555 // The frame that opened this frame, if any. Will be set to null if the
556 // opener is closed, or if this frame disowns its opener by setting its
557 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19558 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44559
560 // An observer that clears this node's |opener_| if the opener is destroyed.
561 // This observer is added to the |opener_|'s observer list when the |opener_|
562 // is set to a non-null node, and it is removed from that list when |opener_|
563 // changes or when this node is destroyed. It is also cleared if |opener_|
564 // is disowned.
565 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
566
567 // The frame that opened this frame, if any. Contrary to opener_, this
568 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19569 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44570
Wolfgang Beyerd8809db2020-09-30 15:29:39571 // The devtools frame token of the frame which opened this frame. This is
572 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07573 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39574
danakjc492bf82020-09-09 20:02:44575 // An observer that clears this node's |original_opener_| if the opener is
576 // destroyed.
577 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
578
arthursonzogni034bb9c2020-10-01 08:29:56579 // When created by an opener, the URL specified in window.open(url)
580 // Please refer to {Get,Set}InitialPopupURL() documentation.
581 GURL initial_popup_url_;
582
583 // When created using window.open, the origin of the creator.
584 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
585 url::Origin popup_creator_origin_;
586
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56587 // Returns true iff SetCurrentURL has been called with a non-blank URL.
588 // TODO(https://crbug.com/1215096): Migrate all current usage of this to
589 // use `is_on_initial_empty_document_or_subsequent_empty_documents_` instead.
590 bool has_committed_real_load_ = false;
591
592 // Whether this frame is still on the initial about:blank document or any
593 // subsequent about:blank documents committed after the initial about:blank
594 // document. This will be false if either of these has happened:
Rakina Zata Amnid09b6112021-06-05 06:20:14595 // - SetCurrentUrl() has been called with a non about:blank URL.
596 // - The document's input stream has been opened with document.open().
597 // See:
598 // 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:56599 // TODO(https://crbug.com/1215096): Make this false after non-initial
Rakina Zata Amnid09b6112021-06-05 06:20:14600 // about:blank commits as well, making this only track whether the current
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56601 // document is the initial empty document or not. Currently we are still
602 // preserving most of the old behavior of `has_committed_real_load_` (except
603 // for the document.open() bit here) due to our current handling of initial
604 // empty document for session history and navigation (where we treat the
605 // the initial about:blank document and subsequent about:blank documents the
606 // same way).
607 bool is_on_initial_empty_document_or_subsequent_empty_documents_ = true;
danakjc492bf82020-09-09 20:02:44608
609 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19610 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44611
Daniel Cheng6ac128172021-05-25 18:49:01612 // The type of frame owner for this frame. This is only relevant for non-main
613 // frames.
Daniel Cheng9bd90f92021-04-23 20:49:45614 const blink::mojom::FrameOwnerElementType frame_owner_element_type_ =
615 blink::mojom::FrameOwnerElementType::kNone;
616
Daniel Cheng6ac128172021-05-25 18:49:01617 // The tree scope type of frame owner element, i.e. whether the element is in
618 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
619 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
620 // relevant for non-main frames.
621 const blink::mojom::TreeScopeType tree_scope_type_ =
622 blink::mojom::TreeScopeType::kDocument;
623
danakjc492bf82020-09-09 20:02:44624 // Track information that needs to be replicated to processes that have
625 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37626 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44627
628 // Track the pending sandbox flags and container policy for this frame. When a
629 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
630 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34631 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44632 // they take effect on the next frame navigation.
633 blink::FramePolicy pending_frame_policy_;
634
635 // Whether the frame was created by javascript. This is useful to prune
636 // history entries when the frame is removed (because frames created by
637 // scripts are never recreated with the same unique name - see
638 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19639 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44640
641 // Used for devtools instrumentation and trace-ability. The token is
642 // propagated to Blink's LocalFrame and both Blink and content/
643 // can tag calls and requests with this token in order to attribute them
644 // to the context frame.
645 // |devtools_frame_token_| is only defined by the browser process and is never
646 // sent back from the renderer in the control calls. It should be never used
647 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19648 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44649
650 // Tracks the scrolling and margin properties for this frame. These
651 // properties affect the child renderer but are stored on its parent's
652 // frame element. When this frame's parent dynamically updates these
653 // properties, we update them here too.
654 //
655 // Note that dynamic updates only take effect on the next frame navigation.
656 blink::mojom::FrameOwnerProperties frame_owner_properties_;
657
658 // Contains the current parsed value of the 'csp' attribute of this frame.
659 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
660
Antonio Sartori5abc8de2021-07-13 08:42:47661 // Reflects the 'anonymous' attribute of the corresponding iframe html
662 // element.
663 bool anonymous_ = false;
664
danakjc492bf82020-09-09 20:02:44665 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
666 // be reset and a RenderFrameHost will be responsible for the navigation.
667 std::unique_ptr<NavigationRequest> navigation_request_;
668
669 // List of objects observing this FrameTreeNode.
670 base::ObserverList<Observer>::Unchecked observers_;
671
672 base::TimeTicks last_focus_time_;
673
arthursonzogni9816b9192021-03-29 16:09:19674 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44675
676 // The user activation state of the current frame. See |UserActivationState|
677 // for details on how this state is maintained.
678 blink::UserActivationState user_activation_state_;
679
680 // A helper for tracing the snapshots of this FrameTreeNode and attributing
681 // browser process activities to this node (when possible). It is unrelated
682 // to the core logic of FrameTreeNode.
683 FrameTreeNodeBlameContext blame_context_;
684
Lukasz Anforowicz147141962020-12-16 18:03:24685 // Manages creation and swapping of RenderFrameHosts for this frame.
686 //
687 // This field needs to be declared last, because destruction of
688 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
689 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
690 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
691 // may try to use FrameTreeNode's fields above - this would be an undefined
692 // behavior if the fields (even trivially-destructible ones) were destructed
693 // before the RenderFrameHostManager's destructor runs. See also
694 // https://crbug.com/1157988.
695 RenderFrameHostManager render_manager_;
696
danakjc492bf82020-09-09 20:02:44697 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
698};
699
700} // namespace content
701
702#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_