Remove render view id from the audio input and output, part two!
[chromium-blink-merge.git] / content / renderer / pepper / host_array_buffer_var.cc
blobad9e5e6bd43284f6e811fc5db1ec132aa8ca3601
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"
7 #include <stdio.h>
8 #include <string.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/render_thread_impl.h"
19 #include "ppapi/c/pp_instance.h"
21 using ppapi::ArrayBufferVar;
22 using blink::WebArrayBuffer;
24 namespace content {
26 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes)
27 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)),
28 valid_(true) {}
30 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer& buffer)
31 : buffer_(buffer), valid_(true) {}
33 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes,
34 base::SharedMemoryHandle handle)
35 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)) {
36 base::SharedMemory s(handle, true);
37 valid_ = s.Map(size_in_bytes);
38 if (valid_) {
39 memcpy(buffer_.data(), s.memory(), size_in_bytes);
40 s.Unmap();
44 HostArrayBufferVar::~HostArrayBufferVar() {}
46 void* HostArrayBufferVar::Map() {
47 if (!valid_)
48 return NULL;
49 return buffer_.data();
52 void HostArrayBufferVar::Unmap() {
53 // We do not used shared memory on the host side. Nothing to do.
56 uint32 HostArrayBufferVar::ByteLength() { return buffer_.byteLength(); }
58 bool HostArrayBufferVar::CopyToNewShmem(
59 PP_Instance instance,
60 int* host_shm_handle_id,
61 base::SharedMemoryHandle* plugin_shm_handle) {
62 scoped_ptr<base::SharedMemory> shm(
63 RenderThread::Get()
64 ->HostAllocateSharedMemoryBuffer(ByteLength())
65 .release());
66 if (!shm)
67 return false;
69 shm->Map(ByteLength());
70 memcpy(shm->memory(), Map(), ByteLength());
71 shm->Unmap();
73 // Duplicate the handle here; the SharedMemory destructor closes
74 // its handle on us.
75 HostGlobals* hg = HostGlobals::Get();
76 PluginModule* pm = hg->GetModule(hg->GetModuleForInstance(instance));
77 base::ProcessId p = pm->GetPeerProcessId();
78 if (p == base::kNullProcessId) {
79 // In-process, clone for ourselves.
80 p = base::GetCurrentProcId();
83 base::PlatformFile platform_file =
84 PlatformFileFromSharedMemoryHandle(shm->handle());
86 *plugin_shm_handle = BrokerGetFileHandleForProcess(platform_file, p, false);
87 *host_shm_handle_id = -1;
88 return true;
91 } // namespace content