Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / content / renderer / render_frame_proxy.cc
blob733a0bbf2ea876b94f43d4f46a83b2bda8aebd9e
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 bool RenderFrameProxy::OnMessageReceived(const IPC::Message& msg) {
179 bool handled = true;
180 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxy, msg)
181 IPC_MESSAGE_HANDLER(FrameMsg_DeleteProxy, OnDeleteProxy)
182 IPC_MESSAGE_HANDLER(FrameMsg_ChildFrameProcessGone, OnChildFrameProcessGone)
183 IPC_MESSAGE_HANDLER_GENERIC(FrameMsg_CompositorFrameSwapped,
184 OnCompositorFrameSwapped(msg))
185 IPC_MESSAGE_HANDLER(FrameMsg_DisownOpener, OnDisownOpener)
186 IPC_MESSAGE_HANDLER(FrameMsg_DidStartLoading, OnDidStartLoading)
187 IPC_MESSAGE_HANDLER(FrameMsg_DidStopLoading, OnDidStopLoading)
188 IPC_MESSAGE_HANDLER(FrameMsg_DispatchLoad, OnDispatchLoad)
189 IPC_MESSAGE_UNHANDLED(handled = false)
190 IPC_END_MESSAGE_MAP()
192 // Note: If |handled| is true, |this| may have been deleted.
193 return handled;
196 bool RenderFrameProxy::Send(IPC::Message* message) {
197 return RenderThread::Get()->Send(message);
200 void RenderFrameProxy::OnDeleteProxy() {
201 DCHECK(web_frame_->isWebRemoteFrame());
202 web_frame_->detach();
205 void RenderFrameProxy::OnChildFrameProcessGone() {
206 if (compositing_helper_.get())
207 compositing_helper_->ChildFrameGone();
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 get<0>(param).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(
224 frame.Pass(),
225 get<0>(param).producing_route_id,
226 get<0>(param).output_surface_id,
227 get<0>(param).producing_host_id,
228 get<0>(param).shared_memory_handle);
231 void RenderFrameProxy::OnDisownOpener() {
232 // TODO(creis): We should only see this for main frames for now. To support
233 // disowning the opener on subframes, we will need to move WebContentsImpl's
234 // opener_ to FrameTreeNode.
235 CHECK(!web_frame_->parent());
237 // When there is a RenderFrame for this proxy, tell it to disown its opener.
238 // TODO(creis): Remove this when we only have WebRemoteFrames and make sure
239 // they know they have an opener.
240 RenderFrameImpl* render_frame =
241 RenderFrameImpl::FromRoutingID(frame_routing_id_);
242 if (render_frame) {
243 if (render_frame->GetWebFrame()->opener())
244 render_frame->GetWebFrame()->setOpener(NULL);
245 return;
248 if (web_frame_->opener())
249 web_frame_->setOpener(NULL);
252 void RenderFrameProxy::OnDidStartLoading() {
253 if (IsMainFrameDetachedFromTree())
254 return;
256 web_frame_->didStartLoading();
259 void RenderFrameProxy::OnDidStopLoading() {
260 if (IsMainFrameDetachedFromTree())
261 return;
263 web_frame_->didStopLoading();
266 void RenderFrameProxy::OnDispatchLoad() {
267 web_frame_->DispatchLoadEventForFrameOwner();
270 void RenderFrameProxy::frameDetached() {
271 if (web_frame_->parent())
272 web_frame_->parent()->removeChild(web_frame_);
274 web_frame_->close();
275 delete this;
278 void RenderFrameProxy::postMessageEvent(
279 blink::WebLocalFrame* source_frame,
280 blink::WebRemoteFrame* target_frame,
281 blink::WebSecurityOrigin target_origin,
282 blink::WebDOMMessageEvent event) {
283 DCHECK(!web_frame_ || web_frame_ == target_frame);
285 ViewMsg_PostMessage_Params params;
286 params.is_data_raw_string = false;
287 params.data = event.data().toString();
288 params.source_origin = event.origin();
289 if (!target_origin.isNull())
290 params.target_origin = target_origin.toString();
292 blink::WebMessagePortChannelArray channels = event.releaseChannels();
293 if (!channels.isEmpty()) {
294 std::vector<int> message_port_ids(channels.size());
295 // Extract the port IDs from the channel array.
296 for (size_t i = 0; i < channels.size(); ++i) {
297 WebMessagePortChannelImpl* webchannel =
298 static_cast<WebMessagePortChannelImpl*>(channels[i]);
299 message_port_ids[i] = webchannel->message_port_id();
300 webchannel->QueueMessages();
301 DCHECK_NE(message_port_ids[i], MSG_ROUTING_NONE);
303 params.message_port_ids = message_port_ids;
306 // Include the routing ID for the source frame (if one exists), which the
307 // browser process will translate into the routing ID for the equivalent
308 // frame in the target process.
309 params.source_routing_id = MSG_ROUTING_NONE;
310 if (source_frame) {
311 RenderViewImpl* source_view =
312 RenderViewImpl::FromWebView(source_frame->view());
313 if (source_view)
314 params.source_routing_id = source_view->routing_id();
317 Send(new ViewHostMsg_RouteMessageEvent(render_view_->GetRoutingID(), params));
320 void RenderFrameProxy::initializeChildFrame(
321 const blink::WebRect& frame_rect,
322 float scale_factor) {
323 Send(new FrameHostMsg_InitializeChildFrame(
324 routing_id_, frame_rect, scale_factor));
327 void RenderFrameProxy::navigate(const blink::WebURLRequest& request,
328 bool should_replace_current_entry) {
329 FrameHostMsg_OpenURL_Params params;
330 params.url = request.url();
331 params.referrer = Referrer(
332 GURL(request.httpHeaderField(blink::WebString::fromUTF8("Referer"))),
333 request.referrerPolicy());
334 params.disposition = CURRENT_TAB;
335 params.should_replace_current_entry = should_replace_current_entry;
336 params.user_gesture =
337 blink::WebUserGestureIndicator::isProcessingUserGesture();
338 blink::WebUserGestureIndicator::consumeUserGesture();
339 Send(new FrameHostMsg_OpenURL(routing_id_, params));
342 void RenderFrameProxy::forwardInputEvent(const blink::WebInputEvent* event) {
343 Send(new FrameHostMsg_ForwardInputEvent(routing_id_, event));
346 } // namespace