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/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "content/child/webmessageportchannel_impl.h"
12 #include "content/common/frame_messages.h"
13 #include "content/common/frame_replication_state.h"
14 #include "content/common/swapped_out_messages.h"
15 #include "content/common/view_messages.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/renderer/child_frame_compositing_helper.h"
18 #include "content/renderer/render_frame_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "content/renderer/render_view_impl.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/web/WebLocalFrame.h"
23 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
24 #include "third_party/WebKit/public/web/WebView.h"
30 // Facilitates lookup of RenderFrameProxy by routing_id.
31 typedef std::map
<int, RenderFrameProxy
*> RoutingIDProxyMap
;
32 static base::LazyInstance
<RoutingIDProxyMap
> g_routing_id_proxy_map
=
33 LAZY_INSTANCE_INITIALIZER
;
35 // Facilitates lookup of RenderFrameProxy by WebFrame.
36 typedef std::map
<blink::WebFrame
*, RenderFrameProxy
*> FrameMap
;
37 base::LazyInstance
<FrameMap
> g_frame_map
= LAZY_INSTANCE_INITIALIZER
;
42 RenderFrameProxy
* RenderFrameProxy::CreateProxyToReplaceFrame(
43 RenderFrameImpl
* frame_to_replace
,
45 blink::WebTreeScopeType scope
) {
46 CHECK_NE(routing_id
, MSG_ROUTING_NONE
);
48 scoped_ptr
<RenderFrameProxy
> proxy(
49 new RenderFrameProxy(routing_id
, frame_to_replace
->GetRoutingID()));
51 // When a RenderFrame is replaced by a RenderProxy, the WebRemoteFrame should
52 // always come from WebRemoteFrame::create and a call to WebFrame::swap must
54 blink::WebRemoteFrame
* web_frame
=
55 blink::WebRemoteFrame::create(scope
, proxy
.get());
56 proxy
->Init(web_frame
, frame_to_replace
->render_view());
57 return proxy
.release();
60 RenderFrameProxy
* RenderFrameProxy::CreateFrameProxy(
62 int parent_routing_id
,
63 int render_view_routing_id
,
64 const FrameReplicationState
& replicated_state
) {
65 scoped_ptr
<RenderFrameProxy
> proxy(
66 new RenderFrameProxy(routing_id
, MSG_ROUTING_NONE
));
67 RenderViewImpl
* render_view
= NULL
;
68 blink::WebRemoteFrame
* web_frame
= NULL
;
69 if (parent_routing_id
== MSG_ROUTING_NONE
) {
70 // Create a top level WebRemoteFrame.
71 render_view
= RenderViewImpl::FromRoutingID(render_view_routing_id
);
73 blink::WebRemoteFrame::create(replicated_state
.scope
, proxy
.get());
74 render_view
->webview()->setMainFrame(web_frame
);
76 // Create a frame under an existing parent. The parent is always expected
77 // to be a RenderFrameProxy, because navigations initiated by local frames
78 // should not wind up here.
79 RenderFrameProxy
* parent
=
80 RenderFrameProxy::FromRoutingID(parent_routing_id
);
81 web_frame
= parent
->web_frame()->createRemoteChild(
82 replicated_state
.scope
,
83 blink::WebString::fromUTF8(replicated_state
.name
),
84 replicated_state
.sandbox_flags
, proxy
.get());
85 render_view
= parent
->render_view();
88 proxy
->Init(web_frame
, render_view
);
90 // Initialize proxy's WebRemoteFrame with the security origin and other
91 // replicated information.
92 // TODO(dcheng): Calling this when parent_routing_id != MSG_ROUTING_NONE is
93 // mostly redundant, since we already pass the name and sandbox flags in
94 // createLocalChild(). We should update the Blink interface so it also takes
95 // the origin. Then it will be clear that the replication call is only needed
96 // for the case of setting up a main frame proxy.
97 proxy
->SetReplicatedState(replicated_state
);
99 return proxy
.release();
103 RenderFrameProxy
* RenderFrameProxy::FromRoutingID(int32 routing_id
) {
104 RoutingIDProxyMap
* proxies
= g_routing_id_proxy_map
.Pointer();
105 RoutingIDProxyMap::iterator it
= proxies
->find(routing_id
);
106 return it
== proxies
->end() ? NULL
: it
->second
;
110 RenderFrameProxy
* RenderFrameProxy::FromWebFrame(blink::WebFrame
* web_frame
) {
111 FrameMap::iterator iter
= g_frame_map
.Get().find(web_frame
);
112 if (iter
!= g_frame_map
.Get().end()) {
113 RenderFrameProxy
* proxy
= iter
->second
;
114 DCHECK_EQ(web_frame
, proxy
->web_frame());
121 bool RenderFrameProxy::IsSwappedOutStateForbidden() {
122 return base::CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kSitePerProcess
);
126 RenderFrameProxy::RenderFrameProxy(int routing_id
, int frame_routing_id
)
127 : routing_id_(routing_id
),
128 frame_routing_id_(frame_routing_id
),
131 std::pair
<RoutingIDProxyMap::iterator
, bool> result
=
132 g_routing_id_proxy_map
.Get().insert(std::make_pair(routing_id_
, this));
133 CHECK(result
.second
) << "Inserting a duplicate item.";
134 RenderThread::Get()->AddRoute(routing_id_
, this);
137 RenderFrameProxy::~RenderFrameProxy() {
138 // TODO(nasko): Set the render_frame_proxy to null to avoid a double deletion
139 // when detaching the main frame. This can be removed once RenderFrameImpl and
140 // RenderFrameProxy have been completely decoupled. See
141 // https://crbug.com/357747.
142 RenderFrameImpl
* render_frame
=
143 RenderFrameImpl::FromRoutingID(frame_routing_id_
);
145 render_frame
->set_render_frame_proxy(nullptr);
147 render_view()->UnregisterRenderFrameProxy(this);
150 RenderThread::Get()->RemoveRoute(routing_id_
);
151 g_routing_id_proxy_map
.Get().erase(routing_id_
);
154 void RenderFrameProxy::Init(blink::WebRemoteFrame
* web_frame
,
155 RenderViewImpl
* render_view
) {
159 web_frame_
= web_frame
;
160 render_view_
= render_view
;
162 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
163 render_view_
->RegisterRenderFrameProxy(this);
165 std::pair
<FrameMap::iterator
, bool> result
=
166 g_frame_map
.Get().insert(std::make_pair(web_frame_
, this));
167 CHECK(result
.second
) << "Inserted a duplicate item.";
170 bool RenderFrameProxy::IsMainFrameDetachedFromTree() const {
171 return web_frame_
->top() == web_frame_
&&
172 render_view_
->webview()->mainFrame()->isWebLocalFrame();
175 void RenderFrameProxy::DidCommitCompositorFrame() {
176 if (compositing_helper_
.get())
177 compositing_helper_
->DidCommitCompositorFrame();
180 void RenderFrameProxy::SetReplicatedState(const FrameReplicationState
& state
) {
182 web_frame_
->setReplicatedOrigin(state
.origin
);
183 web_frame_
->setReplicatedSandboxFlags(state
.sandbox_flags
);
184 web_frame_
->setReplicatedName(blink::WebString::fromUTF8(state
.name
));
187 // Update the proxy's SecurityContext and FrameOwner with new sandbox flags
188 // that were set by its parent in another process.
190 // Normally, when a frame's sandbox attribute is changed dynamically, the
191 // frame's FrameOwner is updated with the new sandbox flags right away, while
192 // the frame's SecurityContext is updated when the frame is navigated and the
193 // new sandbox flags take effect.
195 // Currently, there is no use case for a proxy's pending FrameOwner sandbox
196 // flags, so there's no message sent to proxies when the sandbox attribute is
197 // first updated. Instead, the update message is sent and this function is
198 // called when the new flags take effect, so that the proxy updates its
199 // SecurityContext. This is needed to ensure that sandbox flags are inherited
200 // properly if this proxy ever parents a local frame. The proxy's FrameOwner
201 // flags are also updated here with the caveat that the FrameOwner won't learn
202 // about updates to its flags until they take effect.
203 void RenderFrameProxy::OnDidUpdateSandboxFlags(blink::WebSandboxFlags flags
) {
204 web_frame_
->setReplicatedSandboxFlags(flags
);
205 web_frame_
->setFrameOwnerSandboxFlags(flags
);
208 bool RenderFrameProxy::OnMessageReceived(const IPC::Message
& msg
) {
210 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy
, msg
)
211 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy
, OnDeleteProxy
)
212 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone
, OnChildFrameProcessGone
)
213 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped
,
214 OnCompositorFrameSwapped(msg
))
215 IPC_MESSAGE_HANDLER(FrameMsg_SetChildFrameSurface
, OnSetChildFrameSurface
)
216 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener
, OnDisownOpener
)
217 IPC_MESSAGE_HANDLER(FrameMsg_DidStartLoading
, OnDidStartLoading
)
218 IPC_MESSAGE_HANDLER(FrameMsg_DidStopLoading
, OnDidStopLoading
)
219 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateSandboxFlags
, OnDidUpdateSandboxFlags
)
220 IPC_MESSAGE_HANDLER(FrameMsg_DispatchLoad
, OnDispatchLoad
)
221 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateName
, OnDidUpdateName
)
222 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateOrigin
, OnDidUpdateOrigin
)
223 IPC_MESSAGE_UNHANDLED(handled
= false)
224 IPC_END_MESSAGE_MAP()
226 // Note: If |handled| is true, |this| may have been deleted.
230 bool RenderFrameProxy::Send(IPC::Message
* message
) {
231 return RenderThread::Get()->Send(message
);
234 void RenderFrameProxy::OnDeleteProxy() {
235 DCHECK(web_frame_
->isWebRemoteFrame());
236 web_frame_
->detach();
239 void RenderFrameProxy::OnChildFrameProcessGone() {
240 if (compositing_helper_
.get())
241 compositing_helper_
->ChildFrameGone();
244 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message
& message
) {
245 // If this WebFrame has already been detached, its parent will be null. This
246 // can happen when swapping a WebRemoteFrame with a WebLocalFrame, where this
247 // message may arrive after the frame was removed from the frame tree, but
248 // before the frame has been destroyed. http://crbug.com/446575.
249 if (!web_frame()->parent())
252 FrameMsg_CompositorFrameSwapped::Param param
;
253 if (!FrameMsg_CompositorFrameSwapped::Read(&message
, ¶m
))
256 scoped_ptr
<cc::CompositorFrame
> frame(new cc::CompositorFrame
);
257 base::get
<0>(param
).frame
.AssignTo(frame
.get());
259 if (!compositing_helper_
.get()) {
260 compositing_helper_
=
261 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
262 compositing_helper_
->EnableCompositing(true);
264 compositing_helper_
->OnCompositorFrameSwapped(
266 base::get
<0>(param
).producing_route_id
,
267 base::get
<0>(param
).output_surface_id
,
268 base::get
<0>(param
).producing_host_id
,
269 base::get
<0>(param
).shared_memory_handle
);
272 void RenderFrameProxy::OnSetChildFrameSurface(
273 const cc::SurfaceId
& surface_id
,
274 const gfx::Size
& frame_size
,
276 const cc::SurfaceSequence
& sequence
) {
277 // If this WebFrame has already been detached, its parent will be null. This
278 // can happen when swapping a WebRemoteFrame with a WebLocalFrame, where this
279 // message may arrive after the frame was removed from the frame tree, but
280 // before the frame has been destroyed. http://crbug.com/446575.
281 if (!web_frame()->parent())
284 if (!compositing_helper_
.get()) {
285 compositing_helper_
=
286 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
287 compositing_helper_
->EnableCompositing(true);
289 compositing_helper_
->OnSetSurface(surface_id
, frame_size
, scale_factor
,
293 void RenderFrameProxy::OnDisownOpener() {
294 // TODO(creis): We should only see this for main frames for now. To support
295 // disowning the opener on subframes, we will need to move WebContentsImpl's
296 // opener_ to FrameTreeNode.
297 CHECK(!web_frame_
->parent());
299 // When there is a RenderFrame for this proxy, tell it to disown its opener.
300 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
301 // they know they have an opener.
302 if (!RenderFrameProxy::IsSwappedOutStateForbidden()) {
303 RenderFrameImpl
* render_frame
=
304 RenderFrameImpl::FromRoutingID(frame_routing_id_
);
306 if (render_frame
->GetWebFrame()->opener())
307 render_frame
->GetWebFrame()->setOpener(NULL
);
312 if (web_frame_
->opener())
313 web_frame_
->setOpener(NULL
);
316 void RenderFrameProxy::OnDidStartLoading() {
317 if (IsMainFrameDetachedFromTree())
320 web_frame_
->didStartLoading();
323 void RenderFrameProxy::OnDidStopLoading() {
324 if (IsMainFrameDetachedFromTree())
327 web_frame_
->didStopLoading();
330 void RenderFrameProxy::OnDispatchLoad() {
331 web_frame_
->DispatchLoadEventForFrameOwner();
334 void RenderFrameProxy::OnDidUpdateName(const std::string
& name
) {
335 web_frame_
->setReplicatedName(blink::WebString::fromUTF8(name
));
338 void RenderFrameProxy::OnDidUpdateOrigin(const url::Origin
& origin
) {
339 web_frame_
->setReplicatedOrigin(origin
);
342 void RenderFrameProxy::frameDetached(DetachType type
) {
343 if (type
== DetachType::Remove
&& web_frame_
->parent()) {
344 web_frame_
->parent()->removeChild(web_frame_
);
346 // Let the browser process know this subframe is removed, so that it is
347 // destroyed in its current process.
348 Send(new FrameHostMsg_Detach(routing_id_
));
353 // Remove the entry in the WebFrame->RenderFrameProxy map, as the |web_frame_|
354 // is no longer valid.
355 FrameMap::iterator it
= g_frame_map
.Get().find(web_frame_
);
356 CHECK(it
!= g_frame_map
.Get().end());
357 CHECK_EQ(it
->second
, this);
358 g_frame_map
.Get().erase(it
);
360 web_frame_
= nullptr;
365 void RenderFrameProxy::postMessageEvent(
366 blink::WebLocalFrame
* source_frame
,
367 blink::WebRemoteFrame
* target_frame
,
368 blink::WebSecurityOrigin target_origin
,
369 blink::WebDOMMessageEvent event
) {
370 DCHECK(!web_frame_
|| web_frame_
== target_frame
);
372 FrameMsg_PostMessage_Params params
;
373 params
.is_data_raw_string
= false;
374 params
.data
= event
.data().toString();
375 params
.source_origin
= event
.origin();
376 if (!target_origin
.isNull())
377 params
.target_origin
= target_origin
.toString();
379 params
.message_ports
=
380 WebMessagePortChannelImpl::ExtractMessagePortIDs(event
.releaseChannels());
382 // Include the routing ID for the source frame (if one exists), which the
383 // browser process will translate into the routing ID for the equivalent
384 // frame in the target process.
385 params
.source_routing_id
= MSG_ROUTING_NONE
;
387 RenderFrameImpl
* source_render_frame
=
388 RenderFrameImpl::FromWebFrame(source_frame
);
389 if (source_render_frame
)
390 params
.source_routing_id
= source_render_frame
->GetRoutingID();
392 params
.source_view_routing_id
= MSG_ROUTING_NONE
;
394 Send(new FrameHostMsg_RouteMessageEvent(routing_id_
, params
));
397 void RenderFrameProxy::initializeChildFrame(
398 const blink::WebRect
& frame_rect
,
399 float scale_factor
) {
400 Send(new FrameHostMsg_InitializeChildFrame(
401 routing_id_
, frame_rect
, scale_factor
));
404 void RenderFrameProxy::navigate(const blink::WebURLRequest
& request
,
405 bool should_replace_current_entry
) {
406 FrameHostMsg_OpenURL_Params params
;
407 params
.url
= request
.url();
408 params
.referrer
= Referrer(
409 GURL(request
.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
410 request
.referrerPolicy());
411 params
.disposition
= CURRENT_TAB
;
412 params
.should_replace_current_entry
= should_replace_current_entry
;
413 params
.user_gesture
=
414 blink::WebUserGestureIndicator::isProcessingUserGesture();
415 blink::WebUserGestureIndicator::consumeUserGesture();
416 Send(new FrameHostMsg_OpenURL(routing_id_
, params
));
419 void RenderFrameProxy::forwardInputEvent(const blink::WebInputEvent
* event
) {
420 Send(new FrameHostMsg_ForwardInputEvent(routing_id_
, event
));