Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / render_frame_proxy.cc
blob2a8a389beb9dcd58c3a26191e3f5a078e429e92a
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/common/frame_messages.h"
11 #include "content/common/swapped_out_messages.h"
12 #include "content/renderer/child_frame_compositing_helper.h"
13 #include "content/renderer/render_frame_impl.h"
14 #include "content/renderer/render_thread_impl.h"
15 #include "content/renderer/render_view_impl.h"
16 #include "third_party/WebKit/public/web/WebView.h"
18 namespace content {
20 namespace {
22 // Facilitates lookup of RenderFrameProxy by routing_id.
23 typedef std::map<int, RenderFrameProxy*> RoutingIDProxyMap;
24 static base::LazyInstance<RoutingIDProxyMap> g_routing_id_proxy_map =
25 LAZY_INSTANCE_INITIALIZER;
27 // Facilitates lookup of RenderFrameProxy by WebFrame.
28 typedef std::map<blink::WebFrame*, RenderFrameProxy*> FrameMap;
29 base::LazyInstance<FrameMap> g_frame_map = LAZY_INSTANCE_INITIALIZER;
31 } // namespace
33 // static
34 RenderFrameProxy* RenderFrameProxy::CreateProxyToReplaceFrame(
35 RenderFrameImpl* frame_to_replace,
36 int routing_id) {
37 CHECK_NE(routing_id, MSG_ROUTING_NONE);
39 scoped_ptr<RenderFrameProxy> proxy(
40 new RenderFrameProxy(routing_id, frame_to_replace->GetRoutingID()));
42 blink::WebRemoteFrame* web_frame = NULL;
43 if (frame_to_replace->GetWebFrame()->parent() &&
44 frame_to_replace->GetWebFrame()->parent()->isWebRemoteFrame()) {
45 blink::WebRemoteFrame* parent_web_frame =
46 frame_to_replace->GetWebFrame()->parent()->toWebRemoteFrame();
47 web_frame = parent_web_frame->createRemoteChild("", proxy.get());
48 } else {
49 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 scoped_ptr<RenderFrameProxy> proxy(
61 new RenderFrameProxy(routing_id, MSG_ROUTING_NONE));
62 RenderViewImpl* render_view = NULL;
63 blink::WebRemoteFrame* web_frame = NULL;
64 if (parent_routing_id == MSG_ROUTING_NONE) {
65 // Create a top level frame.
66 render_view = RenderViewImpl::FromRoutingID(render_view_routing_id);
67 web_frame = blink::WebRemoteFrame::create(proxy.get());
68 render_view->webview()->setMainFrame(web_frame);
69 } else {
70 // Create a frame under an existing parent. The parent is always expected
71 // to be a RenderFrameProxy, because navigations initiated by local frames
72 // should not wind up here.
73 RenderFrameProxy* parent =
74 RenderFrameProxy::FromRoutingID(parent_routing_id);
75 web_frame = parent->web_frame()->createRemoteChild("", proxy.get());
76 render_view = parent->render_view();
79 proxy->Init(web_frame, render_view);
81 return proxy.release();
84 // static
85 RenderFrameProxy* RenderFrameProxy::FromRoutingID(int32 routing_id) {
86 RoutingIDProxyMap* proxies = g_routing_id_proxy_map.Pointer();
87 RoutingIDProxyMap::iterator it = proxies->find(routing_id);
88 return it == proxies->end() ? NULL : it->second;
91 // static
92 RenderFrameProxy* RenderFrameProxy::FromWebFrame(blink::WebFrame* web_frame) {
93 FrameMap::iterator iter = g_frame_map.Get().find(web_frame);
94 if (iter != g_frame_map.Get().end()) {
95 RenderFrameProxy* proxy = iter->second;
96 DCHECK_EQ(web_frame, proxy->web_frame());
97 return proxy;
99 return NULL;
102 RenderFrameProxy::RenderFrameProxy(int routing_id, int frame_routing_id)
103 : routing_id_(routing_id),
104 frame_routing_id_(frame_routing_id),
105 web_frame_(NULL),
106 render_view_(NULL) {
107 std::pair<RoutingIDProxyMap::iterator, bool> result =
108 g_routing_id_proxy_map.Get().insert(std::make_pair(routing_id_, this));
109 CHECK(result.second) << "Inserting a duplicate item.";
110 RenderThread::Get()->AddRoute(routing_id_, this);
113 RenderFrameProxy::~RenderFrameProxy() {
114 render_view()->UnregisterRenderFrameProxy(this);
116 FrameMap::iterator it = g_frame_map.Get().find(web_frame_);
117 CHECK(it != g_frame_map.Get().end());
118 CHECK_EQ(it->second, this);
119 g_frame_map.Get().erase(it);
121 RenderThread::Get()->RemoveRoute(routing_id_);
122 g_routing_id_proxy_map.Get().erase(routing_id_);
124 // TODO(nick): Call close unconditionally when web_frame() is always remote.
125 if (web_frame()->isWebRemoteFrame())
126 web_frame()->close();
129 void RenderFrameProxy::Init(blink::WebRemoteFrame* web_frame,
130 RenderViewImpl* render_view) {
131 CHECK(web_frame);
132 CHECK(render_view);
134 web_frame_ = web_frame;
135 render_view_ = render_view;
137 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
138 render_view_->RegisterRenderFrameProxy(this);
140 std::pair<FrameMap::iterator, bool> result =
141 g_frame_map.Get().insert(std::make_pair(web_frame_, this));
142 CHECK(result.second) << "Inserted a duplicate item.";
145 void RenderFrameProxy::DidCommitCompositorFrame() {
146 if (compositing_helper_.get())
147 compositing_helper_->DidCommitCompositorFrame();
150 bool RenderFrameProxy::OnMessageReceived(const IPC::Message& msg) {
151 bool handled = true;
152 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy, msg)
153 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy, OnDeleteProxy)
154 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone, OnChildFrameProcessGone)
155 IPC_MESSAGE_HANDLER(FrameMsg_BuffersSwapped, OnBuffersSwapped)
156 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped,
157 OnCompositorFrameSwapped(msg))
158 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener, OnDisownOpener)
159 IPC_MESSAGE_UNHANDLED(handled = false)
160 IPC_END_MESSAGE_MAP()
162 // If |handled| is true, |this| may have been deleted.
163 if (handled)
164 return true;
166 RenderFrameImpl* render_frame =
167 RenderFrameImpl::FromRoutingID(frame_routing_id_);
168 return render_frame && render_frame->OnMessageReceived(msg);
171 bool RenderFrameProxy::Send(IPC::Message* message) {
172 if (!SwappedOutMessages::CanSendWhileSwappedOut(message)) {
173 delete message;
174 return false;
176 message->set_routing_id(routing_id_);
177 return RenderThread::Get()->Send(message);
180 void RenderFrameProxy::OnDeleteProxy() {
181 RenderFrameImpl* render_frame =
182 RenderFrameImpl::FromRoutingID(frame_routing_id_);
184 if (render_frame)
185 render_frame->set_render_frame_proxy(NULL);
187 delete this;
190 void RenderFrameProxy::OnChildFrameProcessGone() {
191 if (compositing_helper_.get())
192 compositing_helper_->ChildFrameGone();
195 void RenderFrameProxy::OnBuffersSwapped(
196 const FrameMsg_BuffersSwapped_Params& params) {
197 if (!compositing_helper_.get()) {
198 compositing_helper_ =
199 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
200 compositing_helper_->EnableCompositing(true);
202 compositing_helper_->OnBuffersSwapped(
203 params.size,
204 params.mailbox,
205 params.gpu_route_id,
206 params.gpu_host_id,
207 web_frame()->view()->deviceScaleFactor());
210 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message& message) {
211 FrameMsg_CompositorFrameSwapped::Param param;
212 if (!FrameMsg_CompositorFrameSwapped::Read(&message, &param))
213 return;
215 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
216 param.a.frame.AssignTo(frame.get());
218 if (!compositing_helper_.get()) {
219 compositing_helper_ =
220 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
221 compositing_helper_->EnableCompositing(true);
223 compositing_helper_->OnCompositorFrameSwapped(frame.Pass(),
224 param.a.producing_route_id,
225 param.a.output_surface_id,
226 param.a.producing_host_id,
227 param.a.shared_memory_handle);
230 void RenderFrameProxy::OnDisownOpener() {
231 // TODO(creis): We should only see this for main frames for now. To support
232 // disowning the opener on subframes, we will need to move WebContentsImpl's
233 // opener_ to FrameTreeNode.
234 CHECK(!web_frame_->parent());
236 // When there is a RenderFrame for this proxy, tell it to disown its opener.
237 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
238 // they know they have an opener.
239 RenderFrameImpl* render_frame =
240 RenderFrameImpl::FromRoutingID(frame_routing_id_);
241 if (render_frame) {
242 if (render_frame->GetWebFrame()->opener())
243 render_frame->GetWebFrame()->setOpener(NULL);
244 return;
247 if (web_frame_->opener())
248 web_frame_->setOpener(NULL);
251 } // namespace