| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/var.h" | 5 #include "ppapi/cpp/var.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
| 13 #ifndef PPAPI_VAR_REMOVE_SCRIPTING | |
| 14 # include "ppapi/c/dev/ppb_var_deprecated.h" | |
| 15 #endif | |
| 16 #include "ppapi/c/ppb_var.h" | 13 #include "ppapi/c/ppb_var.h" |
| 17 #include "ppapi/cpp/instance.h" | 14 #include "ppapi/cpp/instance.h" |
| 18 #include "ppapi/cpp/logging.h" | 15 #include "ppapi/cpp/logging.h" |
| 19 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
| 20 #include "ppapi/cpp/module_impl.h" | 17 #include "ppapi/cpp/module_impl.h" |
| 21 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | |
| 22 | 18 |
| 23 // Define equivalent to snprintf on Windows. | 19 // Define equivalent to snprintf on Windows. |
| 24 #if defined(_MSC_VER) | 20 #if defined(_MSC_VER) |
| 25 # define snprintf sprintf_s | 21 # define snprintf sprintf_s |
| 26 #endif | 22 #endif |
| 27 | 23 |
| 28 namespace pp { | 24 namespace pp { |
| 29 | 25 |
| 30 namespace { | 26 namespace { |
| 31 | 27 |
| 32 template <> const char* interface_name<PPB_Var>() { | 28 template <> const char* interface_name<PPB_Var>() { |
| 33 return PPB_VAR_INTERFACE; | 29 return PPB_VAR_INTERFACE; |
| 34 } | 30 } |
| 35 | 31 |
| 36 #ifdef PPAPI_VAR_REMOVE_SCRIPTING | |
| 37 typedef PPB_Var PPB_Var_Interface_Type; | |
| 38 #else | |
| 39 typedef PPB_Var_Deprecated PPB_Var_Interface_Type; | |
| 40 | |
| 41 template <> const char* interface_name<PPB_Var_Deprecated>() { | |
| 42 return PPB_VAR_DEPRECATED_INTERFACE; | |
| 43 } | |
| 44 #endif | |
| 45 | |
| 46 // Technically you can call AddRef and Release on any Var, but it may involve | 32 // Technically you can call AddRef and Release on any Var, but it may involve |
| 47 // cross-process calls depending on the plugin. This is an optimization so we | 33 // cross-process calls depending on the plugin. This is an optimization so we |
| 48 // only do refcounting on the necessary objects. | 34 // only do refcounting on the necessary objects. |
| 49 inline bool NeedsRefcounting(const PP_Var& var) { | 35 inline bool NeedsRefcounting(const PP_Var& var) { |
| 50 return var.type > PP_VARTYPE_DOUBLE; | 36 return var.type > PP_VARTYPE_DOUBLE; |
| 51 } | 37 } |
| 52 | 38 |
| 53 } // namespace | 39 } // namespace |
| 54 | 40 |
| 55 using namespace deprecated; | |
| 56 | |
| 57 Var::Var() { | 41 Var::Var() { |
| 58 memset(&var_, 0, sizeof(var_)); | 42 memset(&var_, 0, sizeof(var_)); |
| 59 var_.type = PP_VARTYPE_UNDEFINED; | 43 var_.type = PP_VARTYPE_UNDEFINED; |
| 60 needs_release_ = false; | 44 needs_release_ = false; |
| 61 } | 45 } |
| 62 | 46 |
| 63 Var::Var(Null) { | 47 Var::Var(Null) { |
| 64 memset(&var_, 0, sizeof(var_)); | 48 memset(&var_, 0, sizeof(var_)); |
| 65 var_.type = PP_VARTYPE_NULL; | 49 var_.type = PP_VARTYPE_NULL; |
| 66 needs_release_ = false; | 50 needs_release_ = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 81 } | 65 } |
| 82 | 66 |
| 83 Var::Var(double d) { | 67 Var::Var(double d) { |
| 84 var_.type = PP_VARTYPE_DOUBLE; | 68 var_.type = PP_VARTYPE_DOUBLE; |
| 85 var_.padding = 0; | 69 var_.padding = 0; |
| 86 var_.value.as_double = d; | 70 var_.value.as_double = d; |
| 87 needs_release_ = false; | 71 needs_release_ = false; |
| 88 } | 72 } |
| 89 | 73 |
| 90 Var::Var(const char* utf8_str) { | 74 Var::Var(const char* utf8_str) { |
| 91 if (has_interface<PPB_Var_Interface_Type>()) { | 75 if (has_interface<PPB_Var>()) { |
| 92 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; | 76 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; |
| 93 var_ = get_interface<PPB_Var_Interface_Type>()->VarFromUtf8( | 77 var_ = get_interface<PPB_Var>()->VarFromUtf8(Module::Get()->pp_module(), |
| 94 Module::Get()->pp_module(), utf8_str, len); | 78 utf8_str, len); |
| 95 } else { | 79 } else { |
| 96 var_.type = PP_VARTYPE_NULL; | 80 var_.type = PP_VARTYPE_NULL; |
| 97 var_.padding = 0; | 81 var_.padding = 0; |
| 98 } | 82 } |
| 99 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 83 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
| 100 } | 84 } |
| 101 | 85 |
| 102 Var::Var(const std::string& utf8_str) { | 86 Var::Var(const std::string& utf8_str) { |
| 103 if (has_interface<PPB_Var_Interface_Type>()) { | 87 if (has_interface<PPB_Var>()) { |
| 104 var_ = get_interface<PPB_Var_Interface_Type>()->VarFromUtf8( | 88 var_ = get_interface<PPB_Var>()->VarFromUtf8( |
| 105 Module::Get()->pp_module(), | 89 Module::Get()->pp_module(), |
| 106 utf8_str.c_str(), | 90 utf8_str.c_str(), |
| 107 static_cast<uint32_t>(utf8_str.size())); | 91 static_cast<uint32_t>(utf8_str.size())); |
| 108 } else { | 92 } else { |
| 109 var_.type = PP_VARTYPE_NULL; | 93 var_.type = PP_VARTYPE_NULL; |
| 110 var_.padding = 0; | 94 var_.padding = 0; |
| 111 } | 95 } |
| 112 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 96 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
| 113 } | 97 } |
| 114 | 98 |
| 115 Var::Var(const Var& other) { | 99 Var::Var(const Var& other) { |
| 116 var_ = other.var_; | 100 var_ = other.var_; |
| 117 if (NeedsRefcounting(var_)) { | 101 if (NeedsRefcounting(var_)) { |
| 118 if (has_interface<PPB_Var_Interface_Type>()) { | 102 if (has_interface<PPB_Var>()) { |
| 119 needs_release_ = true; | 103 needs_release_ = true; |
| 120 get_interface<PPB_Var_Interface_Type>()->AddRef(var_); | 104 get_interface<PPB_Var>()->AddRef(var_); |
| 121 } else { | 105 } else { |
| 122 var_.type = PP_VARTYPE_NULL; | 106 var_.type = PP_VARTYPE_NULL; |
| 123 needs_release_ = false; | 107 needs_release_ = false; |
| 124 } | 108 } |
| 125 } else { | 109 } else { |
| 126 needs_release_ = false; | 110 needs_release_ = false; |
| 127 } | 111 } |
| 128 } | 112 } |
| 129 | 113 |
| 130 Var::~Var() { | 114 Var::~Var() { |
| 131 if (needs_release_ && has_interface<PPB_Var_Interface_Type>()) | 115 if (needs_release_ && has_interface<PPB_Var>()) |
| 132 get_interface<PPB_Var_Interface_Type>()->Release(var_); | 116 get_interface<PPB_Var>()->Release(var_); |
| 133 } | 117 } |
| 134 | 118 |
| 135 Var& Var::operator=(const Var& other) { | 119 Var& Var::operator=(const Var& other) { |
| 136 if (needs_release_ && has_interface<PPB_Var_Interface_Type>()) | 120 if (needs_release_ && has_interface<PPB_Var>()) |
| 137 get_interface<PPB_Var_Interface_Type>()->Release(var_); | 121 get_interface<PPB_Var>()->Release(var_); |
| 138 var_ = other.var_; | 122 var_ = other.var_; |
| 139 if (NeedsRefcounting(var_)) { | 123 if (NeedsRefcounting(var_)) { |
| 140 if (has_interface<PPB_Var_Interface_Type>()) { | 124 if (has_interface<PPB_Var>()) { |
| 141 needs_release_ = true; | 125 needs_release_ = true; |
| 142 get_interface<PPB_Var_Interface_Type>()->AddRef(var_); | 126 get_interface<PPB_Var>()->AddRef(var_); |
| 143 } else { | 127 } else { |
| 144 var_.type = PP_VARTYPE_NULL; | 128 var_.type = PP_VARTYPE_NULL; |
| 145 needs_release_ = false; | 129 needs_release_ = false; |
| 146 } | 130 } |
| 147 } else { | 131 } else { |
| 148 needs_release_ = false; | 132 needs_release_ = false; |
| 149 } | 133 } |
| 150 return *this; | 134 return *this; |
| 151 } | 135 } |
| 152 | 136 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 PP_NOTREACHED(); | 181 PP_NOTREACHED(); |
| 198 return 0.0; | 182 return 0.0; |
| 199 } | 183 } |
| 200 | 184 |
| 201 std::string Var::AsString() const { | 185 std::string Var::AsString() const { |
| 202 if (!is_string()) { | 186 if (!is_string()) { |
| 203 PP_NOTREACHED(); | 187 PP_NOTREACHED(); |
| 204 return std::string(); | 188 return std::string(); |
| 205 } | 189 } |
| 206 | 190 |
| 207 if (!has_interface<PPB_Var_Interface_Type>()) | 191 if (!has_interface<PPB_Var>()) |
| 208 return std::string(); | 192 return std::string(); |
| 209 uint32_t len; | 193 uint32_t len; |
| 210 const char* str = | 194 const char* str = get_interface<PPB_Var>()->VarToUtf8(var_, &len); |
| 211 get_interface<PPB_Var_Interface_Type>()->VarToUtf8(var_, &len); | |
| 212 return std::string(str, len); | 195 return std::string(str, len); |
| 213 } | 196 } |
| 214 | 197 |
| 215 #ifndef PPAPI_VAR_REMOVE_SCRIPTING | |
| 216 Var::Var(Instance* instance, ScriptableObject* object) { | |
| 217 if (has_interface<PPB_Var_Deprecated>()) { | |
| 218 var_ = get_interface<PPB_Var_Deprecated>()->CreateObject( | |
| 219 instance->pp_instance(), object->GetClass(), object); | |
| 220 needs_release_ = true; | |
| 221 } else { | |
| 222 var_.type = PP_VARTYPE_NULL; | |
| 223 var_.padding = 0; | |
| 224 needs_release_ = false; | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 ScriptableObject* Var::AsScriptableObject() const { | |
| 229 if (!is_object()) { | |
| 230 PP_NOTREACHED(); | |
| 231 } else if (has_interface<PPB_Var_Deprecated>()) { | |
| 232 void* object = NULL; | |
| 233 if (get_interface<PPB_Var_Deprecated>()->IsInstanceOf( | |
| 234 var_, ScriptableObject::GetClass(), &object)) { | |
| 235 return reinterpret_cast<ScriptableObject*>(object); | |
| 236 } | |
| 237 } | |
| 238 return NULL; | |
| 239 } | |
| 240 | |
| 241 bool Var::HasProperty(const Var& name, Var* exception) const { | |
| 242 if (!has_interface<PPB_Var_Deprecated>()) | |
| 243 return false; | |
| 244 return get_interface<PPB_Var_Deprecated>()->HasProperty( | |
| 245 var_, name.var_, OutException(exception).get()); | |
| 246 } | |
| 247 | |
| 248 bool Var::HasMethod(const Var& name, Var* exception) const { | |
| 249 if (!has_interface<PPB_Var_Deprecated>()) | |
| 250 return false; | |
| 251 return get_interface<PPB_Var_Deprecated>()->HasMethod( | |
| 252 var_, name.var_, OutException(exception).get()); | |
| 253 } | |
| 254 | |
| 255 Var Var::GetProperty(const Var& name, Var* exception) const { | |
| 256 if (!has_interface<PPB_Var_Deprecated>()) | |
| 257 return Var(); | |
| 258 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->GetProperty( | |
| 259 var_, name.var_, OutException(exception).get())); | |
| 260 } | |
| 261 | |
| 262 void Var::GetAllPropertyNames(std::vector<Var>* properties, | |
| 263 Var* exception) const { | |
| 264 if (!has_interface<PPB_Var_Deprecated>()) | |
| 265 return; | |
| 266 PP_Var* props = NULL; | |
| 267 uint32_t prop_count = 0; | |
| 268 get_interface<PPB_Var_Deprecated>()->GetAllPropertyNames( | |
| 269 var_, &prop_count, &props, OutException(exception).get()); | |
| 270 if (!prop_count) | |
| 271 return; | |
| 272 properties->resize(prop_count); | |
| 273 for (uint32_t i = 0; i < prop_count; ++i) { | |
| 274 Var temp(PassRef(), props[i]); | |
| 275 (*properties)[i] = temp; | |
| 276 } | |
| 277 Module::Get()->core()->MemFree(props); | |
| 278 } | |
| 279 | |
| 280 void Var::SetProperty(const Var& name, const Var& value, Var* exception) { | |
| 281 if (!has_interface<PPB_Var_Deprecated>()) | |
| 282 return; | |
| 283 get_interface<PPB_Var_Deprecated>()->SetProperty( | |
| 284 var_, name.var_, value.var_, OutException(exception).get()); | |
| 285 } | |
| 286 | |
| 287 void Var::RemoveProperty(const Var& name, Var* exception) { | |
| 288 if (!has_interface<PPB_Var_Deprecated>()) | |
| 289 return; | |
| 290 get_interface<PPB_Var_Deprecated>()->RemoveProperty( | |
| 291 var_, name.var_, OutException(exception).get()); | |
| 292 } | |
| 293 | |
| 294 Var Var::Call(const Var& method_name, uint32_t argc, Var* argv, | |
| 295 Var* exception) { | |
| 296 if (!has_interface<PPB_Var_Deprecated>()) | |
| 297 return Var(); | |
| 298 if (argc > 0) { | |
| 299 std::vector<PP_Var> args; | |
| 300 args.reserve(argc); | |
| 301 for (size_t i = 0; i < argc; i++) | |
| 302 args.push_back(argv[i].var_); | |
| 303 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 304 var_, method_name.var_, argc, &args[0], OutException(exception).get())); | |
| 305 } else { | |
| 306 // Don't try to get the address of a vector if it's empty. | |
| 307 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 308 var_, method_name.var_, 0, NULL, OutException(exception).get())); | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 Var Var::Construct(uint32_t argc, Var* argv, Var* exception) const { | |
| 313 if (!has_interface<PPB_Var_Deprecated>()) | |
| 314 return Var(); | |
| 315 if (argc > 0) { | |
| 316 std::vector<PP_Var> args; | |
| 317 args.reserve(argc); | |
| 318 for (size_t i = 0; i < argc; i++) | |
| 319 args.push_back(argv[i].var_); | |
| 320 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Construct( | |
| 321 var_, argc, &args[0], OutException(exception).get())); | |
| 322 } else { | |
| 323 // Don't try to get the address of a vector if it's empty. | |
| 324 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Construct( | |
| 325 var_, 0, NULL, OutException(exception).get())); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 Var Var::Call(const Var& method_name, Var* exception) { | |
| 330 if (!has_interface<PPB_Var_Deprecated>()) | |
| 331 return Var(); | |
| 332 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 333 var_, method_name.var_, 0, NULL, OutException(exception).get())); | |
| 334 } | |
| 335 | |
| 336 Var Var::Call(const Var& method_name, const Var& arg1, Var* exception) { | |
| 337 if (!has_interface<PPB_Var_Deprecated>()) | |
| 338 return Var(); | |
| 339 PP_Var args[1] = {arg1.var_}; | |
| 340 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 341 var_, method_name.var_, 1, args, OutException(exception).get())); | |
| 342 } | |
| 343 | |
| 344 Var Var::Call(const Var& method_name, const Var& arg1, const Var& arg2, | |
| 345 Var* exception) { | |
| 346 if (!has_interface<PPB_Var_Deprecated>()) | |
| 347 return Var(); | |
| 348 PP_Var args[2] = {arg1.var_, arg2.var_}; | |
| 349 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 350 var_, method_name.var_, 2, args, OutException(exception).get())); | |
| 351 } | |
| 352 | |
| 353 Var Var::Call(const Var& method_name, const Var& arg1, const Var& arg2, | |
| 354 const Var& arg3, Var* exception) { | |
| 355 if (!has_interface<PPB_Var_Deprecated>()) | |
| 356 return Var(); | |
| 357 PP_Var args[3] = {arg1.var_, arg2.var_, arg3.var_}; | |
| 358 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 359 var_, method_name.var_, 3, args, OutException(exception).get())); | |
| 360 } | |
| 361 | |
| 362 Var Var::Call(const Var& method_name, const Var& arg1, const Var& arg2, | |
| 363 const Var& arg3, const Var& arg4, Var* exception) { | |
| 364 if (!has_interface<PPB_Var_Deprecated>()) | |
| 365 return Var(); | |
| 366 PP_Var args[4] = {arg1.var_, arg2.var_, arg3.var_, arg4.var_}; | |
| 367 return Var(PassRef(), get_interface<PPB_Var_Deprecated>()->Call( | |
| 368 var_, method_name.var_, 4, args, OutException(exception).get())); | |
| 369 } | |
| 370 #endif | |
| 371 | |
| 372 std::string Var::DebugString() const { | 198 std::string Var::DebugString() const { |
| 373 char buf[256]; | 199 char buf[256]; |
| 374 if (is_undefined()) { | 200 if (is_undefined()) { |
| 375 snprintf(buf, sizeof(buf), "Var<UNDEFINED>"); | 201 snprintf(buf, sizeof(buf), "Var<UNDEFINED>"); |
| 376 } else if (is_null()) { | 202 } else if (is_null()) { |
| 377 snprintf(buf, sizeof(buf), "Var<NULL>"); | 203 snprintf(buf, sizeof(buf), "Var<NULL>"); |
| 378 } else if (is_bool()) { | 204 } else if (is_bool()) { |
| 379 snprintf(buf, sizeof(buf), AsBool() ? "Var<true>" : "Var<false>"); | 205 snprintf(buf, sizeof(buf), AsBool() ? "Var<true>" : "Var<false>"); |
| 380 } else if (is_int()) { | 206 } else if (is_int()) { |
| 381 // Note that the following static_cast is necessary because | 207 // Note that the following static_cast is necessary because |
| (...skipping 12 matching lines...) Expand all Loading... |
| 394 str.append("..."); | 220 str.append("..."); |
| 395 } | 221 } |
| 396 snprintf(buf, sizeof(buf), format, str.c_str()); | 222 snprintf(buf, sizeof(buf), format, str.c_str()); |
| 397 } else if (is_object()) { | 223 } else if (is_object()) { |
| 398 snprintf(buf, sizeof(buf), "Var<OBJECT>"); | 224 snprintf(buf, sizeof(buf), "Var<OBJECT>"); |
| 399 } | 225 } |
| 400 return buf; | 226 return buf; |
| 401 } | 227 } |
| 402 | 228 |
| 403 } // namespace pp | 229 } // namespace pp |
| OLD | NEW |