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
27 #include "config.h"
28
29 #if ENABLE(NETSCAPE_PLUGIN_API)
30
31 #include "c_utility.h"
32
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 "PlatformString.h"
41 #include "npruntime_impl.h"
42 #include "npruntime_priv.h"
43 #include "runtime_object.h"
44 #include "runtime_root.h"
45 #include <wtf/Assertions.h>
46
47 namespace JSC { namespace Bindings {
48
convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8 * UTF8Chars,int UTF8Length)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()
convertValueToNPVariant(ExecState * exec,JSValue value,NPVariant * result)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.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::s_info) {
89 CRuntimeObject* runtimeObject = static_cast<CRuntimeObject*>(object);
90 CInstance* instance = runtimeObject->getInternalCInstance();
91 if (instance) {
92 NPObject* obj = instance->getObject();
93 _NPN_RetainObject(obj);
94 OBJECT_TO_NPVARIANT(obj, *result);
95 }
96 } else {
97 #ifdef ANDROID
98 RootObject* rootObject = findRootObject(exec->dynamicGlobalObject());
99 if (!rootObject)
100 rootObject = findRootObject(exec->lexicalGlobalObject());
101 #else
102 JSGlobalObject* globalObject = exec->dynamicGlobalObject();
103
104 RootObject* rootObject = findRootObject(globalObject);
105 #endif
106 if (rootObject) {
107 NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject);
108 OBJECT_TO_NPVARIANT(npObject, *result);
109 }
110 }
111 }
112 }
113
convertNPVariantToValue(ExecState * exec,const NPVariant * variant,RootObject * rootObject)114 JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootObject* rootObject)
115 {
116 JSLock lock(SilenceAssertionsOnly);
117
118 NPVariantType type = variant->type;
119
120 if (type == NPVariantType_Bool)
121 return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant));
122 if (type == NPVariantType_Null)
123 return jsNull();
124 if (type == NPVariantType_Void)
125 return jsUndefined();
126 if (type == NPVariantType_Int32)
127 return jsNumber(NPVARIANT_TO_INT32(*variant));
128 if (type == NPVariantType_Double)
129 return jsNumber(NPVARIANT_TO_DOUBLE(*variant));
130 if (type == NPVariantType_String)
131 return WebCore::jsString(exec, convertNPStringToUTF16(&variant->value.stringValue));
132 if (type == NPVariantType_Object) {
133 NPObject* obj = variant->value.objectValue;
134
135 if (obj->_class == NPScriptObjectClass)
136 // Get JSObject from NP_JavaScriptObject.
137 return ((JavaScriptObject*)obj)->imp;
138
139 // Wrap NPObject in a CInstance.
140 return CInstance::create(obj, rootObject)->createRuntimeObject(exec);
141 }
142
143 return jsUndefined();
144 }
145
convertNPStringToUTF16(const NPString * string)146 String convertNPStringToUTF16(const NPString* string)
147 {
148 return String::fromUTF8WithLatin1Fallback(string->UTF8Characters, string->UTF8Length);
149 }
150
identifierFromNPIdentifier(ExecState * exec,const NPUTF8 * name)151 Identifier identifierFromNPIdentifier(ExecState* exec, const NPUTF8* name)
152 {
153 return Identifier(exec, WebCore::stringToUString(convertUTF8ToUTF16WithLatin1Fallback(name, -1)));
154 }
155
156 } }
157
158 #endif // ENABLE(NETSCAPE_PLUGIN_API)
159