VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/java/src/nsJavaInterfaces.cpp@ 46593

Last change on this file since 46593 was 46593, checked in by vboxsync, 12 years ago

updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.7 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 IBM Corporation.
17 * Portions created by the Initial Developer are Copyright (C) 2007
18 * IBM Corporation. All Rights Reserved.
19 *
20 * Contributor(s):
21 * Javier Pedemonte ([email protected])
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ***** END LICENSE BLOCK ***** */
36
37#include "nsJavaInterfaces.h"
38#include "nsJavaWrapper.h"
39#include "nsJavaXPCOMBindingUtils.h"
40#include "nsJavaXPTCStub.h"
41#include "nsIComponentRegistrar.h"
42#include "nsString.h"
43#include "nsISimpleEnumerator.h"
44#include "nsIInterfaceInfoManager.h"
45#include "nsIInputStream.h"
46#include "nsEnumeratorUtils.h"
47#include "nsAppFileLocProviderProxy.h"
48#ifndef VBOX
49#include "nsXULAppAPI.h"
50#endif
51#include "nsILocalFile.h"
52
53#ifdef XP_MACOSX
54#include "jawt.h"
55#endif
56
57
58#ifdef VBOX
59#if 0
60#include "org_mozilla_xpcom_internal_GREImpl.h"
61#include "org_mozilla_xpcom_internal_JavaXPCOMMethods.h"
62#include "org_mozilla_xpcom_internal_MozillaImpl.h"
63#include "org_mozilla_xpcom_internal_XPCOMImpl.h"
64#include "org_mozilla_xpcom_internal_XPCOMJavaProxy.h"
65#include "org_mozilla_xpcom_ProfileLock.h"
66#endif
67#include <VBox/com/com.h>
68using namespace com;
69#include <iprt/initterm.h>
70#include <iprt/string.h>
71#include <alloca.h>
72#endif
73
74extern "C" NS_EXPORT void JNICALL
75MOZILLA_NATIVE(initialize) (JNIEnv* env, jobject)
76{
77 if (!InitializeJavaGlobals(env)) {
78 jclass clazz =
79 env->FindClass("org/mozilla/xpcom/XPCOMInitializationException");
80 if (clazz) {
81 env->ThrowNew(clazz, "Failed to initialize JavaXPCOM");
82 }
83 }
84}
85
86nsresult
87InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory,
88 jobject aAppDirectory, jobject aAppDirProvider)
89{
90 nsresult rv;
91
92 // create an nsILocalFile from given java.io.File
93 nsCOMPtr<nsILocalFile> libXULDir;
94 if (aLibXULDirectory) {
95 rv = File_to_nsILocalFile(env, aLibXULDirectory, getter_AddRefs(libXULDir));
96 NS_ENSURE_SUCCESS(rv, rv);
97 }
98 nsCOMPtr<nsILocalFile> appDir;
99 if (aAppDirectory) {
100 rv = File_to_nsILocalFile(env, aAppDirectory, getter_AddRefs(appDir));
101 NS_ENSURE_SUCCESS(rv, rv);
102 }
103
104 // create nsAppFileLocProviderProxy from given Java object
105 nsCOMPtr<nsIDirectoryServiceProvider> provider;
106 if (aAppDirProvider) {
107 rv = NS_NewAppFileLocProviderProxy(aAppDirProvider,
108 getter_AddRefs(provider));
109 NS_ENSURE_SUCCESS(rv, rv);
110 }
111
112 // init libXUL
113#ifdef VBOX
114 return 0;
115#else
116 return XRE_InitEmbedding(libXULDir, appDir, provider, nsnull, 0);
117#endif
118}
119
120extern "C" NS_EXPORT void JNICALL
121GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory,
122 jobject aAppDirectory, jobject aAppDirProvider)
123{
124 nsresult rv = InitEmbedding_Impl(env, aLibXULDirectory, aAppDirectory,
125 aAppDirProvider);
126
127 if (NS_FAILED(rv)) {
128 ThrowException(env, rv, "Failure in initEmbedding");
129 FreeJavaGlobals(env);
130 }
131}
132
133extern "C" NS_EXPORT void JNICALL
134GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject)
135{
136 // Free globals before calling XRE_TermEmbedding(), since we need some
137 // XPCOM services.
138 FreeJavaGlobals(env);
139
140#ifndef VBOX
141 XRE_TermEmbedding();
142#endif
143}
144#ifdef VBOX
145nsresult
146InitXPCOMVBox_Impl(JNIEnv* env, jobject aVBoxBinDirectory)
147{
148#if defined(VBOX_PATH_APP_PRIVATE_ARCH) && defined(VBOX_PATH_SHARED_LIBS)
149 rv = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
150#else
151 const char *pszHome = nsnull;
152 const char *jhome = nsnull;
153 jstring path = nsnull;
154
155 int rv;
156 jclass clazz;
157 jmethodID getPathMID;
158
159 if (aVBoxBinDirectory &&
160 (clazz = env->FindClass("java/io/File")) &&
161 (getPathMID = env->GetMethodID(clazz, "getAbsolutePath",
162 "()Ljava/lang/String;"))
163 )
164 {
165 path = (jstring)env->CallObjectMethod(aVBoxBinDirectory, getPathMID);
166 pszHome = jhome = env->GetStringUTFChars(path, nsnull);
167 }
168
169 if (pszHome == nsnull)
170 pszHome = getenv("VBOX_PROGRAM_PATH");
171
172 if (pszHome) {
173 size_t cchHome = strlen(pszHome);
174 char *pszExePath = (char *)alloca(cchHome + 32);
175 memcpy(pszExePath, pszHome, cchHome);
176 memcpy(pszExePath + cchHome, "/javafake", sizeof("/javafake"));
177 rv = RTR3InitEx(RTR3INIT_VER_CUR, RTR3INIT_FLAGS_DLL | RTR3INIT_FLAGS_UNOBTRUSIVE, 0, NULL, pszExePath);
178 } else {
179 rv = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
180 }
181
182 if (jhome)
183 env->ReleaseStringUTFChars(path, jhome);
184#endif
185
186 return com::Initialize();
187}
188#endif
189
190nsresult
191InitXPCOM_Impl(JNIEnv* env, jobject aMozBinDirectory,
192 jobject aAppFileLocProvider, jobject* aResult)
193{
194 nsresult rv;
195 // create an nsILocalFile from given java.io.File
196 nsCOMPtr<nsILocalFile> directory;
197 if (aMozBinDirectory) {
198 rv = File_to_nsILocalFile(env, aMozBinDirectory, getter_AddRefs(directory));
199 NS_ENSURE_SUCCESS(rv, rv);
200 }
201
202 // create nsAppFileLocProviderProxy from given Java object
203 nsCOMPtr<nsIDirectoryServiceProvider> provider;
204 if (aAppFileLocProvider) {
205 rv = NS_NewAppFileLocProviderProxy(aAppFileLocProvider,
206 getter_AddRefs(provider));
207 NS_ENSURE_SUCCESS(rv, rv);
208 }
209
210 // init XPCOM
211 nsCOMPtr<nsIServiceManager> servMan;
212 rv = NS_InitXPCOM2(getter_AddRefs(servMan), directory, provider);
213 NS_ENSURE_SUCCESS(rv, rv);
214
215 // create Java proxy for service manager returned by NS_InitXPCOM2
216 return NativeInterfaceToJavaObject(env, servMan, NS_GET_IID(nsIServiceManager),
217 nsnull, aResult);
218}
219
220extern "C" NS_EXPORT jobject JNICALL
221XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory,
222 jobject aAppFileLocProvider)
223{
224#ifdef VBOX
225 nsresult rv = InitXPCOMVBox_Impl(env, aMozBinDirectory);
226 if (NS_SUCCEEDED(rv))
227 return nsnull;
228#else
229 jobject servMan;
230 nsresult rv = InitXPCOM_Impl(env, aMozBinDirectory, aAppFileLocProvider,
231 &servMan);
232 if (NS_SUCCEEDED(rv))
233 return servMan;
234#endif
235
236 ThrowException(env, rv, "Failure in initXPCOM");
237 FreeJavaGlobals(env);
238 return nsnull;
239}
240
241extern "C" NS_EXPORT void JNICALL
242#ifdef VBOX
243XPCOM_NATIVE2(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr)
244#else
245XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr)
246#endif
247{
248#ifdef VBOX
249 // Free globals before calling NS_ShutdownXPCOM(), since we need some
250 // XPCOM services.
251 //FreeJavaGlobals(env);
252 //com::Shutdown();
253#else
254 nsresult rv;
255 nsIServiceManager* servMgr = nsnull;
256 if (aServMgr) {
257 // Get native XPCOM instance
258 nsISupports* instancePtr = nsnull;
259 rv = JavaObjectToNativeInterface(env, aServMgr,
260 NS_GET_IID(nsIServiceManager), (void**) &instancePtr);
261 NS_ASSERTION(NS_SUCCEEDED(rv) && instancePtr != nsnull,
262 "Failed to get XPCOM obj for ServiceMgr.");
263 if (NS_SUCCEEDED(rv)) {
264 rv = instancePtr->QueryInterface(NS_GET_IID(nsIServiceManager),
265 (void**) &servMgr);
266 NS_ASSERTION(NS_SUCCEEDED(rv), "QI for nsIServiceManager failed");
267 }
268
269 // Even if we failed to get the matching xpcom object, we don't abort this
270 // function. Just call NS_ShutdownXPCOM with a null service manager.
271 }
272
273 // Free globals before calling NS_ShutdownXPCOM(), since we need some
274 // XPCOM services.
275 FreeJavaGlobals(env);
276
277 rv = NS_ShutdownXPCOM(servMgr);
278 if (NS_FAILED(rv))
279 ThrowException(env, rv, "NS_ShutdownXPCOM failed");
280#endif
281}
282
283extern "C" NS_EXPORT jobject JNICALL
284XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath,
285 jboolean aFollowLinks)
286{
287 // Create a Mozilla string from the jstring
288 const PRUnichar* buf = nsnull;
289 if (aPath) {
290 buf = env->GetStringChars(aPath, nsnull);
291 if (!buf)
292 return nsnull; // exception already thrown
293 }
294
295 nsAutoString path_str(buf);
296 env->ReleaseStringChars(aPath, buf);
297
298 // Make call to given function
299 nsCOMPtr<nsILocalFile> file;
300 nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, getter_AddRefs(file));
301
302 if (NS_SUCCEEDED(rv)) {
303 jobject javaProxy;
304 rv = NativeInterfaceToJavaObject(env, file, NS_GET_IID(nsILocalFile),
305 nsnull, &javaProxy);
306 if (NS_SUCCEEDED(rv))
307 return javaProxy;
308 }
309
310 ThrowException(env, rv, "Failure in newLocalFile");
311 return nsnull;
312}
313
314extern "C" NS_EXPORT jobject JNICALL
315#ifdef VBOX
316XPCOM_NATIVE2(getComponentManager) (JNIEnv *env, jobject)
317#else
318XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject)
319#endif
320{
321 // Call XPCOM method
322 nsCOMPtr<nsIComponentManager> cm;
323 nsresult rv = NS_GetComponentManager(getter_AddRefs(cm));
324
325 if (NS_SUCCEEDED(rv)) {
326 jobject javaProxy;
327 rv = NativeInterfaceToJavaObject(env, cm, NS_GET_IID(nsIComponentManager),
328 nsnull, &javaProxy);
329 if (NS_SUCCEEDED(rv))
330 return javaProxy;
331 }
332
333 ThrowException(env, rv, "Failure in getComponentManager");
334 return nsnull;
335}
336
337extern "C" NS_EXPORT jobject JNICALL
338XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject)
339{
340 // Call XPCOM method
341 nsCOMPtr<nsIComponentRegistrar> cr;
342 nsresult rv = NS_GetComponentRegistrar(getter_AddRefs(cr));
343
344 if (NS_SUCCEEDED(rv)) {
345 jobject javaProxy;
346 rv = NativeInterfaceToJavaObject(env, cr, NS_GET_IID(nsIComponentRegistrar),
347 nsnull, &javaProxy);
348 if (NS_SUCCEEDED(rv))
349 return javaProxy;
350 }
351
352 ThrowException(env, rv, "Failure in getComponentRegistrar");
353 return nsnull;
354}
355
356#ifdef VBOX
357# include <VBox/com/EventQueue.h>
358# include <iprt/err.h>
359
360extern "C" NS_EXPORT jint JNICALL
361XPCOM_NATIVE2(waitForEvents) (JNIEnv *env, jobject, jlong aTimeout)
362{
363 com::EventQueue* aEventQ = com::EventQueue::getMainEventQueue();
364 NS_WARN_IF_FALSE(aEventQ != nsnull, "Null main event queue");
365 if (!aEventQ)
366 return -1;
367
368 int rc = aEventQ->processEventQueue(aTimeout < 0 ? RT_INDEFINITE_WAIT : (uint32_t)aTimeout);
369
370 if (RT_SUCCESS(rc))
371 return 0;
372
373 if ( rc == VERR_TIMEOUT
374 || rc == VERR_INTERRUPTED)
375 return 1;
376
377 return 2;
378}
379#endif
380
381extern "C" NS_EXPORT jobject JNICALL
382#ifdef VBOX
383XPCOM_NATIVE2(getServiceManager) (JNIEnv *env, jobject)
384#else
385XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject)
386#endif
387{
388 // Call XPCOM method
389 nsCOMPtr<nsIServiceManager> sm;
390 nsresult rv = NS_GetServiceManager(getter_AddRefs(sm));
391
392 if (NS_SUCCEEDED(rv)) {
393 jobject javaProxy;
394 rv = NativeInterfaceToJavaObject(env, sm, NS_GET_IID(nsIServiceManager),
395 nsnull, &javaProxy);
396 if (NS_SUCCEEDED(rv))
397 return javaProxy;
398 }
399
400 ThrowException(env, rv, "Failure in getServiceManager");
401 return nsnull;
402}
403
404extern "C" NS_EXPORT jobject JNICALL
405GRE_NATIVE(lockProfileDirectory) (JNIEnv* env, jobject, jobject aDirectory)
406{
407 nsresult rv = NS_ERROR_FAILURE;
408
409 if (aDirectory) {
410 nsCOMPtr<nsILocalFile> profileDir;
411 rv = File_to_nsILocalFile(env, aDirectory, getter_AddRefs(profileDir));
412
413 if (NS_SUCCEEDED(rv)) {
414 nsISupports* lock;
415#ifdef VBOX
416 rv = 0;
417 lock = 0;
418#else
419 rv = XRE_LockProfileDirectory(profileDir, &lock);
420#endif
421
422 if (NS_SUCCEEDED(rv)) {
423 jclass clazz =
424 env->FindClass("org/mozilla/xpcom/ProfileLock");
425 if (clazz) {
426 jmethodID mid = env->GetMethodID(clazz, "<init>", "(J)V");
427 if (mid) {
428 return env->NewObject(clazz, mid, reinterpret_cast<jlong>(lock));
429 }
430 }
431
432 // if we get here, then something failed
433 rv = NS_ERROR_FAILURE;
434 }
435 }
436 }
437
438 ThrowException(env, rv, "Failure in lockProfileDirectory");
439 return nsnull;
440}
441
442extern "C" NS_EXPORT void JNICALL
443GRE_NATIVE(notifyProfile) (JNIEnv *env, jobject)
444{
445#ifndef VBOX
446 XRE_NotifyProfile();
447#endif
448}
449
450#ifdef XP_MACOSX
451extern PRUint64 GetPlatformHandle(JAWT_DrawingSurfaceInfo* dsi);
452#endif
453
454extern "C" NS_EXPORT jlong JNICALL
455MOZILLA_NATIVE(getNativeHandleFromAWT) (JNIEnv* env, jobject clazz,
456 jobject widget)
457{
458 PRUint64 handle = 0;
459
460#if defined(XP_MACOSX) && !defined(VBOX)
461 JAWT awt;
462 awt.version = JAWT_VERSION_1_4;
463 jboolean result = JAWT_GetAWT(env, &awt);
464 if (result == JNI_FALSE)
465 return 0;
466
467 JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, widget);
468 if (ds != nsnull) {
469 jint lock = ds->Lock(ds);
470 if (!(lock & JAWT_LOCK_ERROR)) {
471 JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
472 if (dsi) {
473 handle = GetPlatformHandle(dsi);
474 ds->FreeDrawingSurfaceInfo(dsi);
475 }
476
477 ds->Unlock(ds);
478 }
479
480 awt.FreeDrawingSurface(ds);
481 }
482#else
483 NS_WARNING("getNativeHandleFromAWT JNI method not implemented");
484#endif
485
486 return handle;
487}
488
489extern "C" NS_EXPORT jlong JNICALL
490JXUTILS_NATIVE(wrapJavaObject) (JNIEnv* env, jobject, jobject aJavaObject,
491 jstring aIID)
492{
493 nsresult rv;
494 void* xpcomObject = nsnull;
495
496 if (!aJavaObject || !aIID) {
497 rv = NS_ERROR_NULL_POINTER;
498 } else {
499 const char* str = env->GetStringUTFChars(aIID, nsnull);
500 if (!str) {
501 rv = NS_ERROR_OUT_OF_MEMORY;
502 } else {
503 nsID iid;
504 if (iid.Parse(str)) {
505 rv = JavaObjectToNativeInterface(env, aJavaObject, iid, &xpcomObject);
506 if (NS_SUCCEEDED(rv)) {
507 rv = ((nsISupports*) xpcomObject)->QueryInterface(iid, &xpcomObject);
508 }
509 } else {
510 rv = NS_ERROR_INVALID_ARG;
511 }
512
513 env->ReleaseStringUTFChars(aIID, str);
514 }
515 }
516
517 if (NS_FAILED(rv)) {
518 ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
519 }
520 return reinterpret_cast<jlong>(xpcomObject);
521}
522
523extern "C" NS_EXPORT jobject JNICALL
524JXUTILS_NATIVE(wrapXPCOMObject) (JNIEnv* env, jobject, jlong aXPCOMObject,
525 jstring aIID)
526{
527 nsresult rv;
528 jobject javaObject = nsnull;
529 nsISupports* xpcomObject = reinterpret_cast<nsISupports*>(aXPCOMObject);
530
531 if (!xpcomObject || !aIID) {
532 rv = NS_ERROR_NULL_POINTER;
533 } else {
534 const char* str = env->GetStringUTFChars(aIID, nsnull);
535 if (!str) {
536 rv = NS_ERROR_OUT_OF_MEMORY;
537 } else {
538 nsID iid;
539 if (iid.Parse(str)) {
540 // XXX Should we be passing something other than NULL for aObjectLoader?
541 rv = NativeInterfaceToJavaObject(env, xpcomObject, iid, nsnull,
542 &javaObject);
543 } else {
544 rv = NS_ERROR_INVALID_ARG;
545 }
546
547 env->ReleaseStringUTFChars(aIID, str);
548 }
549 }
550
551 if (NS_FAILED(rv)) {
552 ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
553 }
554 return javaObject;
555}
Note: See TracBrowser for help on using the repository browser.

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