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 blink::WebTreeScopeType scope
) {
44 CHECK_NE(routing_id
, MSG_ROUTING_NONE
);
46 scoped_ptr
<RenderFrameProxy
> proxy(
47 new RenderFrameProxy(routing_id
, frame_to_replace
->GetRoutingID()));
49 // When a RenderFrame is replaced by a RenderProxy, the WebRemoteFrame should
50 // always come from WebRemoteFrame::create and a call to WebFrame::swap must
52 blink::WebRemoteFrame
* web_frame
=
53 blink::WebRemoteFrame::create(scope
, proxy
.get());
54 proxy
->Init(web_frame
, frame_to_replace
->render_view());
55 return proxy
.release();
58 RenderFrameProxy
* RenderFrameProxy::CreateFrameProxy(
60 int parent_routing_id
,
61 int render_view_routing_id
,
62 const FrameReplicationState
& replicated_state
) {
63 scoped_ptr
<RenderFrameProxy
> proxy(
64 new RenderFrameProxy(routing_id
, MSG_ROUTING_NONE
));
65 RenderViewImpl
* render_view
= NULL
;
66 blink::WebRemoteFrame
* web_frame
= NULL
;
67 if (parent_routing_id
== MSG_ROUTING_NONE
) {
68 // Create a top level frame.
69 render_view
= RenderViewImpl::FromRoutingID(render_view_routing_id
);
71 blink::WebRemoteFrame::create(replicated_state
.scope
, proxy
.get());
72 render_view
->webview()->setMainFrame(web_frame
);
74 // Create a frame under an existing parent. The parent is always expected
75 // to be a RenderFrameProxy, because navigations initiated by local frames
76 // should not wind up here.
77 RenderFrameProxy
* parent
=
78 RenderFrameProxy::FromRoutingID(parent_routing_id
);
79 web_frame
= parent
->web_frame()->createRemoteChild(
80 replicated_state
.scope
,
81 blink::WebString::fromUTF8(replicated_state
.name
),
82 replicated_state
.sandbox_flags
, proxy
.get());
83 render_view
= parent
->render_view();
86 proxy
->Init(web_frame
, render_view
);
88 // Initialize proxy's WebRemoteFrame with the security origin and other
89 // replicated information.
90 // TODO(dcheng): Calling this when parent_routing_id != MSG_ROUTING_NONE is
91 // mostly redundant, since we already pass the name and sandbox flags in
92 // createLocalChild(). We should update the Blink interface so it also takes
93 // the origin. Then it will be clear that the replication call is only needed
94 // for the case of setting up a main frame proxy.
95 proxy
->SetReplicatedState(replicated_state
);
97 return proxy
.release();
101 RenderFrameProxy
* RenderFrameProxy::FromRoutingID(int32 routing_id
) {
102 RoutingIDProxyMap
* proxies
= g_routing_id_proxy_map
.Pointer();
103 RoutingIDProxyMap::iterator it
= proxies
->find(routing_id
);
104 return it
== proxies
->end() ? NULL
: it
->second
;
108 RenderFrameProxy
* RenderFrameProxy::FromWebFrame(blink::WebFrame
* web_frame
) {
109 FrameMap::iterator iter
= g_frame_map
.Get().find(web_frame
);
110 if (iter
!= g_frame_map
.Get().end()) {
111 RenderFrameProxy
* proxy
= iter
->second
;
112 DCHECK_EQ(web_frame
, proxy
->web_frame());
118 RenderFrameProxy::RenderFrameProxy(int routing_id
, int frame_routing_id
)
119 : routing_id_(routing_id
),
120 frame_routing_id_(frame_routing_id
),
123 std::pair
<RoutingIDProxyMap::iterator
, bool> result
=
124 g_routing_id_proxy_map
.Get().insert(std::make_pair(routing_id_
, this));
125 CHECK(result
.second
) << "Inserting a duplicate item.";
126 RenderThread::Get()->AddRoute(routing_id_
, this);
129 RenderFrameProxy::~RenderFrameProxy() {
130 // TODO(nasko): Set the render_frame_proxy to null to avoid a double deletion
131 // when detaching the main frame. This can be removed once RenderFrameImpl and
132 // RenderFrameProxy have been completely decoupled. See
133 // https://crbug.com/357747.
134 RenderFrameImpl
* render_frame
=
135 RenderFrameImpl::FromRoutingID(frame_routing_id_
);
137 render_frame
->set_render_frame_proxy(nullptr);
139 render_view()->UnregisterRenderFrameProxy(this);
142 RenderThread::Get()->RemoveRoute(routing_id_
);
143 g_routing_id_proxy_map
.Get().erase(routing_id_
);
146 void RenderFrameProxy::Init(blink::WebRemoteFrame
* web_frame
,
147 RenderViewImpl
* render_view
) {
151 web_frame_
= web_frame
;
152 render_view_
= render_view
;
154 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
155 render_view_
->RegisterRenderFrameProxy(this);
157 std::pair
<FrameMap::iterator
, bool> result
=
158 g_frame_map
.Get().insert(std::make_pair(web_frame_
, this));
159 CHECK(result
.second
) << "Inserted a duplicate item.";
162 bool RenderFrameProxy::IsMainFrameDetachedFromTree() const {
163 return web_frame_
->top() == web_frame_
&&
164 render_view_
->webview()->mainFrame()->isWebLocalFrame();
167 void RenderFrameProxy::DidCommitCompositorFrame() {
168 if (compositing_helper_
.get())
169 compositing_helper_
->DidCommitCompositorFrame();
172 void RenderFrameProxy::SetReplicatedState(const FrameReplicationState
& state
) {
174 web_frame_
->setReplicatedOrigin(blink::WebSecurityOrigin::createFromString(
175 blink::WebString::fromUTF8(state
.origin
.string())));
176 web_frame_
->setReplicatedSandboxFlags(state
.sandbox_flags
);
177 web_frame_
->setReplicatedName(blink::WebString::fromUTF8(state
.name
));
180 // Update the proxy's SecurityContext and FrameOwner with new sandbox flags
181 // that were set by its parent in another process.
183 // Normally, when a frame's sandbox attribute is changed dynamically, the
184 // frame's FrameOwner is updated with the new sandbox flags right away, while
185 // the frame's SecurityContext is updated when the frame is navigated and the
186 // new sandbox flags take effect.
188 // Currently, there is no use case for a proxy's pending FrameOwner sandbox
189 // flags, so there's no message sent to proxies when the sandbox attribute is
190 // first updated. Instead, the update message is sent and this function is
191 // called when the new flags take effect, so that the proxy updates its
192 // SecurityContext. This is needed to ensure that sandbox flags are inherited
193 // properly if this proxy ever parents a local frame. The proxy's FrameOwner
194 // flags are also updated here with the caveat that the FrameOwner won't learn
195 // about updates to its flags until they take effect.
196 void RenderFrameProxy::OnDidUpdateSandboxFlags(blink::WebSandboxFlags flags
) {
197 web_frame_
->setReplicatedSandboxFlags(flags
);
198 web_frame_
->setFrameOwnerSandboxFlags(flags
);
201 bool RenderFrameProxy::OnMessageReceived(const IPC::Message
& msg
) {
203 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy
, msg
)
204 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy
, OnDeleteProxy
)
205 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone
, OnChildFrameProcessGone
)
206 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped
,
207 OnCompositorFrameSwapped(msg
))
208 IPC_MESSAGE_HANDLER(FrameMsg_SetChildFrameSurface
, OnSetChildFrameSurface
)
209 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener
, OnDisownOpener
)
210 IPC_MESSAGE_HANDLER(FrameMsg_DidStartLoading
, OnDidStartLoading
)
211 IPC_MESSAGE_HANDLER(FrameMsg_DidStopLoading
, OnDidStopLoading
)
212 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateSandboxFlags
, OnDidUpdateSandboxFlags
)
213 IPC_MESSAGE_HANDLER(FrameMsg_DispatchLoad
, OnDispatchLoad
)
214 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateName
, OnDidUpdateName
)
215 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateOrigin
, OnDidUpdateOrigin
)
216 IPC_MESSAGE_UNHANDLED(handled
= false)
217 IPC_END_MESSAGE_MAP()
219 // Note: If |handled| is true, |this| may have been deleted.
223 bool RenderFrameProxy::Send(IPC::Message
* message
) {
224 return RenderThread::Get()->Send(message
);
227 void RenderFrameProxy::OnDeleteProxy() {
228 DCHECK(web_frame_
->isWebRemoteFrame());
229 web_frame_
->detach();
232 void RenderFrameProxy::OnChildFrameProcessGone() {
233 if (compositing_helper_
.get())
234 compositing_helper_
->ChildFrameGone();
237 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message
& message
) {
238 // If this WebFrame has already been detached, its parent will be null. This
239 // can happen when swapping a WebRemoteFrame with a WebLocalFrame, where this
240 // message may arrive after the frame was removed from the frame tree, but
241 // before the frame has been destroyed. http://crbug.com/446575.
242 if (!web_frame()->parent())
245 FrameMsg_CompositorFrameSwapped::Param param
;
246 if (!FrameMsg_CompositorFrameSwapped::Read(&message
, ¶m
))
249 scoped_ptr
<cc::CompositorFrame
> frame(new cc::CompositorFrame
);
250 base::get
<0>(param
).frame
.AssignTo(frame
.get());
252 if (!compositing_helper_
.get()) {
253 compositing_helper_
=
254 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
255 compositing_helper_
->EnableCompositing(true);
257 compositing_helper_
->OnCompositorFrameSwapped(
259 base::get
<0>(param
).producing_route_id
,
260 base::get
<0>(param
).output_surface_id
,
261 base::get
<0>(param
).producing_host_id
,
262 base::get
<0>(param
).shared_memory_handle
);
265 void RenderFrameProxy::OnSetChildFrameSurface(
266 const cc::SurfaceId
& surface_id
,
267 const gfx::Size
& frame_size
,
269 const cc::SurfaceSequence
& sequence
) {
270 // If this WebFrame has already been detached, its parent will be null. This
271 // can happen when swapping a WebRemoteFrame with a WebLocalFrame, where this
272 // message may arrive after the frame was removed from the frame tree, but
273 // before the frame has been destroyed. http://crbug.com/446575.
274 if (!web_frame()->parent())
277 if (!compositing_helper_
.get()) {
278 compositing_helper_
=
279 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
280 compositing_helper_
->EnableCompositing(true);
282 compositing_helper_
->OnSetSurface(surface_id
, frame_size
, scale_factor
,
286 void RenderFrameProxy::OnDisownOpener() {
287 // TODO(creis): We should only see this for main frames for now. To support
288 // disowning the opener on subframes, we will need to move WebContentsImpl's
289 // opener_ to FrameTreeNode.
290 CHECK(!web_frame_
->parent());
292 // When there is a RenderFrame for this proxy, tell it to disown its opener.
293 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
294 // they know they have an opener.
295 RenderFrameImpl
* render_frame
=
296 RenderFrameImpl::FromRoutingID(frame_routing_id_
);
298 if (render_frame
->GetWebFrame()->opener())
299 render_frame
->GetWebFrame()->setOpener(NULL
);
303 if (web_frame_
->opener())
304 web_frame_
->setOpener(NULL
);
307 void RenderFrameProxy::OnDidStartLoading() {
308 if (IsMainFrameDetachedFromTree())
311 web_frame_
->didStartLoading();
314 void RenderFrameProxy::OnDidStopLoading() {
315 if (IsMainFrameDetachedFromTree())
318 web_frame_
->didStopLoading();
321 void RenderFrameProxy::OnDispatchLoad() {
322 web_frame_
->DispatchLoadEventForFrameOwner();
325 void RenderFrameProxy::OnDidUpdateName(const std::string
& name
) {
326 web_frame_
->setReplicatedName(blink::WebString::fromUTF8(name
));
329 void RenderFrameProxy::OnDidUpdateOrigin(const url::Origin
& origin
) {
330 web_frame_
->setReplicatedOrigin(blink::WebSecurityOrigin::createFromString(
331 blink::WebString::fromUTF8(origin
.string())));
334 void RenderFrameProxy::frameDetached() {
335 frameDetached(DetachType::Remove
);
338 void RenderFrameProxy::frameDetached(DetachType type
) {
339 if (type
== DetachType::Remove
&& web_frame_
->parent()) {
340 web_frame_
->parent()->removeChild(web_frame_
);
342 // Let the browser process know this subframe is removed, so that it is
343 // destroyed in its current process.
344 Send(new FrameHostMsg_Detach(routing_id_
));
349 // Remove the entry in the WebFrame->RenderFrameProxy map, as the |web_frame_|
350 // is no longer valid.
351 FrameMap::iterator it
= g_frame_map
.Get().find(web_frame_
);
352 CHECK(it
!= g_frame_map
.Get().end());
353 CHECK_EQ(it
->second
, this);
354 g_frame_map
.Get().erase(it
);
356 web_frame_
= nullptr;
361 void RenderFrameProxy::postMessageEvent(
362 blink::WebLocalFrame
* source_frame
,
363 blink::WebRemoteFrame
* target_frame
,
364 blink::WebSecurityOrigin target_origin
,
365 blink::WebDOMMessageEvent event
) {
366 DCHECK(!web_frame_
|| web_frame_
== target_frame
);
368 FrameMsg_PostMessage_Params params
;
369 params
.is_data_raw_string
= false;
370 params
.data
= event
.data().toString();
371 params
.source_origin
= event
.origin();
372 if (!target_origin
.isNull())
373 params
.target_origin
= target_origin
.toString();
375 params
.message_ports
=
376 WebMessagePortChannelImpl::ExtractMessagePortIDs(event
.releaseChannels());
378 // Include the routing ID for the source frame (if one exists), which the
379 // browser process will translate into the routing ID for the equivalent
380 // frame in the target process.
381 params
.source_routing_id
= MSG_ROUTING_NONE
;
383 RenderFrameImpl
* source_render_frame
=
384 RenderFrameImpl::FromWebFrame(source_frame
);
385 if (source_render_frame
)
386 params
.source_routing_id
= source_render_frame
->GetRoutingID();
388 params
.source_view_routing_id
= MSG_ROUTING_NONE
;
390 Send(new FrameHostMsg_RouteMessageEvent(routing_id_
, params
));
393 void RenderFrameProxy::initializeChildFrame(
394 const blink::WebRect
& frame_rect
,
395 float scale_factor
) {
396 Send(new FrameHostMsg_InitializeChildFrame(
397 routing_id_
, frame_rect
, scale_factor
));
400 void RenderFrameProxy::navigate(const blink::WebURLRequest
& request
,
401 bool should_replace_current_entry
) {
402 FrameHostMsg_OpenURL_Params params
;
403 params
.url
= request
.url();
404 params
.referrer
= Referrer(
405 GURL(request
.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
406 request
.referrerPolicy());
407 params
.disposition
= CURRENT_TAB
;
408 params
.should_replace_current_entry
= should_replace_current_entry
;
409 params
.user_gesture
=
410 blink::WebUserGestureIndicator::isProcessingUserGesture();
411 blink::WebUserGestureIndicator::consumeUserGesture();
412 Send(new FrameHostMsg_OpenURL(routing_id_
, params
));
415 void RenderFrameProxy::forwardInputEvent(const blink::WebInputEvent
* event
) {
416 Send(new FrameHostMsg_ForwardInputEvent(routing_id_
, event
));