blob: bdb6b4a3c23fd178e843dde1205fd7d400c45514 [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"
danakjc492bf82020-09-09 20:02:4423#include "content/browser/renderer_host/navigation_controller_impl.h"
24#include "content/browser/renderer_host/navigation_request.h"
25#include "content/browser/renderer_host/navigator.h"
26#include "content/browser/renderer_host/navigator_delegate.h"
27#include "content/browser/renderer_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4128#include "content/browser/renderer_host/render_view_host_impl.h"
Lucas Furukawa Gadanief8290a2019-07-29 20:27:5129#include "content/common/navigation_params_utils.h"
dmazzonie950ea232015-03-13 21:39:4530#include "content/public/browser/browser_thread.h"
Nan Linaaf84f72021-12-02 22:31:5631#include "content/public/browser/site_isolation_policy.h"
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1432#include "content/public/common/content_features.h"
arthursonzognib93a4472020-04-10 07:38:0033#include "services/network/public/cpp/web_sandbox_flags.h"
34#include "services/network/public/mojom/web_sandbox_flags.mojom-shared.h"
Harkiran Bolaria59290d62021-03-17 01:53:0135#include "third_party/blink/public/common/features.h"
Liam Bradyb0f1f0e2022-08-19 21:42:1136#include "third_party/blink/public/common/frame/fenced_frame_sandbox_flags.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1537#include "third_party/blink/public/common/loader/loader_constants.h"
Antonio Gomes4b2c5132020-01-16 11:49:4838#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
Julie Jeongeun Kimd90e2dd2020-03-03 11:45:3739#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
[email protected]9b159a52013-10-03 17:24:5540
41namespace content {
42
dmazzonie950ea232015-03-13 21:39:4543namespace {
44
45// This is a global map between frame_tree_node_ids and pointers to
46// FrameTreeNodes.
Takuto Ikutaadf31eb2019-01-05 00:32:4847typedef std::unordered_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4548
scottmg5e65e3a2017-03-08 08:48:4649base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
50 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4551
Nan Line376738a2022-03-25 22:05:4152FencedFrame* FindFencedFrame(const FrameTreeNode* frame_tree_node) {
53 // TODO(crbug.com/1123606): Consider having a pointer to `FencedFrame` in
54 // `FrameTreeNode` or having a map between them.
55
56 // Try and find the `FencedFrame` that `frame_tree_node` represents.
57 DCHECK(frame_tree_node->parent());
58 std::vector<FencedFrame*> fenced_frames =
59 frame_tree_node->parent()->GetFencedFrames();
60 for (FencedFrame* fenced_frame : fenced_frames) {
61 if (frame_tree_node->frame_tree_node_id() ==
62 fenced_frame->GetOuterDelegateFrameTreeNodeId()) {
63 return fenced_frame;
64 }
65 }
66 return nullptr;
67}
68
fdegansa696e5112015-04-17 01:57:5969} // namespace
fdegans1d16355162015-03-26 11:58:3470
alexmose201c7cd2015-06-10 17:14:2171// This observer watches the opener of its owner FrameTreeNode and clears the
Arthur Hemerye4659282022-03-28 08:36:1572// owner's opener if the opener is destroyed or swaps BrowsingInstance.
alexmose201c7cd2015-06-10 17:14:2173class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
74 public:
jochen6004a362017-02-04 00:11:4075 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
76 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2177
Peter Boström9b036532021-10-28 23:37:2878 OpenerDestroyedObserver(const OpenerDestroyedObserver&) = delete;
79 OpenerDestroyedObserver& operator=(const OpenerDestroyedObserver&) = delete;
80
alexmose201c7cd2015-06-10 17:14:2181 // FrameTreeNode::Observer
82 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
Arthur Hemerye4659282022-03-28 08:36:1583 NullifyOpener(node);
84 }
85
86 // FrameTreeNode::Observer
87 void OnFrameTreeNodeDisownedOpenee(FrameTreeNode* node) override {
88 NullifyOpener(node);
89 }
90
91 void NullifyOpener(FrameTreeNode* node) {
jochen6004a362017-02-04 00:11:4092 if (observing_original_opener_) {
Rakina Zata Amni3a48ae42022-05-05 03:39:5693 // The "original opener" is special. It's used for attribution, and
94 // clients walk down the original opener chain. Therefore, if a link in
95 // the chain is being destroyed, reconnect the observation to the parent
96 // of the link being destroyed.
97 CHECK_EQ(owner_->first_live_main_frame_in_original_opener_chain(), node);
98 owner_->SetOriginalOpener(
99 node->first_live_main_frame_in_original_opener_chain());
Avi Drissman36465f332017-09-11 20:49:39100 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40101 } else {
102 CHECK_EQ(owner_->opener(), node);
103 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:39104 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40105 }
alexmose201c7cd2015-06-10 17:14:21106 }
107
108 private:
Keishi Hattori0e45c022021-11-27 09:25:52109 raw_ptr<FrameTreeNode> owner_;
jochen6004a362017-02-04 00:11:40110 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:21111};
112
Kevin McNee88e61552020-10-22 20:41:11113const int FrameTreeNode::kFrameTreeNodeInvalidId = -1;
114
115static_assert(FrameTreeNode::kFrameTreeNodeInvalidId ==
116 RenderFrameHost::kNoFrameTreeNodeId,
117 "Have consistent sentinel values for an invalid FTN id.");
118
vishal.b782eb5d2015-04-29 12:22:57119int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:55120
dmazzonie950ea232015-03-13 21:39:45121// static
vishal.b782eb5d2015-04-29 12:22:57122FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:19123 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:15124 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
jdoerrie55ec69d2018-10-08 13:34:46125 auto it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:45126 return it == nodes->end() ? nullptr : it->second;
127}
128
Alexander Timin381e7e182020-04-28 19:04:03129// static
130FrameTreeNode* FrameTreeNode::From(RenderFrameHost* rfh) {
131 if (!rfh)
132 return nullptr;
133 return static_cast<RenderFrameHostImpl*>(rfh)->frame_tree_node();
134}
135
Abhijeet Kandalkarb43affa72022-09-27 16:48:01136FrameTreeNode::FencedFrameStatus ComputeFencedFrameStatus(
Harkiran Bolaria16f2c48d2022-04-22 12:39:57137 FrameTree* frame_tree,
138 RenderFrameHostImpl* parent,
139 const blink::FramePolicy& frame_policy) {
Abhijeet Kandalkarb43affa72022-09-27 16:48:01140 using FencedFrameStatus = FrameTreeNode::FencedFrameStatus;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57141 if (blink::features::IsFencedFramesEnabled()) {
142 switch (blink::features::kFencedFramesImplementationTypeParam.Get()) {
143 case blink::features::FencedFramesImplementationType::kMPArch: {
144 if (frame_tree->type() == FrameTree::Type::kFencedFrame) {
145 if (!parent)
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58146 return FencedFrameStatus::kFencedFrameRoot;
147 return FencedFrameStatus::kIframeNestedWithinFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57148 } else {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58149 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57150 }
151 }
152 case blink::features::FencedFramesImplementationType::kShadowDOM: {
153 // Different from the MPArch case, the ShadowDOM implementation of
154 // fenced frame lives in the same FrameTree as its parent, so we need to
155 // check its effective frame policy instead.
156 if (frame_policy.is_fenced) {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58157 return FencedFrameStatus::kFencedFrameRoot;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57158 } else if (parent && parent->frame_tree_node()->IsInFencedFrameTree()) {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58159 return FencedFrameStatus::kIframeNestedWithinFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57160 }
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58161 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57162 }
163 default: {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58164 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57165 }
166 }
167 }
168
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58169 return FencedFrameStatus::kNotNestedInFencedFrame;
Harkiran Bolaria16f2c48d2022-04-22 12:39:57170}
171
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54172FrameTreeNode::FrameTreeNode(
173 FrameTree* frame_tree,
Alexander Timin381e7e182020-04-28 19:04:03174 RenderFrameHostImpl* parent,
Daniel Cheng6ac128172021-05-25 18:49:01175 blink::mojom::TreeScopeType tree_scope_type,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54176 bool is_created_by_script,
177 const base::UnguessableToken& devtools_frame_token,
178 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
Kevin McNee43fe8292021-10-04 22:59:41179 blink::FrameOwnerElementType owner_type,
Dominic Farolino08662c82021-06-11 07:36:34180 const blink::FramePolicy& frame_policy)
[email protected]bffc8302014-01-23 20:52:16181 : frame_tree_(frame_tree),
[email protected]bffc8302014-01-23 20:52:16182 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59183 parent_(parent),
Daniel Cheng9bd90f92021-04-23 20:49:45184 frame_owner_element_type_(owner_type),
Daniel Cheng6ac128172021-05-25 18:49:01185 tree_scope_type_(tree_scope_type),
Dominic Farolino08662c82021-06-11 07:36:34186 pending_frame_policy_(frame_policy),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45187 is_created_by_script_(is_created_by_script),
Pavel Feldman25234722017-10-11 02:49:06188 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31189 frame_owner_properties_(frame_owner_properties),
Yuzu Saijo03dbf9b2022-07-22 04:29:45190 attributes_(blink::mojom::IframeAttributes::New()),
Harkiran Bolaria16f2c48d2022-04-22 12:39:57191 fenced_frame_status_(
192 ComputeFencedFrameStatus(frame_tree_, parent_, frame_policy)),
Harkiran Bolaria0b3bdef02022-03-10 13:04:40193 render_manager_(this, frame_tree->manager_delegate()) {
Harkiran Bolariaa8347782022-04-06 09:25:11194 TRACE_EVENT_BEGIN("navigation", "FrameTreeNode",
195 perfetto::Track::FromPointer(this),
196 "frame_tree_node_when_created", this);
rob97250742015-12-10 17:45:15197 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45198 g_frame_tree_node_id_map.Get().insert(
199 std::make_pair(frame_tree_node_id_, this));
200 CHECK(result.second);
alexmos998581d2015-01-22 01:01:59201}
[email protected]9b159a52013-10-03 17:24:55202
Dominic Farolino8a2187b2021-12-24 20:44:21203void FrameTreeNode::DestroyInnerFrameTreeIfExists() {
204 // If `this` is an dummy outer delegate node, then we really are representing
205 // an inner FrameTree for one of the following consumers:
206 // - `Portal`
207 // - `FencedFrame`
208 // - `GuestView`
209 // If we are representing a `FencedFrame` object, we need to destroy it
210 // alongside ourself. `Portals` and `GuestView` however, *currently* have a
211 // more complex lifetime and are dealt with separately.
212 bool is_outer_dummy_node = false;
213 if (current_frame_host() &&
214 current_frame_host()->inner_tree_main_frame_tree_node_id() !=
215 FrameTreeNode::kFrameTreeNodeInvalidId) {
216 is_outer_dummy_node = true;
217 }
218
219 if (is_outer_dummy_node) {
Nan Line376738a2022-03-25 22:05:41220 FencedFrame* doomed_fenced_frame = FindFencedFrame(this);
Dominic Farolino8a2187b2021-12-24 20:44:21221 // `doomed_fenced_frame` might not actually exist, because some outer dummy
222 // `FrameTreeNode`s might correspond to `Portal`s, which do not have their
223 // lifetime managed in the same way as `FencedFrames`.
224 if (doomed_fenced_frame) {
225 parent()->DestroyFencedFrame(*doomed_fenced_frame);
226 }
227 }
228}
229
[email protected]9b159a52013-10-03 17:24:55230FrameTreeNode::~FrameTreeNode() {
Harkiran Bolariaa8347782022-04-06 09:25:11231 TRACE_EVENT("navigation", "FrameTreeNode::~FrameTreeNode");
Daniel Chengc3d1e8d2021-06-23 02:11:45232 // There should always be a current RenderFrameHost except during prerender
233 // activation. Prerender activation moves the current RenderFrameHost from
234 // the old FrameTree's FrameTreeNode to the new FrameTree's FrameTreeNode and
235 // then destroys the old FrameTree. See
236 // `RenderFrameHostManager::TakePrerenderedPage()`.
Harkiran Bolaria59290d62021-03-17 01:53:01237 if (current_frame_host()) {
238 // Remove the children.
239 current_frame_host()->ResetChildren();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45240
Harkiran Bolaria59290d62021-03-17 01:53:01241 current_frame_host()->ResetLoadingState();
242 } else {
Hiroki Nakagawa0a90bd42021-04-21 01:53:05243 DCHECK(blink::features::IsPrerender2Enabled());
Harkiran Bolaria59290d62021-03-17 01:53:01244 DCHECK(!parent()); // Only main documents can be activated.
245 DCHECK(!opener()); // Prerendered frame trees can't have openers.
246
247 // Activation is not allowed during ongoing navigations.
248 DCHECK(!navigation_request_);
249
Carlos Caballerod1c80432021-04-20 08:16:32250 // TODO(https://crbug.com/1199693): Need to determine how to handle pending
Harkiran Bolaria59290d62021-03-17 01:53:01251 // deletions, as observers will be notified.
252 DCHECK(!render_manager()->speculative_frame_host());
253 }
Nate Chapin22ea6592019-03-05 22:29:02254
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45255 // If the removed frame was created by a script, then its history entry will
256 // never be reused - we can save some memory by removing the history entry.
257 // See also https://crbug.com/784356.
258 if (is_created_by_script_ && parent_) {
Carlos Caballero04aab362021-02-15 17:38:16259 NavigationEntryImpl* nav_entry =
260 navigator().controller().GetLastCommittedEntry();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45261 if (nav_entry) {
262 nav_entry->RemoveEntryForFrame(this,
263 /* only_if_different_position = */ false);
264 }
265 }
266
dmazzonie950ea232015-03-13 21:39:45267 frame_tree_->FrameRemoved(this);
Carlos Caballero6ff6ace2021-02-05 16:53:00268
Dominic Farolino8a2187b2021-12-24 20:44:21269 DestroyInnerFrameTreeIfExists();
270
Carlos Caballero6ff6ace2021-02-05 16:53:00271 // Do not dispatch notification for the root frame as ~WebContentsImpl already
272 // dispatches it for now.
273 // TODO(https://crbug.com/1170277): This is only needed because the FrameTree
274 // is a member of WebContentsImpl and we would call back into it during
275 // destruction. We should clean up the FrameTree destruction code and call the
276 // delegate unconditionally.
277 if (parent())
278 render_manager_.delegate()->OnFrameTreeNodeDestroyed(this);
279
ericwilligers254597b2016-10-17 10:32:31280 for (auto& observer : observers_)
281 observer.OnFrameTreeNodeDestroyed(this);
Lukasz Anforowicz147141962020-12-16 18:03:24282 observers_.Clear();
alexmose201c7cd2015-06-10 17:14:21283
284 if (opener_)
285 opener_->RemoveObserver(opener_observer_.get());
Rakina Zata Amni3a48ae42022-05-05 03:39:56286 if (first_live_main_frame_in_original_opener_chain_)
287 first_live_main_frame_in_original_opener_chain_->RemoveObserver(
288 original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45289
290 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18291
Daniel Chengc3d1e8d2021-06-23 02:11:45292 // If a frame with a pending navigation is detached, make sure the
293 // WebContents (and its observers) update their loading state.
294 // TODO(dcheng): This should just check `IsLoading()`, but `IsLoading()`
295 // assumes that `current_frame_host_` is not null. This is incompatible with
296 // prerender activation when destroying the old frame tree (see above).
danakjf9400602019-06-07 15:44:58297 bool did_stop_loading = false;
298
jam39258caf2016-11-02 14:48:18299 if (navigation_request_) {
danakjf9400602019-06-07 15:44:58300 navigation_request_.reset();
danakjf9400602019-06-07 15:44:58301 did_stop_loading = true;
jam39258caf2016-11-02 14:48:18302 }
Nate Chapin22ea6592019-03-05 22:29:02303
danakjf9400602019-06-07 15:44:58304 // ~SiteProcessCountTracker DCHECKs in some tests if the speculative
305 // RenderFrameHostImpl is not destroyed last. Ideally this would be closer to
306 // (possible before) the ResetLoadingState() call above.
danakjf9400602019-06-07 15:44:58307 if (render_manager_.speculative_frame_host()) {
Daniel Chengc3d1e8d2021-06-23 02:11:45308 // TODO(dcheng): Shouldn't a FrameTreeNode with a speculative
309 // RenderFrameHost always be considered loading?
danakjf9400602019-06-07 15:44:58310 did_stop_loading |= render_manager_.speculative_frame_host()->is_loading();
Daniel Chengc3d1e8d2021-06-23 02:11:45311 // `FrameTree::Shutdown()` has special handling for the main frame's
312 // speculative RenderFrameHost, and the speculative RenderFrameHost should
313 // already be reset for main frames.
314 DCHECK(!IsMainFrame());
315
316 // This does not use `UnsetSpeculativeRenderFrameHost()`: if the speculative
317 // RenderFrameHost has already reached kPendingCommit, it would needlessly
318 // re-create a proxy for a frame that's going away.
319 render_manager_.DiscardSpeculativeRenderFrameHostForShutdown();
danakjf9400602019-06-07 15:44:58320 }
321
322 if (did_stop_loading)
323 DidStopLoading();
324
Harkiran Bolaria59290d62021-03-17 01:53:01325 // IsLoading() requires that current_frame_host() is non-null.
326 DCHECK(!current_frame_host() || !IsLoading());
Harkiran Bolariaa8347782022-04-06 09:25:11327
328 // Matches the TRACE_EVENT_BEGIN in the constructor.
329 TRACE_EVENT_END("navigation", perfetto::Track::FromPointer(this));
[email protected]9b159a52013-10-03 17:24:55330}
331
alexmose201c7cd2015-06-10 17:14:21332void FrameTreeNode::AddObserver(Observer* observer) {
333 observers_.AddObserver(observer);
334}
335
336void FrameTreeNode::RemoveObserver(Observer* observer) {
337 observers_.RemoveObserver(observer);
338}
339
[email protected]94d0cc12013-12-18 00:07:41340bool FrameTreeNode::IsMainFrame() const {
341 return frame_tree_->root() == this;
342}
343
Ian Vollick25a9d032022-04-12 23:20:17344bool FrameTreeNode::IsOutermostMainFrame() {
345 return !GetParentOrOuterDocument();
346}
347
Hiroki Nakagawaab309622021-05-19 16:38:13348void FrameTreeNode::ResetForNavigation() {
arthursonzogni76098e52020-11-25 14:18:45349 // This frame has had its user activation bits cleared in the renderer before
350 // arriving here. We just need to clear them here and in the other renderer
351 // processes that may have a reference to this frame.
Alexander Timin45b716c2020-11-06 01:40:31352 //
353 // We do not take user activation into account when calculating
354 // |ResetForNavigationResult|, as we are using it to determine bfcache
355 // eligibility and the page can get another user gesture after restore.
Antonio Gomes4b2c5132020-01-16 11:49:48356 UpdateUserActivationState(
Mustaq Ahmeddc195e5b2020-08-04 18:45:11357 blink::mojom::UserActivationUpdateType::kClearActivation,
358 blink::mojom::UserActivationNotificationType::kNone);
Ian Clelland5cbaaf82017-11-27 22:00:03359}
360
Dave Tapuskac8de3b02021-12-03 21:51:01361RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocument() {
362 return GetParentOrOuterDocumentHelper(/*escape_guest_view=*/false);
363}
364
365RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocumentOrEmbedder() {
366 return GetParentOrOuterDocumentHelper(/*escape_guest_view=*/true);
367}
368
369RenderFrameHostImpl* FrameTreeNode::GetParentOrOuterDocumentHelper(
370 bool escape_guest_view) {
371 // Find the parent in the FrameTree (iframe).
372 if (parent_)
373 return parent_;
374
375 if (!escape_guest_view) {
376 // If we are not a fenced frame root nor inside a portal then return early.
377 // This code does not escape GuestViews.
378 if (!IsFencedFrameRoot() && !frame_tree_->delegate()->IsPortal())
379 return nullptr;
380 }
381
382 // Find the parent in the outer embedder (GuestView, Portal, or Fenced Frame).
383 FrameTreeNode* frame_in_embedder = render_manager()->GetOuterDelegateNode();
384 if (frame_in_embedder)
385 return frame_in_embedder->current_frame_host()->GetParent();
386
387 // No parent found.
388 return nullptr;
389}
390
Julie Jeongeun Kimf38c1eca2021-12-14 07:46:55391FrameType FrameTreeNode::GetFrameType() const {
392 if (!IsMainFrame())
393 return FrameType::kSubframe;
394
395 switch (frame_tree()->type()) {
396 case FrameTree::Type::kPrimary:
397 return FrameType::kPrimaryMainFrame;
398 case FrameTree::Type::kPrerender:
399 return FrameType::kPrerenderMainFrame;
400 case FrameTree::Type::kFencedFrame:
401 // We also have FencedFramesImplementationType::kShadowDOM for a
402 // fenced frame implementation based on <iframe> + shadowDOM,
403 // which will return kSubframe as it's a modified <iframe> rather
404 // than a dedicated FrameTree. This returns kSubframe for the
405 // shadow dom implementation in order to keep consistency (i.e.
406 // NavigationHandle::GetParentFrame returning non-null value for
407 // shadow-dom based FFs).
408 return FrameType::kFencedFrameRoot;
409 }
410}
411
alexmose201c7cd2015-06-10 17:14:21412void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
Harkiran Bolariaa8347782022-04-06 09:25:11413 TRACE_EVENT("navigation", "FrameTreeNode::SetOpener",
414 ChromeTrackEvent::kFrameTreeNodeInfo, opener);
alexmose201c7cd2015-06-10 17:14:21415 if (opener_) {
416 opener_->RemoveObserver(opener_observer_.get());
417 opener_observer_.reset();
418 }
419
420 opener_ = opener;
421
422 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55423 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21424 opener_->AddObserver(opener_observer_.get());
425 }
426}
427
Wolfgang Beyerd8809db2020-09-30 15:29:39428void FrameTreeNode::SetOpenerDevtoolsFrameToken(
429 base::UnguessableToken opener_devtools_frame_token) {
430 DCHECK(!opener_devtools_frame_token_ ||
431 opener_devtools_frame_token_->is_empty());
432 opener_devtools_frame_token_ = std::move(opener_devtools_frame_token);
433}
434
jochen6004a362017-02-04 00:11:40435void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39436 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37437 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40438
Rakina Zata Amni3a48ae42022-05-05 03:39:56439 if (first_live_main_frame_in_original_opener_chain_) {
440 first_live_main_frame_in_original_opener_chain_->RemoveObserver(
441 original_opener_observer_.get());
Avi Drissman36465f332017-09-11 20:49:39442 original_opener_observer_.reset();
443 }
444
Rakina Zata Amni3a48ae42022-05-05 03:39:56445 first_live_main_frame_in_original_opener_chain_ = opener;
jochen6004a362017-02-04 00:11:40446
Rakina Zata Amni3a48ae42022-05-05 03:39:56447 if (first_live_main_frame_in_original_opener_chain_) {
448 original_opener_observer_ = std::make_unique<OpenerDestroyedObserver>(
449 this, true /* observing_original_opener */);
450 first_live_main_frame_in_original_opener_chain_->AddObserver(
451 original_opener_observer_.get());
jochen6004a362017-02-04 00:11:40452 }
453}
454
engedy6e2e0992017-05-25 18:58:42455void FrameTreeNode::SetCollapsed(bool collapsed) {
Dave Tapuska65e50aa2022-03-09 23:44:13456 DCHECK(!IsMainFrame() || IsFencedFrameRoot());
engedy6e2e0992017-05-25 18:58:42457 if (is_collapsed_ == collapsed)
458 return;
459
460 is_collapsed_ = collapsed;
461 render_manager_.OnDidChangeCollapsedState(collapsed);
462}
463
Harkiran Bolaria59290d62021-03-17 01:53:01464void FrameTreeNode::SetFrameTree(FrameTree& frame_tree) {
Hiroki Nakagawa0a90bd42021-04-21 01:53:05465 DCHECK(blink::features::IsPrerender2Enabled());
Harkiran Bolaria59290d62021-03-17 01:53:01466 frame_tree_ = &frame_tree;
Kevin McNeeb110d0c2021-10-26 15:53:00467 DCHECK(current_frame_host());
468 current_frame_host()->SetFrameTree(frame_tree);
469 RenderFrameHostImpl* speculative_frame_host =
470 render_manager_.speculative_frame_host();
471 if (speculative_frame_host)
472 speculative_frame_host->SetFrameTree(frame_tree);
Harkiran Bolaria59290d62021-03-17 01:53:01473}
474
Luna Luc3fdacdf2017-11-08 04:48:53475void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Dominic Farolinobfd1f0292022-03-23 19:12:24476 // The `is_fenced` and `fenced_frame_mode` bits should never be able to
477 // transition from their initial values. Since we never expect to be in a
478 // position where it can even be updated to new value, if we catch this
479 // happening we have to kill the renderer and refuse to accept any other frame
480 // policy changes here.
481 if (pending_frame_policy_.is_fenced != frame_policy.is_fenced ||
482 pending_frame_policy_.fenced_frame_mode !=
483 frame_policy.fenced_frame_mode) {
Dominic Farolino08662c82021-06-11 07:36:34484 mojo::ReportBadMessage(
Dominic Farolinobfd1f0292022-03-23 19:12:24485 "FramePolicy properties dealing with fenced frames are considered "
486 "immutable, and therefore should never be changed by the renderer.");
Dominic Farolino08662c82021-06-11 07:36:34487 return;
488 }
489
Liam Bradyb0f1f0e2022-08-19 21:42:11490 // Inside of a fenced frame, the sandbox flags should not be able to change
491 // from its initial value. If the flags change, we have to assume the change
492 // came from a compromised renderer and terminate it.
493 // We will only do the check if the sandbox flags are already set to
494 // kFencedFrameForcedSandboxFlags. This is to allow the sandbox flags to
495 // be set initially (go from kNone -> kFencedFrameForcedSandboxFlags). Once
496 // it has been set, it cannot change to another value.
497 // Note: The bad message is only expected to hit for ShadowDOM fenced frames.
498 // For MPArch, the RFHI will detect that the change is not coming from the
499 // frame's parent in DidChangeFramePolicy() (an MPArch fenced frame parent
500 // is null since it's the root frame in its tree) and terminate the
501 // renderer before we reach this point.
502 // TODO(crbug.com/1262022) When ShadowDOM is removed, turn this into a DCHECK
503 // and remove the BadMessage call.
504 if (IsFencedFrameRoot() &&
505 pending_frame_policy_.sandbox_flags ==
506 blink::kFencedFrameForcedSandboxFlags &&
507 frame_policy.sandbox_flags != blink::kFencedFrameForcedSandboxFlags) {
508 DCHECK(frame_tree()->IsFencedFramesShadowDOMBased());
509 bad_message::ReceivedBadMessage(
510 current_frame_host()->GetProcess(),
511 bad_message::FF_FROZEN_SANDBOX_FLAGS_CHANGED);
512 return;
513 }
514
Ian Clellandcdc4f312017-10-13 22:24:12515 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25516
Ian Clellandcdc4f312017-10-13 22:24:12517 if (parent()) {
518 // Subframes should always inherit their parent's sandbox flags.
Alexander Timin381e7e182020-04-28 19:04:03519 pending_frame_policy_.sandbox_flags |=
Harkiran Bolaria4eacb3a2021-12-13 20:03:47520 parent()->browsing_context_state()->active_sandbox_flags();
Charlie Hue1b77ac2019-12-13 21:30:17521 // This is only applied on subframes; container policy and required document
522 // policy are not mutable on main frame.
Ian Clellandcdc4f312017-10-13 22:24:12523 pending_frame_policy_.container_policy = frame_policy.container_policy;
Charlie Hue1b77ac2019-12-13 21:30:17524 pending_frame_policy_.required_document_policy =
525 frame_policy.required_document_policy;
Ian Clellandcdc4f312017-10-13 22:24:12526 }
iclelland92f8c0b2017-04-19 12:43:05527}
528
Yuzu Saijo03dbf9b2022-07-22 04:29:45529void FrameTreeNode::SetAttributes(
530 blink::mojom::IframeAttributesPtr attributes) {
531 if (!anonymous() && attributes->anonymous) {
532 // Log this only when anonymous is changed to true.
Arthur Sonzogni2e9c6111392022-05-02 08:37:13533 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
534 parent_, blink::mojom::WebFeature::kAnonymousIframe);
535 }
Yuzu Saijo03dbf9b2022-07-22 04:29:45536 attributes_ = std::move(attributes);
Arthur Sonzogni2e9c6111392022-05-02 08:37:13537}
538
fdegans4a49ce932015-03-12 17:11:37539bool FrameTreeNode::IsLoading() const {
540 RenderFrameHostImpl* current_frame_host =
541 render_manager_.current_frame_host();
fdegans4a49ce932015-03-12 17:11:37542
543 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39544
clamy610c63b32017-12-22 15:05:18545 if (navigation_request_)
546 return true;
clamy11e11512015-07-07 16:42:17547
clamy610c63b32017-12-22 15:05:18548 RenderFrameHostImpl* speculative_frame_host =
549 render_manager_.speculative_frame_host();
Daniel Chengc3d1e8d2021-06-23 02:11:45550 // TODO(dcheng): Shouldn't a FrameTreeNode with a speculative RenderFrameHost
551 // always be considered loading?
clamy610c63b32017-12-22 15:05:18552 if (speculative_frame_host && speculative_frame_host->is_loading())
553 return true;
fdegans4a49ce932015-03-12 17:11:37554 return current_frame_host->is_loading();
555}
556
Alex Moshchuk9b0fd822020-10-26 23:08:15557bool FrameTreeNode::HasPendingCrossDocumentNavigation() const {
558 // Having a |navigation_request_| on FrameTreeNode implies that there's an
559 // ongoing navigation that hasn't reached the ReadyToCommit state. If the
560 // navigation is between ReadyToCommit and DidCommitNavigation, the
561 // NavigationRequest will be held by RenderFrameHost, which is checked below.
562 if (navigation_request_ && !navigation_request_->IsSameDocument())
563 return true;
564
565 // Having a speculative RenderFrameHost should imply a cross-document
566 // navigation.
567 if (render_manager_.speculative_frame_host())
568 return true;
569
570 return render_manager_.current_frame_host()
571 ->HasPendingCommitForCrossDocumentNavigation();
572}
573
Arthur Hemeryc3380172018-01-22 14:00:17574void FrameTreeNode::TransferNavigationRequestOwnership(
575 RenderFrameHostImpl* render_frame_host) {
Andrey Kosyakovf2d4ff72018-10-29 20:09:59576 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Arthur Hemeryc3380172018-01-22 14:00:17577 render_frame_host->SetNavigationRequest(std::move(navigation_request_));
578}
579
carloskc49005eb2015-06-16 11:25:07580void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57581 std::unique_ptr<NavigationRequest> navigation_request) {
arthursonzognic79c251c2016-08-18 15:00:37582 // This is never called when navigating to a Javascript URL. For the loading
583 // state, this matches what Blink is doing: Blink doesn't send throbber
584 // notifications for Javascript URLS.
585 DCHECK(!navigation_request->common_params().url.SchemeIs(
586 url::kJavaScriptScheme));
587
Yu Gaoc8c18552022-06-22 14:38:45588 bool was_previously_loading =
589 frame_tree()->LoadingTree()->IsLoadingIncludingInnerFrameTrees();
clamy44e84ce2016-02-22 15:38:25590
clamy82a2f4d2016-02-02 14:20:41591 // There's no need to reset the state: there's still an ongoing load, and the
592 // RenderFrameHostManager will take care of updates to the speculative
593 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05594 if (was_previously_loading) {
Mohamed Abdelhalimf03d4a22019-10-01 13:34:31595 if (navigation_request_ && navigation_request_->IsNavigationStarted()) {
jamcd0b7b22017-03-24 22:13:05596 // Mark the old request as aborted.
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52597 navigation_request_->set_net_error(net::ERR_ABORTED);
jamcd0b7b22017-03-24 22:13:05598 }
Daniel Cheng390e2a72022-09-28 06:07:53599 ResetNavigationRequestButKeepState();
jamcd0b7b22017-03-24 22:13:05600 }
clamy44e84ce2016-02-22 15:38:25601
602 navigation_request_ = std::move(navigation_request);
Shubhie Panickerddf2a4e2018-03-06 00:09:06603 if (was_discarded_) {
604 navigation_request_->set_was_discarded();
605 was_discarded_ = false;
606 }
clamy8e2e299202016-04-05 11:44:59607 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39608
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51609 bool to_different_document = !NavigationTypeUtils::IsSameDocument(
arthursonzogni92f18682017-02-08 23:00:04610 navigation_request_->common_params().navigation_type);
611
612 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16613}
614
Daniel Cheng390e2a72022-09-28 06:07:53615void FrameTreeNode::ResetNavigationRequest(NavigationDiscardReason reason) {
616 if (!navigation_request_)
617 return;
618
619 ResetNavigationRequestButKeepState();
620
621 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
622 // it created for the navigation. Also register that the load stopped.
623 DidStopLoading();
624 render_manager_.CleanUpNavigation(reason);
625}
626
627void FrameTreeNode::ResetNavigationRequestButKeepState() {
fdegans39ff0382015-04-29 19:04:39628 if (!navigation_request_)
629 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23630
Andrey Kosyakovf2d4ff72018-10-29 20:09:59631 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
clamydcb434c12015-04-16 19:29:16632 navigation_request_.reset();
633}
634
Nate Chapin9aabf5f2021-11-12 00:31:19635void FrameTreeNode::DidStartLoading(bool should_show_loading_ui,
clamy44e84ce2016-02-22 15:38:25636 bool was_previously_loading) {
Camille Lamyefd54b02018-10-04 16:54:14637 TRACE_EVENT2("navigation", "FrameTreeNode::DidStartLoading",
Nate Chapin9aabf5f2021-11-12 00:31:19638 "frame_tree_node", frame_tree_node_id(),
639 "should_show_loading_ui ", should_show_loading_ui);
Clark DuVallc97bcf72021-12-08 22:58:24640 base::ElapsedTimer timer;
fdegansa696e5112015-04-17 01:57:59641
Sreeja Kamishetty15f9944a22022-03-10 10:16:08642 frame_tree()->LoadingTree()->DidStartLoadingNode(
643 *this, should_show_loading_ui, was_previously_loading);
fdegansa696e5112015-04-17 01:57:59644
645 // Set initial load progress and update overall progress. This will notify
646 // the WebContents of the load progress change.
Sreeja Kamishetty15f9944a22022-03-10 10:16:08647 //
648 // Only notify when the load is triggered from primary/prerender main frame as
649 // we only update load progress for these nodes which happens when the frame
650 // tree matches the loading tree.
651 if (frame_tree() == frame_tree()->LoadingTree())
652 DidChangeLoadProgress(blink::kInitialLoadProgress);
fdegansa696e5112015-04-17 01:57:59653
Harkiran Bolaria3f83fba72022-03-10 17:48:40654 // Notify the proxies of the event.
655 current_frame_host()->browsing_context_state()->OnDidStartLoading();
Clark DuVallc97bcf72021-12-08 22:58:24656 base::UmaHistogramTimes(
657 base::StrCat({"Navigation.DidStartLoading.",
Ian Vollick5f45867c2022-08-05 08:29:56658 IsOutermostMainFrame() ? "MainFrame" : "Subframe"}),
Clark DuVallc97bcf72021-12-08 22:58:24659 timer.Elapsed());
fdegansa696e5112015-04-17 01:57:59660}
661
662void FrameTreeNode::DidStopLoading() {
Camille Lamyefd54b02018-10-04 16:54:14663 TRACE_EVENT1("navigation", "FrameTreeNode::DidStopLoading", "frame_tree_node",
664 frame_tree_node_id());
fdegansa696e5112015-04-17 01:57:59665 // Set final load progress and update overall progress. This will notify
666 // the WebContents of the load progress change.
Sreeja Kamishetty15f9944a22022-03-10 10:16:08667 //
668 // Only notify when the load is triggered from primary/prerender main frame as
669 // we only update load progress for these nodes which happens when the frame
670 // tree matches the loading tree.
671 if (frame_tree() == frame_tree()->LoadingTree())
672 DidChangeLoadProgress(blink::kFinalLoadProgress);
fdegansa696e5112015-04-17 01:57:59673
Harkiran Bolaria3f83fba72022-03-10 17:48:40674 // Notify the proxies of the event.
675 current_frame_host()->browsing_context_state()->OnDidStopLoading();
Lucas Furukawa Gadani6faef602019-05-06 21:16:03676
Sreeja Kamishetty15f9944a22022-03-10 10:16:08677 FrameTree* loading_tree = frame_tree()->LoadingTree();
678 // When loading tree is null, ignore invoking DidStopLoadingNode as the frame
679 // tree is already deleted. This can happen when prerendering gets cancelled
680 // and DidStopLoading is called during FrameTree destruction.
681 if (loading_tree)
682 loading_tree->DidStopLoadingNode(*this);
fdegansa696e5112015-04-17 01:57:59683}
684
685void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15686 DCHECK_GE(load_progress, blink::kInitialLoadProgress);
687 DCHECK_LE(load_progress, blink::kFinalLoadProgress);
688 current_frame_host()->DidChangeLoadProgress(load_progress);
fdegansa696e5112015-04-17 01:57:59689}
690
clamyf73862c42015-07-08 12:31:33691bool FrameTreeNode::StopLoading() {
arthursonzogni66f711c2019-10-08 14:40:36692 if (navigation_request_ && navigation_request_->IsNavigationStarted())
693 navigation_request_->set_net_error(net::ERR_ABORTED);
Daniel Cheng390e2a72022-09-28 06:07:53694 ResetNavigationRequest(NavigationDiscardReason::kCancelled);
clamyf73862c42015-07-08 12:31:33695
clamyf73862c42015-07-08 12:31:33696 if (!IsMainFrame())
697 return true;
698
699 render_manager_.Stop();
700 return true;
701}
702
alexmos21acae52015-11-07 01:04:43703void FrameTreeNode::DidFocus() {
704 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31705 for (auto& observer : observers_)
706 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43707}
708
clamy44e84ce2016-02-22 15:38:25709void FrameTreeNode::BeforeUnloadCanceled() {
Julie Jeongeun Kim8cf7ae32022-05-02 03:47:29710 // TODO(clamy): Support BeforeUnload in subframes. Fenced Frames don't run
711 // BeforeUnload. Maybe need to check whether other MPArch inner pages cases
712 // need beforeunload(e.g., portals, GuestView if it gets ported to MPArch).
Ian Vollick1c6dd3e2022-04-13 02:06:26713 if (!IsOutermostMainFrame())
clamy44e84ce2016-02-22 15:38:25714 return;
715
716 RenderFrameHostImpl* current_frame_host =
717 render_manager_.current_frame_host();
718 DCHECK(current_frame_host);
719 current_frame_host->ResetLoadingState();
720
clamy610c63b32017-12-22 15:05:18721 RenderFrameHostImpl* speculative_frame_host =
722 render_manager_.speculative_frame_host();
723 if (speculative_frame_host)
724 speculative_frame_host->ResetLoadingState();
Alexander Timin23c110b2021-01-14 02:39:04725 // Note: there is no need to set an error code on the NavigationHandle as
726 // the observers have not been notified about its creation.
727 // We also reset navigation request only when this navigation request was
728 // responsible for this dialog, as a new navigation request might cancel
729 // existing unrelated dialog.
Daniel Cheng390e2a72022-09-28 06:07:53730 if (navigation_request_ && navigation_request_->IsWaitingForBeforeUnload()) {
731 ResetNavigationRequest(NavigationDiscardReason::kCancelled);
732 }
clamy44e84ce2016-02-22 15:38:25733}
734
Mustaq Ahmedecb5c38e2020-07-29 00:34:30735bool FrameTreeNode::NotifyUserActivation(
736 blink::mojom::UserActivationNotificationType notification_type) {
Garrett Tanzer753cc532022-03-02 21:30:59737 // User activation notifications shouldn't propagate into/out of fenced
738 // frames.
739 // For ShadowDOM, fenced frames are in the same frame tree as their embedder,
740 // so we need to perform additional checks to enforce the boundary.
741 // For MPArch, fenced frames have a separate frame tree, so this boundary is
742 // enforced by default.
743 // https://docs.google.com/document/d/1WnIhXOFycoje_sEoZR3Mo0YNSR2Ki7LABIC_HEWFaog
744 bool shadow_dom_fenced_frame_enabled =
David Bokanc3fb5fa2022-07-04 14:55:31745 frame_tree()->IsFencedFramesShadowDOMBased();
Garrett Tanzer753cc532022-03-02 21:30:59746
Alex Moshchuk03904192021-04-02 07:29:08747 // User Activation V2 requires activating all ancestor frames in addition to
748 // the current frame. See
749 // https://html.spec.whatwg.org/multipage/interaction.html#tracking-user-activation.
Alexander Timina1dfadaa2020-04-28 13:30:06750 for (RenderFrameHostImpl* rfh = current_frame_host(); rfh;
751 rfh = rfh->GetParent()) {
John Delaneyb625dca92021-04-14 17:00:34752 rfh->DidReceiveUserActivation();
Mustaq Ahmedecb5c38e2020-07-29 00:34:30753 rfh->frame_tree_node()->user_activation_state_.Activate(notification_type);
Garrett Tanzer753cc532022-03-02 21:30:59754
755 if (shadow_dom_fenced_frame_enabled &&
756 rfh->frame_tree_node()->IsFencedFrameRoot()) {
757 break;
758 }
John Delaneyedd8d6c2019-01-25 00:23:57759 }
Alex Moshchuk03904192021-04-02 07:29:08760
Harkiran Bolaria0b3bdef02022-03-10 13:04:40761 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
762 true);
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14763
Garrett Tanzer753cc532022-03-02 21:30:59764 absl::optional<base::UnguessableToken> originator_nonce =
765 fenced_frame_nonce();
766
Mustaq Ahmed0180320f2019-03-21 16:07:01767 // See the "Same-origin Visibility" section in |UserActivationState| class
768 // doc.
Mustaq Ahmede5f12562019-10-30 18:02:03769 if (base::FeatureList::IsEnabled(
Garrett Tanzer753cc532022-03-02 21:30:59770 features::kUserActivationSameOriginVisibility)) {
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14771 const url::Origin& current_origin =
772 this->current_frame_host()->GetLastCommittedOrigin();
773 for (FrameTreeNode* node : frame_tree()->Nodes()) {
Garrett Tanzer753cc532022-03-02 21:30:59774 if (shadow_dom_fenced_frame_enabled &&
775 node->fenced_frame_nonce() != originator_nonce) {
776 continue;
777 }
778
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14779 if (node->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith(
780 current_origin)) {
Mustaq Ahmedecb5c38e2020-07-29 00:34:30781 node->user_activation_state_.Activate(notification_type);
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14782 }
783 }
784 }
785
Carlos Caballero40b0efd2021-01-26 11:55:00786 navigator().controller().NotifyUserActivation();
Alex Moshchuk03904192021-04-02 07:29:08787 current_frame_host()->MaybeIsolateForUserActivation();
Shivani Sharma194877032019-03-07 17:52:47788
Mustaq Ahmedc4cb7162018-06-05 16:28:36789 return true;
790}
791
792bool FrameTreeNode::ConsumeTransientUserActivation() {
Garrett Tanzer753cc532022-03-02 21:30:59793 // User activation consumptions shouldn't propagate into/out of fenced
794 // frames.
795 // For ShadowDOM, fenced frames are in the same frame tree as their embedder,
796 // so we need to perform additional checks to enforce the boundary.
797 // For MPArch, fenced frames have a separate frame tree, so this boundary is
798 // enforced by default.
799 // https://docs.google.com/document/d/1WnIhXOFycoje_sEoZR3Mo0YNSR2Ki7LABIC_HEWFaog
800 bool shadow_dom_fenced_frame_enabled =
David Bokanc3fb5fa2022-07-04 14:55:31801 frame_tree()->IsFencedFramesShadowDOMBased();
Garrett Tanzer753cc532022-03-02 21:30:59802 absl::optional<base::UnguessableToken> originator_nonce =
803 fenced_frame_nonce();
804
Mustaq Ahmedc4cb7162018-06-05 16:28:36805 bool was_active = user_activation_state_.IsActive();
Garrett Tanzer753cc532022-03-02 21:30:59806 for (FrameTreeNode* node : frame_tree()->Nodes()) {
807 if (shadow_dom_fenced_frame_enabled &&
808 node->fenced_frame_nonce() != originator_nonce) {
809 continue;
810 }
811
Mustaq Ahmedc4cb7162018-06-05 16:28:36812 node->user_activation_state_.ConsumeIfActive();
Garrett Tanzer753cc532022-03-02 21:30:59813 }
Harkiran Bolaria0b3bdef02022-03-10 13:04:40814 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
815 false);
Mustaq Ahmedc4cb7162018-06-05 16:28:36816 return was_active;
817}
818
Shivani Sharmac4f561582018-11-15 15:58:39819bool FrameTreeNode::ClearUserActivation() {
Shivani Sharmac4f561582018-11-15 15:58:39820 for (FrameTreeNode* node : frame_tree()->SubtreeNodes(this))
821 node->user_activation_state_.Clear();
Harkiran Bolaria0b3bdef02022-03-10 13:04:40822 current_frame_host()->browsing_context_state()->set_has_active_user_gesture(
823 false);
Shivani Sharmac4f561582018-11-15 15:58:39824 return true;
825}
826
Ella Ge9caed612019-08-09 16:17:25827bool FrameTreeNode::VerifyUserActivation() {
Ella Gea78f6772019-12-11 10:35:25828 DCHECK(base::FeatureList::IsEnabled(
829 features::kBrowserVerifiedUserActivationMouse) ||
830 base::FeatureList::IsEnabled(
831 features::kBrowserVerifiedUserActivationKeyboard));
832
Ella Ge9caed612019-08-09 16:17:25833 return render_manager_.current_frame_host()
834 ->GetRenderWidgetHost()
Mustaq Ahmed83bb1722019-10-22 20:00:10835 ->RemovePendingUserActivationIfAvailable();
Ella Ge9caed612019-08-09 16:17:25836}
837
Mustaq Ahmedc4cb7162018-06-05 16:28:36838bool FrameTreeNode::UpdateUserActivationState(
Mustaq Ahmeddc195e5b2020-08-04 18:45:11839 blink::mojom::UserActivationUpdateType update_type,
840 blink::mojom::UserActivationNotificationType notification_type) {
Ella Ge9caed612019-08-09 16:17:25841 bool update_result = false;
Mustaq Ahmedc4cb7162018-06-05 16:28:36842 switch (update_type) {
Antonio Gomes4b2c5132020-01-16 11:49:48843 case blink::mojom::UserActivationUpdateType::kConsumeTransientActivation:
Ella Ge9caed612019-08-09 16:17:25844 update_result = ConsumeTransientUserActivation();
845 break;
Antonio Gomes4b2c5132020-01-16 11:49:48846 case blink::mojom::UserActivationUpdateType::kNotifyActivation:
Mustaq Ahmeddc195e5b2020-08-04 18:45:11847 update_result = NotifyUserActivation(notification_type);
Ella Ge9caed612019-08-09 16:17:25848 break;
Antonio Gomes4b2c5132020-01-16 11:49:48849 case blink::mojom::UserActivationUpdateType::
Liviu Tintad9391fb92020-09-28 23:50:07850 kNotifyActivationPendingBrowserVerification: {
851 const bool user_activation_verified = VerifyUserActivation();
852 // Add UMA metric for when browser user activation verification succeeds
853 base::UmaHistogramBoolean("Event.BrowserVerifiedUserActivation",
854 user_activation_verified);
855 if (user_activation_verified) {
Mustaq Ahmedecb5c38e2020-07-29 00:34:30856 update_result = NotifyUserActivation(
Mustaq Ahmed2cfb0402020-09-29 19:24:35857 blink::mojom::UserActivationNotificationType::kInteraction);
Antonio Gomes4b2c5132020-01-16 11:49:48858 update_type = blink::mojom::UserActivationUpdateType::kNotifyActivation;
Ella Ge9caed612019-08-09 16:17:25859 } else {
arthursonzogni9816b9192021-03-29 16:09:19860 // TODO(https://crbug.com/848778): We need to decide what to do when
861 // user activation verification failed. NOTREACHED here will make all
Ella Ge9caed612019-08-09 16:17:25862 // unrelated tests that inject event to renderer fail.
863 return false;
864 }
Liviu Tintad9391fb92020-09-28 23:50:07865 } break;
Antonio Gomes4b2c5132020-01-16 11:49:48866 case blink::mojom::UserActivationUpdateType::kClearActivation:
Ella Ge9caed612019-08-09 16:17:25867 update_result = ClearUserActivation();
868 break;
Mustaq Ahmedc4cb7162018-06-05 16:28:36869 }
Mustaq Ahmeddc195e5b2020-08-04 18:45:11870 render_manager_.UpdateUserActivationState(update_type, notification_type);
Ella Ge9caed612019-08-09 16:17:25871 return update_result;
japhet61835ae12017-01-20 01:25:39872}
873
Arthur Sonzognif8840b92018-11-07 14:10:35874void FrameTreeNode::PruneChildFrameNavigationEntries(
875 NavigationEntryImpl* entry) {
876 for (size_t i = 0; i < current_frame_host()->child_count(); ++i) {
877 FrameTreeNode* child = current_frame_host()->child_at(i);
878 if (child->is_created_by_script_) {
879 entry->RemoveEntryForFrame(child,
880 /* only_if_different_position = */ false);
881 } else {
882 child->PruneChildFrameNavigationEntries(entry);
883 }
884 }
885}
886
arthursonzogni034bb9c2020-10-01 08:29:56887void FrameTreeNode::SetInitialPopupURL(const GURL& initial_popup_url) {
888 DCHECK(initial_popup_url_.is_empty());
Rakina Zata Amni4182b032021-11-01 04:22:01889 DCHECK(is_on_initial_empty_document_);
arthursonzogni034bb9c2020-10-01 08:29:56890 initial_popup_url_ = initial_popup_url;
891}
892
893void FrameTreeNode::SetPopupCreatorOrigin(
894 const url::Origin& popup_creator_origin) {
Rakina Zata Amni4182b032021-11-01 04:22:01895 DCHECK(is_on_initial_empty_document_);
arthursonzogni034bb9c2020-10-01 08:29:56896 popup_creator_origin_ = popup_creator_origin;
897}
898
Rakina Zata Amni4b1968d2021-09-09 03:29:47899void FrameTreeNode::WriteIntoTrace(
Alexander Timin074cd182022-03-23 18:11:22900 perfetto::TracedProto<TraceProto> proto) const {
Rakina Zata Amni4b1968d2021-09-09 03:29:47901 proto->set_frame_tree_node_id(frame_tree_node_id());
Alexander Timin074cd182022-03-23 18:11:22902 proto->set_is_main_frame(IsMainFrame());
903 proto.Set(TraceProto::kCurrentFrameHost, current_frame_host());
904 proto.Set(TraceProto::kSpeculativeFrameHost,
905 render_manager()->speculative_frame_host());
Rakina Zata Amni4b1968d2021-09-09 03:29:47906}
907
Carlos Caballero76711352021-03-24 17:38:21908bool FrameTreeNode::HasNavigation() {
909 if (navigation_request())
910 return true;
911
912 // Same-RenderFrameHost navigation is committing:
913 if (current_frame_host()->HasPendingCommitNavigation())
914 return true;
915
916 // Cross-RenderFrameHost navigation is committing:
917 if (render_manager()->speculative_frame_host())
918 return true;
919
920 return false;
921}
922
Dominic Farolino4bc10ee2021-08-31 00:37:36923bool FrameTreeNode::IsFencedFrameRoot() const {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58924 return fenced_frame_status_ == FencedFrameStatus::kFencedFrameRoot;
shivanigithubf3ddff52021-07-03 22:06:30925}
926
927bool FrameTreeNode::IsInFencedFrameTree() const {
Abhijeet Kandalkar3f29bc42022-09-23 12:39:58928 return fenced_frame_status_ != FencedFrameStatus::kNotNestedInFencedFrame;
shivanigithubf3ddff52021-07-03 22:06:30929}
930
shivanigithub4cd016a2021-09-20 21:10:30931void FrameTreeNode::SetFencedFrameNonceIfNeeded() {
932 if (!IsInFencedFrameTree()) {
933 return;
934 }
935
936 if (IsFencedFrameRoot()) {
937 fenced_frame_nonce_ = base::UnguessableToken::Create();
938 return;
939 }
940
941 // For nested iframes in a fenced frame tree, propagate the same nonce as was
942 // set in the fenced frame root.
943 DCHECK(parent_);
944 absl::optional<base::UnguessableToken> nonce =
945 parent_->frame_tree_node()->fenced_frame_nonce();
946 DCHECK(nonce.has_value());
947 fenced_frame_nonce_ = nonce;
948}
949
Nan Line376738a2022-03-25 22:05:41950absl::optional<blink::mojom::FencedFrameMode>
951FrameTreeNode::GetFencedFrameMode() {
Garrett Tanzera42fdef2022-06-13 16:09:14952 if (!IsInFencedFrameTree()) {
Nan Line376738a2022-03-25 22:05:41953 return absl::nullopt;
Garrett Tanzera42fdef2022-06-13 16:09:14954 }
Nan Lin171fe9a2022-02-17 16:42:16955
Garrett Tanzera42fdef2022-06-13 16:09:14956 switch (blink::features::kFencedFramesImplementationTypeParam.Get()) {
957 case blink::features::FencedFramesImplementationType::kMPArch: {
958 FrameTreeNode* outer_delegate_node =
959 render_manager()->GetOuterDelegateNode();
960 DCHECK(outer_delegate_node);
Nan Lin171fe9a2022-02-17 16:42:16961
Garrett Tanzera42fdef2022-06-13 16:09:14962 FencedFrame* fenced_frame = FindFencedFrame(outer_delegate_node);
963 DCHECK(fenced_frame);
Nan Line376738a2022-03-25 22:05:41964
Garrett Tanzera42fdef2022-06-13 16:09:14965 return fenced_frame->mode();
966 }
967 case blink::features::FencedFramesImplementationType::kShadowDOM: {
968 FrameTreeNode* node = this;
969 while (!node->IsFencedFrameRoot()) {
Liam Brady582b21bc82022-08-10 14:03:21970 node = node->parent()->frame_tree_node();
Garrett Tanzera42fdef2022-06-13 16:09:14971 }
972 return node->pending_frame_policy_.fenced_frame_mode;
973 }
974 }
Nan Lin171fe9a2022-02-17 16:42:16975}
976
Nan Linaaf84f72021-12-02 22:31:56977bool FrameTreeNode::IsErrorPageIsolationEnabled() const {
Nan Lind9de87d2022-03-18 16:53:03978 // Error page isolation is enabled for main frames only (crbug.com/1092524).
979 // Note that this will also enable error page isolation for fenced frames in
980 // MPArch mode, but not ShadowDOM mode.
981 // See the issue in crbug.com/1264224#c7 for why it can't be enabled for
982 // ShadowDOM mode.
983 return SiteIsolationPolicy::IsErrorPageIsolationEnabled(IsMainFrame());
Nan Linaaf84f72021-12-02 22:31:56984}
985
W. James MacLean81b8d01f2022-01-25 20:50:59986void FrameTreeNode::SetSrcdocValue(const std::string& srcdoc_value) {
987 srcdoc_value_ = srcdoc_value;
988}
989
Garrett Tanzer2975eeac2022-08-22 16:34:01990absl::optional<const FencedFrameURLMapping::SharedStorageBudgetMetadata*>
Yao Xiao1ac702d2022-06-08 17:20:49991FrameTreeNode::FindSharedStorageBudgetMetadata() {
992 FrameTreeNode* node = this;
993
994 while (true) {
Garrett Tanzer2975eeac2022-08-22 16:34:01995 if (node->fenced_frame_properties_ &&
996 node->fenced_frame_properties_->shared_storage_budget_metadata) {
Yao Xiao1ac702d2022-06-08 17:20:49997 DCHECK(node->IsFencedFrameRoot());
Garrett Tanzer2975eeac2022-08-22 16:34:01998 return node->fenced_frame_properties_->shared_storage_budget_metadata;
Yao Xiao1ac702d2022-06-08 17:20:49999 }
1000
1001 if (node->GetParentOrOuterDocument()) {
1002 node = node->GetParentOrOuterDocument()->frame_tree_node();
1003 } else {
1004 break;
1005 }
1006 }
1007
Garrett Tanzer2975eeac2022-08-22 16:34:011008 return absl::nullopt;
Yao Xiao1ac702d2022-06-08 17:20:491009}
1010
Harkiran Bolariaebbe7702022-02-22 19:19:031011const scoped_refptr<BrowsingContextState>&
1012FrameTreeNode::GetBrowsingContextStateForSubframe() const {
1013 DCHECK(!IsMainFrame());
1014 return current_frame_host()->browsing_context_state();
1015}
1016
Arthur Hemerye4659282022-03-28 08:36:151017void FrameTreeNode::ClearOpenerReferences() {
1018 // Simulate the FrameTreeNode being dead to opener observers. They will
1019 // nullify their opener.
1020 // Note: observers remove themselves from observers_, no need to take care of
1021 // that manually.
1022 for (auto& observer : observers_)
1023 observer.OnFrameTreeNodeDisownedOpenee(this);
1024}
1025
Liam Bradyd2a41e152022-07-19 13:58:481026bool FrameTreeNode::AncestorOrSelfHasCSPEE() const {
1027 // Check if CSPEE is set in this frame or any ancestor frames.
Yuzu Saijo03dbf9b2022-07-22 04:29:451028 return csp_attribute() || (parent() && parent()->required_csp());
Liam Bradyd2a41e152022-07-19 13:58:481029}
1030
[email protected]9b159a52013-10-03 17:24:551031} // namespace content