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_delegate.h"
13 #include "content/browser/frame_host/render_frame_host_impl.h"
14 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
15 #include "content/browser/message_port_message_filter.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_view_base.h"
18 #include "content/browser/site_instance_impl.h"
19 #include "content/common/frame_messages.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "ipc/ipc_message.h"
27 // The (process id, routing id) pair that identifies one RenderFrameProxy.
28 typedef std::pair
<int32
, int32
> RenderFrameProxyHostID
;
29 typedef base::hash_map
<RenderFrameProxyHostID
, RenderFrameProxyHost
*>
30 RoutingIDFrameProxyMap
;
31 base::LazyInstance
<RoutingIDFrameProxyMap
> g_routing_id_frame_proxy_map
=
32 LAZY_INSTANCE_INITIALIZER
;
37 RenderFrameProxyHost
* RenderFrameProxyHost::FromID(int process_id
,
39 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
40 RoutingIDFrameProxyMap
* frames
= g_routing_id_frame_proxy_map
.Pointer();
41 RoutingIDFrameProxyMap::iterator it
= frames
->find(
42 RenderFrameProxyHostID(process_id
, routing_id
));
43 return it
== frames
->end() ? NULL
: it
->second
;
46 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance
* site_instance
,
47 RenderViewHostImpl
* render_view_host
,
48 FrameTreeNode
* frame_tree_node
)
49 : routing_id_(site_instance
->GetProcess()->GetNextRoutingID()),
50 site_instance_(site_instance
),
51 process_(site_instance
->GetProcess()),
52 frame_tree_node_(frame_tree_node
),
53 render_frame_proxy_created_(false),
54 render_view_host_(render_view_host
) {
55 GetProcess()->AddRoute(routing_id_
, this);
56 CHECK(g_routing_id_frame_proxy_map
.Get().insert(
58 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
),
60 CHECK(render_view_host_
);
61 frame_tree_node_
->frame_tree()->AddRenderViewHostRef(render_view_host_
);
63 if (!frame_tree_node_
->IsMainFrame() &&
64 frame_tree_node_
->parent()
66 ->current_frame_host()
67 ->GetSiteInstance() == site_instance
) {
68 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
69 // it is created in the parent's process. CrossProcessFrameConnector
70 // initialization only needs to happen on an initial cross-process
71 // navigation, when the RenderFrameHost leaves the same process as its
72 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
73 // process navigations, but it will be destroyed if the frame is
74 // navigated back to the same SiteInstance as its parent.
75 cross_process_frame_connector_
.reset(new CrossProcessFrameConnector(this));
79 RenderFrameProxyHost::~RenderFrameProxyHost() {
80 if (GetProcess()->HasConnection()) {
81 // TODO(nasko): For now, don't send this IPC for top-level frames, as
82 // the top-level RenderFrame will delete the RenderFrameProxy.
83 // This can be removed once we don't have a swapped out state on
84 // RenderFrame. See https://crbug.com/357747
85 if (!frame_tree_node_
->IsMainFrame())
86 Send(new FrameMsg_DeleteProxy(routing_id_
));
89 frame_tree_node_
->frame_tree()->ReleaseRenderViewHostRef(render_view_host_
);
90 GetProcess()->RemoveRoute(routing_id_
);
91 g_routing_id_frame_proxy_map
.Get().erase(
92 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
));
95 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView
* view
) {
96 cross_process_frame_connector_
->set_view(
97 static_cast<RenderWidgetHostViewChildFrame
*>(view
));
100 RenderViewHostImpl
* RenderFrameProxyHost::GetRenderViewHost() {
101 return frame_tree_node_
->frame_tree()->GetRenderViewHost(
102 site_instance_
.get());
105 RenderWidgetHostView
* RenderFrameProxyHost::GetRenderWidgetHostView() {
106 return frame_tree_node_
->parent()->render_manager()
107 ->GetRenderWidgetHostView();
110 void RenderFrameProxyHost::TakeFrameHostOwnership(
111 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
112 CHECK(render_frame_host_
== nullptr);
113 render_frame_host_
= render_frame_host
.Pass();
114 render_frame_host_
->set_render_frame_proxy_host(this);
117 scoped_ptr
<RenderFrameHostImpl
> RenderFrameProxyHost::PassFrameHostOwnership() {
118 render_frame_host_
->set_render_frame_proxy_host(NULL
);
119 return render_frame_host_
.Pass();
122 bool RenderFrameProxyHost::Send(IPC::Message
*msg
) {
123 return GetProcess()->Send(msg
);
126 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message
& msg
) {
127 if (cross_process_frame_connector_
.get() &&
128 cross_process_frame_connector_
->OnMessageReceived(msg
))
132 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost
, msg
)
133 IPC_MESSAGE_HANDLER(FrameHostMsg_Detach
, OnDetach
)
134 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL
, OnOpenURL
)
135 IPC_MESSAGE_HANDLER(FrameHostMsg_RouteMessageEvent
, OnRouteMessageEvent
)
136 IPC_MESSAGE_UNHANDLED(handled
= false)
137 IPC_END_MESSAGE_MAP()
141 bool RenderFrameProxyHost::InitRenderFrameProxy() {
142 DCHECK(!render_frame_proxy_created_
);
144 // It is possible to reach this when the process is dead (in particular, when
145 // creating proxies from CreateProxiesForChildFrame). In that case, don't
146 // create the proxy. The process shouldn't be resurrected just to create
147 // RenderFrameProxies; it should be restored only if it needs to host a
148 // RenderFrame. When that happens, the process will be reinitialized, and
149 // all necessary proxies, including any of the ones we skipped here, will be
150 // created by CreateProxiesForSiteInstance. See https://crbug.com/476846
151 if (!GetProcess()->HasConnection())
154 int parent_routing_id
= MSG_ROUTING_NONE
;
155 if (frame_tree_node_
->parent()) {
156 // It is safe to use GetRenderFrameProxyHost to get the parent proxy, since
157 // new child frames always start out as local frames, so a new proxy should
158 // never have a RenderFrameHost as a parent.
159 RenderFrameProxyHost
* parent_proxy
=
160 frame_tree_node_
->parent()->render_manager()->GetRenderFrameProxyHost(
161 site_instance_
.get());
163 // When this is called, the parent RenderFrameProxy should already exist.
164 // The FrameNew_NewFrameProxy will crash on the renderer side if there's no
166 CHECK(parent_proxy
->is_render_frame_proxy_live());
167 parent_routing_id
= parent_proxy
->GetRoutingID();
168 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
171 Send(new FrameMsg_NewFrameProxy(routing_id_
,
173 frame_tree_node_
->frame_tree()
174 ->GetRenderViewHost(site_instance_
.get())
177 ->current_replication_state()));
179 render_frame_proxy_created_
= true;
183 void RenderFrameProxyHost::DisownOpener() {
184 Send(new FrameMsg_DisownOpener(GetRoutingID()));
187 void RenderFrameProxyHost::OnDetach() {
188 // This message should only be received for subframes. Note that we can't
189 // restrict it to just the current SiteInstances of the ancestors of this
190 // frame, because another frame in the tree may be able to detach this frame
191 // by navigating its parent.
192 if (frame_tree_node_
->IsMainFrame()) {
193 bad_message::ReceivedBadMessage(GetProcess(), bad_message::RFPH_DETACH
);
196 frame_tree_node_
->frame_tree()->RemoveFrame(frame_tree_node_
);
199 void RenderFrameProxyHost::OnOpenURL(
200 const FrameHostMsg_OpenURL_Params
& params
) {
201 // TODO(creis): Verify that we are in the same BrowsingInstance as the current
202 // RenderFrameHost. See NavigatorImpl::RequestOpenURL.
203 frame_tree_node_
->current_frame_host()->OpenURL(params
, site_instance_
.get());
206 void RenderFrameProxyHost::OnRouteMessageEvent(
207 const FrameMsg_PostMessage_Params
& params
) {
208 RenderFrameHostImpl
* target_rfh
= frame_tree_node()->current_frame_host();
210 // Only deliver the message if the request came from a RenderFrameHost in the
211 // same BrowsingInstance or if this WebContents is dedicated to a browser
214 // TODO(alexmos, lazyboy): The check for browser plugin guest currently
215 // requires going through the delegate. It should be refactored and
216 // performed here once OOPIF support in <webview> is further along.
217 SiteInstance
* target_site_instance
= target_rfh
->GetSiteInstance();
218 if (!target_site_instance
->IsRelatedSiteInstance(GetSiteInstance()) &&
219 !target_rfh
->delegate()->ShouldRouteMessageEvent(target_rfh
,
223 FrameMsg_PostMessage_Params
new_params(params
);
225 // If there is a source_routing_id, translate it to the routing ID of the
226 // equivalent RenderFrameProxyHost in the target process.
227 if (new_params
.source_routing_id
!= MSG_ROUTING_NONE
) {
228 RenderFrameHostImpl
* source_rfh
= RenderFrameHostImpl::FromID(
229 GetProcess()->GetID(), new_params
.source_routing_id
);
231 new_params
.source_routing_id
= MSG_ROUTING_NONE
;
233 // Ensure that we have a swapped-out RVH and proxy for the source frame.
234 // If it doesn't exist, create it on demand and also create its opener
235 // chain, since those will also be accessible to the target page.
237 // TODO(alexmos): This currently only works for top-level frames and
238 // won't create the right proxy if the message source is a subframe on a
239 // cross-process tab. This will be cleaned up as part of moving opener
240 // tracking to FrameTreeNode (https://crbug.com/225940). For now, if the
241 // message is sent from a subframe on a cross-process tab, set the source
242 // routing ID to the main frame of the source tab, which matches legacy
243 // postMessage behavior prior to --site-per-process.
244 int source_view_routing_id
=
245 target_rfh
->delegate()->EnsureOpenerRenderViewsExist(source_rfh
);
247 RenderFrameProxyHost
* source_proxy_in_target_site_instance
=
248 source_rfh
->frame_tree_node()
250 ->GetRenderFrameProxyHost(target_rfh
->GetSiteInstance());
251 if (source_proxy_in_target_site_instance
) {
252 new_params
.source_routing_id
=
253 source_proxy_in_target_site_instance
->GetRoutingID();
254 } else if (source_view_routing_id
!= MSG_ROUTING_NONE
) {
255 RenderViewHostImpl
* source_rvh
= RenderViewHostImpl::FromID(
256 target_rfh
->GetProcess()->GetID(), source_view_routing_id
);
258 new_params
.source_routing_id
= source_rvh
->main_frame_routing_id();
260 new_params
.source_routing_id
= MSG_ROUTING_NONE
;
265 if (!params
.message_ports
.empty()) {
266 // Updating the message port information has to be done in the IO thread;
267 // MessagePortMessageFilter::RouteMessageEventWithMessagePorts will send
268 // FrameMsg_PostMessageEvent after it's done. Note that a trivial solution
269 // would've been to post a task on the IO thread to do the IO-thread-bound
270 // work, and make that post a task back to WebContentsImpl in the UI
271 // thread. But we cannot do that, since there's nothing to guarantee that
272 // WebContentsImpl stays alive during the round trip.
273 scoped_refptr
<MessagePortMessageFilter
> message_port_message_filter(
274 static_cast<RenderProcessHostImpl
*>(target_rfh
->GetProcess())
275 ->message_port_message_filter());
276 BrowserThread::PostTask(
277 BrowserThread::IO
, FROM_HERE
,
278 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts
,
279 message_port_message_filter
, target_rfh
->GetRoutingID(),
283 new FrameMsg_PostMessageEvent(target_rfh
->GetRoutingID(), new_params
));
287 } // namespace content