1 // Copyright (c) 2012 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/pepper/pepper_in_process_router.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
13 #include "content/renderer/render_frame_impl.h"
14 #include "ipc/ipc_message.h"
15 #include "ipc/ipc_sender.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/ppapi_globals.h"
18 #include "ppapi/shared_impl/resource_tracker.h"
20 using ppapi::UnpackMessage
;
24 class PepperInProcessRouter::Channel
: public IPC::Sender
{
26 Channel(const base::Callback
<bool(IPC::Message
*)>& callback
)
27 : callback_(callback
) {}
29 ~Channel() override
{}
31 bool Send(IPC::Message
* message
) override
{ return callback_
.Run(message
); }
34 base::Callback
<bool(IPC::Message
*)> callback_
;
37 PepperInProcessRouter::PepperInProcessRouter(RendererPpapiHostImpl
* host_impl
)
38 : host_impl_(host_impl
),
39 pending_message_id_(0),
42 browser_channel_
.reset(new Channel(base::Bind(
43 &PepperInProcessRouter::SendToBrowser
, base::Unretained(this))));
44 host_to_plugin_router_
.reset(new Channel(base::Bind(
45 &PepperInProcessRouter::SendToPlugin
, base::Unretained(this))));
46 plugin_to_host_router_
.reset(new Channel(
47 base::Bind(&PepperInProcessRouter::SendToHost
, base::Unretained(this))));
50 PepperInProcessRouter::~PepperInProcessRouter() {}
52 IPC::Sender
* PepperInProcessRouter::GetPluginToRendererSender() {
53 return plugin_to_host_router_
.get();
56 IPC::Sender
* PepperInProcessRouter::GetRendererToPluginSender() {
57 return host_to_plugin_router_
.get();
60 ppapi::proxy::Connection
PepperInProcessRouter::GetPluginConnection(
61 PP_Instance instance
) {
63 RenderFrame
* frame
= host_impl_
->GetRenderFrameForInstance(instance
);
65 routing_id
= frame
->GetRoutingID();
66 return ppapi::proxy::Connection(
67 browser_channel_
.get(), plugin_to_host_router_
.get(), routing_id
);
71 bool PepperInProcessRouter::OnPluginMsgReceived(const IPC::Message
& msg
) {
72 // Emulate the proxy by dispatching the relevant message here.
73 ppapi::proxy::ResourceMessageReplyParams reply_params
;
74 IPC::Message nested_msg
;
76 if (msg
.type() == PpapiPluginMsg_ResourceReply::ID
) {
77 // Resource reply from the renderer (no routing id).
78 if (!UnpackMessage
<PpapiPluginMsg_ResourceReply
>(
79 msg
, &reply_params
, &nested_msg
)) {
83 } else if (msg
.type() == PpapiHostMsg_InProcessResourceReply::ID
) {
84 // Resource reply from the browser (has a routing id).
85 if (!UnpackMessage
<PpapiHostMsg_InProcessResourceReply
>(
86 msg
, &reply_params
, &nested_msg
)) {
93 ppapi::Resource
* resource
=
94 ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource(
95 reply_params
.pp_resource());
96 // If the resource doesn't exist, it may have been destroyed so just ignore
99 resource
->OnReplyReceived(reply_params
, nested_msg
);
103 bool PepperInProcessRouter::SendToHost(IPC::Message
* msg
) {
104 scoped_ptr
<IPC::Message
> message(msg
);
106 if (!message
->is_sync()) {
107 // If this is a resource destroyed message, post a task to dispatch it.
108 // Dispatching it synchronously can cause the host to re-enter the proxy
109 // code while we're still in the resource destructor, leading to a crash.
110 // http://crbug.com/276368.
111 // This won't cause message reordering problems because the resource
112 // destroyed message is always the last one sent for a resource.
113 if (message
->type() == PpapiHostMsg_ResourceDestroyed::ID
) {
114 base::ThreadTaskRunnerHandle::Get()->PostTask(
115 FROM_HERE
, base::Bind(&PepperInProcessRouter::DispatchHostMsg
,
116 weak_factory_
.GetWeakPtr(),
117 base::Owned(message
.release())));
120 bool result
= host_impl_
->GetPpapiHost()->OnMessageReceived(*message
);
121 DCHECK(result
) << "The message was not handled by the host.";
126 pending_message_id_
= IPC::SyncMessage::GetMessageId(*message
);
127 reply_deserializer_
.reset(
128 static_cast<IPC::SyncMessage
*>(message
.get())->GetReplyDeserializer());
129 reply_result_
= false;
131 bool result
= host_impl_
->GetPpapiHost()->OnMessageReceived(*message
);
132 DCHECK(result
) << "The message was not handled by the host.";
134 pending_message_id_
= 0;
135 reply_deserializer_
.reset(NULL
);
136 return reply_result_
;
139 bool PepperInProcessRouter::SendToPlugin(IPC::Message
* msg
) {
140 scoped_ptr
<IPC::Message
> message(msg
);
141 CHECK(!msg
->is_sync());
142 if (IPC::SyncMessage::IsMessageReplyTo(*message
, pending_message_id_
)) {
143 if (!msg
->is_reply_error())
144 reply_result_
= reply_deserializer_
->SerializeOutputParameters(*message
);
146 CHECK(!pending_message_id_
);
147 // Dispatch plugin messages from the message loop.
148 base::ThreadTaskRunnerHandle::Get()->PostTask(
150 base::Bind(&PepperInProcessRouter::DispatchPluginMsg
,
151 weak_factory_
.GetWeakPtr(), base::Owned(message
.release())));
156 void PepperInProcessRouter::DispatchHostMsg(IPC::Message
* msg
) {
157 bool handled
= host_impl_
->GetPpapiHost()->OnMessageReceived(*msg
);
161 void PepperInProcessRouter::DispatchPluginMsg(IPC::Message
* msg
) {
162 bool handled
= OnPluginMsgReceived(*msg
);
166 bool PepperInProcessRouter::SendToBrowser(IPC::Message
* msg
) {
167 return RenderThread::Get()->Send(msg
);
170 } // namespace content