Index: ppapi/proxy/ppb_buffer_proxy.cc |
diff --git a/ppapi/proxy/ppb_buffer_proxy.cc b/ppapi/proxy/ppb_buffer_proxy.cc |
index 5c2fd8a89be9c95086b775135d13580db6e3f6c4..a358de7714d0127053d3bbfec8d6daae03747702 100644 |
--- a/ppapi/proxy/ppb_buffer_proxy.cc |
+++ b/ppapi/proxy/ppb_buffer_proxy.cc |
@@ -9,12 +9,16 @@ |
#include "base/logging.h" |
#include "build/build_config.h" |
#include "ppapi/c/pp_completion_callback.h" |
+#include "ppapi/c/pp_errors.h" |
#include "ppapi/c/pp_resource.h" |
#include "ppapi/c/dev/ppb_buffer_dev.h" |
+#include "ppapi/proxy/host_dispatcher.h" |
#include "ppapi/proxy/plugin_dispatcher.h" |
#include "ppapi/proxy/plugin_resource.h" |
#include "ppapi/proxy/ppapi_messages.h" |
+#include "ppapi/thunk/enter.h" |
#include "ppapi/thunk/ppb_buffer_api.h" |
+#include "ppapi/thunk/ppb_buffer_trusted_api.h" |
#include "ppapi/thunk/thunk.h" |
namespace pp { |
@@ -33,7 +37,7 @@ class Buffer : public ppapi::thunk::PPB_Buffer_API, |
public PluginResource { |
public: |
Buffer(const HostResource& resource, |
- int memory_handle, |
+ const base::SharedMemoryHandle& shm_handle, |
uint32_t size); |
virtual ~Buffer(); |
@@ -50,19 +54,18 @@ class Buffer : public ppapi::thunk::PPB_Buffer_API, |
virtual void Unmap() OVERRIDE; |
private: |
- int memory_handle_; |
+ base::SharedMemory shm_; |
uint32_t size_; |
- |
void* mapped_data_; |
DISALLOW_COPY_AND_ASSIGN(Buffer); |
}; |
Buffer::Buffer(const HostResource& resource, |
- int memory_handle, |
+ const base::SharedMemoryHandle& shm_handle, |
uint32_t size) |
: PluginResource(resource), |
- memory_handle_(memory_handle), |
+ shm_(shm_handle, false), |
size_(size), |
mapped_data_(NULL) { |
} |
@@ -89,12 +92,13 @@ PP_Bool Buffer::IsMapped() { |
} |
void* Buffer::Map() { |
- // TODO(brettw) implement this. |
- return mapped_data_; |
+ if (!shm_.memory()) |
+ shm_.Map(size_); |
+ return shm_.memory(); |
} |
void Buffer::Unmap() { |
- // TODO(brettw) implement this. |
+ shm_.Unmap(); |
} |
PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher, |
@@ -125,15 +129,14 @@ PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance, |
return 0; |
HostResource result; |
- int32_t shm_handle = -1; |
+ base::SharedMemoryHandle shm_handle = base::SharedMemory::NULLHandle(); |
dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( |
INTERFACE_ID_PPB_BUFFER, instance, size, |
&result, &shm_handle)); |
- if (result.is_null()) |
+ if (result.is_null() || !base::SharedMemory::IsHandleValid(shm_handle)) |
return 0; |
- linked_ptr<Buffer> object(new Buffer(result, |
- static_cast<int>(shm_handle), size)); |
+ linked_ptr<Buffer> object(new Buffer(result, shm_handle, size)); |
return PluginResourceTracker::GetInstance()->AddResource(object); |
} |
@@ -147,15 +150,41 @@ bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { |
return handled; |
} |
-void PPB_Buffer_Proxy::OnMsgCreate(PP_Instance instance, |
- uint32_t size, |
- HostResource* result_resource, |
- int* result_shm_handle) { |
- result_resource->SetHostResource( |
- instance, |
- ppb_buffer_target()->Create(instance, size)); |
- // TODO(brettw) set the shm handle from a trusted interface. |
- *result_shm_handle = 0; |
+void PPB_Buffer_Proxy::OnMsgCreate( |
+ PP_Instance instance, |
+ uint32_t size, |
+ HostResource* result_resource, |
+ base::SharedMemoryHandle* result_shm_handle) { |
+ // Overwritten below on success. |
+ *result_shm_handle = base::SharedMemory::NULLHandle(); |
+ HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
+ if (!dispatcher) |
+ return; |
+ PP_Resource local_buffer_resource = |
+ ppb_buffer_target()->Create(instance, size); |
+ if (local_buffer_resource == 0) |
+ return; |
+ ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_BufferTrusted_API> |
+ trusted_buffer(local_buffer_resource, false); |
+ if (trusted_buffer.failed()) |
+ return; |
+ int local_fd; |
+ if (trusted_buffer.object()->GetSharedMemory(&local_fd) != PP_OK) |
+ return; |
+ |
+ result_resource->SetHostResource(instance, local_buffer_resource); |
+ |
+ // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, |
+ // those casts are ugly. |
+ base::PlatformFile platform_file = |
+#if defined(OS_WIN) |
+ reinterpret_cast<HANDLE>(static_cast<intptr_t>(local_fd)); |
+#elif defined(OS_POSIX) |
+ local_fd; |
+#else |
+ #error Not implemented. |
+#endif |
+ *result_shm_handle = dispatcher->ShareHandleWithRemote(platform_file, false); |
} |
} // namespace proxy |