Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ppapi / shared_impl / ppb_var_shared.cc
blobd256fc16c1477a77399ba8490060ef841756fdc9
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.
5 #include "ppapi/shared_impl/ppb_var_shared.h"
7 #include <limits>
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/c/ppb_var_array_buffer.h"
11 #include "ppapi/c/pp_var.h"
12 #include "ppapi/shared_impl/ppapi_globals.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/resource_tracker.h"
15 #include "ppapi/shared_impl/resource_var.h"
16 #include "ppapi/shared_impl/var.h"
17 #include "ppapi/shared_impl/var_tracker.h"
19 using ppapi::PpapiGlobals;
20 using ppapi::StringVar;
22 namespace ppapi {
23 namespace {
25 // PPB_Var methods -------------------------------------------------------------
27 void AddRefVar(PP_Var var) {
28 ProxyAutoLock lock;
29 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var);
32 void ReleaseVar(PP_Var var) {
33 ProxyAutoLock lock;
34 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var);
37 PP_Var VarFromUtf8(const char* data, uint32_t len) {
38 ProxyAutoLock lock;
39 return StringVar::StringToPPVar(data, len);
42 PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) {
43 return VarFromUtf8(data, len);
46 const char* VarToUtf8(PP_Var var, uint32_t* len) {
47 ProxyAutoLock lock;
48 StringVar* str = StringVar::FromPPVar(var);
49 if (str) {
50 *len = static_cast<uint32_t>(str->value().size());
51 return str->value().c_str();
53 *len = 0;
54 return NULL;
57 PP_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;
67 PP_Var VarFromResource(PP_Resource resource) {
68 ProxyAutoLock lock;
69 return PpapiGlobals::Get()->GetVarTracker()->MakeResourcePPVar(resource);
72 const PPB_Var var_interface = {&AddRefVar, &ReleaseVar, &VarFromUtf8,
73 &VarToUtf8, &VarToResource, &VarFromResource};
75 const PPB_Var_1_1 var_interface1_1 = {&AddRefVar, &ReleaseVar,
76 &VarFromUtf8, &VarToUtf8};
78 const PPB_Var_1_0 var_interface1_0 = {&AddRefVar, &ReleaseVar,
79 &VarFromUtf8_1_0, &VarToUtf8};
81 // PPB_VarArrayBuffer methods --------------------------------------------------
83 PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) {
84 ProxyAutoLock lock;
85 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
86 size_in_bytes);
89 PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) {
90 ProxyAutoLock lock;
91 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
92 if (!buffer)
93 return PP_FALSE;
94 *byte_length = buffer->ByteLength();
95 return PP_TRUE;
98 void* Map(PP_Var array) {
99 ProxyAutoLock lock;
100 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
101 if (!buffer)
102 return NULL;
103 return buffer->Map();
106 void Unmap(PP_Var array) {
107 ProxyAutoLock lock;
108 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
109 if (buffer)
110 buffer->Unmap();
113 const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = {
114 &CreateArrayBufferVar, &ByteLength, &Map, &Unmap};
116 } // namespace
118 // static
119 const PPB_Var_1_2* PPB_Var_Shared::GetVarInterface1_2() {
120 return &var_interface;
123 // static
124 const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() {
125 return &var_interface1_1;
128 // static
129 const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() {
130 return &var_interface1_0;
133 // static
134 const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() {
135 return &var_arraybuffer_interface;
138 } // namespace ppapi