Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / plugins / ppapi / host_array_buffer_var.cc
blobe3d06a5ec908fecbcf053966bd66644d4b4266b9
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 "webkit/plugins/ppapi/host_array_buffer_var.h"
7 #include <stdio.h>
8 #include <string.h>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/process_util.h"
13 #include "ppapi/c/pp_instance.h"
14 #include "webkit/plugins/ppapi/host_globals.h"
15 #include "webkit/plugins/ppapi/plugin_module.h"
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
18 using ppapi::ArrayBufferVar;
19 using WebKit::WebArrayBuffer;
21 namespace webkit {
22 namespace ppapi {
24 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes)
25 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)),
26 valid_(true) {
29 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer& buffer)
30 : buffer_(buffer),
31 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);
39 if (valid_) {
40 memcpy(buffer_.data(), s.memory(), size_in_bytes);
41 s.Unmap();
45 HostArrayBufferVar::~HostArrayBufferVar() {
48 void* HostArrayBufferVar::Map() {
49 if (!valid_)
50 return NULL;
51 return buffer_.data();
54 void HostArrayBufferVar::Unmap() {
55 // We do not used shared memory on the host side. Nothing to do.
58 uint32 HostArrayBufferVar::ByteLength() {
59 return buffer_.byteLength();
62 bool HostArrayBufferVar::CopyToNewShmem(
63 PP_Instance instance,
64 int* host_shm_handle_id,
65 base::SharedMemoryHandle* plugin_shm_handle) {
66 webkit::ppapi::PluginInstance* i =
67 webkit::ppapi::HostGlobals::Get()->GetInstance(instance);
68 scoped_ptr<base::SharedMemory> shm(i->delegate()->CreateAnonymousSharedMemory(
69 ByteLength()));
70 if (!shm.get())
71 return false;
73 shm->Map(ByteLength());
74 memcpy(shm->memory(), Map(), ByteLength());
75 shm->Unmap();
77 // Duplicate the handle here; the SharedMemory destructor closes
78 // its handle on us.
79 HostGlobals* hg = HostGlobals::Get();
80 PluginModule* pm = hg->GetModule(hg->GetModuleForInstance(instance));
81 base::ProcessId p = pm->GetPeerProcessId();
82 if (p == base::kNullProcessId) {
83 // In-process, clone for ourselves.
84 p = base::GetCurrentProcId();
87 base::PlatformFile platform_file =
88 #if defined(OS_WIN)
89 shm->handle();
90 #elif defined(OS_POSIX)
91 shm->handle().fd;
92 #else
93 #error Not implemented.
94 #endif
96 *plugin_shm_handle =
97 i->delegate()->ShareHandleWithRemote(platform_file, p, false);
98 *host_shm_handle_id = -1;
99 return true;
102 } // namespace ppapi
103 } // namespace webkit