blob: a8092e237b5efd66770d03b2aa5afde44420e6c9 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]9b159a52013-10-03 17:24:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakjc492bf82020-09-09 20:02:445#include "content/browser/renderer_host/frame_tree_node.h"
[email protected]9b159a52013-10-03 17:24:556
Daniel Cheng6ca7f1c92017-08-09 21:45:417#include <math.h>
[email protected]9b159a52013-10-03 17:24:558#include <queue>
Takuto Ikutaadf31eb2019-01-05 00:32:489#include <unordered_map>
dcheng36b6aec92015-12-26 06:16:3610#include <utility>
[email protected]9b159a52013-10-03 17:24:5511
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1412#include "base/feature_list.h"
scottmg6ece5ae2017-02-01 18:25:1913#include "base/lazy_instance.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
Liviu Tintad9391fb92020-09-28 23:50:0715#include "base/metrics/histogram_functions.h"
dcheng23ca947d2016-05-04 20:04:1516#include "base/metrics/histogram_macros.h"
David Sandersd4bf5eb2022-03-17 07:12:0517#include "base/observer_list.h"
Clark DuVallc97bcf72021-12-08 22:58:2418#include "base/strings/strcat.h"
Daniel Cheng6ca7f1c92017-08-09 21:45:4119#include "base/strings/string_util.h"
Clark DuVallc97bcf72021-12-08 22:58:2420#include "base/timer/elapsed_timer.h"
Andrey Kosyakovf2d4ff72018-10-29 20:09:5921#include "content/browser/devtools/devtools_instrumentation.h"
Dominic Farolino8a2187b2021-12-24 20:44:2122#include "content/browser/fenced_frame/fenced_frame.h"
Paul Semel3e241042022-10-11 12:57:3123#include "content/browser/renderer_host/frame_tree.h"
danakjc492bf82020-09-09 20:02:4424#include "content/browser/renderer_host/navigation_controller_impl.h"
25#include "content/browser/renderer_host/navigation_request.h"
26#include "content/browser/renderer_host/navigator.h"
27#include "content/browser/renderer_host/navigator_delegate.h"
28#include "content/browser/renderer_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4129#include "content/browser/renderer_host/render_view_host_impl.h"
Lucas Furukawa Gadanief8290a2019-07-29 20:27:5130#include "content/common/navigation_params_utils.h"
dmazzonie950ea232015-03-13 21:39:4531#include "content/public/browser/browser_thread.h"
Nan Linaaf84f72021-12-02 22:31:5632#include "content/public/browser/site_isolation_policy.h"
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1433#include "content/public/common/content_features.h"
arthursonzognib93a4472020-04-10 07:38:0034#include "services/network/public/cpp/web_sandbox_flags.h"
35#include "services/network/public/mojom/web_sandbox_flags.mojom-shared.h"
Harkiran Bolaria59290d62021-03-17 01:53:0136#include "third_party/blink/public/common/features.h"
Liam Bradyb0f1f0e2022-08-19 21:42:1137#include "third_party/blink/public/common/frame/fenced_frame_sandbox_flags.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1538#include "third_party/blink/public/common/loader/loader_constants.h"
Antonio Gomes4b2c5132020-01-16 11:49:4839#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
Julie Jeongeun Kimd90e2dd2020-03-03 11:45:3740#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
[email protected]9b159a52013-10-03 17:24:5541
42namespace content {
43
dmazzonie950ea232015-03-13 21:39:4544namespace {
45
46// This is a global map between frame_tree_node_ids and pointers to
47// FrameTreeNodes.
Takuto Ikutaadf31eb2019-01-05 00:32:4848typedef std::unordered_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4549
scottmg5e65e3a2017-03-08 08:48:4650base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
51 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4552
Nan Line376738a2022-03-25 22:05:4153FencedFrame* FindFencedFrame(const FrameTreeNode* frame_tree_node) {
54 // TODO(crbug.com/1123606): Consider having a pointer to `FencedFrame` in
55 // `FrameTreeNode` or having a map between them.
56
57 // Try and find the `FencedFrame` that `frame_tree_node` represents.
58 DCHECK(frame_tree_node->parent());
59 std::vector<FencedFrame*> fenced_frames =
60 frame_tree_node->parent()->GetFencedFrames();
61 for (FencedFrame* fenced_frame : fenced_frames) {
62 if (frame_tree_node->frame_tree_node_id() ==
63 fenced_frame->GetOuterDelegateFrameTreeNodeId()) {
64 return fenced_frame;
65 }
66 }
67 return nullptr;
68}
69
fdegansa696e5112015-04-17 01:57:5970} // namespace
fdegans1d16355162015-03-26 11:58:3471
alexmose201c7cd2015-06-10 17:14:2172// This observer watches the opener of its owner FrameTreeNode and clears the
Arthur Hemerye4659282022-03-28 08:36:1573// owner's opener if the opener is destroyed or swaps BrowsingInstance.
alexmose201c7cd2015-06-10 17:14:2174class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
75 public:
jochen6004a362017-02-04 00:11:4076 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
77 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2178
Peter Boström9b036532021-10-28 23:37:2879 OpenerDestroyedObserver(const OpenerDestroyedObserver&) = delete;
80 OpenerDestroyedObserver& operator=(const OpenerDestroyedObserver&) = delete;
81
alexmose201c7cd2015-06-10 17:14:2182 // FrameTreeNode::Observer
83 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
Arthur Hemerye4659282022-03-28 08:36:1584 NullifyOpener(node);
85 }
86
87 // FrameTreeNode::Observer
88 void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) override {
89 NullifyOpener(node);
90 }
91
92 void NullifyOpener(FrameTreeNode* node) {
jochen6004a362017-02-04 00:11:4093 if (observing_original_opener_) {
Rakina Zata Amni3a48ae42022-05-05 03:39:5694 // The "original opener" is special. It's used for attribution, and
95 // clients walk down the original opener chain. Therefore, if a link in
96 // the chain is being destroyed, reconnect the observation to the parent
97 // of the link being destroyed.
98 CHECK_EQ(owner_->first_live_main_frame_in_original_opener_chain(), node);
99 owner_->SetOriginalOpener(
100 node->first_live_main_frame_in_original_opener_chain());
Avi Drissman36465f332017-09-11 20:49:39101 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40102 } else {
103 CHECK_EQ(owner_->opener(), node);
104 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:39105 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40106 }
alexmose201c7cd2015-06-10 17:14:21107 }
108
109 private:
Keishi Hattori0e45c022021-11-27 09:25:52110 raw_ptr<FrameTreeNode> owner_;
jochen6004a362017-02-04 00:11:40111 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:21112};
113
Kevin McNee88e61552020-10-22 20:41:11114const int FrameTreeNode::kFrameTreeNodeInvalidId = -1;
115
116static_assert(FrameTreeNode::kFrameTreeNodeInvalidId ==
117 RenderFrameHost::kNoFrameTreeNodeId,
118 "Have consistent sentinel values for an invalid FTN id.");
119
vishal.b782eb5d2015-04-29 12:22:57120int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:55121
dmazzonie950ea232015-03-13 21:39:45122// static
vishal.b782eb5d2015-04-29 12:22:57123FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:19124 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:15125 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
jdoerrie55ec69d2018-10-08 13:34:46126 auto it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:45127 return it == nodes->end() ? nullptr : it->second;
128}
129
Alexander Timin381e7e182020-04-28 19:04:03130// static
131FrameTreeNode* FrameTreeNode::From(RenderFrameHost* rfh) {
132 if (!rfh)
133 return nullptr;
134 return static_cast<RenderFrameHostImpl*>(rfh)->frame_tree_node();
135}
136
Abhijeet Kandalkarb43affa72022-09-27 16:48:01137FrameTreeNode::FencedFrameStatus ComputeFencedFrameStatus(
Harkiran Bolaria16f2c48d2022-04-22 12:39:57138 FrameTree* frame_tree,
139 RenderFrameHostImpl* parent,
140 const blink::FramePolicy& frame_policy) {
Abhijeet Kandalkarb43affa72022-09-27 16:48:01141 using FencedFrameStatus = FrameTreeNode::FencedFrameStatus;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57142 if (blink::features::IsFencedFramesEnabled()) {
143 switch (blink::features::kFencedFramesImplementationTypeParam.Get()) {
144 case blink::features::FencedFramesImplementationType::kMPArch: {
145 if (frame_tree->type() == FrameTree::Type::kFencedFrame) {
146 if (!parent)
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58147 return FencedFrameStatus::kFencedFrameRoot;
148 return FencedFrameStatus::kIframeNestedWithinFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57149 } else {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58150 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57151 }
152 }
153 case blink::features::FencedFramesImplementationType::kShadowDOM: {
154 // Different from the MPArch case, the ShadowDOM implementation of
155 // fenced frame lives in the same FrameTree as its parent, so we need to
156 // check its effective frame policy instead.
157 if (frame_policy.is_fenced) {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58158 return FencedFrameStatus::kFencedFrameRoot;
Abhijeet Kandalkardf9af612022-10-06 07:02:45159 } else if (parent && parent->IsNestedWithinFencedFrame()) {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58160 return FencedFrameStatus::kIframeNestedWithinFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57161 }
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58162 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57163 }
164 default: {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58165 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57166 }
167 }
168 }
169
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58170 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57171}
172
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54173FrameTreeNode::FrameTreeNode(
174 FrameTree* frame_tree,
Alexander Timin381e7e182020-04-28 19:04:03175 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:01176 blink::mojom::TreeScopeType tree_scope_type,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54177 bool is_created_by_script,
178 const base::UnguessableToken& devtools_frame_token,
179 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:41180 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:34181 const blink::FramePolicy& frame_policy)
[email protected]bffc8302014-01-23 20:52:16182 : frame_tree_(frame_tree),
[email protected]bffc8302014-01-23 20:52:16183 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59184 parent_(parent),
Daniel Cheng9bd90f92021-04-23 20:49:45185 frame_owner_element_type_(owner_type),
Daniel Cheng6ac128172021-05-25 18:49:01186 tree_scope_type_(tree_scope_type),
Dominic Farolino08662c82021-06-11 07:36:34187 pending_frame_policy_(frame_policy),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45188 is_created_by_script_(is_created_by_script),
Pavel Feldman25234722017-10-11 02:49:06189 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31190 frame_owner_properties_(frame_owner_properties),
Yuzu Saijo03dbf9b2022-07-22 04:29:45191 attributes_(blink::mojom::IframeAttributes::New()),
Harkiran Bolaria16f2c48d2022-04-22 12:39:57192 fenced_frame_status_(
193 ComputeFencedFrameStatus(frame_tree_, parent_, frame_policy)),
Harkiran Bolaria0b3bdef02022-03-10 13:04:40194 render_manager_(this, frame_tree->manager_delegate()) {
Harkiran Bolariaa8347782022-04-06 09:25:11195 TRACE_EVENT_BEGIN("navigation", "FrameTreeNode",
196 perfetto::Track::FromPointer(this),
197 "frame_tree_node_when_created", this);
rob97250742015-12-10 17:45:15198 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45199 g_frame_tree_node_id_map.Get().insert(
200 std::make_pair(frame_tree_node_id_, this));
201 CHECK(result.second);
alexmos998581d2015-01-22 01:01:59202}
[email protected]9b159a52013-10-03 17:24:55203
Dominic Farolino8a2187b2021-12-24 20:44:21204void FrameTreeNode::DestroyInnerFrameTreeIfExists() {
205 // If `this` is an dummy outer delegate node, then we really are representing
206 // an inner FrameTree for one of the following consumers:
207 // - `Portal`
208 // - `FencedFrame`
209 // - `GuestView`
210 // If we are representing a `FencedFrame` object, we need to destroy it
211 // alongside ourself. `Portals` and `GuestView` however, *currently* have a
212 // more complex lifetime and are dealt with separately.
213 bool is_outer_dummy_node = false;
214 if (current_frame_host() &&
215 current_frame_host()->inner_tree_main_frame_tree_node_id() !=
216 FrameTreeNode::kFrameTreeNodeInvalidId) {
217 is_outer_dummy_node = true;
218 }
219
220 if (is_outer_dummy_node) {
Nan Line376738a2022-03-25 22:05:41221 FencedFrame* doomed_fenced_frame = FindFencedFrame(this);
Dominic Farolino8a2187b2021-12-24 20:44:21222 // `doomed_fenced_frame` might not actually exist, because some outer dummy
223 // `FrameTreeNode`s might correspond to `Portal`s, which do not have their
224 // lifetime managed in the same way as `FencedFrames`.
225 if (doomed_fenced_frame) {
226 parent()->DestroyFencedFrame(*doomed_fenced_frame);
227 }
228 }
229}
230
[email protected]9b159a52013-10-03 17:24:55231FrameTreeNode::~FrameTreeNode() {
Harkiran Bolariaa8347782022-04-06 09:25:11232 TRACE_EVENT("navigation", "FrameTreeNode::~FrameTreeNode");
Daniel Chengc3d1e8d2021-06-23 02:11:45233 // There should always be a current RenderFrameHost except during prerender
234 // activation. Prerender activation moves the current RenderFrameHost from
235 // the old FrameTree's FrameTreeNode to the new FrameTree's FrameTreeNode and
236 // then destroys the old FrameTree. See
237 // `RenderFrameHostManager::TakePrerenderedPage()`.
Harkiran Bolaria59290d62021-03-17 01:53:01238 if (current_frame_host()) {
239 // Remove the children.
240 current_frame_host()->ResetChildren();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45241
Harkiran Bolaria59290d62021-03-17 01:53:01242 current_frame_host()->ResetLoadingState();
243 } else {
Hiroki Nakagawa0a90bd42021-04-21 01:53:05244 DCHECK(blink::features::IsPrerender2Enabled());
Harkiran Bolaria59290d62021-03-17 01:53:01245 DCHECK(!parent()); // Only main documents can be activated.
246 DCHECK(!opener()); // Prerendered frame trees can't have openers.
247
248 // Activation is not allowed during ongoing navigations.
249 DCHECK(!navigation_request_);
250
Carlos Caballerod1c80432021-04-20 08:16:32251 // TODO(https://crbug.com/1199693): Need to determine how to handle pending
Harkiran Bolaria59290d62021-03-17 01:53:01252 // deletions, as observers will be notified.
253 DCHECK(!render_manager()->speculative_frame_host());
254 }
Nate Chapin22ea6592019-03-05 22:29:02255
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45256 // If the removed frame was created by a script, then its history entry will
257 // never be reused - we can save some memory by removing the history entry.
258 // See also https://crbug.com/784356.
259 if (is_created_by_script_ && parent_) {
Carlos Caballero04aab362021-02-15 17:38:16260 NavigationEntryImpl* nav_entry =
261 navigator().controller().GetLastCommittedEntry();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45262 if (nav_entry) {
263 nav_entry->RemoveEntryForFrame(this,
264 /* only_if_different_position = */ false);
265 }
266 }
267
dmazzonie950ea232015-03-13 21:39:45268 frame_tree_->FrameRemoved(this);
Carlos Caballero6ff6ace2021-02-05 16:53:00269
Dominic Farolino8a2187b2021-12-24 20:44:21270 DestroyInnerFrameTreeIfExists();
271
Carlos Caballero6ff6ace2021-02-05 16:53:00272 // Do not dispatch notification for the root frame as ~WebContentsImpl already
273 // dispatches it for now.
274 // TODO(https://crbug.com/1170277): This is only needed because the FrameTree
275 // is a member of WebContentsImpl and we would call back into it during
276 // destruction. We should clean up the FrameTree destruction code and call the
277 // delegate unconditionally.
278 if (parent())
279 render_manager_.delegate()->OnFrameTreeNodeDestroyed(this);
280
ericwilligers254597b2016-10-17 10:32:31281 for (auto& observer : observers_)
282 observer.OnFrameTreeNodeDestroyed(this);
Lukasz Anforowicz147141962020-12-16 18:03:24283 observers_.Clear();
alexmose201c7cd2015-06-10 17:14:21284
285 if (opener_)
286 opener_->RemoveObserver(opener_observer_.get());
Rakina Zata Amni3a48ae42022-05-05 03:39:56287 if (first_live_main_frame_in_original_opener_chain_)
288 first_live_main_frame_in_original_opener_chain_->RemoveObserver(
289 original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45290
291 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18292
Daniel Chengc3d1e8d2021-06-23 02:11:45293 // If a frame with a pending navigation is detached, make sure the
294 // WebContents (and its observers) update their loading state.
295 // TODO(dcheng): This should just check `IsLoading()`, but `IsLoading()`
296 // assumes that `current_frame_host_` is not null. This is incompatible with
297 // prerender activation when destroying the old frame tree (see above).
danakjf9400602019-06-07 15:44:58298 bool did_stop_loading = false;
299
jam39258caf2016-11-02 14:48:18300 if (navigation_request_) {
danakjf9400602019-06-07 15:44:58301 navigation_request_.reset();
danakjf9400602019-06-07 15:44:58302 did_stop_loading = true;
jam39258caf2016-11-02 14:48:18303 }
Nate Chapin22ea6592019-03-05 22:29:02304
danakjf9400602019-06-07 15:44:58305 // ~SiteProcessCountTracker DCHECKs in some tests if the speculative
306 // RenderFrameHostImpl is not destroyed last. Ideally this would be closer to
307 // (possible before) the ResetLoadingState() call above.
danakjf9400602019-06-07 15:44:58308 if (render_manager_.speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:45309 // TODO(dcheng): Shouldn't a FrameTreeNode with a speculative
310 // RenderFrameHost always be considered loading?
danakjf9400602019-06-07 15:44:58311 did_stop_loading |= render_manager_.speculative_frame_host()->is_loading();
Daniel Chengc3d1e8d2021-06-23 02:11:45312 // `FrameTree::Shutdown()` has special handling for the main frame's
313 // speculative RenderFrameHost, and the speculative RenderFrameHost should
314 // already be reset for main frames.
315 DCHECK(!IsMainFrame());
316
317 // This does not use `UnsetSpeculativeRenderFrameHost()`: if the speculative
318 // RenderFrameHost has already reached kPendingCommit, it would needlessly
319 // re-create a proxy for a frame that's going away.
320 render_manager_.DiscardSpeculativeRenderFrameHostForShutdown();
danakjf9400602019-06-07 15:44:58321 }
322
323 if (did_stop_loading)
324 DidStopLoading();
325
Harkiran Bolaria59290d62021-03-17 01:53:01326 // IsLoading() requires that current_frame_host() is non-null.
327 DCHECK(!current_frame_host() || !IsLoading());
Harkiran Bolariaa8347782022-04-06 09:25:11328
329 // Matches the TRACE_EVENT_BEGIN in the constructor.
330 TRACE_EVENT_END("navigation", perfetto::Track::FromPointer(this));
[email protected]9b159a52013-10-03 17:24:55331}
332
alexmose201c7cd2015-06-10 17:14:21333void FrameTreeNode::AddObserver(Observer* observer) {
334 observers_.AddObserver(observer);
335}
336
337void FrameTreeNode::RemoveObserver(Observer* observer) {
338 observers_.RemoveObserver(observer);
339}
340
[email protected]94d0cc12013-12-18 00:07:41341bool FrameTreeNode::IsMainFrame() const {
342 return frame_tree_->root() == this;
343}
344
Paul Semel3e241042022-10-11 12:57:31345Navigator& FrameTreeNode::navigator() {
346 return frame_tree()->navigator();
347}
348
Ian Vollick25a9d032022-04-12 23:20:17349bool FrameTreeNode::IsOutermostMainFrame() {
350 return !GetParentOrOuterDocument();
351}
352
Hiroki Nakagawaab309622021-05-19 16:38:13353void FrameTreeNode::ResetForNavigation() {
arthursonzogni76098e52020-11-25 14:18:45354 // This frame has had its user activation bits cleared in the renderer before
355 // arriving here. We just need to clear them here and in the other renderer
356 // processes that may have a reference to this frame.
Alexander Timin45b716c2020-11-06 01:40:31357 //
358 // We do not take user activation into account when calculating
359 // |ResetForNavigationResult|, as we are using it to determine bfcache
360 // eligibility and the page can get another user gesture after restore.
Antonio Gomes4b2c5132020-01-16 11:49:48361 UpdateUserActivationState(
Mustaq Ahmeddc195e5b2020-08-04 18:45:11362 blink::mojom::UserActivationUpdateType::kClearActivation,
363 blink::mojom::UserActivationNotificationType::kNone);
Ian Clelland5cbaaf82017-11-27 22:00:03364}
365
Dave Tapuskac8de3b02021-12-03 21:51:01366RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocument() {
367 return GetParentOrOuterDocumentHelper(/*escape_guest_view=*/false);
368}
369
370RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocumentOrEmbedder() {
371 return GetParentOrOuterDocumentHelper(/*escape_guest_view=*/true);
372}
373
374RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocumentHelper(
375 bool escape_guest_view) {
376 // Find the parent in the FrameTree (iframe).
377 if (parent_)
378 return parent_;
379
380 if (!escape_guest_view) {
381 // If we are not a fenced frame root nor inside a portal then return early.
382 // This code does not escape GuestViews.
383 if (!IsFencedFrameRoot() && !frame_tree_->delegate()->IsPortal())
384 return nullptr;
385 }
386
387 // Find the parent in the outer embedder (GuestView, Portal, or Fenced Frame).
388 FrameTreeNode* frame_in_embedder = render_manager()->GetOuterDelegateNode();
389 if (frame_in_embedder)
390 return frame_in_embedder->current_frame_host()->GetParent();
391
392 // No parent found.
393 return nullptr;
394}
395
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55396FrameType FrameTreeNode::GetFrameType() const {
397 if (!IsMainFrame())
398 return FrameType::kSubframe;
399
400 switch (frame_tree()->type()) {
401 case FrameTree::Type::kPrimary:
402 return FrameType::kPrimaryMainFrame;
403 case FrameTree::Type::kPrerender:
404 return FrameType::kPrerenderMainFrame;
405 case FrameTree::Type::kFencedFrame:
406 // We also have FencedFramesImplementationType::kShadowDOM for a
407 // fenced frame implementation based on <iframe> + shadowDOM,
408 // which will return kSubframe as it's a modified <iframe> rather
409 // than a dedicated FrameTree. This returns kSubframe for the
410 // shadow dom implementation in order to keep consistency (i.e.
411 // NavigationHandle::GetParentFrame returning non-null value for
412 // shadow-dom based FFs).
413 return FrameType::kFencedFrameRoot;
414 }
415}
416
alexmose201c7cd2015-06-10 17:14:21417void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
Harkiran Bolariaa8347782022-04-06 09:25:11418 TRACE_EVENT("navigation", "FrameTreeNode::SetOpener",
419 ChromeTrackEvent::kFrameTreeNodeInfo, opener);
alexmose201c7cd2015-06-10 17:14:21420 if (opener_) {
421 opener_->RemoveObserver(opener_observer_.get());
422 opener_observer_.reset();
423 }
424
425 opener_ = opener;
426
427 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55428 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21429 opener_->AddObserver(opener_observer_.get());
430 }
431}
432
Wolfgang Beyerd8809db2020-09-30 15:29:39433void FrameTreeNode::SetOpenerDevtoolsFrameToken(
434 base::UnguessableToken opener_devtools_frame_token) {
435 DCHECK(!opener_devtools_frame_token_ ||
436 opener_devtools_frame_token_->is_empty());
437 opener_devtools_frame_token_ = std::move(opener_devtools_frame_token);
438}
439
jochen6004a362017-02-04 00:11:40440void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39441 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37442 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40443
Rakina Zata Amni3a48ae42022-05-05 03:39:56444 if (first_live_main_frame_in_original_opener_chain_) {
445 first_live_main_frame_in_original_opener_chain_->RemoveObserver(
446 original_opener_observer_.get());
Avi Drissman36465f332017-09-11 20:49:39447 original_opener_observer_.reset();
448 }
449
Rakina Zata Amni3a48ae42022-05-05 03:39:56450 first_live_main_frame_in_original_opener_chain_ = opener;
jochen6004a362017-02-04 00:11:40451
Rakina Zata Amni3a48ae42022-05-05 03:39:56452 if (first_live_main_frame_in_original_opener_chain_) {
453 original_opener_observer_ = std::make_unique<OpenerDestroyedObserver>(
454 this, true /* observing_original_opener */);
455 first_live_main_frame_in_original_opener_chain_->AddObserver(
456 original_opener_observer_.get());
jochen6004a362017-02-04 00:11:40457 }
458}
459
engedy6e2e0992017-05-25 18:58:42460void FrameTreeNode::SetCollapsed(bool collapsed) {
Dave Tapuska65e50aa2022-03-09 23:44:13461 DCHECK(!IsMainFrame() || IsFencedFrameRoot());
engedy6e2e0992017-05-25 18:58:42462 if (is_collapsed_ == collapsed)
463 return;
464
465 is_collapsed_ = collapsed;
466 render_manager_.OnDidChangeCollapsedState(collapsed);
467}
468
Harkiran Bolaria59290d62021-03-17 01:53:01469void FrameTreeNode::SetFrameTree(FrameTree& frame_tree) {
Hiroki Nakagawa0a90bd42021-04-21 01:53:05470 DCHECK(blink::features::IsPrerender2Enabled());
Harkiran Bolaria59290d62021-03-17 01:53:01471 frame_tree_ = &frame_tree;
Kevin McNeeb110d0c2021-10-26 15:53:00472 DCHECK(current_frame_host());
473 current_frame_host()->SetFrameTree(frame_tree);
474 RenderFrameHostImpl* speculative_frame_host =
475 render_manager_.speculative_frame_host();
476 if (speculative_frame_host)
477 speculative_frame_host->SetFrameTree(frame_tree);
Harkiran Bolaria59290d62021-03-17 01:53:01478}
479
Luna Luc3fdacdf2017-11-08 04:48:53480void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Dominic Farolinobfd1f0292022-03-23 19:12:24481 // The `is_fenced` and `fenced_frame_mode` bits should never be able to
482 // transition from their initial values. Since we never expect to be in a
483 // position where it can even be updated to new value, if we catch this
484 // happening we have to kill the renderer and refuse to accept any other frame
485 // policy changes here.
486 if (pending_frame_policy_.is_fenced != frame_policy.is_fenced ||
487 pending_frame_policy_.fenced_frame_mode !=
488 frame_policy.fenced_frame_mode) {
Dominic Farolino08662c82021-06-11 07:36:34489 mojo::ReportBadMessage(
Dominic Farolinobfd1f0292022-03-23 19:12:24490 "FramePolicy properties dealing with fenced frames are considered "
491 "immutable, and therefore should never be changed by the renderer.");
Dominic Farolino08662c82021-06-11 07:36:34492 return;
493 }
494
Liam Bradyb0f1f0e2022-08-19 21:42:11495 // Inside of a fenced frame, the sandbox flags should not be able to change
496 // from its initial value. If the flags change, we have to assume the change
497 // came from a compromised renderer and terminate it.
498 // We will only do the check if the sandbox flags are already set to
499 // kFencedFrameForcedSandboxFlags. This is to allow the sandbox flags to
500 // be set initially (go from kNone -> kFencedFrameForcedSandboxFlags). Once
501 // it has been set, it cannot change to another value.
502 // Note: The bad message is only expected to hit for ShadowDOM fenced frames.
503 // For MPArch, the RFHI will detect that the change is not coming from the
504 // frame's parent in DidChangeFramePolicy() (an MPArch fenced frame parent
505 // is null since it's the root frame in its tree) and terminate the
506 // renderer before we reach this point.
507 // TODO(crbug.com/1262022) When ShadowDOM is removed, turn this into a DCHECK
508 // and remove the BadMessage call.
509 if (IsFencedFrameRoot() &&
510 pending_frame_policy_.sandbox_flags ==
511 blink::kFencedFrameForcedSandboxFlags &&
512 frame_policy.sandbox_flags != blink::kFencedFrameForcedSandboxFlags) {
513 DCHECK(frame_tree()->IsFencedFramesShadowDOMBased());
514 bad_message::ReceivedBadMessage(
515 current_frame_host()->GetProcess(),
516 bad_message::FF_FROZEN_SANDBOX_FLAGS_CHANGED);
517 return;
518 }
519
Ian Clellandcdc4f312017-10-13 22:24:12520 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25521
Ian Clellandcdc4f312017-10-13 22:24:12522 if (parent()) {
523 // Subframes should always inherit their parent's sandbox flags.
Alexander Timin381e7e182020-04-28 19:04:03524 pending_frame_policy_.sandbox_flags |=
Harkiran Bolaria4eacb3a2021-12-13 20:03:47525 parent()->browsing_context_state()->active_sandbox_flags();
Charlie Hue1b77ac2019-12-13 21:30:17526 // This is only applied on subframes; container policy and required document
527 // policy are not mutable on main frame.
Ian Clellandcdc4f312017-10-13 22:24:12528 pending_frame_policy_.container_policy = frame_policy.container_policy;
Charlie Hue1b77ac2019-12-13 21:30:17529 pending_frame_policy_.required_document_policy =
530 frame_policy.required_document_policy;
Ian Clellandcdc4f312017-10-13 22:24:12531 }
iclelland92f8c0b2017-04-19 12:43:05532}
533
Yuzu Saijo03dbf9b2022-07-22 04:29:45534void FrameTreeNode::SetAttributes(
535 blink::mojom::IframeAttributesPtr attributes) {
536 if (!anonymous() && attributes->anonymous) {
537 // Log this only when anonymous is changed to true.
Arthur Sonzogni2e9c6111392022-05-02 08:37:13538 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
539 parent_, blink::mojom::WebFeature::kAnonymousIframe);
540 }
Yuzu Saijo03dbf9b2022-07-22 04:29:45541 attributes_ = std::move(attributes);
Arthur Sonzogni2e9c6111392022-05-02 08:37:13542}
543
fdegans4a49ce932015-03-12 17:11:37544bool FrameTreeNode::IsLoading() const {
545 RenderFrameHostImpl* current_frame_host =
546 render_manager_.current_frame_host();
fdegans4a49ce932015-03-12 17:11:37547
548 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39549
clamy610c63b32017-12-22 15:05:18550 if (navigation_request_)
551 return true;
clamy11e11512015-07-07 16:42:17552
clamy610c63b32017-12-22 15:05:18553 RenderFrameHostImpl* speculative_frame_host =
554 render_manager_.speculative_frame_host();
Daniel Chengc3d1e8d2021-06-23 02:11:45555 // TODO(dcheng): Shouldn't a FrameTreeNode with a speculative RenderFrameHost
556 // always be considered loading?
clamy610c63b32017-12-22 15:05:18557 if (speculative_frame_host && speculative_frame_host->is_loading())
558 return true;
fdegans4a49ce932015-03-12 17:11:37559 return current_frame_host->is_loading();
560}
561
Alex Moshchuk9b0fd822020-10-26 23:08:15562bool FrameTreeNode::HasPendingCrossDocumentNavigation() const {
563 // Having a |navigation_request_| on FrameTreeNode implies that there's an
564 // ongoing navigation that hasn't reached the ReadyToCommit state. If the
565 // navigation is between ReadyToCommit and DidCommitNavigation, the
566 // NavigationRequest will be held by RenderFrameHost, which is checked below.
567 if (navigation_request_ && !navigation_request_->IsSameDocument())
568 return true;
569
570 // Having a speculative RenderFrameHost should imply a cross-document
571 // navigation.
572 if (render_manager_.speculative_frame_host())
573 return true;
574
575 return render_manager_.current_frame_host()
576 ->HasPendingCommitForCrossDocumentNavigation();
577}
578
Arthur Hemeryc3380172018-01-22 14:00:17579void FrameTreeNode::TransferNavigationRequestOwnership(
580 RenderFrameHostImpl* render_frame_host) {
Andrey Kosyakovf2d4ff72018-10-29 20:09:59581 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Arthur Hemeryc3380172018-01-22 14:00:17582 render_frame_host->SetNavigationRequest(std::move(navigation_request_));
583}
584
carloskc49005eb2015-06-16 11:25:07585void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57586 std::unique_ptr<NavigationRequest> navigation_request) {
arthursonzognic79c251c2016-08-18 15:00:37587 // This is never called when navigating to a Javascript URL. For the loading
588 // state, this matches what Blink is doing: Blink doesn't send throbber
589 // notifications for Javascript URLS.
590 DCHECK(!navigation_request->common_params().url.SchemeIs(
591 url::kJavaScriptScheme));
592
Yu Gaoc8c18552022-06-22 14:38:45593 bool was_previously_loading =
594 frame_tree()->LoadingTree()->IsLoadingIncludingInnerFrameTrees();
clamy44e84ce2016-02-22 15:38:25595
clamy82a2f4d2016-02-02 14:20:41596 // There's no need to reset the state: there's still an ongoing load, and the
597 // RenderFrameHostManager will take care of updates to the speculative
598 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05599 if (was_previously_loading) {
Mohamed Abdelhalimf03d4a22019-10-01 13:34:31600 if (navigation_request_ && navigation_request_->IsNavigationStarted()) {
jamcd0b7b22017-03-24 22:13:05601 // Mark the old request as aborted.
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52602 navigation_request_->set_net_error(net::ERR_ABORTED);
jamcd0b7b22017-03-24 22:13:05603 }
Daniel Cheng390e2a72022-09-28 06:07:53604 ResetNavigationRequestButKeepState();
jamcd0b7b22017-03-24 22:13:05605 }
clamy44e84ce2016-02-22 15:38:25606
607 navigation_request_ = std::move(navigation_request);
Shubhie Panickerddf2a4e2018-03-06 00:09:06608 if (was_discarded_) {
609 navigation_request_->set_was_discarded();
610 was_discarded_ = false;
611 }
clamy8e2e299202016-04-05 11:44:59612 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39613
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51614 bool to_different_document = !NavigationTypeUtils::IsSameDocument(
arthursonzogni92f18682017-02-08 23:00:04615 navigation_request_->common_params().navigation_type);
616
617 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16618}
619
Daniel Cheng390e2a72022-09-28 06:07:53620void FrameTreeNode::ResetNavigationRequest(NavigationDiscardReason reason) {
621 if (!navigation_request_)
622 return;
623
624 ResetNavigationRequestButKeepState();
625
626 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
627 // it created for the navigation. Also register that the load stopped.
628 DidStopLoading();
629 render_manager_.CleanUpNavigation(reason);
630}
631
632void FrameTreeNode::ResetNavigationRequestButKeepState() {
fdegans39ff0382015-04-29 19:04:39633 if (!navigation_request_)
634 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23635
Andrey Kosyakovf2d4ff72018-10-29 20:09:59636 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
clamydcb434c12015-04-16 19:29:16637 navigation_request_.reset();
638}
639
Nate Chapin9aabf5f2021-11-12 00:31:19640void FrameTreeNode::DidStartLoading(bool should_show_loading_ui,
clamy44e84ce2016-02-22 15:38:25641 bool was_previously_loading) {
Camille Lamyefd54b02018-10-04 16:54:14642 TRACE_EVENT2("navigation", "FrameTreeNode::DidStartLoading",
Nate Chapin9aabf5f2021-11-12 00:31:19643 "frame_tree_node", frame_tree_node_id(),
644 "should_show_loading_ui ", should_show_loading_ui);
Clark DuVallc97bcf72021-12-08 22:58:24645 base::ElapsedTimer timer;
fdegansa696e5112015-04-17 01:57:59646
Sreeja Kamishetty15f9944a22022-03-10 10:16:08647 frame_tree()->LoadingTree()->DidStartLoadingNode(
648 *this, should_show_loading_ui, was_previously_loading);
fdegansa696e5112015-04-17 01:57:59649
650 // Set initial load progress and update overall progress. This will notify
651 // the WebContents of the load progress change.
Sreeja Kamishetty15f9944a22022-03-10 10:16:08652 //
653 // Only notify when the load is triggered from primary/prerender main frame as
654 // we only update load progress for these nodes which happens when the frame
655 // tree matches the loading tree.
656 if (frame_tree() == frame_tree()->LoadingTree())
657 DidChangeLoadProgress(blink::kInitialLoadProgress);
fdegansa696e5112015-04-17 01:57:59658
Harkiran Bolaria3f83fba72022-03-10 17:48:40659 // Notify the proxies of the event.
660 current_frame_host()->browsing_context_state()->OnDidStartLoading();
Clark DuVallc97bcf72021-12-08 22:58:24661 base::UmaHistogramTimes(
662 base::StrCat({"Navigation.DidStartLoading.",
Ian Vollick5f45867c2022-08-05 08:29:56663 IsOutermostMainFrame() ? "MainFrame" : "Subframe"}),
Clark DuVallc97bcf72021-12-08 22:58:24664 timer.Elapsed());
fdegansa696e5112015-04-17 01:57:59665}
666
667void FrameTreeNode::DidStopLoading() {
Camille Lamyefd54b02018-10-04 16:54:14668 TRACE_EVENT1("navigation", "FrameTreeNode::DidStopLoading", "frame_tree_node",
669 frame_tree_node_id());
fdegansa696e5112015-04-17 01:57:59670 // Set final load progress and update overall progress. This will notify
671 // the WebContents of the load progress change.
Sreeja Kamishetty15f9944a22022-03-10 10:16:08672 //
673 // Only notify when the load is triggered from primary/prerender main frame as
674 // we only update load progress for these nodes which happens when the frame
675 // tree matches the loading tree.
676 if (frame_tree() == frame_tree()->LoadingTree())
677 DidChangeLoadProgress(blink::kFinalLoadProgress);
fdegansa696e5112015-04-17 01:57:59678
Harkiran Bolaria3f83fba72022-03-10 17:48:40679 // Notify the proxies of the event.
680 current_frame_host()->browsing_context_state()->OnDidStopLoading();
Lucas Furukawa Gadani6faef602019-05-06 21:16:03681
Sreeja Kamishetty15f9944a22022-03-10 10:16:08682 FrameTree* loading_tree = frame_tree()->LoadingTree();
683 // When loading tree is null, ignore invoking DidStopLoadingNode as the frame
684 // tree is already deleted. This can happen when prerendering gets cancelled
685 // and DidStopLoading is called during FrameTree destruction.
686 if (loading_tree)
687 loading_tree->DidStopLoadingNode(*this);
fdegansa696e5112015-04-17 01:57:59688}
689
690void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15691 DCHECK_GE(load_progress, blink::kInitialLoadProgress);
692 DCHECK_LE(load_progress, blink::kFinalLoadProgress);
693 current_frame_host()->DidChangeLoadProgress(load_progress);
fdegansa696e5112015-04-17 01:57:59694}
695
clamyf73862c42015-07-08 12:31:33696bool FrameTreeNode::StopLoading() {
arthursonzogni66f711c2019-10-08 14:40:36697 if (navigation_request_ && navigation_request_->IsNavigationStarted())
698 navigation_request_->set_net_error(net::ERR_ABORTED);
Daniel Cheng390e2a72022-09-28 06:07:53699 ResetNavigationRequest(NavigationDiscardReason::kCancelled);
clamyf73862c42015-07-08 12:31:33700
clamyf73862c42015-07-08 12:31:33701 if (!IsMainFrame())
702 return true;
703
704 render_manager_.Stop();
705 return true;
706}
707
alexmos21acae52015-11-07 01:04:43708void FrameTreeNode::DidFocus() {
709 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31710 for (auto& observer : observers_)
711 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43712}
713
clamy44e84ce2016-02-22 15:38:25714void FrameTreeNode::BeforeUnloadCanceled() {
Julie Jeongeun Kim8cf7ae32022-05-02 03:47:29715 // TODO(clamy): Support BeforeUnload in subframes. Fenced Frames don't run
716 // BeforeUnload. Maybe need to check whether other MPArch inner pages cases
717 // need beforeunload(e.g., portals, GuestView if it gets ported to MPArch).
Ian Vollick1c6dd3e2022-04-13 02:06:26718 if (!IsOutermostMainFrame())
clamy44e84ce2016-02-22 15:38:25719 return;
720
721 RenderFrameHostImpl* current_frame_host =
722 render_manager_.current_frame_host();
723 DCHECK(current_frame_host);
724 current_frame_host->ResetLoadingState();
725
clamy610c63b32017-12-22 15:05:18726 RenderFrameHostImpl* speculative_frame_host =
727 render_manager_.speculative_frame_host();
728 if (speculative_frame_host)
729 speculative_frame_host->ResetLoadingState();
Alexander Timin23c110b2021-01-14 02:39:04730 // Note: there is no need to set an error code on the NavigationHandle as
731 // the observers have not been notified about its creation.
732 // We also reset navigation request only when this navigation request was
733 // responsible for this dialog, as a new navigation request might cancel
734 // existing unrelated dialog.
Daniel Cheng390e2a72022-09-28 06:07:53735 if (navigation_request_ && navigation_request_->IsWaitingForBeforeUnload()) {
736 ResetNavigationRequest(NavigationDiscardReason::kCancelled);
737 }
clamy44e84ce2016-02-22 15:38:25738}
739
Mustaq Ahmedecb5c38e2020-07-29 00:34:30740bool FrameTreeNode::NotifyUserActivation(
741 blink::mojom::UserActivationNotificationType notification_type) {
Garrett Tanzer753cc532022-03-02 21:30:59742 // User activation notifications shouldn't propagate into/out of fenced
743 // frames.
744 // For ShadowDOM, fenced frames are in the same frame tree as their embedder,
745 // so we need to perform additional checks to enforce the boundary.
746 // For MPArch, fenced frames have a separate frame tree, so this boundary is
747 // enforced by default.
748 // https://docs.google.com/document/d/1WnIhXOFycoje_sEoZR3Mo0YNSR2Ki7LABIC_HEWFaog
749 bool shadow_dom_fenced_frame_enabled =
David Bokanc3fb5fa2022-07-04 14:55:31750 frame_tree()->IsFencedFramesShadowDOMBased();
Garrett Tanzer753cc532022-03-02 21:30:59751
Alex Moshchuk03904192021-04-02 07:29:08752 // User Activation V2 requires activating all ancestor frames in addition to
753 // the current frame. See
754 // https://html.spec.whatwg.org/multipage/interaction.html#tracking-user-activation.
Alexander Timina1dfadaa2020-04-28 13:30:06755 for (RenderFrameHostImpl* rfh = current_frame_host(); rfh;
756 rfh = rfh->GetParent()) {
John Delaneyb625dca92021-04-14 17:00:34757 rfh->DidReceiveUserActivation();
Mustaq Ahmedecb5c38e2020-07-29 00:34:30758 rfh->frame_tree_node()->user_activation_state_.Activate(notification_type);
Garrett Tanzer753cc532022-03-02 21:30:59759
760 if (shadow_dom_fenced_frame_enabled &&
761 rfh->frame_tree_node()->IsFencedFrameRoot()) {
762 break;
763 }
John Delaneyedd8d6c2019-01-25 00:23:57764 }
Alex Moshchuk03904192021-04-02 07:29:08765
Harkiran Bolaria0b3bdef02022-03-10 13:04:40766 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
767 true);
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14768
Garrett Tanzer753cc532022-03-02 21:30:59769 absl::optional<base::UnguessableToken> originator_nonce =
Garrett Tanzer34cb92fe2022-09-28 17:50:54770 GetFencedFrameNonce();
Garrett Tanzer753cc532022-03-02 21:30:59771
Mustaq Ahmed0180320f2019-03-21 16:07:01772 // See the "Same-origin Visibility" section in |UserActivationState| class
773 // doc.
Mustaq Ahmede5f12562019-10-30 18:02:03774 if (base::FeatureList::IsEnabled(
Garrett Tanzer753cc532022-03-02 21:30:59775 features::kUserActivationSameOriginVisibility)) {
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14776 const url::Origin& current_origin =
777 this->current_frame_host()->GetLastCommittedOrigin();
778 for (FrameTreeNode* node : frame_tree()->Nodes()) {
Garrett Tanzer753cc532022-03-02 21:30:59779 if (shadow_dom_fenced_frame_enabled &&
Garrett Tanzer34cb92fe2022-09-28 17:50:54780 node->GetFencedFrameNonce() != originator_nonce) {
Garrett Tanzer753cc532022-03-02 21:30:59781 continue;
782 }
783
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14784 if (node->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith(
785 current_origin)) {
Mustaq Ahmedecb5c38e2020-07-29 00:34:30786 node->user_activation_state_.Activate(notification_type);
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14787 }
788 }
789 }
790
Carlos Caballero40b0efd2021-01-26 11:55:00791 navigator().controller().NotifyUserActivation();
Alex Moshchuk03904192021-04-02 07:29:08792 current_frame_host()->MaybeIsolateForUserActivation();
Shivani Sharma194877032019-03-07 17:52:47793
Mustaq Ahmedc4cb7162018-06-05 16:28:36794 return true;
795}
796
797bool FrameTreeNode::ConsumeTransientUserActivation() {
Garrett Tanzer753cc532022-03-02 21:30:59798 // User activation consumptions shouldn't propagate into/out of fenced
799 // frames.
800 // For ShadowDOM, fenced frames are in the same frame tree as their embedder,
801 // so we need to perform additional checks to enforce the boundary.
802 // For MPArch, fenced frames have a separate frame tree, so this boundary is
803 // enforced by default.
804 // https://docs.google.com/document/d/1WnIhXOFycoje_sEoZR3Mo0YNSR2Ki7LABIC_HEWFaog
805 bool shadow_dom_fenced_frame_enabled =
David Bokanc3fb5fa2022-07-04 14:55:31806 frame_tree()->IsFencedFramesShadowDOMBased();
Garrett Tanzer753cc532022-03-02 21:30:59807 absl::optional<base::UnguessableToken> originator_nonce =
Garrett Tanzer34cb92fe2022-09-28 17:50:54808 GetFencedFrameNonce();
Garrett Tanzer753cc532022-03-02 21:30:59809
Mustaq Ahmedc4cb7162018-06-05 16:28:36810 bool was_active = user_activation_state_.IsActive();
Garrett Tanzer753cc532022-03-02 21:30:59811 for (FrameTreeNode* node : frame_tree()->Nodes()) {
812 if (shadow_dom_fenced_frame_enabled &&
Garrett Tanzer34cb92fe2022-09-28 17:50:54813 node->GetFencedFrameNonce() != originator_nonce) {
Garrett Tanzer753cc532022-03-02 21:30:59814 continue;
815 }
816
Mustaq Ahmedc4cb7162018-06-05 16:28:36817 node->user_activation_state_.ConsumeIfActive();
Garrett Tanzer753cc532022-03-02 21:30:59818 }
Harkiran Bolaria0b3bdef02022-03-10 13:04:40819 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
820 false);
Mustaq Ahmedc4cb7162018-06-05 16:28:36821 return was_active;
822}
823
Shivani Sharmac4f561582018-11-15 15:58:39824bool FrameTreeNode::ClearUserActivation() {
Shivani Sharmac4f561582018-11-15 15:58:39825 for (FrameTreeNode* node : frame_tree()->SubtreeNodes(this))
826 node->user_activation_state_.Clear();
Harkiran Bolaria0b3bdef02022-03-10 13:04:40827 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
828 false);
Shivani Sharmac4f561582018-11-15 15:58:39829 return true;
830}
831
Ella Ge9caed612019-08-09 16:17:25832bool FrameTreeNode::VerifyUserActivation() {
Ella Gea78f6772019-12-11 10:35:25833 DCHECK(base::FeatureList::IsEnabled(
834 features::kBrowserVerifiedUserActivationMouse) ||
835 base::FeatureList::IsEnabled(
836 features::kBrowserVerifiedUserActivationKeyboard));
837
Ella Ge9caed612019-08-09 16:17:25838 return render_manager_.current_frame_host()
839 ->GetRenderWidgetHost()
Mustaq Ahmed83bb1722019-10-22 20:00:10840 ->RemovePendingUserActivationIfAvailable();
Ella Ge9caed612019-08-09 16:17:25841}
842
Mustaq Ahmedc4cb7162018-06-05 16:28:36843bool FrameTreeNode::UpdateUserActivationState(
Mustaq Ahmeddc195e5b2020-08-04 18:45:11844 blink::mojom::UserActivationUpdateType update_type,
845 blink::mojom::UserActivationNotificationType notification_type) {
Ella Ge9caed612019-08-09 16:17:25846 bool update_result = false;
Mustaq Ahmedc4cb7162018-06-05 16:28:36847 switch (update_type) {
Antonio Gomes4b2c5132020-01-16 11:49:48848 case blink::mojom::UserActivationUpdateType::kConsumeTransientActivation:
Ella Ge9caed612019-08-09 16:17:25849 update_result = ConsumeTransientUserActivation();
850 break;
Antonio Gomes4b2c5132020-01-16 11:49:48851 case blink::mojom::UserActivationUpdateType::kNotifyActivation:
Mustaq Ahmeddc195e5b2020-08-04 18:45:11852 update_result = NotifyUserActivation(notification_type);
Ella Ge9caed612019-08-09 16:17:25853 break;
Antonio Gomes4b2c5132020-01-16 11:49:48854 case blink::mojom::UserActivationUpdateType::
Liviu Tintad9391fb92020-09-28 23:50:07855 kNotifyActivationPendingBrowserVerification: {
856 const bool user_activation_verified = VerifyUserActivation();
857 // Add UMA metric for when browser user activation verification succeeds
858 base::UmaHistogramBoolean("Event.BrowserVerifiedUserActivation",
859 user_activation_verified);
860 if (user_activation_verified) {
Mustaq Ahmedecb5c38e2020-07-29 00:34:30861 update_result = NotifyUserActivation(
Mustaq Ahmed2cfb0402020-09-29 19:24:35862 blink::mojom::UserActivationNotificationType::kInteraction);
Antonio Gomes4b2c5132020-01-16 11:49:48863 update_type = blink::mojom::UserActivationUpdateType::kNotifyActivation;
Ella Ge9caed612019-08-09 16:17:25864 } else {
arthursonzogni9816b9192021-03-29 16:09:19865 // TODO(https://crbug.com/848778): We need to decide what to do when
866 // user activation verification failed. NOTREACHED here will make all
Ella Ge9caed612019-08-09 16:17:25867 // unrelated tests that inject event to renderer fail.
868 return false;
869 }
Liviu Tintad9391fb92020-09-28 23:50:07870 } break;
Antonio Gomes4b2c5132020-01-16 11:49:48871 case blink::mojom::UserActivationUpdateType::kClearActivation:
Ella Ge9caed612019-08-09 16:17:25872 update_result = ClearUserActivation();
873 break;
Mustaq Ahmedc4cb7162018-06-05 16:28:36874 }
Mustaq Ahmeddc195e5b2020-08-04 18:45:11875 render_manager_.UpdateUserActivationState(update_type, notification_type);
Ella Ge9caed612019-08-09 16:17:25876 return update_result;
japhet61835ae12017-01-20 01:25:39877}
878
Arthur Sonzognif8840b92018-11-07 14:10:35879void FrameTreeNode::PruneChildFrameNavigationEntries(
880 NavigationEntryImpl* entry) {
881 for (size_t i = 0; i < current_frame_host()->child_count(); ++i) {
882 FrameTreeNode* child = current_frame_host()->child_at(i);
883 if (child->is_created_by_script_) {
884 entry->RemoveEntryForFrame(child,
885 /* only_if_different_position = */ false);
886 } else {
887 child->PruneChildFrameNavigationEntries(entry);
888 }
889 }
890}
891
arthursonzogni034bb9c2020-10-01 08:29:56892void FrameTreeNode::SetInitialPopupURL(const GURL& initial_popup_url) {
893 DCHECK(initial_popup_url_.is_empty());
Rakina Zata Amni4182b032021-11-01 04:22:01894 DCHECK(is_on_initial_empty_document_);
arthursonzogni034bb9c2020-10-01 08:29:56895 initial_popup_url_ = initial_popup_url;
896}
897
898void FrameTreeNode::SetPopupCreatorOrigin(
899 const url::Origin& popup_creator_origin) {
Rakina Zata Amni4182b032021-11-01 04:22:01900 DCHECK(is_on_initial_empty_document_);
arthursonzogni034bb9c2020-10-01 08:29:56901 popup_creator_origin_ = popup_creator_origin;
902}
903
Rakina Zata Amni4b1968d2021-09-09 03:29:47904void FrameTreeNode::WriteIntoTrace(
Alexander Timin074cd182022-03-23 18:11:22905 perfetto::TracedProto<TraceProto> proto) const {
Rakina Zata Amni4b1968d2021-09-09 03:29:47906 proto->set_frame_tree_node_id(frame_tree_node_id());
Alexander Timin074cd182022-03-23 18:11:22907 proto->set_is_main_frame(IsMainFrame());
908 proto.Set(TraceProto::kCurrentFrameHost, current_frame_host());
909 proto.Set(TraceProto::kSpeculativeFrameHost,
910 render_manager()->speculative_frame_host());
Rakina Zata Amni4b1968d2021-09-09 03:29:47911}
912
Carlos Caballero76711352021-03-24 17:38:21913bool FrameTreeNode::HasNavigation() {
914 if (navigation_request())
915 return true;
916
917 // Same-RenderFrameHost navigation is committing:
918 if (current_frame_host()->HasPendingCommitNavigation())
919 return true;
920
921 // Cross-RenderFrameHost navigation is committing:
922 if (render_manager()->speculative_frame_host())
923 return true;
924
925 return false;
926}
927
Dominic Farolino4bc10ee2021-08-31 00:37:36928bool FrameTreeNode::IsFencedFrameRoot() const {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58929 return fenced_frame_status_ == FencedFrameStatus::kFencedFrameRoot;
shivanigithubf3ddff52021-07-03 22:06:30930}
931
932bool FrameTreeNode::IsInFencedFrameTree() const {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58933 return fenced_frame_status_ != FencedFrameStatus::kNotNestedInFencedFrame;
shivanigithubf3ddff52021-07-03 22:06:30934}
935
Garrett Tanzer34cb92fe2022-09-28 17:50:54936const absl::optional<FencedFrameURLMapping::FencedFrameProperties>&
937FrameTreeNode::GetFencedFrameProperties() {
shivanigithub4cd016a2021-09-20 21:10:30938 if (!IsInFencedFrameTree()) {
Garrett Tanzer34cb92fe2022-09-28 17:50:54939 // If we might be in a urn iframe, try to find the "urn iframe root"
940 // and if it exists, return the attached `FencedFrameProperties`.
941 if (blink::features::IsAllowURNsInIframeEnabled()) {
942 FrameTreeNode* node = this;
943 while (node->parent()) {
944 CHECK(node->parent()->frame_tree_node());
945 if (node->fenced_frame_properties_.has_value()) {
946 return node->fenced_frame_properties_;
947 }
948 node = node->parent()->frame_tree_node();
949 }
950 }
951 return fenced_frame_properties_;
952 }
953
954 switch (blink::features::kFencedFramesImplementationTypeParam.Get()) {
955 case blink::features::FencedFramesImplementationType::kMPArch: {
956 // Because we already confirmed we're in a fenced frame tree, we know
957 // there must be a fenced frame root with properties stored.
958 CHECK(frame_tree());
959 CHECK(frame_tree()->root());
960 CHECK(frame_tree()->root()->fenced_frame_properties_.has_value());
961 return frame_tree()->root()->fenced_frame_properties_;
962 }
963 case blink::features::FencedFramesImplementationType::kShadowDOM: {
964 FrameTreeNode* node = this;
965
966 while (true) {
967 if (node->IsFencedFrameRoot()) {
968 // Because non-opaque url navigations in ShadowDOM fenced frames
969 // do not install fenced frame properties, this may be absl::nullopt
970 // underneath.
971 return node->fenced_frame_properties_;
972 }
973
974 CHECK(node->parent());
975 CHECK(node->parent()->frame_tree_node());
976 node = node->parent()->frame_tree_node();
977 }
978 }
979 }
980}
981
982absl::optional<base::UnguessableToken> FrameTreeNode::GetFencedFrameNonce() {
983 auto& root_fenced_frame_properties = GetFencedFrameProperties();
984 if (!root_fenced_frame_properties.has_value()) {
985 return absl::nullopt;
986 }
987 return root_fenced_frame_properties->partition_nonce;
988}
989
990void FrameTreeNode::SetFencedFramePropertiesIfNeeded() {
991 if (!IsFencedFrameRoot()) {
shivanigithub4cd016a2021-09-20 21:10:30992 return;
993 }
994
Garrett Tanzer34cb92fe2022-09-28 17:50:54995 // The fenced frame properties are set only on the fenced frame root.
996 // In the future, they will be set on the FrameTree instead.
997 fenced_frame_properties_ = FencedFrameURLMapping::FencedFrameProperties();
shivanigithub4cd016a2021-09-20 21:10:30998}
999
Nan Line376738a2022-03-25 22:05:411000absl::optional<blink::mojom::FencedFrameMode>
1001FrameTreeNode::GetFencedFrameMode() {
Garrett Tanzera42fdef2022-06-13 16:09:141002 if (!IsInFencedFrameTree()) {
Nan Line376738a2022-03-25 22:05:411003 return absl::nullopt;
Garrett Tanzera42fdef2022-06-13 16:09:141004 }
Nan Lin171fe9a2022-02-17 16:42:161005
Garrett Tanzera42fdef2022-06-13 16:09:141006 switch (blink::features::kFencedFramesImplementationTypeParam.Get()) {
1007 case blink::features::FencedFramesImplementationType::kMPArch: {
1008 FrameTreeNode* outer_delegate_node =
1009 render_manager()->GetOuterDelegateNode();
1010 DCHECK(outer_delegate_node);
Nan Lin171fe9a2022-02-17 16:42:161011
Garrett Tanzera42fdef2022-06-13 16:09:141012 FencedFrame* fenced_frame = FindFencedFrame(outer_delegate_node);
1013 DCHECK(fenced_frame);
Nan Line376738a2022-03-25 22:05:411014
Garrett Tanzera42fdef2022-06-13 16:09:141015 return fenced_frame->mode();
1016 }
1017 case blink::features::FencedFramesImplementationType::kShadowDOM: {
1018 FrameTreeNode* node = this;
1019 while (!node->IsFencedFrameRoot()) {
Liam Brady582b21bc82022-08-10 14:03:211020 node = node->parent()->frame_tree_node();
Garrett Tanzera42fdef2022-06-13 16:09:141021 }
1022 return node->pending_frame_policy_.fenced_frame_mode;
1023 }
1024 }
Nan Lin171fe9a2022-02-17 16:42:161025}
1026
Nan Linaaf84f72021-12-02 22:31:561027bool FrameTreeNode::IsErrorPageIsolationEnabled() const {
Nan Lind9de87d2022-03-18 16:53:031028 // Error page isolation is enabled for main frames only (crbug.com/1092524).
1029 // Note that this will also enable error page isolation for fenced frames in
1030 // MPArch mode, but not ShadowDOM mode.
1031 // See the issue in crbug.com/1264224#c7 for why it can't be enabled for
1032 // ShadowDOM mode.
1033 return SiteIsolationPolicy::IsErrorPageIsolationEnabled(IsMainFrame());
Nan Linaaf84f72021-12-02 22:31:561034}
1035
W. James MacLean81b8d01f2022-01-25 20:50:591036void FrameTreeNode::SetSrcdocValue(const std::string& srcdoc_value) {
1037 srcdoc_value_ = srcdoc_value;
1038}
1039
Garrett Tanzer2975eeac2022-08-22 16:34:011040absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:491041FrameTreeNode::FindSharedStorageBudgetMetadata() {
1042 FrameTreeNode* node = this;
1043
1044 while (true) {
Garrett Tanzer2975eeac2022-08-22 16:34:011045 if (node->fenced_frame_properties_ &&
1046 node->fenced_frame_properties_->shared_storage_budget_metadata) {
Yao Xiao1ac702d2022-06-08 17:20:491047 DCHECK(node->IsFencedFrameRoot());
Garrett Tanzer2975eeac2022-08-22 16:34:011048 return node->fenced_frame_properties_->shared_storage_budget_metadata;
Yao Xiao1ac702d2022-06-08 17:20:491049 }
1050
1051 if (node->GetParentOrOuterDocument()) {
1052 node = node->GetParentOrOuterDocument()->frame_tree_node();
1053 } else {
1054 break;
1055 }
1056 }
1057
Garrett Tanzer2975eeac2022-08-22 16:34:011058 return absl::nullopt;
Yao Xiao1ac702d2022-06-08 17:20:491059}
1060
Harkiran Bolariaebbe7702022-02-22 19:19:031061const scoped_refptr<BrowsingContextState>&
1062FrameTreeNode::GetBrowsingContextStateForSubframe() const {
1063 DCHECK(!IsMainFrame());
1064 return current_frame_host()->browsing_context_state();
1065}
1066
Arthur Hemerye4659282022-03-28 08:36:151067void FrameTreeNode::ClearOpenerReferences() {
1068 // Simulate the FrameTreeNode being dead to opener observers. They will
1069 // nullify their opener.
1070 // Note: observers remove themselves from observers_, no need to take care of
1071 // that manually.
1072 for (auto& observer : observers_)
1073 observer.OnFrameTreeNodeDisownedOpenee(this);
1074}
1075
Liam Bradyd2a41e152022-07-19 13:58:481076bool FrameTreeNode::AncestorOrSelfHasCSPEE() const {
1077 // Check if CSPEE is set in this frame or any ancestor frames.
Yuzu Saijo03dbf9b2022-07-22 04:29:451078 return csp_attribute() || (parent() && parent()->required_csp());
Liam Bradyd2a41e152022-07-19 13:58:481079}
1080
[email protected]9b159a52013-10-03 17:24:551081} // namespace content