VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/java/src/nsJavaXPCOMBindingUtils.h@ 98262

Last change on this file since 98262 was 29140, checked in by vboxsync, 15 years ago

JXPCOM OSE fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Java XPCOM Bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * IBM Corporation. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Javier Pedemonte (jhpedemonte@gmail.com)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef _nsJavaXPCOMBindingUtils_h_
39#define _nsJavaXPCOMBindingUtils_h_
40
41#include "jni.h"
42#include "xptcall.h"
43#include "nsCOMPtr.h"
44#include "nsString.h"
45#include "pldhash.h"
46#include "nsJavaXPTCStub.h"
47#include "nsAutoLock.h"
48#include "nsTHashtable.h"
49#include "nsHashKeys.h"
50#include "nsILocalFile.h"
51
52//#define DEBUG_JAVAXPCOM
53//#define DEBUG_JAVAXPCOM_REFCNT
54
55#ifdef DEBUG_JAVAXPCOM
56#define LOG(x) printf x
57#else
58#define LOG(x) /* nothing */
59#endif
60
61
62/*********************
63 * Java JNI globals
64 *********************/
65
66extern jclass systemClass;
67extern jclass booleanClass;
68extern jclass charClass;
69extern jclass byteClass;
70extern jclass shortClass;
71extern jclass intClass;
72extern jclass longClass;
73extern jclass floatClass;
74extern jclass doubleClass;
75extern jclass stringClass;
76extern jclass nsISupportsClass;
77extern jclass xpcomExceptionClass;
78extern jclass xpcomJavaProxyClass;
79extern jclass weakReferenceClass;
80extern jclass javaXPCOMUtilsClass;
81
82extern jmethodID hashCodeMID;
83extern jmethodID booleanValueMID;
84extern jmethodID booleanInitMID;
85extern jmethodID charValueMID;
86extern jmethodID charInitMID;
87extern jmethodID byteValueMID;
88extern jmethodID byteInitMID;
89extern jmethodID shortValueMID;
90extern jmethodID shortInitMID;
91extern jmethodID intValueMID;
92extern jmethodID intInitMID;
93extern jmethodID longValueMID;
94extern jmethodID longInitMID;
95extern jmethodID floatValueMID;
96extern jmethodID floatInitMID;
97extern jmethodID doubleValueMID;
98extern jmethodID doubleInitMID;
99extern jmethodID createProxyMID;
100extern jmethodID isXPCOMJavaProxyMID;
101extern jmethodID getNativeXPCOMInstMID;
102extern jmethodID weakReferenceConstructorMID;
103extern jmethodID getReferentMID;
104extern jmethodID clearReferentMID;
105extern jmethodID findClassInLoaderMID;
106
107#ifdef DEBUG_JAVAXPCOM
108extern jmethodID getNameMID;
109extern jmethodID proxyToStringMID;
110#endif
111
112class NativeToJavaProxyMap;
113extern NativeToJavaProxyMap* gNativeToJavaProxyMap;
114class JavaToXPTCStubMap;
115extern JavaToXPTCStubMap* gJavaToXPTCStubMap;
116
117extern nsTHashtable<nsDepCharHashKey>* gJavaKeywords;
118
119// The Java garbage collector runs in a separate thread. Since it calls the
120// finalizeProxy() function in nsJavaWrapper.cpp, we need to make sure that
121// all the structures touched by finalizeProxy() are multithread aware.
122extern PRLock* gJavaXPCOMLock;
123
124extern PRBool gJavaXPCOMInitialized;
125
126/**
127 * Initialize global structures used by JavaXPCOM.
128 * @param env Java environment pointer
129 * @return PR_TRUE if JavaXPCOM is initialized; PR_FALSE if an error occurred
130 */
131PRBool InitializeJavaGlobals(JNIEnv *env);
132
133/**
134 * Frees global structures that were allocated by InitializeJavaGlobals().
135 * @param env Java environment pointer
136 */
137void FreeJavaGlobals(JNIEnv* env);
138
139
140/*************************
141 * JavaXPCOMInstance
142 *************************/
143
144class JavaXPCOMInstance
145{
146public:
147 JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo);
148 ~JavaXPCOMInstance();
149
150 nsISupports* GetInstance() { return mInstance; }
151 nsIInterfaceInfo* InterfaceInfo() { return mIInfo; }
152
153private:
154 nsISupports* mInstance;
155 nsIInterfaceInfo* mIInfo;
156};
157
158
159/**************************************
160 * Java<->XPCOM object mappings
161 **************************************/
162
163/**
164 * Maps native XPCOM objects to their associated Java proxy object.
165 */
166class NativeToJavaProxyMap
167{
168 friend PLDHashOperator DestroyJavaProxyMappingEnum(PLDHashTable* aTable,
169 PLDHashEntryHdr* aHeader,
170 PRUint32 aNumber,
171 void* aData);
172
173protected:
174 struct ProxyList
175 {
176 ProxyList(const jobject aRef, const nsIID& aIID, ProxyList* aList)
177 : javaObject(aRef)
178 , iid(aIID)
179 , next(aList)
180 { }
181
182 const jobject javaObject;
183 const nsIID iid;
184 ProxyList* next;
185 };
186
187 struct Entry : public PLDHashEntryHdr
188 {
189 nsISupports* key;
190 ProxyList* list;
191 };
192
193public:
194 NativeToJavaProxyMap()
195 : mHashTable(nsnull)
196 { }
197
198 ~NativeToJavaProxyMap()
199 {
200 NS_ASSERTION(mHashTable == nsnull,
201 "MUST call Destroy() before deleting object");
202 }
203
204 nsresult Init();
205
206 nsresult Destroy(JNIEnv* env);
207
208 nsresult Add(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID,
209 jobject aProxy);
210
211 nsresult Find(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID,
212 jobject* aResult);
213
214 nsresult Remove(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID);
215
216protected:
217 PLDHashTable* mHashTable;
218};
219
220/**
221 * Maps Java objects to their associated nsJavaXPTCStub.
222 */
223class JavaToXPTCStubMap
224{
225 friend PLDHashOperator DestroyXPTCMappingEnum(PLDHashTable* aTable,
226 PLDHashEntryHdr* aHeader,
227 PRUint32 aNumber, void* aData);
228
229protected:
230 struct Entry : public PLDHashEntryHdr
231 {
232 jint key;
233 nsJavaXPTCStub* xptcstub;
234 };
235
236public:
237 JavaToXPTCStubMap()
238 : mHashTable(nsnull)
239 { }
240
241 ~JavaToXPTCStubMap()
242 {
243 NS_ASSERTION(mHashTable == nsnull,
244 "MUST call Destroy() before deleting object");
245 }
246
247 nsresult Init();
248
249 nsresult Destroy();
250
251 nsresult Add(jint aJavaObjectHashCode, nsJavaXPTCStub* aProxy);
252
253 nsresult Find(jint aJavaObjectHashCode, const nsIID& aIID,
254 nsJavaXPTCStub** aResult);
255
256 nsresult Remove(jint aJavaObjectHashCode);
257
258protected:
259 PLDHashTable* mHashTable;
260};
261
262
263/*******************************
264 * Helper functions
265 *******************************/
266
267/**
268 * Convert a native nsISupports to a Java object.
269 *
270 * @param env Java environment pointer
271 * @param aXPCOMObject XPCOM object for which to find/create Java object
272 * @param aIID desired interface IID for Java object
273 * @param aObjectLoader Java object whose class loader we use for finding
274 * classes; can be null
275 * @param aResult on success, holds reference to Java object
276 *
277 * @return NS_OK if succeeded; all other return values are error codes.
278 */
279nsresult NativeInterfaceToJavaObject(JNIEnv* env, nsISupports* aXPCOMObject,
280 const nsIID& aIID, jobject aObjectLoader,
281 jobject* aResult);
282
283/**
284 * Convert a Java object to a native nsISupports object.
285 *
286 * @param env Java environment pointer
287 * @param aJavaObject Java object for which to find/create XPCOM object
288 * @param aIID desired interface IID for XPCOM object
289 * @param aResult on success, holds AddRef'd reference to XPCOM object
290 *
291 * @return NS_OK if succeeded; all other return values are error codes.
292 */
293nsresult JavaObjectToNativeInterface(JNIEnv* env, jobject aJavaObject,
294 const nsIID& aIID, void** aResult);
295
296nsresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
297 const XPTMethodDescriptor *methodInfo,
298 const nsXPTParamInfo &paramInfo,
299 PRUint8 paramType, PRUint16 methodIndex,
300 nsXPTCMiniVariant *dispatchParams,
301 PRBool isFullVariantArray,
302 nsID &result);
303
304/**
305 * Returns the Class object associated with the class or interface with the
306 * given string name, using the class loader of the given object.
307 *
308 * @param env Java environment pointer
309 * @param aObjectLoader Java object whose class loader is used to load class
310 * @param aClassName fully qualified name of class to load
311 *
312 * @return java.lang.Class object of requested Class; NULL if the class
313 * wasn't found
314 *
315 * @see http://java.sun.com/j2se/1.3/docs/guide/jni/jni-12.html#classops
316 */
317inline jclass
318FindClassInLoader(JNIEnv* env, jobject aObjectLoader, const char* aClassName)
319{
320 jclass clazz = nsnull;
321 jstring name = env->NewStringUTF(aClassName);
322 if (name)
323 clazz = (jclass) env->CallStaticObjectMethod(javaXPCOMUtilsClass,
324 findClassInLoaderMID, aObjectLoader, name);
325
326#ifdef DEBUG
327 if (!clazz)
328 fprintf(stderr, "WARNING: failed to find class [%s]\n", aClassName);
329#endif
330 return clazz;
331}
332
333
334/*******************************
335 * JNI helper functions
336 *******************************/
337
338/**
339 * Returns a pointer to the appropriate JNIEnv structure. This function is
340 * useful in callbacks or other functions that are not called directly from
341 * Java and therefore do not have the JNIEnv structure passed in.
342 *
343 * @return pointer to JNIEnv structure for current thread
344 */
345JNIEnv* GetJNIEnv();
346
347/**
348 * Constructs and throws an exception. Some error codes (such as
349 * NS_ERROR_OUT_OF_MEMORY) are handled by the appropriate Java exception/error.
350 * Otherwise, an instance of XPCOMException is created with the given error
351 * code and message.
352 *
353 * @param env Java environment pointer
354 * @param aErrorCode The error code returned by an XPCOM/Gecko function. Pass
355 * zero for the default behaviour.
356 * @param aMessage A string that provides details for throwing this
357 * exception. Pass in <code>nsnull</code> for the default
358 * behaviour.
359 *
360 * @throws OutOfMemoryError if aErrorCode == NS_ERROR_OUT_OF_MEMORY
361 * XPCOMException for all other error codes
362 */
363void ThrowException(JNIEnv* env, const nsresult aErrorCode,
364 const char* aMessage);
365
366/**
367 * Helper functions for converting from java.lang.String to
368 * nsAString/nsACstring. Caller must delete nsAString/nsACString.
369 *
370 * @param env Java environment pointer
371 * @param aString Java string to convert
372 *
373 * @return nsAString/nsACString with same content as given Java string;
374 * a 'void' nsAString/nsACString object if aString is
375 * <code>null</code>; or <code>nsnull</code> if out of memory
376 */
377nsAString* jstring_to_nsAString(JNIEnv* env, jstring aString);
378nsACString* jstring_to_nsACString(JNIEnv* env, jstring aString);
379
380/**
381 * Helper function for converting from java.io.File to nsILocalFile.
382 *
383 * @param env Java environment pointer
384 * @param aFile Java File to convert
385 * @param aLocalFile returns the converted nsILocalFile
386 *
387 * @return NS_OK for success; other values indicate error in conversion
388 */
389nsresult File_to_nsILocalFile(JNIEnv* env, jobject aFile,
390 nsILocalFile** aLocalFile);
391
392#endif // _nsJavaXPCOMBindingUtils_h_
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette