plugin_array_buffer_var.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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.
4
5#include "ppapi/proxy/plugin_array_buffer_var.h"
6
7#include <stdlib.h>
8
9#include <limits>
10
11#include "base/memory/shared_memory.h"
12#include "base/process_util.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"
22
23using base::SharedMemory;
24using base::SharedMemoryHandle;
25using ppapi::proxy::PluginGlobals;
26using ppapi::proxy::PluginResourceTracker;
27
28namespace ppapi {
29
30PluginArrayBufferVar::PluginArrayBufferVar(uint32 size_in_bytes)
31    : buffer_(size_in_bytes),
32      plugin_handle_(base::SharedMemory::NULLHandle()),
33      size_in_bytes_(size_in_bytes) {
34}
35
36PluginArrayBufferVar::PluginArrayBufferVar(uint32 size_in_bytes,
37                                           SharedMemoryHandle plugin_handle)
38    : plugin_handle_(plugin_handle),
39      size_in_bytes_(size_in_bytes) {
40}
41
42PluginArrayBufferVar::~PluginArrayBufferVar() {
43  Unmap();
44
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();
52  }
53}
54
55void* 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;
63    }
64    return shmem_->memory();
65  }
66  if (buffer_.empty())
67    return NULL;
68  return &(buffer_[0]);
69}
70
71void PluginArrayBufferVar::Unmap() {
72  if (shmem_.get())
73    shmem_->Unmap();
74}
75
76uint32 PluginArrayBufferVar::ByteLength() {
77  return size_in_bytes_;
78}
79
80bool 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;
88
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;
95
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();
102
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();
107
108  return true;
109}
110
111}  // namespace ppapi
112