blob: f4aee2f93fb8f666d1e21b8b11f1627c6eda3865 [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"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
danakjc492bf82020-09-09 20:02:4416#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"
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:5523#include "content/public/browser/frame_type.h"
danakjc492bf82020-09-09 20:02:4424#include "services/network/public/mojom/content_security_policy.mojom-forward.h"
Lei Zhang698df03c2021-05-21 04:23:3425#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin McNee43fe8292021-10-04 22:59:4126#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4427#include "third_party/blink/public/common/frame/frame_policy.h"
28#include "third_party/blink/public/common/frame/user_activation_state.h"
danakjc492bf82020-09-09 20:02:4429#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
Gyuyoung Kimc16e52e92021-03-19 02:45:3730#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom-forward.h"
Daniel Cheng6ac128172021-05-25 18:49:0131#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
danakjc492bf82020-09-09 20:02:4432#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
33#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-forward.h"
34
35#include "url/gurl.h"
36#include "url/origin.h"
37
38namespace content {
39
40class NavigationRequest;
41class RenderFrameHostImpl;
42class NavigationEntryImpl;
43
44// When a page contains iframes, its renderer process maintains a tree structure
45// of those frames. We are mirroring this tree in the browser process. This
46// class represents a node in this tree and is a wrapper for all objects that
47// are frame-specific (as opposed to page-specific).
48//
49// Each FrameTreeNode has a current RenderFrameHost, which can change over
50// time as the frame is navigated. Any immediate subframes of the current
51// document are tracked using FrameTreeNodes owned by the current
52// RenderFrameHost, rather than as children of FrameTreeNode itself. This
53// allows subframe FrameTreeNodes to stay alive while a RenderFrameHost is
54// still alive - for example while pending deletion, after a new current
55// RenderFrameHost has replaced it.
56class CONTENT_EXPORT FrameTreeNode {
57 public:
58 class Observer {
59 public:
60 // Invoked when a FrameTreeNode is being destroyed.
61 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode* node) {}
62
63 // Invoked when a FrameTreeNode becomes focused.
64 virtual void OnFrameTreeNodeFocused(FrameTreeNode* node) {}
65
Fergal Dalya1d569972021-03-16 03:24:5366 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4467 };
68
69 static const int kFrameTreeNodeInvalidId;
70
71 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
72 // regardless of which FrameTree it is in.
73 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
74
75 // Returns the FrameTreeNode for the given |rfh|. Same as
76 // rfh->frame_tree_node(), but also supports nullptrs.
77 static FrameTreeNode* From(RenderFrameHost* rfh);
78
79 // Callers are are expected to initialize sandbox flags separately after
80 // calling the constructor.
81 FrameTreeNode(
82 FrameTree* frame_tree,
83 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0184 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4485 const std::string& name,
86 const std::string& unique_name,
87 bool is_created_by_script,
88 const base::UnguessableToken& devtools_frame_token,
89 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4190 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3491 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4492
Peter Boström828b9022021-09-21 02:28:4393 FrameTreeNode(const FrameTreeNode&) = delete;
94 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
95
danakjc492bf82020-09-09 20:02:4496 ~FrameTreeNode();
97
98 void AddObserver(Observer* observer);
99 void RemoveObserver(Observer* observer);
100
101 bool IsMainFrame() const;
102
arthursonzogni76098e52020-11-25 14:18:45103 // Clears any state in this node which was set by the document itself (CSP &
104 // UserActivationState) and notifies proxies as appropriate. Invoked after
105 // committing navigation to a new document (since the new document comes with
106 // a fresh set of CSP).
107 // TODO(arthursonzogni): Remove this function. The frame/document must not be
108 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13109 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44110
111 FrameTree* frame_tree() const { return frame_tree_; }
112 Navigator& navigator() { return frame_tree()->navigator(); }
113
114 RenderFrameHostManager* render_manager() { return &render_manager_; }
115 int frame_tree_node_id() const { return frame_tree_node_id_; }
Harkiran Bolaria4eacb3a2021-12-13 20:03:47116 const std::string& frame_name() const {
117 return render_manager_.current_replication_state().name;
118 }
danakjc492bf82020-09-09 20:02:44119
120 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47121 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44122 }
123
124 // See comment on the member declaration.
125 const base::UnguessableToken& devtools_frame_token() const {
126 return devtools_frame_token_;
127 }
128
129 size_t child_count() const { return current_frame_host()->child_count(); }
130
danakjc492bf82020-09-09 20:02:44131 RenderFrameHostImpl* parent() const { return parent_; }
132
Dave Tapuskac8de3b02021-12-03 21:51:01133 // See `RenderFrameHost::GetParentOrOuterDocument()` for
134 // documentation.
135 RenderFrameHostImpl* GetParentOrOuterDocument();
136
137 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
138 // documentation.
139 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
140
danakjc492bf82020-09-09 20:02:44141 FrameTreeNode* opener() const { return opener_; }
142
143 FrameTreeNode* original_opener() const { return original_opener_; }
144
Anton Bikineevf62d1bf2021-05-15 17:56:07145 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39146 return opener_devtools_frame_token_;
147 }
148
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55149 // Returns the type of the frame. Refer to frame_type.h for the details.
150 FrameType GetFrameType() const;
151
danakjc492bf82020-09-09 20:02:44152 // Assigns a new opener for this node and, if |opener| is non-null, registers
153 // an observer that will clear this node's opener if |opener| is ever
154 // destroyed.
155 void SetOpener(FrameTreeNode* opener);
156
157 // Assigns the initial opener for this node, and if |opener| is non-null,
158 // registers an observer that will clear this node's opener if |opener| is
159 // ever destroyed. The value set here is the root of the tree.
160 //
161 // It is not possible to change the opener once it was set.
162 void SetOriginalOpener(FrameTreeNode* opener);
163
Wolfgang Beyerd8809db2020-09-30 15:29:39164 // Assigns an opener frame id for this node. This string id is only set once
165 // and cannot be changed. It persists, even if the |opener| is destroyed. It
166 // is used for attribution in the DevTools frontend.
167 void SetOpenerDevtoolsFrameToken(
168 base::UnguessableToken opener_devtools_frame_token);
169
danakjc492bf82020-09-09 20:02:44170 FrameTreeNode* child_at(size_t index) const {
171 return current_frame_host()->child_at(index);
172 }
173
174 // Returns the URL of the last committed page in the current frame.
175 const GURL& current_url() const {
176 return current_frame_host()->GetLastCommittedURL();
177 }
178
Rakina Zata Amni86c88fa2021-11-01 01:27:30179 // Sets the last committed URL for this frame.
danakjc492bf82020-09-09 20:02:44180 void SetCurrentURL(const GURL& url);
181
Rakina Zata Amni90555282022-01-21 07:35:54182 // Sets `is_on_initial_empty_document_` to false.
183 void SetNotOnInitialEmptyDocument() { is_on_initial_empty_document_ = false; }
Rakina Zata Amni86c88fa2021-11-01 01:27:30184
Rakina Zata Amni91d485b42021-12-08 02:50:13185 // Returns false if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30186 // empty document, or if the current document's input stream has been opened
187 // with document.open(), causing the document to lose its "initial empty
188 // document" status. For more details, see the definition of
189 // `is_on_initial_empty_document_`.
190 bool is_on_initial_empty_document() const {
191 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56192 }
193
Rakina Zata Amni86c88fa2021-11-01 01:27:30194 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56195 // false. Must only be called after the current document's input stream has
196 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30197 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14198
danakjc492bf82020-09-09 20:02:44199 // Returns whether the frame's owner element in the parent document is
200 // collapsed, that is, removed from the layout as if it did not exist, as per
201 // request by the embedder (of the content/ layer).
202 bool is_collapsed() const { return is_collapsed_; }
203
204 // Sets whether to collapse the frame's owner element in the parent document,
205 // that is, to remove it from the layout as if it did not exist, as per
206 // request by the embedder (of the content/ layer). Cannot be called for main
207 // frames.
208 //
209 // This only has an effect for <iframe> owner elements, and is a no-op when
210 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
211 void SetCollapsed(bool collapsed);
212
213 // Returns the origin of the last committed page in this frame.
214 // WARNING: To get the last committed origin for a particular
215 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
216 // which will behave correctly even when the RenderFrameHost is not the
217 // current one for this frame (such as when it's pending deletion).
218 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47219 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44220 }
221
danakjc492bf82020-09-09 20:02:44222 // Set the current name and notify proxies about the update.
223 void SetFrameName(const std::string& name, const std::string& unique_name);
224
danakjc492bf82020-09-09 20:02:44225 // Returns the latest frame policy (sandbox flags and container policy) for
226 // this frame. This includes flags inherited from parent frames and the latest
227 // flags from the <iframe> element hosting this frame. The returned policies
228 // may not yet have taken effect, since "sandbox" and "allow" attribute
229 // updates in an <iframe> element take effect on next navigation. To retrieve
230 // the currently active policy for this frame, use effective_frame_policy().
231 const blink::FramePolicy& pending_frame_policy() const {
232 return pending_frame_policy_;
233 }
234
235 // Update this frame's sandbox flags and container policy. This is called
236 // when a parent frame updates the "sandbox" attribute in the <iframe> element
237 // for this frame, or any of the attributes which affect the container policy
238 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
239 // These policies won't take effect until next navigation. If this frame's
240 // parent is itself sandboxed, the parent's sandbox flags are combined with
241 // those in |frame_policy|.
242 // Attempting to change the container policy on the main frame will have no
243 // effect.
244 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
245
246 // Returns the currently active frame policy for this frame, including the
247 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39248 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44249 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
250 // origin of the iframe's src attribute (which may be different from the URL
251 // of the document currently loaded into the frame). This does not include
252 // policy changes that have been made by updating the containing iframe
253 // element attributes since the frame was last navigated; use
254 // pending_frame_policy() for those.
255 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47256 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44257 }
258
danakjc492bf82020-09-09 20:02:44259 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
260 return frame_owner_properties_;
261 }
262
263 void set_frame_owner_properties(
264 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
265 frame_owner_properties_ = frame_owner_properties;
266 }
267
268 const network::mojom::ContentSecurityPolicy* csp_attribute() {
269 return csp_attribute_.get();
270 }
271
272 void set_csp_attribute(
273 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
274 csp_attribute_ = std::move(parsed_csp_attribute);
275 }
276
Antonio Sartori5abc8de2021-07-13 08:42:47277 // Reflects the 'anonymous' attribute of the corresponding iframe html
278 // element.
279 bool anonymous() const { return anonymous_; }
280 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
281
danakjc492bf82020-09-09 20:02:44282 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47283 return render_manager_.current_replication_state().origin.IsSameOriginWith(
284 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44285 }
286
Gyuyoung Kimc16e52e92021-03-19 02:45:37287 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47288 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44289 }
290
291 RenderFrameHostImpl* current_frame_host() const {
292 return render_manager_.current_frame_host();
293 }
294
danakjc492bf82020-09-09 20:02:44295 // Returns true if this node is in a loading state.
296 bool IsLoading() const;
297
Alex Moshchuk9b0fd822020-10-26 23:08:15298 // Returns true if this node has a cross-document navigation in progress.
299 bool HasPendingCrossDocumentNavigation() const;
300
danakjc492bf82020-09-09 20:02:44301 NavigationRequest* navigation_request() { return navigation_request_.get(); }
302
303 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
304 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
305 // RenderFrameHost that is committing the navigation.
306 void TransferNavigationRequestOwnership(
307 RenderFrameHostImpl* render_frame_host);
308
309 // Takes ownership of |navigation_request| and makes it the current
310 // NavigationRequest of this frame. This corresponds to the start of a new
311 // navigation. If there was an ongoing navigation request before calling this
312 // function, it is canceled. |navigation_request| should not be null.
313 void CreatedNavigationRequest(
314 std::unique_ptr<NavigationRequest> navigation_request);
315
316 // Resets the current navigation request. If |keep_state| is true, any state
317 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
318 // loading state) will not be reset by the function.
319 void ResetNavigationRequest(bool keep_state);
320
321 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19322 // |should_show_loading_ui| indicates whether this navigation should be
323 // visible in the UI. True for cross-document navigations and navigations
324 // intercepted by appHistory's transitionWhile().
danakjc492bf82020-09-09 20:02:44325 // |was_previously_loading| is false if the FrameTree was not loading before.
326 // The caller is required to provide this boolean as the delegate should only
327 // be notified if the FrameTree went from non-loading to loading state.
328 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19329 void DidStartLoading(bool should_show_loading_ui,
330 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44331
332 // A RenderFrameHost in this node stopped loading.
333 void DidStopLoading();
334
335 // The load progress for a RenderFrameHost in this node was updated to
336 // |load_progress|. This will notify the FrameTree which will in turn notify
337 // the WebContents.
338 void DidChangeLoadProgress(double load_progress);
339
340 // Called when the user directed the page to stop loading. Stops all loads
341 // happening in the FrameTreeNode. This method should be used with
342 // FrameTree::ForEach to stop all loads in the entire FrameTree.
343 bool StopLoading();
344
345 // Returns the time this frame was last focused.
346 base::TimeTicks last_focus_time() const { return last_focus_time_; }
347
348 // Called when this node becomes focused. Updates the node's last focused
349 // time and notifies observers.
350 void DidFocus();
351
352 // Called when the user closed the modal dialogue for BeforeUnload and
353 // cancelled the navigation. This should stop any load happening in the
354 // FrameTreeNode.
355 void BeforeUnloadCanceled();
356
357 // Returns the BlameContext associated with this node.
358 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
359
360 // Updates the user activation state in the browser frame tree and in the
361 // frame trees in all renderer processes except the renderer for this node
362 // (which initiated the update). Returns |false| if the update tries to
363 // consume an already consumed/expired transient state, |true| otherwise. See
364 // the comment on user_activation_state_ below.
365 //
366 // The |notification_type| parameter is used for histograms, only for the case
367 // |update_state == kNotifyActivation|.
368 bool UpdateUserActivationState(
369 blink::mojom::UserActivationUpdateType update_type,
370 blink::mojom::UserActivationNotificationType notification_type);
371
danakjc492bf82020-09-09 20:02:44372 // Returns the sandbox flags currently in effect for this frame. This includes
373 // flags inherited from parent frames, the currently active flags from the
374 // <iframe> element hosting this frame, as well as any flags set from a
375 // Content-Security-Policy HTTP header. This does not include flags that have
376 // have been updated in an <iframe> element but have not taken effect yet; use
377 // pending_frame_policy() for those. To see the flags which will take effect
378 // on navigation (which does not include the CSP-set flags), use
379 // effective_frame_policy().
380 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47381 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44382 }
383
danakjc492bf82020-09-09 20:02:44384 // Returns whether the frame received a user gesture on a previous navigation
385 // on the same eTLD+1.
386 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47387 return render_manager_.current_replication_state()
388 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44389 }
390
391 // When a tab is discarded, WebContents sets was_discarded on its
392 // root FrameTreeNode.
393 // In addition, when a child frame is created, this bit is passed on from
394 // parent to child.
395 // When a navigation request is created, was_discarded is passed on to the
396 // request and reset to false in FrameTreeNode.
397 void set_was_discarded() { was_discarded_ = true; }
398 bool was_discarded() const { return was_discarded_; }
399
400 // Returns the sticky bit of the User Activation v2 state of the
401 // |FrameTreeNode|.
402 bool HasStickyUserActivation() const {
403 return user_activation_state_.HasBeenActive();
404 }
405
406 // Returns the transient bit of the User Activation v2 state of the
407 // |FrameTreeNode|.
408 bool HasTransientUserActivation() {
409 return user_activation_state_.IsActive();
410 }
411
412 // Remove history entries for all frames created by script in this frame's
413 // subtree. If a frame created by a script is removed, then its history entry
414 // will never be reused - this saves memory.
415 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
416
Kevin McNee43fe8292021-10-04 22:59:41417 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45418 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44419 }
danakjc492bf82020-09-09 20:02:44420
Daniel Cheng6ac128172021-05-25 18:49:01421 blink::mojom::TreeScopeType tree_scope_type() const {
422 return tree_scope_type_;
423 }
424
arthursonzogni034bb9c2020-10-01 08:29:56425 // The initial popup URL for new window opened using:
426 // `window.open(initial_popup_url)`.
427 // An empty GURL otherwise.
428 //
429 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
430 // document served from this URL. The FrameTreeNode always starts hosting the
431 // initial empty document and attempts a navigation toward this URL. However
432 // the navigation might be delayed, redirected and even cancelled.
433 void SetInitialPopupURL(const GURL& initial_popup_url);
434 const GURL& initial_popup_url() const { return initial_popup_url_; }
435
436 // The origin of the document that used window.open() to create this frame.
437 // Otherwise, an opaque Origin with a nonce different from all previously
438 // existing Origins.
439 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
440 const url::Origin& popup_creator_origin() const {
441 return popup_creator_origin_;
442 }
443
Harkiran Bolaria59290d62021-03-17 01:53:01444 // Sets the associated FrameTree for this node. The node can change FrameTrees
445 // when blink::features::Prerender2 is enabled, which allows a page loaded in
446 // the prerendered FrameTree to be used for a navigation in the primary frame
447 // tree.
448 void SetFrameTree(FrameTree& frame_tree);
449
Alexander Timinf785f342021-03-18 00:00:56450 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24451 void WriteIntoTrace(perfetto::TracedValue context) const;
Rakina Zata Amni4b1968d2021-09-09 03:29:47452 void WriteIntoTrace(
453 perfetto::TracedProto<perfetto::protos::pbzero::FrameTreeNodeInfo> proto);
Alexander Timinf785f342021-03-18 00:00:56454
Carlos Caballero76711352021-03-24 17:38:21455 // Returns true the node is navigating, i.e. it has an associated
456 // NavigationRequest.
457 bool HasNavigation();
458
shivanigithubf3ddff52021-07-03 22:06:30459 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30460 // Note that these two functions cannot be invoked from a FrameTree's or
461 // its root node's constructor since they require the frame tree and the
462 // root node to be completely constructed.
463 //
shivanigithubf3ddff52021-07-03 22:06:30464 // Returns false if fenced frames are disabled. Returns true if the feature is
465 // enabled and if |this| is a fenced frame. Returns false for
466 // iframes embedded in a fenced frame. To clarify: for the MPArch
467 // implementation this only returns true if |this| is the actual
468 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
469 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36470 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30471
472 // Returns false if fenced frames are disabled. Returns true if the
473 // feature is enabled and if |this| or any of its ancestor nodes is a
474 // fenced frame.
475 bool IsInFencedFrameTree() const;
476
shivanigithub4cd016a2021-09-20 21:10:30477 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
478 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
479 // details.
480 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
481 return fenced_frame_nonce_;
482 }
483
484 // If applicable, set the fenced frame nonce. See comment on
485 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
486 // by FrameTree::Init() or FrameTree::AddFrame().
487 void SetFencedFrameNonceIfNeeded();
488
Dave Tapuskac8de3b02021-12-03 21:51:01489 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
490 // Do not use directly.
491 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
492
Harkiran Bolariab4437fd2021-08-11 17:51:22493 // Sets the unique_name and name fields on replication_state_. To be used in
494 // prerender activation to make sure the FrameTreeNode replication state is
495 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
496 // renderers should already have the correct value, so unlike
497 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47498 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
499 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22500 void set_frame_name_for_activation(const std::string& unique_name,
501 const std::string& name) {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47502 render_manager_.browsing_context_state()->set_frame_name(unique_name, name);
Harkiran Bolariab4437fd2021-08-11 17:51:22503 }
504
Nan Linaaf84f72021-12-02 22:31:56505 // Returns true if error page isolation is enabled.
506 bool IsErrorPageIsolationEnabled() const;
507
danakjc492bf82020-09-09 20:02:44508 private:
Charlie Hubb5943d2021-03-09 19:46:12509 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44510 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12511 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44512 ContainerPolicySandboxDynamic);
513
Dominic Farolino8a2187b2021-12-24 20:44:21514 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
515 // representing an inner FrameTree, this method destroys said inner FrameTree.
516 void DestroyInnerFrameTreeIfExists();
517
danakjc492bf82020-09-09 20:02:44518 class OpenerDestroyedObserver;
519
danakjc492bf82020-09-09 20:02:44520 // The |notification_type| parameter is used for histograms only.
521 bool NotifyUserActivation(
522 blink::mojom::UserActivationNotificationType notification_type);
523
524 bool ConsumeTransientUserActivation();
525
526 bool ClearUserActivation();
527
528 // Verify that the renderer process is allowed to set user activation on this
529 // frame by checking whether this frame's RenderWidgetHost had previously seen
530 // an input event that might lead to user activation. If user activation
531 // should be allowed, this returns true and also clears corresponding pending
532 // user activation state in the widget. Otherwise, this returns false.
533 bool VerifyUserActivation();
534
535 // The next available browser-global FrameTreeNode ID.
536 static int next_frame_tree_node_id_;
537
538 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52539 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44540
danakjc492bf82020-09-09 20:02:44541 // A browser-global identifier for the frame in the page, which stays stable
542 // even if the frame does a cross-process navigation.
543 const int frame_tree_node_id_;
544
545 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
546 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52547 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44548
danakjc492bf82020-09-09 20:02:44549 // The frame that opened this frame, if any. Will be set to null if the
550 // opener is closed, or if this frame disowns its opener by setting its
551 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52552 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44553
554 // An observer that clears this node's |opener_| if the opener is destroyed.
555 // This observer is added to the |opener_|'s observer list when the |opener_|
556 // is set to a non-null node, and it is removed from that list when |opener_|
557 // changes or when this node is destroyed. It is also cleared if |opener_|
558 // is disowned.
559 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
560
561 // The frame that opened this frame, if any. Contrary to opener_, this
562 // cannot be changed unless the original opener is destroyed.
Keishi Hattori0e45c022021-11-27 09:25:52563 raw_ptr<FrameTreeNode> original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44564
Wolfgang Beyerd8809db2020-09-30 15:29:39565 // The devtools frame token of the frame which opened this frame. This is
566 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07567 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39568
danakjc492bf82020-09-09 20:02:44569 // An observer that clears this node's |original_opener_| if the opener is
570 // destroyed.
571 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
572
arthursonzogni034bb9c2020-10-01 08:29:56573 // When created by an opener, the URL specified in window.open(url)
574 // Please refer to {Get,Set}InitialPopupURL() documentation.
575 GURL initial_popup_url_;
576
577 // When created using window.open, the origin of the creator.
578 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
579 url::Origin popup_creator_origin_;
580
Rakina Zata Amni86c88fa2021-11-01 01:27:30581 // Whether this frame is still on the initial about:blank document or the
582 // synchronously committed about:blank document committed at frame creation,
583 // and its "initial empty document"-ness is still true.
584 // This will be false if either of these has happened:
585 // - SetCurrentUrl() was called after committing a document that is not the
586 // initial about:blank document or the synchronously committed about:blank
587 // document, per
588 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
589 // - The document's input stream has been opened with document.open(), per
590 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
591 // NOTE: we treat both the "initial about:blank document" and the
592 // "synchronously committed about:blank document" as the initial empty
593 // document. In the future, we plan to remove the synchronous about:blank
594 // commit so that this state will only be true if the frame is on the
595 // "initial about:blank document". See also:
596 // - https://github.com/whatwg/html/issues/6863
597 // - https://crbug.com/1215096
598 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56599
danakjc492bf82020-09-09 20:02:44600 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19601 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44602
Daniel Cheng6ac128172021-05-25 18:49:01603 // The type of frame owner for this frame. This is only relevant for non-main
604 // frames.
Kevin McNee43fe8292021-10-04 22:59:41605 const blink::FrameOwnerElementType frame_owner_element_type_ =
606 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45607
Daniel Cheng6ac128172021-05-25 18:49:01608 // The tree scope type of frame owner element, i.e. whether the element is in
609 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
610 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
611 // relevant for non-main frames.
612 const blink::mojom::TreeScopeType tree_scope_type_ =
613 blink::mojom::TreeScopeType::kDocument;
614
danakjc492bf82020-09-09 20:02:44615 // Track the pending sandbox flags and container policy for this frame. When a
616 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
617 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47618 // is stored here, and transferred into
619 // render_manager_.current_replication_state().frame_policy when they take
620 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44621 blink::FramePolicy pending_frame_policy_;
622
623 // Whether the frame was created by javascript. This is useful to prune
624 // history entries when the frame is removed (because frames created by
625 // scripts are never recreated with the same unique name - see
626 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19627 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44628
629 // Used for devtools instrumentation and trace-ability. The token is
630 // propagated to Blink's LocalFrame and both Blink and content/
631 // can tag calls and requests with this token in order to attribute them
632 // to the context frame.
633 // |devtools_frame_token_| is only defined by the browser process and is never
634 // sent back from the renderer in the control calls. It should be never used
635 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19636 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44637
638 // Tracks the scrolling and margin properties for this frame. These
639 // properties affect the child renderer but are stored on its parent's
640 // frame element. When this frame's parent dynamically updates these
641 // properties, we update them here too.
642 //
643 // Note that dynamic updates only take effect on the next frame navigation.
644 blink::mojom::FrameOwnerProperties frame_owner_properties_;
645
646 // Contains the current parsed value of the 'csp' attribute of this frame.
647 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
648
Antonio Sartori5abc8de2021-07-13 08:42:47649 // Reflects the 'anonymous' attribute of the corresponding iframe html
650 // element.
651 bool anonymous_ = false;
652
danakjc492bf82020-09-09 20:02:44653 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
654 // be reset and a RenderFrameHost will be responsible for the navigation.
655 std::unique_ptr<NavigationRequest> navigation_request_;
656
657 // List of objects observing this FrameTreeNode.
658 base::ObserverList<Observer>::Unchecked observers_;
659
660 base::TimeTicks last_focus_time_;
661
arthursonzogni9816b9192021-03-29 16:09:19662 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44663
664 // The user activation state of the current frame. See |UserActivationState|
665 // for details on how this state is maintained.
666 blink::UserActivationState user_activation_state_;
667
668 // A helper for tracing the snapshots of this FrameTreeNode and attributing
669 // browser process activities to this node (when possible). It is unrelated
670 // to the core logic of FrameTreeNode.
671 FrameTreeNodeBlameContext blame_context_;
672
shivanigithub4cd016a2021-09-20 21:10:30673 // Fenced Frames:
674 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
675 // frame and any iframes nested within it. Not set if this frame is not in a
676 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
677 // the MPArch version but for the shadow DOM version we need to keep it here
678 // since the fenced frame root is not a main frame for the latter. The value
679 // of the nonce will be the same for all of the the frames inside a fenced
680 // frame tree. If there is a nested fenced frame it will have a different
681 // nonce than its parent fenced frame. The nonce will stay the same across
682 // navigations because it is always used in conjunction with other fields of
683 // the keys. If the navigation is same-origin/site then the same network stack
684 // partition/storage will be reused and if it's cross-origin/site then other
685 // parts of the key will change and so, even with the same nonce, another
686 // partition will be used.
687 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
688
Lukasz Anforowicz147141962020-12-16 18:03:24689 // Manages creation and swapping of RenderFrameHosts for this frame.
690 //
691 // This field needs to be declared last, because destruction of
692 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
693 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
694 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
695 // may try to use FrameTreeNode's fields above - this would be an undefined
696 // behavior if the fields (even trivially-destructible ones) were destructed
697 // before the RenderFrameHostManager's destructor runs. See also
698 // https://crbug.com/1157988.
699 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44700};
701
702} // namespace content
703
704#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_