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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/host_array_buffer_var.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/process/process_handle.h"
14 #include "content/common/pepper_file_util.h"
15 #include "content/common/sandbox_util.h"
16 #include "content/renderer/pepper/host_globals.h"
17 #include "content/renderer/pepper/plugin_module.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "ppapi/c/pp_instance.h"
21 using ppapi::ArrayBufferVar
;
22 using blink::WebArrayBuffer
;
26 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes
)
27 : buffer_(WebArrayBuffer::create(size_in_bytes
, 1 /* element_size */)),
30 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer
& buffer
)
31 : buffer_(buffer
), valid_(true) {}
33 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes
,
34 base::SharedMemoryHandle handle
)
35 : buffer_(WebArrayBuffer::create(size_in_bytes
, 1 /* element_size */)) {
36 base::SharedMemory
s(handle
, true);
37 valid_
= s
.Map(size_in_bytes
);
39 memcpy(buffer_
.data(), s
.memory(), size_in_bytes
);
44 HostArrayBufferVar::~HostArrayBufferVar() {}
46 void* HostArrayBufferVar::Map() {
49 return buffer_
.data();
52 void HostArrayBufferVar::Unmap() {
53 // We do not used shared memory on the host side. Nothing to do.
56 uint32
HostArrayBufferVar::ByteLength() { return buffer_
.byteLength(); }
58 bool HostArrayBufferVar::CopyToNewShmem(
60 int* host_shm_handle_id
,
61 base::SharedMemoryHandle
* plugin_shm_handle
) {
62 scoped_ptr
<base::SharedMemory
> shm(
64 ->HostAllocateSharedMemoryBuffer(ByteLength())
69 shm
->Map(ByteLength());
70 memcpy(shm
->memory(), Map(), ByteLength());
73 // Duplicate the handle here; the SharedMemory destructor closes
75 HostGlobals
* hg
= HostGlobals::Get();
76 PluginModule
* pm
= hg
->GetModule(hg
->GetModuleForInstance(instance
));
77 base::ProcessId p
= pm
->GetPeerProcessId();
78 if (p
== base::kNullProcessId
) {
79 // In-process, clone for ourselves.
80 p
= base::GetCurrentProcId();
83 base::PlatformFile platform_file
=
84 PlatformFileFromSharedMemoryHandle(shm
->handle());
86 *plugin_shm_handle
= BrokerGetFileHandleForProcess(platform_file
, p
, false);
87 *host_shm_handle_id
= -1;
91 } // namespace content