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 "ppapi/proxy/plugin_array_buffer_var.h"
11 #include "base/process_util.h"
12 #include "base/shared_memory.h"
13 #include "ppapi/c/dev/ppb_buffer_dev.h"
14 #include "ppapi/proxy/plugin_dispatcher.h"
15 #include "ppapi/proxy/plugin_globals.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/proxy/serialized_structs.h"
18 #include "ppapi/shared_impl/host_resource.h"
19 #include "ppapi/shared_impl/resource.h"
20 #include "ppapi/thunk/enter.h"
21 #include "ppapi/thunk/ppb_buffer_api.h"
23 using base::SharedMemory
;
24 using base::SharedMemoryHandle
;
25 using ppapi::proxy::PluginGlobals
;
26 using ppapi::proxy::PluginResourceTracker
;
30 PluginArrayBufferVar::PluginArrayBufferVar(uint32 size_in_bytes
)
31 : buffer_(size_in_bytes
),
32 plugin_handle_(base::SharedMemory::NULLHandle()),
33 size_in_bytes_(size_in_bytes
) {
36 PluginArrayBufferVar::PluginArrayBufferVar(uint32 size_in_bytes
,
37 SharedMemoryHandle plugin_handle
)
38 : plugin_handle_(plugin_handle
),
39 size_in_bytes_(size_in_bytes
) {
42 PluginArrayBufferVar::~PluginArrayBufferVar() {
45 if (shmem_
.get() == NULL
) {
46 // The SharedMemory destuctor can't close the handle for us.
47 if (SharedMemory::IsHandleValid(plugin_handle_
))
48 SharedMemory::CloseHandle(plugin_handle_
);
50 // Delete SharedMemory, if we have one.
55 void* PluginArrayBufferVar::Map() {
57 return shmem_
->memory();
58 if (SharedMemory::IsHandleValid(plugin_handle_
)) {
59 shmem_
.reset(new SharedMemory(plugin_handle_
, false));
60 if (!shmem_
->Map(size_in_bytes_
)) {
64 return shmem_
->memory();
71 void PluginArrayBufferVar::Unmap() {
76 uint32
PluginArrayBufferVar::ByteLength() {
77 return size_in_bytes_
;
80 bool PluginArrayBufferVar::CopyToNewShmem(
83 SharedMemoryHandle
* plugin_out_handle
) {
84 ppapi::proxy::PluginDispatcher
* dispatcher
=
85 ppapi::proxy::PluginDispatcher::GetForInstance(instance
);
89 ppapi::proxy::SerializedHandle plugin_handle
;
90 dispatcher
->Send(new PpapiHostMsg_SharedMemory_CreateSharedMemory(
91 instance
, ByteLength(), host_handle_id
, &plugin_handle
));
92 if (!plugin_handle
.IsHandleValid() || !plugin_handle
.is_shmem() ||
93 *host_handle_id
== -1)
96 base::SharedMemoryHandle tmp_handle
= plugin_handle
.shmem();
97 SharedMemory
s(tmp_handle
, false);
98 if (!s
.Map(ByteLength()))
100 memcpy(s
.memory(), Map(), ByteLength());
103 // We don't need to keep the shared memory around on the plugin side;
104 // we've already copied all our data into it. We'll make it invalid
106 *plugin_out_handle
= base::SharedMemory::NULLHandle();