blob: 39d31915c038f442d936917696c2f34d395e4ac8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/plugins/ppapi/ppb_buffer_impl.h"
#include <algorithm>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "ppapi/c/dev/ppb_buffer_dev.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_resource.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
using ::ppapi::thunk::PPB_Buffer_API;
namespace webkit {
namespace ppapi {
PPB_Buffer_Impl::PPB_Buffer_Impl(PluginInstance* instance)
: Resource(instance), size_(0) {
}
PPB_Buffer_Impl::~PPB_Buffer_Impl() {
}
// static
PP_Resource PPB_Buffer_Impl::Create(PP_Instance pp_instance, uint32_t size) {
PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance);
if (!instance)
return 0;
scoped_refptr<PPB_Buffer_Impl> buffer(new PPB_Buffer_Impl(instance));
if (!buffer->Init(size))
return 0;
return buffer->GetReference();
}
PPB_Buffer_Impl* PPB_Buffer_Impl::AsPPB_Buffer_Impl() {
return this;
}
PPB_Buffer_API* PPB_Buffer_Impl::AsPPB_Buffer_API() {
return this;
}
bool PPB_Buffer_Impl::Init(uint32_t size) {
if (size == 0 || !instance())
return false;
size_ = size;
shared_memory_.reset(
instance()->delegate()->CreateAnonymousSharedMemory(size));
return shared_memory_.get() != NULL;
}
PP_Bool PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes) {
*size_in_bytes = size_;
return PP_TRUE;
}
PP_Bool PPB_Buffer_Impl::IsMapped() {
return PP_FromBool(!!shared_memory_->memory());
}
void* PPB_Buffer_Impl::Map() {
DCHECK(size_);
DCHECK(shared_memory_.get());
if (!shared_memory_->Map(size_))
return NULL;
return shared_memory_->memory();
}
void PPB_Buffer_Impl::Unmap() {
shared_memory_->Unmap();
}
BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API* api) : api_(api) {
needs_unmap_ = !PP_ToBool(api->IsMapped());
data_ = api->Map();
api->Describe(&size_);
}
BufferAutoMapper::~BufferAutoMapper() {
if (needs_unmap_)
api_->Unmap();
}
} // namespace ppapi
} // namespace webkit
|