VirtualBox

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

Last change on this file since 107722 was 107722, checked in by vboxsync, 4 months ago

xpcom18a4/java/src/nsJavaInterfaces.cpp: Small restructuring, bugref:3409

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