summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/var_array_buffer.cc
blob: 358b2c90d716d34b70690354bb77b2b005e85354 (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) 2012 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/cpp/var_array_buffer.h"

#include <limits>

#include "ppapi/c/ppb_var_array_buffer.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module_impl.h"

namespace pp {

namespace {

template <> const char* interface_name<PPB_VarArrayBuffer_1_0>() {
  return PPB_VAR_ARRAY_BUFFER_INTERFACE_1_0;
}

}  // namespace

VarArrayBuffer::VarArrayBuffer(const Var& var) : Var(var) {
  if (!var.is_array_buffer()) {
    PP_NOTREACHED();
    var_ = PP_MakeNull();
  }
}

VarArrayBuffer::VarArrayBuffer(uint32_t size_in_bytes) {
  if (has_interface<PPB_VarArrayBuffer_1_0>()) {
    var_ = get_interface<PPB_VarArrayBuffer_1_0>()->Create(size_in_bytes);
    needs_release_ = true;
  } else {
    PP_NOTREACHED();
    var_ = PP_MakeNull();
  }
}

pp::VarArrayBuffer& VarArrayBuffer::operator=(const VarArrayBuffer& other) {
  Var::operator=(other);
  return *this;
}

pp::Var& VarArrayBuffer::operator=(const Var& other) {
  if (other.is_array_buffer()) {
    return Var::operator=(other);
  } else {
    PP_NOTREACHED();
    return *this;
  }
}

uint32_t VarArrayBuffer::ByteLength() const {
  uint32_t byte_length = std::numeric_limits<uint32_t>::max();
  if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
    get_interface<PPB_VarArrayBuffer_1_0>()->ByteLength(var_, &byte_length);
  else
    PP_NOTREACHED();
  return byte_length;
}

void* VarArrayBuffer::Map() {
  if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
    return get_interface<PPB_VarArrayBuffer_1_0>()->Map(var_);
  PP_NOTREACHED();
  return NULL;
}

void VarArrayBuffer::Unmap() {
  if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
    get_interface<PPB_VarArrayBuffer_1_0>()->Unmap(var_);
  else
    PP_NOTREACHED();
}

}  // namespace pp