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"
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
;
25 // PPB_Var methods -------------------------------------------------------------
27 void AddRefVar(PP_Var var
) {
29 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var
);
32 void ReleaseVar(PP_Var var
) {
34 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var
);
37 PP_Var
VarFromUtf8(const char* data
, uint32_t len
) {
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
) {
48 StringVar
* str
= StringVar::FromPPVar(var
);
50 *len
= static_cast<uint32_t>(str
->value().size());
51 return str
->value().c_str();
57 PP_Resource
VarToResource(PP_Var var
) {
59 ResourceVar
* resource
= ResourceVar::FromPPVar(var
);
62 PP_Resource pp_resource
= resource
->GetPPResource();
63 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource
);
67 PP_Var
VarFromResource(PP_Resource resource
) {
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
) {
85 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
89 PP_Bool
ByteLength(PP_Var array
, uint32_t* byte_length
) {
91 ArrayBufferVar
* buffer
= ArrayBufferVar::FromPPVar(array
);
94 *byte_length
= buffer
->ByteLength();
98 void* Map(PP_Var array
) {
100 ArrayBufferVar
* buffer
= ArrayBufferVar::FromPPVar(array
);
103 return buffer
->Map();
106 void Unmap(PP_Var array
) {
108 ArrayBufferVar
* buffer
= ArrayBufferVar::FromPPVar(array
);
113 const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface
= {
114 &CreateArrayBufferVar
, &ByteLength
, &Map
, &Unmap
};
119 const PPB_Var_1_2
* PPB_Var_Shared::GetVarInterface1_2() {
120 return &var_interface
;
124 const PPB_Var_1_1
* PPB_Var_Shared::GetVarInterface1_1() {
125 return &var_interface1_1
;
129 const PPB_Var_1_0
* PPB_Var_Shared::GetVarInterface1_0() {
130 return &var_interface1_0
;
134 const PPB_VarArrayBuffer_1_0
* PPB_Var_Shared::GetVarArrayBufferInterface1_0() {
135 return &var_arraybuffer_interface
;