1 // Copyright 2013 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/browser/renderer_host/pepper/pepper_renderer_connection.h"
8 #include "base/memory/ref_counted.h"
9 #include "content/browser/browser_child_process_host_impl.h"
10 #include "content/browser/ppapi_plugin_process_host.h"
11 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
12 #include "content/common/frame_messages.h"
13 #include "content/common/pepper_renderer_instance_data.h"
14 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h"
15 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
16 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h"
18 #include "ipc/ipc_message_macros.h"
19 #include "ppapi/host/resource_host.h"
20 #include "ppapi/proxy/ppapi_message_utils.h"
21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/proxy/resource_message_params.h"
28 const uint32 kFilteredMessageClasses
[] = {
33 // Responsible for creating the pending resource hosts, holding their IDs until
34 // all of them have been created for a single message, and sending the reply to
35 // say that the hosts have been created.
36 class PendingHostCreator
: public base::RefCounted
<PendingHostCreator
> {
38 PendingHostCreator(BrowserPpapiHostImpl
* host
,
39 BrowserMessageFilter
* connection
,
42 size_t nested_msgs_size
);
44 // Adds the given resource host as a pending one. The host is remembered as
45 // host number |index|, and will ultimately be sent to the plugin to be
46 // attached to a real resource.
47 void AddPendingResourceHost(
49 scoped_ptr
<ppapi::host::ResourceHost
> resource_host
);
52 friend class base::RefCounted
<PendingHostCreator
>;
54 // When the last reference to this class is released, all of the resource
55 // hosts would have been added. This destructor sends the message to the
56 // plugin to tell it to attach real hosts to all of the pending hosts that
57 // have been added by this object.
58 ~PendingHostCreator();
60 BrowserPpapiHostImpl
* host_
;
61 BrowserMessageFilter
* connection_
;
64 std::vector
<int> pending_resource_host_ids_
;
67 PendingHostCreator::PendingHostCreator(BrowserPpapiHostImpl
* host
,
68 BrowserMessageFilter
* connection
,
71 size_t nested_msgs_size
)
73 connection_(connection
),
74 routing_id_(routing_id
),
75 sequence_id_(sequence_id
),
76 pending_resource_host_ids_(nested_msgs_size
, 0) {}
78 void PendingHostCreator::AddPendingResourceHost(
80 scoped_ptr
<ppapi::host::ResourceHost
> resource_host
) {
81 pending_resource_host_ids_
[index
] =
82 host_
->GetPpapiHost()->AddPendingResourceHost(resource_host
.Pass());
85 PendingHostCreator::~PendingHostCreator() {
86 connection_
->Send(new PpapiHostMsg_CreateResourceHostsFromHostReply(
87 routing_id_
, sequence_id_
, pending_resource_host_ids_
));
92 PepperRendererConnection::PepperRendererConnection(int render_process_id
)
93 : BrowserMessageFilter(kFilteredMessageClasses
,
94 arraysize(kFilteredMessageClasses
)),
95 render_process_id_(render_process_id
) {
96 // Only give the renderer permission for stable APIs.
97 in_process_host_
.reset(new BrowserPpapiHostImpl(this,
98 ppapi::PpapiPermissions(),
102 true /* in_process */,
103 false /* external_plugin */));
106 PepperRendererConnection::~PepperRendererConnection() {}
108 BrowserPpapiHostImpl
* PepperRendererConnection::GetHostForChildProcess(
109 int child_process_id
) const {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
112 // Find the plugin which this message refers to. Check NaCl plugins first.
113 BrowserPpapiHostImpl
* host
= static_cast<BrowserPpapiHostImpl
*>(
114 GetContentClient()->browser()->GetExternalBrowserPpapiHost(
118 // Check trusted pepper plugins.
119 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
120 if (iter
->process() &&
121 iter
->process()->GetData().id
== child_process_id
) {
123 host
= iter
->host_impl();
129 // If the message is being sent from an in-process plugin, we own the
131 if (!host
&& child_process_id
== 0) {
132 host
= in_process_host_
.get();
138 bool PepperRendererConnection::OnMessageReceived(const IPC::Message
& msg
) {
139 if (in_process_host_
->GetPpapiHost()->OnMessageReceived(msg
))
143 IPC_BEGIN_MESSAGE_MAP(PepperRendererConnection
, msg
)
144 IPC_MESSAGE_HANDLER(PpapiHostMsg_CreateResourceHostsFromHost
,
145 OnMsgCreateResourceHostsFromHost
)
146 IPC_MESSAGE_HANDLER(FrameHostMsg_DidCreateInProcessInstance
,
147 OnMsgDidCreateInProcessInstance
)
148 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDeleteInProcessInstance
,
149 OnMsgDidDeleteInProcessInstance
)
150 IPC_MESSAGE_UNHANDLED(handled
= false)
151 IPC_END_MESSAGE_MAP()
156 void PepperRendererConnection::OnMsgCreateResourceHostsFromHost(
158 int child_process_id
,
159 const ppapi::proxy::ResourceMessageCallParams
& params
,
160 PP_Instance instance
,
161 const std::vector
<IPC::Message
>& nested_msgs
) {
162 BrowserPpapiHostImpl
* host
= GetHostForChildProcess(child_process_id
);
164 DLOG(ERROR
) << "Invalid plugin process ID.";
168 scoped_refptr
<PendingHostCreator
> creator
= new PendingHostCreator(
169 host
, this, routing_id
, params
.sequence(), nested_msgs
.size());
170 for (size_t i
= 0; i
< nested_msgs
.size(); ++i
) {
171 const IPC::Message
& nested_msg
= nested_msgs
[i
];
172 scoped_ptr
<ppapi::host::ResourceHost
> resource_host
;
173 if (host
->IsValidInstance(instance
)) {
174 if (nested_msg
.type() == PpapiHostMsg_FileRef_CreateForRawFS::ID
) {
175 // FileRef_CreateForRawFS is only permitted from the renderer. Because
176 // of this, we handle this message here and not in
177 // content_browser_pepper_host_factory.cc.
178 base::FilePath external_path
;
179 if (ppapi::UnpackMessage
<PpapiHostMsg_FileRef_CreateForRawFS
>(
180 nested_msg
, &external_path
)) {
181 resource_host
.reset(new PepperFileRefHost(
182 host
, instance
, params
.pp_resource(), external_path
));
184 } else if (nested_msg
.type() ==
185 PpapiHostMsg_FileSystem_CreateFromRenderer::ID
) {
186 // Similarly, FileSystem_CreateFromRenderer is only permitted from the
188 std::string root_url
;
189 PP_FileSystemType file_system_type
;
190 if (ppapi::UnpackMessage
<PpapiHostMsg_FileSystem_CreateFromRenderer
>(
191 nested_msg
, &root_url
, &file_system_type
)) {
192 PepperFileSystemBrowserHost
* browser_host
=
193 new PepperFileSystemBrowserHost(
194 host
, instance
, params
.pp_resource(), file_system_type
);
195 resource_host
.reset(browser_host
);
196 // Open the file system resource host. This is an asynchronous
197 // operation, and we must only add the pending resource host and
198 // send the message once it completes.
199 browser_host
->OpenExisting(
201 base::Bind(&PendingHostCreator::AddPendingResourceHost
,
204 base::Passed(&resource_host
)));
205 // Do not fall through; the fall-through case adds the pending
206 // resource host to the list. We must do this asynchronously.
212 if (!resource_host
.get()) {
213 resource_host
= host
->GetPpapiHost()->CreateResourceHost(
214 params
.pp_resource(), instance
, nested_msg
);
217 if (resource_host
.get())
218 creator
->AddPendingResourceHost(i
, resource_host
.Pass());
221 // Note: All of the pending host IDs that were added as part of this
222 // operation will automatically be sent to the plugin when |creator| is
223 // released. This may happen immediately, or (if there are asynchronous
224 // requests to create resource hosts), once all of them complete.
227 void PepperRendererConnection::OnMsgDidCreateInProcessInstance(
228 PP_Instance instance
,
229 const PepperRendererInstanceData
& instance_data
) {
230 PepperRendererInstanceData data
= instance_data
;
231 data
.render_process_id
= render_process_id_
;
232 in_process_host_
->AddInstance(instance
, data
);
235 void PepperRendererConnection::OnMsgDidDeleteInProcessInstance(
236 PP_Instance instance
) {
237 in_process_host_
->DeleteInstance(instance
);
240 } // namespace content