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/frame_host/cross_process_frame_connector.h"
9 #include "content/browser/frame_host/frame_tree.h"
10 #include "content/browser/frame_host/frame_tree_node.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/renderer_host/render_widget_host_view_base.h"
15 #include "content/browser/site_instance_impl.h"
16 #include "content/common/frame_messages.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ipc/ipc_message.h"
24 // The (process id, routing id) pair that identifies one RenderFrameProxy.
25 typedef std::pair
<int32
, int32
> RenderFrameProxyHostID
;
26 typedef base::hash_map
<RenderFrameProxyHostID
, RenderFrameProxyHost
*>
27 RoutingIDFrameProxyMap
;
28 base::LazyInstance
<RoutingIDFrameProxyMap
> g_routing_id_frame_proxy_map
=
29 LAZY_INSTANCE_INITIALIZER
;
34 RenderFrameProxyHost
* RenderFrameProxyHost::FromID(int process_id
,
36 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
37 RoutingIDFrameProxyMap
* frames
= g_routing_id_frame_proxy_map
.Pointer();
38 RoutingIDFrameProxyMap::iterator it
= frames
->find(
39 RenderFrameProxyHostID(process_id
, routing_id
));
40 return it
== frames
->end() ? NULL
: it
->second
;
43 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance
* site_instance
,
44 FrameTreeNode
* frame_tree_node
)
45 : routing_id_(site_instance
->GetProcess()->GetNextRoutingID()),
46 site_instance_(site_instance
),
47 process_(site_instance
->GetProcess()),
48 frame_tree_node_(frame_tree_node
),
49 render_frame_proxy_created_(false) {
50 GetProcess()->AddRoute(routing_id_
, this);
51 CHECK(g_routing_id_frame_proxy_map
.Get().insert(
53 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
),
56 if (!frame_tree_node_
->IsMainFrame() &&
57 frame_tree_node_
->parent()
59 ->current_frame_host()
60 ->GetSiteInstance() == site_instance
) {
61 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
62 // it is created in the parent's process. CrossProcessFrameConnector
63 // initialization only needs to happen on an initial cross-process
64 // navigation, when the RenderFrameHost leaves the same process as its
65 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
66 // process navigations, but it will be destroyed if the frame is
67 // navigated back to the same SiteInstance as its parent.
68 cross_process_frame_connector_
.reset(new CrossProcessFrameConnector(this));
72 RenderFrameProxyHost::~RenderFrameProxyHost() {
73 if (GetProcess()->HasConnection()) {
74 // TODO(nasko): For now, don't send this IPC for top-level frames, as
75 // the top-level RenderFrame will delete the RenderFrameProxy.
76 // This can be removed once we don't have a swapped out state on
77 // RenderFrame. See https://crbug.com/357747
78 if (!frame_tree_node_
->IsMainFrame())
79 Send(new FrameMsg_DeleteProxy(routing_id_
));
82 GetProcess()->RemoveRoute(routing_id_
);
83 g_routing_id_frame_proxy_map
.Get().erase(
84 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_
));
87 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView
* view
) {
88 cross_process_frame_connector_
->set_view(
89 static_cast<RenderWidgetHostViewChildFrame
*>(view
));
92 RenderViewHostImpl
* RenderFrameProxyHost::GetRenderViewHost() {
93 return frame_tree_node_
->frame_tree()->GetRenderViewHost(
94 site_instance_
.get());
97 void RenderFrameProxyHost::TakeFrameHostOwnership(
98 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
99 render_frame_host_
= render_frame_host
.Pass();
100 render_frame_host_
->set_render_frame_proxy_host(this);
103 scoped_ptr
<RenderFrameHostImpl
> RenderFrameProxyHost::PassFrameHostOwnership() {
104 render_frame_host_
->set_render_frame_proxy_host(NULL
);
105 return render_frame_host_
.Pass();
108 bool RenderFrameProxyHost::Send(IPC::Message
*msg
) {
109 return GetProcess()->Send(msg
);
112 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message
& msg
) {
113 if (cross_process_frame_connector_
.get() &&
114 cross_process_frame_connector_
->OnMessageReceived(msg
))
118 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost
, msg
)
119 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL
, OnOpenURL
)
120 IPC_MESSAGE_UNHANDLED(handled
= false)
121 IPC_END_MESSAGE_MAP()
125 bool RenderFrameProxyHost::InitRenderFrameProxy() {
126 DCHECK(!render_frame_proxy_created_
);
127 // The process may (if we're sharing a process with another host that already
128 // initialized it) or may not (we have our own process or the old process
129 // crashed) have been initialized. Calling Init multiple times will be
130 // ignored, so this is safe.
131 if (!GetProcess()->Init())
134 DCHECK(GetProcess()->HasConnection());
136 int parent_routing_id
= MSG_ROUTING_NONE
;
137 if (frame_tree_node_
->parent()) {
138 parent_routing_id
= frame_tree_node_
->parent()
140 ->GetRoutingIdForSiteInstance(site_instance_
.get());
141 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
144 Send(new FrameMsg_NewFrameProxy(routing_id_
,
146 frame_tree_node_
->frame_tree()
147 ->GetRenderViewHost(site_instance_
.get())
150 ->current_replication_state()));
152 render_frame_proxy_created_
= true;
156 void RenderFrameProxyHost::DisownOpener() {
157 Send(new FrameMsg_DisownOpener(GetRoutingID()));
160 void RenderFrameProxyHost::OnOpenURL(
161 const FrameHostMsg_OpenURL_Params
& params
) {
162 frame_tree_node_
->current_frame_host()->OpenURL(params
, site_instance_
.get());
165 } // namespace content