2 * Copyright (C) 2004, 2006, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #if ENABLE(NETSCAPE_PLUGIN_API)
31 #include "c_utility.h"
33 #include "CRuntimeObject.h"
34 #include "JSDOMBinding.h"
35 #include "JSDOMWindow.h"
36 #include "NP_jsobject.h"
37 #include "c_instance.h"
38 #include <runtime/JSGlobalObject.h>
39 #include <runtime/JSLock.h>
40 #include "npruntime_impl.h"
41 #include "npruntime_priv.h"
42 #include "runtime_object.h"
43 #include "runtime_root.h"
44 #include <wtf/Assertions.h>
45 #include <wtf/text/WTFString.h>
47 namespace JSC { namespace Bindings {
49 static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length)
51 ASSERT(UTF8Chars || UTF8Length == 0);
54 UTF8Length = static_cast<int>(strlen(UTF8Chars));
56 String result = String::fromUTF8(UTF8Chars, UTF8Length);
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.
63 result = String(UTF8Chars, UTF8Length);
68 // Variant value must be released with NPReleaseVariantValue()
69 void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result)
71 JSLockHolder lock(exec);
73 VOID_TO_NPVARIANT(*result);
75 if (value.isString()) {
76 String ustring = value.toString(exec)->value(exec);
77 CString cstring = ustring.utf8();
78 NPString string = { (const NPUTF8*)cstring.data(), static_cast<uint32_t>(cstring.length()) };
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() == CRuntimeObject::info()) {
89 CRuntimeObject* runtimeObject = static_cast<CRuntimeObject*>(object);
90 CInstance* instance = runtimeObject->getInternalCInstance();
92 NPObject* obj = instance->getObject();
93 _NPN_RetainObject(obj);
94 OBJECT_TO_NPVARIANT(obj, *result);
97 JSGlobalObject* globalObject = exec->vmEntryGlobalObject();
99 RootObject* rootObject = findRootObject(globalObject);
101 NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject);
102 OBJECT_TO_NPVARIANT(npObject, *result);
108 JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootObject* rootObject)
110 JSLockHolder lock(exec);
112 NPVariantType type = variant->type;
114 if (type == NPVariantType_Bool)
115 return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant));
116 if (type == NPVariantType_Null)
118 if (type == NPVariantType_Void)
119 return jsUndefined();
120 if (type == NPVariantType_Int32)
121 return jsNumber(NPVARIANT_TO_INT32(*variant));
122 if (type == NPVariantType_Double)
123 return jsNumber(NPVARIANT_TO_DOUBLE(*variant));
124 if (type == NPVariantType_String)
125 return jsStringWithCache(exec, convertNPStringToUTF16(&variant->value.stringValue));
126 if (type == NPVariantType_Object) {
127 NPObject* obj = variant->value.objectValue;
129 if (obj->_class == NPScriptObjectClass)
130 // Get JSObject from NP_JavaScriptObject.
131 return ((JavaScriptObject*)obj)->imp;
133 // Wrap NPObject in a CInstance.
134 return CInstance::create(obj, rootObject)->createRuntimeObject(exec);
137 return jsUndefined();
140 String convertNPStringToUTF16(const NPString* string)
142 return String::fromUTF8WithLatin1Fallback(string->UTF8Characters, string->UTF8Length);
145 Identifier identifierFromNPIdentifier(ExecState* exec, const NPUTF8* name)
147 return Identifier(exec, convertUTF8ToUTF16WithLatin1Fallback(name, -1));
152 #endif // ENABLE(NETSCAPE_PLUGIN_API)