blob: bb8dc30c1ca298b0a355fab0aa0fb83c8a2acff3 [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"
Pavel Feldmand8352ac2017-11-10 00:37:4118#include "content/browser/devtools/render_frame_devtools_agent_host.h"
[email protected]94d0cc12013-12-18 00:07:4119#include "content/browser/frame_host/frame_tree.h"
clamydcb434c12015-04-16 19:29:1620#include "content/browser/frame_host/navigation_request.h"
[email protected]190b8c52013-11-09 01:35:4421#include "content/browser/frame_host/navigator.h"
[email protected]d4a8ca482013-10-30 21:06:4022#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4123#include "content/browser/renderer_host/render_view_host_impl.h"
clamyf73862c42015-07-08 12:31:3324#include "content/common/frame_messages.h"
nickd30fd962015-07-27 21:51:0825#include "content/common/site_isolation_policy.h"
dmazzonie950ea232015-03-13 21:39:4526#include "content/public/browser/browser_thread.h"
carloskd80262f52015-12-16 14:40:3527#include "content/public/common/browser_side_navigation_policy.h"
Luna Luc3fdacdf2017-11-08 04:48:5328#include "third_party/WebKit/common/sandbox_flags.h"
[email protected]9b159a52013-10-03 17:24:5529
30namespace content {
31
dmazzonie950ea232015-03-13 21:39:4532namespace {
33
34// This is a global map between frame_tree_node_ids and pointers to
35// FrameTreeNodes.
rob97250742015-12-10 17:45:1536typedef base::hash_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4537
scottmg5e65e3a2017-03-08 08:48:4638base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
39 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4540
fdegansa696e5112015-04-17 01:57:5941// These values indicate the loading progress status. The minimum progress
42// value matches what Blink's ProgressTracker has traditionally used for a
43// minimum progress value.
44const double kLoadingProgressNotStarted = 0.0;
45const double kLoadingProgressMinimum = 0.1;
46const double kLoadingProgressDone = 1.0;
dmazzonie950ea232015-03-13 21:39:4547
Daniel Cheng6ca7f1c92017-08-09 21:45:4148void RecordUniqueNameSize(FrameTreeNode* node) {
49 const auto& unique_name = node->current_replication_state().unique_name;
50
51 // Don't record numbers for the root node, which always has an empty unique
52 // name.
53 if (!node->parent()) {
54 DCHECK(unique_name.empty());
55 return;
56 }
57
58 // The original requested name is derived from the browsing context name and
59 // is essentially unbounded in size...
60 UMA_HISTOGRAM_COUNTS_1M(
61 "SessionRestore.FrameUniqueNameOriginalRequestedNameSize",
62 node->current_replication_state().name.size());
63 // If the name is a frame path, attempt to normalize the statistics based on
64 // the number of frames in the frame path.
65 if (base::StartsWith(unique_name, "<!--framePath //",
66 base::CompareCase::SENSITIVE)) {
67 size_t depth = 1;
68 while (node->parent()) {
69 ++depth;
70 node = node->parent();
71 }
72 // The max possible size of a unique name is 80 characters, so the expected
73 // size per component shouldn't be much more than that.
74 UMA_HISTOGRAM_COUNTS_100(
75 "SessionRestore.FrameUniqueNameWithFramePathSizePerComponent",
76 round(unique_name.size() / static_cast<float>(depth)));
77 // Blink allows a maximum of ~1024 subframes in a document, so this should
78 // be less than (80 character name + 1 character delimiter) * 1024.
79 UMA_HISTOGRAM_COUNTS_100000(
80 "SessionRestore.FrameUniqueNameWithFramePathSize", unique_name.size());
81 } else {
82 UMA_HISTOGRAM_COUNTS_100(
83 "SessionRestore.FrameUniqueNameFromRequestedNameSize",
84 unique_name.size());
85 }
dcheng23ca947d2016-05-04 20:04:1586}
87
fdegansa696e5112015-04-17 01:57:5988} // namespace
fdegans1d16355162015-03-26 11:58:3489
alexmose201c7cd2015-06-10 17:14:2190// This observer watches the opener of its owner FrameTreeNode and clears the
91// owner's opener if the opener is destroyed.
92class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
93 public:
jochen6004a362017-02-04 00:11:4094 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
95 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2196
97 // FrameTreeNode::Observer
98 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
jochen6004a362017-02-04 00:11:4099 if (observing_original_opener_) {
Avi Drissman36465f332017-09-11 20:49:39100 // The "original owner" is special. It's used for attribution, and clients
101 // walk down the original owner chain. Therefore, if a link in the chain
102 // is being destroyed, reconnect the observation to the parent of the link
103 // being destroyed.
jochen6004a362017-02-04 00:11:40104 CHECK_EQ(owner_->original_opener(), node);
Avi Drissman36465f332017-09-11 20:49:39105 owner_->SetOriginalOpener(node->original_opener());
106 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40107 } else {
108 CHECK_EQ(owner_->opener(), node);
109 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:39110 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:40111 }
alexmose201c7cd2015-06-10 17:14:21112 }
113
114 private:
115 FrameTreeNode* owner_;
jochen6004a362017-02-04 00:11:40116 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:21117
118 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver);
119};
120
vishal.b782eb5d2015-04-29 12:22:57121int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:55122
dmazzonie950ea232015-03-13 21:39:45123// static
vishal.b782eb5d2015-04-29 12:22:57124FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:19125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:15126 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
127 FrameTreeNodeIdMap::iterator it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:45128 return it == nodes->end() ? nullptr : it->second;
129}
130
raymes31457802016-07-20 06:08:09131FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
132 Navigator* navigator,
133 RenderFrameHostDelegate* render_frame_delegate,
134 RenderWidgetHostDelegate* render_widget_delegate,
135 RenderFrameHostManager::Delegate* manager_delegate,
136 FrameTreeNode* parent,
137 blink::WebTreeScopeType scope,
138 const std::string& name,
139 const std::string& unique_name,
Pavel Feldman25234722017-10-11 02:49:06140 const base::UnguessableToken& devtools_frame_token,
raymes31457802016-07-20 06:08:09141 const FrameOwnerProperties& frame_owner_properties)
[email protected]bffc8302014-01-23 20:52:16142 : frame_tree_(frame_tree),
143 navigator_(navigator),
144 render_manager_(this,
145 render_frame_delegate,
[email protected]bffc8302014-01-23 20:52:16146 render_widget_delegate,
147 manager_delegate),
148 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59149 parent_(parent),
alexmose201c7cd2015-06-10 17:14:21150 opener_(nullptr),
jochen6004a362017-02-04 00:11:40151 original_opener_(nullptr),
creisf0f069a2015-07-23 23:51:53152 has_committed_real_load_(false),
engedy6e2e0992017-05-25 18:58:42153 is_collapsed_(false),
estarka886b8d2015-12-18 21:53:08154 replication_state_(
155 scope,
156 name,
lukasza464d8692016-02-22 19:26:32157 unique_name,
estarkbd8e26f2016-03-16 23:30:37158 false /* should enforce strict mixed content checking */,
japhet61835ae12017-01-20 01:25:39159 false /* is a potentially trustworthy unique origin */,
160 false /* has received a user gesture */),
Pavel Feldman25234722017-10-11 02:49:06161 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31162 frame_owner_properties_(frame_owner_properties),
xiaochenghb9554bb2016-05-21 14:20:48163 loading_progress_(kLoadingProgressNotStarted),
164 blame_context_(frame_tree_node_id_, parent) {
rob97250742015-12-10 17:45:15165 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45166 g_frame_tree_node_id_map.Get().insert(
167 std::make_pair(frame_tree_node_id_, this));
168 CHECK(result.second);
benjhaydend4da63d2016-03-11 21:29:33169
Daniel Cheng6ca7f1c92017-08-09 21:45:41170 RecordUniqueNameSize(this);
xiaochenghb9554bb2016-05-21 14:20:48171
172 // Note: this should always be done last in the constructor.
173 blame_context_.Initialize();
alexmos998581d2015-01-22 01:01:59174}
[email protected]9b159a52013-10-03 17:24:55175
176FrameTreeNode::~FrameTreeNode() {
paulmeyerf3119f52016-05-17 17:37:19177 std::vector<std::unique_ptr<FrameTreeNode>>().swap(children_);
dmazzonie950ea232015-03-13 21:39:45178 frame_tree_->FrameRemoved(this);
ericwilligers254597b2016-10-17 10:32:31179 for (auto& observer : observers_)
180 observer.OnFrameTreeNodeDestroyed(this);
alexmose201c7cd2015-06-10 17:14:21181
182 if (opener_)
183 opener_->RemoveObserver(opener_observer_.get());
jochen6004a362017-02-04 00:11:40184 if (original_opener_)
185 original_opener_->RemoveObserver(original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45186
187 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18188
189 if (navigation_request_) {
190 // PlzNavigate: if a frame with a pending navigation is detached, make sure
191 // the WebContents (and its observers) update their loading state.
192 navigation_request_.reset();
193 DidStopLoading();
194 }
[email protected]9b159a52013-10-03 17:24:55195}
196
alexmose201c7cd2015-06-10 17:14:21197void FrameTreeNode::AddObserver(Observer* observer) {
198 observers_.AddObserver(observer);
199}
200
201void FrameTreeNode::RemoveObserver(Observer* observer) {
202 observers_.RemoveObserver(observer);
203}
204
[email protected]94d0cc12013-12-18 00:07:41205bool FrameTreeNode::IsMainFrame() const {
206 return frame_tree_->root() == this;
207}
208
dcheng9bfa5162016-04-09 01:00:57209FrameTreeNode* FrameTreeNode::AddChild(std::unique_ptr<FrameTreeNode> child,
nick8814e652015-12-18 01:44:12210 int process_id,
211 int frame_routing_id) {
dgroganfb22f9a2014-10-20 21:32:32212 // Child frame must always be created in the same process as the parent.
213 CHECK_EQ(process_id, render_manager_.current_host()->GetProcess()->GetID());
214
[email protected]94d0cc12013-12-18 00:07:41215 // Initialize the RenderFrameHost for the new node. We always create child
216 // frames in the same SiteInstance as the current frame, and they can swap to
217 // a different one if they navigate away.
218 child->render_manager()->Init(
[email protected]94d0cc12013-12-18 00:07:41219 render_manager_.current_host()->GetSiteInstance(),
dcheng29f5a6c2015-08-31 21:43:27220 render_manager_.current_host()->GetRoutingID(), frame_routing_id,
jambaaab3a2016-11-05 00:46:18221 MSG_ROUTING_NONE, false);
alexmos46e85ec2015-04-03 21:04:35222
223 // Other renderer processes in this BrowsingInstance may need to find out
224 // about the new frame. Create a proxy for the child frame in all
225 // SiteInstances that have a proxy for the frame's parent, since all frames
226 // in a frame tree should have the same set of proxies.
Ken Buchanan077f50312017-08-23 15:34:47227 render_manager_.CreateProxiesForChildFrame(child.get());
alexmos46e85ec2015-04-03 21:04:35228
dcheng36b6aec92015-12-26 06:16:36229 children_.push_back(std::move(child));
nick8814e652015-12-18 01:44:12230 return children_.back().get();
[email protected]9b159a52013-10-03 17:24:55231}
232
[email protected]741fd682013-11-08 08:26:55233void FrameTreeNode::RemoveChild(FrameTreeNode* child) {
nickb6769e632015-11-13 23:25:18234 for (auto iter = children_.begin(); iter != children_.end(); ++iter) {
235 if (iter->get() == child) {
236 // Subtle: we need to make sure the node is gone from the tree before
237 // observers are notified of its deletion.
dcheng9bfa5162016-04-09 01:00:57238 std::unique_ptr<FrameTreeNode> node_to_delete(std::move(*iter));
nickb6769e632015-11-13 23:25:18239 children_.erase(iter);
240 node_to_delete.reset();
241 return;
242 }
[email protected]bffc8302014-01-23 20:52:16243 }
[email protected]9b159a52013-10-03 17:24:55244}
245
[email protected]81c6c5e2014-02-13 20:20:07246void FrameTreeNode::ResetForNewProcess() {
Erik Chen173bf3042017-07-31 06:06:21247 current_frame_host()->SetLastCommittedUrl(GURL());
xiaochenghb9554bb2016-05-21 14:20:48248 blame_context_.TakeSnapshot();
[email protected]482ce3c2013-11-27 18:17:09249
nickb6769e632015-11-13 23:25:18250 // Remove child nodes from the tree, then delete them. This destruction
251 // operation will notify observers.
dcheng9bfa5162016-04-09 01:00:57252 std::vector<std::unique_ptr<FrameTreeNode>>().swap(children_);
[email protected]9b159a52013-10-03 17:24:55253}
254
alexmose201c7cd2015-06-10 17:14:21255void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
256 if (opener_) {
257 opener_->RemoveObserver(opener_observer_.get());
258 opener_observer_.reset();
259 }
260
261 opener_ = opener;
262
263 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55264 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21265 opener_->AddObserver(opener_observer_.get());
266 }
267}
268
jochen6004a362017-02-04 00:11:40269void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39270 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37271 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40272
Avi Drissman36465f332017-09-11 20:49:39273 if (original_opener_) {
274 original_opener_->RemoveObserver(original_opener_observer_.get());
275 original_opener_observer_.reset();
276 }
277
jochen6004a362017-02-04 00:11:40278 original_opener_ = opener;
279
280 if (original_opener_) {
jochen6004a362017-02-04 00:11:40281 original_opener_observer_ =
Jeremy Roman04f27c372017-10-27 15:20:55282 std::make_unique<OpenerDestroyedObserver>(this, true);
jochen6004a362017-02-04 00:11:40283 original_opener_->AddObserver(original_opener_observer_.get());
284 }
285}
286
creisf0f069a2015-07-23 23:51:53287void FrameTreeNode::SetCurrentURL(const GURL& url) {
csharrisona3bd0b32016-10-19 18:40:48288 if (!has_committed_real_load_ && url != url::kAboutBlankURL)
creisf0f069a2015-07-23 23:51:53289 has_committed_real_load_ = true;
Erik Chen173bf3042017-07-31 06:06:21290 current_frame_host()->SetLastCommittedUrl(url);
xiaochenghb9554bb2016-05-21 14:20:48291 blame_context_.TakeSnapshot();
creisf0f069a2015-07-23 23:51:53292}
293
estarkbd8e26f2016-03-16 23:30:37294void FrameTreeNode::SetCurrentOrigin(
295 const url::Origin& origin,
296 bool is_potentially_trustworthy_unique_origin) {
297 if (!origin.IsSameOriginWith(replication_state_.origin) ||
298 replication_state_.has_potentially_trustworthy_unique_origin !=
299 is_potentially_trustworthy_unique_origin) {
300 render_manager_.OnDidUpdateOrigin(origin,
301 is_potentially_trustworthy_unique_origin);
302 }
alexmosa7a4ff822015-04-27 17:59:56303 replication_state_.origin = origin;
estarkbd8e26f2016-03-16 23:30:37304 replication_state_.has_potentially_trustworthy_unique_origin =
305 is_potentially_trustworthy_unique_origin;
alexmosa7a4ff822015-04-27 17:59:56306}
alexmosbe2f4c32015-03-10 02:30:23307
engedy6e2e0992017-05-25 18:58:42308void FrameTreeNode::SetCollapsed(bool collapsed) {
309 DCHECK(!IsMainFrame());
310 if (is_collapsed_ == collapsed)
311 return;
312
313 is_collapsed_ = collapsed;
314 render_manager_.OnDidChangeCollapsedState(collapsed);
315}
316
lukasza464d8692016-02-22 19:26:32317void FrameTreeNode::SetFrameName(const std::string& name,
318 const std::string& unique_name) {
319 if (name == replication_state_.name) {
320 // |unique_name| shouldn't change unless |name| changes.
321 DCHECK_EQ(unique_name, replication_state_.unique_name);
322 return;
323 }
lukasza5140a412016-09-15 21:12:30324
325 if (parent()) {
326 // Non-main frames should have a non-empty unique name.
327 DCHECK(!unique_name.empty());
328 } else {
329 // Unique name of main frames should always stay empty.
330 DCHECK(unique_name.empty());
331 }
332
Daniel Cheng6ca7f1c92017-08-09 21:45:41333 // Note the unique name should only be able to change before the first real
334 // load is committed, but that's not strongly enforced here.
335 if (unique_name != replication_state_.unique_name)
336 RecordUniqueNameSize(this);
lukasza464d8692016-02-22 19:26:32337 render_manager_.OnDidUpdateName(name, unique_name);
alexmosa7a4ff822015-04-27 17:59:56338 replication_state_.name = name;
lukasza464d8692016-02-22 19:26:32339 replication_state_.unique_name = unique_name;
alexmosbe2f4c32015-03-10 02:30:23340}
341
raymesd405a052016-12-05 23:41:34342void FrameTreeNode::SetFeaturePolicyHeader(
Luna Lu2e713992017-11-07 01:45:58343 const blink::ParsedFeaturePolicy& parsed_header) {
raymesd405a052016-12-05 23:41:34344 replication_state_.feature_policy_header = parsed_header;
iclellandab749ec92016-11-23 02:00:43345}
346
iclelland2c79efe22017-02-09 22:44:03347void FrameTreeNode::ResetFeaturePolicyHeader() {
iclellandab749ec92016-11-23 02:00:43348 replication_state_.feature_policy_header.clear();
349}
350
arthursonzogni662aa652017-03-28 11:09:50351void FrameTreeNode::AddContentSecurityPolicies(
352 const std::vector<ContentSecurityPolicyHeader>& headers) {
353 replication_state_.accumulated_csp_headers.insert(
354 replication_state_.accumulated_csp_headers.end(), headers.begin(),
355 headers.end());
356 render_manager_.OnDidAddContentSecurityPolicies(headers);
lukasza8e1c02e42016-05-17 20:05:10357}
358
arthursonzogni7fed384c2017-03-18 03:07:34359void FrameTreeNode::ResetCspHeaders() {
lukasza8e1c02e42016-05-17 20:05:10360 replication_state_.accumulated_csp_headers.clear();
361 render_manager_.OnDidResetContentSecurityPolicy();
362}
363
mkwstf672e7ef2016-06-09 20:51:07364void FrameTreeNode::SetInsecureRequestPolicy(
365 blink::WebInsecureRequestPolicy policy) {
366 if (policy == replication_state_.insecure_request_policy)
estarka886b8d2015-12-18 21:53:08367 return;
mkwstf672e7ef2016-06-09 20:51:07368 render_manager_.OnEnforceInsecureRequestPolicy(policy);
369 replication_state_.insecure_request_policy = policy;
estarka886b8d2015-12-18 21:53:08370}
371
Luna Luc3fdacdf2017-11-08 04:48:53372void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Ian Clellandcdc4f312017-10-13 22:24:12373 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25374
Ian Clellandcdc4f312017-10-13 22:24:12375 if (parent()) {
376 // Subframes should always inherit their parent's sandbox flags.
377 pending_frame_policy_.sandbox_flags |=
378 parent()->effective_frame_policy().sandbox_flags;
379 // This is only applied on subframes; container policy is not mutable on
380 // main frame.
381 pending_frame_policy_.container_policy = frame_policy.container_policy;
382 }
iclelland92f8c0b2017-04-19 12:43:05383}
384
mlamouria85eb3f2015-01-26 17:36:27385bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
386 if (!other || !other->child_count())
387 return false;
388
389 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
390 if (node == other)
391 return true;
392 }
393
394 return false;
395}
396
alexmos9f8705a2015-05-06 19:58:59397FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39398 return GetSibling(-1);
399}
alexmos9f8705a2015-05-06 19:58:59400
paulmeyer322777fb2016-05-16 23:15:39401FrameTreeNode* FrameTreeNode::NextSibling() const {
402 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59403}
404
fdegans4a49ce932015-03-12 17:11:37405bool FrameTreeNode::IsLoading() const {
406 RenderFrameHostImpl* current_frame_host =
407 render_manager_.current_frame_host();
408 RenderFrameHostImpl* pending_frame_host =
409 render_manager_.pending_frame_host();
410
411 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39412
carloskd80262f52015-12-16 14:40:35413 if (IsBrowserSideNavigationEnabled()) {
fdegans39ff0382015-04-29 19:04:39414 if (navigation_request_)
415 return true;
clamy11e11512015-07-07 16:42:17416
417 RenderFrameHostImpl* speculative_frame_host =
418 render_manager_.speculative_frame_host();
419 if (speculative_frame_host && speculative_frame_host->is_loading())
420 return true;
fdegans39ff0382015-04-29 19:04:39421 } else {
422 if (pending_frame_host && pending_frame_host->is_loading())
423 return true;
424 }
fdegans4a49ce932015-03-12 17:11:37425 return current_frame_host->is_loading();
426}
427
iclelland92f8c0b2017-04-19 12:43:05428bool FrameTreeNode::CommitPendingFramePolicy() {
Ian Clellandcdc4f312017-10-13 22:24:12429 bool did_change_flags = pending_frame_policy_.sandbox_flags !=
430 replication_state_.frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05431 bool did_change_container_policy =
Ian Clellandcdc4f312017-10-13 22:24:12432 pending_frame_policy_.container_policy !=
433 replication_state_.frame_policy.container_policy;
iclelland92f8c0b2017-04-19 12:43:05434 if (did_change_flags)
Ian Clellandcdc4f312017-10-13 22:24:12435 replication_state_.frame_policy.sandbox_flags =
436 pending_frame_policy_.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05437 if (did_change_container_policy)
Ian Clellandcdc4f312017-10-13 22:24:12438 replication_state_.frame_policy.container_policy =
439 pending_frame_policy_.container_policy;
iclelland92f8c0b2017-04-19 12:43:05440 return did_change_flags || did_change_container_policy;
alexmos6b294562015-03-05 19:24:10441}
442
carloskc49005eb2015-06-16 11:25:07443void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57444 std::unique_ptr<NavigationRequest> navigation_request) {
carloskd80262f52015-12-16 14:40:35445 CHECK(IsBrowserSideNavigationEnabled());
clamy82a2f4d2016-02-02 14:20:41446
arthursonzognic79c251c2016-08-18 15:00:37447 // This is never called when navigating to a Javascript URL. For the loading
448 // state, this matches what Blink is doing: Blink doesn't send throbber
449 // notifications for Javascript URLS.
450 DCHECK(!navigation_request->common_params().url.SchemeIs(
451 url::kJavaScriptScheme));
452
clamy44e84ce2016-02-22 15:38:25453 bool was_previously_loading = frame_tree()->IsLoading();
454
clamy82a2f4d2016-02-02 14:20:41455 // There's no need to reset the state: there's still an ongoing load, and the
456 // RenderFrameHostManager will take care of updates to the speculative
457 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05458 if (was_previously_loading) {
clamy080e7962017-05-25 00:44:18459 if (navigation_request_ && navigation_request_->navigation_handle()) {
jamcd0b7b22017-03-24 22:13:05460 // Mark the old request as aborted.
461 navigation_request_->navigation_handle()->set_net_error_code(
462 net::ERR_ABORTED);
463 }
clamya86695b2017-03-23 14:45:48464 ResetNavigationRequest(true, true);
jamcd0b7b22017-03-24 22:13:05465 }
clamy44e84ce2016-02-22 15:38:25466
467 navigation_request_ = std::move(navigation_request);
clamy8e2e299202016-04-05 11:44:59468 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39469
arthursonzogni92f18682017-02-08 23:00:04470 bool to_different_document = !FrameMsg_Navigate_Type::IsSameDocument(
471 navigation_request_->common_params().navigation_type);
472
473 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16474}
475
clamya86695b2017-03-23 14:45:48476void FrameTreeNode::ResetNavigationRequest(bool keep_state,
477 bool inform_renderer) {
carloskd80262f52015-12-16 14:40:35478 CHECK(IsBrowserSideNavigationEnabled());
fdegans39ff0382015-04-29 19:04:39479 if (!navigation_request_)
480 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23481
Pavel Feldmand8352ac2017-11-10 00:37:41482 RenderFrameDevToolsAgentHost::OnResetNavigationRequest(
483 navigation_request_.get());
484
John Abd-El-Malekdcc7bf42017-09-12 22:30:23485 // The renderer should be informed if the caller allows to do so and the
486 // navigation came from a BeginNavigation IPC.
487 int need_to_inform_renderer =
488 inform_renderer && navigation_request_->from_begin_navigation();
489
clamy8e2e299202016-04-05 11:44:59490 NavigationRequest::AssociatedSiteInstanceType site_instance_type =
491 navigation_request_->associated_site_instance_type();
clamydcb434c12015-04-16 19:29:16492 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39493
clamy82a2f4d2016-02-02 14:20:41494 if (keep_state)
fdegans39ff0382015-04-29 19:04:39495 return;
496
clamy82a2f4d2016-02-02 14:20:41497 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
498 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39499 DidStopLoading();
500 render_manager_.CleanUpNavigation();
clamy2567242a2016-02-22 18:27:38501
clamy8e2e299202016-04-05 11:44:59502 // When reusing the same SiteInstance, a pending WebUI may have been created
503 // on behalf of the navigation in the current RenderFrameHost. Clear it.
504 if (site_instance_type ==
505 NavigationRequest::AssociatedSiteInstanceType::CURRENT) {
506 current_frame_host()->ClearPendingWebUI();
507 }
508
clamy2567242a2016-02-22 18:27:38509 // If the navigation is renderer-initiated, the renderer should also be
clamya86695b2017-03-23 14:45:48510 // informed that the navigation stopped if needed. In the case the renderer
511 // process asked for the navigation to be aborted, e.g. following a
512 // document.open, do not send an IPC to the renderer process as it already
513 // expects the navigation to stop.
John Abd-El-Malekdcc7bf42017-09-12 22:30:23514 if (need_to_inform_renderer) {
clamy2567242a2016-02-22 18:27:38515 current_frame_host()->Send(
John Abd-El-Malekdcc7bf42017-09-12 22:30:23516 new FrameMsg_DroppedNavigation(current_frame_host()->GetRoutingID()));
clamy2567242a2016-02-22 18:27:38517 }
518
clamydcb434c12015-04-16 19:29:16519}
520
fdegansa696e5112015-04-17 01:57:59521bool FrameTreeNode::has_started_loading() const {
522 return loading_progress_ != kLoadingProgressNotStarted;
523}
524
525void FrameTreeNode::reset_loading_progress() {
526 loading_progress_ = kLoadingProgressNotStarted;
527}
528
clamy44e84ce2016-02-22 15:38:25529void FrameTreeNode::DidStartLoading(bool to_different_document,
530 bool was_previously_loading) {
fdegansa696e5112015-04-17 01:57:59531 // Any main frame load to a new document should reset the load progress since
532 // it will replace the current page and any frames. The WebContents will
533 // be notified when DidChangeLoadProgress is called.
534 if (to_different_document && IsMainFrame())
535 frame_tree_->ResetLoadProgress();
536
537 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25538 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59539 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
540
541 // Set initial load progress and update overall progress. This will notify
542 // the WebContents of the load progress change.
543 DidChangeLoadProgress(kLoadingProgressMinimum);
544
545 // Notify the RenderFrameHostManager of the event.
546 render_manager()->OnDidStartLoading();
547}
548
549void FrameTreeNode::DidStopLoading() {
fdegansa696e5112015-04-17 01:57:59550 // Set final load progress and update overall progress. This will notify
551 // the WebContents of the load progress change.
552 DidChangeLoadProgress(kLoadingProgressDone);
553
fdegansa696e5112015-04-17 01:57:59554 // Notify the WebContents.
555 if (!frame_tree_->IsLoading())
556 navigator()->GetDelegate()->DidStopLoading();
557
fdegansa696e5112015-04-17 01:57:59558 // Notify the RenderFrameHostManager of the event.
559 render_manager()->OnDidStopLoading();
fdegansa696e5112015-04-17 01:57:59560}
561
562void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
563 loading_progress_ = load_progress;
564 frame_tree_->UpdateLoadProgress();
565}
566
clamyf73862c42015-07-08 12:31:33567bool FrameTreeNode::StopLoading() {
jam0299edae2017-03-10 00:49:22568 if (IsBrowserSideNavigationEnabled()) {
569 if (navigation_request_) {
clamy080e7962017-05-25 00:44:18570 int expected_pending_nav_entry_id = navigation_request_->nav_entry_id();
571 if (navigation_request_->navigation_handle()) {
572 navigation_request_->navigation_handle()->set_net_error_code(
573 net::ERR_ABORTED);
574 expected_pending_nav_entry_id =
575 navigation_request_->navigation_handle()->pending_nav_entry_id();
576 }
577 navigator_->DiscardPendingEntryIfNeeded(expected_pending_nav_entry_id);
jam0299edae2017-03-10 00:49:22578 }
clamya86695b2017-03-23 14:45:48579 ResetNavigationRequest(false, true);
jam0299edae2017-03-10 00:49:22580 }
clamyf73862c42015-07-08 12:31:33581
582 // TODO(nasko): see if child frames should send IPCs in site-per-process
583 // mode.
584 if (!IsMainFrame())
585 return true;
586
587 render_manager_.Stop();
588 return true;
589}
590
alexmos21acae52015-11-07 01:04:43591void FrameTreeNode::DidFocus() {
592 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31593 for (auto& observer : observers_)
594 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43595}
596
clamy44e84ce2016-02-22 15:38:25597void FrameTreeNode::BeforeUnloadCanceled() {
598 // TODO(clamy): Support BeforeUnload in subframes.
599 if (!IsMainFrame())
600 return;
601
602 RenderFrameHostImpl* current_frame_host =
603 render_manager_.current_frame_host();
604 DCHECK(current_frame_host);
605 current_frame_host->ResetLoadingState();
606
607 if (IsBrowserSideNavigationEnabled()) {
608 RenderFrameHostImpl* speculative_frame_host =
609 render_manager_.speculative_frame_host();
610 if (speculative_frame_host)
611 speculative_frame_host->ResetLoadingState();
clamy080e7962017-05-25 00:44:18612 // Note: there is no need to set an error code on the NavigationHandle here
613 // as it has not been created yet. It is only created when the
614 // BeforeUnloadACK is received.
615 if (navigation_request_)
616 ResetNavigationRequest(false, true);
clamy44e84ce2016-02-22 15:38:25617 } else {
618 RenderFrameHostImpl* pending_frame_host =
619 render_manager_.pending_frame_host();
620 if (pending_frame_host)
621 pending_frame_host->ResetLoadingState();
622 }
623}
624
japhet61835ae12017-01-20 01:25:39625void FrameTreeNode::OnSetHasReceivedUserGesture() {
626 render_manager_.OnSetHasReceivedUserGesture();
627 replication_state_.has_received_user_gesture = true;
628}
629
paulmeyer322777fb2016-05-16 23:15:39630FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19631 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39632 return nullptr;
633
634 for (size_t i = 0; i < parent_->child_count(); ++i) {
635 if (parent_->child_at(i) == this) {
636 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
637 i + relative_offset >= parent_->child_count()) {
638 return nullptr;
639 }
640 return parent_->child_at(i + relative_offset);
641 }
642 }
643
644 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
645 return nullptr;
646}
647
[email protected]9b159a52013-10-03 17:24:55648} // namespace content