blob: 7fad84103cd1dcc77037e29948b3543246952977 [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
alexmose201c7cd2015-06-10 17:14:21235void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
236 if (opener_) {
237 opener_->RemoveObserver(opener_observer_.get());
238 opener_observer_.reset();
239 }
240
241 opener_ = opener;
242
243 if (opener_) {
Jeremy Roman04f27c372017-10-27 15:20:55244 opener_observer_ = std::make_unique<OpenerDestroyedObserver>(this, false);
alexmose201c7cd2015-06-10 17:14:21245 opener_->AddObserver(opener_observer_.get());
246 }
247}
248
jochen6004a362017-02-04 00:11:40249void FrameTreeNode::SetOriginalOpener(FrameTreeNode* opener) {
Avi Drissman36465f332017-09-11 20:49:39250 // The original opener tracks main frames only.
avi8d1aa162017-03-27 18:27:37251 DCHECK(opener == nullptr || !opener->parent());
jochen6004a362017-02-04 00:11:40252
Avi Drissman36465f332017-09-11 20:49:39253 if (original_opener_) {
254 original_opener_->RemoveObserver(original_opener_observer_.get());
255 original_opener_observer_.reset();
256 }
257
jochen6004a362017-02-04 00:11:40258 original_opener_ = opener;
259
260 if (original_opener_) {
jochen6004a362017-02-04 00:11:40261 original_opener_observer_ =
Jeremy Roman04f27c372017-10-27 15:20:55262 std::make_unique<OpenerDestroyedObserver>(this, true);
jochen6004a362017-02-04 00:11:40263 original_opener_->AddObserver(original_opener_observer_.get());
264 }
265}
266
creisf0f069a2015-07-23 23:51:53267void FrameTreeNode::SetCurrentURL(const GURL& url) {
Balazs Engedyc8a7cccf2018-03-12 23:00:49268 if (!has_committed_real_load_ && !url.IsAboutBlank())
creisf0f069a2015-07-23 23:51:53269 has_committed_real_load_ = true;
Erik Chen173bf3042017-07-31 06:06:21270 current_frame_host()->SetLastCommittedUrl(url);
xiaochenghb9554bb2016-05-21 14:20:48271 blame_context_.TakeSnapshot();
creisf0f069a2015-07-23 23:51:53272}
273
estarkbd8e26f2016-03-16 23:30:37274void FrameTreeNode::SetCurrentOrigin(
275 const url::Origin& origin,
276 bool is_potentially_trustworthy_unique_origin) {
277 if (!origin.IsSameOriginWith(replication_state_.origin) ||
278 replication_state_.has_potentially_trustworthy_unique_origin !=
279 is_potentially_trustworthy_unique_origin) {
280 render_manager_.OnDidUpdateOrigin(origin,
281 is_potentially_trustworthy_unique_origin);
282 }
alexmosa7a4ff822015-04-27 17:59:56283 replication_state_.origin = origin;
estarkbd8e26f2016-03-16 23:30:37284 replication_state_.has_potentially_trustworthy_unique_origin =
285 is_potentially_trustworthy_unique_origin;
alexmosa7a4ff822015-04-27 17:59:56286}
alexmosbe2f4c32015-03-10 02:30:23287
engedy6e2e0992017-05-25 18:58:42288void FrameTreeNode::SetCollapsed(bool collapsed) {
289 DCHECK(!IsMainFrame());
290 if (is_collapsed_ == collapsed)
291 return;
292
293 is_collapsed_ = collapsed;
294 render_manager_.OnDidChangeCollapsedState(collapsed);
295}
296
lukasza464d8692016-02-22 19:26:32297void FrameTreeNode::SetFrameName(const std::string& name,
298 const std::string& unique_name) {
299 if (name == replication_state_.name) {
300 // |unique_name| shouldn't change unless |name| changes.
301 DCHECK_EQ(unique_name, replication_state_.unique_name);
302 return;
303 }
lukasza5140a412016-09-15 21:12:30304
305 if (parent()) {
306 // Non-main frames should have a non-empty unique name.
307 DCHECK(!unique_name.empty());
308 } else {
309 // Unique name of main frames should always stay empty.
310 DCHECK(unique_name.empty());
311 }
312
Daniel Cheng6ca7f1c92017-08-09 21:45:41313 // Note the unique name should only be able to change before the first real
314 // load is committed, but that's not strongly enforced here.
lukasza464d8692016-02-22 19:26:32315 render_manager_.OnDidUpdateName(name, unique_name);
alexmosa7a4ff822015-04-27 17:59:56316 replication_state_.name = name;
lukasza464d8692016-02-22 19:26:32317 replication_state_.unique_name = unique_name;
alexmosbe2f4c32015-03-10 02:30:23318}
319
arthursonzogni662aa652017-03-28 11:09:50320void FrameTreeNode::AddContentSecurityPolicies(
321 const std::vector<ContentSecurityPolicyHeader>& headers) {
322 replication_state_.accumulated_csp_headers.insert(
323 replication_state_.accumulated_csp_headers.end(), headers.begin(),
324 headers.end());
325 render_manager_.OnDidAddContentSecurityPolicies(headers);
lukasza8e1c02e42016-05-17 20:05:10326}
327
mkwstf672e7ef2016-06-09 20:51:07328void FrameTreeNode::SetInsecureRequestPolicy(
329 blink::WebInsecureRequestPolicy policy) {
330 if (policy == replication_state_.insecure_request_policy)
estarka886b8d2015-12-18 21:53:08331 return;
mkwstf672e7ef2016-06-09 20:51:07332 render_manager_.OnEnforceInsecureRequestPolicy(policy);
333 replication_state_.insecure_request_policy = policy;
estarka886b8d2015-12-18 21:53:08334}
335
arthursonzogni4b62a5cb2018-01-17 14:14:26336void FrameTreeNode::SetInsecureNavigationsSet(
337 const std::vector<uint32_t>& insecure_navigations_set) {
338 DCHECK(std::is_sorted(insecure_navigations_set.begin(),
339 insecure_navigations_set.end()));
340 if (insecure_navigations_set == replication_state_.insecure_navigations_set)
341 return;
342 render_manager_.OnEnforceInsecureNavigationsSet(insecure_navigations_set);
343 replication_state_.insecure_navigations_set = insecure_navigations_set;
344}
345
Luna Luc3fdacdf2017-11-08 04:48:53346void FrameTreeNode::SetPendingFramePolicy(blink::FramePolicy frame_policy) {
Ian Clellandcdc4f312017-10-13 22:24:12347 pending_frame_policy_.sandbox_flags = frame_policy.sandbox_flags;
alexmos6e940102016-01-19 22:47:25348
Ian Clellandcdc4f312017-10-13 22:24:12349 if (parent()) {
350 // Subframes should always inherit their parent's sandbox flags.
Ian Clelland5cbaaf82017-11-27 22:00:03351 pending_frame_policy_.sandbox_flags |= parent()->active_sandbox_flags();
Ian Clellandcdc4f312017-10-13 22:24:12352 // This is only applied on subframes; container policy is not mutable on
353 // main frame.
354 pending_frame_policy_.container_policy = frame_policy.container_policy;
355 }
iclelland92f8c0b2017-04-19 12:43:05356}
357
alexmos9f8705a2015-05-06 19:58:59358FrameTreeNode* FrameTreeNode::PreviousSibling() const {
paulmeyer322777fb2016-05-16 23:15:39359 return GetSibling(-1);
360}
alexmos9f8705a2015-05-06 19:58:59361
paulmeyer322777fb2016-05-16 23:15:39362FrameTreeNode* FrameTreeNode::NextSibling() const {
363 return GetSibling(1);
alexmos9f8705a2015-05-06 19:58:59364}
365
fdegans4a49ce932015-03-12 17:11:37366bool FrameTreeNode::IsLoading() const {
367 RenderFrameHostImpl* current_frame_host =
368 render_manager_.current_frame_host();
fdegans4a49ce932015-03-12 17:11:37369
370 DCHECK(current_frame_host);
fdegans39ff0382015-04-29 19:04:39371
clamy610c63b32017-12-22 15:05:18372 if (navigation_request_)
373 return true;
clamy11e11512015-07-07 16:42:17374
clamy610c63b32017-12-22 15:05:18375 RenderFrameHostImpl* speculative_frame_host =
376 render_manager_.speculative_frame_host();
377 if (speculative_frame_host && speculative_frame_host->is_loading())
378 return true;
fdegans4a49ce932015-03-12 17:11:37379 return current_frame_host->is_loading();
380}
381
iclelland92f8c0b2017-04-19 12:43:05382bool FrameTreeNode::CommitPendingFramePolicy() {
Ian Clellandcdc4f312017-10-13 22:24:12383 bool did_change_flags = pending_frame_policy_.sandbox_flags !=
384 replication_state_.frame_policy.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05385 bool did_change_container_policy =
Ian Clellandcdc4f312017-10-13 22:24:12386 pending_frame_policy_.container_policy !=
387 replication_state_.frame_policy.container_policy;
iclelland92f8c0b2017-04-19 12:43:05388 if (did_change_flags)
Ian Clellandcdc4f312017-10-13 22:24:12389 replication_state_.frame_policy.sandbox_flags =
390 pending_frame_policy_.sandbox_flags;
iclelland92f8c0b2017-04-19 12:43:05391 if (did_change_container_policy)
Ian Clellandcdc4f312017-10-13 22:24:12392 replication_state_.frame_policy.container_policy =
393 pending_frame_policy_.container_policy;
Ian Clellandedb8c5dd2018-03-01 17:01:37394 UpdateFramePolicyHeaders(pending_frame_policy_.sandbox_flags,
395 replication_state_.feature_policy_header);
iclelland92f8c0b2017-04-19 12:43:05396 return did_change_flags || did_change_container_policy;
alexmos6b294562015-03-05 19:24:10397}
398
Arthur Hemeryc3380172018-01-22 14:00:17399void FrameTreeNode::TransferNavigationRequestOwnership(
400 RenderFrameHostImpl* render_frame_host) {
Andrey Kosyakovf2d4ff72018-10-29 20:09:59401 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Arthur Hemeryc3380172018-01-22 14:00:17402 render_frame_host->SetNavigationRequest(std::move(navigation_request_));
403}
404
carloskc49005eb2015-06-16 11:25:07405void FrameTreeNode::CreatedNavigationRequest(
dcheng9bfa5162016-04-09 01:00:57406 std::unique_ptr<NavigationRequest> navigation_request) {
arthursonzognic79c251c2016-08-18 15:00:37407 // This is never called when navigating to a Javascript URL. For the loading
408 // state, this matches what Blink is doing: Blink doesn't send throbber
409 // notifications for Javascript URLS.
410 DCHECK(!navigation_request->common_params().url.SchemeIs(
411 url::kJavaScriptScheme));
412
clamy44e84ce2016-02-22 15:38:25413 bool was_previously_loading = frame_tree()->IsLoading();
414
clamy82a2f4d2016-02-02 14:20:41415 // There's no need to reset the state: there's still an ongoing load, and the
416 // RenderFrameHostManager will take care of updates to the speculative
417 // RenderFrameHost in DidCreateNavigationRequest below.
jamcd0b7b22017-03-24 22:13:05418 if (was_previously_loading) {
clamy080e7962017-05-25 00:44:18419 if (navigation_request_ && navigation_request_->navigation_handle()) {
jamcd0b7b22017-03-24 22:13:05420 // Mark the old request as aborted.
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52421 navigation_request_->set_net_error(net::ERR_ABORTED);
jamcd0b7b22017-03-24 22:13:05422 }
clamya86695b2017-03-23 14:45:48423 ResetNavigationRequest(true, true);
jamcd0b7b22017-03-24 22:13:05424 }
clamy44e84ce2016-02-22 15:38:25425
426 navigation_request_ = std::move(navigation_request);
Shubhie Panickerddf2a4e2018-03-06 00:09:06427 if (was_discarded_) {
428 navigation_request_->set_was_discarded();
429 was_discarded_ = false;
430 }
clamy8e2e299202016-04-05 11:44:59431 render_manager()->DidCreateNavigationRequest(navigation_request_.get());
fdegans39ff0382015-04-29 19:04:39432
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51433 bool to_different_document = !NavigationTypeUtils::IsSameDocument(
arthursonzogni92f18682017-02-08 23:00:04434 navigation_request_->common_params().navigation_type);
435
436 DidStartLoading(to_different_document, was_previously_loading);
clamydcb434c12015-04-16 19:29:16437}
438
clamya86695b2017-03-23 14:45:48439void FrameTreeNode::ResetNavigationRequest(bool keep_state,
440 bool inform_renderer) {
fdegans39ff0382015-04-29 19:04:39441 if (!navigation_request_)
442 return;
John Abd-El-Malekdcc7bf42017-09-12 22:30:23443
Andrey Kosyakovf2d4ff72018-10-29 20:09:59444 devtools_instrumentation::OnResetNavigationRequest(navigation_request_.get());
Pavel Feldmand8352ac2017-11-10 00:37:41445
John Abd-El-Malekdcc7bf42017-09-12 22:30:23446 // The renderer should be informed if the caller allows to do so and the
447 // navigation came from a BeginNavigation IPC.
Arthur Hemeryd3011f62018-05-30 10:38:44448 bool need_to_inform_renderer =
449 !IsPerNavigationMojoInterfaceEnabled() & inform_renderer &&
450 navigation_request_->from_begin_navigation();
John Abd-El-Malekdcc7bf42017-09-12 22:30:23451
clamy8e2e299202016-04-05 11:44:59452 NavigationRequest::AssociatedSiteInstanceType site_instance_type =
453 navigation_request_->associated_site_instance_type();
clamydcb434c12015-04-16 19:29:16454 navigation_request_.reset();
fdegans39ff0382015-04-29 19:04:39455
clamy82a2f4d2016-02-02 14:20:41456 if (keep_state)
fdegans39ff0382015-04-29 19:04:39457 return;
458
clamy82a2f4d2016-02-02 14:20:41459 // The RenderFrameHostManager should clean up any speculative RenderFrameHost
460 // it created for the navigation. Also register that the load stopped.
fdegans39ff0382015-04-29 19:04:39461 DidStopLoading();
462 render_manager_.CleanUpNavigation();
clamy2567242a2016-02-22 18:27:38463
clamy8e2e299202016-04-05 11:44:59464 // When reusing the same SiteInstance, a pending WebUI may have been created
465 // on behalf of the navigation in the current RenderFrameHost. Clear it.
466 if (site_instance_type ==
467 NavigationRequest::AssociatedSiteInstanceType::CURRENT) {
468 current_frame_host()->ClearPendingWebUI();
469 }
470
clamy2567242a2016-02-22 18:27:38471 // If the navigation is renderer-initiated, the renderer should also be
clamya86695b2017-03-23 14:45:48472 // informed that the navigation stopped if needed. In the case the renderer
473 // process asked for the navigation to be aborted, e.g. following a
474 // document.open, do not send an IPC to the renderer process as it already
475 // expects the navigation to stop.
John Abd-El-Malekdcc7bf42017-09-12 22:30:23476 if (need_to_inform_renderer) {
clamy2567242a2016-02-22 18:27:38477 current_frame_host()->Send(
John Abd-El-Malekdcc7bf42017-09-12 22:30:23478 new FrameMsg_DroppedNavigation(current_frame_host()->GetRoutingID()));
clamy2567242a2016-02-22 18:27:38479 }
clamydcb434c12015-04-16 19:29:16480}
481
clamy44e84ce2016-02-22 15:38:25482void FrameTreeNode::DidStartLoading(bool to_different_document,
483 bool was_previously_loading) {
Camille Lamyefd54b02018-10-04 16:54:14484 TRACE_EVENT2("navigation", "FrameTreeNode::DidStartLoading",
485 "frame_tree_node", frame_tree_node_id(), "to different document",
486 to_different_document);
fdegansa696e5112015-04-17 01:57:59487 // Any main frame load to a new document should reset the load progress since
488 // it will replace the current page and any frames. The WebContents will
489 // be notified when DidChangeLoadProgress is called.
490 if (to_different_document && IsMainFrame())
491 frame_tree_->ResetLoadProgress();
492
493 // Notify the WebContents.
clamy44e84ce2016-02-22 15:38:25494 if (!was_previously_loading)
fdegansa696e5112015-04-17 01:57:59495 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
496
497 // Set initial load progress and update overall progress. This will notify
498 // the WebContents of the load progress change.
499 DidChangeLoadProgress(kLoadingProgressMinimum);
500
501 // Notify the RenderFrameHostManager of the event.
502 render_manager()->OnDidStartLoading();
503}
504
505void FrameTreeNode::DidStopLoading() {
Camille Lamyefd54b02018-10-04 16:54:14506 TRACE_EVENT1("navigation", "FrameTreeNode::DidStopLoading", "frame_tree_node",
507 frame_tree_node_id());
fdegansa696e5112015-04-17 01:57:59508 // Set final load progress and update overall progress. This will notify
509 // the WebContents of the load progress change.
510 DidChangeLoadProgress(kLoadingProgressDone);
511
Lucas Furukawa Gadani6faef602019-05-06 21:16:03512 // Notify the RenderFrameHostManager of the event.
513 render_manager()->OnDidStopLoading();
514
fdegansa696e5112015-04-17 01:57:59515 // Notify the WebContents.
516 if (!frame_tree_->IsLoading())
517 navigator()->GetDelegate()->DidStopLoading();
518
Dominic Mazzoni3c7007a2017-12-12 07:34:05519 // Notify accessibility that the user is no longer trying to load or
520 // reload a page.
Dominic Farolinof87ec2a2019-07-04 09:18:09521 // TODO(domfarolino): Remove this in favor of notifying via the delegate's
522 // DidStopLoading() above.
Dominic Mazzoni3c7007a2017-12-12 07:34:05523 BrowserAccessibilityManager* manager =
524 current_frame_host()->browser_accessibility_manager();
525 if (manager)
526 manager->DidStopLoading();
fdegansa696e5112015-04-17 01:57:59527}
528
529void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
Nate Chapin93536702018-02-07 00:12:21530 DCHECK_GE(load_progress, kLoadingProgressMinimum);
531 DCHECK_LE(load_progress, kLoadingProgressDone);
532 if (IsMainFrame())
533 frame_tree_->UpdateLoadProgress(load_progress);
fdegansa696e5112015-04-17 01:57:59534}
535
clamyf73862c42015-07-08 12:31:33536bool FrameTreeNode::StopLoading() {
Nasko Oskova9511352018-09-15 00:11:10537 if (navigation_request_) {
538 int expected_pending_nav_entry_id = navigation_request_->nav_entry_id();
539 if (navigation_request_->navigation_handle()) {
Mohamed Abdelhalimb4db22a2019-06-18 10:46:52540 navigation_request_->set_net_error(net::ERR_ABORTED);
Nasko Oskova9511352018-09-15 00:11:10541 expected_pending_nav_entry_id =
542 navigation_request_->navigation_handle()->pending_nav_entry_id();
jam0299edae2017-03-10 00:49:22543 }
Nasko Oskova9511352018-09-15 00:11:10544 navigator_->DiscardPendingEntryIfNeeded(expected_pending_nav_entry_id);
jam0299edae2017-03-10 00:49:22545 }
Nasko Oskova9511352018-09-15 00:11:10546 ResetNavigationRequest(false, true);
clamyf73862c42015-07-08 12:31:33547
548 // TODO(nasko): see if child frames should send IPCs in site-per-process
549 // mode.
550 if (!IsMainFrame())
551 return true;
552
553 render_manager_.Stop();
554 return true;
555}
556
alexmos21acae52015-11-07 01:04:43557void FrameTreeNode::DidFocus() {
558 last_focus_time_ = base::TimeTicks::Now();
ericwilligers254597b2016-10-17 10:32:31559 for (auto& observer : observers_)
560 observer.OnFrameTreeNodeFocused(this);
alexmos21acae52015-11-07 01:04:43561}
562
clamy44e84ce2016-02-22 15:38:25563void FrameTreeNode::BeforeUnloadCanceled() {
564 // TODO(clamy): Support BeforeUnload in subframes.
565 if (!IsMainFrame())
566 return;
567
568 RenderFrameHostImpl* current_frame_host =
569 render_manager_.current_frame_host();
570 DCHECK(current_frame_host);
571 current_frame_host->ResetLoadingState();
572
clamy610c63b32017-12-22 15:05:18573 RenderFrameHostImpl* speculative_frame_host =
574 render_manager_.speculative_frame_host();
575 if (speculative_frame_host)
576 speculative_frame_host->ResetLoadingState();
577 // Note: there is no need to set an error code on the NavigationHandle here
578 // as it has not been created yet. It is only created when the
579 // BeforeUnloadACK is received.
580 if (navigation_request_)
581 ResetNavigationRequest(false, true);
clamy44e84ce2016-02-22 15:38:25582}
583
Mustaq Ahmedc4cb7162018-06-05 16:28:36584bool FrameTreeNode::NotifyUserActivation() {
John Delaneyedd8d6c2019-01-25 00:23:57585 for (FrameTreeNode* node = this; node; node = node->parent()) {
586 if (!node->user_activation_state_.HasBeenActive() &&
587 node->current_frame_host())
588 node->current_frame_host()->DidReceiveFirstUserActivation();
Mustaq Ahmedc4cb7162018-06-05 16:28:36589 node->user_activation_state_.Activate();
John Delaneyedd8d6c2019-01-25 00:23:57590 }
japhet61835ae12017-01-20 01:25:39591 replication_state_.has_received_user_gesture = true;
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14592
Mustaq Ahmed0180320f2019-03-21 16:07:01593 // See the "Same-origin Visibility" section in |UserActivationState| class
594 // doc.
Mustaq Ahmeda5dfa60b2018-12-08 00:30:14595 if (base::FeatureList::IsEnabled(features::kUserActivationV2) &&
596 base::FeatureList::IsEnabled(
597 features::kUserActivationSameOriginVisibility)) {
598 const url::Origin& current_origin =
599 this->current_frame_host()->GetLastCommittedOrigin();
600 for (FrameTreeNode* node : frame_tree()->Nodes()) {
601 if (node->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith(
602 current_origin)) {
603 node->user_activation_state_.Activate();
604 }
605 }
606 }
607
Shivani Sharma20749e922019-03-11 17:00:26608 NavigationControllerImpl* controller =
609 static_cast<NavigationControllerImpl*>(navigator()->GetController());
610 if (controller)
611 controller->NotifyUserActivation();
Shivani Sharma194877032019-03-07 17:52:47612
Mustaq Ahmedc4cb7162018-06-05 16:28:36613 return true;
614}
615
616bool FrameTreeNode::ConsumeTransientUserActivation() {
617 bool was_active = user_activation_state_.IsActive();
618 for (FrameTreeNode* node : frame_tree()->Nodes())
619 node->user_activation_state_.ConsumeIfActive();
620 return was_active;
621}
622
Shivani Sharmac4f561582018-11-15 15:58:39623bool FrameTreeNode::ClearUserActivation() {
Shivani Sharmac4f561582018-11-15 15:58:39624 for (FrameTreeNode* node : frame_tree()->SubtreeNodes(this))
625 node->user_activation_state_.Clear();
626 return true;
627}
628
Mustaq Ahmedc4cb7162018-06-05 16:28:36629bool FrameTreeNode::UpdateUserActivationState(
630 blink::UserActivationUpdateType update_type) {
631 render_manager_.UpdateUserActivationState(update_type);
632 switch (update_type) {
633 case blink::UserActivationUpdateType::kConsumeTransientActivation:
634 return ConsumeTransientUserActivation();
635
636 case blink::UserActivationUpdateType::kNotifyActivation:
637 return NotifyUserActivation();
Shivani Sharmac4f561582018-11-15 15:58:39638
639 case blink::UserActivationUpdateType::kClearActivation:
640 return ClearUserActivation();
Mustaq Ahmedc4cb7162018-06-05 16:28:36641 }
642 NOTREACHED() << "Invalid update_type.";
japhet61835ae12017-01-20 01:25:39643}
644
Becca Hughes60af7d42017-12-12 10:53:15645void FrameTreeNode::OnSetHasReceivedUserGestureBeforeNavigation(bool value) {
646 render_manager_.OnSetHasReceivedUserGestureBeforeNavigation(value);
647 replication_state_.has_received_user_gesture_before_nav = value;
648}
649
paulmeyer322777fb2016-05-16 23:15:39650FrameTreeNode* FrameTreeNode::GetSibling(int relative_offset) const {
paulmeyerf3119f52016-05-17 17:37:19651 if (!parent_ || !parent_->child_count())
paulmeyer322777fb2016-05-16 23:15:39652 return nullptr;
653
654 for (size_t i = 0; i < parent_->child_count(); ++i) {
655 if (parent_->child_at(i) == this) {
656 if ((relative_offset < 0 && static_cast<size_t>(-relative_offset) > i) ||
657 i + relative_offset >= parent_->child_count()) {
658 return nullptr;
659 }
660 return parent_->child_at(i + relative_offset);
661 }
662 }
663
664 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
665 return nullptr;
666}
667
Ian Clellandedb8c5dd2018-03-01 17:01:37668void FrameTreeNode::UpdateFramePolicyHeaders(
669 blink::WebSandboxFlags sandbox_flags,
670 const blink::ParsedFeaturePolicy& parsed_header) {
671 bool changed = false;
672 if (replication_state_.feature_policy_header != parsed_header) {
673 replication_state_.feature_policy_header = parsed_header;
674 changed = true;
675 }
Ian Clelland5cbaaf82017-11-27 22:00:03676 // TODO(iclelland): Kill the renderer if sandbox flags is not a subset of the
677 // currently effective sandbox flags from the frame. https://crbug.com/740556
Ian Clellandedb8c5dd2018-03-01 17:01:37678 blink::WebSandboxFlags updated_flags =
Ian Clelland5cbaaf82017-11-27 22:00:03679 sandbox_flags | effective_frame_policy().sandbox_flags;
Ian Clellandedb8c5dd2018-03-01 17:01:37680 if (replication_state_.active_sandbox_flags != updated_flags) {
681 replication_state_.active_sandbox_flags = updated_flags;
682 changed = true;
683 }
684 // Notify any proxies if the policies have been changed.
685 if (changed)
686 render_manager()->OnDidSetFramePolicyHeaders();
Ian Clelland5cbaaf82017-11-27 22:00:03687}
688
Lan Weif0cb8e6e2019-04-19 00:14:53689void FrameTreeNode::TransferUserActivationFrom(
690 RenderFrameHostImpl* source_rfh) {
691 user_activation_state_.TransferFrom(
692 source_rfh->frame_tree_node()->user_activation_state_);
693
694 // Notify proxies in non-source and non-target renderer processes to
695 // transfer the activation state from the source proxy to the target
696 // so the user activation state of those proxies matches the source
697 // renderer and the target renderer (which are separately updated).
698 render_manager_.TransferUserActivationFrom(source_rfh);
Lan Wei550671a22019-03-19 00:59:24699}
700
Arthur Sonzognif8840b92018-11-07 14:10:35701void FrameTreeNode::PruneChildFrameNavigationEntries(
702 NavigationEntryImpl* entry) {
703 for (size_t i = 0; i < current_frame_host()->child_count(); ++i) {
704 FrameTreeNode* child = current_frame_host()->child_at(i);
705 if (child->is_created_by_script_) {
706 entry->RemoveEntryForFrame(child,
707 /* only_if_different_position = */ false);
708 } else {
709 child->PruneChildFrameNavigationEntries(entry);
710 }
711 }
712}
713
Ehsan Karamad39407082019-02-19 23:38:19714void FrameTreeNode::SetOpenerFeaturePolicyState(
715 const blink::FeaturePolicy::FeatureState& feature_state) {
716 DCHECK(IsMainFrame());
717 if (base::FeatureList::IsEnabled(features::kFeaturePolicyForSandbox)) {
718 replication_state_.opener_feature_state = feature_state;
719 }
720}
721
[email protected]9b159a52013-10-03 17:24:55722} // namespace content