• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 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.
4 
5 #include "chrome/renderer/pepper/pepper_shared_memory_message_filter.h"
6 
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/shared_memory.h"
9 #include "base/process/process_handle.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/renderer/pepper_plugin_instance.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "content/public/renderer/renderer_ppapi_host.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/shared_impl/var_tracker.h"
17 
PepperSharedMemoryMessageFilter(content::RendererPpapiHost * host)18 PepperSharedMemoryMessageFilter::PepperSharedMemoryMessageFilter(
19     content::RendererPpapiHost* host)
20     : InstanceMessageFilter(host->GetPpapiHost()),
21       host_(host) {
22 }
23 
~PepperSharedMemoryMessageFilter()24 PepperSharedMemoryMessageFilter::~PepperSharedMemoryMessageFilter() {
25 }
26 
OnInstanceMessageReceived(const IPC::Message & msg)27 bool PepperSharedMemoryMessageFilter::OnInstanceMessageReceived(
28     const IPC::Message& msg) {
29   bool handled = true;
30   IPC_BEGIN_MESSAGE_MAP(PepperSharedMemoryMessageFilter, msg)
31     IPC_MESSAGE_HANDLER(PpapiHostMsg_SharedMemory_CreateSharedMemory,
32                         OnHostMsgCreateSharedMemory)
33     IPC_MESSAGE_UNHANDLED(handled = false)
34   IPC_END_MESSAGE_MAP()
35   return handled;
36 }
37 
Send(IPC::Message * msg)38 bool PepperSharedMemoryMessageFilter::Send(IPC::Message* msg) {
39   return host_->GetPpapiHost()->Send(msg);
40 }
41 
OnHostMsgCreateSharedMemory(PP_Instance instance,uint32_t size,int * host_handle_id,ppapi::proxy::SerializedHandle * plugin_handle)42 void PepperSharedMemoryMessageFilter::OnHostMsgCreateSharedMemory(
43     PP_Instance instance,
44     uint32_t size,
45     int* host_handle_id,
46     ppapi::proxy::SerializedHandle* plugin_handle) {
47   plugin_handle->set_null_shmem();
48   *host_handle_id = -1;
49   scoped_ptr<base::SharedMemory> shm(content::RenderThread::Get()->
50       HostAllocateSharedMemoryBuffer(size).Pass());
51   if (!shm.get())
52     return;
53 
54   base::SharedMemoryHandle host_shm_handle;
55   shm->ShareToProcess(base::GetCurrentProcessHandle(), &host_shm_handle);
56   *host_handle_id = content::PepperPluginInstance::Get(instance)->
57       GetVarTracker()->TrackSharedMemoryHandle(instance, host_shm_handle, size);
58 
59   base::PlatformFile host_handle =
60 #if defined(OS_WIN)
61       host_shm_handle;
62 #elif defined(OS_POSIX)
63       host_shm_handle.fd;
64 #else
65   #error Not implemented.
66 #endif
67   // We set auto_close to false since we need our file descriptor to
68   // actually be duplicated on linux. The shared memory destructor will
69   // close the original handle for us.
70   plugin_handle->set_shmem(
71       host_->ShareHandleWithRemote(host_handle, false), size);
72 }
73