blob: 7896e49b0e7d50426c547a7aace114992c268488 [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"
Kevin McNee43fe8292021-10-04 22:59:4125#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
danakjc492bf82020-09-09 20:02:4426#include "third_party/blink/public/common/frame/frame_policy.h"
27#include "third_party/blink/public/common/frame/user_activation_state.h"
danakjc492bf82020-09-09 20:02:4428#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,
Kevin McNee43fe8292021-10-04 22:59:4189 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:3490 const blink::FramePolicy& frame_owner);
danakjc492bf82020-09-09 20:02:4491
Peter Boström828b9022021-09-21 02:28:4392 FrameTreeNode(const FrameTreeNode&) = delete;
93 FrameTreeNode& operator=(const FrameTreeNode&) = delete;
94
danakjc492bf82020-09-09 20:02:4495 ~FrameTreeNode();
96
97 void AddObserver(Observer* observer);
98 void RemoveObserver(Observer* observer);
99
100 bool IsMainFrame() const;
101
arthursonzogni76098e52020-11-25 14:18:45102 // Clears any state in this node which was set by the document itself (CSP &
103 // UserActivationState) and notifies proxies as appropriate. Invoked after
104 // committing navigation to a new document (since the new document comes with
105 // a fresh set of CSP).
106 // TODO(arthursonzogni): Remove this function. The frame/document must not be
107 // left temporarily with lax state.
Hiroki Nakagawaab309622021-05-19 16:38:13108 void ResetForNavigation();
danakjc492bf82020-09-09 20:02:44109
110 FrameTree* frame_tree() const { return frame_tree_; }
111 Navigator& navigator() { return frame_tree()->navigator(); }
112
113 RenderFrameHostManager* render_manager() { return &render_manager_; }
114 int frame_tree_node_id() const { return frame_tree_node_id_; }
Antonio Sartori90f41212021-01-22 10:08:34115 const std::string& frame_name() const { return replication_state_->name; }
danakjc492bf82020-09-09 20:02:44116
117 const std::string& unique_name() const {
Antonio Sartori90f41212021-01-22 10:08:34118 return replication_state_->unique_name;
danakjc492bf82020-09-09 20:02:44119 }
120
121 // See comment on the member declaration.
122 const base::UnguessableToken& devtools_frame_token() const {
123 return devtools_frame_token_;
124 }
125
126 size_t child_count() const { return current_frame_host()->child_count(); }
127
danakjc492bf82020-09-09 20:02:44128 RenderFrameHostImpl* parent() const { return parent_; }
129
130 FrameTreeNode* opener() const { return opener_; }
131
132 FrameTreeNode* original_opener() const { return original_opener_; }
133
Anton Bikineevf62d1bf2021-05-15 17:56:07134 const absl::optional<base::UnguessableToken>& opener_devtools_frame_token() {
Wolfgang Beyerd8809db2020-09-30 15:29:39135 return opener_devtools_frame_token_;
136 }
137
danakjc492bf82020-09-09 20:02:44138 // Gets the total number of descendants to this FrameTreeNode in addition to
139 // this node.
140 size_t GetFrameTreeSize() const;
141
142 // Assigns a new opener for this node and, if |opener| is non-null, registers
143 // an observer that will clear this node's opener if |opener| is ever
144 // destroyed.
145 void SetOpener(FrameTreeNode* opener);
146
147 // Assigns the initial opener for this node, and if |opener| is non-null,
148 // registers an observer that will clear this node's opener if |opener| is
149 // ever destroyed. The value set here is the root of the tree.
150 //
151 // It is not possible to change the opener once it was set.
152 void SetOriginalOpener(FrameTreeNode* opener);
153
Wolfgang Beyerd8809db2020-09-30 15:29:39154 // Assigns an opener frame id for this node. This string id is only set once
155 // and cannot be changed. It persists, even if the |opener| is destroyed. It
156 // is used for attribution in the DevTools frontend.
157 void SetOpenerDevtoolsFrameToken(
158 base::UnguessableToken opener_devtools_frame_token);
159
danakjc492bf82020-09-09 20:02:44160 FrameTreeNode* child_at(size_t index) const {
161 return current_frame_host()->child_at(index);
162 }
163
164 // Returns the URL of the last committed page in the current frame.
165 const GURL& current_url() const {
166 return current_frame_host()->GetLastCommittedURL();
167 }
168
169 // Sets the last committed URL for this frame and updates
170 // has_committed_real_load accordingly.
171 void SetCurrentURL(const GURL& url);
172
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56173 // Returns true if SetCurrentURL has been called with a non-blank URL.
174 // TODO(https://crbug.com/1215096): Migrate most usage of
175 // has_committed_real_load() to call
176 // is_on_initial_empty_document_or_subsequent_empty_documents() instead.
danakjc492bf82020-09-09 20:02:44177 bool has_committed_real_load() const { return has_committed_real_load_; }
178
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56179 // Returns true if SetCurrentURL has been called with a non-blank URL or
180 // if the current document's input stream has been opened with
181 // document.open(). For more details, see the definition of
182 // `is_on_initial_empty_document_or_subsequent_empty_documents_`.
183 bool is_on_initial_empty_document_or_subsequent_empty_documents() const {
184 return is_on_initial_empty_document_or_subsequent_empty_documents_;
185 }
186
187 // Sets `is_on_initial_empty_document_or_subsequent_empty_documents_` to
188 // false. Must only be called after the current document's input stream has
189 // been opened with document.open().
190 void DidOpenDocumentInputStream() {
191 is_on_initial_empty_document_or_subsequent_empty_documents_ = false;
192 }
Rakina Zata Amnid09b6112021-06-05 06:20:14193
danakjc492bf82020-09-09 20:02:44194 // Returns whether the frame's owner element in the parent document is
195 // collapsed, that is, removed from the layout as if it did not exist, as per
196 // request by the embedder (of the content/ layer).
197 bool is_collapsed() const { return is_collapsed_; }
198
199 // Sets whether to collapse the frame's owner element in the parent document,
200 // that is, to remove it from the layout as if it did not exist, as per
201 // request by the embedder (of the content/ layer). Cannot be called for main
202 // frames.
203 //
204 // This only has an effect for <iframe> owner elements, and is a no-op when
205 // called on sub-frames hosted in <frame>, <object>, and <embed> elements.
206 void SetCollapsed(bool collapsed);
207
208 // Returns the origin of the last committed page in this frame.
209 // WARNING: To get the last committed origin for a particular
210 // RenderFrameHost, use RenderFrameHost::GetLastCommittedOrigin() instead,
211 // which will behave correctly even when the RenderFrameHost is not the
212 // current one for this frame (such as when it's pending deletion).
213 const url::Origin& current_origin() const {
Antonio Sartori90f41212021-01-22 10:08:34214 return replication_state_->origin;
danakjc492bf82020-09-09 20:02:44215 }
216
217 // Set the current origin and notify proxies about the update.
218 void SetCurrentOrigin(const url::Origin& origin,
219 bool is_potentially_trustworthy_unique_origin);
220
221 // Set the current name and notify proxies about the update.
222 void SetFrameName(const std::string& name, const std::string& unique_name);
223
danakjc492bf82020-09-09 20:02:44224 // Sets the current insecure request policy, and notifies proxies about the
225 // update.
226 void SetInsecureRequestPolicy(blink::mojom::InsecureRequestPolicy policy);
227
228 // Sets the current set of insecure urls to upgrade, and notifies proxies
229 // about the update.
230 void SetInsecureNavigationsSet(
231 const std::vector<uint32_t>& insecure_navigations_set);
232
233 // Returns the latest frame policy (sandbox flags and container policy) for
234 // this frame. This includes flags inherited from parent frames and the latest
235 // flags from the <iframe> element hosting this frame. The returned policies
236 // may not yet have taken effect, since "sandbox" and "allow" attribute
237 // updates in an <iframe> element take effect on next navigation. To retrieve
238 // the currently active policy for this frame, use effective_frame_policy().
239 const blink::FramePolicy& pending_frame_policy() const {
240 return pending_frame_policy_;
241 }
242
243 // Update this frame's sandbox flags and container policy. This is called
244 // when a parent frame updates the "sandbox" attribute in the <iframe> element
245 // for this frame, or any of the attributes which affect the container policy
246 // ("allowfullscreen", "allowpaymentrequest", "allow", and "src".)
247 // These policies won't take effect until next navigation. If this frame's
248 // parent is itself sandboxed, the parent's sandbox flags are combined with
249 // those in |frame_policy|.
250 // Attempting to change the container policy on the main frame will have no
251 // effect.
252 void SetPendingFramePolicy(blink::FramePolicy frame_policy);
253
254 // Returns the currently active frame policy for this frame, including the
255 // sandbox flags which were present at the time the document was loaded, and
Charlie Hu5130d25e2021-03-05 21:53:39256 // the permissions policy container policy, which is set by the iframe's
danakjc492bf82020-09-09 20:02:44257 // allowfullscreen, allowpaymentrequest, and allow attributes, along with the
258 // origin of the iframe's src attribute (which may be different from the URL
259 // of the document currently loaded into the frame). This does not include
260 // policy changes that have been made by updating the containing iframe
261 // element attributes since the frame was last navigated; use
262 // pending_frame_policy() for those.
263 const blink::FramePolicy& effective_frame_policy() const {
Antonio Sartori90f41212021-01-22 10:08:34264 return replication_state_->frame_policy;
danakjc492bf82020-09-09 20:02:44265 }
266
267 // Set the frame_policy provided in function parameter as active frame policy,
268 // while leaving pending_frame_policy_ untouched.
269 bool CommitFramePolicy(const blink::FramePolicy& frame_policy);
270
271 const blink::mojom::FrameOwnerProperties& frame_owner_properties() {
272 return frame_owner_properties_;
273 }
274
275 void set_frame_owner_properties(
276 const blink::mojom::FrameOwnerProperties& frame_owner_properties) {
277 frame_owner_properties_ = frame_owner_properties;
278 }
279
280 const network::mojom::ContentSecurityPolicy* csp_attribute() {
281 return csp_attribute_.get();
282 }
283
284 void set_csp_attribute(
285 network::mojom::ContentSecurityPolicyPtr parsed_csp_attribute) {
286 csp_attribute_ = std::move(parsed_csp_attribute);
287 }
288
Antonio Sartori5abc8de2021-07-13 08:42:47289 // Reflects the 'anonymous' attribute of the corresponding iframe html
290 // element.
291 bool anonymous() const { return anonymous_; }
292 void set_anonymous(bool anonymous) { anonymous_ = anonymous; }
293
danakjc492bf82020-09-09 20:02:44294 bool HasSameOrigin(const FrameTreeNode& node) const {
Antonio Sartori90f41212021-01-22 10:08:34295 return replication_state_->origin.IsSameOriginWith(
296 node.replication_state_->origin);
danakjc492bf82020-09-09 20:02:44297 }
298
Gyuyoung Kimc16e52e92021-03-19 02:45:37299 const blink::mojom::FrameReplicationState& current_replication_state() const {
Antonio Sartori90f41212021-01-22 10:08:34300 return *replication_state_;
danakjc492bf82020-09-09 20:02:44301 }
302
303 RenderFrameHostImpl* current_frame_host() const {
304 return render_manager_.current_frame_host();
305 }
306
307 // Return the node immediately preceding this node in its parent's children,
308 // or nullptr if there is no such node.
309 FrameTreeNode* PreviousSibling() const;
310
311 // Return the node immediately following this node in its parent's children,
312 // or nullptr if there is no such node.
313 FrameTreeNode* NextSibling() const;
314
315 // Returns true if this node is in a loading state.
316 bool IsLoading() const;
317
Alex Moshchuk9b0fd822020-10-26 23:08:15318 // Returns true if this node has a cross-document navigation in progress.
319 bool HasPendingCrossDocumentNavigation() const;
320
danakjc492bf82020-09-09 20:02:44321 NavigationRequest* navigation_request() { return navigation_request_.get(); }
322
323 // Transfers the ownership of the NavigationRequest to |render_frame_host|.
324 // From ReadyToCommit to DidCommit, the NavigationRequest is owned by the
325 // RenderFrameHost that is committing the navigation.
326 void TransferNavigationRequestOwnership(
327 RenderFrameHostImpl* render_frame_host);
328
329 // Takes ownership of |navigation_request| and makes it the current
330 // NavigationRequest of this frame. This corresponds to the start of a new
331 // navigation. If there was an ongoing navigation request before calling this
332 // function, it is canceled. |navigation_request| should not be null.
333 void CreatedNavigationRequest(
334 std::unique_ptr<NavigationRequest> navigation_request);
335
336 // Resets the current navigation request. If |keep_state| is true, any state
337 // created by the NavigationRequest (e.g. speculative RenderFrameHost,
338 // loading state) will not be reset by the function.
339 void ResetNavigationRequest(bool keep_state);
340
341 // A RenderFrameHost in this node started loading.
342 // |to_different_document| will be true unless the load is a fragment
343 // navigation, or triggered by history.pushState/replaceState.
344 // |was_previously_loading| is false if the FrameTree was not loading before.
345 // The caller is required to provide this boolean as the delegate should only
346 // be notified if the FrameTree went from non-loading to loading state.
347 // However, when it is called, the FrameTree should be in a loading state.
348 void DidStartLoading(bool to_different_document, bool was_previously_loading);
349
350 // A RenderFrameHost in this node stopped loading.
351 void DidStopLoading();
352
353 // The load progress for a RenderFrameHost in this node was updated to
354 // |load_progress|. This will notify the FrameTree which will in turn notify
355 // the WebContents.
356 void DidChangeLoadProgress(double load_progress);
357
358 // Called when the user directed the page to stop loading. Stops all loads
359 // happening in the FrameTreeNode. This method should be used with
360 // FrameTree::ForEach to stop all loads in the entire FrameTree.
361 bool StopLoading();
362
363 // Returns the time this frame was last focused.
364 base::TimeTicks last_focus_time() const { return last_focus_time_; }
365
366 // Called when this node becomes focused. Updates the node's last focused
367 // time and notifies observers.
368 void DidFocus();
369
370 // Called when the user closed the modal dialogue for BeforeUnload and
371 // cancelled the navigation. This should stop any load happening in the
372 // FrameTreeNode.
373 void BeforeUnloadCanceled();
374
375 // Returns the BlameContext associated with this node.
376 FrameTreeNodeBlameContext& blame_context() { return blame_context_; }
377
378 // 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
390 void OnSetHadStickyUserActivationBeforeNavigation(bool value);
391
392 // Returns the sandbox flags currently in effect for this frame. This includes
393 // flags inherited from parent frames, the currently active flags from the
394 // <iframe> element hosting this frame, as well as any flags set from a
395 // Content-Security-Policy HTTP header. This does not include flags that have
396 // have been updated in an <iframe> element but have not taken effect yet; use
397 // pending_frame_policy() for those. To see the flags which will take effect
398 // on navigation (which does not include the CSP-set flags), use
399 // effective_frame_policy().
400 network::mojom::WebSandboxFlags active_sandbox_flags() const {
Antonio Sartori90f41212021-01-22 10:08:34401 return replication_state_->active_sandbox_flags;
danakjc492bf82020-09-09 20:02:44402 }
403
404 // Updates the active sandbox flags in this frame, in response to a
405 // Content-Security-Policy header adding additional flags, in addition to
406 // those given to this frame by its parent, or in response to the
Charlie Hu5130d25e2021-03-05 21:53:39407 // Permissions-Policy header being set. Note that on navigation, these updates
danakjc492bf82020-09-09 20:02:44408 // will be cleared, and the flags in the pending frame policy will be applied
409 // to the frame.
Alexander Timin45b716c2020-11-06 01:40:31410 // Returns true iff this operation has changed state of either sandbox flags
Charlie Hu5130d25e2021-03-05 21:53:39411 // or permissions policy.
Alexander Timin45b716c2020-11-06 01:40:31412 bool UpdateFramePolicyHeaders(
danakjc492bf82020-09-09 20:02:44413 network::mojom::WebSandboxFlags sandbox_flags,
Charlie Hue24f04832021-03-04 21:07:06414 const blink::ParsedPermissionsPolicy& parsed_header);
danakjc492bf82020-09-09 20:02:44415
416 // Returns whether the frame received a user gesture on a previous navigation
417 // on the same eTLD+1.
418 bool has_received_user_gesture_before_nav() const {
Antonio Sartori90f41212021-01-22 10:08:34419 return replication_state_->has_received_user_gesture_before_nav;
danakjc492bf82020-09-09 20:02:44420 }
421
422 // When a tab is discarded, WebContents sets was_discarded on its
423 // root FrameTreeNode.
424 // In addition, when a child frame is created, this bit is passed on from
425 // parent to child.
426 // When a navigation request is created, was_discarded is passed on to the
427 // request and reset to false in FrameTreeNode.
428 void set_was_discarded() { was_discarded_ = true; }
429 bool was_discarded() const { return was_discarded_; }
430
431 // Returns the sticky bit of the User Activation v2 state of the
432 // |FrameTreeNode|.
433 bool HasStickyUserActivation() const {
434 return user_activation_state_.HasBeenActive();
435 }
436
437 // Returns the transient bit of the User Activation v2 state of the
438 // |FrameTreeNode|.
439 bool HasTransientUserActivation() {
440 return user_activation_state_.IsActive();
441 }
442
443 // Remove history entries for all frames created by script in this frame's
444 // subtree. If a frame created by a script is removed, then its history entry
445 // will never be reused - this saves memory.
446 void PruneChildFrameNavigationEntries(NavigationEntryImpl* entry);
447
Kevin McNee43fe8292021-10-04 22:59:41448 blink::FrameOwnerElementType frame_owner_element_type() const {
Daniel Cheng9bd90f92021-04-23 20:49:45449 return frame_owner_element_type_;
danakjc492bf82020-09-09 20:02:44450 }
danakjc492bf82020-09-09 20:02:44451
Daniel Cheng6ac128172021-05-25 18:49:01452 blink::mojom::TreeScopeType tree_scope_type() const {
453 return tree_scope_type_;
454 }
455
Alex Turner10d557a42021-06-01 19:06:49456 void SetIsAdSubframe(bool is_ad_subframe);
danakjc492bf82020-09-09 20:02:44457
arthursonzogni034bb9c2020-10-01 08:29:56458 // The initial popup URL for new window opened using:
459 // `window.open(initial_popup_url)`.
460 // An empty GURL otherwise.
461 //
462 // [WARNING] There is no guarantee the FrameTreeNode will ever host a
463 // document served from this URL. The FrameTreeNode always starts hosting the
464 // initial empty document and attempts a navigation toward this URL. However
465 // the navigation might be delayed, redirected and even cancelled.
466 void SetInitialPopupURL(const GURL& initial_popup_url);
467 const GURL& initial_popup_url() const { return initial_popup_url_; }
468
469 // The origin of the document that used window.open() to create this frame.
470 // Otherwise, an opaque Origin with a nonce different from all previously
471 // existing Origins.
472 void SetPopupCreatorOrigin(const url::Origin& popup_creator_origin);
473 const url::Origin& popup_creator_origin() const {
474 return popup_creator_origin_;
475 }
476
Harkiran Bolaria59290d62021-03-17 01:53:01477 // Sets the associated FrameTree for this node. The node can change FrameTrees
478 // when blink::features::Prerender2 is enabled, which allows a page loaded in
479 // the prerendered FrameTree to be used for a navigation in the primary frame
480 // tree.
481 void SetFrameTree(FrameTree& frame_tree);
482
Alexander Timinf785f342021-03-18 00:00:56483 // Write a representation of this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24484 void WriteIntoTrace(perfetto::TracedValue context) const;
Rakina Zata Amni4b1968d2021-09-09 03:29:47485 void WriteIntoTrace(
486 perfetto::TracedProto<perfetto::protos::pbzero::FrameTreeNodeInfo> proto);
Alexander Timinf785f342021-03-18 00:00:56487
Carlos Caballero76711352021-03-24 17:38:21488 // Returns true the node is navigating, i.e. it has an associated
489 // NavigationRequest.
490 bool HasNavigation();
491
shivanigithubf3ddff52021-07-03 22:06:30492 // Fenced frames (meta-bug crbug.com/1111084):
shivanigithub4cd016a2021-09-20 21:10:30493 // Note that these two functions cannot be invoked from a FrameTree's or
494 // its root node's constructor since they require the frame tree and the
495 // root node to be completely constructed.
496 //
shivanigithubf3ddff52021-07-03 22:06:30497 // Returns false if fenced frames are disabled. Returns true if the feature is
498 // enabled and if |this| is a fenced frame. Returns false for
499 // iframes embedded in a fenced frame. To clarify: for the MPArch
500 // implementation this only returns true if |this| is the actual
501 // root node of the inner FrameTree and not the proxy FrameTreeNode in the
502 // outer FrameTree.
Dominic Farolino4bc10ee2021-08-31 00:37:36503 bool IsFencedFrameRoot() const;
shivanigithubf3ddff52021-07-03 22:06:30504
505 // Returns false if fenced frames are disabled. Returns true if the
506 // feature is enabled and if |this| or any of its ancestor nodes is a
507 // fenced frame.
508 bool IsInFencedFrameTree() const;
509
shivanigithub4cd016a2021-09-20 21:10:30510 // Returns a valid nonce if `IsInFencedFrameTree()` returns true for `this`.
511 // Returns nullopt otherwise. See comments on `fenced_frame_nonce_` for more
512 // details.
513 absl::optional<base::UnguessableToken> fenced_frame_nonce() {
514 return fenced_frame_nonce_;
515 }
516
517 // If applicable, set the fenced frame nonce. See comment on
518 // fenced_frame_nonce() for when it is set to a non-null value. Invoked
519 // by FrameTree::Init() or FrameTree::AddFrame().
520 void SetFencedFrameNonceIfNeeded();
521
Harkiran Bolariab4437fd2021-08-11 17:51:22522 // Sets the unique_name and name fields on replication_state_. To be used in
523 // prerender activation to make sure the FrameTreeNode replication state is
524 // correct after the RenderFrameHost is moved between FrameTreeNodes. The
525 // renderers should already have the correct value, so unlike
526 // FrameTreeNode::SetFrameName, we do not notify them here.
527 // TODO(https://crbug.com/1237091): Remove this once the Browsing Instance
528 // Frame State is implemented.
529 void set_frame_name_for_activation(const std::string& unique_name,
530 const std::string& name) {
531 replication_state_->unique_name = unique_name;
532 replication_state_->name = name;
533 }
534
danakjc492bf82020-09-09 20:02:44535 private:
Charlie Hubb5943d2021-03-09 19:46:12536 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44537 ContainerPolicyDynamic);
Charlie Hubb5943d2021-03-09 19:46:12538 FRIEND_TEST_ALL_PREFIXES(SitePerProcessPermissionsPolicyBrowserTest,
danakjc492bf82020-09-09 20:02:44539 ContainerPolicySandboxDynamic);
540
541 class OpenerDestroyedObserver;
542
543 FrameTreeNode* GetSibling(int relative_offset) const;
544
545 // The |notification_type| parameter is used for histograms only.
546 bool NotifyUserActivation(
547 blink::mojom::UserActivationNotificationType notification_type);
548
549 bool ConsumeTransientUserActivation();
550
551 bool ClearUserActivation();
552
553 // Verify that the renderer process is allowed to set user activation on this
554 // frame by checking whether this frame's RenderWidgetHost had previously seen
555 // an input event that might lead to user activation. If user activation
556 // should be allowed, this returns true and also clears corresponding pending
557 // user activation state in the widget. Otherwise, this returns false.
558 bool VerifyUserActivation();
559
560 // The next available browser-global FrameTreeNode ID.
561 static int next_frame_tree_node_id_;
562
563 // The FrameTree that owns us.
564 FrameTree* frame_tree_; // not owned.
565
danakjc492bf82020-09-09 20:02:44566 // A browser-global identifier for the frame in the page, which stays stable
567 // even if the frame does a cross-process navigation.
568 const int frame_tree_node_id_;
569
570 // The RenderFrameHost owning this FrameTreeNode, which cannot change for the
571 // life of this FrameTreeNode. |nullptr| if this node is the root.
572 RenderFrameHostImpl* const parent_;
573
danakjc492bf82020-09-09 20:02:44574 // The frame that opened this frame, if any. Will be set to null if the
575 // opener is closed, or if this frame disowns its opener by setting its
576 // window.opener to null.
arthursonzogni9816b9192021-03-29 16:09:19577 FrameTreeNode* opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44578
579 // An observer that clears this node's |opener_| if the opener is destroyed.
580 // This observer is added to the |opener_|'s observer list when the |opener_|
581 // is set to a non-null node, and it is removed from that list when |opener_|
582 // changes or when this node is destroyed. It is also cleared if |opener_|
583 // is disowned.
584 std::unique_ptr<OpenerDestroyedObserver> opener_observer_;
585
586 // The frame that opened this frame, if any. Contrary to opener_, this
587 // cannot be changed unless the original opener is destroyed.
arthursonzogni9816b9192021-03-29 16:09:19588 FrameTreeNode* original_opener_ = nullptr;
danakjc492bf82020-09-09 20:02:44589
Wolfgang Beyerd8809db2020-09-30 15:29:39590 // The devtools frame token of the frame which opened this frame. This is
591 // not cleared even if the opener is destroyed or disowns the frame.
Anton Bikineevf62d1bf2021-05-15 17:56:07592 absl::optional<base::UnguessableToken> opener_devtools_frame_token_;
Wolfgang Beyerd8809db2020-09-30 15:29:39593
danakjc492bf82020-09-09 20:02:44594 // An observer that clears this node's |original_opener_| if the opener is
595 // destroyed.
596 std::unique_ptr<OpenerDestroyedObserver> original_opener_observer_;
597
arthursonzogni034bb9c2020-10-01 08:29:56598 // When created by an opener, the URL specified in window.open(url)
599 // Please refer to {Get,Set}InitialPopupURL() documentation.
600 GURL initial_popup_url_;
601
602 // When created using window.open, the origin of the creator.
603 // Please refer to {Get,Set}PopupCreatorOrigin() documentation.
604 url::Origin popup_creator_origin_;
605
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56606 // Returns true iff SetCurrentURL has been called with a non-blank URL.
607 // TODO(https://crbug.com/1215096): Migrate all current usage of this to
608 // use `is_on_initial_empty_document_or_subsequent_empty_documents_` instead.
609 bool has_committed_real_load_ = false;
610
611 // Whether this frame is still on the initial about:blank document or any
612 // subsequent about:blank documents committed after the initial about:blank
613 // document. This will be false if either of these has happened:
Rakina Zata Amnid09b6112021-06-05 06:20:14614 // - SetCurrentUrl() has been called with a non about:blank URL.
615 // - The document's input stream has been opened with document.open().
616 // See:
617 // 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:56618 // TODO(https://crbug.com/1215096): Make this false after non-initial
Rakina Zata Amnid09b6112021-06-05 06:20:14619 // about:blank commits as well, making this only track whether the current
Rakina Zata Amnifc4cc3d42021-06-10 09:03:56620 // document is the initial empty document or not. Currently we are still
621 // preserving most of the old behavior of `has_committed_real_load_` (except
622 // for the document.open() bit here) due to our current handling of initial
623 // empty document for session history and navigation (where we treat the
624 // the initial about:blank document and subsequent about:blank documents the
625 // same way).
626 bool is_on_initial_empty_document_or_subsequent_empty_documents_ = true;
danakjc492bf82020-09-09 20:02:44627
628 // Whether the frame's owner element in the parent document is collapsed.
arthursonzogni9816b9192021-03-29 16:09:19629 bool is_collapsed_ = false;
danakjc492bf82020-09-09 20:02:44630
Daniel Cheng6ac128172021-05-25 18:49:01631 // The type of frame owner for this frame. This is only relevant for non-main
632 // frames.
Kevin McNee43fe8292021-10-04 22:59:41633 const blink::FrameOwnerElementType frame_owner_element_type_ =
634 blink::FrameOwnerElementType::kNone;
Daniel Cheng9bd90f92021-04-23 20:49:45635
Daniel Cheng6ac128172021-05-25 18:49:01636 // The tree scope type of frame owner element, i.e. whether the element is in
637 // the document tree (https://dom.spec.whatwg.org/#document-trees) or the
638 // shadow tree (https://dom.spec.whatwg.org/#shadow-trees). This is only
639 // relevant for non-main frames.
640 const blink::mojom::TreeScopeType tree_scope_type_ =
641 blink::mojom::TreeScopeType::kDocument;
642
danakjc492bf82020-09-09 20:02:44643 // Track information that needs to be replicated to processes that have
644 // proxies for this frame.
Gyuyoung Kimc16e52e92021-03-19 02:45:37645 blink::mojom::FrameReplicationStatePtr replication_state_;
danakjc492bf82020-09-09 20:02:44646
647 // Track the pending sandbox flags and container policy for this frame. When a
648 // parent frame dynamically updates 'sandbox', 'allow', 'allowfullscreen',
649 // 'allowpaymentrequest' or 'src' attributes, the updated policy for the frame
Antonio Sartori90f41212021-01-22 10:08:34650 // is stored here, and transferred into replication_state_->frame_policy when
danakjc492bf82020-09-09 20:02:44651 // they take effect on the next frame navigation.
652 blink::FramePolicy pending_frame_policy_;
653
654 // Whether the frame was created by javascript. This is useful to prune
655 // history entries when the frame is removed (because frames created by
656 // scripts are never recreated with the same unique name - see
657 // https://crbug.com/500260).
arthursonzogni9816b9192021-03-29 16:09:19658 const bool is_created_by_script_;
danakjc492bf82020-09-09 20:02:44659
660 // Used for devtools instrumentation and trace-ability. The token is
661 // propagated to Blink's LocalFrame and both Blink and content/
662 // can tag calls and requests with this token in order to attribute them
663 // to the context frame.
664 // |devtools_frame_token_| is only defined by the browser process and is never
665 // sent back from the renderer in the control calls. It should be never used
666 // to look up the FrameTreeNode instance.
arthursonzogni9816b9192021-03-29 16:09:19667 const base::UnguessableToken devtools_frame_token_;
danakjc492bf82020-09-09 20:02:44668
669 // Tracks the scrolling and margin properties for this frame. These
670 // properties affect the child renderer but are stored on its parent's
671 // frame element. When this frame's parent dynamically updates these
672 // properties, we update them here too.
673 //
674 // Note that dynamic updates only take effect on the next frame navigation.
675 blink::mojom::FrameOwnerProperties frame_owner_properties_;
676
677 // Contains the current parsed value of the 'csp' attribute of this frame.
678 network::mojom::ContentSecurityPolicyPtr csp_attribute_;
679
Antonio Sartori5abc8de2021-07-13 08:42:47680 // Reflects the 'anonymous' attribute of the corresponding iframe html
681 // element.
682 bool anonymous_ = false;
683
danakjc492bf82020-09-09 20:02:44684 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
685 // be reset and a RenderFrameHost will be responsible for the navigation.
686 std::unique_ptr<NavigationRequest> navigation_request_;
687
688 // List of objects observing this FrameTreeNode.
689 base::ObserverList<Observer>::Unchecked observers_;
690
691 base::TimeTicks last_focus_time_;
692
arthursonzogni9816b9192021-03-29 16:09:19693 bool was_discarded_ = false;
danakjc492bf82020-09-09 20:02:44694
695 // The user activation state of the current frame. See |UserActivationState|
696 // for details on how this state is maintained.
697 blink::UserActivationState user_activation_state_;
698
699 // A helper for tracing the snapshots of this FrameTreeNode and attributing
700 // browser process activities to this node (when possible). It is unrelated
701 // to the core logic of FrameTreeNode.
702 FrameTreeNodeBlameContext blame_context_;
703
shivanigithub4cd016a2021-09-20 21:10:30704 // Fenced Frames:
705 // Nonce used in the net::IsolationInfo and blink::StorageKey for a fenced
706 // frame and any iframes nested within it. Not set if this frame is not in a
707 // fenced frame's FrameTree. Note that this could be a field in FrameTree for
708 // the MPArch version but for the shadow DOM version we need to keep it here
709 // since the fenced frame root is not a main frame for the latter. The value
710 // of the nonce will be the same for all of the the frames inside a fenced
711 // frame tree. If there is a nested fenced frame it will have a different
712 // nonce than its parent fenced frame. The nonce will stay the same across
713 // navigations because it is always used in conjunction with other fields of
714 // the keys. If the navigation is same-origin/site then the same network stack
715 // partition/storage will be reused and if it's cross-origin/site then other
716 // parts of the key will change and so, even with the same nonce, another
717 // partition will be used.
718 absl::optional<base::UnguessableToken> fenced_frame_nonce_;
719
Lukasz Anforowicz147141962020-12-16 18:03:24720 // Manages creation and swapping of RenderFrameHosts for this frame.
721 //
722 // This field needs to be declared last, because destruction of
723 // RenderFrameHostManager may call arbitrary callbacks (e.g. via
724 // WebContentsObserver::DidFinishNavigation fired after RenderFrameHostManager
725 // destructs a RenderFrameHostImpl and its NavigationRequest). Such callbacks
726 // may try to use FrameTreeNode's fields above - this would be an undefined
727 // behavior if the fields (even trivially-destructible ones) were destructed
728 // before the RenderFrameHostManager's destructor runs. See also
729 // https://crbug.com/1157988.
730 RenderFrameHostManager render_manager_;
danakjc492bf82020-09-09 20:02:44731};
732
733} // namespace content
734
735#endif // CONTENT_BROWSER_RENDERER_HOST_FRAME_TREE_NODE_H_