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/stl_util.h"
11 #include "content/browser/frame_host/frame_tree.h"
12 #include "content/browser/frame_host/navigator.h"
13 #include "content/browser/frame_host/render_frame_host_impl.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/common/content_switches.h"
22 // This is a global map between frame_tree_node_ids and pointers to
24 typedef base::hash_map
<int64
, FrameTreeNode
*> FrameTreeNodeIDMap
;
26 base::LazyInstance
<FrameTreeNodeIDMap
> g_frame_tree_node_id_map
=
27 LAZY_INSTANCE_INITIALIZER
;
31 const double FrameTreeNode::kLoadingProgressNotStarted
= 0.0;
32 const double FrameTreeNode::kLoadingProgressMinimum
= 0.1;
33 const double FrameTreeNode::kLoadingProgressDone
= 1.0;
35 int64
FrameTreeNode::next_frame_tree_node_id_
= 1;
38 FrameTreeNode
* FrameTreeNode::GloballyFindByID(int64 frame_tree_node_id
) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
40 FrameTreeNodeIDMap
* nodes
= g_frame_tree_node_id_map
.Pointer();
41 FrameTreeNodeIDMap::iterator it
= nodes
->find(frame_tree_node_id
);
42 return it
== nodes
->end() ? nullptr : it
->second
;
45 FrameTreeNode::FrameTreeNode(FrameTree
* frame_tree
,
47 RenderFrameHostDelegate
* render_frame_delegate
,
48 RenderViewHostDelegate
* render_view_delegate
,
49 RenderWidgetHostDelegate
* render_widget_delegate
,
50 RenderFrameHostManager::Delegate
* manager_delegate
,
51 const std::string
& name
)
52 : frame_tree_(frame_tree
),
53 navigator_(navigator
),
55 render_frame_delegate
,
57 render_widget_delegate
,
59 frame_tree_node_id_(next_frame_tree_node_id_
++),
61 replication_state_(name
),
62 effective_sandbox_flags_(SandboxFlags::NONE
),
63 loading_progress_(kLoadingProgressNotStarted
) {
64 std::pair
<FrameTreeNodeIDMap::iterator
, bool> result
=
65 g_frame_tree_node_id_map
.Get().insert(
66 std::make_pair(frame_tree_node_id_
, this));
70 FrameTreeNode::~FrameTreeNode() {
71 frame_tree_
->FrameRemoved(this);
73 g_frame_tree_node_id_map
.Get().erase(frame_tree_node_id_
);
74 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
75 switches::kEnableBrowserSideNavigation
)) {
76 navigator_
->CancelNavigation(this);
80 bool FrameTreeNode::IsMainFrame() const {
81 return frame_tree_
->root() == this;
84 void FrameTreeNode::AddChild(scoped_ptr
<FrameTreeNode
> child
,
86 int frame_routing_id
) {
87 // Child frame must always be created in the same process as the parent.
88 CHECK_EQ(process_id
, render_manager_
.current_host()->GetProcess()->GetID());
90 // Initialize the RenderFrameHost for the new node. We always create child
91 // frames in the same SiteInstance as the current frame, and they can swap to
92 // a different one if they navigate away.
93 child
->render_manager()->Init(
94 render_manager_
.current_host()->GetSiteInstance()->GetBrowserContext(),
95 render_manager_
.current_host()->GetSiteInstance(),
96 render_manager_
.current_host()->GetRoutingID(),
98 child
->set_parent(this);
100 // Other renderer processes in this BrowsingInstance may need to find out
101 // about the new frame. Create a proxy for the child frame in all
102 // SiteInstances that have a proxy for the frame's parent, since all frames
103 // in a frame tree should have the same set of proxies.
104 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kSitePerProcess
))
106 render_manager_
.CreateProxiesForChildFrame(child
.get());
108 children_
.push_back(child
.release());
111 void FrameTreeNode::RemoveChild(FrameTreeNode
* child
) {
112 std::vector
<FrameTreeNode
*>::iterator iter
;
113 for (iter
= children_
.begin(); iter
!= children_
.end(); ++iter
) {
114 if ((*iter
) == child
)
118 if (iter
!= children_
.end()) {
119 // Subtle: we need to make sure the node is gone from the tree before
120 // observers are notified of its deletion.
121 scoped_ptr
<FrameTreeNode
> node_to_delete(*iter
);
122 children_
.weak_erase(iter
);
123 node_to_delete
.reset();
127 void FrameTreeNode::ResetForNewProcess() {
128 current_url_
= GURL();
130 // The children may not have been cleared if a cross-process navigation
131 // commits before the old process cleans everything up. Make sure the child
132 // nodes get deleted before swapping to a new process.
133 ScopedVector
<FrameTreeNode
> old_children
= children_
.Pass();
134 old_children
.clear(); // May notify observers.
137 void FrameTreeNode::SetFrameName(const std::string
& name
) {
138 replication_state_
.name
= name
;
140 // Notify this frame's proxies about the updated name.
141 render_manager_
.OnDidUpdateName(name
);
144 bool FrameTreeNode::IsDescendantOf(FrameTreeNode
* other
) const {
145 if (!other
|| !other
->child_count())
148 for (FrameTreeNode
* node
= parent(); node
; node
= node
->parent()) {
156 bool FrameTreeNode::IsLoading() const {
157 RenderFrameHostImpl
* current_frame_host
=
158 render_manager_
.current_frame_host();
159 RenderFrameHostImpl
* pending_frame_host
=
160 render_manager_
.pending_frame_host();
162 DCHECK(current_frame_host
);
163 // TODO(fdegans): Change the implementation logic for PlzNavigate once
164 // DidStartLoading and DidStopLoading are properly called.
165 if (pending_frame_host
&& pending_frame_host
->is_loading())
167 return current_frame_host
->is_loading();
170 bool FrameTreeNode::CommitPendingSandboxFlags() {
171 bool did_change_flags
=
172 effective_sandbox_flags_
!= replication_state_
.sandbox_flags
;
173 effective_sandbox_flags_
= replication_state_
.sandbox_flags
;
174 return did_change_flags
;
177 } // namespace content