• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cpp/var_array_buffer.h"
6 
7 #include <limits>
8 
9 #include "ppapi/c/ppb_var_array_buffer.h"
10 #include "ppapi/cpp/logging.h"
11 #include "ppapi/cpp/module_impl.h"
12 
13 namespace pp {
14 
15 namespace {
16 
interface_name()17 template <> const char* interface_name<PPB_VarArrayBuffer_1_0>() {
18   return PPB_VAR_ARRAY_BUFFER_INTERFACE_1_0;
19 }
20 
21 }  // namespace
22 
VarArrayBuffer()23 VarArrayBuffer::VarArrayBuffer() {
24   ConstructWithSize(0);
25 }
26 
VarArrayBuffer(const Var & var)27 VarArrayBuffer::VarArrayBuffer(const Var& var) : Var(var) {
28   if (!var.is_array_buffer()) {
29     PP_NOTREACHED();
30     var_ = PP_MakeNull();
31   }
32 }
33 
VarArrayBuffer(uint32_t size_in_bytes)34 VarArrayBuffer::VarArrayBuffer(uint32_t size_in_bytes) {
35   ConstructWithSize(size_in_bytes);
36 }
37 
operator =(const VarArrayBuffer & other)38 pp::VarArrayBuffer& VarArrayBuffer::operator=(const VarArrayBuffer& other) {
39   Var::operator=(other);
40   return *this;
41 }
42 
operator =(const Var & other)43 pp::Var& VarArrayBuffer::operator=(const Var& other) {
44   if (other.is_array_buffer()) {
45     return Var::operator=(other);
46   } else {
47     PP_NOTREACHED();
48     return *this;
49   }
50 }
51 
ByteLength() const52 uint32_t VarArrayBuffer::ByteLength() const {
53   uint32_t byte_length = std::numeric_limits<uint32_t>::max();
54   if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
55     get_interface<PPB_VarArrayBuffer_1_0>()->ByteLength(var_, &byte_length);
56   else
57     PP_NOTREACHED();
58   return byte_length;
59 }
60 
Map()61 void* VarArrayBuffer::Map() {
62   if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
63     return get_interface<PPB_VarArrayBuffer_1_0>()->Map(var_);
64   PP_NOTREACHED();
65   return NULL;
66 }
67 
Unmap()68 void VarArrayBuffer::Unmap() {
69   if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_1_0>())
70     get_interface<PPB_VarArrayBuffer_1_0>()->Unmap(var_);
71   else
72     PP_NOTREACHED();
73 }
74 
75 
ConstructWithSize(uint32_t size_in_bytes)76 void VarArrayBuffer::ConstructWithSize(uint32_t size_in_bytes) {
77   PP_DCHECK(is_undefined());
78 
79   if (has_interface<PPB_VarArrayBuffer_1_0>()) {
80     var_ = get_interface<PPB_VarArrayBuffer_1_0>()->Create(size_in_bytes);
81   } else {
82     PP_NOTREACHED();
83     var_ = PP_MakeNull();
84   }
85   is_managed_ = true;
86 }
87 
88 }  // namespace pp
89