summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/ppb_var_shared.cc
blob: d256fc16c1477a77399ba8490060ef841756fdc9 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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/shared_impl/ppb_var_shared.h"

#include <limits>

#include "ppapi/c/ppb_var.h"
#include "ppapi/c/ppb_var_array_buffer.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/resource_var.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/shared_impl/var_tracker.h"

using ppapi::PpapiGlobals;
using ppapi::StringVar;

namespace ppapi {
namespace {

// PPB_Var methods -------------------------------------------------------------

void AddRefVar(PP_Var var) {
  ProxyAutoLock lock;
  PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var);
}

void ReleaseVar(PP_Var var) {
  ProxyAutoLock lock;
  PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var);
}

PP_Var VarFromUtf8(const char* data, uint32_t len) {
  ProxyAutoLock lock;
  return StringVar::StringToPPVar(data, len);
}

PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) {
  return VarFromUtf8(data, len);
}

const char* VarToUtf8(PP_Var var, uint32_t* len) {
  ProxyAutoLock lock;
  StringVar* str = StringVar::FromPPVar(var);
  if (str) {
    *len = static_cast<uint32_t>(str->value().size());
    return str->value().c_str();
  }
  *len = 0;
  return NULL;
}

PP_Resource VarToResource(PP_Var var) {
  ProxyAutoLock lock;
  ResourceVar* resource = ResourceVar::FromPPVar(var);
  if (!resource)
    return 0;
  PP_Resource pp_resource = resource->GetPPResource();
  PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource);
  return pp_resource;
}

PP_Var VarFromResource(PP_Resource resource) {
  ProxyAutoLock lock;
  return PpapiGlobals::Get()->GetVarTracker()->MakeResourcePPVar(resource);
}

const PPB_Var var_interface = {&AddRefVar, &ReleaseVar,    &VarFromUtf8,
                               &VarToUtf8, &VarToResource, &VarFromResource};

const PPB_Var_1_1 var_interface1_1 = {&AddRefVar,   &ReleaseVar,
                                      &VarFromUtf8, &VarToUtf8};

const PPB_Var_1_0 var_interface1_0 = {&AddRefVar,       &ReleaseVar,
                                      &VarFromUtf8_1_0, &VarToUtf8};

// PPB_VarArrayBuffer methods --------------------------------------------------

PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) {
  ProxyAutoLock lock;
  return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
      size_in_bytes);
}

PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) {
  ProxyAutoLock lock;
  ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
  if (!buffer)
    return PP_FALSE;
  *byte_length = buffer->ByteLength();
  return PP_TRUE;
}

void* Map(PP_Var array) {
  ProxyAutoLock lock;
  ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
  if (!buffer)
    return NULL;
  return buffer->Map();
}

void Unmap(PP_Var array) {
  ProxyAutoLock lock;
  ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
  if (buffer)
    buffer->Unmap();
}

const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = {
    &CreateArrayBufferVar, &ByteLength, &Map, &Unmap};

}  // namespace

// static
const PPB_Var_1_2* PPB_Var_Shared::GetVarInterface1_2() {
  return &var_interface;
}

// static
const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() {
  return &var_interface1_1;
}

// static
const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() {
  return &var_interface1_0;
}

// static
const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() {
  return &var_arraybuffer_interface;
}

}  // namespace ppapi