blob: 3904248d6e6f5ae421639e8d6db24fe36de5e63d [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"
alexmos6e940102016-01-19 22:47:2527#include "third_party/WebKit/public/web/WebSandboxFlags.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,
139 const FrameOwnerProperties& frame_owner_properties)
[email protected]bffc8302014-01-23 20:52:16140 : frame_tree_(frame_tree),
141 navigator_(navigator),
142 render_manager_(this,
143 render_frame_delegate,
[email protected]bffc8302014-01-23 20:52:16144 render_widget_delegate,
145 manager_delegate),
146 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59147 parent_(parent),
alexmose201c7cd2015-06-10 17:14:21148 opener_(nullptr),
jochen6004a362017-02-04 00:11:40149 original_opener_(nullptr),
creisf0f069a2015-07-23 23:51:53150 has_committed_real_load_(false),
engedy6e2e0992017-05-25 18:58:42151 is_collapsed_(false),
estarka886b8d2015-12-18 21:53:08152 replication_state_(
153 scope,
154 name,
lukasza464d8692016-02-22 19:26:32155 unique_name,
Blink Reformat1c4d759e2017-04-09 16:34:54156 blink::WebSandboxFlags::kNone,
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 */),
Blink Reformat1c4d759e2017-04-09 16:34:54160 pending_sandbox_flags_(blink::WebSandboxFlags::kNone),
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_) {
Avi Drissman36465f332017-09-11 20:49:39263 opener_observer_ = base::MakeUnique<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_ =
281 base::MakeUnique<OpenerDestroyedObserver>(this, true);
282 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(
iclelland4dbcfdcb2017-02-02 23:51:49342 const ParsedFeaturePolicyHeader& 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
alexmos6e940102016-01-19 22:47:25371void FrameTreeNode::SetPendingSandboxFlags(
372 blink::WebSandboxFlags sandbox_flags) {
373 pending_sandbox_flags_ = sandbox_flags;
374
375 // Subframes should always inherit their parent's sandbox flags.
376 if (parent())
377 pending_sandbox_flags_ |= parent()->effective_sandbox_flags();
378}
379
iclelland92f8c0b2017-04-19 12:43:05380void FrameTreeNode::SetPendingContainerPolicy(
381 const ParsedFeaturePolicyHeader& container_policy) {
382 // This should only be called on subframes; container policy is not mutable on
383 // main frame.
384 DCHECK(!IsMainFrame());
385 pending_container_policy_ = container_policy;
386}
387
mlamouria85eb3f2015-01-26 17:36:27388bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
389 if (!other || !other->child_count())
390 return false;
391
392 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
393 if (node == other)
394 return true;
395 }
396
397 return false;
398}
399
alexmos9f8705a2015-05-06 19:58:59400FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39401 return GetSibling(-1);
402}
alexmos9f8705a2015-05-06 19:58:59403
paulmeyer322777fb2016-05-16 23:15:39404FrameTreeNode* FrameTreeNode::NextSibling() const {
405 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59406}
407
fdegans4a49ce932015-03-12 17:11:37408bool FrameTreeNode::IsLoading() const {
409 RenderFrameHostImpl* current_frame_host =
410 render_manager_.current_frame_host();
411 RenderFrameHostImpl* pending_frame_host =
412 render_manager_.pending_frame_host();
413
414 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39415
carloskd80262f52015-12-16 14:40:35416 if (IsBrowserSideNavigationEnabled()) {
fdegans39ff0382015-04-29 19:04:39417 if (navigation_request_)
418 return true;
clamy11e11512015-07-07 16:42:17419
420 RenderFrameHostImpl* speculative_frame_host =
421 render_manager_.speculative_frame_host();
422 if (speculative_frame_host && speculative_frame_host->is_loading())
423 return true;
fdegans39ff0382015-04-29 19:04:39424 } else {
425 if (pending_frame_host && pending_frame_host->is_loading())
426 return true;
427 }
fdegans4a49ce932015-03-12 17:11:37428 return current_frame_host->is_loading();
429}
430
iclelland92f8c0b2017-04-19 12:43:05431bool FrameTreeNode::CommitPendingFramePolicy() {
alexmos6b294562015-03-05 19:24:10432 bool did_change_flags =
alexmos6e940102016-01-19 22:47:25433 pending_sandbox_flags_ != replication_state_.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05434 bool did_change_container_policy =
435 pending_container_policy_ != replication_state_.container_policy;
436 if (did_change_flags)
437 replication_state_.sandbox_flags = pending_sandbox_flags_;
438 if (did_change_container_policy)
439 replication_state_.container_policy = pending_container_policy_;
440 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
482 // The renderer should be informed if the caller allows to do so and the
483 // navigation came from a BeginNavigation IPC.
484 int need_to_inform_renderer =
485 inform_renderer && navigation_request_->from_begin_navigation();
486
clamy8e2e299202016-04-05 11:44:59487 NavigationRequest::AssociatedSiteInstanceType site_instance_type =
488 navigation_request_->associated_site_instance_type();
clamydcb434c12015-04-16 19:29:16489 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39490
clamy82a2f4d2016-02-02 14:20:41491 if (keep_state)
fdegans39ff0382015-04-29 19:04:39492 return;
493
clamy82a2f4d2016-02-02 14:20:41494 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
495 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39496 DidStopLoading();
497 render_manager_.CleanUpNavigation();
clamy2567242a2016-02-22 18:27:38498
clamy8e2e299202016-04-05 11:44:59499 // When reusing the same SiteInstance, a pending WebUI may have been created
500 // on behalf of the navigation in the current RenderFrameHost. Clear it.
501 if (site_instance_type ==
502 NavigationRequest::AssociatedSiteInstanceType::CURRENT) {
503 current_frame_host()->ClearPendingWebUI();
504 }
505
clamy2567242a2016-02-22 18:27:38506 // If the navigation is renderer-initiated, the renderer should also be
clamya86695b2017-03-23 14:45:48507 // informed that the navigation stopped if needed. In the case the renderer
508 // process asked for the navigation to be aborted, e.g. following a
509 // document.open, do not send an IPC to the renderer process as it already
510 // expects the navigation to stop.
John Abd-El-Malekdcc7bf42017-09-12 22:30:23511 if (need_to_inform_renderer) {
clamy2567242a2016-02-22 18:27:38512 current_frame_host()->Send(
John Abd-El-Malekdcc7bf42017-09-12 22:30:23513 new FrameMsg_DroppedNavigation(current_frame_host()->GetRoutingID()));
clamy2567242a2016-02-22 18:27:38514 }
515
clamydcb434c12015-04-16 19:29:16516}
517
fdegansa696e5112015-04-17 01:57:59518bool FrameTreeNode::has_started_loading() const {
519 return loading_progress_ != kLoadingProgressNotStarted;
520}
521
522void FrameTreeNode::reset_loading_progress() {
523 loading_progress_ = kLoadingProgressNotStarted;
524}
525
clamy44e84ce2016-02-22 15:38:25526void FrameTreeNode::DidStartLoading(bool to_different_document,
527 bool was_previously_loading) {
fdegansa696e5112015-04-17 01:57:59528 // Any main frame load to a new document should reset the load progress since
529 // it will replace the current page and any frames. The WebContents will
530 // be notified when DidChangeLoadProgress is called.
531 if (to_different_document && IsMainFrame())
532 frame_tree_->ResetLoadProgress();
533
534 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25535 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59536 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
537
538 // Set initial load progress and update overall progress. This will notify
539 // the WebContents of the load progress change.
540 DidChangeLoadProgress(kLoadingProgressMinimum);
541
542 // Notify the RenderFrameHostManager of the event.
543 render_manager()->OnDidStartLoading();
544}
545
546void FrameTreeNode::DidStopLoading() {
fdegansa696e5112015-04-17 01:57:59547 // Set final load progress and update overall progress. This will notify
548 // the WebContents of the load progress change.
549 DidChangeLoadProgress(kLoadingProgressDone);
550
fdegansa696e5112015-04-17 01:57:59551 // Notify the WebContents.
552 if (!frame_tree_->IsLoading())
553 navigator()->GetDelegate()->DidStopLoading();
554
fdegansa696e5112015-04-17 01:57:59555 // Notify the RenderFrameHostManager of the event.
556 render_manager()->OnDidStopLoading();
fdegansa696e5112015-04-17 01:57:59557}
558
559void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
560 loading_progress_ = load_progress;
561 frame_tree_->UpdateLoadProgress();
562}
563
clamyf73862c42015-07-08 12:31:33564bool FrameTreeNode::StopLoading() {
jam0299edae2017-03-10 00:49:22565 if (IsBrowserSideNavigationEnabled()) {
566 if (navigation_request_) {
clamy080e7962017-05-25 00:44:18567 int expected_pending_nav_entry_id = navigation_request_->nav_entry_id();
568 if (navigation_request_->navigation_handle()) {
569 navigation_request_->navigation_handle()->set_net_error_code(
570 net::ERR_ABORTED);
571 expected_pending_nav_entry_id =
572 navigation_request_->navigation_handle()->pending_nav_entry_id();
573 }
574 navigator_->DiscardPendingEntryIfNeeded(expected_pending_nav_entry_id);
jam0299edae2017-03-10 00:49:22575 }
clamya86695b2017-03-23 14:45:48576 ResetNavigationRequest(false, true);
jam0299edae2017-03-10 00:49:22577 }
clamyf73862c42015-07-08 12:31:33578
579 // TODO(nasko): see if child frames should send IPCs in site-per-process
580 // mode.
581 if (!IsMainFrame())
582 return true;
583
584 render_manager_.Stop();
585 return true;
586}
587
alexmos21acae52015-11-07 01:04:43588void FrameTreeNode::DidFocus() {
589 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31590 for (auto& observer : observers_)
591 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43592}
593
clamy44e84ce2016-02-22 15:38:25594void FrameTreeNode::BeforeUnloadCanceled() {
595 // TODO(clamy): Support BeforeUnload in subframes.
596 if (!IsMainFrame())
597 return;
598
599 RenderFrameHostImpl* current_frame_host =
600 render_manager_.current_frame_host();
601 DCHECK(current_frame_host);
602 current_frame_host->ResetLoadingState();
603
604 if (IsBrowserSideNavigationEnabled()) {
605 RenderFrameHostImpl* speculative_frame_host =
606 render_manager_.speculative_frame_host();
607 if (speculative_frame_host)
608 speculative_frame_host->ResetLoadingState();
clamy080e7962017-05-25 00:44:18609 // Note: there is no need to set an error code on the NavigationHandle here
610 // as it has not been created yet. It is only created when the
611 // BeforeUnloadACK is received.
612 if (navigation_request_)
613 ResetNavigationRequest(false, true);
clamy44e84ce2016-02-22 15:38:25614 } else {
615 RenderFrameHostImpl* pending_frame_host =
616 render_manager_.pending_frame_host();
617 if (pending_frame_host)
618 pending_frame_host->ResetLoadingState();
619 }
620}
621
japhet61835ae12017-01-20 01:25:39622void FrameTreeNode::OnSetHasReceivedUserGesture() {
623 render_manager_.OnSetHasReceivedUserGesture();
624 replication_state_.has_received_user_gesture = true;
625}
626
paulmeyer322777fb2016-05-16 23:15:39627FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19628 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39629 return nullptr;
630
631 for (size_t i = 0; i < parent_->child_count(); ++i) {
632 if (parent_->child_at(i) == this) {
633 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
634 i + relative_offset >= parent_->child_count()) {
635 return nullptr;
636 }
637 return parent_->child_at(i + relative_offset);
638 }
639 }
640
641 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
642 return nullptr;
643}
644
[email protected]9b159a52013-10-03 17:24:55645} // namespace content