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