1 /* 2 * Copyright 2010, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 27 #include "config.h" 28 #include "JavaNPObjectV8.h" 29 30 #if ENABLE(JAVA_BRIDGE) 31 32 #include "JNIUtilityPrivate.h" 33 #include "JavaClassV8.h" 34 #include "JavaFieldV8.h" 35 #include "JavaInstanceV8.h" 36 #include "JavaMethod.h" 37 #include "JavaValueV8.h" 38 #include "npruntime_impl.h" 39 40 namespace JSC { 41 42 namespace Bindings { 43 44 static NPObject* AllocJavaNPObject(NPP, NPClass*) 45 { 46 JavaNPObject* obj = static_cast<JavaNPObject*>(malloc(sizeof(JavaNPObject))); 47 if (!obj) 48 return 0; 49 memset(obj, 0, sizeof(JavaNPObject)); 50 return reinterpret_cast<NPObject*>(obj); 51 } 52 53 static void FreeJavaNPObject(NPObject* npobj) 54 { 55 JavaNPObject* obj = reinterpret_cast<JavaNPObject*>(npobj); 56 obj->m_instance = 0; // free does not call the destructor 57 free(obj); 58 } 59 60 static NPClass JavaNPObjectClass = { 61 NP_CLASS_STRUCT_VERSION, 62 AllocJavaNPObject, // allocate, 63 FreeJavaNPObject, // free, 64 0, // invalidate 65 JavaNPObjectHasMethod, 66 JavaNPObjectInvoke, 67 0, // invokeDefault, 68 JavaNPObjectHasProperty, 69 JavaNPObjectGetProperty, 70 0, // setProperty 71 0, // removeProperty 72 0, // enumerate 73 0 // construct 74 }; 75 76 NPObject* JavaInstanceToNPObject(JavaInstance* instance) 77 { 78 JavaNPObject* object = reinterpret_cast<JavaNPObject*>(_NPN_CreateObject(0, &JavaNPObjectClass)); 79 object->m_instance = instance; 80 return reinterpret_cast<NPObject*>(object); 81 } 82 83 // Returns null if obj is not a wrapper of JavaInstance 84 JavaInstance* ExtractJavaInstance(NPObject* obj) 85 { 86 if (obj->_class == &JavaNPObjectClass) 87 return reinterpret_cast<JavaNPObject*>(obj)->m_instance.get(); 88 return 0; 89 } 90 91 bool JavaNPObjectHasMethod(NPObject* obj, NPIdentifier identifier) 92 { 93 JavaInstance* instance = ExtractJavaInstance(obj); 94 if (!instance) 95 return false; 96 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); 97 if (!name) 98 return false; 99 100 instance->begin(); 101 bool result = (instance->getClass()->methodsNamed(name).size() > 0); 102 instance->end(); 103 104 // TODO: use NPN_MemFree 105 free(name); 106 107 return result; 108 } 109 110 bool JavaNPObjectInvoke(NPObject* obj, NPIdentifier identifier, const NPVariant* args, uint32_t argCount, NPVariant* result) 111 { 112 JavaInstance* instance = ExtractJavaInstance(obj); 113 if (!instance) 114 return false; 115 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); 116 if (!name) 117 return false; 118 119 instance->begin(); 120 121 MethodList methodList = instance->getClass()->methodsNamed(name); 122 // TODO: use NPN_MemFree 123 free(name); 124 125 // Try to find a good match for the overloaded method. The 126 // fundamental problem is that JavaScript doesn't have the 127 // notion of method overloading and Java does. We could 128 // get a bit more sophisticated and attempt to do some 129 // type checking as well as checking the number of parameters. 130 size_t numMethods = methodList.size(); 131 JavaMethod* aMethod; 132 JavaMethod* jMethod = 0; 133 for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) { 134 aMethod = methodList[methodIndex]; 135 if (aMethod->numParameters() == static_cast<int>(argCount)) { 136 jMethod = aMethod; 137 break; 138 } 139 } 140 if (!jMethod) { 141 instance->end(); 142 return false; 143 } 144 145 JavaValue* jArgs = new JavaValue[argCount]; 146 for (unsigned int i = 0; i < argCount; i++) 147 jArgs[i] = convertNPVariantToJavaValue(args[i], jMethod->parameterAt(i)); 148 149 JavaValue jResult = instance->invokeMethod(jMethod, jArgs); 150 instance->end(); 151 delete[] jArgs; 152 153 VOID_TO_NPVARIANT(*result); 154 convertJavaValueToNPVariant(jResult, result); 155 return true; 156 } 157 158 bool JavaNPObjectHasProperty(NPObject* obj, NPIdentifier identifier) 159 { 160 JavaInstance* instance = ExtractJavaInstance(obj); 161 if (!instance) 162 return false; 163 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); 164 if (!name) 165 return false; 166 instance->begin(); 167 bool result = instance->getClass()->fieldNamed(name); 168 instance->end(); 169 free(name); 170 return result; 171 } 172 173 bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result) 174 { 175 VOID_TO_NPVARIANT(*result); 176 JavaInstance* instance = ExtractJavaInstance(obj); 177 if (!instance) 178 return false; 179 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier); 180 if (!name) 181 return false; 182 183 instance->begin(); 184 JavaField* field = instance->getClass()->fieldNamed(name); 185 free(name); // TODO: use NPN_MemFree 186 if (!field) { 187 instance->end(); 188 return false; 189 } 190 191 #if PLATFORM(ANDROID) 192 // JSC does not seem to support returning object properties so we emulate that 193 // behaviour here. 194 JavaValue value; 195 #else 196 JavaValue value = instance->getField(field); 197 #endif // PLATFORM(ANDROID) 198 instance->end(); 199 200 convertJavaValueToNPVariant(value, result); 201 202 return true; 203 } 204 205 } // namespace Bindings 206 207 } // namespace JSC 208 209 #endif // ENABLE(JAVA_BRIDGE) 210