1 /* 2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2006 Alexey Proskuryakov (ap (at) 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 27 #include "config.h" 28 29 #if ENABLE(NETSCAPE_PLUGIN_API) 30 31 #include "c_utility.h" 32 33 #include "JSDOMWindow.h" 34 #include "NP_jsobject.h" 35 #include "c_instance.h" 36 #include <runtime/JSGlobalObject.h> 37 #include <runtime/JSLock.h> 38 #include "PlatformString.h" 39 #include "npruntime_impl.h" 40 #include "npruntime_priv.h" 41 #include "runtime_object.h" 42 #include "runtime_root.h" 43 #include <wtf/Assertions.h> 44 45 using WebCore::String; 46 47 namespace JSC { namespace Bindings { 48 49 static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length) 50 { 51 ASSERT(UTF8Chars || UTF8Length == 0); 52 53 if (UTF8Length == -1) 54 UTF8Length = static_cast<int>(strlen(UTF8Chars)); 55 56 String result = String::fromUTF8(UTF8Chars, UTF8Length); 57 58 // If we got back a null string indicating an unsuccessful conversion, fall back to latin 1. 59 // Some plugins return invalid UTF-8 in NPVariantType_String, see <http://bugs.webkit.org/show_bug.cgi?id=5163> 60 // There is no "bad data" for latin1. It is unlikely that the plugin was really sending text in this encoding, 61 // but it should have used UTF-8, and now we are simply avoiding a crash. 62 if (result.isNull()) 63 result = String(UTF8Chars, UTF8Length); 64 65 return result; 66 } 67 68 // Variant value must be released with NPReleaseVariantValue() 69 void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result) 70 { 71 JSLock lock(SilenceAssertionsOnly); 72 73 VOID_TO_NPVARIANT(*result); 74 75 if (value.isString()) { 76 UString ustring = value.toString(exec); 77 CString cstring = ustring.UTF8String(); 78 NPString string = { (const NPUTF8*)cstring.c_str(), static_cast<uint32_t>(cstring.size()) }; 79 NPN_InitializeVariantWithStringCopy(result, &string); 80 } else if (value.isNumber()) { 81 DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result); 82 } else if (value.isBoolean()) { 83 BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result); 84 } else if (value.isNull()) { 85 NULL_TO_NPVARIANT(*result); 86 } else if (value.isObject()) { 87 JSObject* object = asObject(value); 88 if (object->classInfo() == &RuntimeObjectImp::s_info) { 89 RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(object); 90 CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance()); 91 if (instance) { 92 NPObject* obj = instance->getObject(); 93 _NPN_RetainObject(obj); 94 OBJECT_TO_NPVARIANT(obj, *result); 95 } 96 } else { 97 JSGlobalObject* globalObject = exec->dynamicGlobalObject(); 98 99 RootObject* rootObject = findRootObject(globalObject); 100 if (rootObject) { 101 NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject); 102 OBJECT_TO_NPVARIANT(npObject, *result); 103 } 104 } 105 } 106 } 107 108 JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootObject* rootObject) 109 { 110 JSLock lock(SilenceAssertionsOnly); 111 112 NPVariantType type = variant->type; 113 114 if (type == NPVariantType_Bool) 115 return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant)); 116 if (type == NPVariantType_Null) 117 return jsNull(); 118 if (type == NPVariantType_Void) 119 return jsUndefined(); 120 if (type == NPVariantType_Int32) 121 return jsNumber(exec, NPVARIANT_TO_INT32(*variant)); 122 if (type == NPVariantType_Double) 123 return jsNumber(exec, NPVARIANT_TO_DOUBLE(*variant)); 124 if (type == NPVariantType_String) 125 return jsString(exec, convertNPStringToUTF16(&variant->value.stringValue)); 126 if (type == NPVariantType_Object) { 127 NPObject* obj = variant->value.objectValue; 128 129 if (obj->_class == NPScriptObjectClass) 130 // Get JSObject from NP_JavaScriptObject. 131 return ((JavaScriptObject*)obj)->imp; 132 133 // Wrap NPObject in a CInstance. 134 return CInstance::create(obj, rootObject)->createRuntimeObject(exec); 135 } 136 137 return jsUndefined(); 138 } 139 140 String convertNPStringToUTF16(const NPString* string) 141 { 142 return String::fromUTF8WithLatin1Fallback(string->UTF8Characters, string->UTF8Length); 143 } 144 145 Identifier identifierFromNPIdentifier(const NPUTF8* name) 146 { 147 return Identifier(WebCore::JSDOMWindow::commonJSGlobalData(), convertUTF8ToUTF16WithLatin1Fallback(name, -1)); 148 } 149 150 } } 151 152 #endif // ENABLE(NETSCAPE_PLUGIN_API) 153