Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/pepper/host_array_buffer_var.h" | 5 #include "content/renderer/pepper/host_array_buffer_var.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
| 13 #include "base/process/process_handle.h" | 13 #include "base/process/process_handle.h" |
| 14 #include "content/common/sandbox_util.h" | 14 #include "content/common/sandbox_util.h" |
| 15 #include "content/renderer/pepper/host_globals.h" | 15 #include "content/renderer/pepper/host_globals.h" |
| 16 #include "content/renderer/pepper/plugin_module.h" | 16 #include "content/renderer/pepper/plugin_module.h" |
| 17 #include "content/renderer/render_thread_impl.h" | 17 #include "content/renderer/render_thread_impl.h" |
| 18 #include "ppapi/c/pp_instance.h" | 18 #include "ppapi/c/pp_instance.h" |
| 19 | 19 |
| 20 using ppapi::ArrayBufferVar; | 20 using ppapi::ArrayBufferVar; |
| 21 using blink::WebArrayBuffer; | 21 using blink::WebArrayBuffer; |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes) | 25 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes) |
| 26 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)), | 26 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)), |
| 27 valid_(true) { | 27 valid_(true) {} |
| 28 } | |
| 29 | 28 |
| 30 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer& buffer) | 29 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer& buffer) |
| 31 : buffer_(buffer), | 30 : buffer_(buffer), valid_(true) {} |
| 32 valid_(true) { | |
| 33 } | |
| 34 | 31 |
| 35 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes, | 32 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes, |
| 36 base::SharedMemoryHandle handle) | 33 base::SharedMemoryHandle handle) |
| 37 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)) { | 34 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)) { |
| 38 base::SharedMemory s(handle, true); | 35 base::SharedMemory s(handle, true); |
| 39 valid_ = s.Map(size_in_bytes); | 36 valid_ = s.Map(size_in_bytes); |
| 40 if (valid_) { | 37 if (valid_) { |
| 41 memcpy(buffer_.data(), s.memory(), size_in_bytes); | 38 memcpy(buffer_.data(), s.memory(), size_in_bytes); |
| 42 s.Unmap(); | 39 s.Unmap(); |
| 43 } | 40 } |
| 44 } | 41 } |
| 45 | 42 |
| 46 HostArrayBufferVar::~HostArrayBufferVar() { | 43 HostArrayBufferVar::~HostArrayBufferVar() {} |
| 47 } | |
| 48 | 44 |
| 49 void* HostArrayBufferVar::Map() { | 45 void* HostArrayBufferVar::Map() { |
| 50 if (!valid_) | 46 if (!valid_) |
| 51 return NULL; | 47 return NULL; |
| 52 return buffer_.data(); | 48 return buffer_.data(); |
| 53 } | 49 } |
| 54 | 50 |
| 55 void HostArrayBufferVar::Unmap() { | 51 void HostArrayBufferVar::Unmap() { |
| 56 // We do not used shared memory on the host side. Nothing to do. | 52 // We do not used shared memory on the host side. Nothing to do. |
| 57 } | 53 } |
| 58 | 54 |
| 59 uint32 HostArrayBufferVar::ByteLength() { | 55 uint32 HostArrayBufferVar::ByteLength() { return buffer_.byteLength(); } |
| 60 return buffer_.byteLength(); | |
| 61 } | |
| 62 | 56 |
| 63 bool HostArrayBufferVar::CopyToNewShmem( | 57 bool HostArrayBufferVar::CopyToNewShmem( |
| 64 PP_Instance instance, | 58 PP_Instance instance, |
| 65 int* host_shm_handle_id, | 59 int* host_shm_handle_id, |
| 66 base::SharedMemoryHandle* plugin_shm_handle) { | 60 base::SharedMemoryHandle* plugin_shm_handle) { |
| 67 scoped_ptr<base::SharedMemory> shm( | 61 scoped_ptr<base::SharedMemory> shm( |
| 68 RenderThread::Get()->HostAllocateSharedMemoryBuffer(ByteLength()). | 62 RenderThread::Get() |
| 69 release()); | 63 ->HostAllocateSharedMemoryBuffer(ByteLength()) |
| 64 .release()); | |
|
bbudge
2014/04/07 17:02:17
I wonder why this didn't go on the previous line?
| |
| 70 if (!shm) | 65 if (!shm) |
| 71 return false; | 66 return false; |
| 72 | 67 |
| 73 shm->Map(ByteLength()); | 68 shm->Map(ByteLength()); |
| 74 memcpy(shm->memory(), Map(), ByteLength()); | 69 memcpy(shm->memory(), Map(), ByteLength()); |
| 75 shm->Unmap(); | 70 shm->Unmap(); |
| 76 | 71 |
| 77 // Duplicate the handle here; the SharedMemory destructor closes | 72 // Duplicate the handle here; the SharedMemory destructor closes |
| 78 // its handle on us. | 73 // its handle on us. |
| 79 HostGlobals* hg = HostGlobals::Get(); | 74 HostGlobals* hg = HostGlobals::Get(); |
| 80 PluginModule* pm = hg->GetModule(hg->GetModuleForInstance(instance)); | 75 PluginModule* pm = hg->GetModule(hg->GetModuleForInstance(instance)); |
| 81 base::ProcessId p = pm->GetPeerProcessId(); | 76 base::ProcessId p = pm->GetPeerProcessId(); |
| 82 if (p == base::kNullProcessId) { | 77 if (p == base::kNullProcessId) { |
| 83 // In-process, clone for ourselves. | 78 // In-process, clone for ourselves. |
| 84 p = base::GetCurrentProcId(); | 79 p = base::GetCurrentProcId(); |
| 85 } | 80 } |
| 86 | 81 |
| 87 base::PlatformFile platform_file = | 82 base::PlatformFile platform_file = |
| 88 #if defined(OS_WIN) | 83 #if defined(OS_WIN) |
| 89 shm->handle(); | 84 shm->handle(); |
| 90 #elif defined(OS_POSIX) | 85 #elif defined(OS_POSIX) |
| 91 shm->handle().fd; | 86 shm->handle().fd; |
| 92 #else | 87 #else |
| 93 #error Not implemented. | 88 #error Not implemented. |
| 94 #endif | 89 #endif |
| 95 | 90 |
| 96 *plugin_shm_handle = BrokerGetFileHandleForProcess(platform_file, p, false); | 91 *plugin_shm_handle = BrokerGetFileHandleForProcess(platform_file, p, false); |
| 97 *host_shm_handle_id = -1; | 92 *host_shm_handle_id = -1; |
| 98 return true; | 93 return true; |
| 99 } | 94 } |
| 100 | 95 |
| 101 } // namespace content | 96 } // namespace content |
| OLD | NEW |