1// Copyright (c) 2013 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/array_var.h" 6 7#include <limits> 8 9#include "base/logging.h" 10#include "base/memory/ref_counted.h" 11#include "ppapi/shared_impl/ppapi_globals.h" 12#include "ppapi/shared_impl/var_tracker.h" 13 14namespace ppapi { 15 16ArrayVar::ArrayVar() {} 17 18ArrayVar::~ArrayVar() {} 19 20// static 21ArrayVar* ArrayVar::FromPPVar(const PP_Var& var) { 22 if (var.type != PP_VARTYPE_ARRAY) 23 return NULL; 24 25 scoped_refptr<Var> var_object( 26 PpapiGlobals::Get()->GetVarTracker()->GetVar(var)); 27 if (!var_object.get()) 28 return NULL; 29 return var_object->AsArrayVar(); 30} 31 32ArrayVar* ArrayVar::AsArrayVar() { return this; } 33 34PP_VarType ArrayVar::GetType() const { return PP_VARTYPE_ARRAY; } 35 36PP_Var ArrayVar::Get(uint32_t index) const { 37 if (index >= elements_.size()) 38 return PP_MakeUndefined(); 39 40 const PP_Var& element = elements_[index].get(); 41 if (PpapiGlobals::Get()->GetVarTracker()->AddRefVar(element)) 42 return element; 43 else 44 return PP_MakeUndefined(); 45} 46 47PP_Bool ArrayVar::Set(uint32_t index, const PP_Var& value) { 48 if (index == std::numeric_limits<uint32_t>::max()) 49 return PP_FALSE; 50 51 if (index >= elements_.size()) { 52 // Insert ScopedPPVars of type PP_VARTYPE_UNDEFINED to reach the new size 53 // (index + 1). 54 elements_.resize(index + 1); 55 } 56 57 elements_[index] = value; 58 return PP_TRUE; 59} 60 61uint32_t ArrayVar::GetLength() const { 62 if (elements_.size() > std::numeric_limits<uint32_t>::max()) { 63 CHECK(false); 64 return 0; 65 } 66 67 return static_cast<uint32_t>(elements_.size()); 68} 69 70PP_Bool ArrayVar::SetLength(uint32_t length) { 71 // If |length| is larger than the current size, ScopedPPVars of type 72 // PP_VARTYPE_UNDEFINED will be inserted to reach the new length. 73 elements_.resize(length); 74 return PP_TRUE; 75} 76 77} // namespace ppapi 78