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 "ppapi/shared_impl/scoped_pp_var.h"
7 #include "ppapi/c/dev/ppb_memory_dev.h"
8 #include "ppapi/shared_impl/ppapi_globals.h"
9 #include "ppapi/shared_impl/var_tracker.h"
10 #include "ppapi/thunk/thunk.h"
16 void CallAddRef(const PP_Var
& v
) {
17 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(v
);
20 void CallRelease(const PP_Var
& v
) {
21 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(v
);
26 ScopedPPVar::ScopedPPVar() : var_(PP_MakeUndefined()) {}
28 ScopedPPVar::ScopedPPVar(const PP_Var
& v
) : var_(v
) { CallAddRef(var_
); }
30 ScopedPPVar::ScopedPPVar(const PassRef
&, const PP_Var
& v
) : var_(v
) {}
32 ScopedPPVar::ScopedPPVar(const ScopedPPVar
& other
) : var_(other
.var_
) {
36 ScopedPPVar::~ScopedPPVar() { CallRelease(var_
); }
38 ScopedPPVar
& ScopedPPVar::operator=(const PP_Var
& v
) {
45 PP_Var
ScopedPPVar::Release() {
47 var_
= PP_MakeUndefined();
51 ScopedPPVarArray::ScopedPPVarArray(const PassPPBMemoryAllocatedArray
&,
57 ScopedPPVarArray::ScopedPPVarArray(size_t size
)
60 array_
= static_cast<PP_Var
*>(
61 thunk::GetPPB_Memory_Dev_0_1_Thunk()->MemAlloc(
62 static_cast<uint32_t>(sizeof(PP_Var
) * size
)));
64 for (size_t i
= 0; i
< size_
; ++i
)
65 array_
[i
] = PP_MakeUndefined();
68 ScopedPPVarArray::~ScopedPPVarArray() {
69 for (size_t i
= 0; i
< size_
; ++i
)
70 CallRelease(array_
[i
]);
72 thunk::GetPPB_Memory_Dev_0_1_Thunk()->MemFree(array_
);
76 PP_Var
* ScopedPPVarArray::Release(const PassPPBMemoryAllocatedArray
&) {
77 PP_Var
* result
= array_
;
83 void ScopedPPVarArray::Set(size_t index
, const ScopedPPVar
& var
) {
84 DCHECK(index
< size_
);
85 CallAddRef(var
.get());
86 CallRelease(array_
[index
]);
87 array_
[index
] = var
.get();