Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / shared_impl / scoped_pp_var.cc
bloba6d1aa34997713d08504b24b6b5e962444496967
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"
12 namespace ppapi {
14 namespace {
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);
24 } // namespace
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_) {
33 CallAddRef(var_);
36 ScopedPPVar::~ScopedPPVar() { CallRelease(var_); }
38 ScopedPPVar& ScopedPPVar::operator=(const PP_Var& v) {
39 CallAddRef(v);
40 CallRelease(var_);
41 var_ = v;
42 return *this;
45 PP_Var ScopedPPVar::Release() {
46 PP_Var result = var_;
47 var_ = PP_MakeUndefined();
48 return result;
51 ScopedPPVarArray::ScopedPPVarArray(const PassPPBMemoryAllocatedArray&,
52 PP_Var* array,
53 size_t size)
54 : array_(array),
55 size_(size) {}
57 ScopedPPVarArray::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)));
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]);
71 if (size_ > 0)
72 thunk::GetPPB_Memory_Dev_0_1_Thunk()->MemFree(array_);
76 PP_Var* ScopedPPVarArray::Release(const PassPPBMemoryAllocatedArray&) {
77 PP_Var* result = array_;
78 array_ = NULL;
79 size_ = 0;
80 return result;
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();
90 } // namespace ppapi