blob: 004970affcdfaf5174060c0a5c7e80145ad05316 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
danakjc492bf82020-09-09 20:02:442// 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>
David Sanders2c1194d92022-04-19 23:32:3212#include <utility>
danakjc492bf82020-09-09 20:02:4413
14#include "base/gtest_prod_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
David Sanders2c1194d92022-04-19 23:32:3216#include "base/memory/scoped_refptr.h"
David Sandersd4bf5eb2022-03-17 07:12:0517#include "base/observer_list.h"
danakjc492bf82020-09-09 20:02:4418#include "content/browser/renderer_host/frame_tree.h"
danakjc492bf82020-09-09 20:02:4419#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"
David Sanders2c1194d92022-04-19 23:32:3232#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom-forward.h"
danakjc492bf82020-09-09 20:02:4433
Gabriel Charetted87f10f2022-03-31 00:44:2234#include "base/time/time.h"
danakjc492bf82020-09-09 20:02:4435#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
Arthur Hemerye4659282022-03-28 08:36:1566 // Invoked when a FrameTreeNode moves to a different BrowsingInstance and
67 // the popups it opened should be disowned.
68 virtual void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) {}
69
Fergal Dalya1d569972021-03-16 03:24:5370 virtual ~Observer() = default;
danakjc492bf82020-09-09 20:02:4471 };
72
73 static const int kFrameTreeNodeInvalidId;
74
75 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
76 // regardless of which FrameTree it is in.
77 static FrameTreeNode* GloballyFindByID(int frame_tree_node_id);
78
79 // Returns the FrameTreeNode for the given |rfh|. Same as
80 // rfh->frame_tree_node(), but also supports nullptrs.
81 static FrameTreeNode* From(RenderFrameHost* rfh);
82
83 // Callers are are expected to initialize sandbox flags separately after
84 // calling the constructor.
85 FrameTreeNode(
86 FrameTree* frame_tree,
87 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:0188 blink::mojom::TreeScopeType tree_scope_type,
danakjc492bf82020-09-09 20:02:4489 bool is_created_by_script,
90 const base::UnguessableToken& devtools_frame_token,
91 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:4192 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3493 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4494
Peter Boström828b9022021-09-21 02:28:4395 FrameTreeNode(const FrameTreeNode&) = delete;
96 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
97
danakjc492bf82020-09-09 20:02:4498 ~FrameTreeNode();
99
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
102
Ian Vollick25a9d032022-04-12 23:20:17103 // Frame trees may be nested so it can be the case that IsMainFrame() is true,
104 // but is not the outermost main frame. In particular, !IsMainFrame() cannot
105 // be used to check if the frame is an embedded frame -- use
106 // !IsOutermostMainFrame() instead. NB: this does not escape guest views;
107 // IsOutermostMainFrame will be true for the outermost main frame in an inner
108 // guest view.
danakjc492bf82020-09-09 20:02:44109 bool IsMainFrame() const;
Ian Vollick25a9d032022-04-12 23:20:17110 bool IsOutermostMainFrame();
danakjc492bf82020-09-09 20:02:44111
arthursonzogni76098e52020-11-25 14:18:45112 // Clears any state in this node which was set by the document itself (CSP &
113 // UserActivationState) and notifies proxies as appropriate. Invoked after
114 // committing navigation to a new document (since the new document comes with
115 // a fresh set of CSP).
116 // TODO(arthursonzogni): Remove this function. The frame/document must not be
117 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13118 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44119
120 FrameTree* frame_tree() const { return frame_tree_; }
121 Navigator& navigator() { return frame_tree()->navigator(); }
122
123 RenderFrameHostManager* render_manager() { return &render_manager_; }
Alexander Timin33e2e2c12022-03-03 04:21:33124 const RenderFrameHostManager* render_manager() const {
125 return &render_manager_;
126 }
danakjc492bf82020-09-09 20:02:44127 int frame_tree_node_id() const { return frame_tree_node_id_; }
Yuzu Saijo03dbf9b2022-07-22 04:29:45128 // This reflects window.name, which is initially set to the the "name"
129 // attribute. But this won't reflect changes of 'name' attribute and instead
130 // reflect changes to the Window object's name property.
131 // This is different from IframeAttributes' name in that this will not get
132 // updated when 'name' attribute gets updated.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47133 const std::string& frame_name() const {
134 return render_manager_.current_replication_state().name;
135 }
danakjc492bf82020-09-09 20:02:44136
137 const std::string& unique_name() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47138 return render_manager_.current_replication_state().unique_name;
danakjc492bf82020-09-09 20:02:44139 }
140
141 // See comment on the member declaration.
142 const base::UnguessableToken& devtools_frame_token() const {
143 return devtools_frame_token_;
144 }
145
Andrey Kosyakov6e82c3b2022-09-26 22:43:46146 // This may only change when an RFH changes host frame tree nodes
147 // during prerender activation.
148 // TODO(caseq,dsv): see if we can get rid of it.
149 void set_devtools_frame_token(const base::UnguessableToken& token) {
150 devtools_frame_token_ = token;
151 }
danakjc492bf82020-09-09 20:02:44152 size_t child_count() const { return current_frame_host()->child_count(); }
153
danakjc492bf82020-09-09 20:02:44154 RenderFrameHostImpl* parent() const { return parent_; }
155
Dave Tapuskac8de3b02021-12-03 21:51:01156 // See `RenderFrameHost::GetParentOrOuterDocument()` for
157 // documentation.
158 RenderFrameHostImpl* GetParentOrOuterDocument();
159
160 // See `RenderFrameHostImpl::GetParentOrOuterDocumentOrEmbedder()` for
161 // documentation.
162 RenderFrameHostImpl* GetParentOrOuterDocumentOrEmbedder();
163
danakjc492bf82020-09-09 20:02:44164 FrameTreeNode* opener() const { return opener_; }
165
Rakina Zata Amni3a48ae42022-05-05 03:39:56166 FrameTreeNode* first_live_main_frame_in_original_opener_chain() const {
167 return first_live_main_frame_in_original_opener_chain_;
168 }
danakjc492bf82020-09-09 20:02:44169
Anton Bikineevf62d1bf2021-05-15 17:56:07170 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39171 return opener_devtools_frame_token_;
172 }
173
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55174 // Returns the type of the frame. Refer to frame_type.h for the details.
175 FrameType GetFrameType() const;
176
danakjc492bf82020-09-09 20:02:44177 // Assigns a new opener for this node and, if |opener| is non-null, registers
178 // an observer that will clear this node's opener if |opener| is ever
179 // destroyed.
180 void SetOpener(FrameTreeNode* opener);
181
182 // Assigns the initial opener for this node, and if |opener| is non-null,
183 // registers an observer that will clear this node's opener if |opener| is
184 // ever destroyed. The value set here is the root of the tree.
185 //
186 // It is not possible to change the opener once it was set.
187 void SetOriginalOpener(FrameTreeNode* opener);
188
Wolfgang Beyerd8809db2020-09-30 15:29:39189 // Assigns an opener frame id for this node. This string id is only set once
190 // and cannot be changed. It persists, even if the |opener| is destroyed. It
191 // is used for attribution in the DevTools frontend.
192 void SetOpenerDevtoolsFrameToken(
193 base::UnguessableToken opener_devtools_frame_token);
194
danakjc492bf82020-09-09 20:02:44195 FrameTreeNode* child_at(size_t index) const {
196 return current_frame_host()->child_at(index);
197 }
198
199 // Returns the URL of the last committed page in the current frame.
200 const GURL& current_url() const {
201 return current_frame_host()->GetLastCommittedURL();
202 }
203
Rakina Zata Amni90555282022-01-21 07:35:54204 // Sets `is_on_initial_empty_document_` to false.
205 void SetNotOnInitialEmptyDocument() { is_on_initial_empty_document_ = false; }
Rakina Zata Amni86c88fa2021-11-01 01:27:30206
Rakina Zata Amni91d485b42021-12-08 02:50:13207 // Returns false if the frame has committed a document that is not the initial
Rakina Zata Amni86c88fa2021-11-01 01:27:30208 // empty document, or if the current document's input stream has been opened
209 // with document.open(), causing the document to lose its "initial empty
210 // document" status. For more details, see the definition of
211 // `is_on_initial_empty_document_`.
212 bool is_on_initial_empty_document() const {
213 return is_on_initial_empty_document_;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56214 }
215
Rakina Zata Amni86c88fa2021-11-01 01:27:30216 // Sets `is_on_initial_empty_document_` to
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56217 // false. Must only be called after the current document's input stream has
218 // been opened with document.open().
Rakina Zata Amni86c88fa2021-11-01 01:27:30219 void DidOpenDocumentInputStream() { is_on_initial_empty_document_ = false; }
Rakina Zata Amnid09b6112021-06-05 06:20:14220
danakjc492bf82020-09-09 20:02:44221 // Returns whether the frame's owner element in the parent document is
222 // collapsed, that is, removed from the layout as if it did not exist, as per
223 // request by the embedder (of the content/ layer).
224 bool is_collapsed() const { return is_collapsed_; }
225
226 // Sets whether to collapse the frame's owner element in the parent document,
227 // that is, to remove it from the layout as if it did not exist, as per
228 // request by the embedder (of the content/ layer). Cannot be called for main
229 // frames.
230 //
231 // This only has an effect for <iframe> owner elements, and is a no-op when
232 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
233 void SetCollapsed(bool collapsed);
234
235 // Returns the origin of the last committed page in this frame.
236 // WARNING: To get the last committed origin for a particular
237 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
238 // which will behave correctly even when the RenderFrameHost is not the
239 // current one for this frame (such as when it's pending deletion).
240 const url::Origin& current_origin() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47241 return render_manager_.current_replication_state().origin;
danakjc492bf82020-09-09 20:02:44242 }
243
danakjc492bf82020-09-09 20:02:44244 // Returns the latest frame policy (sandbox flags and container policy) for
245 // this frame. This includes flags inherited from parent frames and the latest
246 // flags from the <iframe> element hosting this frame. The returned policies
247 // may not yet have taken effect, since "sandbox" and "allow" attribute
248 // updates in an <iframe> element take effect on next navigation. To retrieve
249 // the currently active policy for this frame, use effective_frame_policy().
250 const blink::FramePolicy& pending_frame_policy() const {
251 return pending_frame_policy_;
252 }
253
254 // Update this frame's sandbox flags and container policy. This is called
255 // when a parent frame updates the "sandbox" attribute in the <iframe> element
256 // for this frame, or any of the attributes which affect the container policy
257 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
258 // These policies won't take effect until next navigation. If this frame's
259 // parent is itself sandboxed, the parent's sandbox flags are combined with
260 // those in |frame_policy|.
261 // Attempting to change the container policy on the main frame will have no
262 // effect.
263 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
264
265 // Returns the currently active frame policy for this frame, including the
266 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39267 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44268 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
269 // origin of the iframe's src attribute (which may be different from the URL
270 // of the document currently loaded into the frame). This does not include
271 // policy changes that have been made by updating the containing iframe
272 // element attributes since the frame was last navigated; use
273 // pending_frame_policy() for those.
274 const blink::FramePolicy& effective_frame_policy() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47275 return render_manager_.current_replication_state().frame_policy;
danakjc492bf82020-09-09 20:02:44276 }
277
danakjc492bf82020-09-09 20:02:44278 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
279 return frame_owner_properties_;
280 }
281
282 void set_frame_owner_properties(
283 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
284 frame_owner_properties_ = frame_owner_properties;
285 }
286
Yuzu Saijo03dbf9b2022-07-22 04:29:45287 // Reflects the attributes of the corresponding iframe html element, such
288 // as 'anonymous', 'id', 'name' and 'src'. These values should not be
289 // exposed to cross-origin renderers.
290 const network::mojom::ContentSecurityPolicy* csp_attribute() const {
291 return attributes_->parsed_csp_attribute.get();
danakjc492bf82020-09-09 20:02:44292 }
Yuzu Saijo03dbf9b2022-07-22 04:29:45293 bool anonymous() const { return attributes_->anonymous; }
294 const std::string& html_id() const { return attributes_->id; }
295 // This tracks iframe's 'name' attribute instead of window.name, which is
296 // tracked in FrameReplicationState. See the comment for frame_name() for
297 // more details.
298 const std::string& html_name() const { return attributes_->name; }
299 const std::string& html_src() const { return attributes_->src; }
danakjc492bf82020-09-09 20:02:44300
Yuzu Saijo03dbf9b2022-07-22 04:29:45301 void SetAttributes(blink::mojom::IframeAttributesPtr attributes);
Antonio Sartori5abc8de2021-07-13 08:42:47302
danakjc492bf82020-09-09 20:02:44303 bool HasSameOrigin(const FrameTreeNode& node) const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47304 return render_manager_.current_replication_state().origin.IsSameOriginWith(
305 node.current_replication_state().origin);
danakjc492bf82020-09-09 20:02:44306 }
307
Gyuyoung Kimc16e52e92021-03-19 02:45:37308 const blink::mojom::FrameReplicationState& current_replication_state() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47309 return render_manager_.current_replication_state();
danakjc492bf82020-09-09 20:02:44310 }
311
312 RenderFrameHostImpl* current_frame_host() const {
313 return render_manager_.current_frame_host();
314 }
315
danakjc492bf82020-09-09 20:02:44316 // Returns true if this node is in a loading state.
317 bool IsLoading() const;
318
Alex Moshchuk9b0fd822020-10-26 23:08:15319 // Returns true if this node has a cross-document navigation in progress.
320 bool HasPendingCrossDocumentNavigation() const;
321
danakjc492bf82020-09-09 20:02:44322 NavigationRequest* navigation_request() { return navigation_request_.get(); }
323
324 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
325 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
326 // RenderFrameHost that is committing the navigation.
327 void TransferNavigationRequestOwnership(
328 RenderFrameHostImpl* render_frame_host);
329
330 // Takes ownership of |navigation_request| and makes it the current
331 // NavigationRequest of this frame. This corresponds to the start of a new
332 // navigation. If there was an ongoing navigation request before calling this
333 // function, it is canceled. |navigation_request| should not be null.
334 void CreatedNavigationRequest(
335 std::unique_ptr<NavigationRequest> navigation_request);
336
337 // Resets the current navigation request. If |keep_state| is true, any state
338 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
339 // loading state) will not be reset by the function.
340 void ResetNavigationRequest(bool keep_state);
341
342 // A RenderFrameHost in this node started loading.
Nate Chapin9aabf5f2021-11-12 00:31:19343 // |should_show_loading_ui| indicates whether this navigation should be
344 // visible in the UI. True for cross-document navigations and navigations
Nate Chapin24295252022-09-27 18:31:08345 // intercepted by the navigation API's intercept().
danakjc492bf82020-09-09 20:02:44346 // |was_previously_loading| is false if the FrameTree was not loading before.
347 // The caller is required to provide this boolean as the delegate should only
348 // be notified if the FrameTree went from non-loading to loading state.
349 // However, when it is called, the FrameTree should be in a loading state.
Nate Chapin9aabf5f2021-11-12 00:31:19350 void DidStartLoading(bool should_show_loading_ui,
351 bool was_previously_loading);
danakjc492bf82020-09-09 20:02:44352
353 // A RenderFrameHost in this node stopped loading.
354 void DidStopLoading();
355
356 // The load progress for a RenderFrameHost in this node was updated to
357 // |load_progress|. This will notify the FrameTree which will in turn notify
358 // the WebContents.
359 void DidChangeLoadProgress(double load_progress);
360
361 // Called when the user directed the page to stop loading. Stops all loads
362 // happening in the FrameTreeNode. This method should be used with
363 // FrameTree::ForEach to stop all loads in the entire FrameTree.
364 bool StopLoading();
365
366 // Returns the time this frame was last focused.
367 base::TimeTicks last_focus_time() const { return last_focus_time_; }
368
369 // Called when this node becomes focused. Updates the node's last focused
370 // time and notifies observers.
371 void DidFocus();
372
373 // Called when the user closed the modal dialogue for BeforeUnload and
374 // cancelled the navigation. This should stop any load happening in the
375 // FrameTreeNode.
376 void BeforeUnloadCanceled();
377
danakjc492bf82020-09-09 20:02:44378 // Updates the user activation state in the browser frame tree and in the
379 // frame trees in all renderer processes except the renderer for this node
380 // (which initiated the update). Returns |false| if the update tries to
381 // consume an already consumed/expired transient state, |true| otherwise. See
382 // the comment on user_activation_state_ below.
383 //
384 // The |notification_type| parameter is used for histograms, only for the case
385 // |update_state == kNotifyActivation|.
386 bool UpdateUserActivationState(
387 blink::mojom::UserActivationUpdateType update_type,
388 blink::mojom::UserActivationNotificationType notification_type);
389
danakjc492bf82020-09-09 20:02:44390 // Returns the sandbox flags currently in effect for this frame. This includes
391 // flags inherited from parent frames, the currently active flags from the
392 // <iframe> element hosting this frame, as well as any flags set from a
393 // Content-Security-Policy HTTP header. This does not include flags that have
394 // have been updated in an <iframe> element but have not taken effect yet; use
395 // pending_frame_policy() for those. To see the flags which will take effect
396 // on navigation (which does not include the CSP-set flags), use
397 // effective_frame_policy().
398 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47399 return render_manager_.current_replication_state().active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44400 }
401
danakjc492bf82020-09-09 20:02:44402 // Returns whether the frame received a user gesture on a previous navigation
403 // on the same eTLD+1.
404 bool has_received_user_gesture_before_nav() const {
Harkiran Bolaria4eacb3a2021-12-13 20:03:47405 return render_manager_.current_replication_state()
406 .has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44407 }
408
409 // When a tab is discarded, WebContents sets was_discarded on its
410 // root FrameTreeNode.
411 // In addition, when a child frame is created, this bit is passed on from
412 // parent to child.
413 // When a navigation request is created, was_discarded is passed on to the
414 // request and reset to false in FrameTreeNode.
415 void set_was_discarded() { was_discarded_ = true; }
416 bool was_discarded() const { return was_discarded_; }
417
418 // Returns the sticky bit of the User Activation v2 state of the
419 // |FrameTreeNode|.
420 bool HasStickyUserActivation() const {
421 return user_activation_state_.HasBeenActive();
422 }
423
424 // Returns the transient bit of the User Activation v2 state of the
425 // |FrameTreeNode|.
426 bool HasTransientUserActivation() {
427 return user_activation_state_.IsActive();
428 }
429
430 // Remove history entries for all frames created by script in this frame's
431 // subtree. If a frame created by a script is removed, then its history entry
432 // will never be reused - this saves memory.
433 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
434
Abhijeet Kandalkarb43affa72022-09-27 16:48:01435 using FencedFrameStatus = RenderFrameHostImpl::FencedFrameStatus;
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58436 FencedFrameStatus fenced_frame_status() const { return fenced_frame_status_; }
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
arthursonzogni034bb9c2020-10-01 08:29:56446 // The initial popup URL for new window opened using:
447 // `window.open(initial_popup_url)`.
448 // An empty GURL otherwise.
449 //
450 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
451 // document served from this URL. The FrameTreeNode always starts hosting the
452 // initial empty document and attempts a navigation toward this URL. However
453 // the navigation might be delayed, redirected and even cancelled.
454 void SetInitialPopupURL(const GURL& initial_popup_url);
455 const GURL& initial_popup_url() const { return initial_popup_url_; }
456
457 // The origin of the document that used window.open() to create this frame.
458 // Otherwise, an opaque Origin with a nonce different from all previously
459 // existing Origins.
460 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
461 const url::Origin& popup_creator_origin() const {
462 return popup_creator_origin_;
463 }
464
Harkiran Bolaria59290d62021-03-17 01:53:01465 // Sets the associated FrameTree for this node. The node can change FrameTrees
466 // when blink::features::Prerender2 is enabled, which allows a page loaded in
467 // the prerendered FrameTree to be used for a navigation in the primary frame
468 // tree.
469 void SetFrameTree(FrameTree& frame_tree);
470
Alexander Timin074cd182022-03-23 18:11:22471 using TraceProto = perfetto::protos::pbzero::FrameTreeNodeInfo;
Alexander Timinf785f342021-03-18 00:00:56472 // Write a representation of this object into a trace.
Alexander Timin074cd182022-03-23 18:11:22473 void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
Alexander Timinf785f342021-03-18 00:00:56474
Carlos Caballero76711352021-03-24 17:38:21475 // Returns true the node is navigating, i.e. it has an associated
476 // NavigationRequest.
477 bool HasNavigation();
478
shivanigithubf3ddff52021-07-03 22:06:30479 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30480 // Note that these two functions cannot be invoked from a FrameTree's or
481 // its root node's constructor since they require the frame tree and the
482 // root node to be completely constructed.
483 //
shivanigithubf3ddff52021-07-03 22:06:30484 // Returns false if fenced frames are disabled. Returns true if the feature is
485 // enabled and if |this| is a fenced frame. Returns false for
486 // iframes embedded in a fenced frame. To clarify: for the MPArch
487 // implementation this only returns true if |this| is the actual
488 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
489 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36490 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30491
492 // Returns false if fenced frames are disabled. Returns true if the
493 // feature is enabled and if |this| or any of its ancestor nodes is a
494 // fenced frame.
495 bool IsInFencedFrameTree() const;
496
shivanigithub4cd016a2021-09-20 21:10:30497 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
498 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
499 // details.
500 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
501 return fenced_frame_nonce_;
502 }
503
504 // If applicable, set the fenced frame nonce. See comment on
505 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
506 // by FrameTree::Init() or FrameTree::AddFrame().
507 void SetFencedFrameNonceIfNeeded();
508
Garrett Tanzera42fdef2022-06-13 16:09:14509 // Returns the mode attribute set on the fenced frame root if this frame is
510 // in a fenced frame tree, otherwise returns `absl::nullopt`.
Nan Line376738a2022-03-25 22:05:41511 absl::optional<blink::mojom::FencedFrameMode> GetFencedFrameMode();
Nan Lin171fe9a2022-02-17 16:42:16512
Dave Tapuskac8de3b02021-12-03 21:51:01513 // Helper for GetParentOrOuterDocument/GetParentOrOuterDocumentOrEmbedder.
514 // Do not use directly.
515 RenderFrameHostImpl* GetParentOrOuterDocumentHelper(bool escape_guest_view);
516
Harkiran Bolariab4437fd2021-08-11 17:51:22517 // Sets the unique_name and name fields on replication_state_. To be used in
518 // prerender activation to make sure the FrameTreeNode replication state is
519 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
520 // renderers should already have the correct value, so unlike
521 // FrameTreeNode::SetFrameName, we do not notify them here.
Harkiran Bolaria4eacb3a2021-12-13 20:03:47522 // TODO(https://crbug.com/1237091): Remove this once the BrowsingContextState
523 // is implemented to utilize the new path.
Harkiran Bolariab4437fd2021-08-11 17:51:22524 void set_frame_name_for_activation(const std::string& unique_name,
525 const std::string& name) {
Harkiran Bolaria0b3bdef02022-03-10 13:04:40526 current_frame_host()->browsing_context_state()->set_frame_name(unique_name,
527 name);
Harkiran Bolariab4437fd2021-08-11 17:51:22528 }
529
Nan Linaaf84f72021-12-02 22:31:56530 // Returns true if error page isolation is enabled.
531 bool IsErrorPageIsolationEnabled() const;
532
W. James MacLean81b8d01f2022-01-25 20:50:59533 // Functions to store and retrieve a frame's srcdoc value on this
534 // FrameTreeNode.
535 void SetSrcdocValue(const std::string& srcdoc_value);
536 const std::string& srcdoc_value() const { return srcdoc_value_; }
537
Garrett Tanzerc69f4642022-08-15 22:15:14538 void set_fenced_frame_properties(
539 absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
540 fenced_frame_properties) {
Garrett Tanzer2975eeac2022-08-22 16:34:01541 // TODO(crbug.com/1262022): Reenable this DCHECK once ShadowDOM and
542 // loading urns in iframes (for FLEDGE OT) are gone.
543 // DCHECK_EQ(fenced_frame_status_,
544 // RenderFrameHostImpl::FencedFrameStatus::kFencedFrameRoot);
Garrett Tanzerc69f4642022-08-15 22:15:14545 fenced_frame_properties_ = fenced_frame_properties;
546 }
547
548 const absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
549 fenced_frame_properties() {
550 return fenced_frame_properties_;
551 }
552
Yao Xiao1ac702d2022-06-08 17:20:49553 // Traverse up from this node. The `shared_storage_budget_metadata()` of the
554 // first seen node with a non-null budget metadata will be returned (i.e. this
555 // node inherits that budget metadata), and this node is expected to be an
556 // outermost fenced frame root. Return nullptr if not found (i.e. this node is
557 // not subjected to shared storage budgeting).
Garrett Tanzer2975eeac2022-08-22 16:34:01558 absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49559 FindSharedStorageBudgetMetadata();
560
Harkiran Bolariaebbe7702022-02-22 19:19:03561 // Accessor to BrowsingContextState for subframes only. Only main frame
562 // navigations can change BrowsingInstances and BrowsingContextStates,
563 // therefore for subframes associated BrowsingContextState never changes. This
564 // helper method makes this more explicit and guards against calling this on
565 // main frames (there an appropriate BrowsingContextState should be obtained
566 // from RenderFrameHost or from RenderFrameProxyHost as e.g. during
567 // cross-BrowsingInstance navigations multiple BrowsingContextStates exist in
568 // the same frame).
569 const scoped_refptr<BrowsingContextState>&
570 GetBrowsingContextStateForSubframe() const;
571
Arthur Hemerye4659282022-03-28 08:36:15572 // Clears the opener property of popups referencing this FrameTreeNode as
573 // their opener.
574 void ClearOpenerReferences();
575
Liam Bradyd2a41e152022-07-19 13:58:48576 // Calculates whether one of the ancestor frames or this frame has a CSPEE
577 // in place. This is eventually sent over to LocalFrame in the renderer where
578 // it will be used by HTMLFencedFrameElement::canLoadOpaqueURL for information
579 // it can't get on its own.
580 bool AncestorOrSelfHasCSPEE() const;
581
danakjc492bf82020-09-09 20:02:44582 private:
Yuzu Saijo03dbf9b2022-07-22 04:29:45583 friend class CSPEmbeddedEnforcementUnitTest;
Charlie Hubb5943d2021-03-09 19:46:12584 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44585 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12586 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44587 ContainerPolicySandboxDynamic);
Yuzu Saijo03dbf9b2022-07-22 04:29:45588 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest, StorageKeyToCommit);
589 FRIEND_TEST_ALL_PREFIXES(NavigationRequestTest,
590 NavigationToAnonymousDocumentNetworkIsolationInfo);
591 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplTest,
592 ChildOfAnonymousIsAnonymous);
Yifan Luo86a79f42022-08-16 18:38:27593 FRIEND_TEST_ALL_PREFIXES(ContentPasswordManagerDriverTest,
594 PasswordAutofillDisabledOnAnonymousIframe);
danakjc492bf82020-09-09 20:02:44595
Dominic Farolino8a2187b2021-12-24 20:44:21596 // Called by the destructor. When `this` is an outer dummy FrameTreeNode
597 // representing an inner FrameTree, this method destroys said inner FrameTree.
598 void DestroyInnerFrameTreeIfExists();
599
danakjc492bf82020-09-09 20:02:44600 class OpenerDestroyedObserver;
601
danakjc492bf82020-09-09 20:02:44602 // The |notification_type| parameter is used for histograms only.
603 bool NotifyUserActivation(
604 blink::mojom::UserActivationNotificationType notification_type);
605
606 bool ConsumeTransientUserActivation();
607
608 bool ClearUserActivation();
609
610 // Verify that the renderer process is allowed to set user activation on this
611 // frame by checking whether this frame's RenderWidgetHost had previously seen
612 // an input event that might lead to user activation. If user activation
613 // should be allowed, this returns true and also clears corresponding pending
614 // user activation state in the widget. Otherwise, this returns false.
615 bool VerifyUserActivation();
616
617 // The next available browser-global FrameTreeNode ID.
618 static int next_frame_tree_node_id_;
619
620 // The FrameTree that owns us.
Keishi Hattori0e45c022021-11-27 09:25:52621 raw_ptr<FrameTree> frame_tree_; // not owned.
danakjc492bf82020-09-09 20:02:44622
danakjc492bf82020-09-09 20:02:44623 // A browser-global identifier for the frame in the page, which stays stable
624 // even if the frame does a cross-process navigation.
625 const int frame_tree_node_id_;
626
627 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
628 // life of this FrameTreeNode. |nullptr| if this node is the root.
Keishi Hattori0e45c022021-11-27 09:25:52629 const raw_ptr<RenderFrameHostImpl> parent_;
danakjc492bf82020-09-09 20:02:44630
danakjc492bf82020-09-09 20:02:44631 // The frame that opened this frame, if any. Will be set to null if the
632 // opener is closed, or if this frame disowns its opener by setting its
633 // window.opener to null.
Keishi Hattori0e45c022021-11-27 09:25:52634 raw_ptr<FrameTreeNode> opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44635
636 // An observer that clears this node's |opener_| if the opener is destroyed.
637 // This observer is added to the |opener_|'s observer list when the |opener_|
638 // is set to a non-null node, and it is removed from that list when |opener_|
639 // changes or when this node is destroyed. It is also cleared if |opener_|
640 // is disowned.
641 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
642
Rakina Zata Amni3a48ae42022-05-05 03:39:56643 // Unlike `opener_`, the "original opener chain" doesn't reflect
644 // window.opener, which can be suppressed or updated. The "original opener"
645 // is the main frame of the actual opener of this frame. This traces the all
646 // the way back, so if the original opener was closed (deleted or severed due
647 // to COOP), but _it_ had an original opener, this will return the original
648 // opener's original opener, etc. So this value will always be set as long as
649 // there is at least one live frame in the chain whose connection is not
650 // severed due to COOP.
651 raw_ptr<FrameTreeNode> first_live_main_frame_in_original_opener_chain_ =
652 nullptr;
danakjc492bf82020-09-09 20:02:44653
Wolfgang Beyerd8809db2020-09-30 15:29:39654 // The devtools frame token of the frame which opened this frame. This is
655 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07656 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39657
Rakina Zata Amni3a48ae42022-05-05 03:39:56658 // An observer that updates this node's
659 // |first_live_main_frame_in_original_opener_chain_| to the next original
660 // opener in the chain if the original opener is destroyed.
danakjc492bf82020-09-09 20:02:44661 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
662
arthursonzogni034bb9c2020-10-01 08:29:56663 // When created by an opener, the URL specified in window.open(url)
664 // Please refer to {Get,Set}InitialPopupURL() documentation.
665 GURL initial_popup_url_;
666
667 // When created using window.open, the origin of the creator.
668 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
669 url::Origin popup_creator_origin_;
670
W. James MacLean81b8d01f2022-01-25 20:50:59671 // If the url from the the last BeginNavigation is about:srcdoc, this value
672 // stores the srcdoc_attribute's value for re-use in history navigations.
673 std::string srcdoc_value_;
674
Rakina Zata Amni86c88fa2021-11-01 01:27:30675 // Whether this frame is still on the initial about:blank document or the
676 // synchronously committed about:blank document committed at frame creation,
677 // and its "initial empty document"-ness is still true.
678 // This will be false if either of these has happened:
Arthur Sonzogni47c79cc2022-08-30 15:25:27679 // - The current RenderFrameHost commits a cross-document navigation that is
680 // not the synchronously committed about:blank document per:
Rakina Zata Amni86c88fa2021-11-01 01:27:30681 // https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts:is-initial-about:blank
682 // - The document's input stream has been opened with document.open(), per
683 // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#opening-the-input-stream:is-initial-about:blank
684 // NOTE: we treat both the "initial about:blank document" and the
685 // "synchronously committed about:blank document" as the initial empty
686 // document. In the future, we plan to remove the synchronous about:blank
687 // commit so that this state will only be true if the frame is on the
688 // "initial about:blank document". See also:
689 // - https://github.com/whatwg/html/issues/6863
690 // - https://crbug.com/1215096
691 bool is_on_initial_empty_document_ = true;
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56692
danakjc492bf82020-09-09 20:02:44693 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19694 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44695
Daniel Cheng6ac128172021-05-25 18:49:01696 // The type of frame owner for this frame. This is only relevant for non-main
697 // frames.
Kevin McNee43fe8292021-10-04 22:59:41698 const blink::FrameOwnerElementType frame_owner_element_type_ =
699 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45700
Daniel Cheng6ac128172021-05-25 18:49:01701 // The tree scope type of frame owner element, i.e. whether the element is in
702 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
703 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
704 // relevant for non-main frames.
705 const blink::mojom::TreeScopeType tree_scope_type_ =
706 blink::mojom::TreeScopeType::kDocument;
707
danakjc492bf82020-09-09 20:02:44708 // Track the pending sandbox flags and container policy for this frame. When a
709 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
710 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Harkiran Bolaria4eacb3a2021-12-13 20:03:47711 // is stored here, and transferred into
712 // render_manager_.current_replication_state().frame_policy when they take
713 // effect on the next frame navigation.
danakjc492bf82020-09-09 20:02:44714 blink::FramePolicy pending_frame_policy_;
715
716 // Whether the frame was created by javascript. This is useful to prune
717 // history entries when the frame is removed (because frames created by
718 // scripts are never recreated with the same unique name - see
719 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19720 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44721
722 // Used for devtools instrumentation and trace-ability. The token is
723 // propagated to Blink's LocalFrame and both Blink and content/
724 // can tag calls and requests with this token in order to attribute them
725 // to the context frame.
726 // |devtools_frame_token_| is only defined by the browser process and is never
727 // sent back from the renderer in the control calls. It should be never used
728 // to look up the FrameTreeNode instance.
Andrey Kosyakov6e82c3b2022-09-26 22:43:46729 base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44730
731 // Tracks the scrolling and margin properties for this frame. These
732 // properties affect the child renderer but are stored on its parent's
733 // frame element. When this frame's parent dynamically updates these
734 // properties, we update them here too.
735 //
736 // Note that dynamic updates only take effect on the next frame navigation.
737 blink::mojom::FrameOwnerProperties frame_owner_properties_;
738
Yuzu Saijo03dbf9b2022-07-22 04:29:45739 // Contains the tracked HTML attributes of the corresponding iframe element,
740 // such as 'id' and 'src'.
741 blink::mojom::IframeAttributesPtr attributes_;
Antonio Sartori5abc8de2021-07-13 08:42:47742
danakjc492bf82020-09-09 20:02:44743 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
744 // be reset and a RenderFrameHost will be responsible for the navigation.
745 std::unique_ptr<NavigationRequest> navigation_request_;
746
747 // List of objects observing this FrameTreeNode.
748 base::ObserverList<Observer>::Unchecked observers_;
749
750 base::TimeTicks last_focus_time_;
751
arthursonzogni9816b9192021-03-29 16:09:19752 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44753
754 // The user activation state of the current frame. See |UserActivationState|
755 // for details on how this state is maintained.
756 blink::UserActivationState user_activation_state_;
757
shivanigithub4cd016a2021-09-20 21:10:30758 // Fenced Frames:
759 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
760 // frame and any iframes nested within it. Not set if this frame is not in a
761 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
762 // the MPArch version but for the shadow DOM version we need to keep it here
763 // since the fenced frame root is not a main frame for the latter. The value
shivanigithub14182aa2022-05-24 19:29:49764 // of the nonce will be the same for all of the the iframes inside a fenced
shivanigithub4cd016a2021-09-20 21:10:30765 // frame tree. If there is a nested fenced frame it will have a different
766 // nonce than its parent fenced frame. The nonce will stay the same across
shivanigithub14182aa2022-05-24 19:29:49767 // navigations initiated from the fenced frame tree because it is always used
768 // in conjunction with other fields of the keys and would be good to access
769 // the same storage across same-origin navigations. If the navigation is
770 // same-origin/site then the same network stack partition/storage will be
771 // reused and if it's cross-origin/site then other parts of the key will
772 // change and so, even with the same nonce, another partition will be used.
773 // But if the navigation is initiated from the embedder, the nonce will be
774 // reinitialized irrespective of same or cross origin such that there is no
775 // privacy leak via storage shared between two embedder initiated navigations.
776 // Note that this reinitialization is only implemented for MPArch.
shivanigithub4cd016a2021-09-20 21:10:30777 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
778
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58779 const FencedFrameStatus fenced_frame_status_ =
780 FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57781
Garrett Tanzerc69f4642022-08-15 22:15:14782 // If this is a fenced frame resulting from a urn:uuid navigation, this
783 // contains all the metadata specifying the resulting context.
Garrett Tanzerc69f4642022-08-15 22:15:14784 absl::optional<FencedFrameURLMapping::FencedFrameProperties>
785 fenced_frame_properties_;
786
Lukasz Anforowicz147141962020-12-16 18:03:24787 // Manages creation and swapping of RenderFrameHosts for this frame.
788 //
789 // This field needs to be declared last, because destruction of
790 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
791 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
792 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
793 // may try to use FrameTreeNode's fields above - this would be an undefined
794 // behavior if the fields (even trivially-destructible ones) were destructed
795 // before the RenderFrameHostManager's destructor runs. See also
796 // https://crbug.com/1157988.
797 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44798};
799
800} // namespace content
801
802#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_