Add ICU message format support
[chromium-blink-merge.git] / content / renderer / pepper / host_array_buffer_var.cc
blob67f5f81f0edcbcadeeb89a91098b508937a42a78
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/pepper/renderer_ppapi_host_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "ppapi/c/pp_instance.h"
22 using ppapi::ArrayBufferVar;
23 using blink::WebArrayBuffer;
25 namespace content {
27 HostArrayBufferVar::HostArrayBufferVar(uint32 size_in_bytes)
28 : buffer_(WebArrayBuffer::create(size_in_bytes, 1 /* element_size */)),
29 valid_(true) {}
31 HostArrayBufferVar::HostArrayBufferVar(const WebArrayBuffer& buffer)
32 : buffer_(buffer), 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() {}
47 void* HostArrayBufferVar::Map() {
48 if (!valid_)
49 return NULL;
50 return buffer_.data();
53 void HostArrayBufferVar::Unmap() {
54 // We do not used shared memory on the host side. Nothing to do.
57 uint32 HostArrayBufferVar::ByteLength() { return buffer_.byteLength(); }
59 bool HostArrayBufferVar::CopyToNewShmem(
60 PP_Instance instance,
61 int* host_shm_handle_id,
62 base::SharedMemoryHandle* plugin_shm_handle) {
63 scoped_ptr<base::SharedMemory> shm(
64 RenderThread::Get()
65 ->HostAllocateSharedMemoryBuffer(ByteLength())
66 .release());
67 if (!shm)
68 return false;
70 shm->Map(ByteLength());
71 memcpy(shm->memory(), Map(), ByteLength());
72 shm->Unmap();
74 // Duplicate the handle here; the SharedMemory destructor closes
75 // its handle on us.
76 HostGlobals* hg = HostGlobals::Get();
77 PluginModule* pm = hg->GetModule(hg->GetModuleForInstance(instance));
79 *plugin_shm_handle =
80 pm->renderer_ppapi_host()->ShareSharedMemoryHandleWithRemote(
81 shm->handle());
82 *host_shm_handle_id = -1;
83 return true;
86 } // namespace content