If a RemoteFrame asks to navigate, send an OpenURL IPC
[chromium-blink-merge.git] / content / renderer / render_frame_proxy.cc
blob6e719c1ab31ecf8029184c4cf5ff762cece9eb25
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/swapped_out_messages.h"
13 #include "content/common/view_messages.h"
14 #include "content/renderer/child_frame_compositing_helper.h"
15 #include "content/renderer/render_frame_impl.h"
16 #include "content/renderer/render_thread_impl.h"
17 #include "content/renderer/render_view_impl.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
20 #include "third_party/WebKit/public/web/WebView.h"
22 namespace content {
24 namespace {
26 // Facilitates lookup of RenderFrameProxy by routing_id.
27 typedef std::map<int, RenderFrameProxy*> RoutingIDProxyMap;
28 static base::LazyInstance<RoutingIDProxyMap> g_routing_id_proxy_map =
29 LAZY_INSTANCE_INITIALIZER;
31 // Facilitates lookup of RenderFrameProxy by WebFrame.
32 typedef std::map<blink::WebFrame*, RenderFrameProxy*> FrameMap;
33 base::LazyInstance<FrameMap> g_frame_map = LAZY_INSTANCE_INITIALIZER;
35 } // namespace
37 // static
38 RenderFrameProxy* RenderFrameProxy::CreateProxyToReplaceFrame(
39 RenderFrameImpl* frame_to_replace,
40 int routing_id) {
41 CHECK_NE(routing_id, MSG_ROUTING_NONE);
43 scoped_ptr<RenderFrameProxy> proxy(
44 new RenderFrameProxy(routing_id, frame_to_replace->GetRoutingID()));
46 // When a RenderFrame is replaced by a RenderProxy, the WebRemoteFrame should
47 // always come from WebRemoteFrame::create and a call to WebFrame::swap must
48 // follow later.
49 blink::WebRemoteFrame* web_frame = blink::WebRemoteFrame::create(proxy.get());
50 proxy->Init(web_frame, frame_to_replace->render_view());
51 return proxy.release();
54 RenderFrameProxy* RenderFrameProxy::CreateFrameProxy(
55 int routing_id,
56 int parent_routing_id,
57 int render_view_routing_id) {
58 scoped_ptr<RenderFrameProxy> proxy(
59 new RenderFrameProxy(routing_id, MSG_ROUTING_NONE));
60 RenderViewImpl* render_view = NULL;
61 blink::WebRemoteFrame* web_frame = NULL;
62 if (parent_routing_id == MSG_ROUTING_NONE) {
63 // Create a top level frame.
64 render_view = RenderViewImpl::FromRoutingID(render_view_routing_id);
65 web_frame = blink::WebRemoteFrame::create(proxy.get());
66 render_view->webview()->setMainFrame(web_frame);
67 } else {
68 // Create a frame under an existing parent. The parent is always expected
69 // to be a RenderFrameProxy, because navigations initiated by local frames
70 // should not wind up here.
71 RenderFrameProxy* parent =
72 RenderFrameProxy::FromRoutingID(parent_routing_id);
73 web_frame = parent->web_frame()->createRemoteChild("", proxy.get());
74 render_view = parent->render_view();
77 proxy->Init(web_frame, render_view);
79 return proxy.release();
82 // static
83 RenderFrameProxy* RenderFrameProxy::FromRoutingID(int32 routing_id) {
84 RoutingIDProxyMap* proxies = g_routing_id_proxy_map.Pointer();
85 RoutingIDProxyMap::iterator it = proxies->find(routing_id);
86 return it == proxies->end() ? NULL : it->second;
89 // static
90 RenderFrameProxy* RenderFrameProxy::FromWebFrame(blink::WebFrame* web_frame) {
91 FrameMap::iterator iter = g_frame_map.Get().find(web_frame);
92 if (iter != g_frame_map.Get().end()) {
93 RenderFrameProxy* proxy = iter->second;
94 DCHECK_EQ(web_frame, proxy->web_frame());
95 return proxy;
97 return NULL;
100 RenderFrameProxy::RenderFrameProxy(int routing_id, int frame_routing_id)
101 : routing_id_(routing_id),
102 frame_routing_id_(frame_routing_id),
103 web_frame_(NULL),
104 render_view_(NULL) {
105 std::pair<RoutingIDProxyMap::iterator, bool> result =
106 g_routing_id_proxy_map.Get().insert(std::make_pair(routing_id_, this));
107 CHECK(result.second) << "Inserting a duplicate item.";
108 RenderThread::Get()->AddRoute(routing_id_, this);
111 RenderFrameProxy::~RenderFrameProxy() {
112 render_view()->UnregisterRenderFrameProxy(this);
114 FrameMap::iterator it = g_frame_map.Get().find(web_frame_);
115 CHECK(it != g_frame_map.Get().end());
116 CHECK_EQ(it->second, this);
117 g_frame_map.Get().erase(it);
119 RenderThread::Get()->RemoveRoute(routing_id_);
120 g_routing_id_proxy_map.Get().erase(routing_id_);
122 // TODO(nick): Call close unconditionally when web_frame() is always remote.
123 if (web_frame()->isWebRemoteFrame())
124 web_frame()->close();
127 void RenderFrameProxy::Init(blink::WebRemoteFrame* web_frame,
128 RenderViewImpl* render_view) {
129 CHECK(web_frame);
130 CHECK(render_view);
132 web_frame_ = web_frame;
133 render_view_ = render_view;
135 // TODO(nick): Should all RenderFrameProxies remain observers of their views?
136 render_view_->RegisterRenderFrameProxy(this);
138 std::pair<FrameMap::iterator, bool> result =
139 g_frame_map.Get().insert(std::make_pair(web_frame_, this));
140 CHECK(result.second) << "Inserted a duplicate item.";
143 void RenderFrameProxy::DidCommitCompositorFrame() {
144 if (compositing_helper_.get())
145 compositing_helper_->DidCommitCompositorFrame();
148 bool RenderFrameProxy::OnMessageReceived(const IPC::Message& msg) {
149 bool handled = true;
150 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy, msg)
151 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy, OnDeleteProxy)
152 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone, OnChildFrameProcessGone)
153 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped,
154 OnCompositorFrameSwapped(msg))
155 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener, OnDisownOpener)
156 IPC_MESSAGE_UNHANDLED(handled = false)
157 IPC_END_MESSAGE_MAP()
159 // If |handled| is true, |this| may have been deleted.
160 if (handled)
161 return true;
163 RenderFrameImpl* render_frame =
164 RenderFrameImpl::FromRoutingID(frame_routing_id_);
165 return render_frame && render_frame->OnMessageReceived(msg);
168 bool RenderFrameProxy::Send(IPC::Message* message) {
169 if (!SwappedOutMessages::CanSendWhileSwappedOut(message)) {
170 delete message;
171 return false;
173 message->set_routing_id(routing_id_);
174 return RenderThread::Get()->Send(message);
177 void RenderFrameProxy::OnDeleteProxy() {
178 RenderFrameImpl* render_frame =
179 RenderFrameImpl::FromRoutingID(frame_routing_id_);
181 if (render_frame)
182 render_frame->set_render_frame_proxy(NULL);
184 delete this;
187 void RenderFrameProxy::OnChildFrameProcessGone() {
188 if (compositing_helper_.get())
189 compositing_helper_->ChildFrameGone();
192 void RenderFrameProxy::OnCompositorFrameSwapped(const IPC::Message& message) {
193 FrameMsg_CompositorFrameSwapped::Param param;
194 if (!FrameMsg_CompositorFrameSwapped::Read(&message, &param))
195 return;
197 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
198 param.a.frame.AssignTo(frame.get());
200 if (!compositing_helper_.get()) {
201 compositing_helper_ =
202 ChildFrameCompositingHelper::CreateForRenderFrameProxy(this);
203 compositing_helper_->EnableCompositing(true);
205 compositing_helper_->OnCompositorFrameSwapped(frame.Pass(),
206 param.a.producing_route_id,
207 param.a.output_surface_id,
208 param.a.producing_host_id,
209 param.a.shared_memory_handle);
212 void RenderFrameProxy::OnDisownOpener() {
213 // TODO(creis): We should only see this for main frames for now. To support
214 // disowning the opener on subframes, we will need to move WebContentsImpl's
215 // opener_ to FrameTreeNode.
216 CHECK(!web_frame_->parent());
218 // When there is a RenderFrame for this proxy, tell it to disown its opener.
219 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
220 // they know they have an opener.
221 RenderFrameImpl* render_frame =
222 RenderFrameImpl::FromRoutingID(frame_routing_id_);
223 if (render_frame) {
224 if (render_frame->GetWebFrame()->opener())
225 render_frame->GetWebFrame()->setOpener(NULL);
226 return;
229 if (web_frame_->opener())
230 web_frame_->setOpener(NULL);
233 void RenderFrameProxy::postMessageEvent(
234 blink::WebLocalFrame* source_frame,
235 blink::WebRemoteFrame* target_frame,
236 blink::WebSecurityOrigin target_origin,
237 blink::WebDOMMessageEvent event) {
238 DCHECK(!web_frame_ || web_frame_ == target_frame);
240 ViewMsg_PostMessage_Params params;
241 params.is_data_raw_string = false;
242 params.data = event.data().toString();
243 params.source_origin = event.origin();
244 if (!target_origin.isNull())
245 params.target_origin = target_origin.toString();
247 blink::WebMessagePortChannelArray channels = event.releaseChannels();
248 if (!channels.isEmpty()) {
249 std::vector<int> message_port_ids(channels.size());
250 // Extract the port IDs from the channel array.
251 for (size_t i = 0; i < channels.size(); ++i) {
252 WebMessagePortChannelImpl* webchannel =
253 static_cast<WebMessagePortChannelImpl*>(channels[i]);
254 message_port_ids[i] = webchannel->message_port_id();
255 webchannel->QueueMessages();
256 DCHECK_NE(message_port_ids[i], MSG_ROUTING_NONE);
258 params.message_port_ids = message_port_ids;
261 // Include the routing ID for the source frame (if one exists), which the
262 // browser process will translate into the routing ID for the equivalent
263 // frame in the target process.
264 params.source_routing_id = MSG_ROUTING_NONE;
265 if (source_frame) {
266 RenderViewImpl* source_view =
267 RenderViewImpl::FromWebView(source_frame->view());
268 if (source_view)
269 params.source_routing_id = source_view->routing_id();
272 Send(new ViewHostMsg_RouteMessageEvent(render_view_->GetRoutingID(), params));
275 void RenderFrameProxy::initializeChildFrame(
276 const blink::WebRect& frame_rect,
277 float scale_factor) {
278 Send(new FrameHostMsg_InitializeChildFrame(
279 routing_id_, frame_rect, scale_factor));
282 void RenderFrameProxy::navigate(const blink::WebURLRequest& request,
283 bool should_replace_current_entry) {
284 FrameHostMsg_OpenURL_Params params;
285 params.url = request.url();
286 params.referrer = Referrer(
287 GURL(request.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
288 request.referrerPolicy());
289 params.disposition = CURRENT_TAB;
290 params.should_replace_current_entry = should_replace_current_entry;
291 params.user_gesture =
292 blink::WebUserGestureIndicator::isProcessingUserGesture();
293 blink::WebUserGestureIndicator::consumeUserGesture();
294 Send(new FrameHostMsg_OpenURL(routing_id_, params));
297 } // namespace