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/pepper/renderer_ppapi_host_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "ppapi/c/pp_instance.h"
22 using ppapi::ArrayBufferVar
;
23 using blink::WebArrayBuffer
;
27 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes
)
28 : buffer_(WebArrayBuffer::create(size_in_bytes
, 1 /* element_size */)),
31 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer
& buffer
)
32 : buffer_(buffer
), valid_(true) {}
34 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes
,
35 base::SharedMemoryHandle handle
)
36 : buffer_(WebArrayBuffer::create(size_in_bytes
, 1 /* element_size */)) {
37 base::SharedMemory
s(handle
, true);
38 valid_
= s
.Map(size_in_bytes
);
40 memcpy(buffer_
.data(), s
.memory(), size_in_bytes
);
45 HostArrayBufferVar::~HostArrayBufferVar() {}
47 void* HostArrayBufferVar::Map() {
50 return buffer_
.data();
53 void HostArrayBufferVar::Unmap() {
54 // We do not used shared memory on the host side. Nothing to do.
57 uint32
HostArrayBufferVar::ByteLength() { return buffer_
.byteLength(); }
59 bool HostArrayBufferVar::CopyToNewShmem(
61 int* host_shm_handle_id
,
62 base::SharedMemoryHandle
* plugin_shm_handle
) {
63 scoped_ptr
<base::SharedMemory
> shm(
65 ->HostAllocateSharedMemoryBuffer(ByteLength())
70 shm
->Map(ByteLength());
71 memcpy(shm
->memory(), Map(), ByteLength());
74 // Duplicate the handle here; the SharedMemory destructor closes
76 HostGlobals
* hg
= HostGlobals::Get();
77 PluginModule
* pm
= hg
->GetModule(hg
->GetModuleForInstance(instance
));
80 pm
->renderer_ppapi_host()->ShareSharedMemoryHandleWithRemote(
82 *host_shm_handle_id
= -1;
86 } // namespace content