blob: b391237f235689d1cbf6a1acdef6c5b704f14b9c [file] [log] [blame]
[email protected]9b159a52013-10-03 17:24:551// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4a8ca482013-10-30 21:06:405#include "content/browser/frame_host/frame_tree_node.h"
[email protected]9b159a52013-10-03 17:24:556
Daniel Cheng6ca7f1c92017-08-09 21:45:417#include <math.h>
8
[email protected]9b159a52013-10-03 17:24:559#include <queue>
Takuto Ikutaadf31eb2019-01-05 00:32:4810#include <unordered_map>
dcheng36b6aec92015-12-26 06:16:3611#include <utility>
[email protected]9b159a52013-10-03 17:24:5512
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1413#include "base/feature_list.h"
scottmg6ece5ae2017-02-01 18:25:1914#include "base/lazy_instance.h"
avib7348942015-12-25 20:57:1015#include "base/macros.h"
dcheng23ca947d2016-05-04 20:04:1516#include "base/metrics/histogram_macros.h"
[email protected]9b159a52013-10-03 17:24:5517#include "base/stl_util.h"
Daniel Cheng6ca7f1c92017-08-09 21:45:4118#include "base/strings/string_util.h"
Andrey Kosyakovf2d4ff72018-10-29 20:09:5919#include "content/browser/devtools/devtools_instrumentation.h"
[email protected]94d0cc12013-12-18 00:07:4120#include "content/browser/frame_host/frame_tree.h"
Shivani Sharma194877032019-03-07 17:52:4721#include "content/browser/frame_host/navigation_controller_impl.h"
clamydcb434c12015-04-16 19:29:1622#include "content/browser/frame_host/navigation_request.h"
[email protected]190b8c52013-11-09 01:35:4423#include "content/browser/frame_host/navigator.h"
[email protected]d4a8ca482013-10-30 21:06:4024#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4125#include "content/browser/renderer_host/render_view_host_impl.h"
clamyf73862c42015-07-08 12:31:3326#include "content/common/frame_messages.h"
Lucas Furukawa Gadanief8290a2019-07-29 20:27:5127#include "content/common/navigation_params.h"
28#include "content/common/navigation_params_utils.h"
dmazzonie950ea232015-03-13 21:39:4529#include "content/public/browser/browser_thread.h"
Mustaq Ahmeda5dfa60b2018-12-08 00:30:1430#include "content/public/common/content_features.h"
Arthur Sonzognif21fb512018-11-06 09:31:5831#include "content/public/common/navigation_policy.h"
Blink Reformata30d4232018-04-07 15:31:0632#include "third_party/blink/public/common/frame/sandbox_flags.h"
Mustaq Ahmedc4cb7162018-06-05 16:28:3633#include "third_party/blink/public/common/frame/user_activation_update_type.h"
[email protected]9b159a52013-10-03 17:24:5534
35namespace content {
36
dmazzonie950ea232015-03-13 21:39:4537namespace {
38
39// This is a global map between frame_tree_node_ids and pointers to
40// FrameTreeNodes.
Takuto Ikutaadf31eb2019-01-05 00:32:4841typedef std::unordered_map<int, FrameTreeNode*> FrameTreeNodeIdMap;
dmazzonie950ea232015-03-13 21:39:4542
scottmg5e65e3a2017-03-08 08:48:4643base::LazyInstance<FrameTreeNodeIdMap>::DestructorAtExit
44 g_frame_tree_node_id_map = LAZY_INSTANCE_INITIALIZER;
dmazzonie950ea232015-03-13 21:39:4545
fdegansa696e5112015-04-17 01:57:5946// These values indicate the loading progress status. The minimum progress
47// value matches what Blink's ProgressTracker has traditionally used for a
48// minimum progress value.
fdegansa696e5112015-04-17 01:57:5949const double kLoadingProgressMinimum = 0.1;
50const double kLoadingProgressDone = 1.0;
dmazzonie950ea232015-03-13 21:39:4551
fdegansa696e5112015-04-17 01:57:5952} // namespace
fdegans1d16355162015-03-26 11:58:3453
alexmose201c7cd2015-06-10 17:14:2154// This observer watches the opener of its owner FrameTreeNode and clears the
55// owner's opener if the opener is destroyed.
56class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
57 public:
jochen6004a362017-02-04 00:11:4058 OpenerDestroyedObserver(FrameTreeNode* owner, bool observing_original_opener)
59 : owner_(owner), observing_original_opener_(observing_original_opener) {}
alexmose201c7cd2015-06-10 17:14:2160
61 // FrameTreeNode::Observer
62 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
jochen6004a362017-02-04 00:11:4063 if (observing_original_opener_) {
Avi Drissman36465f332017-09-11 20:49:3964 // The "original owner" is special. It's used for attribution, and clients
65 // walk down the original owner chain. Therefore, if a link in the chain
66 // is being destroyed, reconnect the observation to the parent of the link
67 // being destroyed.
jochen6004a362017-02-04 00:11:4068 CHECK_EQ(owner_->original_opener(), node);
Avi Drissman36465f332017-09-11 20:49:3969 owner_->SetOriginalOpener(node->original_opener());
70 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:4071 } else {
72 CHECK_EQ(owner_->opener(), node);
73 owner_->SetOpener(nullptr);
Avi Drissman36465f332017-09-11 20:49:3974 // |this| is deleted at this point.
jochen6004a362017-02-04 00:11:4075 }
alexmose201c7cd2015-06-10 17:14:2176 }
77
78 private:
79 FrameTreeNode* owner_;
jochen6004a362017-02-04 00:11:4080 bool observing_original_opener_;
alexmose201c7cd2015-06-10 17:14:2181
82 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver);
83};
84
vishal.b782eb5d2015-04-29 12:22:5785int FrameTreeNode::next_frame_tree_node_id_ = 1;
[email protected]9b159a52013-10-03 17:24:5586
dmazzonie950ea232015-03-13 21:39:4587// static
vishal.b782eb5d2015-04-29 12:22:5788FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
mostynb366eaf12015-03-26 00:51:1989 DCHECK_CURRENTLY_ON(BrowserThread::UI);
rob97250742015-12-10 17:45:1590 FrameTreeNodeIdMap* nodes = g_frame_tree_node_id_map.Pointer();
jdoerrie55ec69d2018-10-08 13:34:4691 auto it = nodes->find(frame_tree_node_id);
dmazzonie950ea232015-03-13 21:39:4592 return it == nodes->end() ? nullptr : it->second;
93}
94
raymes31457802016-07-20 06:08:0995FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
96 Navigator* navigator,
raymes31457802016-07-20 06:08:0997 FrameTreeNode* parent,
98 blink::WebTreeScopeType scope,
99 const std::string& name,
100 const std::string& unique_name,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45101 bool is_created_by_script,
Pavel Feldman25234722017-10-11 02:49:06102 const base::UnguessableToken& devtools_frame_token,
Ehsan Karamad192a8da2018-10-21 03:48:08103 const FrameOwnerProperties& frame_owner_properties,
104 blink::FrameOwnerElementType owner_type)
[email protected]bffc8302014-01-23 20:52:16105 : frame_tree_(frame_tree),
106 navigator_(navigator),
Lucas Furukawa Gadani72cc21c2018-09-04 18:50:46107 render_manager_(this, frame_tree->manager_delegate()),
[email protected]bffc8302014-01-23 20:52:16108 frame_tree_node_id_(next_frame_tree_node_id_++),
xiaochengh98488162016-05-19 15:17:59109 parent_(parent),
Bo Liua13e7c02018-03-28 22:24:02110 depth_(parent ? parent->depth_ + 1 : 0u),
alexmose201c7cd2015-06-10 17:14:21111 opener_(nullptr),
jochen6004a362017-02-04 00:11:40112 original_opener_(nullptr),
creisf0f069a2015-07-23 23:51:53113 has_committed_real_load_(false),
engedy6e2e0992017-05-25 18:58:42114 is_collapsed_(false),
estarka886b8d2015-12-18 21:53:08115 replication_state_(
116 scope,
117 name,
lukasza464d8692016-02-22 19:26:32118 unique_name,
estarkbd8e26f2016-03-16 23:30:37119 false /* should enforce strict mixed content checking */,
arthursonzogni4b62a5cb2018-01-17 14:14:26120 std::vector<uint32_t>()
121 /* hashes of hosts for insecure request upgrades */,
japhet61835ae12017-01-20 01:25:39122 false /* is a potentially trustworthy unique origin */,
Becca Hughes60af7d42017-12-12 10:53:15123 false /* has received a user gesture */,
Ehsan Karamad192a8da2018-10-21 03:48:08124 false /* has received a user gesture before nav */,
125 owner_type),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45126 is_created_by_script_(is_created_by_script),
Pavel Feldman25234722017-10-11 02:49:06127 devtools_frame_token_(devtools_frame_token),
lazyboy70605c32015-11-03 01:27:31128 frame_owner_properties_(frame_owner_properties),
Shubhie Panickerddf2a4e2018-03-06 00:09:06129 was_discarded_(false),
xiaochenghb9554bb2016-05-21 14:20:48130 blame_context_(frame_tree_node_id_, parent) {
rob97250742015-12-10 17:45:15131 std::pair<FrameTreeNodeIdMap::iterator, bool> result =
dmazzonie950ea232015-03-13 21:39:45132 g_frame_tree_node_id_map.Get().insert(
133 std::make_pair(frame_tree_node_id_, this));
134 CHECK(result.second);
benjhaydend4da63d2016-03-11 21:29:33135
xiaochenghb9554bb2016-05-21 14:20:48136 // Note: this should always be done last in the constructor.
137 blame_context_.Initialize();
alexmos998581d2015-01-22 01:01:59138}
[email protected]9b159a52013-10-03 17:24:55139
140FrameTreeNode::~FrameTreeNode() {
Nasko Oskov252ae042018-10-04 21:44:12141 // Remove the children.
142 current_frame_host()->ResetChildren();
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45143
Nate Chapin22ea6592019-03-05 22:29:02144 current_frame_host()->ResetLoadingState();
145
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45146 // If the removed frame was created by a script, then its history entry will
147 // never be reused - we can save some memory by removing the history entry.
148 // See also https://crbug.com/784356.
149 if (is_created_by_script_ && parent_) {
150 NavigationEntryImpl* nav_entry = static_cast<NavigationEntryImpl*>(
151 navigator()->GetController()->GetLastCommittedEntry());
152 if (nav_entry) {
153 nav_entry->RemoveEntryForFrame(this,
154 /* only_if_different_position = */ false);
155 }
156 }
157
dmazzonie950ea232015-03-13 21:39:45158 frame_tree_->FrameRemoved(this);
ericwilligers254597b2016-10-17 10:32:31159 for (auto& observer : observers_)
160 observer.OnFrameTreeNodeDestroyed(this);
alexmose201c7cd2015-06-10 17:14:21161
162 if (opener_)
163 opener_->RemoveObserver(opener_observer_.get());
jochen6004a362017-02-04 00:11:40164 if (original_opener_)
165 original_opener_->RemoveObserver(original_opener_observer_.get());
dmazzonie950ea232015-03-13 21:39:45166
167 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
jam39258caf2016-11-02 14:48:18168
danakjf9400602019-06-07 15:44:58169 bool did_stop_loading = false;
170
jam39258caf2016-11-02 14:48:18171 if (navigation_request_) {
danakjf9400602019-06-07 15:44:58172 navigation_request_.reset();
Arthur Hemery0dd65812019-08-01 14:18:45173 // If a frame with a pending navigation is detached, make sure the
174 // WebContents (and its observers) update their loading state.
danakjf9400602019-06-07 15:44:58175 did_stop_loading = true;
jam39258caf2016-11-02 14:48:18176 }
Nate Chapin22ea6592019-03-05 22:29:02177
danakjf9400602019-06-07 15:44:58178 // ~SiteProcessCountTracker DCHECKs in some tests if the speculative
179 // RenderFrameHostImpl is not destroyed last. Ideally this would be closer to
180 // (possible before) the ResetLoadingState() call above.
181 //
182 // There is an inherent race condition causing bugs 838348/915179/et al, where
183 // the renderer may have committed the speculative main frame and the browser
184 // has not heard about it yet. If this is a main frame, then in that case the
185 // speculative RenderFrame was unable to be deleted (it is owned by the
186 // renderer) and we should not be able to cancel the navigation at this point.
187 // CleanUpNavigation() would normally be called here but it will try to undo
188 // the navigation and expose the race condition. When it replaces the main
189 // frame with a RenderFrameProxy, that leaks the committed main frame, leaving
190 // the frame and its friend group with pointers that will become invalid
191 // shortly as we are shutting everything down and deleting the RenderView etc.
192 // We avoid this problematic situation by not calling CleanUpNavigation() or
193 // DiscardUnusedFrame() here. The speculative RenderFrameHost is simply
194 // returned and deleted immediately. This satisfies the requirement that the
195 // speculative RenderFrameHost is removed from the RenderFrameHostManager
196 // before it is destroyed.
197 if (render_manager_.speculative_frame_host()) {
198 did_stop_loading |= render_manager_.speculative_frame_host()->is_loading();
199 render_manager_.UnsetSpeculativeRenderFrameHost();
200 }
201
202 if (did_stop_loading)
203 DidStopLoading();
204
Nate Chapin22ea6592019-03-05 22:29:02205 DCHECK(!IsLoading());
[email protected]9b159a52013-10-03 17:24:55206}
207
alexmose201c7cd2015-06-10 17:14:21208void FrameTreeNode::AddObserver(Observer* observer) {
209 observers_.AddObserver(observer);
210}
211
212void FrameTreeNode::RemoveObserver(Observer* observer) {
213 observers_.RemoveObserver(observer);
214}
215
[email protected]94d0cc12013-12-18 00:07:41216bool FrameTreeNode::IsMainFrame() const {
217 return frame_tree_->root() == this;
218}
219
Ian Clelland5cbaaf82017-11-27 22:00:03220void FrameTreeNode::ResetForNavigation() {
221 // Discard any CSP headers from the previous document.
222 replication_state_.accumulated_csp_headers.clear();
223 render_manager_.OnDidResetContentSecurityPolicy();
224
Ian Clellandedb8c5dd2018-03-01 17:01:37225 // Clear any CSP-set sandbox flags, and the declared feature policy for the
226 // frame.
227 UpdateFramePolicyHeaders(blink::WebSandboxFlags::kNone, {});
Shivani Sharmac4f561582018-11-15 15:58:39228
Mustaq Ahmedc53e4c562019-01-29 19:05:09229 // This frame has had its user activation bits cleared in the renderer
230 // before arriving here. We just need to clear them here and in the other
231 // renderer processes that may have a reference to this frame.
232 UpdateUserActivationState(blink::UserActivationUpdateType::kClearActivation);
Ian Clelland5cbaaf82017-11-27 22:00:03233}
234
yilkal34a3d752019-08-30 18:20:30235size_t FrameTreeNode::GetFrameTreeSize() const {
236 if (is_collapsed())
237 return 0;
238
239 size_t size = 0;
240 for (size_t i = 0; i < child_count(); i++) {
241 size += child_at(i)->GetFrameTreeSize();
242 }
243
244 // Account for this node.
245 size++;
246 return size;
247}
248
alexmose201c7cd2015-06-10 17:14:21249void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
250 if (opener_) {
251 opener_->RemoveObserver(opener_observer_.get());
252 opener_observer_.reset();
253 }
254
255 opener_ = opener;
256
257 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55258 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21259 opener_->AddObserver(opener_observer_.get());
260 }
261}
262
jochen6004a362017-02-04 00:11:40263void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39264 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37265 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40266
Avi Drissman36465f332017-09-11 20:49:39267 if (original_opener_) {
268 original_opener_->RemoveObserver(original_opener_observer_.get());
269 original_opener_observer_.reset();
270 }
271
jochen6004a362017-02-04 00:11:40272 original_opener_ = opener;
273
274 if (original_opener_) {
jochen6004a362017-02-04 00:11:40275 original_opener_observer_ =
Jeremy Roman04f27c372017-10-27 15:20:55276 std::make_unique<OpenerDestroyedObserver>(this, true);
jochen6004a362017-02-04 00:11:40277 original_opener_->AddObserver(original_opener_observer_.get());
278 }
279}
280
creisf0f069a2015-07-23 23:51:53281void FrameTreeNode::SetCurrentURL(const GURL& url) {
Balazs Engedyc8a7cccf2018-03-12 23:00:49282 if (!has_committed_real_load_ && !url.IsAboutBlank())
creisf0f069a2015-07-23 23:51:53283 has_committed_real_load_ = true;
Erik Chen173bf3042017-07-31 06:06:21284 current_frame_host()->SetLastCommittedUrl(url);
xiaochenghb9554bb2016-05-21 14:20:48285 blame_context_.TakeSnapshot();
creisf0f069a2015-07-23 23:51:53286}
287
estarkbd8e26f2016-03-16 23:30:37288void FrameTreeNode::SetCurrentOrigin(
289 const url::Origin& origin,
290 bool is_potentially_trustworthy_unique_origin) {
291 if (!origin.IsSameOriginWith(replication_state_.origin) ||
292 replication_state_.has_potentially_trustworthy_unique_origin !=
293 is_potentially_trustworthy_unique_origin) {
294 render_manager_.OnDidUpdateOrigin(origin,
295 is_potentially_trustworthy_unique_origin);
296 }
alexmosa7a4ff822015-04-27 17:59:56297 replication_state_.origin = origin;
estarkbd8e26f2016-03-16 23:30:37298 replication_state_.has_potentially_trustworthy_unique_origin =
299 is_potentially_trustworthy_unique_origin;
alexmosa7a4ff822015-04-27 17:59:56300}
alexmosbe2f4c32015-03-10 02:30:23301
engedy6e2e0992017-05-25 18:58:42302void FrameTreeNode::SetCollapsed(bool collapsed) {
303 DCHECK(!IsMainFrame());
304 if (is_collapsed_ == collapsed)
305 return;
306
307 is_collapsed_ = collapsed;
308 render_manager_.OnDidChangeCollapsedState(collapsed);
309}
310
lukasza464d8692016-02-22 19:26:32311void FrameTreeNode::SetFrameName(const std::string& name,
312 const std::string& unique_name) {
313 if (name == replication_state_.name) {
314 // |unique_name| shouldn't change unless |name| changes.
315 DCHECK_EQ(unique_name, replication_state_.unique_name);
316 return;
317 }
lukasza5140a412016-09-15 21:12:30318
319 if (parent()) {
320 // Non-main frames should have a non-empty unique name.
321 DCHECK(!unique_name.empty());
322 } else {
323 // Unique name of main frames should always stay empty.
324 DCHECK(unique_name.empty());
325 }
326
Daniel Cheng6ca7f1c92017-08-09 21:45:41327 // Note the unique name should only be able to change before the first real
328 // load is committed, but that's not strongly enforced here.
lukasza464d8692016-02-22 19:26:32329 render_manager_.OnDidUpdateName(name, unique_name);
alexmosa7a4ff822015-04-27 17:59:56330 replication_state_.name = name;
lukasza464d8692016-02-22 19:26:32331 replication_state_.unique_name = unique_name;
alexmosbe2f4c32015-03-10 02:30:23332}
333
arthursonzogni662aa652017-03-28 11:09:50334void FrameTreeNode::AddContentSecurityPolicies(
335 const std::vector<ContentSecurityPolicyHeader>& headers) {
336 replication_state_.accumulated_csp_headers.insert(
337 replication_state_.accumulated_csp_headers.end(), headers.begin(),
338 headers.end());
339 render_manager_.OnDidAddContentSecurityPolicies(headers);
lukasza8e1c02e42016-05-17 20:05:10340}
341
mkwstf672e7ef2016-06-09 20:51:07342void FrameTreeNode::SetInsecureRequestPolicy(
343 blink::WebInsecureRequestPolicy policy) {
344 if (policy == replication_state_.insecure_request_policy)
estarka886b8d2015-12-18 21:53:08345 return;
mkwstf672e7ef2016-06-09 20:51:07346 render_manager_.OnEnforceInsecureRequestPolicy(policy);
347 replication_state_.insecure_request_policy = policy;
estarka886b8d2015-12-18 21:53:08348}
349
arthursonzogni4b62a5cb2018-01-17 14:14:26350void FrameTreeNode::SetInsecureNavigationsSet(
351 const std::vector<uint32_t>& insecure_navigations_set) {
352 DCHECK(std::is_sorted(insecure_navigations_set.begin(),
353 insecure_navigations_set.end()));
354 if (insecure_navigations_set == replication_state_.insecure_navigations_set)
355 return;
356 render_manager_.OnEnforceInsecureNavigationsSet(insecure_navigations_set);
357 replication_state_.insecure_navigations_set = insecure_navigations_set;
358}
359
Luna Luc3fdacdf2017-11-08 04:48:53360void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Ian Clellandcdc4f312017-10-13 22:24:12361 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25362
Ian Clellandcdc4f312017-10-13 22:24:12363 if (parent()) {
364 // Subframes should always inherit their parent's sandbox flags.
Ian Clelland5cbaaf82017-11-27 22:00:03365 pending_frame_policy_.sandbox_flags |= parent()->active_sandbox_flags();
Ian Clellandcdc4f312017-10-13 22:24:12366 // This is only applied on subframes; container policy is not mutable on
367 // main frame.
368 pending_frame_policy_.container_policy = frame_policy.container_policy;
369 }
iclelland92f8c0b2017-04-19 12:43:05370}
371
alexmos9f8705a2015-05-06 19:58:59372FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39373 return GetSibling(-1);
374}
alexmos9f8705a2015-05-06 19:58:59375
paulmeyer322777fb2016-05-16 23:15:39376FrameTreeNode* FrameTreeNode::NextSibling() const {
377 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59378}
379
fdegans4a49ce932015-03-12 17:11:37380bool FrameTreeNode::IsLoading() const {
381 RenderFrameHostImpl* current_frame_host =
382 render_manager_.current_frame_host();
fdegans4a49ce932015-03-12 17:11:37383
384 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39385
clamy610c63b32017-12-22 15:05:18386 if (navigation_request_)
387 return true;
clamy11e11512015-07-07 16:42:17388
clamy610c63b32017-12-22 15:05:18389 RenderFrameHostImpl* speculative_frame_host =
390 render_manager_.speculative_frame_host();
391 if (speculative_frame_host && speculative_frame_host->is_loading())
392 return true;
fdegans4a49ce932015-03-12 17:11:37393 return current_frame_host->is_loading();
394}
395
iclelland92f8c0b2017-04-19 12:43:05396bool FrameTreeNode::CommitPendingFramePolicy() {
Ian Clellandcdc4f312017-10-13 22:24:12397 bool did_change_flags = pending_frame_policy_.sandbox_flags !=
398 replication_state_.frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05399 bool did_change_container_policy =
Ian Clellandcdc4f312017-10-13 22:24:12400 pending_frame_policy_.container_policy !=
401 replication_state_.frame_policy.container_policy;
iclelland92f8c0b2017-04-19 12:43:05402 if (did_change_flags)
Ian Clellandcdc4f312017-10-13 22:24:12403 replication_state_.frame_policy.sandbox_flags =
404 pending_frame_policy_.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05405 if (did_change_container_policy)
Ian Clellandcdc4f312017-10-13 22:24:12406 replication_state_.frame_policy.container_policy =
407 pending_frame_policy_.container_policy;
Ian Clellandedb8c5dd2018-03-01 17:01:37408 UpdateFramePolicyHeaders(pending_frame_policy_.sandbox_flags,
409 replication_state_.feature_policy_header);
iclelland92f8c0b2017-04-19 12:43:05410 return did_change_flags || did_change_container_policy;
alexmos6b294562015-03-05 19:24:10411}
412
Arthur Hemeryc3380172018-01-22 14:00:17413void FrameTreeNode::TransferNavigationRequestOwnership(
414 RenderFrameHostImpl* render_frame_host) {
Andrey Kosyakovf2d4ff72018-10-29 20:09:59415 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Arthur Hemeryc3380172018-01-22 14:00:17416 render_frame_host->SetNavigationRequest(std::move(navigation_request_));
417}
418
carloskc49005eb2015-06-16 11:25:07419void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57420 std::unique_ptr<NavigationRequest> navigation_request) {
arthursonzognic79c251c2016-08-18 15:00:37421 // This is never called when navigating to a Javascript URL. For the loading
422 // state, this matches what Blink is doing: Blink doesn't send throbber
423 // notifications for Javascript URLS.
424 DCHECK(!navigation_request->common_params().url.SchemeIs(
425 url::kJavaScriptScheme));
426
clamy44e84ce2016-02-22 15:38:25427 bool was_previously_loading = frame_tree()->IsLoading();
428
clamy82a2f4d2016-02-02 14:20:41429 // There's no need to reset the state: there's still an ongoing load, and the
430 // RenderFrameHostManager will take care of updates to the speculative
431 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05432 if (was_previously_loading) {
clamy080e7962017-05-25 00:44:18433 if (navigation_request_ && navigation_request_->navigation_handle()) {
jamcd0b7b22017-03-24 22:13:05434 // Mark the old request as aborted.
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52435 navigation_request_->set_net_error(net::ERR_ABORTED);
jamcd0b7b22017-03-24 22:13:05436 }
clamya86695b2017-03-23 14:45:48437 ResetNavigationRequest(true, true);
jamcd0b7b22017-03-24 22:13:05438 }
clamy44e84ce2016-02-22 15:38:25439
440 navigation_request_ = std::move(navigation_request);
Shubhie Panickerddf2a4e2018-03-06 00:09:06441 if (was_discarded_) {
442 navigation_request_->set_was_discarded();
443 was_discarded_ = false;
444 }
clamy8e2e299202016-04-05 11:44:59445 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39446
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51447 bool to_different_document = !NavigationTypeUtils::IsSameDocument(
arthursonzogni92f18682017-02-08 23:00:04448 navigation_request_->common_params().navigation_type);
449
450 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16451}
452
clamya86695b2017-03-23 14:45:48453void FrameTreeNode::ResetNavigationRequest(bool keep_state,
454 bool inform_renderer) {
fdegans39ff0382015-04-29 19:04:39455 if (!navigation_request_)
456 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23457
Andrey Kosyakovf2d4ff72018-10-29 20:09:59458 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Pavel Feldmand8352ac2017-11-10 00:37:41459
John Abd-El-Malekdcc7bf42017-09-12 22:30:23460 // The renderer should be informed if the caller allows to do so and the
461 // navigation came from a BeginNavigation IPC.
Arthur Hemeryd3011f62018-05-30 10:38:44462 bool need_to_inform_renderer =
463 !IsPerNavigationMojoInterfaceEnabled() & inform_renderer &&
464 navigation_request_->from_begin_navigation();
John Abd-El-Malekdcc7bf42017-09-12 22:30:23465
clamy8e2e299202016-04-05 11:44:59466 NavigationRequest::AssociatedSiteInstanceType site_instance_type =
467 navigation_request_->associated_site_instance_type();
clamydcb434c12015-04-16 19:29:16468 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39469
clamy82a2f4d2016-02-02 14:20:41470 if (keep_state)
fdegans39ff0382015-04-29 19:04:39471 return;
472
clamy82a2f4d2016-02-02 14:20:41473 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
474 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39475 DidStopLoading();
476 render_manager_.CleanUpNavigation();
clamy2567242a2016-02-22 18:27:38477
clamy8e2e299202016-04-05 11:44:59478 // When reusing the same SiteInstance, a pending WebUI may have been created
479 // on behalf of the navigation in the current RenderFrameHost. Clear it.
480 if (site_instance_type ==
481 NavigationRequest::AssociatedSiteInstanceType::CURRENT) {
482 current_frame_host()->ClearPendingWebUI();
483 }
484
clamy2567242a2016-02-22 18:27:38485 // If the navigation is renderer-initiated, the renderer should also be
clamya86695b2017-03-23 14:45:48486 // informed that the navigation stopped if needed. In the case the renderer
487 // process asked for the navigation to be aborted, e.g. following a
488 // document.open, do not send an IPC to the renderer process as it already
489 // expects the navigation to stop.
John Abd-El-Malekdcc7bf42017-09-12 22:30:23490 if (need_to_inform_renderer) {
clamy2567242a2016-02-22 18:27:38491 current_frame_host()->Send(
John Abd-El-Malekdcc7bf42017-09-12 22:30:23492 new FrameMsg_DroppedNavigation(current_frame_host()->GetRoutingID()));
clamy2567242a2016-02-22 18:27:38493 }
clamydcb434c12015-04-16 19:29:16494}
495
clamy44e84ce2016-02-22 15:38:25496void FrameTreeNode::DidStartLoading(bool to_different_document,
497 bool was_previously_loading) {
Camille Lamyefd54b02018-10-04 16:54:14498 TRACE_EVENT2("navigation", "FrameTreeNode::DidStartLoading",
499 "frame_tree_node", frame_tree_node_id(), "to different document",
500 to_different_document);
fdegansa696e5112015-04-17 01:57:59501 // Any main frame load to a new document should reset the load progress since
502 // it will replace the current page and any frames. The WebContents will
503 // be notified when DidChangeLoadProgress is called.
504 if (to_different_document && IsMainFrame())
505 frame_tree_->ResetLoadProgress();
506
507 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25508 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59509 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
510
511 // Set initial load progress and update overall progress. This will notify
512 // the WebContents of the load progress change.
513 DidChangeLoadProgress(kLoadingProgressMinimum);
514
515 // Notify the RenderFrameHostManager of the event.
516 render_manager()->OnDidStartLoading();
517}
518
519void FrameTreeNode::DidStopLoading() {
Camille Lamyefd54b02018-10-04 16:54:14520 TRACE_EVENT1("navigation", "FrameTreeNode::DidStopLoading", "frame_tree_node",
521 frame_tree_node_id());
fdegansa696e5112015-04-17 01:57:59522 // Set final load progress and update overall progress. This will notify
523 // the WebContents of the load progress change.
524 DidChangeLoadProgress(kLoadingProgressDone);
525
Lucas Furukawa Gadani6faef602019-05-06 21:16:03526 // Notify the RenderFrameHostManager of the event.
527 render_manager()->OnDidStopLoading();
528
fdegansa696e5112015-04-17 01:57:59529 // Notify the WebContents.
530 if (!frame_tree_->IsLoading())
531 navigator()->GetDelegate()->DidStopLoading();
532
Dominic Mazzoni3c7007a2017-12-12 07:34:05533 // Notify accessibility that the user is no longer trying to load or
534 // reload a page.
Dominic Farolinof87ec2a2019-07-04 09:18:09535 // TODO(domfarolino): Remove this in favor of notifying via the delegate's
536 // DidStopLoading() above.
Dominic Mazzoni3c7007a2017-12-12 07:34:05537 BrowserAccessibilityManager* manager =
538 current_frame_host()->browser_accessibility_manager();
539 if (manager)
540 manager->DidStopLoading();
fdegansa696e5112015-04-17 01:57:59541}
542
543void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
Nate Chapin93536702018-02-07 00:12:21544 DCHECK_GE(load_progress, kLoadingProgressMinimum);
545 DCHECK_LE(load_progress, kLoadingProgressDone);
546 if (IsMainFrame())
547 frame_tree_->UpdateLoadProgress(load_progress);
fdegansa696e5112015-04-17 01:57:59548}
549
clamyf73862c42015-07-08 12:31:33550bool FrameTreeNode::StopLoading() {
Nasko Oskova9511352018-09-15 00:11:10551 if (navigation_request_) {
552 int expected_pending_nav_entry_id = navigation_request_->nav_entry_id();
553 if (navigation_request_->navigation_handle()) {
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52554 navigation_request_->set_net_error(net::ERR_ABORTED);
Nasko Oskova9511352018-09-15 00:11:10555 expected_pending_nav_entry_id =
556 navigation_request_->navigation_handle()->pending_nav_entry_id();
jam0299edae2017-03-10 00:49:22557 }
Nasko Oskova9511352018-09-15 00:11:10558 navigator_->DiscardPendingEntryIfNeeded(expected_pending_nav_entry_id);
jam0299edae2017-03-10 00:49:22559 }
Nasko Oskova9511352018-09-15 00:11:10560 ResetNavigationRequest(false, true);
clamyf73862c42015-07-08 12:31:33561
562 // TODO(nasko): see if child frames should send IPCs in site-per-process
563 // mode.
564 if (!IsMainFrame())
565 return true;
566
567 render_manager_.Stop();
568 return true;
569}
570
alexmos21acae52015-11-07 01:04:43571void FrameTreeNode::DidFocus() {
572 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31573 for (auto& observer : observers_)
574 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43575}
576
clamy44e84ce2016-02-22 15:38:25577void FrameTreeNode::BeforeUnloadCanceled() {
578 // TODO(clamy): Support BeforeUnload in subframes.
579 if (!IsMainFrame())
580 return;
581
582 RenderFrameHostImpl* current_frame_host =
583 render_manager_.current_frame_host();
584 DCHECK(current_frame_host);
585 current_frame_host->ResetLoadingState();
586
clamy610c63b32017-12-22 15:05:18587 RenderFrameHostImpl* speculative_frame_host =
588 render_manager_.speculative_frame_host();
589 if (speculative_frame_host)
590 speculative_frame_host->ResetLoadingState();
591 // Note: there is no need to set an error code on the NavigationHandle here
592 // as it has not been created yet. It is only created when the
593 // BeforeUnloadACK is received.
594 if (navigation_request_)
595 ResetNavigationRequest(false, true);
clamy44e84ce2016-02-22 15:38:25596}
597
Mustaq Ahmedc4cb7162018-06-05 16:28:36598bool FrameTreeNode::NotifyUserActivation() {
John Delaneyedd8d6c2019-01-25 00:23:57599 for (FrameTreeNode* node = this; node; node = node->parent()) {
600 if (!node->user_activation_state_.HasBeenActive() &&
601 node->current_frame_host())
602 node->current_frame_host()->DidReceiveFirstUserActivation();
Mustaq Ahmedc4cb7162018-06-05 16:28:36603 node->user_activation_state_.Activate();
John Delaneyedd8d6c2019-01-25 00:23:57604 }
japhet61835ae12017-01-20 01:25:39605 replication_state_.has_received_user_gesture = true;
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14606
Mustaq Ahmed0180320f2019-03-21 16:07:01607 // See the "Same-origin Visibility" section in |UserActivationState| class
608 // doc.
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14609 if (base::FeatureList::IsEnabled(features::kUserActivationV2) &&
610 base::FeatureList::IsEnabled(
611 features::kUserActivationSameOriginVisibility)) {
612 const url::Origin& current_origin =
613 this->current_frame_host()->GetLastCommittedOrigin();
614 for (FrameTreeNode* node : frame_tree()->Nodes()) {
615 if (node->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith(
616 current_origin)) {
617 node->user_activation_state_.Activate();
618 }
619 }
620 }
621
Shivani Sharma20749e922019-03-11 17:00:26622 NavigationControllerImpl* controller =
623 static_cast<NavigationControllerImpl*>(navigator()->GetController());
624 if (controller)
625 controller->NotifyUserActivation();
Shivani Sharma194877032019-03-07 17:52:47626
Mustaq Ahmedc4cb7162018-06-05 16:28:36627 return true;
628}
629
630bool FrameTreeNode::ConsumeTransientUserActivation() {
631 bool was_active = user_activation_state_.IsActive();
632 for (FrameTreeNode* node : frame_tree()->Nodes())
633 node->user_activation_state_.ConsumeIfActive();
634 return was_active;
635}
636
Shivani Sharmac4f561582018-11-15 15:58:39637bool FrameTreeNode::ClearUserActivation() {
Shivani Sharmac4f561582018-11-15 15:58:39638 for (FrameTreeNode* node : frame_tree()->SubtreeNodes(this))
639 node->user_activation_state_.Clear();
640 return true;
641}
642
Ella Ge9caed612019-08-09 16:17:25643bool FrameTreeNode::VerifyUserActivation() {
644 if (!base::FeatureList::IsEnabled(features::kBrowserVerifiedUserActivation))
645 return true;
646 return render_manager_.current_frame_host()
647 ->GetRenderWidgetHost()
648 ->ConsumePendingUserActivationIfAllowed();
649}
650
Mustaq Ahmedc4cb7162018-06-05 16:28:36651bool FrameTreeNode::UpdateUserActivationState(
652 blink::UserActivationUpdateType update_type) {
Ella Ge9caed612019-08-09 16:17:25653 bool update_result = false;
Mustaq Ahmedc4cb7162018-06-05 16:28:36654 switch (update_type) {
655 case blink::UserActivationUpdateType::kConsumeTransientActivation:
Ella Ge9caed612019-08-09 16:17:25656 update_result = ConsumeTransientUserActivation();
657 break;
Mustaq Ahmedc4cb7162018-06-05 16:28:36658 case blink::UserActivationUpdateType::kNotifyActivation:
Ella Ge9caed612019-08-09 16:17:25659 update_result = NotifyUserActivation();
660 break;
661 case blink::UserActivationUpdateType::
662 kNotifyActivationPendingBrowserVerification:
663 if (VerifyUserActivation()) {
664 update_result = NotifyUserActivation();
665 update_type = blink::UserActivationUpdateType::kNotifyActivation;
666 } else {
667 // TODO(crbug.com/848778): We need to decide what to do when user
668 // activation verification failed. NOTREACHED here will make all
669 // unrelated tests that inject event to renderer fail.
670 return false;
671 }
672 break;
Shivani Sharmac4f561582018-11-15 15:58:39673 case blink::UserActivationUpdateType::kClearActivation:
Ella Ge9caed612019-08-09 16:17:25674 update_result = ClearUserActivation();
675 break;
Mustaq Ahmedc4cb7162018-06-05 16:28:36676 }
Ella Ge9caed612019-08-09 16:17:25677 render_manager_.UpdateUserActivationState(update_type);
678 return update_result;
japhet61835ae12017-01-20 01:25:39679}
680
Becca Hughes60af7d42017-12-12 10:53:15681void FrameTreeNode::OnSetHasReceivedUserGestureBeforeNavigation(bool value) {
682 render_manager_.OnSetHasReceivedUserGestureBeforeNavigation(value);
683 replication_state_.has_received_user_gesture_before_nav = value;
684}
685
paulmeyer322777fb2016-05-16 23:15:39686FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19687 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39688 return nullptr;
689
690 for (size_t i = 0; i < parent_->child_count(); ++i) {
691 if (parent_->child_at(i) == this) {
692 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
693 i + relative_offset >= parent_->child_count()) {
694 return nullptr;
695 }
696 return parent_->child_at(i + relative_offset);
697 }
698 }
699
700 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
701 return nullptr;
702}
703
Ian Clellandedb8c5dd2018-03-01 17:01:37704void FrameTreeNode::UpdateFramePolicyHeaders(
705 blink::WebSandboxFlags sandbox_flags,
706 const blink::ParsedFeaturePolicy& parsed_header) {
707 bool changed = false;
708 if (replication_state_.feature_policy_header != parsed_header) {
709 replication_state_.feature_policy_header = parsed_header;
710 changed = true;
711 }
Ian Clelland5cbaaf82017-11-27 22:00:03712 // TODO(iclelland): Kill the renderer if sandbox flags is not a subset of the
713 // currently effective sandbox flags from the frame. https://crbug.com/740556
Ian Clellandedb8c5dd2018-03-01 17:01:37714 blink::WebSandboxFlags updated_flags =
Ian Clelland5cbaaf82017-11-27 22:00:03715 sandbox_flags | effective_frame_policy().sandbox_flags;
Ian Clellandedb8c5dd2018-03-01 17:01:37716 if (replication_state_.active_sandbox_flags != updated_flags) {
717 replication_state_.active_sandbox_flags = updated_flags;
718 changed = true;
719 }
720 // Notify any proxies if the policies have been changed.
721 if (changed)
722 render_manager()->OnDidSetFramePolicyHeaders();
Ian Clelland5cbaaf82017-11-27 22:00:03723}
724
Lan Weif0cb8e6e2019-04-19 00:14:53725void FrameTreeNode::TransferUserActivationFrom(
726 RenderFrameHostImpl* source_rfh) {
727 user_activation_state_.TransferFrom(
728 source_rfh->frame_tree_node()->user_activation_state_);
729
730 // Notify proxies in non-source and non-target renderer processes to
731 // transfer the activation state from the source proxy to the target
732 // so the user activation state of those proxies matches the source
733 // renderer and the target renderer (which are separately updated).
734 render_manager_.TransferUserActivationFrom(source_rfh);
Lan Wei550671a22019-03-19 00:59:24735}
736
Arthur Sonzognif8840b92018-11-07 14:10:35737void FrameTreeNode::PruneChildFrameNavigationEntries(
738 NavigationEntryImpl* entry) {
739 for (size_t i = 0; i < current_frame_host()->child_count(); ++i) {
740 FrameTreeNode* child = current_frame_host()->child_at(i);
741 if (child->is_created_by_script_) {
742 entry->RemoveEntryForFrame(child,
743 /* only_if_different_position = */ false);
744 } else {
745 child->PruneChildFrameNavigationEntries(entry);
746 }
747 }
748}
749
Ehsan Karamad39407082019-02-19 23:38:19750void FrameTreeNode::SetOpenerFeaturePolicyState(
751 const blink::FeaturePolicy::FeatureState& feature_state) {
752 DCHECK(IsMainFrame());
753 if (base::FeatureList::IsEnabled(features::kFeaturePolicyForSandbox)) {
754 replication_state_.opener_feature_state = feature_state;
755 }
756}
757
[email protected]9b159a52013-10-03 17:24:55758} // namespace content