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/renderer/render_frame_proxy.h"
9 #include "base/lazy_instance.h"
10 #include "content/child/webmessageportchannel_impl.h"
11 #include "content/common/frame_messages.h"
12 #include "content/common/frame_replication_state.h"
13 #include "content/common/swapped_out_messages.h"
14 #include "content/common/view_messages.h"
15 #include "content/renderer/child_frame_compositing_helper.h"
16 #include "content/renderer/render_frame_impl.h"
17 #include "content/renderer/render_thread_impl.h"
18 #include "content/renderer/render_view_impl.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
22 #include "third_party/WebKit/public/web/WebView.h"
28 // Facilitates lookup of RenderFrameProxy by routing_id.
29 typedef std::map
<int, RenderFrameProxy
*> RoutingIDProxyMap
;
30 static base::LazyInstance
<RoutingIDProxyMap
> g_routing_id_proxy_map
=
31 LAZY_INSTANCE_INITIALIZER
;
33 // Facilitates lookup of RenderFrameProxy by WebFrame.
34 typedef std::map
<blink::WebFrame
*, RenderFrameProxy
*> FrameMap
;
35 base::LazyInstance
<FrameMap
> g_frame_map
= LAZY_INSTANCE_INITIALIZER
;
40 RenderFrameProxy
* RenderFrameProxy::CreateProxyToReplaceFrame(
41 RenderFrameImpl
* frame_to_replace
,
43 CHECK_NE(routing_id
, MSG_ROUTING_NONE
);
45 scoped_ptr
<RenderFrameProxy
> proxy(
46 new RenderFrameProxy(routing_id
, frame_to_replace
->GetRoutingID()));
48 // When a RenderFrame is replaced by a RenderProxy, the WebRemoteFrame should
49 // always come from WebRemoteFrame::create and a call to WebFrame::swap must
51 blink::WebRemoteFrame
* web_frame
= blink::WebRemoteFrame::create(proxy
.get());
52 proxy
->Init(web_frame
, frame_to_replace
->render_view());
53 return proxy
.release();
56 RenderFrameProxy
* RenderFrameProxy::CreateFrameProxy(
58 int parent_routing_id
,
59 int render_view_routing_id
,
60 const FrameReplicationState
& replicated_state
) {
61 scoped_ptr
<RenderFrameProxy
> proxy(
62 new RenderFrameProxy(routing_id
, MSG_ROUTING_NONE
));
63 RenderViewImpl
* render_view
= NULL
;
64 blink::WebRemoteFrame
* web_frame
= NULL
;
65 if (parent_routing_id
== MSG_ROUTING_NONE
) {
66 // Create a top level frame.
67 render_view
= RenderViewImpl::FromRoutingID(render_view_routing_id
);
68 web_frame
= blink::WebRemoteFrame::create(proxy
.get());
69 render_view
->webview()->setMainFrame(web_frame
);
71 // Create a frame under an existing parent. The parent is always expected
72 // to be a RenderFrameProxy, because navigations initiated by local frames
73 // should not wind up here.
74 RenderFrameProxy
* parent
=
75 RenderFrameProxy::FromRoutingID(parent_routing_id
);
76 web_frame
= parent
->web_frame()->createRemoteChild(
77 blink::WebString::fromUTF8(replicated_state
.name
), proxy
.get());
78 render_view
= parent
->render_view();
81 proxy
->Init(web_frame
, render_view
);
83 // Initialize proxy's WebRemoteFrame with the security origin and other
84 // replicated information.
85 proxy
->SetReplicatedState(replicated_state
);
87 return proxy
.release();
91 RenderFrameProxy
* RenderFrameProxy::FromRoutingID(int32 routing_id
) {
92 RoutingIDProxyMap
* proxies
= g_routing_id_proxy_map
.Pointer();
93 RoutingIDProxyMap::iterator it
= proxies
->find(routing_id
);
94 return it
== proxies
->end() ? NULL
: it
->second
;
98 RenderFrameProxy
* RenderFrameProxy::FromWebFrame(blink::WebFrame
* web_frame
) {
99 FrameMap::iterator iter
= g_frame_map
.Get().find(web_frame
);
100 if (iter
!= g_frame_map
.Get().end()) {
101 RenderFrameProxy
* proxy
= iter
->second
;
102 DCHECK_EQ(web_frame
, proxy
->web_frame());
108 RenderFrameProxy::RenderFrameProxy(int routing_id
, int frame_routing_id
)
109 : routing_id_(routing_id
),
110 frame_routing_id_(frame_routing_id
),
113 std::pair
<RoutingIDProxyMap::iterator
, bool> result
=
114 g_routing_id_proxy_map
.Get().insert(std::make_pair(routing_id_
, this));
115 CHECK(result
.second
) << "Inserting a duplicate item.";
116 RenderThread::Get()->AddRoute(routing_id_
, this);
119 RenderFrameProxy::~RenderFrameProxy() {
120 render_view()->UnregisterRenderFrameProxy(this);
122 FrameMap::iterator it
= g_frame_map
.Get().find(web_frame_
);
123 CHECK(it
!= g_frame_map
.Get().end());
124 CHECK_EQ(it
->second
, this);
125 g_frame_map
.Get().erase(it
);
127 RenderThread::Get()->RemoveRoute(routing_id_
);
128 g_routing_id_proxy_map
.Get().erase(routing_id_
);
131 void RenderFrameProxy::Init(blink::WebRemoteFrame
* web_frame
,
132 RenderViewImpl
* render_view
) {
136 web_frame_
= web_frame
;
137 render_view_
= render_view
;
139 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
140 render_view_
->RegisterRenderFrameProxy(this);
142 std::pair
<FrameMap::iterator
, bool> result
=
143 g_frame_map
.Get().insert(std::make_pair(web_frame_
, this));
144 CHECK(result
.second
) << "Inserted a duplicate item.";
147 bool RenderFrameProxy::IsMainFrameDetachedFromTree() const {
148 return web_frame_
->top() == web_frame_
&&
149 render_view_
->webview()->mainFrame()->isWebLocalFrame();
152 void RenderFrameProxy::DidCommitCompositorFrame() {
153 if (compositing_helper_
.get())
154 compositing_helper_
->DidCommitCompositorFrame();
157 void RenderFrameProxy::SetReplicatedState(const FrameReplicationState
& state
) {
159 web_frame_
->setReplicatedOrigin(blink::WebSecurityOrigin::createFromString(
160 blink::WebString::fromUTF8(state
.origin
.string())));
161 web_frame_
->setReplicatedSandboxFlags(
162 RenderFrameImpl::ContentToWebSandboxFlags(state
.sandbox_flags
));
163 web_frame_
->setReplicatedName(blink::WebString::fromUTF8(state
.name
));
166 bool RenderFrameProxy::OnMessageReceived(const IPC::Message
& msg
) {
168 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy
, msg
)
169 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy
, OnDeleteProxy
)
170 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone
, OnChildFrameProcessGone
)
171 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped
,
172 OnCompositorFrameSwapped(msg
))
173 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener
, OnDisownOpener
)
174 IPC_MESSAGE_HANDLER(FrameMsg_DidStartLoading
, OnDidStartLoading
)
175 IPC_MESSAGE_HANDLER(FrameMsg_DidStopLoading
, OnDidStopLoading
)
176 IPC_MESSAGE_UNHANDLED(handled
= false)
177 IPC_END_MESSAGE_MAP()
179 // Note: If |handled| is true, |this| may have been deleted.
183 bool RenderFrameProxy::Send(IPC::Message
* message
) {
184 return RenderThread::Get()->Send(message
);
187 void RenderFrameProxy::OnDeleteProxy() {
188 DCHECK(web_frame_
->isWebRemoteFrame());
189 web_frame_
->detach();
192 void RenderFrameProxy::OnChildFrameProcessGone() {
193 if (compositing_helper_
.get())
194 compositing_helper_
->ChildFrameGone();
197 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message
& message
) {
198 FrameMsg_CompositorFrameSwapped::Param param
;
199 if (!FrameMsg_CompositorFrameSwapped::Read(&message
, ¶m
))
202 scoped_ptr
<cc::CompositorFrame
> frame(new cc::CompositorFrame
);
203 get
<0>(param
).frame
.AssignTo(frame
.get());
205 if (!compositing_helper_
.get()) {
206 compositing_helper_
=
207 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
208 compositing_helper_
->EnableCompositing(true);
210 compositing_helper_
->OnCompositorFrameSwapped(
212 get
<0>(param
).producing_route_id
,
213 get
<0>(param
).output_surface_id
,
214 get
<0>(param
).producing_host_id
,
215 get
<0>(param
).shared_memory_handle
);
218 void RenderFrameProxy::OnDisownOpener() {
219 // TODO(creis): We should only see this for main frames for now. To support
220 // disowning the opener on subframes, we will need to move WebContentsImpl's
221 // opener_ to FrameTreeNode.
222 CHECK(!web_frame_
->parent());
224 // When there is a RenderFrame for this proxy, tell it to disown its opener.
225 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
226 // they know they have an opener.
227 RenderFrameImpl
* render_frame
=
228 RenderFrameImpl::FromRoutingID(frame_routing_id_
);
230 if (render_frame
->GetWebFrame()->opener())
231 render_frame
->GetWebFrame()->setOpener(NULL
);
235 if (web_frame_
->opener())
236 web_frame_
->setOpener(NULL
);
239 void RenderFrameProxy::OnDidStartLoading() {
240 if (IsMainFrameDetachedFromTree())
243 web_frame_
->didStartLoading();
246 void RenderFrameProxy::OnDidStopLoading() {
247 if (IsMainFrameDetachedFromTree())
250 web_frame_
->didStopLoading();
253 void RenderFrameProxy::frameDetached() {
254 if (web_frame_
->parent())
255 web_frame_
->parent()->removeChild(web_frame_
);
261 void RenderFrameProxy::postMessageEvent(
262 blink::WebLocalFrame
* source_frame
,
263 blink::WebRemoteFrame
* target_frame
,
264 blink::WebSecurityOrigin target_origin
,
265 blink::WebDOMMessageEvent event
) {
266 DCHECK(!web_frame_
|| web_frame_
== target_frame
);
268 ViewMsg_PostMessage_Params params
;
269 params
.is_data_raw_string
= false;
270 params
.data
= event
.data().toString();
271 params
.source_origin
= event
.origin();
272 if (!target_origin
.isNull())
273 params
.target_origin
= target_origin
.toString();
275 blink::WebMessagePortChannelArray channels
= event
.releaseChannels();
276 if (!channels
.isEmpty()) {
277 std::vector
<int> message_port_ids(channels
.size());
278 // Extract the port IDs from the channel array.
279 for (size_t i
= 0; i
< channels
.size(); ++i
) {
280 WebMessagePortChannelImpl
* webchannel
=
281 static_cast<WebMessagePortChannelImpl
*>(channels
[i
]);
282 message_port_ids
[i
] = webchannel
->message_port_id();
283 webchannel
->QueueMessages();
284 DCHECK_NE(message_port_ids
[i
], MSG_ROUTING_NONE
);
286 params
.message_port_ids
= message_port_ids
;
289 // Include the routing ID for the source frame (if one exists), which the
290 // browser process will translate into the routing ID for the equivalent
291 // frame in the target process.
292 params
.source_routing_id
= MSG_ROUTING_NONE
;
294 RenderViewImpl
* source_view
=
295 RenderViewImpl::FromWebView(source_frame
->view());
297 params
.source_routing_id
= source_view
->routing_id();
300 Send(new ViewHostMsg_RouteMessageEvent(render_view_
->GetRoutingID(), params
));
303 void RenderFrameProxy::initializeChildFrame(
304 const blink::WebRect
& frame_rect
,
305 float scale_factor
) {
306 Send(new FrameHostMsg_InitializeChildFrame(
307 routing_id_
, frame_rect
, scale_factor
));
310 void RenderFrameProxy::navigate(const blink::WebURLRequest
& request
,
311 bool should_replace_current_entry
) {
312 FrameHostMsg_OpenURL_Params params
;
313 params
.url
= request
.url();
314 params
.referrer
= Referrer(
315 GURL(request
.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
316 request
.referrerPolicy());
317 params
.disposition
= CURRENT_TAB
;
318 params
.should_replace_current_entry
= should_replace_current_entry
;
319 params
.user_gesture
=
320 blink::WebUserGestureIndicator::isProcessingUserGesture();
321 blink::WebUserGestureIndicator::consumeUserGesture();
322 Send(new FrameHostMsg_OpenURL(routing_id_
, params
));
325 void RenderFrameProxy::forwardInputEvent(const blink::WebInputEvent
* event
) {
326 Send(new FrameHostMsg_ForwardInputEvent(routing_id_
, event
));