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/shared_impl/ppb_var_shared.h" 6 7 #include <limits> 8 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/var.h" 15 #include "ppapi/shared_impl/var_tracker.h" 16 17 using ppapi::PpapiGlobals; 18 using ppapi::StringVar; 19 20 namespace ppapi { 21 namespace { 22 23 24 // PPB_Var methods ------------------------------------------------------------- 25 26 void AddRefVar(PP_Var var) { 27 ProxyAutoLock lock; 28 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var); 29 } 30 31 void ReleaseVar(PP_Var var) { 32 ProxyAutoLock lock; 33 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var); 34 } 35 36 PP_Var VarFromUtf8(const char* data, uint32_t len) { 37 ProxyAutoLock lock; 38 return StringVar::StringToPPVar(data, len); 39 } 40 41 PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) { 42 return VarFromUtf8(data, len); 43 } 44 45 const char* VarToUtf8(PP_Var var, uint32_t* len) { 46 ProxyAutoLock lock; 47 StringVar* str = StringVar::FromPPVar(var); 48 if (str) { 49 *len = static_cast<uint32_t>(str->value().size()); 50 return str->value().c_str(); 51 } 52 *len = 0; 53 return NULL; 54 } 55 56 const PPB_Var var_interface = { 57 &AddRefVar, 58 &ReleaseVar, 59 &VarFromUtf8, 60 &VarToUtf8 61 }; 62 63 const PPB_Var_1_0 var_interface1_0 = { 64 &AddRefVar, 65 &ReleaseVar, 66 &VarFromUtf8_1_0, 67 &VarToUtf8 68 }; 69 70 71 // PPB_VarArrayBuffer methods -------------------------------------------------- 72 73 PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) { 74 ProxyAutoLock lock; 75 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( 76 size_in_bytes); 77 } 78 79 PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) { 80 ProxyAutoLock lock; 81 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); 82 if (!buffer) 83 return PP_FALSE; 84 *byte_length = buffer->ByteLength(); 85 return PP_TRUE; 86 } 87 88 void* Map(PP_Var array) { 89 ProxyAutoLock lock; 90 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); 91 if (!buffer) 92 return NULL; 93 return buffer->Map(); 94 } 95 96 void Unmap(PP_Var array) { 97 ProxyAutoLock lock; 98 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); 99 if (buffer) 100 buffer->Unmap(); 101 } 102 103 const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = { 104 &CreateArrayBufferVar, 105 &ByteLength, 106 &Map, 107 &Unmap 108 }; 109 110 } // namespace 111 112 // static 113 const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() { 114 return &var_interface; 115 } 116 117 // static 118 const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() { 119 return &var_interface1_0; 120 } 121 122 // static 123 const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() { 124 return &var_arraybuffer_interface; 125 } 126 127 } // namespace ppapi 128