1 // Copyright 2014 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/render_frame_proxy_host.h"
7 #include "base/lazy_instance.h"
8 #include "content/browser/bad_message.h"
9 #include "content/browser/frame_host/cross_process_frame_connector.h"
10 #include "content/browser/frame_host/frame_tree.h"
11 #include "content/browser/frame_host/frame_tree_node.h"
12 #include "content/browser/frame_host/render_frame_host_impl.h"
13 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_view_base.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/common/frame_messages.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "ipc/ipc_message.h"
25 // The (process id, routing id) pair that identifies one RenderFrameProxy.
26 typedef std::pair
<int32
, int32
> RenderFrameProxyHostID
;
27 typedef base::hash_map
<RenderFrameProxyHostID
, RenderFrameProxyHost
*>
28 RoutingIDFrameProxyMap
;
29 base::LazyInstance
<RoutingIDFrameProxyMap
> g_routing_id_frame_proxy_map
=
30 LAZY_INSTANCE_INITIALIZER
;
35 RenderFrameProxyHost
* RenderFrameProxyHost::FromID(int process_id
,
37 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
38 RoutingIDFrameProxyMap
* frames
= g_routing_id_frame_proxy_map
.Pointer();
39 RoutingIDFrameProxyMap::iterator it
= frames
->find(
40 RenderFrameProxyHostID(process_id
, routing_id
));
41 return it
== frames
->end() ? NULL
: it
->second
;
44 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance
* site_instance
,
45 FrameTreeNode
* frame_tree_node
)
46 : routing_id_(site_instance
->GetProcess()->GetNextRoutingID()),
47 site_instance_(site_instance
),
48 process_(site_instance
->GetProcess()),
49 frame_tree_node_(frame_tree_node
),
50 render_frame_proxy_created_(false) {
51 GetProcess()->AddRoute(routing_id_
, this);
52 CHECK(g_routing_id_frame_proxy_map
.Get().insert(
54 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
),
57 if (!frame_tree_node_
->IsMainFrame() &&
58 frame_tree_node_
->parent()
60 ->current_frame_host()
61 ->GetSiteInstance() == site_instance
) {
62 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
63 // it is created in the parent's process. CrossProcessFrameConnector
64 // initialization only needs to happen on an initial cross-process
65 // navigation, when the RenderFrameHost leaves the same process as its
66 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
67 // process navigations, but it will be destroyed if the frame is
68 // navigated back to the same SiteInstance as its parent.
69 cross_process_frame_connector_
.reset(new CrossProcessFrameConnector(this));
73 RenderFrameProxyHost::~RenderFrameProxyHost() {
74 if (GetProcess()->HasConnection()) {
75 // TODO(nasko): For now, don't send this IPC for top-level frames, as
76 // the top-level RenderFrame will delete the RenderFrameProxy.
77 // This can be removed once we don't have a swapped out state on
78 // RenderFrame. See https://crbug.com/357747
79 if (!frame_tree_node_
->IsMainFrame())
80 Send(new FrameMsg_DeleteProxy(routing_id_
));
83 GetProcess()->RemoveRoute(routing_id_
);
84 g_routing_id_frame_proxy_map
.Get().erase(
85 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
));
88 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView
* view
) {
89 cross_process_frame_connector_
->set_view(
90 static_cast<RenderWidgetHostViewChildFrame
*>(view
));
93 RenderViewHostImpl
* RenderFrameProxyHost::GetRenderViewHost() {
94 return frame_tree_node_
->frame_tree()->GetRenderViewHost(
95 site_instance_
.get());
98 void RenderFrameProxyHost::TakeFrameHostOwnership(
99 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
100 render_frame_host_
= render_frame_host
.Pass();
101 render_frame_host_
->set_render_frame_proxy_host(this);
104 scoped_ptr
<RenderFrameHostImpl
> RenderFrameProxyHost::PassFrameHostOwnership() {
105 render_frame_host_
->set_render_frame_proxy_host(NULL
);
106 return render_frame_host_
.Pass();
109 bool RenderFrameProxyHost::Send(IPC::Message
*msg
) {
110 return GetProcess()->Send(msg
);
113 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message
& msg
) {
114 if (cross_process_frame_connector_
.get() &&
115 cross_process_frame_connector_
->OnMessageReceived(msg
))
119 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost
, msg
)
120 IPC_MESSAGE_HANDLER(FrameHostMsg_Detach
, OnDetach
)
121 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL
, OnOpenURL
)
122 IPC_MESSAGE_UNHANDLED(handled
= false)
123 IPC_END_MESSAGE_MAP()
127 bool RenderFrameProxyHost::InitRenderFrameProxy() {
128 DCHECK(!render_frame_proxy_created_
);
129 // The process may (if we're sharing a process with another host that already
130 // initialized it) or may not (we have our own process or the old process
131 // crashed) have been initialized. Calling Init multiple times will be
132 // ignored, so this is safe.
133 if (!GetProcess()->Init())
136 DCHECK(GetProcess()->HasConnection());
138 int parent_routing_id
= MSG_ROUTING_NONE
;
139 if (frame_tree_node_
->parent()) {
140 parent_routing_id
= frame_tree_node_
->parent()
142 ->GetRoutingIdForSiteInstance(site_instance_
.get());
143 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
146 Send(new FrameMsg_NewFrameProxy(routing_id_
,
148 frame_tree_node_
->frame_tree()
149 ->GetRenderViewHost(site_instance_
.get())
152 ->current_replication_state()));
154 render_frame_proxy_created_
= true;
158 void RenderFrameProxyHost::DisownOpener() {
159 Send(new FrameMsg_DisownOpener(GetRoutingID()));
162 void RenderFrameProxyHost::OnDetach() {
163 // This message should only be received for subframes. Note that we can't
164 // restrict it to just the current SiteInstances of the ancestors of this
165 // frame, because another frame in the tree may be able to detach this frame
166 // by navigating its parent.
167 if (frame_tree_node_
->IsMainFrame()) {
168 bad_message::ReceivedBadMessage(GetProcess(), bad_message::RFPH_DETACH
);
171 frame_tree_node_
->frame_tree()->RemoveFrame(frame_tree_node_
);
174 void RenderFrameProxyHost::OnOpenURL(
175 const FrameHostMsg_OpenURL_Params
& params
) {
176 // TODO(creis): Verify that we are in the same BrowsingInstance as the current
177 // RenderFrameHost. See NavigatorImpl::RequestOpenURL.
178 frame_tree_node_
->current_frame_host()->OpenURL(params
, site_instance_
.get());
181 } // namespace content