OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_var.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "native_client/src/include/checked_cast.h" |
| 13 #include "native_client/src/include/nacl_macros.h" |
| 14 #include "native_client/src/include/portability.h" |
| 15 #include "native_client/src/include/portability_io.h" |
| 16 #include "native_client/src/shared/ppapi_proxy/proxy_var_cache.h" |
| 17 #include "native_client/src/shared/ppapi_proxy/string_proxy_var.h" |
| 18 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 19 #include "ppapi/c/pp_var.h" |
| 20 #include "ppapi/c/ppb_var.h" |
| 21 |
| 22 namespace ppapi_proxy { |
| 23 |
| 24 namespace { |
| 25 |
| 26 void AddRef(PP_Var var) { |
| 27 DebugPrintf("PPB_Var::AddRef: var=PPB_Var(%s)\n", |
| 28 PluginVar::DebugString(var).c_str()); |
| 29 ProxyVarCache::GetInstance().RetainProxyVar(var); |
| 30 } |
| 31 |
| 32 void Release(PP_Var var) { |
| 33 DebugPrintf("PPB_Var::Release: var=PPB_Var(%s)\n", |
| 34 PluginVar::DebugString(var).c_str()); |
| 35 ProxyVarCache::GetInstance().ReleaseProxyVar(var); |
| 36 } |
| 37 |
| 38 PP_Var VarFromUtf8(PP_Module module_id, const char* data, uint32_t len) { |
| 39 DebugPrintf("PPB_Var::VarFromUtf8: data='%.*s'\n", len, data); |
| 40 UNREFERENCED_PARAMETER(module_id); |
| 41 if (!StringIsUtf8(data, len)) { |
| 42 DebugPrintf("PPB_Var::VarFromUtf8: not UTF8\n"); |
| 43 return PP_MakeNull(); |
| 44 } |
| 45 SharedProxyVar proxy_var(new StringProxyVar(data, len)); |
| 46 ProxyVarCache::GetInstance().RetainSharedProxyVar(proxy_var); |
| 47 PP_Var var; |
| 48 var.type = PP_VARTYPE_STRING; |
| 49 var.value.as_id = proxy_var->id(); |
| 50 // Increment the reference count for the return to the caller. |
| 51 AddRef(var); |
| 52 DebugPrintf("PPB_Var::VarFromUtf8: as_id=%"NACL_PRId64"\n", var.value.as_id); |
| 53 return var; |
| 54 } |
| 55 |
| 56 const char* VarToUtf8(PP_Var var, uint32_t* len) { |
| 57 DebugPrintf("PPB_Var::VarToUtf8: as_id=%"NACL_PRId64"\n", var.value.as_id); |
| 58 SharedStringProxyVar string_var = StringProxyVar::CastFromProxyVar( |
| 59 ProxyVarCache::GetInstance().SharedProxyVarForVar(var)); |
| 60 const char* data = NULL; |
| 61 if (string_var == NULL) { |
| 62 *len = 0; |
| 63 } else { |
| 64 *len = static_cast<uint32_t>(string_var->contents().size()); |
| 65 // Mimics PPAPI implementation: as long as SharedStringProxyVar is alive, |
| 66 // the return value can be validly used. |
| 67 data = string_var->contents().c_str(); |
| 68 } |
| 69 DebugPrintf("PPB_Var::VarToUtf8: data='%.*s'\n", *len, data); |
| 70 return data; |
| 71 } |
| 72 |
| 73 int64_t GetVarId(PP_Var var) { |
| 74 SharedProxyVar proxy_var = |
| 75 ProxyVarCache::GetInstance().SharedProxyVarForVar(var); |
| 76 if (proxy_var == NULL) { |
| 77 return -1; |
| 78 } else { |
| 79 return proxy_var->id(); |
| 80 } |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 const PPB_Var* PluginVar::GetInterface() { |
| 86 static const PPB_Var var_interface = { |
| 87 AddRef, |
| 88 Release, |
| 89 VarFromUtf8, |
| 90 VarToUtf8 |
| 91 }; |
| 92 return &var_interface; |
| 93 } |
| 94 |
| 95 std::string PluginVar::DebugString(const PP_Var& var) { |
| 96 switch (var.type) { |
| 97 case PP_VARTYPE_UNDEFINED: |
| 98 return "##UNDEFINED##"; |
| 99 case PP_VARTYPE_NULL: |
| 100 return "##NULL##"; |
| 101 case PP_VARTYPE_BOOL: |
| 102 return (var.value.as_bool ? "true" : "false"); |
| 103 case PP_VARTYPE_INT32: |
| 104 { |
| 105 char buf[32]; |
| 106 const size_t kBufSize = sizeof(buf); |
| 107 SNPRINTF(buf, kBufSize, "%d", static_cast<int>(var.value.as_int)); |
| 108 return buf; |
| 109 } |
| 110 case PP_VARTYPE_DOUBLE: |
| 111 { |
| 112 char buf[32]; |
| 113 const size_t kBufSize = sizeof(buf); |
| 114 SNPRINTF(buf, kBufSize, "%f", var.value.as_double); |
| 115 return buf; |
| 116 } |
| 117 case PP_VARTYPE_STRING: |
| 118 { |
| 119 uint32_t len; |
| 120 const char* data = VarToUtf8(var, &len); |
| 121 return std::string(data, len); |
| 122 } |
| 123 case PP_VARTYPE_OBJECT: |
| 124 { |
| 125 char buf[32]; |
| 126 const size_t kBufSize = sizeof(buf); |
| 127 SNPRINTF(buf, kBufSize, "%"NACL_PRIu64"", GetVarId(var)); |
| 128 return std::string("##OBJECT##") + buf + "##"; |
| 129 } |
| 130 case PP_VARTYPE_ARRAY: |
| 131 case PP_VARTYPE_DICTIONARY: |
| 132 NACL_NOTREACHED(); |
| 133 break; |
| 134 } |
| 135 ASSERT_MSG(0, "Unexpected type seen"); |
| 136 return "##ERROR##"; |
| 137 } |
| 138 |
| 139 PP_Var PluginVar::StringToPPVar(PP_Module module_id, const std::string& str) { |
| 140 static const PPB_Var* ppb_var = NULL; |
| 141 if (ppb_var == NULL) { |
| 142 ppb_var = static_cast<const PPB_Var*>( |
| 143 ppapi_proxy::PluginVar::GetInterface()); |
| 144 } |
| 145 if (ppb_var == NULL) { |
| 146 return PP_MakeUndefined(); |
| 147 } |
| 148 return ppb_var->VarFromUtf8( |
| 149 module_id, str.c_str(), nacl::assert_cast<uint32_t>(str.size())); |
| 150 } |
| 151 |
| 152 std::string PluginVar::PPVarToString(const PP_Var& var) { |
| 153 static const PPB_Var* ppb_var = NULL; |
| 154 if (ppb_var == NULL) { |
| 155 ppb_var = static_cast<const PPB_Var*>( |
| 156 ppapi_proxy::PluginVar::GetInterface()); |
| 157 } |
| 158 if (ppb_var == NULL || var.type != PP_VARTYPE_STRING) { |
| 159 return ""; |
| 160 } |
| 161 uint32_t len; |
| 162 return ppb_var->VarToUtf8(var, &len); |
| 163 } |
| 164 |
| 165 void PluginVar::Print(const PP_Var& var) { |
| 166 switch (var.type) { |
| 167 case PP_VARTYPE_UNDEFINED: |
| 168 DebugPrintf("PP_Var(undefined)"); |
| 169 break; |
| 170 case PP_VARTYPE_NULL: |
| 171 DebugPrintf("PP_Var(null)"); |
| 172 break; |
| 173 case PP_VARTYPE_BOOL: |
| 174 DebugPrintf("PP_Var(bool: %s)", var.value.as_bool ? "true" : "false"); |
| 175 break; |
| 176 case PP_VARTYPE_INT32: |
| 177 DebugPrintf("PP_Var(int32: %"NACL_PRId32")", var.value.as_int); |
| 178 break; |
| 179 case PP_VARTYPE_DOUBLE: |
| 180 DebugPrintf("PP_Var(double: %f)", var.value.as_double); |
| 181 break; |
| 182 case PP_VARTYPE_STRING: |
| 183 { |
| 184 std::string str = DebugString(var); |
| 185 DebugPrintf("PP_Var(string: '%*s')", |
| 186 static_cast<uint32_t>(str.size()), |
| 187 str.c_str()); |
| 188 } |
| 189 case PP_VARTYPE_OBJECT: |
| 190 DebugPrintf("PP_Var(object: %"NACL_PRIu64")", GetVarId(var)); |
| 191 break; |
| 192 case PP_VARTYPE_ARRAY: |
| 193 case PP_VARTYPE_DICTIONARY: |
| 194 NACL_NOTREACHED(); |
| 195 break; |
| 196 } |
| 197 } |
| 198 |
| 199 } // namespace ppapi_proxy |
OLD | NEW |