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/pepper_renderer_instance_data.h"
13 #include "content/common/view_messages.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/ppapi_message_utils.h"
23 #include "ppapi/proxy/resource_message_params.h"
29 // Responsible for creating the pending resource hosts, holding their IDs until
30 // all of them have been created for a single message, and sending the reply to
31 // say that the hosts have been created.
32 class PendingHostCreator
33 : public base::RefCounted<PendingHostCreator> {
35 PendingHostCreator(BrowserPpapiHostImpl* host,
36 BrowserMessageFilter* connection,
39 size_t nested_msgs_size);
41 // Adds the given resource host as a pending one. The host is remembered as
42 // host number |index|, and will ultimately be sent to the plugin to be
43 // attached to a real resource.
44 void AddPendingResourceHost(
46 scoped_ptr<ppapi::host::ResourceHost> resource_host);
49 friend class base::RefCounted<PendingHostCreator>;
51 // When the last reference to this class is released, all of the resource
52 // hosts would have been added. This destructor sends the message to the
53 // plugin to tell it to attach real hosts to all of the pending hosts that
54 // have been added by this object.
55 ~PendingHostCreator();
57 BrowserPpapiHostImpl* host_;
58 BrowserMessageFilter* connection_;
61 std::vector<int> pending_resource_host_ids_;
64 PendingHostCreator::PendingHostCreator(BrowserPpapiHostImpl* host,
65 BrowserMessageFilter* connection,
68 size_t nested_msgs_size)
70 connection_(connection),
71 routing_id_(routing_id),
72 sequence_id_(sequence_id),
73 pending_resource_host_ids_(nested_msgs_size, 0) {}
75 void PendingHostCreator::AddPendingResourceHost(
77 scoped_ptr<ppapi::host::ResourceHost> resource_host) {
78 pending_resource_host_ids_[index] =
79 host_->GetPpapiHost()->AddPendingResourceHost(resource_host.Pass());
82 PendingHostCreator::~PendingHostCreator() {
83 connection_->Send(new PpapiHostMsg_CreateResourceHostsFromHostReply(
84 routing_id_, sequence_id_, pending_resource_host_ids_));
89 PepperRendererConnection::PepperRendererConnection(int render_process_id)
90 : render_process_id_(render_process_id) {
91 // Only give the renderer permission for stable APIs.
92 in_process_host_.reset(new BrowserPpapiHostImpl(this,
93 ppapi::PpapiPermissions(),
97 true /* in_process */,
98 false /* external_plugin */));
101 PepperRendererConnection::~PepperRendererConnection() {
104 BrowserPpapiHostImpl* PepperRendererConnection::GetHostForChildProcess(
105 int child_process_id) const {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
108 // Find the plugin which this message refers to. Check NaCl plugins first.
109 BrowserPpapiHostImpl* host = static_cast<BrowserPpapiHostImpl*>(
110 GetContentClient()->browser()->GetExternalBrowserPpapiHost(
114 // Check trusted pepper plugins.
115 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
116 if (iter->process() &&
117 iter->process()->GetData().id == child_process_id) {
119 host = iter->host_impl();
125 // If the message is being sent from an in-process plugin, we own the
127 if (!host && child_process_id == 0) {
128 host = in_process_host_.get();
134 bool PepperRendererConnection::OnMessageReceived(const IPC::Message& msg,
135 bool* message_was_ok) {
136 if (in_process_host_->GetPpapiHost()->OnMessageReceived(msg))
140 IPC_BEGIN_MESSAGE_MAP_EX(PepperRendererConnection, msg, *message_was_ok)
141 IPC_MESSAGE_HANDLER(PpapiHostMsg_CreateResourceHostsFromHost,
142 OnMsgCreateResourceHostsFromHost)
143 IPC_MESSAGE_HANDLER(ViewHostMsg_DidCreateInProcessInstance,
144 OnMsgDidCreateInProcessInstance)
145 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDeleteInProcessInstance,
146 OnMsgDidDeleteInProcessInstance)
147 IPC_MESSAGE_UNHANDLED(handled = false)
148 IPC_END_MESSAGE_MAP_EX()
153 void PepperRendererConnection::OnMsgCreateResourceHostsFromHost(
155 int child_process_id,
156 const ppapi::proxy::ResourceMessageCallParams& params,
157 PP_Instance instance,
158 const std::vector<IPC::Message>& nested_msgs) {
159 BrowserPpapiHostImpl* host = GetHostForChildProcess(child_process_id);
161 DLOG(ERROR) << "Invalid plugin process ID.";
165 scoped_refptr<PendingHostCreator> creator = new PendingHostCreator(
166 host, this, routing_id, params.sequence(), nested_msgs.size());
167 for (size_t i = 0; i < nested_msgs.size(); ++i) {
168 const IPC::Message& nested_msg = nested_msgs[i];
169 scoped_ptr<ppapi::host::ResourceHost> resource_host;
170 if (host->IsValidInstance(instance)) {
171 if (nested_msg.type() == PpapiHostMsg_FileRef_CreateExternal::ID) {
172 // FileRef_CreateExternal is only permitted from the renderer. Because
173 // of this, we handle this message here and not in
174 // content_browser_pepper_host_factory.cc.
175 base::FilePath external_path;
176 if (ppapi::UnpackMessage<PpapiHostMsg_FileRef_CreateExternal>(
177 nested_msg, &external_path)) {
178 resource_host.reset(new PepperFileRefHost(
179 host, instance, params.pp_resource(), external_path));
181 } else if (nested_msg.type() ==
182 PpapiHostMsg_FileSystem_CreateFromRenderer::ID) {
183 // Similarly, FileSystem_CreateFromRenderer is only permitted from the
185 std::string root_url;
186 PP_FileSystemType file_system_type;
187 if (ppapi::UnpackMessage<PpapiHostMsg_FileSystem_CreateFromRenderer>(
188 nested_msg, &root_url, &file_system_type)) {
189 PepperFileSystemBrowserHost* browser_host =
190 new PepperFileSystemBrowserHost(host,
192 params.pp_resource(),
194 resource_host.reset(browser_host);
195 // Open the file system resource host. This is an asynchronous
196 // operation, and we must only add the pending resource host and
197 // send the message once it completes.
198 browser_host->OpenExisting(
201 &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, 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