VirtualBox

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

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