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 18 namespace chrome { 19 20 PepperSharedMemoryMessageFilter::PepperSharedMemoryMessageFilter( 21 content::RendererPpapiHost* host) 22 : InstanceMessageFilter(host->GetPpapiHost()), 23 host_(host) { 24 } 25 26 PepperSharedMemoryMessageFilter::~PepperSharedMemoryMessageFilter() { 27 } 28 29 bool PepperSharedMemoryMessageFilter::OnInstanceMessageReceived( 30 const IPC::Message& msg) { 31 bool handled = true; 32 IPC_BEGIN_MESSAGE_MAP(PepperSharedMemoryMessageFilter, msg) 33 IPC_MESSAGE_HANDLER(PpapiHostMsg_SharedMemory_CreateSharedMemory, 34 OnHostMsgCreateSharedMemory) 35 IPC_MESSAGE_UNHANDLED(handled = false) 36 IPC_END_MESSAGE_MAP() 37 return handled; 38 } 39 40 bool PepperSharedMemoryMessageFilter::Send(IPC::Message* msg) { 41 return host_->GetPpapiHost()->Send(msg); 42 } 43 44 void PepperSharedMemoryMessageFilter::OnHostMsgCreateSharedMemory( 45 PP_Instance instance, 46 uint32_t size, 47 int* host_handle_id, 48 ppapi::proxy::SerializedHandle* plugin_handle) { 49 plugin_handle->set_null_shmem(); 50 *host_handle_id = -1; 51 scoped_ptr<base::SharedMemory> shm(content::RenderThread::Get()-> 52 HostAllocateSharedMemoryBuffer(size).Pass()); 53 if (!shm.get()) 54 return; 55 56 base::SharedMemoryHandle host_shm_handle; 57 shm->ShareToProcess(base::GetCurrentProcessHandle(), &host_shm_handle); 58 *host_handle_id = content::PepperPluginInstance::Get(instance)-> 59 GetVarTracker()->TrackSharedMemoryHandle(instance, host_shm_handle, size); 60 61 base::PlatformFile host_handle = 62 #if defined(OS_WIN) 63 host_shm_handle; 64 #elif defined(OS_POSIX) 65 host_shm_handle.fd; 66 #else 67 #error Not implemented. 68 #endif 69 // We set auto_close to false since we need our file descriptor to 70 // actually be duplicated on linux. The shared memory destructor will 71 // close the original handle for us. 72 plugin_handle->set_shmem( 73 host_->ShareHandleWithRemote(host_handle, false), size); 74 } 75 76 } // namespace chrome 77