c_utility.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
  3. * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #if ENABLE(NETSCAPE_PLUGIN_API)
  28. #include "c_utility.h"
  29. #include "CRuntimeObject.h"
  30. #include "JSDOMBinding.h"
  31. #include "JSDOMWindow.h"
  32. #include "NP_jsobject.h"
  33. #include "c_instance.h"
  34. #include <runtime/JSGlobalObject.h>
  35. #include <runtime/JSLock.h>
  36. #include "npruntime_impl.h"
  37. #include "npruntime_priv.h"
  38. #include "runtime_object.h"
  39. #include "runtime_root.h"
  40. #include <wtf/Assertions.h>
  41. #include <wtf/text/WTFString.h>
  42. namespace JSC { namespace Bindings {
  43. static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length)
  44. {
  45. ASSERT(UTF8Chars || UTF8Length == 0);
  46. if (UTF8Length == -1)
  47. UTF8Length = static_cast<int>(strlen(UTF8Chars));
  48. String result = String::fromUTF8(UTF8Chars, UTF8Length);
  49. // If we got back a null string indicating an unsuccessful conversion, fall back to latin 1.
  50. // Some plugins return invalid UTF-8 in NPVariantType_String, see <http://bugs.webkit.org/show_bug.cgi?id=5163>
  51. // There is no "bad data" for latin1. It is unlikely that the plugin was really sending text in this encoding,
  52. // but it should have used UTF-8, and now we are simply avoiding a crash.
  53. if (result.isNull())
  54. result = String(UTF8Chars, UTF8Length);
  55. return result;
  56. }
  57. // Variant value must be released with NPReleaseVariantValue()
  58. void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result)
  59. {
  60. JSLockHolder lock(exec);
  61. VOID_TO_NPVARIANT(*result);
  62. if (value.isString()) {
  63. String ustring = value.toString(exec)->value(exec);
  64. CString cstring = ustring.utf8();
  65. NPString string = { (const NPUTF8*)cstring.data(), static_cast<uint32_t>(cstring.length()) };
  66. NPN_InitializeVariantWithStringCopy(result, &string);
  67. } else if (value.isNumber()) {
  68. DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result);
  69. } else if (value.isBoolean()) {
  70. BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result);
  71. } else if (value.isNull()) {
  72. NULL_TO_NPVARIANT(*result);
  73. } else if (value.isObject()) {
  74. JSObject* object = asObject(value);
  75. if (object->classInfo() == &CRuntimeObject::s_info) {
  76. CRuntimeObject* runtimeObject = static_cast<CRuntimeObject*>(object);
  77. CInstance* instance = runtimeObject->getInternalCInstance();
  78. if (instance) {
  79. NPObject* obj = instance->getObject();
  80. _NPN_RetainObject(obj);
  81. OBJECT_TO_NPVARIANT(obj, *result);
  82. }
  83. } else {
  84. JSGlobalObject* globalObject = exec->dynamicGlobalObject();
  85. RootObject* rootObject = findRootObject(globalObject);
  86. if (rootObject) {
  87. NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject);
  88. OBJECT_TO_NPVARIANT(npObject, *result);
  89. }
  90. }
  91. }
  92. }
  93. JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootObject* rootObject)
  94. {
  95. JSLockHolder lock(exec);
  96. NPVariantType type = variant->type;
  97. if (type == NPVariantType_Bool)
  98. return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant));
  99. if (type == NPVariantType_Null)
  100. return jsNull();
  101. if (type == NPVariantType_Void)
  102. return jsUndefined();
  103. if (type == NPVariantType_Int32)
  104. return jsNumber(NPVARIANT_TO_INT32(*variant));
  105. if (type == NPVariantType_Double)
  106. return jsNumber(NPVARIANT_TO_DOUBLE(*variant));
  107. if (type == NPVariantType_String)
  108. return WebCore::jsStringWithCache(exec, convertNPStringToUTF16(&variant->value.stringValue));
  109. if (type == NPVariantType_Object) {
  110. NPObject* obj = variant->value.objectValue;
  111. if (obj->_class == NPScriptObjectClass)
  112. // Get JSObject from NP_JavaScriptObject.
  113. return ((JavaScriptObject*)obj)->imp;
  114. // Wrap NPObject in a CInstance.
  115. return CInstance::create(obj, rootObject)->createRuntimeObject(exec);
  116. }
  117. return jsUndefined();
  118. }
  119. String convertNPStringToUTF16(const NPString* string)
  120. {
  121. return String::fromUTF8WithLatin1Fallback(string->UTF8Characters, string->UTF8Length);
  122. }
  123. Identifier identifierFromNPIdentifier(ExecState* exec, const NPUTF8* name)
  124. {
  125. return Identifier(exec, convertUTF8ToUTF16WithLatin1Fallback(name, -1));
  126. }
  127. } }
  128. #endif // ENABLE(NETSCAPE_PLUGIN_API)