Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/proxy/ppb_buffer_proxy.h" | 5 #include "ppapi/proxy/ppb_buffer_proxy.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "ppapi/c/pp_completion_callback.h" | 11 #include "ppapi/c/pp_completion_callback.h" |
| 12 #include "ppapi/c/pp_resource.h" | 12 #include "ppapi/c/pp_resource.h" |
| 13 #include "ppapi/c/dev/ppb_buffer_dev.h" | 13 #include "ppapi/c/dev/ppb_buffer_dev.h" |
| 14 #include "ppapi/proxy/host_dispatcher.h" | |
| 14 #include "ppapi/proxy/plugin_dispatcher.h" | 15 #include "ppapi/proxy/plugin_dispatcher.h" |
| 15 #include "ppapi/proxy/plugin_resource.h" | 16 #include "ppapi/proxy/plugin_resource.h" |
| 16 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
| 18 #include "ppapi/thunk/enter.h" | |
| 17 #include "ppapi/thunk/ppb_buffer_api.h" | 19 #include "ppapi/thunk/ppb_buffer_api.h" |
| 20 #include "ppapi/thunk/ppb_buffer_trusted_api.h" | |
| 18 #include "ppapi/thunk/thunk.h" | 21 #include "ppapi/thunk/thunk.h" |
| 19 | 22 |
| 20 namespace pp { | 23 namespace pp { |
| 21 namespace proxy { | 24 namespace proxy { |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 InterfaceProxy* CreateBufferProxy(Dispatcher* dispatcher, | 28 InterfaceProxy* CreateBufferProxy(Dispatcher* dispatcher, |
| 26 const void* target_interface) { | 29 const void* target_interface) { |
| 27 return new PPB_Buffer_Proxy(dispatcher, target_interface); | 30 return new PPB_Buffer_Proxy(dispatcher, target_interface); |
| 28 } | 31 } |
| 29 | 32 |
| 30 } // namespace | 33 } // namespace |
| 31 | 34 |
| 32 class Buffer : public ppapi::thunk::PPB_Buffer_API, | 35 class Buffer : public ppapi::thunk::PPB_Buffer_API, |
| 33 public PluginResource { | 36 public PluginResource { |
| 34 public: | 37 public: |
| 35 Buffer(const HostResource& resource, | 38 Buffer(const HostResource& resource, |
| 36 int memory_handle, | 39 const base::SharedMemoryHandle& shm_handle, |
| 37 uint32_t size); | 40 uint32_t size); |
| 38 virtual ~Buffer(); | 41 virtual ~Buffer(); |
| 39 | 42 |
| 40 // Resource overrides. | 43 // Resource overrides. |
| 41 virtual Buffer* AsBuffer() OVERRIDE; | 44 virtual Buffer* AsBuffer() OVERRIDE; |
| 42 | 45 |
| 43 // ResourceObjectBase overries. | 46 // ResourceObjectBase overries. |
| 44 virtual ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE; | 47 virtual ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE; |
| 45 | 48 |
| 46 // PPB_Buffer_API implementation. | 49 // PPB_Buffer_API implementation. |
| 47 virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE; | 50 virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE; |
| 48 virtual PP_Bool IsMapped() OVERRIDE; | 51 virtual PP_Bool IsMapped() OVERRIDE; |
| 49 virtual void* Map() OVERRIDE; | 52 virtual void* Map() OVERRIDE; |
| 50 virtual void Unmap() OVERRIDE; | 53 virtual void Unmap() OVERRIDE; |
| 51 | 54 |
| 52 private: | 55 private: |
| 53 int memory_handle_; | 56 base::SharedMemory shm_; |
| 54 uint32_t size_; | 57 uint32_t size_; |
| 55 | |
| 56 void* mapped_data_; | 58 void* mapped_data_; |
| 57 | 59 |
| 58 DISALLOW_COPY_AND_ASSIGN(Buffer); | 60 DISALLOW_COPY_AND_ASSIGN(Buffer); |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 Buffer::Buffer(const HostResource& resource, | 63 Buffer::Buffer(const HostResource& resource, |
| 62 int memory_handle, | 64 const base::SharedMemoryHandle& shm_handle, |
| 63 uint32_t size) | 65 uint32_t size) |
| 64 : PluginResource(resource), | 66 : PluginResource(resource), |
| 65 memory_handle_(memory_handle), | 67 shm_(shm_handle, false), |
| 66 size_(size), | 68 size_(size), |
| 67 mapped_data_(NULL) { | 69 mapped_data_(NULL) { |
| 68 } | 70 } |
| 69 | 71 |
| 70 Buffer::~Buffer() { | 72 Buffer::~Buffer() { |
| 71 Unmap(); | 73 Unmap(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 Buffer* Buffer::AsBuffer() { | 76 Buffer* Buffer::AsBuffer() { |
| 75 return this; | 77 return this; |
| 76 } | 78 } |
| 77 | 79 |
| 78 ppapi::thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() { | 80 ppapi::thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() { |
| 79 return this; | 81 return this; |
| 80 } | 82 } |
| 81 | 83 |
| 82 PP_Bool Buffer::Describe(uint32_t* size_in_bytes) { | 84 PP_Bool Buffer::Describe(uint32_t* size_in_bytes) { |
| 83 *size_in_bytes = size_; | 85 *size_in_bytes = size_; |
| 84 return PP_TRUE; | 86 return PP_TRUE; |
| 85 } | 87 } |
| 86 | 88 |
| 87 PP_Bool Buffer::IsMapped() { | 89 PP_Bool Buffer::IsMapped() { |
| 88 return PP_FromBool(!!mapped_data_); | 90 return PP_FromBool(!!mapped_data_); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void* Buffer::Map() { | 93 void* Buffer::Map() { |
| 92 // TODO(brettw) implement this. | 94 if (!shm_.memory()) |
| 93 return mapped_data_; | 95 shm_.Map(size_); |
| 96 return shm_.memory(); | |
| 94 } | 97 } |
| 95 | 98 |
| 96 void Buffer::Unmap() { | 99 void Buffer::Unmap() { |
| 97 // TODO(brettw) implement this. | 100 shm_.Unmap(); |
| 98 } | 101 } |
| 99 | 102 |
| 100 PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher, | 103 PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher, |
| 101 const void* target_interface) | 104 const void* target_interface) |
| 102 : InterfaceProxy(dispatcher, target_interface) { | 105 : InterfaceProxy(dispatcher, target_interface) { |
| 103 } | 106 } |
| 104 | 107 |
| 105 PPB_Buffer_Proxy::~PPB_Buffer_Proxy() { | 108 PPB_Buffer_Proxy::~PPB_Buffer_Proxy() { |
| 106 } | 109 } |
| 107 | 110 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 118 } | 121 } |
| 119 | 122 |
| 120 // static | 123 // static |
| 121 PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance, | 124 PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance, |
| 122 uint32_t size) { | 125 uint32_t size) { |
| 123 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 126 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 124 if (!dispatcher) | 127 if (!dispatcher) |
| 125 return 0; | 128 return 0; |
| 126 | 129 |
| 127 HostResource result; | 130 HostResource result; |
| 128 int32_t shm_handle = -1; | 131 base::SharedMemoryHandle shm_handle = base::SharedMemory::NULLHandle(); |
| 129 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( | 132 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( |
| 130 INTERFACE_ID_PPB_BUFFER, instance, size, | 133 INTERFACE_ID_PPB_BUFFER, instance, size, |
| 131 &result, &shm_handle)); | 134 &result, &shm_handle)); |
| 132 if (result.is_null()) | 135 if (result.is_null() || !base::SharedMemory::IsHandleValid(shm_handle)) |
| 133 return 0; | 136 return 0; |
| 134 | 137 |
| 135 linked_ptr<Buffer> object(new Buffer(result, | 138 linked_ptr<Buffer> object(new Buffer(result, shm_handle, size)); |
| 136 static_cast<int>(shm_handle), size)); | |
| 137 return PluginResourceTracker::GetInstance()->AddResource(object); | 139 return PluginResourceTracker::GetInstance()->AddResource(object); |
| 138 } | 140 } |
| 139 | 141 |
| 140 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { | 142 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 141 bool handled = true; | 143 bool handled = true; |
| 142 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg) | 144 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg) |
| 143 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate) | 145 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate) |
| 144 IPC_MESSAGE_UNHANDLED(handled = false) | 146 IPC_MESSAGE_UNHANDLED(handled = false) |
| 145 IPC_END_MESSAGE_MAP() | 147 IPC_END_MESSAGE_MAP() |
| 146 // TODO(brettw) handle bad messages! | 148 // TODO(brettw) handle bad messages! |
| 147 return handled; | 149 return handled; |
| 148 } | 150 } |
| 149 | 151 |
| 150 void PPB_Buffer_Proxy::OnMsgCreate(PP_Instance instance, | 152 void PPB_Buffer_Proxy::OnMsgCreate( |
| 151 uint32_t size, | 153 PP_Instance instance, |
| 152 HostResource* result_resource, | 154 uint32_t size, |
| 153 int* result_shm_handle) { | 155 HostResource* result_resource, |
| 154 result_resource->SetHostResource( | 156 base::SharedMemoryHandle* result_shm_handle) { |
| 155 instance, | 157 // Overwritten below on success. |
| 156 ppb_buffer_target()->Create(instance, size)); | 158 *result_shm_handle = base::SharedMemory::NULLHandle(); |
| 157 // TODO(brettw) set the shm handle from a trusted interface. | 159 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 158 *result_shm_handle = 0; | 160 if (!dispatcher) |
| 161 return; | |
| 162 PP_Resource local_buffer_resource = | |
| 163 ppb_buffer_target()->Create(instance, size); | |
| 164 if (local_buffer_resource == 0) | |
| 165 return; | |
| 166 ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_BufferTrusted_API> | |
| 167 trusted_buffer(local_buffer_resource, false); | |
| 168 if (trusted_buffer.failed()) | |
| 169 return; | |
| 170 int local_fd; | |
| 171 if (trusted_buffer.object()->GetSharedMemory(&local_fd)) | |
|
piman
2011/06/09 23:11:32
nit: if (... != PP_OK)
I think that's more readabl
Ami GONE FROM CHROMIUM
2011/06/09 23:35:28
Done.
| |
| 172 return; | |
| 173 | |
| 174 result_resource->SetHostResource(instance, local_buffer_resource); | |
| 175 *result_shm_handle = dispatcher->ShareHandleWithRemote(local_fd, false); | |
| 159 } | 176 } |
| 160 | 177 |
| 161 } // namespace proxy | 178 } // namespace proxy |
| 162 } // namespace pp | 179 } // namespace pp |
| OLD | NEW |