Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / render_frame_proxy.cc
blobae6d699ae739783c76a70f4dcb05fcd246c45949
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"
7 #include <map>
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"
24 namespace content {
26 namespace {
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;
37 } // namespace
39 // static
40 RenderFrameProxy* RenderFrameProxy::CreateProxyToReplaceFrame(
41 RenderFrameImpl* frame_to_replace,
42 int routing_id) {
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
50 // follow later.
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(
57 int routing_id,
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);
70 } else {
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),
78 RenderFrameImpl::ContentToWebSandboxFlags(
79 replicated_state.sandbox_flags),
80 proxy.get());
81 render_view = parent->render_view();
84 proxy->Init(web_frame, render_view);
86 // Initialize proxy's WebRemoteFrame with the security origin and other
87 // replicated information.
88 proxy->SetReplicatedState(replicated_state);
90 return proxy.release();
93 // static
94 RenderFrameProxy* RenderFrameProxy::FromRoutingID(int32 routing_id) {
95 RoutingIDProxyMap* proxies = g_routing_id_proxy_map.Pointer();
96 RoutingIDProxyMap::iterator it = proxies->find(routing_id);
97 return it == proxies->end() ? NULL : it->second;
100 // static
101 RenderFrameProxy* RenderFrameProxy::FromWebFrame(blink::WebFrame* web_frame) {
102 FrameMap::iterator iter = g_frame_map.Get().find(web_frame);
103 if (iter != g_frame_map.Get().end()) {
104 RenderFrameProxy* proxy = iter->second;
105 DCHECK_EQ(web_frame, proxy->web_frame());
106 return proxy;
108 return NULL;
111 RenderFrameProxy::RenderFrameProxy(int routing_id, int frame_routing_id)
112 : routing_id_(routing_id),
113 frame_routing_id_(frame_routing_id),
114 web_frame_(NULL),
115 render_view_(NULL) {
116 std::pair<RoutingIDProxyMap::iterator, bool> result =
117 g_routing_id_proxy_map.Get().insert(std::make_pair(routing_id_, this));
118 CHECK(result.second) << "Inserting a duplicate item.";
119 RenderThread::Get()->AddRoute(routing_id_, this);
122 RenderFrameProxy::~RenderFrameProxy() {
123 // TODO(nasko): Set the render_frame_proxy to null to avoid a double deletion
124 // when detaching the main frame. This can be removed once RenderFrameImpl and
125 // RenderFrameProxy have been completely decoupled. See
126 // https://crbug.com/357747.
127 RenderFrameImpl* render_frame =
128 RenderFrameImpl::FromRoutingID(frame_routing_id_);
129 if (render_frame)
130 render_frame->set_render_frame_proxy(nullptr);
132 render_view()->UnregisterRenderFrameProxy(this);
134 FrameMap::iterator it = g_frame_map.Get().find(web_frame_);
135 CHECK(it != g_frame_map.Get().end());
136 CHECK_EQ(it->second, this);
137 g_frame_map.Get().erase(it);
139 RenderThread::Get()->RemoveRoute(routing_id_);
140 g_routing_id_proxy_map.Get().erase(routing_id_);
143 void RenderFrameProxy::Init(blink::WebRemoteFrame* web_frame,
144 RenderViewImpl* render_view) {
145 CHECK(web_frame);
146 CHECK(render_view);
148 web_frame_ = web_frame;
149 render_view_ = render_view;
151 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
152 render_view_->RegisterRenderFrameProxy(this);
154 std::pair<FrameMap::iterator, bool> result =
155 g_frame_map.Get().insert(std::make_pair(web_frame_, this));
156 CHECK(result.second) << "Inserted a duplicate item.";
159 bool RenderFrameProxy::IsMainFrameDetachedFromTree() const {
160 return web_frame_->top() == web_frame_ &&
161 render_view_->webview()->mainFrame()->isWebLocalFrame();
164 void RenderFrameProxy::DidCommitCompositorFrame() {
165 if (compositing_helper_.get())
166 compositing_helper_->DidCommitCompositorFrame();
169 void RenderFrameProxy::SetReplicatedState(const FrameReplicationState& state) {
170 DCHECK(web_frame_);
171 web_frame_->setReplicatedOrigin(blink::WebSecurityOrigin::createFromString(
172 blink::WebString::fromUTF8(state.origin.string())));
173 web_frame_->setReplicatedSandboxFlags(
174 RenderFrameImpl::ContentToWebSandboxFlags(state.sandbox_flags));
175 web_frame_->setReplicatedName(blink::WebString::fromUTF8(state.name));
178 // Update the proxy's SecurityContext and FrameOwner with new sandbox flags
179 // that were set by its parent in another process.
181 // Normally, when a frame's sandbox attribute is changed dynamically, the
182 // frame's FrameOwner is updated with the new sandbox flags right away, while
183 // the frame's SecurityContext is updated when the frame is navigated and the
184 // new sandbox flags take effect.
186 // Currently, there is no use case for a proxy's pending FrameOwner sandbox
187 // flags, so there's no message sent to proxies when the sandbox attribute is
188 // first updated. Instead, the update message is sent and this function is
189 // called when the new flags take effect, so that the proxy updates its
190 // SecurityContext. This is needed to ensure that sandbox flags are inherited
191 // properly if this proxy ever parents a local frame. The proxy's FrameOwner
192 // flags are also updated here with the caveat that the FrameOwner won't learn
193 // about updates to its flags until they take effect.
194 void RenderFrameProxy::OnDidUpdateSandboxFlags(SandboxFlags flags) {
195 web_frame_->setReplicatedSandboxFlags(
196 RenderFrameImpl::ContentToWebSandboxFlags(flags));
197 web_frame_->setFrameOwnerSandboxFlags(
198 RenderFrameImpl::ContentToWebSandboxFlags(flags));
201 bool RenderFrameProxy::OnMessageReceived(const IPC::Message& msg) {
202 bool handled = true;
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_DisownOpener, OnDisownOpener)
209 IPC_MESSAGE_HANDLER(FrameMsg_DidStartLoading, OnDidStartLoading)
210 IPC_MESSAGE_HANDLER(FrameMsg_DidStopLoading, OnDidStopLoading)
211 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateSandboxFlags, OnDidUpdateSandboxFlags)
212 IPC_MESSAGE_HANDLER(FrameMsg_DispatchLoad, OnDispatchLoad)
213 IPC_MESSAGE_HANDLER(FrameMsg_DidUpdateName, OnDidUpdateName)
214 IPC_MESSAGE_UNHANDLED(handled = false)
215 IPC_END_MESSAGE_MAP()
217 // Note: If |handled| is true, |this| may have been deleted.
218 return handled;
221 bool RenderFrameProxy::Send(IPC::Message* message) {
222 return RenderThread::Get()->Send(message);
225 void RenderFrameProxy::OnDeleteProxy() {
226 DCHECK(web_frame_->isWebRemoteFrame());
227 web_frame_->detach();
230 void RenderFrameProxy::OnChildFrameProcessGone() {
231 if (compositing_helper_.get())
232 compositing_helper_->ChildFrameGone();
235 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message& message) {
236 // If this WebFrame has already been detached, its parent will be null. This
237 // can happen when swapping a WebRemoteFrame with a WebLocalFrame, where this
238 // message may arrive after the frame was removed from the frame tree, but
239 // before the frame has been destroyed. http://crbug.com/446575.
240 if (!web_frame()->parent())
241 return;
243 FrameMsg_CompositorFrameSwapped::Param param;
244 if (!FrameMsg_CompositorFrameSwapped::Read(&message, &param))
245 return;
247 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
248 get<0>(param).frame.AssignTo(frame.get());
250 if (!compositing_helper_.get()) {
251 compositing_helper_ =
252 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
253 compositing_helper_->EnableCompositing(true);
255 compositing_helper_->OnCompositorFrameSwapped(
256 frame.Pass(),
257 get<0>(param).producing_route_id,
258 get<0>(param).output_surface_id,
259 get<0>(param).producing_host_id,
260 get<0>(param).shared_memory_handle);
263 void RenderFrameProxy::OnDisownOpener() {
264 // TODO(creis): We should only see this for main frames for now. To support
265 // disowning the opener on subframes, we will need to move WebContentsImpl's
266 // opener_ to FrameTreeNode.
267 CHECK(!web_frame_->parent());
269 // When there is a RenderFrame for this proxy, tell it to disown its opener.
270 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
271 // they know they have an opener.
272 RenderFrameImpl* render_frame =
273 RenderFrameImpl::FromRoutingID(frame_routing_id_);
274 if (render_frame) {
275 if (render_frame->GetWebFrame()->opener())
276 render_frame->GetWebFrame()->setOpener(NULL);
277 return;
280 if (web_frame_->opener())
281 web_frame_->setOpener(NULL);
284 void RenderFrameProxy::OnDidStartLoading() {
285 if (IsMainFrameDetachedFromTree())
286 return;
288 web_frame_->didStartLoading();
291 void RenderFrameProxy::OnDidStopLoading() {
292 if (IsMainFrameDetachedFromTree())
293 return;
295 web_frame_->didStopLoading();
298 void RenderFrameProxy::OnDispatchLoad() {
299 web_frame_->DispatchLoadEventForFrameOwner();
302 void RenderFrameProxy::OnDidUpdateName(const std::string& name) {
303 web_frame_->setReplicatedName(blink::WebString::fromUTF8(name));
306 void RenderFrameProxy::frameDetached() {
307 if (web_frame_->parent())
308 web_frame_->parent()->removeChild(web_frame_);
310 web_frame_->close();
311 delete this;
314 void RenderFrameProxy::postMessageEvent(
315 blink::WebLocalFrame* source_frame,
316 blink::WebRemoteFrame* target_frame,
317 blink::WebSecurityOrigin target_origin,
318 blink::WebDOMMessageEvent event) {
319 DCHECK(!web_frame_ || web_frame_ == target_frame);
321 ViewMsg_PostMessage_Params params;
322 params.is_data_raw_string = false;
323 params.data = event.data().toString();
324 params.source_origin = event.origin();
325 if (!target_origin.isNull())
326 params.target_origin = target_origin.toString();
328 params.message_ports =
329 WebMessagePortChannelImpl::ExtractMessagePortIDs(event.releaseChannels());
331 // Include the routing ID for the source frame (if one exists), which the
332 // browser process will translate into the routing ID for the equivalent
333 // frame in the target process.
334 params.source_routing_id = MSG_ROUTING_NONE;
335 if (source_frame) {
336 RenderViewImpl* source_view =
337 RenderViewImpl::FromWebView(source_frame->view());
338 if (source_view)
339 params.source_routing_id = source_view->routing_id();
342 Send(new ViewHostMsg_RouteMessageEvent(render_view_->GetRoutingID(), params));
345 void RenderFrameProxy::initializeChildFrame(
346 const blink::WebRect& frame_rect,
347 float scale_factor) {
348 Send(new FrameHostMsg_InitializeChildFrame(
349 routing_id_, frame_rect, scale_factor));
352 void RenderFrameProxy::navigate(const blink::WebURLRequest& request,
353 bool should_replace_current_entry) {
354 FrameHostMsg_OpenURL_Params params;
355 params.url = request.url();
356 params.referrer = Referrer(
357 GURL(request.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
358 request.referrerPolicy());
359 params.disposition = CURRENT_TAB;
360 params.should_replace_current_entry = should_replace_current_entry;
361 params.user_gesture =
362 blink::WebUserGestureIndicator::isProcessingUserGesture();
363 blink::WebUserGestureIndicator::consumeUserGesture();
364 Send(new FrameHostMsg_OpenURL(routing_id_, params));
367 void RenderFrameProxy::forwardInputEvent(const blink::WebInputEvent* event) {
368 Send(new FrameHostMsg_ForwardInputEvent(routing_id_, event));
371 } // namespace