Add a few more entries to CFI blacklist.
[chromium-blink-merge.git] / content / renderer / pepper / ppb_buffer_impl.cc
blob12bbbeaa3469824354c4c33b501ea120e47adeea
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/ppb_buffer_impl.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/common/pepper_file_util.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "ppapi/c/dev/ppb_buffer_dev.h"
14 #include "ppapi/c/pp_bool.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/pp_instance.h"
17 #include "ppapi/c/pp_resource.h"
19 using ppapi::thunk::PPB_Buffer_API;
21 namespace content {
23 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance)
24 : Resource(ppapi::OBJECT_IS_IMPL, instance), size_(0), map_count_(0) {}
26 PPB_Buffer_Impl::~PPB_Buffer_Impl() {}
28 // static
29 PP_Resource PPB_Buffer_Impl::Create(PP_Instance instance, uint32_t size) {
30 scoped_refptr<PPB_Buffer_Impl> new_resource(CreateResource(instance, size));
31 if (new_resource.get())
32 return new_resource->GetReference();
33 return 0;
36 // static
37 scoped_refptr<PPB_Buffer_Impl> PPB_Buffer_Impl::CreateResource(
38 PP_Instance instance,
39 uint32_t size) {
40 scoped_refptr<PPB_Buffer_Impl> buffer(new PPB_Buffer_Impl(instance));
41 if (!buffer->Init(size))
42 return scoped_refptr<PPB_Buffer_Impl>();
43 return buffer;
46 PPB_Buffer_Impl* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { return this; }
48 PPB_Buffer_API* PPB_Buffer_Impl::AsPPB_Buffer_API() { return this; }
50 bool PPB_Buffer_Impl::Init(uint32_t size) {
51 if (size == 0)
52 return false;
53 size_ = size;
54 shared_memory_.reset(
55 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size).release());
56 return shared_memory_.get() != NULL;
59 PP_Bool PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes) {
60 *size_in_bytes = size_;
61 return PP_TRUE;
64 PP_Bool PPB_Buffer_Impl::IsMapped() {
65 return PP_FromBool(!!shared_memory_->memory());
68 void* PPB_Buffer_Impl::Map() {
69 DCHECK(size_);
70 DCHECK(shared_memory_.get());
71 if (map_count_++ == 0)
72 shared_memory_->Map(size_);
73 return shared_memory_->memory();
76 void PPB_Buffer_Impl::Unmap() {
77 if (--map_count_ == 0)
78 shared_memory_->Unmap();
81 int32_t PPB_Buffer_Impl::GetSharedMemory(base::SharedMemory** shm) {
82 *shm = shared_memory_.get();
83 return PP_OK;
86 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API* api) : api_(api) {
87 needs_unmap_ = !PP_ToBool(api->IsMapped());
88 data_ = api->Map();
89 api->Describe(&size_);
92 BufferAutoMapper::~BufferAutoMapper() {
93 if (needs_unmap_)
94 api_->Unmap();
97 } // namespace content