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 "content/browser/frame_host/cross_process_frame_connector.h"
8 #include "content/browser/frame_host/frame_tree.h"
9 #include "content/browser/frame_host/frame_tree_node.h"
10 #include "content/browser/frame_host/render_frame_host_impl.h"
11 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/renderer_host/render_widget_host_view_base.h"
14 #include "content/browser/site_instance_impl.h"
15 #include "content/common/frame_messages.h"
16 #include "ipc/ipc_message.h"
20 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance
* site_instance
,
21 FrameTreeNode
* frame_tree_node
)
22 : routing_id_(site_instance
->GetProcess()->GetNextRoutingID()),
23 site_instance_(site_instance
),
24 frame_tree_node_(frame_tree_node
) {
25 GetProcess()->AddRoute(routing_id_
, this);
27 if (!frame_tree_node_
->IsMainFrame() &&
28 frame_tree_node_
->parent()
30 ->current_frame_host()
31 ->GetSiteInstance() == site_instance
) {
32 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
33 // it is created in the parent's process. CrossProcessFrameConnector
34 // initialization only needs to happen on an initial cross-process
35 // navigation, when the RenderFrameHost leaves the same process as its
36 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
37 // process navigations, but it will be destroyed if the frame is
38 // navigated back to the same SiteInstance as its parent.
39 cross_process_frame_connector_
.reset(new CrossProcessFrameConnector(this));
43 RenderFrameProxyHost::~RenderFrameProxyHost() {
44 if (GetProcess()->HasConnection())
45 Send(new FrameMsg_DeleteProxy(routing_id_
));
47 GetProcess()->RemoveRoute(routing_id_
);
50 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView
* view
) {
51 cross_process_frame_connector_
->set_view(
52 static_cast<RenderWidgetHostViewChildFrame
*>(view
));
55 RenderViewHostImpl
* RenderFrameProxyHost::GetRenderViewHost() {
56 return frame_tree_node_
->frame_tree()->GetRenderViewHost(
57 site_instance_
.get());
60 scoped_ptr
<RenderFrameHostImpl
> RenderFrameProxyHost::PassFrameHostOwnership() {
61 render_frame_host_
->set_render_frame_proxy_host(NULL
);
62 return render_frame_host_
.Pass();
65 bool RenderFrameProxyHost::Send(IPC::Message
*msg
) {
66 // TODO(nasko): For now, RenderFrameHost uses this object to send IPC messages
67 // while swapped out. This can be removed once we don't have a swapped out
68 // state on RenderFrameHosts. See https://crbug.com/357747.
69 msg
->set_routing_id(routing_id_
);
70 return GetProcess()->Send(msg
);
73 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message
& msg
) {
74 if (cross_process_frame_connector_
.get() &&
75 cross_process_frame_connector_
->OnMessageReceived(msg
))
78 // TODO(nasko): This can be removed once we don't have a swapped out state on
79 // RenderFrameHosts. See https://crbug.com/357747.
80 if (render_frame_host_
.get())
81 return render_frame_host_
->OnMessageReceived(msg
);
86 bool RenderFrameProxyHost::InitRenderFrameProxy() {
87 // The process may (if we're sharing a process with another host that already
88 // initialized it) or may not (we have our own process or the old process
89 // crashed) have been initialized. Calling Init multiple times will be
90 // ignored, so this is safe.
91 if (!site_instance_
->GetProcess()->Init())
94 DCHECK(GetProcess()->HasConnection());
96 int parent_routing_id
= MSG_ROUTING_NONE
;
97 if (frame_tree_node_
->parent()) {
98 parent_routing_id
= frame_tree_node_
->parent()
100 ->GetRoutingIdForSiteInstance(site_instance_
.get());
101 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
104 Send(new FrameMsg_NewFrameProxy(routing_id_
,
106 frame_tree_node_
->frame_tree()
107 ->GetRenderViewHost(site_instance_
.get())
113 void RenderFrameProxyHost::DisownOpener() {
114 Send(new FrameMsg_DisownOpener(GetRoutingID()));
118 } // namespace content