Android WebView: build debug/release based on variant.
[chromium-blink-merge.git] / ppapi / proxy / plugin_array_buffer_var.cc
blobe990ed8fbe3de636426815fbb7ef39109bdb622f
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"
7 #include <stdlib.h>
9 #include <limits>
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;
28 namespace ppapi {
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() {
43 Unmap();
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_);
49 } else {
50 // Delete SharedMemory, if we have one.
51 shmem_.reset();
55 void* PluginArrayBufferVar::Map() {
56 if (shmem_.get())
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_)) {
61 shmem_.reset();
62 return NULL;
64 return shmem_->memory();
66 if (buffer_.empty())
67 return NULL;
68 return &(buffer_[0]);
71 void PluginArrayBufferVar::Unmap() {
72 if (shmem_.get())
73 shmem_->Unmap();
76 uint32 PluginArrayBufferVar::ByteLength() {
77 return size_in_bytes_;
80 bool PluginArrayBufferVar::CopyToNewShmem(
81 PP_Instance instance,
82 int* host_handle_id,
83 SharedMemoryHandle* plugin_out_handle) {
84 ppapi::proxy::PluginDispatcher* dispatcher =
85 ppapi::proxy::PluginDispatcher::GetForInstance(instance);
86 if (!dispatcher)
87 return false;
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)
94 return false;
96 base::SharedMemoryHandle tmp_handle = plugin_handle.shmem();
97 SharedMemory s(tmp_handle, false);
98 if (!s.Map(ByteLength()))
99 return false;
100 memcpy(s.memory(), Map(), ByteLength());
101 s.Unmap();
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
105 // just to be safe.
106 *plugin_out_handle = base::SharedMemory::NULLHandle();
108 return true;
111 } // namespace ppapi