blob: 75a74ca9ca9e5020ec928d32f40b9ed57fb4c0fe [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>
dcheng36b6aec92015-12-26 06:16:3610#include <utility>
[email protected]9b159a52013-10-03 17:24:5511
scottmg6ece5ae2017-02-01 18:25:1912#include "base/lazy_instance.h"
avib7348942015-12-25 20:57:1013#include "base/macros.h"
dcheng9bfa5162016-04-09 01:00:5714#include "base/memory/ptr_util.h"
dcheng23ca947d2016-05-04 20:04:1515#include "base/metrics/histogram_macros.h"
[email protected]9b159a52013-10-03 17:24:5516#include "base/stl_util.h"
Daniel Cheng6ca7f1c92017-08-09 21:45:4117#include "base/strings/string_util.h"
[email protected]94d0cc12013-12-18 00:07:4118#include "content/browser/frame_host/frame_tree.h"
clamydcb434c12015-04-16 19:29:1619#include "content/browser/frame_host/navigation_request.h"
[email protected]190b8c52013-11-09 01:35:4420#include "content/browser/frame_host/navigator.h"
[email protected]d4a8ca482013-10-30 21:06:4021#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4122#include "content/browser/renderer_host/render_view_host_impl.h"
clamyf73862c42015-07-08 12:31:3323#include "content/common/frame_messages.h"
nickd30fd962015-07-27 21:51:0824#include "content/common/site_isolation_policy.h"
dmazzonie950ea232015-03-13 21:39:4525#include "content/public/browser/browser_thread.h"
carloskd80262f52015-12-16 14:40:3526#include "content/public/common/browser_side_navigation_policy.h"
Luna Luc3fdacdf2017-11-08 04:48:5327#include "third_party/WebKit/common/sandbox_flags.h"
[email protected]9b159a52013-10-03 17:24:5528
29namespace content {
30
dmazzonie950ea232015-03-13 21:39:4531namespace {
32
33// This is a global map between frame_tree_node_ids and pointers to
34// FrameTreeNodes.
rob97250742015-12-10 17:45:1535typedef base::hash_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4536
scottmg5e65e3a2017-03-08 08:48:4637base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
38 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4539
fdegansa696e5112015-04-17 01:57:5940// These values indicate the loading progress status. The minimum progress
41// value matches what Blink's ProgressTracker has traditionally used for a
42// minimum progress value.
43const double kLoadingProgressNotStarted = 0.0;
44const double kLoadingProgressMinimum = 0.1;
45const double kLoadingProgressDone = 1.0;
dmazzonie950ea232015-03-13 21:39:4546
Daniel Cheng6ca7f1c92017-08-09 21:45:4147void RecordUniqueNameSize(FrameTreeNode* node) {
48 const auto& unique_name = node->current_replication_state().unique_name;
49
50 // Don't record numbers for the root node, which always has an empty unique
51 // name.
52 if (!node->parent()) {
53 DCHECK(unique_name.empty());
54 return;
55 }
56
57 // The original requested name is derived from the browsing context name and
58 // is essentially unbounded in size...
59 UMA_HISTOGRAM_COUNTS_1M(
60 "SessionRestore.FrameUniqueNameOriginalRequestedNameSize",
61 node->current_replication_state().name.size());
62 // If the name is a frame path, attempt to normalize the statistics based on
63 // the number of frames in the frame path.
64 if (base::StartsWith(unique_name, "<!--framePath //",
65 base::CompareCase::SENSITIVE)) {
66 size_t depth = 1;
67 while (node->parent()) {
68 ++depth;
69 node = node->parent();
70 }
71 // The max possible size of a unique name is 80 characters, so the expected
72 // size per component shouldn't be much more than that.
73 UMA_HISTOGRAM_COUNTS_100(
74 "SessionRestore.FrameUniqueNameWithFramePathSizePerComponent",
75 round(unique_name.size() / static_cast<float>(depth)));
76 // Blink allows a maximum of ~1024 subframes in a document, so this should
77 // be less than (80 character name + 1 character delimiter) * 1024.
78 UMA_HISTOGRAM_COUNTS_100000(
79 "SessionRestore.FrameUniqueNameWithFramePathSize", unique_name.size());
80 } else {
81 UMA_HISTOGRAM_COUNTS_100(
82 "SessionRestore.FrameUniqueNameFromRequestedNameSize",
83 unique_name.size());
84 }
dcheng23ca947d2016-05-04 20:04:1585}
86
fdegansa696e5112015-04-17 01:57:5987} // namespace
fdegans1d16355162015-03-26 11:58:3488
alexmose201c7cd2015-06-10 17:14:2189// This observer watches the opener of its owner FrameTreeNode and clears the
90// owner's opener if the opener is destroyed.
91class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
92 public:
jochen6004a362017-02-04 00:11:4093 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
94 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2195
96 // FrameTreeNode::Observer
97 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
jochen6004a362017-02-04 00:11:4098 if (observing_original_opener_) {
Avi Drissman36465f332017-09-11 20:49:3999 // The "original owner" is special. It's used for attribution, and clients
100 // walk down the original owner chain. Therefore, if a link in the chain
101 // is being destroyed, reconnect the observation to the parent of the link
102 // being destroyed.
jochen6004a362017-02-04 00:11:40103 CHECK_EQ(owner_->original_opener(), node);
Avi Drissman36465f332017-09-11 20:49:39104 owner_->SetOriginalOpener(node->original_opener());
105 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40106 } else {
107 CHECK_EQ(owner_->opener(), node);
108 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:39109 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40110 }
alexmose201c7cd2015-06-10 17:14:21111 }
112
113 private:
114 FrameTreeNode* owner_;
jochen6004a362017-02-04 00:11:40115 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:21116
117 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver);
118};
119
vishal.b782eb5d2015-04-29 12:22:57120int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:55121
dmazzonie950ea232015-03-13 21:39:45122// static
vishal.b782eb5d2015-04-29 12:22:57123FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:19124 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:15125 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
126 FrameTreeNodeIdMap::iterator it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:45127 return it == nodes->end() ? nullptr : it->second;
128}
129
raymes31457802016-07-20 06:08:09130FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
131 Navigator* navigator,
132 RenderFrameHostDelegate* render_frame_delegate,
133 RenderWidgetHostDelegate* render_widget_delegate,
134 RenderFrameHostManager::Delegate* manager_delegate,
135 FrameTreeNode* parent,
136 blink::WebTreeScopeType scope,
137 const std::string& name,
138 const std::string& unique_name,
Pavel Feldman25234722017-10-11 02:49:06139 const base::UnguessableToken& devtools_frame_token,
raymes31457802016-07-20 06:08:09140 const FrameOwnerProperties& frame_owner_properties)
[email protected]bffc8302014-01-23 20:52:16141 : frame_tree_(frame_tree),
142 navigator_(navigator),
143 render_manager_(this,
144 render_frame_delegate,
[email protected]bffc8302014-01-23 20:52:16145 render_widget_delegate,
146 manager_delegate),
147 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59148 parent_(parent),
alexmose201c7cd2015-06-10 17:14:21149 opener_(nullptr),
jochen6004a362017-02-04 00:11:40150 original_opener_(nullptr),
creisf0f069a2015-07-23 23:51:53151 has_committed_real_load_(false),
engedy6e2e0992017-05-25 18:58:42152 is_collapsed_(false),
estarka886b8d2015-12-18 21:53:08153 replication_state_(
154 scope,
155 name,
lukasza464d8692016-02-22 19:26:32156 unique_name,
estarkbd8e26f2016-03-16 23:30:37157 false /* should enforce strict mixed content checking */,
japhet61835ae12017-01-20 01:25:39158 false /* is a potentially trustworthy unique origin */,
159 false /* has received a user gesture */),
Pavel Feldman25234722017-10-11 02:49:06160 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31161 frame_owner_properties_(frame_owner_properties),
xiaochenghb9554bb2016-05-21 14:20:48162 loading_progress_(kLoadingProgressNotStarted),
163 blame_context_(frame_tree_node_id_, parent) {
rob97250742015-12-10 17:45:15164 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45165 g_frame_tree_node_id_map.Get().insert(
166 std::make_pair(frame_tree_node_id_, this));
167 CHECK(result.second);
benjhaydend4da63d2016-03-11 21:29:33168
Daniel Cheng6ca7f1c92017-08-09 21:45:41169 RecordUniqueNameSize(this);
xiaochenghb9554bb2016-05-21 14:20:48170
171 // Note: this should always be done last in the constructor.
172 blame_context_.Initialize();
alexmos998581d2015-01-22 01:01:59173}
[email protected]9b159a52013-10-03 17:24:55174
175FrameTreeNode::~FrameTreeNode() {
paulmeyerf3119f52016-05-17 17:37:19176 std::vector<std::unique_ptr<FrameTreeNode>>().swap(children_);
dmazzonie950ea232015-03-13 21:39:45177 frame_tree_->FrameRemoved(this);
ericwilligers254597b2016-10-17 10:32:31178 for (auto& observer : observers_)
179 observer.OnFrameTreeNodeDestroyed(this);
alexmose201c7cd2015-06-10 17:14:21180
181 if (opener_)
182 opener_->RemoveObserver(opener_observer_.get());
jochen6004a362017-02-04 00:11:40183 if (original_opener_)
184 original_opener_->RemoveObserver(original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45185
186 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18187
188 if (navigation_request_) {
189 // PlzNavigate: if a frame with a pending navigation is detached, make sure
190 // the WebContents (and its observers) update their loading state.
191 navigation_request_.reset();
192 DidStopLoading();
193 }
[email protected]9b159a52013-10-03 17:24:55194}
195
alexmose201c7cd2015-06-10 17:14:21196void FrameTreeNode::AddObserver(Observer* observer) {
197 observers_.AddObserver(observer);
198}
199
200void FrameTreeNode::RemoveObserver(Observer* observer) {
201 observers_.RemoveObserver(observer);
202}
203
[email protected]94d0cc12013-12-18 00:07:41204bool FrameTreeNode::IsMainFrame() const {
205 return frame_tree_->root() == this;
206}
207
dcheng9bfa5162016-04-09 01:00:57208FrameTreeNode* FrameTreeNode::AddChild(std::unique_ptr<FrameTreeNode> child,
nick8814e652015-12-18 01:44:12209 int process_id,
210 int frame_routing_id) {
dgroganfb22f9a2014-10-20 21:32:32211 // Child frame must always be created in the same process as the parent.
212 CHECK_EQ(process_id, render_manager_.current_host()->GetProcess()->GetID());
213
[email protected]94d0cc12013-12-18 00:07:41214 // Initialize the RenderFrameHost for the new node. We always create child
215 // frames in the same SiteInstance as the current frame, and they can swap to
216 // a different one if they navigate away.
217 child->render_manager()->Init(
[email protected]94d0cc12013-12-18 00:07:41218 render_manager_.current_host()->GetSiteInstance(),
dcheng29f5a6c2015-08-31 21:43:27219 render_manager_.current_host()->GetRoutingID(), frame_routing_id,
jambaaab3a2016-11-05 00:46:18220 MSG_ROUTING_NONE, false);
alexmos46e85ec2015-04-03 21:04:35221
222 // Other renderer processes in this BrowsingInstance may need to find out
223 // about the new frame. Create a proxy for the child frame in all
224 // SiteInstances that have a proxy for the frame's parent, since all frames
225 // in a frame tree should have the same set of proxies.
Ken Buchanan077f50312017-08-23 15:34:47226 render_manager_.CreateProxiesForChildFrame(child.get());
alexmos46e85ec2015-04-03 21:04:35227
dcheng36b6aec92015-12-26 06:16:36228 children_.push_back(std::move(child));
nick8814e652015-12-18 01:44:12229 return children_.back().get();
[email protected]9b159a52013-10-03 17:24:55230}
231
[email protected]741fd682013-11-08 08:26:55232void FrameTreeNode::RemoveChild(FrameTreeNode* child) {
nickb6769e632015-11-13 23:25:18233 for (auto iter = children_.begin(); iter != children_.end(); ++iter) {
234 if (iter->get() == child) {
235 // Subtle: we need to make sure the node is gone from the tree before
236 // observers are notified of its deletion.
dcheng9bfa5162016-04-09 01:00:57237 std::unique_ptr<FrameTreeNode> node_to_delete(std::move(*iter));
nickb6769e632015-11-13 23:25:18238 children_.erase(iter);
239 node_to_delete.reset();
240 return;
241 }
[email protected]bffc8302014-01-23 20:52:16242 }
[email protected]9b159a52013-10-03 17:24:55243}
244
[email protected]81c6c5e2014-02-13 20:20:07245void FrameTreeNode::ResetForNewProcess() {
Erik Chen173bf3042017-07-31 06:06:21246 current_frame_host()->SetLastCommittedUrl(GURL());
xiaochenghb9554bb2016-05-21 14:20:48247 blame_context_.TakeSnapshot();
[email protected]482ce3c2013-11-27 18:17:09248
nickb6769e632015-11-13 23:25:18249 // Remove child nodes from the tree, then delete them. This destruction
250 // operation will notify observers.
dcheng9bfa5162016-04-09 01:00:57251 std::vector<std::unique_ptr<FrameTreeNode>>().swap(children_);
[email protected]9b159a52013-10-03 17:24:55252}
253
alexmose201c7cd2015-06-10 17:14:21254void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
255 if (opener_) {
256 opener_->RemoveObserver(opener_observer_.get());
257 opener_observer_.reset();
258 }
259
260 opener_ = opener;
261
262 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55263 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21264 opener_->AddObserver(opener_observer_.get());
265 }
266}
267
jochen6004a362017-02-04 00:11:40268void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39269 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37270 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40271
Avi Drissman36465f332017-09-11 20:49:39272 if (original_opener_) {
273 original_opener_->RemoveObserver(original_opener_observer_.get());
274 original_opener_observer_.reset();
275 }
276
jochen6004a362017-02-04 00:11:40277 original_opener_ = opener;
278
279 if (original_opener_) {
jochen6004a362017-02-04 00:11:40280 original_opener_observer_ =
Jeremy Roman04f27c372017-10-27 15:20:55281 std::make_unique<OpenerDestroyedObserver>(this, true);
jochen6004a362017-02-04 00:11:40282 original_opener_->AddObserver(original_opener_observer_.get());
283 }
284}
285
creisf0f069a2015-07-23 23:51:53286void FrameTreeNode::SetCurrentURL(const GURL& url) {
csharrisona3bd0b32016-10-19 18:40:48287 if (!has_committed_real_load_ && url != url::kAboutBlankURL)
creisf0f069a2015-07-23 23:51:53288 has_committed_real_load_ = true;
Erik Chen173bf3042017-07-31 06:06:21289 current_frame_host()->SetLastCommittedUrl(url);
xiaochenghb9554bb2016-05-21 14:20:48290 blame_context_.TakeSnapshot();
creisf0f069a2015-07-23 23:51:53291}
292
estarkbd8e26f2016-03-16 23:30:37293void FrameTreeNode::SetCurrentOrigin(
294 const url::Origin& origin,
295 bool is_potentially_trustworthy_unique_origin) {
296 if (!origin.IsSameOriginWith(replication_state_.origin) ||
297 replication_state_.has_potentially_trustworthy_unique_origin !=
298 is_potentially_trustworthy_unique_origin) {
299 render_manager_.OnDidUpdateOrigin(origin,
300 is_potentially_trustworthy_unique_origin);
301 }
alexmosa7a4ff822015-04-27 17:59:56302 replication_state_.origin = origin;
estarkbd8e26f2016-03-16 23:30:37303 replication_state_.has_potentially_trustworthy_unique_origin =
304 is_potentially_trustworthy_unique_origin;
alexmosa7a4ff822015-04-27 17:59:56305}
alexmosbe2f4c32015-03-10 02:30:23306
engedy6e2e0992017-05-25 18:58:42307void FrameTreeNode::SetCollapsed(bool collapsed) {
308 DCHECK(!IsMainFrame());
309 if (is_collapsed_ == collapsed)
310 return;
311
312 is_collapsed_ = collapsed;
313 render_manager_.OnDidChangeCollapsedState(collapsed);
314}
315
lukasza464d8692016-02-22 19:26:32316void FrameTreeNode::SetFrameName(const std::string& name,
317 const std::string& unique_name) {
318 if (name == replication_state_.name) {
319 // |unique_name| shouldn't change unless |name| changes.
320 DCHECK_EQ(unique_name, replication_state_.unique_name);
321 return;
322 }
lukasza5140a412016-09-15 21:12:30323
324 if (parent()) {
325 // Non-main frames should have a non-empty unique name.
326 DCHECK(!unique_name.empty());
327 } else {
328 // Unique name of main frames should always stay empty.
329 DCHECK(unique_name.empty());
330 }
331
Daniel Cheng6ca7f1c92017-08-09 21:45:41332 // Note the unique name should only be able to change before the first real
333 // load is committed, but that's not strongly enforced here.
334 if (unique_name != replication_state_.unique_name)
335 RecordUniqueNameSize(this);
lukasza464d8692016-02-22 19:26:32336 render_manager_.OnDidUpdateName(name, unique_name);
alexmosa7a4ff822015-04-27 17:59:56337 replication_state_.name = name;
lukasza464d8692016-02-22 19:26:32338 replication_state_.unique_name = unique_name;
alexmosbe2f4c32015-03-10 02:30:23339}
340
raymesd405a052016-12-05 23:41:34341void FrameTreeNode::SetFeaturePolicyHeader(
Luna Lu2e713992017-11-07 01:45:58342 const blink::ParsedFeaturePolicy& parsed_header) {
raymesd405a052016-12-05 23:41:34343 replication_state_.feature_policy_header = parsed_header;
iclellandab749ec92016-11-23 02:00:43344}
345
iclelland2c79efe22017-02-09 22:44:03346void FrameTreeNode::ResetFeaturePolicyHeader() {
iclellandab749ec92016-11-23 02:00:43347 replication_state_.feature_policy_header.clear();
348}
349
arthursonzogni662aa652017-03-28 11:09:50350void FrameTreeNode::AddContentSecurityPolicies(
351 const std::vector<ContentSecurityPolicyHeader>& headers) {
352 replication_state_.accumulated_csp_headers.insert(
353 replication_state_.accumulated_csp_headers.end(), headers.begin(),
354 headers.end());
355 render_manager_.OnDidAddContentSecurityPolicies(headers);
lukasza8e1c02e42016-05-17 20:05:10356}
357
arthursonzogni7fed384c2017-03-18 03:07:34358void FrameTreeNode::ResetCspHeaders() {
lukasza8e1c02e42016-05-17 20:05:10359 replication_state_.accumulated_csp_headers.clear();
360 render_manager_.OnDidResetContentSecurityPolicy();
361}
362
mkwstf672e7ef2016-06-09 20:51:07363void FrameTreeNode::SetInsecureRequestPolicy(
364 blink::WebInsecureRequestPolicy policy) {
365 if (policy == replication_state_.insecure_request_policy)
estarka886b8d2015-12-18 21:53:08366 return;
mkwstf672e7ef2016-06-09 20:51:07367 render_manager_.OnEnforceInsecureRequestPolicy(policy);
368 replication_state_.insecure_request_policy = policy;
estarka886b8d2015-12-18 21:53:08369}
370
Luna Luc3fdacdf2017-11-08 04:48:53371void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Ian Clellandcdc4f312017-10-13 22:24:12372 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25373
Ian Clellandcdc4f312017-10-13 22:24:12374 if (parent()) {
375 // Subframes should always inherit their parent's sandbox flags.
376 pending_frame_policy_.sandbox_flags |=
377 parent()->effective_frame_policy().sandbox_flags;
378 // This is only applied on subframes; container policy is not mutable on
379 // main frame.
380 pending_frame_policy_.container_policy = frame_policy.container_policy;
381 }
iclelland92f8c0b2017-04-19 12:43:05382}
383
mlamouria85eb3f2015-01-26 17:36:27384bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
385 if (!other || !other->child_count())
386 return false;
387
388 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
389 if (node == other)
390 return true;
391 }
392
393 return false;
394}
395
alexmos9f8705a2015-05-06 19:58:59396FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39397 return GetSibling(-1);
398}
alexmos9f8705a2015-05-06 19:58:59399
paulmeyer322777fb2016-05-16 23:15:39400FrameTreeNode* FrameTreeNode::NextSibling() const {
401 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59402}
403
fdegans4a49ce932015-03-12 17:11:37404bool FrameTreeNode::IsLoading() const {
405 RenderFrameHostImpl* current_frame_host =
406 render_manager_.current_frame_host();
407 RenderFrameHostImpl* pending_frame_host =
408 render_manager_.pending_frame_host();
409
410 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39411
carloskd80262f52015-12-16 14:40:35412 if (IsBrowserSideNavigationEnabled()) {
fdegans39ff0382015-04-29 19:04:39413 if (navigation_request_)
414 return true;
clamy11e11512015-07-07 16:42:17415
416 RenderFrameHostImpl* speculative_frame_host =
417 render_manager_.speculative_frame_host();
418 if (speculative_frame_host && speculative_frame_host->is_loading())
419 return true;
fdegans39ff0382015-04-29 19:04:39420 } else {
421 if (pending_frame_host && pending_frame_host->is_loading())
422 return true;
423 }
fdegans4a49ce932015-03-12 17:11:37424 return current_frame_host->is_loading();
425}
426
iclelland92f8c0b2017-04-19 12:43:05427bool FrameTreeNode::CommitPendingFramePolicy() {
Ian Clellandcdc4f312017-10-13 22:24:12428 bool did_change_flags = pending_frame_policy_.sandbox_flags !=
429 replication_state_.frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05430 bool did_change_container_policy =
Ian Clellandcdc4f312017-10-13 22:24:12431 pending_frame_policy_.container_policy !=
432 replication_state_.frame_policy.container_policy;
iclelland92f8c0b2017-04-19 12:43:05433 if (did_change_flags)
Ian Clellandcdc4f312017-10-13 22:24:12434 replication_state_.frame_policy.sandbox_flags =
435 pending_frame_policy_.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05436 if (did_change_container_policy)
Ian Clellandcdc4f312017-10-13 22:24:12437 replication_state_.frame_policy.container_policy =
438 pending_frame_policy_.container_policy;
iclelland92f8c0b2017-04-19 12:43:05439 return did_change_flags || did_change_container_policy;
alexmos6b294562015-03-05 19:24:10440}
441
carloskc49005eb2015-06-16 11:25:07442void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57443 std::unique_ptr<NavigationRequest> navigation_request) {
carloskd80262f52015-12-16 14:40:35444 CHECK(IsBrowserSideNavigationEnabled());
clamy82a2f4d2016-02-02 14:20:41445
arthursonzognic79c251c2016-08-18 15:00:37446 // This is never called when navigating to a Javascript URL. For the loading
447 // state, this matches what Blink is doing: Blink doesn't send throbber
448 // notifications for Javascript URLS.
449 DCHECK(!navigation_request->common_params().url.SchemeIs(
450 url::kJavaScriptScheme));
451
clamy44e84ce2016-02-22 15:38:25452 bool was_previously_loading = frame_tree()->IsLoading();
453
clamy82a2f4d2016-02-02 14:20:41454 // There's no need to reset the state: there's still an ongoing load, and the
455 // RenderFrameHostManager will take care of updates to the speculative
456 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05457 if (was_previously_loading) {
clamy080e7962017-05-25 00:44:18458 if (navigation_request_ && navigation_request_->navigation_handle()) {
jamcd0b7b22017-03-24 22:13:05459 // Mark the old request as aborted.
460 navigation_request_->navigation_handle()->set_net_error_code(
461 net::ERR_ABORTED);
462 }
clamya86695b2017-03-23 14:45:48463 ResetNavigationRequest(true, true);
jamcd0b7b22017-03-24 22:13:05464 }
clamy44e84ce2016-02-22 15:38:25465
466 navigation_request_ = std::move(navigation_request);
clamy8e2e299202016-04-05 11:44:59467 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39468
arthursonzogni92f18682017-02-08 23:00:04469 bool to_different_document = !FrameMsg_Navigate_Type::IsSameDocument(
470 navigation_request_->common_params().navigation_type);
471
472 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16473}
474
clamya86695b2017-03-23 14:45:48475void FrameTreeNode::ResetNavigationRequest(bool keep_state,
476 bool inform_renderer) {
carloskd80262f52015-12-16 14:40:35477 CHECK(IsBrowserSideNavigationEnabled());
fdegans39ff0382015-04-29 19:04:39478 if (!navigation_request_)
479 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23480
481 // The renderer should be informed if the caller allows to do so and the
482 // navigation came from a BeginNavigation IPC.
483 int need_to_inform_renderer =
484 inform_renderer && navigation_request_->from_begin_navigation();
485
clamy8e2e299202016-04-05 11:44:59486 NavigationRequest::AssociatedSiteInstanceType site_instance_type =
487 navigation_request_->associated_site_instance_type();
clamydcb434c12015-04-16 19:29:16488 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39489
clamy82a2f4d2016-02-02 14:20:41490 if (keep_state)
fdegans39ff0382015-04-29 19:04:39491 return;
492
clamy82a2f4d2016-02-02 14:20:41493 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
494 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39495 DidStopLoading();
496 render_manager_.CleanUpNavigation();
clamy2567242a2016-02-22 18:27:38497
clamy8e2e299202016-04-05 11:44:59498 // When reusing the same SiteInstance, a pending WebUI may have been created
499 // on behalf of the navigation in the current RenderFrameHost. Clear it.
500 if (site_instance_type ==
501 NavigationRequest::AssociatedSiteInstanceType::CURRENT) {
502 current_frame_host()->ClearPendingWebUI();
503 }
504
clamy2567242a2016-02-22 18:27:38505 // If the navigation is renderer-initiated, the renderer should also be
clamya86695b2017-03-23 14:45:48506 // informed that the navigation stopped if needed. In the case the renderer
507 // process asked for the navigation to be aborted, e.g. following a
508 // document.open, do not send an IPC to the renderer process as it already
509 // expects the navigation to stop.
John Abd-El-Malekdcc7bf42017-09-12 22:30:23510 if (need_to_inform_renderer) {
clamy2567242a2016-02-22 18:27:38511 current_frame_host()->Send(
John Abd-El-Malekdcc7bf42017-09-12 22:30:23512 new FrameMsg_DroppedNavigation(current_frame_host()->GetRoutingID()));
clamy2567242a2016-02-22 18:27:38513 }
514
clamydcb434c12015-04-16 19:29:16515}
516
fdegansa696e5112015-04-17 01:57:59517bool FrameTreeNode::has_started_loading() const {
518 return loading_progress_ != kLoadingProgressNotStarted;
519}
520
521void FrameTreeNode::reset_loading_progress() {
522 loading_progress_ = kLoadingProgressNotStarted;
523}
524
clamy44e84ce2016-02-22 15:38:25525void FrameTreeNode::DidStartLoading(bool to_different_document,
526 bool was_previously_loading) {
fdegansa696e5112015-04-17 01:57:59527 // Any main frame load to a new document should reset the load progress since
528 // it will replace the current page and any frames. The WebContents will
529 // be notified when DidChangeLoadProgress is called.
530 if (to_different_document && IsMainFrame())
531 frame_tree_->ResetLoadProgress();
532
533 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25534 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59535 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
536
537 // Set initial load progress and update overall progress. This will notify
538 // the WebContents of the load progress change.
539 DidChangeLoadProgress(kLoadingProgressMinimum);
540
541 // Notify the RenderFrameHostManager of the event.
542 render_manager()->OnDidStartLoading();
543}
544
545void FrameTreeNode::DidStopLoading() {
fdegansa696e5112015-04-17 01:57:59546 // Set final load progress and update overall progress. This will notify
547 // the WebContents of the load progress change.
548 DidChangeLoadProgress(kLoadingProgressDone);
549
fdegansa696e5112015-04-17 01:57:59550 // Notify the WebContents.
551 if (!frame_tree_->IsLoading())
552 navigator()->GetDelegate()->DidStopLoading();
553
fdegansa696e5112015-04-17 01:57:59554 // Notify the RenderFrameHostManager of the event.
555 render_manager()->OnDidStopLoading();
fdegansa696e5112015-04-17 01:57:59556}
557
558void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
559 loading_progress_ = load_progress;
560 frame_tree_->UpdateLoadProgress();
561}
562
clamyf73862c42015-07-08 12:31:33563bool FrameTreeNode::StopLoading() {
jam0299edae2017-03-10 00:49:22564 if (IsBrowserSideNavigationEnabled()) {
565 if (navigation_request_) {
clamy080e7962017-05-25 00:44:18566 int expected_pending_nav_entry_id = navigation_request_->nav_entry_id();
567 if (navigation_request_->navigation_handle()) {
568 navigation_request_->navigation_handle()->set_net_error_code(
569 net::ERR_ABORTED);
570 expected_pending_nav_entry_id =
571 navigation_request_->navigation_handle()->pending_nav_entry_id();
572 }
573 navigator_->DiscardPendingEntryIfNeeded(expected_pending_nav_entry_id);
jam0299edae2017-03-10 00:49:22574 }
clamya86695b2017-03-23 14:45:48575 ResetNavigationRequest(false, true);
jam0299edae2017-03-10 00:49:22576 }
clamyf73862c42015-07-08 12:31:33577
578 // TODO(nasko): see if child frames should send IPCs in site-per-process
579 // mode.
580 if (!IsMainFrame())
581 return true;
582
583 render_manager_.Stop();
584 return true;
585}
586
alexmos21acae52015-11-07 01:04:43587void FrameTreeNode::DidFocus() {
588 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31589 for (auto& observer : observers_)
590 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43591}
592
clamy44e84ce2016-02-22 15:38:25593void FrameTreeNode::BeforeUnloadCanceled() {
594 // TODO(clamy): Support BeforeUnload in subframes.
595 if (!IsMainFrame())
596 return;
597
598 RenderFrameHostImpl* current_frame_host =
599 render_manager_.current_frame_host();
600 DCHECK(current_frame_host);
601 current_frame_host->ResetLoadingState();
602
603 if (IsBrowserSideNavigationEnabled()) {
604 RenderFrameHostImpl* speculative_frame_host =
605 render_manager_.speculative_frame_host();
606 if (speculative_frame_host)
607 speculative_frame_host->ResetLoadingState();
clamy080e7962017-05-25 00:44:18608 // Note: there is no need to set an error code on the NavigationHandle here
609 // as it has not been created yet. It is only created when the
610 // BeforeUnloadACK is received.
611 if (navigation_request_)
612 ResetNavigationRequest(false, true);
clamy44e84ce2016-02-22 15:38:25613 } else {
614 RenderFrameHostImpl* pending_frame_host =
615 render_manager_.pending_frame_host();
616 if (pending_frame_host)
617 pending_frame_host->ResetLoadingState();
618 }
619}
620
japhet61835ae12017-01-20 01:25:39621void FrameTreeNode::OnSetHasReceivedUserGesture() {
622 render_manager_.OnSetHasReceivedUserGesture();
623 replication_state_.has_received_user_gesture = true;
624}
625
paulmeyer322777fb2016-05-16 23:15:39626FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19627 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39628 return nullptr;
629
630 for (size_t i = 0; i < parent_->child_count(); ++i) {
631 if (parent_->child_at(i) == this) {
632 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
633 i + relative_offset >= parent_->child_count()) {
634 return nullptr;
635 }
636 return parent_->child_at(i + relative_offset);
637 }
638 }
639
640 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
641 return nullptr;
642}
643
[email protected]9b159a52013-10-03 17:24:55644} // namespace content