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. 4 5 #include "ppapi/shared_impl/scoped_pp_var.h" 6 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" 11 12 namespace ppapi { 13 14 namespace { 15 CallAddRef(const PP_Var & v)16void CallAddRef(const PP_Var& v) { 17 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(v); 18 } 19 CallRelease(const PP_Var & v)20void CallRelease(const PP_Var& v) { 21 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(v); 22 } 23 24 } // namespace 25 ScopedPPVar()26ScopedPPVar::ScopedPPVar() : var_(PP_MakeUndefined()) {} 27 ScopedPPVar(const PP_Var & v)28ScopedPPVar::ScopedPPVar(const PP_Var& v) : var_(v) { CallAddRef(var_); } 29 ScopedPPVar(const PassRef &,const PP_Var & v)30ScopedPPVar::ScopedPPVar(const PassRef&, const PP_Var& v) : var_(v) {} 31 ScopedPPVar(const ScopedPPVar & other)32ScopedPPVar::ScopedPPVar(const ScopedPPVar& other) : var_(other.var_) { 33 CallAddRef(var_); 34 } 35 ~ScopedPPVar()36ScopedPPVar::~ScopedPPVar() { CallRelease(var_); } 37 operator =(const PP_Var & v)38ScopedPPVar& ScopedPPVar::operator=(const PP_Var& v) { 39 CallAddRef(v); 40 CallRelease(var_); 41 var_ = v; 42 return *this; 43 } 44 Release()45PP_Var ScopedPPVar::Release() { 46 PP_Var result = var_; 47 var_ = PP_MakeUndefined(); 48 return result; 49 } 50 ScopedPPVarArray(const PassPPBMemoryAllocatedArray &,PP_Var * array,size_t size)51ScopedPPVarArray::ScopedPPVarArray(const PassPPBMemoryAllocatedArray&, 52 PP_Var* array, 53 size_t size) 54 : array_(array), 55 size_(size) {} 56 ScopedPPVarArray(size_t size)57ScopedPPVarArray::ScopedPPVarArray(size_t size) 58 : size_(size) { 59 if (size > 0) { 60 array_ = static_cast<PP_Var*>( 61 thunk::GetPPB_Memory_Dev_0_1_Thunk()->MemAlloc( 62 static_cast<uint32_t>(sizeof(PP_Var) * size))); 63 } 64 for (size_t i = 0; i < size_; ++i) 65 array_[i] = PP_MakeUndefined(); 66 } 67 ~ScopedPPVarArray()68ScopedPPVarArray::~ScopedPPVarArray() { 69 for (size_t i = 0; i < size_; ++i) 70 CallRelease(array_[i]); 71 if (size_ > 0) 72 thunk::GetPPB_Memory_Dev_0_1_Thunk()->MemFree(array_); 73 74 } 75 Release(const PassPPBMemoryAllocatedArray &)76PP_Var* ScopedPPVarArray::Release(const PassPPBMemoryAllocatedArray&) { 77 PP_Var* result = array_; 78 array_ = NULL; 79 size_ = 0; 80 return result; 81 } 82 Set(size_t index,const ScopedPPVar & var)83void 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(); 88 } 89 90 } // namespace ppapi 91