blob: d256fc16c1477a77399ba8490060ef841756fdc9 [file] [log] [blame]
[email protected]256513872012-01-05 15:41:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0925622c2011-06-08 20:22:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d989cce2011-12-08 04:51:145#include "ppapi/shared_impl/ppb_var_shared.h"
[email protected]0925622c2011-06-08 20:22:026
[email protected]872caf562011-12-07 22:50:437#include <limits>
8
[email protected]0925622c2011-06-08 20:22:029#include "ppapi/c/ppb_var.h"
[email protected]9c6e0de2012-01-27 04:55:5510#include "ppapi/c/ppb_var_array_buffer.h"
[email protected]872caf562011-12-07 22:50:4311#include "ppapi/c/pp_var.h"
[email protected]794d83cd2011-10-20 19:09:2012#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]67c125d2011-10-11 18:58:2213#include "ppapi/shared_impl/proxy_lock.h"
[email protected]4d5a4e02014-02-13 14:50:0014#include "ppapi/shared_impl/resource_tracker.h"
15#include "ppapi/shared_impl/resource_var.h"
[email protected]2bbd2c672011-08-09 23:14:1316#include "ppapi/shared_impl/var.h"
[email protected]794d83cd2011-10-20 19:09:2017#include "ppapi/shared_impl/var_tracker.h"
[email protected]2bbd2c672011-08-09 23:14:1318
[email protected]872caf562011-12-07 22:50:4319using ppapi::PpapiGlobals;
20using ppapi::StringVar;
21
[email protected]4d2efd22011-08-18 21:58:0222namespace ppapi {
[email protected]8cc26a42011-12-15 21:22:3123namespace {
24
[email protected]8cc26a42011-12-15 21:22:3125// PPB_Var methods -------------------------------------------------------------
[email protected]0925622c2011-06-08 20:22:0226
27void AddRefVar(PP_Var var) {
[email protected]712835ff2013-02-26 04:54:2228 ProxyAutoLock lock;
[email protected]794d83cd2011-10-20 19:09:2029 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var);
[email protected]0925622c2011-06-08 20:22:0230}
31
32void ReleaseVar(PP_Var var) {
[email protected]712835ff2013-02-26 04:54:2233 ProxyAutoLock lock;
[email protected]794d83cd2011-10-20 19:09:2034 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var);
[email protected]0925622c2011-06-08 20:22:0235}
36
[email protected]872caf562011-12-07 22:50:4337PP_Var VarFromUtf8(const char* data, uint32_t len) {
[email protected]712835ff2013-02-26 04:54:2238 ProxyAutoLock lock;
[email protected]872caf562011-12-07 22:50:4339 return StringVar::StringToPPVar(data, len);
40}
41
42PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) {
43 return VarFromUtf8(data, len);
[email protected]0925622c2011-06-08 20:22:0244}
45
46const char* VarToUtf8(PP_Var var, uint32_t* len) {
[email protected]712835ff2013-02-26 04:54:2247 ProxyAutoLock lock;
[email protected]28cfaed02011-08-22 22:15:5848 StringVar* str = StringVar::FromPPVar(var);
[email protected]0925622c2011-06-08 20:22:0249 if (str) {
[email protected]2bbd2c672011-08-09 23:14:1350 *len = static_cast<uint32_t>(str->value().size());
51 return str->value().c_str();
[email protected]0925622c2011-06-08 20:22:0252 }
53 *len = 0;
54 return NULL;
55}
56
[email protected]4d5a4e02014-02-13 14:50:0057PP_Resource VarToResource(PP_Var var) {
58 ProxyAutoLock lock;
59 ResourceVar* resource = ResourceVar::FromPPVar(var);
60 if (!resource)
61 return 0;
62 PP_Resource pp_resource = resource->GetPPResource();
63 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource);
64 return pp_resource;
65}
66
67PP_Var VarFromResource(PP_Resource resource) {
68 ProxyAutoLock lock;
69 return PpapiGlobals::Get()->GetVarTracker()->MakeResourcePPVar(resource);
70}
71
[email protected]665b5c542014-02-22 08:06:2672const PPB_Var var_interface = {&AddRefVar, &ReleaseVar, &VarFromUtf8,
73 &VarToUtf8, &VarToResource, &VarFromResource};
[email protected]4d5a4e02014-02-13 14:50:0074
[email protected]665b5c542014-02-22 08:06:2675const PPB_Var_1_1 var_interface1_1 = {&AddRefVar, &ReleaseVar,
76 &VarFromUtf8, &VarToUtf8};
[email protected]0925622c2011-06-08 20:22:0277
[email protected]665b5c542014-02-22 08:06:2678const PPB_Var_1_0 var_interface1_0 = {&AddRefVar, &ReleaseVar,
79 &VarFromUtf8_1_0, &VarToUtf8};
[email protected]8cc26a42011-12-15 21:22:3180
[email protected]9c6e0de2012-01-27 04:55:5581// PPB_VarArrayBuffer methods --------------------------------------------------
[email protected]8cc26a42011-12-15 21:22:3182
83PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) {
[email protected]712835ff2013-02-26 04:54:2284 ProxyAutoLock lock;
[email protected]8cc26a42011-12-15 21:22:3185 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
86 size_in_bytes);
87}
88
[email protected]47ef6142012-01-26 21:04:1089PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) {
[email protected]712835ff2013-02-26 04:54:2290 ProxyAutoLock lock;
[email protected]8cc26a42011-12-15 21:22:3191 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
92 if (!buffer)
[email protected]47ef6142012-01-26 21:04:1093 return PP_FALSE;
94 *byte_length = buffer->ByteLength();
95 return PP_TRUE;
[email protected]8cc26a42011-12-15 21:22:3196}
97
[email protected]256513872012-01-05 15:41:5298void* Map(PP_Var array) {
[email protected]712835ff2013-02-26 04:54:2299 ProxyAutoLock lock;
[email protected]8cc26a42011-12-15 21:22:31100 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
101 if (!buffer)
102 return NULL;
103 return buffer->Map();
104}
105
[email protected]47ef6142012-01-26 21:04:10106void Unmap(PP_Var array) {
[email protected]712835ff2013-02-26 04:54:22107 ProxyAutoLock lock;
[email protected]47ef6142012-01-26 21:04:10108 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
109 if (buffer)
110 buffer->Unmap();
111}
112
[email protected]9c6e0de2012-01-27 04:55:55113const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = {
[email protected]665b5c542014-02-22 08:06:26114 &CreateArrayBufferVar, &ByteLength, &Map, &Unmap};
[email protected]8cc26a42011-12-15 21:22:31115
[email protected]0925622c2011-06-08 20:22:02116} // namespace
117
[email protected]872caf562011-12-07 22:50:43118// static
[email protected]4d5a4e02014-02-13 14:50:00119const PPB_Var_1_2* PPB_Var_Shared::GetVarInterface1_2() {
[email protected]5c966022011-09-13 18:09:37120 return &var_interface;
[email protected]0925622c2011-06-08 20:22:02121}
122
[email protected]872caf562011-12-07 22:50:43123// static
[email protected]4d5a4e02014-02-13 14:50:00124const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() {
125 return &var_interface1_1;
126}
127
128// static
[email protected]d989cce2011-12-08 04:51:14129const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() {
[email protected]872caf562011-12-07 22:50:43130 return &var_interface1_0;
131}
132
[email protected]8cc26a42011-12-15 21:22:31133// static
[email protected]9c6e0de2012-01-27 04:55:55134const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() {
135 return &var_arraybuffer_interface;
[email protected]8cc26a42011-12-15 21:22:31136}
137
[email protected]4d2efd22011-08-18 21:58:02138} // namespace ppapi