VirtualBox

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

Last change on this file since 38636 was 38636, checked in by vboxsync, 13 years ago

*,IPRT: Redid the ring-3 init to always convert the arguments to UTF-8.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 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(0);
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, 0, NULL, pszExePath);
178 } else {
179 rv = RTR3InitDll(0);
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
382XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject)
383{
384 // Call XPCOM method
385 nsCOMPtr<nsIServiceManager> sm;
386 nsresult rv = NS_GetServiceManager(getter_AddRefs(sm));
387
388 if (NS_SUCCEEDED(rv)) {
389 jobject javaProxy;
390 rv = NativeInterfaceToJavaObject(env, sm, NS_GET_IID(nsIServiceManager),
391 nsnull, &javaProxy);
392 if (NS_SUCCEEDED(rv))
393 return javaProxy;
394 }
395
396 ThrowException(env, rv, "Failure in getServiceManager");
397 return nsnull;
398}
399
400extern "C" NS_EXPORT jobject JNICALL
401GRE_NATIVE(lockProfileDirectory) (JNIEnv* env, jobject, jobject aDirectory)
402{
403 nsresult rv = NS_ERROR_FAILURE;
404
405 if (aDirectory) {
406 nsCOMPtr<nsILocalFile> profileDir;
407 rv = File_to_nsILocalFile(env, aDirectory, getter_AddRefs(profileDir));
408
409 if (NS_SUCCEEDED(rv)) {
410 nsISupports* lock;
411#ifdef VBOX
412 rv = 0;
413 lock = 0;
414#else
415 rv = XRE_LockProfileDirectory(profileDir, &lock);
416#endif
417
418 if (NS_SUCCEEDED(rv)) {
419 jclass clazz =
420 env->FindClass("org/mozilla/xpcom/ProfileLock");
421 if (clazz) {
422 jmethodID mid = env->GetMethodID(clazz, "<init>", "(J)V");
423 if (mid) {
424 return env->NewObject(clazz, mid, reinterpret_cast<jlong>(lock));
425 }
426 }
427
428 // if we get here, then something failed
429 rv = NS_ERROR_FAILURE;
430 }
431 }
432 }
433
434 ThrowException(env, rv, "Failure in lockProfileDirectory");
435 return nsnull;
436}
437
438extern "C" NS_EXPORT void JNICALL
439GRE_NATIVE(notifyProfile) (JNIEnv *env, jobject)
440{
441#ifndef VBOX
442 XRE_NotifyProfile();
443#endif
444}
445
446#ifdef XP_MACOSX
447extern PRUint64 GetPlatformHandle(JAWT_DrawingSurfaceInfo* dsi);
448#endif
449
450extern "C" NS_EXPORT jlong JNICALL
451MOZILLA_NATIVE(getNativeHandleFromAWT) (JNIEnv* env, jobject clazz,
452 jobject widget)
453{
454 PRUint64 handle = 0;
455
456#if defined(XP_MACOSX) && !defined(VBOX)
457 JAWT awt;
458 awt.version = JAWT_VERSION_1_4;
459 jboolean result = JAWT_GetAWT(env, &awt);
460 if (result == JNI_FALSE)
461 return 0;
462
463 JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, widget);
464 if (ds != nsnull) {
465 jint lock = ds->Lock(ds);
466 if (!(lock & JAWT_LOCK_ERROR)) {
467 JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
468 if (dsi) {
469 handle = GetPlatformHandle(dsi);
470 ds->FreeDrawingSurfaceInfo(dsi);
471 }
472
473 ds->Unlock(ds);
474 }
475
476 awt.FreeDrawingSurface(ds);
477 }
478#else
479 NS_WARNING("getNativeHandleFromAWT JNI method not implemented");
480#endif
481
482 return handle;
483}
484
485extern "C" NS_EXPORT jlong JNICALL
486JXUTILS_NATIVE(wrapJavaObject) (JNIEnv* env, jobject, jobject aJavaObject,
487 jstring aIID)
488{
489 nsresult rv;
490 void* xpcomObject = nsnull;
491
492 if (!aJavaObject || !aIID) {
493 rv = NS_ERROR_NULL_POINTER;
494 } else {
495 const char* str = env->GetStringUTFChars(aIID, nsnull);
496 if (!str) {
497 rv = NS_ERROR_OUT_OF_MEMORY;
498 } else {
499 nsID iid;
500 if (iid.Parse(str)) {
501 rv = JavaObjectToNativeInterface(env, aJavaObject, iid, &xpcomObject);
502 if (NS_SUCCEEDED(rv)) {
503 rv = ((nsISupports*) xpcomObject)->QueryInterface(iid, &xpcomObject);
504 }
505 } else {
506 rv = NS_ERROR_INVALID_ARG;
507 }
508
509 env->ReleaseStringUTFChars(aIID, str);
510 }
511 }
512
513 if (NS_FAILED(rv)) {
514 ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
515 }
516 return reinterpret_cast<jlong>(xpcomObject);
517}
518
519extern "C" NS_EXPORT jobject JNICALL
520JXUTILS_NATIVE(wrapXPCOMObject) (JNIEnv* env, jobject, jlong aXPCOMObject,
521 jstring aIID)
522{
523 nsresult rv;
524 jobject javaObject = nsnull;
525 nsISupports* xpcomObject = reinterpret_cast<nsISupports*>(aXPCOMObject);
526
527 if (!xpcomObject || !aIID) {
528 rv = NS_ERROR_NULL_POINTER;
529 } else {
530 const char* str = env->GetStringUTFChars(aIID, nsnull);
531 if (!str) {
532 rv = NS_ERROR_OUT_OF_MEMORY;
533 } else {
534 nsID iid;
535 if (iid.Parse(str)) {
536 // XXX Should we be passing something other than NULL for aObjectLoader?
537 rv = NativeInterfaceToJavaObject(env, xpcomObject, iid, nsnull,
538 &javaObject);
539 } else {
540 rv = NS_ERROR_INVALID_ARG;
541 }
542
543 env->ReleaseStringUTFChars(aIID, str);
544 }
545 }
546
547 if (NS_FAILED(rv)) {
548 ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
549 }
550 return javaObject;
551}
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