summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/array_var.cc
blob: 6207379f5da0e2ce7415a30496111ed4d9c0acca (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
// Copyright (c) 2013 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 "ppapi/shared_impl/array_var.h"

#include <limits>

#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/var_tracker.h"

namespace ppapi {

ArrayVar::ArrayVar() {}

ArrayVar::~ArrayVar() {}

// static
ArrayVar* ArrayVar::FromPPVar(const PP_Var& var) {
  if (var.type != PP_VARTYPE_ARRAY)
    return NULL;

  scoped_refptr<Var> var_object(
      PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
  if (!var_object.get())
    return NULL;
  return var_object->AsArrayVar();
}

ArrayVar* ArrayVar::AsArrayVar() { return this; }

PP_VarType ArrayVar::GetType() const { return PP_VARTYPE_ARRAY; }

PP_Var ArrayVar::Get(uint32_t index) const {
  if (index >= elements_.size())
    return PP_MakeUndefined();

  const PP_Var& element = elements_[index].get();
  if (PpapiGlobals::Get()->GetVarTracker()->AddRefVar(element))
    return element;
  else
    return PP_MakeUndefined();
}

PP_Bool ArrayVar::Set(uint32_t index, const PP_Var& value) {
  if (index == std::numeric_limits<uint32_t>::max())
    return PP_FALSE;

  if (index >= elements_.size()) {
    // Insert ScopedPPVars of type PP_VARTYPE_UNDEFINED to reach the new size
    // (index + 1).
    elements_.resize(index + 1);
  }

  elements_[index] = value;
  return PP_TRUE;
}

uint32_t ArrayVar::GetLength() const {
  if (elements_.size() > std::numeric_limits<uint32_t>::max()) {
    CHECK(false);
    return 0;
  }

  return static_cast<uint32_t>(elements_.size());
}

PP_Bool ArrayVar::SetLength(uint32_t length) {
  // If |length| is larger than the current size, ScopedPPVars of type
  // PP_VARTYPE_UNDEFINED will be inserted to reach the new length.
  elements_.resize(length);
  return PP_TRUE;
}

}  // namespace ppapi