blob: 5c59198897e17cf69b65f2d24bdb5b4657f018b7 [file] [log] [blame]
[email protected]9b159a52013-10-03 17:24:551// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4a8ca482013-10-30 21:06:405#include "content/browser/frame_host/frame_tree_node.h"
[email protected]9b159a52013-10-03 17:24:556
Daniel Cheng6ca7f1c92017-08-09 21:45:417#include <math.h>
8
[email protected]9b159a52013-10-03 17:24:559#include <queue>
Takuto Ikutaadf31eb2019-01-05 00:32:4810#include <unordered_map>
dcheng36b6aec92015-12-26 06:16:3611#include <utility>
[email protected]9b159a52013-10-03 17:24:5512
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1413#include "base/feature_list.h"
scottmg6ece5ae2017-02-01 18:25:1914#include "base/lazy_instance.h"
avib7348942015-12-25 20:57:1015#include "base/macros.h"
dcheng23ca947d2016-05-04 20:04:1516#include "base/metrics/histogram_macros.h"
[email protected]9b159a52013-10-03 17:24:5517#include "base/stl_util.h"
Daniel Cheng6ca7f1c92017-08-09 21:45:4118#include "base/strings/string_util.h"
Andrey Kosyakovf2d4ff72018-10-29 20:09:5919#include "content/browser/devtools/devtools_instrumentation.h"
[email protected]94d0cc12013-12-18 00:07:4120#include "content/browser/frame_host/frame_tree.h"
Shivani Sharma194877032019-03-07 17:52:4721#include "content/browser/frame_host/navigation_controller_impl.h"
clamydcb434c12015-04-16 19:29:1622#include "content/browser/frame_host/navigation_request.h"
[email protected]190b8c52013-11-09 01:35:4423#include "content/browser/frame_host/navigator.h"
[email protected]d4a8ca482013-10-30 21:06:4024#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4125#include "content/browser/renderer_host/render_view_host_impl.h"
clamyf73862c42015-07-08 12:31:3326#include "content/common/frame_messages.h"
Lucas Furukawa Gadanief8290a2019-07-29 20:27:5127#include "content/common/navigation_params.h"
28#include "content/common/navigation_params_utils.h"
dmazzonie950ea232015-03-13 21:39:4529#include "content/public/browser/browser_thread.h"
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1430#include "content/public/common/content_features.h"
Arthur Sonzognif21fb512018-11-06 09:31:5831#include "content/public/common/navigation_policy.h"
arthursonzognib93a4472020-04-10 07:38:0032#include "services/network/public/cpp/web_sandbox_flags.h"
33#include "services/network/public/mojom/web_sandbox_flags.mojom-shared.h"
Antonio Gomes4b2c5132020-01-16 11:49:4834#include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h"
Julie Jeongeun Kimd90e2dd2020-03-03 11:45:3735#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
[email protected]9b159a52013-10-03 17:24:5536
37namespace content {
38
dmazzonie950ea232015-03-13 21:39:4539namespace {
40
41// This is a global map between frame_tree_node_ids and pointers to
42// FrameTreeNodes.
Takuto Ikutaadf31eb2019-01-05 00:32:4843typedef std::unordered_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4544
scottmg5e65e3a2017-03-08 08:48:4645base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
46 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4547
fdegansa696e5112015-04-17 01:57:5948// These values indicate the loading progress status. The minimum progress
49// value matches what Blink's ProgressTracker has traditionally used for a
50// minimum progress value.
fdegansa696e5112015-04-17 01:57:5951const double kLoadingProgressMinimum = 0.1;
52const double kLoadingProgressDone = 1.0;
dmazzonie950ea232015-03-13 21:39:4553
fdegansa696e5112015-04-17 01:57:5954} // namespace
fdegans1d16355162015-03-26 11:58:3455
Tsuyoshi Horof36fc4ef2019-10-16 10:02:1256const int FrameTreeNode::kFrameTreeNodeInvalidId = -1;
57
alexmose201c7cd2015-06-10 17:14:2158// This observer watches the opener of its owner FrameTreeNode and clears the
59// owner's opener if the opener is destroyed.
60class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
61 public:
jochen6004a362017-02-04 00:11:4062 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
63 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2164
65 // FrameTreeNode::Observer
66 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
jochen6004a362017-02-04 00:11:4067 if (observing_original_opener_) {
Avi Drissman36465f332017-09-11 20:49:3968 // The "original owner" is special. It's used for attribution, and clients
69 // walk down the original owner chain. Therefore, if a link in the chain
70 // is being destroyed, reconnect the observation to the parent of the link
71 // being destroyed.
jochen6004a362017-02-04 00:11:4072 CHECK_EQ(owner_->original_opener(), node);
Avi Drissman36465f332017-09-11 20:49:3973 owner_->SetOriginalOpener(node->original_opener());
74 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:4075 } else {
76 CHECK_EQ(owner_->opener(), node);
77 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:3978 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:4079 }
alexmose201c7cd2015-06-10 17:14:2180 }
81
82 private:
83 FrameTreeNode* owner_;
jochen6004a362017-02-04 00:11:4084 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:2185
86 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver);
87};
88
vishal.b782eb5d2015-04-29 12:22:5789int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:5590
dmazzonie950ea232015-03-13 21:39:4591// static
vishal.b782eb5d2015-04-29 12:22:5792FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:1993 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:1594 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
jdoerrie55ec69d2018-10-08 13:34:4695 auto it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:4596 return it == nodes->end() ? nullptr : it->second;
97}
98
Alexander Timin381e7e182020-04-28 19:04:0399// static
100FrameTreeNode* FrameTreeNode::From(RenderFrameHost* rfh) {
101 if (!rfh)
102 return nullptr;
103 return static_cast<RenderFrameHostImpl*>(rfh)->frame_tree_node();
104}
105
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54106FrameTreeNode::FrameTreeNode(
107 FrameTree* frame_tree,
108 Navigator* navigator,
Alexander Timin381e7e182020-04-28 19:04:03109 RenderFrameHostImpl* parent,
Julie Jeongeun Kim70a2e4e2020-02-21 05:09:54110 blink::WebTreeScopeType scope,
111 const std::string& name,
112 const std::string& unique_name,
113 bool is_created_by_script,
114 const base::UnguessableToken& devtools_frame_token,
115 const blink::mojom::FrameOwnerProperties& frame_owner_properties,
116 blink::FrameOwnerElementType owner_type)
[email protected]bffc8302014-01-23 20:52:16117 : frame_tree_(frame_tree),
118 navigator_(navigator),
Lucas Furukawa Gadani72cc21c2018-09-04 18:50:46119 render_manager_(this, frame_tree->manager_delegate()),
[email protected]bffc8302014-01-23 20:52:16120 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59121 parent_(parent),
Alexander Timin381e7e182020-04-28 19:04:03122 depth_(parent ? parent->frame_tree_node()->depth_ + 1 : 0u),
alexmose201c7cd2015-06-10 17:14:21123 opener_(nullptr),
jochen6004a362017-02-04 00:11:40124 original_opener_(nullptr),
creisf0f069a2015-07-23 23:51:53125 has_committed_real_load_(false),
engedy6e2e0992017-05-25 18:58:42126 is_collapsed_(false),
estarka886b8d2015-12-18 21:53:08127 replication_state_(
128 scope,
129 name,
lukasza464d8692016-02-22 19:26:32130 unique_name,
Julie Jeongeun Kimd90e2dd2020-03-03 11:45:37131 blink::mojom::InsecureRequestPolicy::
132 kLeaveInsecureRequestsAlone /* should enforce strict mixed content
133 checking */
134 ,
arthursonzogni4b62a5cb2018-01-17 14:14:26135 std::vector<uint32_t>()
136 /* hashes of hosts for insecure request upgrades */,
japhet61835ae12017-01-20 01:25:39137 false /* is a potentially trustworthy unique origin */,
Becca Hughes60af7d42017-12-12 10:53:15138 false /* has received a user gesture */,
Ehsan Karamad192a8da2018-10-21 03:48:08139 false /* has received a user gesture before nav */,
140 owner_type),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45141 is_created_by_script_(is_created_by_script),
Pavel Feldman25234722017-10-11 02:49:06142 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31143 frame_owner_properties_(frame_owner_properties),
Shubhie Panickerddf2a4e2018-03-06 00:09:06144 was_discarded_(false),
Alexander Timin381e7e182020-04-28 19:04:03145 blame_context_(frame_tree_node_id_, FrameTreeNode::From(parent)) {
rob97250742015-12-10 17:45:15146 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45147 g_frame_tree_node_id_map.Get().insert(
148 std::make_pair(frame_tree_node_id_, this));
149 CHECK(result.second);
benjhaydend4da63d2016-03-11 21:29:33150
xiaochenghb9554bb2016-05-21 14:20:48151 // Note: this should always be done last in the constructor.
152 blame_context_.Initialize();
alexmos998581d2015-01-22 01:01:59153}
[email protected]9b159a52013-10-03 17:24:55154
155FrameTreeNode::~FrameTreeNode() {
Nasko Oskov252ae042018-10-04 21:44:12156 // Remove the children.
157 current_frame_host()->ResetChildren();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45158
Nate Chapin22ea6592019-03-05 22:29:02159 current_frame_host()->ResetLoadingState();
160
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45161 // If the removed frame was created by a script, then its history entry will
162 // never be reused - we can save some memory by removing the history entry.
163 // See also https://crbug.com/784356.
164 if (is_created_by_script_ && parent_) {
165 NavigationEntryImpl* nav_entry = static_cast<NavigationEntryImpl*>(
166 navigator()->GetController()->GetLastCommittedEntry());
167 if (nav_entry) {
168 nav_entry->RemoveEntryForFrame(this,
169 /* only_if_different_position = */ false);
170 }
171 }
172
dmazzonie950ea232015-03-13 21:39:45173 frame_tree_->FrameRemoved(this);
ericwilligers254597b2016-10-17 10:32:31174 for (auto& observer : observers_)
175 observer.OnFrameTreeNodeDestroyed(this);
alexmose201c7cd2015-06-10 17:14:21176
177 if (opener_)
178 opener_->RemoveObserver(opener_observer_.get());
jochen6004a362017-02-04 00:11:40179 if (original_opener_)
180 original_opener_->RemoveObserver(original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45181
182 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18183
danakjf9400602019-06-07 15:44:58184 bool did_stop_loading = false;
185
jam39258caf2016-11-02 14:48:18186 if (navigation_request_) {
danakjf9400602019-06-07 15:44:58187 navigation_request_.reset();
Arthur Hemery0dd65812019-08-01 14:18:45188 // If a frame with a pending navigation is detached, make sure the
189 // WebContents (and its observers) update their loading state.
danakjf9400602019-06-07 15:44:58190 did_stop_loading = true;
jam39258caf2016-11-02 14:48:18191 }
Nate Chapin22ea6592019-03-05 22:29:02192
danakjf9400602019-06-07 15:44:58193 // ~SiteProcessCountTracker DCHECKs in some tests if the speculative
194 // RenderFrameHostImpl is not destroyed last. Ideally this would be closer to
195 // (possible before) the ResetLoadingState() call above.
196 //
197 // There is an inherent race condition causing bugs 838348/915179/et al, where
198 // the renderer may have committed the speculative main frame and the browser
199 // has not heard about it yet. If this is a main frame, then in that case the
200 // speculative RenderFrame was unable to be deleted (it is owned by the
201 // renderer) and we should not be able to cancel the navigation at this point.
202 // CleanUpNavigation() would normally be called here but it will try to undo
203 // the navigation and expose the race condition. When it replaces the main
204 // frame with a RenderFrameProxy, that leaks the committed main frame, leaving
205 // the frame and its friend group with pointers that will become invalid
206 // shortly as we are shutting everything down and deleting the RenderView etc.
207 // We avoid this problematic situation by not calling CleanUpNavigation() or
208 // DiscardUnusedFrame() here. The speculative RenderFrameHost is simply
209 // returned and deleted immediately. This satisfies the requirement that the
210 // speculative RenderFrameHost is removed from the RenderFrameHostManager
211 // before it is destroyed.
212 if (render_manager_.speculative_frame_host()) {
213 did_stop_loading |= render_manager_.speculative_frame_host()->is_loading();
214 render_manager_.UnsetSpeculativeRenderFrameHost();
215 }
216
217 if (did_stop_loading)
218 DidStopLoading();
219
Nate Chapin22ea6592019-03-05 22:29:02220 DCHECK(!IsLoading());
[email protected]9b159a52013-10-03 17:24:55221}
222
alexmose201c7cd2015-06-10 17:14:21223void FrameTreeNode::AddObserver(Observer* observer) {
224 observers_.AddObserver(observer);
225}
226
227void FrameTreeNode::RemoveObserver(Observer* observer) {
228 observers_.RemoveObserver(observer);
229}
230
[email protected]94d0cc12013-12-18 00:07:41231bool FrameTreeNode::IsMainFrame() const {
232 return frame_tree_->root() == this;
233}
234
Ian Clelland5cbaaf82017-11-27 22:00:03235void FrameTreeNode::ResetForNavigation() {
236 // Discard any CSP headers from the previous document.
237 replication_state_.accumulated_csp_headers.clear();
238 render_manager_.OnDidResetContentSecurityPolicy();
239
Ian Clellandedb8c5dd2018-03-01 17:01:37240 // Clear any CSP-set sandbox flags, and the declared feature policy for the
241 // frame.
arthursonzognib93a4472020-04-10 07:38:00242 UpdateFramePolicyHeaders(network::mojom::WebSandboxFlags::kNone, {});
Shivani Sharmac4f561582018-11-15 15:58:39243
Mustaq Ahmedc53e4c562019-01-29 19:05:09244 // This frame has had its user activation bits cleared in the renderer
245 // before arriving here. We just need to clear them here and in the other
246 // renderer processes that may have a reference to this frame.
Antonio Gomes4b2c5132020-01-16 11:49:48247 UpdateUserActivationState(
248 blink::mojom::UserActivationUpdateType::kClearActivation);
Ian Clelland5cbaaf82017-11-27 22:00:03249}
250
yilkal34a3d752019-08-30 18:20:30251size_t FrameTreeNode::GetFrameTreeSize() const {
252 if (is_collapsed())
253 return 0;
254
255 size_t size = 0;
256 for (size_t i = 0; i < child_count(); i++) {
257 size += child_at(i)->GetFrameTreeSize();
258 }
259
260 // Account for this node.
261 size++;
262 return size;
263}
264
alexmose201c7cd2015-06-10 17:14:21265void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
266 if (opener_) {
267 opener_->RemoveObserver(opener_observer_.get());
268 opener_observer_.reset();
269 }
270
271 opener_ = opener;
272
273 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55274 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21275 opener_->AddObserver(opener_observer_.get());
276 }
277}
278
jochen6004a362017-02-04 00:11:40279void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39280 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37281 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40282
Avi Drissman36465f332017-09-11 20:49:39283 if (original_opener_) {
284 original_opener_->RemoveObserver(original_opener_observer_.get());
285 original_opener_observer_.reset();
286 }
287
jochen6004a362017-02-04 00:11:40288 original_opener_ = opener;
289
290 if (original_opener_) {
jochen6004a362017-02-04 00:11:40291 original_opener_observer_ =
Jeremy Roman04f27c372017-10-27 15:20:55292 std::make_unique<OpenerDestroyedObserver>(this, true);
jochen6004a362017-02-04 00:11:40293 original_opener_->AddObserver(original_opener_observer_.get());
294 }
295}
296
creisf0f069a2015-07-23 23:51:53297void FrameTreeNode::SetCurrentURL(const GURL& url) {
Balazs Engedyc8a7cccf2018-03-12 23:00:49298 if (!has_committed_real_load_ && !url.IsAboutBlank())
creisf0f069a2015-07-23 23:51:53299 has_committed_real_load_ = true;
Erik Chen173bf3042017-07-31 06:06:21300 current_frame_host()->SetLastCommittedUrl(url);
xiaochenghb9554bb2016-05-21 14:20:48301 blame_context_.TakeSnapshot();
creisf0f069a2015-07-23 23:51:53302}
303
estarkbd8e26f2016-03-16 23:30:37304void FrameTreeNode::SetCurrentOrigin(
305 const url::Origin& origin,
306 bool is_potentially_trustworthy_unique_origin) {
307 if (!origin.IsSameOriginWith(replication_state_.origin) ||
308 replication_state_.has_potentially_trustworthy_unique_origin !=
309 is_potentially_trustworthy_unique_origin) {
310 render_manager_.OnDidUpdateOrigin(origin,
311 is_potentially_trustworthy_unique_origin);
312 }
alexmosa7a4ff822015-04-27 17:59:56313 replication_state_.origin = origin;
estarkbd8e26f2016-03-16 23:30:37314 replication_state_.has_potentially_trustworthy_unique_origin =
315 is_potentially_trustworthy_unique_origin;
alexmosa7a4ff822015-04-27 17:59:56316}
alexmosbe2f4c32015-03-10 02:30:23317
engedy6e2e0992017-05-25 18:58:42318void FrameTreeNode::SetCollapsed(bool collapsed) {
319 DCHECK(!IsMainFrame());
320 if (is_collapsed_ == collapsed)
321 return;
322
323 is_collapsed_ = collapsed;
324 render_manager_.OnDidChangeCollapsedState(collapsed);
325}
326
lukasza464d8692016-02-22 19:26:32327void FrameTreeNode::SetFrameName(const std::string& name,
328 const std::string& unique_name) {
329 if (name == replication_state_.name) {
330 // |unique_name| shouldn't change unless |name| changes.
331 DCHECK_EQ(unique_name, replication_state_.unique_name);
332 return;
333 }
lukasza5140a412016-09-15 21:12:30334
335 if (parent()) {
336 // Non-main frames should have a non-empty unique name.
337 DCHECK(!unique_name.empty());
338 } else {
339 // Unique name of main frames should always stay empty.
340 DCHECK(unique_name.empty());
341 }
342
Daniel Cheng6ca7f1c92017-08-09 21:45:41343 // Note the unique name should only be able to change before the first real
344 // load is committed, but that's not strongly enforced here.
lukasza464d8692016-02-22 19:26:32345 render_manager_.OnDidUpdateName(name, unique_name);
alexmosa7a4ff822015-04-27 17:59:56346 replication_state_.name = name;
lukasza464d8692016-02-22 19:26:32347 replication_state_.unique_name = unique_name;
alexmosbe2f4c32015-03-10 02:30:23348}
349
arthursonzogni662aa652017-03-28 11:09:50350void FrameTreeNode::AddContentSecurityPolicies(
arthursonzogni35a95c72020-01-15 17:49:43351 std::vector<network::mojom::ContentSecurityPolicyHeaderPtr> headers) {
352 for (auto& header : headers)
353 replication_state_.accumulated_csp_headers.push_back(*header);
354 render_manager_.OnDidAddContentSecurityPolicies(std::move(headers));
lukasza8e1c02e42016-05-17 20:05:10355}
356
mkwstf672e7ef2016-06-09 20:51:07357void FrameTreeNode::SetInsecureRequestPolicy(
Julie Jeongeun Kimd90e2dd2020-03-03 11:45:37358 blink::mojom::InsecureRequestPolicy policy) {
mkwstf672e7ef2016-06-09 20:51:07359 if (policy == replication_state_.insecure_request_policy)
estarka886b8d2015-12-18 21:53:08360 return;
mkwstf672e7ef2016-06-09 20:51:07361 render_manager_.OnEnforceInsecureRequestPolicy(policy);
362 replication_state_.insecure_request_policy = policy;
estarka886b8d2015-12-18 21:53:08363}
364
arthursonzogni4b62a5cb2018-01-17 14:14:26365void FrameTreeNode::SetInsecureNavigationsSet(
366 const std::vector<uint32_t>& insecure_navigations_set) {
367 DCHECK(std::is_sorted(insecure_navigations_set.begin(),
368 insecure_navigations_set.end()));
369 if (insecure_navigations_set == replication_state_.insecure_navigations_set)
370 return;
371 render_manager_.OnEnforceInsecureNavigationsSet(insecure_navigations_set);
372 replication_state_.insecure_navigations_set = insecure_navigations_set;
373}
374
Luna Luc3fdacdf2017-11-08 04:48:53375void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Ian Clellandcdc4f312017-10-13 22:24:12376 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
Dave Tapuska9b153a942020-02-10 19:35:10377 pending_frame_policy_.disallow_document_access =
378 frame_policy.disallow_document_access;
alexmos6e940102016-01-19 22:47:25379
Ian Clellandcdc4f312017-10-13 22:24:12380 if (parent()) {
381 // Subframes should always inherit their parent's sandbox flags.
Alexander Timin381e7e182020-04-28 19:04:03382 pending_frame_policy_.sandbox_flags |=
383 parent()->frame_tree_node()->active_sandbox_flags();
Charlie Hue1b77ac2019-12-13 21:30:17384 // This is only applied on subframes; container policy and required document
385 // policy are not mutable on main frame.
Ian Clellandcdc4f312017-10-13 22:24:12386 pending_frame_policy_.container_policy = frame_policy.container_policy;
Charlie Hue1b77ac2019-12-13 21:30:17387 pending_frame_policy_.required_document_policy =
388 frame_policy.required_document_policy;
Ian Clellandcdc4f312017-10-13 22:24:12389 }
iclelland92f8c0b2017-04-19 12:43:05390}
391
alexmos9f8705a2015-05-06 19:58:59392FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39393 return GetSibling(-1);
394}
alexmos9f8705a2015-05-06 19:58:59395
paulmeyer322777fb2016-05-16 23:15:39396FrameTreeNode* FrameTreeNode::NextSibling() const {
397 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59398}
399
fdegans4a49ce932015-03-12 17:11:37400bool FrameTreeNode::IsLoading() const {
401 RenderFrameHostImpl* current_frame_host =
402 render_manager_.current_frame_host();
fdegans4a49ce932015-03-12 17:11:37403
404 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39405
clamy610c63b32017-12-22 15:05:18406 if (navigation_request_)
407 return true;
clamy11e11512015-07-07 16:42:17408
clamy610c63b32017-12-22 15:05:18409 RenderFrameHostImpl* speculative_frame_host =
410 render_manager_.speculative_frame_host();
411 if (speculative_frame_host && speculative_frame_host->is_loading())
412 return true;
fdegans4a49ce932015-03-12 17:11:37413 return current_frame_host->is_loading();
414}
415
Charlie Hu5ffc0152019-12-06 15:59:53416bool FrameTreeNode::CommitFramePolicy(
417 const blink::FramePolicy& new_frame_policy) {
418 bool did_change_flags = new_frame_policy.sandbox_flags !=
Ian Clellandcdc4f312017-10-13 22:24:12419 replication_state_.frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05420 bool did_change_container_policy =
Charlie Hu5ffc0152019-12-06 15:59:53421 new_frame_policy.container_policy !=
Ian Clellandcdc4f312017-10-13 22:24:12422 replication_state_.frame_policy.container_policy;
Charlie Hue1b77ac2019-12-13 21:30:17423 bool did_change_required_document_policy =
424 pending_frame_policy_.required_document_policy !=
425 replication_state_.frame_policy.required_document_policy;
Dave Tapuska9b153a942020-02-10 19:35:10426 bool did_change_document_access =
427 new_frame_policy.disallow_document_access !=
428 replication_state_.frame_policy.disallow_document_access;
iclelland92f8c0b2017-04-19 12:43:05429 if (did_change_flags)
Ian Clellandcdc4f312017-10-13 22:24:12430 replication_state_.frame_policy.sandbox_flags =
Charlie Hu5ffc0152019-12-06 15:59:53431 new_frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05432 if (did_change_container_policy)
Ian Clellandcdc4f312017-10-13 22:24:12433 replication_state_.frame_policy.container_policy =
Charlie Hu5ffc0152019-12-06 15:59:53434 new_frame_policy.container_policy;
Charlie Hue1b77ac2019-12-13 21:30:17435 if (did_change_required_document_policy)
436 replication_state_.frame_policy.required_document_policy =
437 new_frame_policy.required_document_policy;
Dave Tapuska9b153a942020-02-10 19:35:10438 if (did_change_document_access)
439 replication_state_.frame_policy.disallow_document_access =
440 new_frame_policy.disallow_document_access;
Charlie Hue1b77ac2019-12-13 21:30:17441
Charlie Hu5ffc0152019-12-06 15:59:53442 UpdateFramePolicyHeaders(new_frame_policy.sandbox_flags,
Ian Clellandedb8c5dd2018-03-01 17:01:37443 replication_state_.feature_policy_header);
Charlie Hue1b77ac2019-12-13 21:30:17444 return did_change_flags || did_change_container_policy ||
Dave Tapuska9b153a942020-02-10 19:35:10445 did_change_required_document_policy || did_change_document_access;
alexmos6b294562015-03-05 19:24:10446}
447
Arthur Hemeryc3380172018-01-22 14:00:17448void FrameTreeNode::TransferNavigationRequestOwnership(
449 RenderFrameHostImpl* render_frame_host) {
Andrey Kosyakovf2d4ff72018-10-29 20:09:59450 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Arthur Hemeryc3380172018-01-22 14:00:17451 render_frame_host->SetNavigationRequest(std::move(navigation_request_));
452}
453
carloskc49005eb2015-06-16 11:25:07454void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57455 std::unique_ptr<NavigationRequest> navigation_request) {
arthursonzognic79c251c2016-08-18 15:00:37456 // This is never called when navigating to a Javascript URL. For the loading
457 // state, this matches what Blink is doing: Blink doesn't send throbber
458 // notifications for Javascript URLS.
459 DCHECK(!navigation_request->common_params().url.SchemeIs(
460 url::kJavaScriptScheme));
461
clamy44e84ce2016-02-22 15:38:25462 bool was_previously_loading = frame_tree()->IsLoading();
463
clamy82a2f4d2016-02-02 14:20:41464 // There's no need to reset the state: there's still an ongoing load, and the
465 // RenderFrameHostManager will take care of updates to the speculative
466 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05467 if (was_previously_loading) {
Mohamed Abdelhalimf03d4a22019-10-01 13:34:31468 if (navigation_request_ && navigation_request_->IsNavigationStarted()) {
jamcd0b7b22017-03-24 22:13:05469 // Mark the old request as aborted.
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52470 navigation_request_->set_net_error(net::ERR_ABORTED);
jamcd0b7b22017-03-24 22:13:05471 }
Arthur Hemery241b9392019-10-24 11:08:41472 ResetNavigationRequest(true);
jamcd0b7b22017-03-24 22:13:05473 }
clamy44e84ce2016-02-22 15:38:25474
475 navigation_request_ = std::move(navigation_request);
Shubhie Panickerddf2a4e2018-03-06 00:09:06476 if (was_discarded_) {
477 navigation_request_->set_was_discarded();
478 was_discarded_ = false;
479 }
clamy8e2e299202016-04-05 11:44:59480 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39481
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51482 bool to_different_document = !NavigationTypeUtils::IsSameDocument(
arthursonzogni92f18682017-02-08 23:00:04483 navigation_request_->common_params().navigation_type);
484
485 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16486}
487
Arthur Hemery241b9392019-10-24 11:08:41488void FrameTreeNode::ResetNavigationRequest(bool keep_state) {
fdegans39ff0382015-04-29 19:04:39489 if (!navigation_request_)
490 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23491
Andrey Kosyakovf2d4ff72018-10-29 20:09:59492 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
clamydcb434c12015-04-16 19:29:16493 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39494
clamy82a2f4d2016-02-02 14:20:41495 if (keep_state)
fdegans39ff0382015-04-29 19:04:39496 return;
497
clamy82a2f4d2016-02-02 14:20:41498 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
499 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39500 DidStopLoading();
501 render_manager_.CleanUpNavigation();
clamydcb434c12015-04-16 19:29:16502}
503
clamy44e84ce2016-02-22 15:38:25504void FrameTreeNode::DidStartLoading(bool to_different_document,
505 bool was_previously_loading) {
Camille Lamyefd54b02018-10-04 16:54:14506 TRACE_EVENT2("navigation", "FrameTreeNode::DidStartLoading",
507 "frame_tree_node", frame_tree_node_id(), "to different document",
508 to_different_document);
fdegansa696e5112015-04-17 01:57:59509 // Any main frame load to a new document should reset the load progress since
510 // it will replace the current page and any frames. The WebContents will
511 // be notified when DidChangeLoadProgress is called.
512 if (to_different_document && IsMainFrame())
513 frame_tree_->ResetLoadProgress();
514
515 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25516 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59517 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
518
519 // Set initial load progress and update overall progress. This will notify
520 // the WebContents of the load progress change.
521 DidChangeLoadProgress(kLoadingProgressMinimum);
522
523 // Notify the RenderFrameHostManager of the event.
524 render_manager()->OnDidStartLoading();
525}
526
527void FrameTreeNode::DidStopLoading() {
Camille Lamyefd54b02018-10-04 16:54:14528 TRACE_EVENT1("navigation", "FrameTreeNode::DidStopLoading", "frame_tree_node",
529 frame_tree_node_id());
fdegansa696e5112015-04-17 01:57:59530 // Set final load progress and update overall progress. This will notify
531 // the WebContents of the load progress change.
532 DidChangeLoadProgress(kLoadingProgressDone);
533
Lucas Furukawa Gadani6faef602019-05-06 21:16:03534 // Notify the RenderFrameHostManager of the event.
535 render_manager()->OnDidStopLoading();
536
fdegansa696e5112015-04-17 01:57:59537 // Notify the WebContents.
538 if (!frame_tree_->IsLoading())
539 navigator()->GetDelegate()->DidStopLoading();
fdegansa696e5112015-04-17 01:57:59540}
541
542void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
Nate Chapin93536702018-02-07 00:12:21543 DCHECK_GE(load_progress, kLoadingProgressMinimum);
544 DCHECK_LE(load_progress, kLoadingProgressDone);
545 if (IsMainFrame())
546 frame_tree_->UpdateLoadProgress(load_progress);
fdegansa696e5112015-04-17 01:57:59547}
548
clamyf73862c42015-07-08 12:31:33549bool FrameTreeNode::StopLoading() {
arthursonzogni66f711c2019-10-08 14:40:36550 if (navigation_request_ && navigation_request_->IsNavigationStarted())
551 navigation_request_->set_net_error(net::ERR_ABORTED);
Arthur Hemery241b9392019-10-24 11:08:41552 ResetNavigationRequest(false);
clamyf73862c42015-07-08 12:31:33553
554 // TODO(nasko): see if child frames should send IPCs in site-per-process
555 // mode.
556 if (!IsMainFrame())
557 return true;
558
559 render_manager_.Stop();
560 return true;
561}
562
alexmos21acae52015-11-07 01:04:43563void FrameTreeNode::DidFocus() {
564 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31565 for (auto& observer : observers_)
566 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43567}
568
clamy44e84ce2016-02-22 15:38:25569void FrameTreeNode::BeforeUnloadCanceled() {
570 // TODO(clamy): Support BeforeUnload in subframes.
571 if (!IsMainFrame())
572 return;
573
574 RenderFrameHostImpl* current_frame_host =
575 render_manager_.current_frame_host();
576 DCHECK(current_frame_host);
577 current_frame_host->ResetLoadingState();
578
clamy610c63b32017-12-22 15:05:18579 RenderFrameHostImpl* speculative_frame_host =
580 render_manager_.speculative_frame_host();
581 if (speculative_frame_host)
582 speculative_frame_host->ResetLoadingState();
583 // Note: there is no need to set an error code on the NavigationHandle here
584 // as it has not been created yet. It is only created when the
Antonio Gomes8678a202020-03-02 20:03:25585 // BeforeUnloadCompleted callback is invoked.
clamy610c63b32017-12-22 15:05:18586 if (navigation_request_)
Arthur Hemery241b9392019-10-24 11:08:41587 ResetNavigationRequest(false);
clamy44e84ce2016-02-22 15:38:25588}
589
Mustaq Ahmedc4cb7162018-06-05 16:28:36590bool FrameTreeNode::NotifyUserActivation() {
Alexander Timina1dfadaa2020-04-28 13:30:06591 for (RenderFrameHostImpl* rfh = current_frame_host(); rfh;
592 rfh = rfh->GetParent()) {
593 if (!rfh->frame_tree_node()->user_activation_state_.HasBeenActive())
594 rfh->DidReceiveFirstUserActivation();
595 rfh->frame_tree_node()->user_activation_state_.Activate();
John Delaneyedd8d6c2019-01-25 00:23:57596 }
japhet61835ae12017-01-20 01:25:39597 replication_state_.has_received_user_gesture = true;
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14598
Mustaq Ahmed0180320f2019-03-21 16:07:01599 // See the "Same-origin Visibility" section in |UserActivationState| class
600 // doc.
Mustaq Ahmede5f12562019-10-30 18:02:03601 if (base::FeatureList::IsEnabled(
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14602 features::kUserActivationSameOriginVisibility)) {
603 const url::Origin& current_origin =
604 this->current_frame_host()->GetLastCommittedOrigin();
605 for (FrameTreeNode* node : frame_tree()->Nodes()) {
606 if (node->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith(
607 current_origin)) {
608 node->user_activation_state_.Activate();
609 }
610 }
611 }
612
Shivani Sharma20749e922019-03-11 17:00:26613 NavigationControllerImpl* controller =
614 static_cast<NavigationControllerImpl*>(navigator()->GetController());
615 if (controller)
616 controller->NotifyUserActivation();
Shivani Sharma194877032019-03-07 17:52:47617
Mustaq Ahmedc4cb7162018-06-05 16:28:36618 return true;
619}
620
621bool FrameTreeNode::ConsumeTransientUserActivation() {
622 bool was_active = user_activation_state_.IsActive();
623 for (FrameTreeNode* node : frame_tree()->Nodes())
624 node->user_activation_state_.ConsumeIfActive();
625 return was_active;
626}
627
Shivani Sharmac4f561582018-11-15 15:58:39628bool FrameTreeNode::ClearUserActivation() {
Shivani Sharmac4f561582018-11-15 15:58:39629 for (FrameTreeNode* node : frame_tree()->SubtreeNodes(this))
630 node->user_activation_state_.Clear();
631 return true;
632}
633
Ella Ge9caed612019-08-09 16:17:25634bool FrameTreeNode::VerifyUserActivation() {
Ella Gea78f6772019-12-11 10:35:25635 DCHECK(base::FeatureList::IsEnabled(
636 features::kBrowserVerifiedUserActivationMouse) ||
637 base::FeatureList::IsEnabled(
638 features::kBrowserVerifiedUserActivationKeyboard));
639
Ella Ge9caed612019-08-09 16:17:25640 return render_manager_.current_frame_host()
641 ->GetRenderWidgetHost()
Mustaq Ahmed83bb1722019-10-22 20:00:10642 ->RemovePendingUserActivationIfAvailable();
Ella Ge9caed612019-08-09 16:17:25643}
644
Mustaq Ahmedc4cb7162018-06-05 16:28:36645bool FrameTreeNode::UpdateUserActivationState(
Antonio Gomes4b2c5132020-01-16 11:49:48646 blink::mojom::UserActivationUpdateType update_type) {
Ella Ge9caed612019-08-09 16:17:25647 bool update_result = false;
Mustaq Ahmedc4cb7162018-06-05 16:28:36648 switch (update_type) {
Antonio Gomes4b2c5132020-01-16 11:49:48649 case blink::mojom::UserActivationUpdateType::kConsumeTransientActivation:
Ella Ge9caed612019-08-09 16:17:25650 update_result = ConsumeTransientUserActivation();
651 break;
Antonio Gomes4b2c5132020-01-16 11:49:48652 case blink::mojom::UserActivationUpdateType::kNotifyActivation:
Ella Ge9caed612019-08-09 16:17:25653 update_result = NotifyUserActivation();
654 break;
Antonio Gomes4b2c5132020-01-16 11:49:48655 case blink::mojom::UserActivationUpdateType::
Ella Ge9caed612019-08-09 16:17:25656 kNotifyActivationPendingBrowserVerification:
657 if (VerifyUserActivation()) {
658 update_result = NotifyUserActivation();
Antonio Gomes4b2c5132020-01-16 11:49:48659 update_type = blink::mojom::UserActivationUpdateType::kNotifyActivation;
Ella Ge9caed612019-08-09 16:17:25660 } else {
661 // TODO(crbug.com/848778): We need to decide what to do when user
662 // activation verification failed. NOTREACHED here will make all
663 // unrelated tests that inject event to renderer fail.
664 return false;
665 }
666 break;
Antonio Gomes4b2c5132020-01-16 11:49:48667 case blink::mojom::UserActivationUpdateType::kClearActivation:
Ella Ge9caed612019-08-09 16:17:25668 update_result = ClearUserActivation();
669 break;
Mustaq Ahmedc4cb7162018-06-05 16:28:36670 }
Ella Ge9caed612019-08-09 16:17:25671 render_manager_.UpdateUserActivationState(update_type);
672 return update_result;
japhet61835ae12017-01-20 01:25:39673}
674
Mustaq Ahmed01261742019-12-16 15:49:06675void FrameTreeNode::OnSetHadStickyUserActivationBeforeNavigation(bool value) {
676 render_manager_.OnSetHadStickyUserActivationBeforeNavigation(value);
Becca Hughes60af7d42017-12-12 10:53:15677 replication_state_.has_received_user_gesture_before_nav = value;
678}
679
paulmeyer322777fb2016-05-16 23:15:39680FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19681 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39682 return nullptr;
683
684 for (size_t i = 0; i < parent_->child_count(); ++i) {
685 if (parent_->child_at(i) == this) {
686 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
687 i + relative_offset >= parent_->child_count()) {
688 return nullptr;
689 }
690 return parent_->child_at(i + relative_offset);
691 }
692 }
693
694 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
695 return nullptr;
696}
697
Ian Clellandedb8c5dd2018-03-01 17:01:37698void FrameTreeNode::UpdateFramePolicyHeaders(
arthursonzognib93a4472020-04-10 07:38:00699 network::mojom::WebSandboxFlags sandbox_flags,
Ian Clellandedb8c5dd2018-03-01 17:01:37700 const blink::ParsedFeaturePolicy& parsed_header) {
701 bool changed = false;
702 if (replication_state_.feature_policy_header != parsed_header) {
703 replication_state_.feature_policy_header = parsed_header;
704 changed = true;
705 }
Ian Clelland5cbaaf82017-11-27 22:00:03706 // TODO(iclelland): Kill the renderer if sandbox flags is not a subset of the
707 // currently effective sandbox flags from the frame. https://crbug.com/740556
arthursonzognib93a4472020-04-10 07:38:00708 network::mojom::WebSandboxFlags updated_flags =
Ian Clelland5cbaaf82017-11-27 22:00:03709 sandbox_flags | effective_frame_policy().sandbox_flags;
Ian Clellandedb8c5dd2018-03-01 17:01:37710 if (replication_state_.active_sandbox_flags != updated_flags) {
711 replication_state_.active_sandbox_flags = updated_flags;
712 changed = true;
713 }
714 // Notify any proxies if the policies have been changed.
715 if (changed)
716 render_manager()->OnDidSetFramePolicyHeaders();
Ian Clelland5cbaaf82017-11-27 22:00:03717}
718
Lan Weif0cb8e6e2019-04-19 00:14:53719void FrameTreeNode::TransferUserActivationFrom(
720 RenderFrameHostImpl* source_rfh) {
721 user_activation_state_.TransferFrom(
722 source_rfh->frame_tree_node()->user_activation_state_);
723
724 // Notify proxies in non-source and non-target renderer processes to
725 // transfer the activation state from the source proxy to the target
726 // so the user activation state of those proxies matches the source
727 // renderer and the target renderer (which are separately updated).
728 render_manager_.TransferUserActivationFrom(source_rfh);
Lan Wei550671a22019-03-19 00:59:24729}
730
Arthur Sonzognif8840b92018-11-07 14:10:35731void FrameTreeNode::PruneChildFrameNavigationEntries(
732 NavigationEntryImpl* entry) {
733 for (size_t i = 0; i < current_frame_host()->child_count(); ++i) {
734 FrameTreeNode* child = current_frame_host()->child_at(i);
735 if (child->is_created_by_script_) {
736 entry->RemoveEntryForFrame(child,
737 /* only_if_different_position = */ false);
738 } else {
739 child->PruneChildFrameNavigationEntries(entry);
740 }
741 }
742}
743
Ehsan Karamad39407082019-02-19 23:38:19744void FrameTreeNode::SetOpenerFeaturePolicyState(
745 const blink::FeaturePolicy::FeatureState& feature_state) {
746 DCHECK(IsMainFrame());
747 if (base::FeatureList::IsEnabled(features::kFeaturePolicyForSandbox)) {
748 replication_state_.opener_feature_state = feature_state;
749 }
750}
751
Yao Xiao24ec9aa2020-01-28 16:36:00752void FrameTreeNode::SetAdFrameType(blink::mojom::AdFrameType ad_frame_type) {
753 DCHECK_NE(ad_frame_type, blink::mojom::AdFrameType::kNonAd);
754 if (replication_state_.ad_frame_type == blink::mojom::AdFrameType::kNonAd) {
755 replication_state_.ad_frame_type = ad_frame_type;
756 render_manager()->OnDidSetAdFrameType(ad_frame_type);
757 } else {
758 DCHECK_EQ(ad_frame_type, replication_state_.ad_frame_type);
759 }
760}
761
[email protected]9b159a52013-10-03 17:24:55762} // namespace content