1 // 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.
5 #include "content/browser/frame_host/frame_tree_node.h"
9 #include "base/command_line.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/stl_util.h"
12 #include "content/browser/frame_host/frame_tree.h"
13 #include "content/browser/frame_host/navigation_request.h"
14 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/render_frame_host_impl.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/common/frame_messages.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/common/content_switches.h"
25 // This is a global map between frame_tree_node_ids and pointers to
27 typedef base::hash_map
<int, FrameTreeNode
*> FrameTreeNodeIDMap
;
29 base::LazyInstance
<FrameTreeNodeIDMap
> g_frame_tree_node_id_map
=
30 LAZY_INSTANCE_INITIALIZER
;
32 // These values indicate the loading progress status. The minimum progress
33 // value matches what Blink's ProgressTracker has traditionally used for a
34 // minimum progress value.
35 const double kLoadingProgressNotStarted
= 0.0;
36 const double kLoadingProgressMinimum
= 0.1;
37 const double kLoadingProgressDone
= 1.0;
41 // This observer watches the opener of its owner FrameTreeNode and clears the
42 // owner's opener if the opener is destroyed.
43 class FrameTreeNode::OpenerDestroyedObserver
: public FrameTreeNode::Observer
{
45 OpenerDestroyedObserver(FrameTreeNode
* owner
) : owner_(owner
) {}
47 // FrameTreeNode::Observer
48 void OnFrameTreeNodeDestroyed(FrameTreeNode
* node
) override
{
49 CHECK_EQ(owner_
->opener(), node
);
50 owner_
->SetOpener(nullptr);
54 FrameTreeNode
* owner_
;
56 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver
);
59 int FrameTreeNode::next_frame_tree_node_id_
= 1;
62 FrameTreeNode
* FrameTreeNode::GloballyFindByID(int frame_tree_node_id
) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
64 FrameTreeNodeIDMap
* nodes
= g_frame_tree_node_id_map
.Pointer();
65 FrameTreeNodeIDMap::iterator it
= nodes
->find(frame_tree_node_id
);
66 return it
== nodes
->end() ? nullptr : it
->second
;
69 FrameTreeNode::FrameTreeNode(FrameTree
* frame_tree
,
71 RenderFrameHostDelegate
* render_frame_delegate
,
72 RenderViewHostDelegate
* render_view_delegate
,
73 RenderWidgetHostDelegate
* render_widget_delegate
,
74 RenderFrameHostManager::Delegate
* manager_delegate
,
75 blink::WebTreeScopeType scope
,
76 const std::string
& name
,
77 blink::WebSandboxFlags sandbox_flags
)
78 : frame_tree_(frame_tree
),
79 navigator_(navigator
),
81 render_frame_delegate
,
83 render_widget_delegate
,
85 frame_tree_node_id_(next_frame_tree_node_id_
++),
88 opener_observer_(nullptr),
89 has_committed_real_load_(false),
90 replication_state_(scope
, name
, sandbox_flags
),
91 // Effective sandbox flags also need to be set, since initial sandbox
92 // flags should apply to the initial empty document in the frame.
93 effective_sandbox_flags_(sandbox_flags
),
94 loading_progress_(kLoadingProgressNotStarted
) {
95 std::pair
<FrameTreeNodeIDMap::iterator
, bool> result
=
96 g_frame_tree_node_id_map
.Get().insert(
97 std::make_pair(frame_tree_node_id_
, this));
101 FrameTreeNode::~FrameTreeNode() {
102 frame_tree_
->FrameRemoved(this);
103 FOR_EACH_OBSERVER(Observer
, observers_
, OnFrameTreeNodeDestroyed(this));
106 opener_
->RemoveObserver(opener_observer_
.get());
108 g_frame_tree_node_id_map
.Get().erase(frame_tree_node_id_
);
111 void FrameTreeNode::AddObserver(Observer
* observer
) {
112 observers_
.AddObserver(observer
);
115 void FrameTreeNode::RemoveObserver(Observer
* observer
) {
116 observers_
.RemoveObserver(observer
);
119 bool FrameTreeNode::IsMainFrame() const {
120 return frame_tree_
->root() == this;
123 void FrameTreeNode::AddChild(scoped_ptr
<FrameTreeNode
> child
,
125 int frame_routing_id
) {
126 // Child frame must always be created in the same process as the parent.
127 CHECK_EQ(process_id
, render_manager_
.current_host()->GetProcess()->GetID());
129 // Initialize the RenderFrameHost for the new node. We always create child
130 // frames in the same SiteInstance as the current frame, and they can swap to
131 // a different one if they navigate away.
132 child
->render_manager()->Init(
133 render_manager_
.current_host()->GetSiteInstance()->GetBrowserContext(),
134 render_manager_
.current_host()->GetSiteInstance(),
135 render_manager_
.current_host()->GetRoutingID(),
137 child
->set_parent(this);
139 // Other renderer processes in this BrowsingInstance may need to find out
140 // about the new frame. Create a proxy for the child frame in all
141 // SiteInstances that have a proxy for the frame's parent, since all frames
142 // in a frame tree should have the same set of proxies.
143 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kSitePerProcess
))
145 render_manager_
.CreateProxiesForChildFrame(child
.get());
147 children_
.push_back(child
.release());
150 void FrameTreeNode::RemoveChild(FrameTreeNode
* child
) {
151 std::vector
<FrameTreeNode
*>::iterator iter
;
152 for (iter
= children_
.begin(); iter
!= children_
.end(); ++iter
) {
153 if ((*iter
) == child
)
157 if (iter
!= children_
.end()) {
158 // Subtle: we need to make sure the node is gone from the tree before
159 // observers are notified of its deletion.
160 scoped_ptr
<FrameTreeNode
> node_to_delete(*iter
);
161 children_
.weak_erase(iter
);
162 node_to_delete
.reset();
166 void FrameTreeNode::ResetForNewProcess() {
167 current_url_
= GURL();
169 // The children may not have been cleared if a cross-process navigation
170 // commits before the old process cleans everything up. Make sure the child
171 // nodes get deleted before swapping to a new process.
172 ScopedVector
<FrameTreeNode
> old_children
= children_
.Pass();
173 old_children
.clear(); // May notify observers.
176 void FrameTreeNode::SetOpener(FrameTreeNode
* opener
) {
178 opener_
->RemoveObserver(opener_observer_
.get());
179 opener_observer_
.reset();
185 if (!opener_observer_
)
186 opener_observer_
= make_scoped_ptr(new OpenerDestroyedObserver(this));
187 opener_
->AddObserver(opener_observer_
.get());
191 void FrameTreeNode::SetCurrentURL(const GURL
& url
) {
192 if (!has_committed_real_load_
&& url
!= GURL(url::kAboutBlankURL
))
193 has_committed_real_load_
= true;
197 void FrameTreeNode::SetCurrentOrigin(const url::Origin
& origin
) {
198 if (!origin
.IsSameOriginWith(replication_state_
.origin
))
199 render_manager_
.OnDidUpdateOrigin(origin
);
200 replication_state_
.origin
= origin
;
203 void FrameTreeNode::SetFrameName(const std::string
& name
) {
204 if (name
!= replication_state_
.name
)
205 render_manager_
.OnDidUpdateName(name
);
206 replication_state_
.name
= name
;
209 bool FrameTreeNode::IsDescendantOf(FrameTreeNode
* other
) const {
210 if (!other
|| !other
->child_count())
213 for (FrameTreeNode
* node
= parent(); node
; node
= node
->parent()) {
221 FrameTreeNode
* FrameTreeNode::PreviousSibling() const {
225 for (size_t i
= 0; i
< parent_
->child_count(); ++i
) {
226 if (parent_
->child_at(i
) == this)
227 return (i
== 0) ? nullptr : parent_
->child_at(i
- 1);
230 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
234 bool FrameTreeNode::IsLoading() const {
235 RenderFrameHostImpl
* current_frame_host
=
236 render_manager_
.current_frame_host();
237 RenderFrameHostImpl
* pending_frame_host
=
238 render_manager_
.pending_frame_host();
240 DCHECK(current_frame_host
);
242 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
243 switches::kEnableBrowserSideNavigation
)) {
244 if (navigation_request_
)
247 RenderFrameHostImpl
* speculative_frame_host
=
248 render_manager_
.speculative_frame_host();
249 if (speculative_frame_host
&& speculative_frame_host
->is_loading())
252 if (pending_frame_host
&& pending_frame_host
->is_loading())
255 return current_frame_host
->is_loading();
258 bool FrameTreeNode::CommitPendingSandboxFlags() {
259 bool did_change_flags
=
260 effective_sandbox_flags_
!= replication_state_
.sandbox_flags
;
261 effective_sandbox_flags_
= replication_state_
.sandbox_flags
;
262 return did_change_flags
;
265 void FrameTreeNode::CreatedNavigationRequest(
266 scoped_ptr
<NavigationRequest
> navigation_request
) {
267 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
268 switches::kEnableBrowserSideNavigation
));
269 ResetNavigationRequest(false);
271 // Force the throbber to start to keep it in sync with what is happening in
272 // the UI. Blink doesn't send throb notifications for JavaScript URLs, so it
273 // is not done here either.
274 if (!navigation_request
->common_params().url
.SchemeIs(
275 url::kJavaScriptScheme
)) {
276 // TODO(fdegans): Check if this is a same-document navigation and set the
278 DidStartLoading(true);
281 navigation_request_
= navigation_request
.Pass();
283 render_manager()->DidCreateNavigationRequest(*navigation_request_
);
286 void FrameTreeNode::ResetNavigationRequest(bool is_commit
) {
287 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
288 switches::kEnableBrowserSideNavigation
));
289 if (!navigation_request_
)
291 navigation_request_
.reset();
293 // During commit, the clean up of a speculative RenderFrameHost is done in
294 // RenderFrameHostManager::DidNavigateFrame. The load is also still being
299 // If the reset corresponds to a cancelation, the RenderFrameHostManager
300 // should clean up any speculative RenderFrameHost it created for the
303 render_manager_
.CleanUpNavigation();
306 bool FrameTreeNode::has_started_loading() const {
307 return loading_progress_
!= kLoadingProgressNotStarted
;
310 void FrameTreeNode::reset_loading_progress() {
311 loading_progress_
= kLoadingProgressNotStarted
;
314 void FrameTreeNode::DidStartLoading(bool to_different_document
) {
315 // Any main frame load to a new document should reset the load progress since
316 // it will replace the current page and any frames. The WebContents will
317 // be notified when DidChangeLoadProgress is called.
318 if (to_different_document
&& IsMainFrame())
319 frame_tree_
->ResetLoadProgress();
321 // Notify the WebContents.
322 if (!frame_tree_
->IsLoading())
323 navigator()->GetDelegate()->DidStartLoading(this, to_different_document
);
325 // Set initial load progress and update overall progress. This will notify
326 // the WebContents of the load progress change.
327 DidChangeLoadProgress(kLoadingProgressMinimum
);
329 // Notify the RenderFrameHostManager of the event.
330 render_manager()->OnDidStartLoading();
333 void FrameTreeNode::DidStopLoading() {
334 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
335 tracked_objects::ScopedTracker
tracking_profile1(
336 FROM_HERE_WITH_EXPLICIT_FUNCTION(
337 "465796 FrameTreeNode::DidStopLoading::Start"));
339 // Set final load progress and update overall progress. This will notify
340 // the WebContents of the load progress change.
341 DidChangeLoadProgress(kLoadingProgressDone
);
343 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
344 tracked_objects::ScopedTracker
tracking_profile2(
345 FROM_HERE_WITH_EXPLICIT_FUNCTION(
346 "465796 FrameTreeNode::DidStopLoading::WCIDidStopLoading"));
348 // Notify the WebContents.
349 if (!frame_tree_
->IsLoading())
350 navigator()->GetDelegate()->DidStopLoading();
352 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
353 tracked_objects::ScopedTracker
tracking_profile3(
354 FROM_HERE_WITH_EXPLICIT_FUNCTION(
355 "465796 FrameTreeNode::DidStopLoading::RFHMDidStopLoading"));
357 // Notify the RenderFrameHostManager of the event.
358 render_manager()->OnDidStopLoading();
360 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
361 tracked_objects::ScopedTracker
tracking_profile4(
362 FROM_HERE_WITH_EXPLICIT_FUNCTION(
363 "465796 FrameTreeNode::DidStopLoading::End"));
366 void FrameTreeNode::DidChangeLoadProgress(double load_progress
) {
367 loading_progress_
= load_progress
;
368 frame_tree_
->UpdateLoadProgress();
371 bool FrameTreeNode::StopLoading() {
372 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
373 switches::kEnableBrowserSideNavigation
)) {
374 ResetNavigationRequest(false);
377 // TODO(nasko): see if child frames should send IPCs in site-per-process
382 render_manager_
.Stop();
386 } // namespace content