1 /*
2 * Copyright 2009, 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 APPLE COMPUTER, INC. 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
29 #include "jni_npobject.h"
30 #include "jni_class.h"
31 #include "jni_instance.h"
32 #include "jni_runtime.h"
33 #include "jni_utility.h"
34
35 namespace JSC { namespace Bindings {
AllocJavaNPObject(NPP,NPClass *)36 static NPObject* AllocJavaNPObject(NPP, NPClass*)
37 {
38 JavaNPObject* obj =
39 static_cast<JavaNPObject*>(malloc(sizeof(JavaNPObject)));
40 if (obj == 0)
41 return 0;
42 bzero(obj, sizeof(JavaNPObject));
43 return reinterpret_cast<NPObject*>(obj);
44 }
45
FreeJavaNPObject(NPObject * npobj)46 static void FreeJavaNPObject(NPObject* npobj)
47 {
48 JavaNPObject* obj = reinterpret_cast<JavaNPObject*>(npobj);
49 obj->_instance = 0; // free does not call the destructor
50 free(obj);
51 }
52
53 static NPClass JavaNPObjectClass = {
54 NP_CLASS_STRUCT_VERSION,
55 AllocJavaNPObject, // allocate,
56 FreeJavaNPObject, // free,
57 0, // invalidate
58 JavaNPObject_HasMethod,
59 JavaNPObject_Invoke,
60 0, // invokeDefault,
61 JavaNPObject_HasProperty,
62 JavaNPObject_GetProperty,
63 0, // setProperty
64 0, // removeProperty
65 0, // enumerate
66 0 // construct
67 };
68
69
JavaInstanceToNPObject(JavaInstance * instance)70 NPObject* JavaInstanceToNPObject(JavaInstance* instance) {
71 JavaNPObject* object = reinterpret_cast<JavaNPObject*>(_NPN_CreateObject(0, &JavaNPObjectClass));
72 object->_instance = instance;
73 return reinterpret_cast<NPObject*>(object);
74 }
75
76
77 // Returns null if obj is not a wrapper of JavaInstance
ExtractJavaInstance(NPObject * obj)78 JavaInstance* ExtractJavaInstance(NPObject* obj) {
79 if (obj->_class == &JavaNPObjectClass) {
80 return reinterpret_cast<JavaNPObject*>(obj)->_instance.get();
81 }
82 return 0;
83 }
84
JavaNPObject_HasMethod(NPObject * obj,NPIdentifier identifier)85 bool JavaNPObject_HasMethod(NPObject* obj, NPIdentifier identifier) {
86 JavaInstance* instance = ExtractJavaInstance(obj);
87 if (instance == 0)
88 return false;
89 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
90 if (name == 0)
91 return false;
92
93 bool result = (instance->getClass()->methodsNamed(name).size() > 0);
94
95 // TODO: use NPN_MemFree
96 free(name);
97
98 return result;
99 }
100
JavaNPObject_Invoke(NPObject * obj,NPIdentifier identifier,const NPVariant * args,uint32_t argCount,NPVariant * result)101 bool JavaNPObject_Invoke(NPObject* obj, NPIdentifier identifier,
102 const NPVariant* args, uint32_t argCount, NPVariant* result) {
103 JavaInstance* instance = ExtractJavaInstance(obj);
104 if (instance == 0)
105 return false;
106 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
107 if (name == 0)
108 return false;
109
110 bool r = instance->invokeMethod(name, args, argCount, result);
111
112 // TODO: use NPN_MemFree
113 free(name);
114 return r;
115
116 }
117
JavaNPObject_HasProperty(NPObject * obj,NPIdentifier identifier)118 bool JavaNPObject_HasProperty(NPObject* obj, NPIdentifier identifier) {
119 JavaInstance* instance = ExtractJavaInstance(obj);
120 if (instance == 0)
121 return false;
122 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
123 if (name == 0)
124 return false;
125 bool result = instance->getClass()->fieldNamed(name) != 0;
126 free(name);
127 return result;
128 }
129
JavaNPObject_GetProperty(NPObject * obj,NPIdentifier identifier,NPVariant * result)130 bool JavaNPObject_GetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result) {
131 VOID_TO_NPVARIANT(*result);
132 JavaInstance* instance = ExtractJavaInstance(obj);
133 if (instance == 0)
134 return false;
135 NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
136 if (name == 0)
137 return false;
138
139 JavaField* field = instance->getClass()->fieldNamed(name);
140 free(name); // TODO: use NPN_MemFree
141
142 if (field == 0) {
143 return false;
144 }
145
146 jobject local_ref = instance->getLocalRef();
147 jvalue value = getJNIField(local_ref,
148 field->getJNIType(),
149 field->name(),
150 field->type());
151 getJNIEnv()->DeleteLocalRef(local_ref);
152
153 convertJValueToNPVariant(value, field->getJNIType(), field->type(), result);
154
155 return true;
156 }
157
158 }} // namespace
159