2004-09-02 16:34:11 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Java XPCOM Bindings.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* IBM Corporation.
|
2005-05-03 20:15:35 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
2004-09-02 16:34:11 +00:00
|
|
|
* IBM Corporation. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Javier Pedemonte (jhpedemonte@gmail.com)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
#include "nsJavaInterfaces.h"
|
2004-09-02 16:34:11 +00:00
|
|
|
#include "nsJavaWrapper.h"
|
|
|
|
#include "nsJavaXPCOMBindingUtils.h"
|
|
|
|
#include "nsJavaXPTCStub.h"
|
2004-12-17 22:25:12 +00:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2004-09-02 16:34:11 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIInterfaceInfoManager.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsEnumeratorUtils.h"
|
2004-10-01 22:59:12 +00:00
|
|
|
#include "nsArray.h"
|
2004-12-17 22:25:12 +00:00
|
|
|
#include "nsAppFileLocProviderProxy.h"
|
2005-02-23 20:19:55 +00:00
|
|
|
#include "nsIEventQueueService.h"
|
2005-10-28 01:59:05 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "nsILocalFile.h"
|
2004-09-02 16:34:11 +00:00
|
|
|
|
|
|
|
|
2005-02-23 20:19:55 +00:00
|
|
|
nsresult
|
2005-10-28 01:59:05 +00:00
|
|
|
InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory,
|
|
|
|
jobject aAppDirectory, jobject aAppDirProvider)
|
2005-02-23 20:19:55 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!InitializeJavaGlobals(env))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// create an nsILocalFile from given java.io.File
|
2005-10-28 01:59:05 +00:00
|
|
|
nsCOMPtr<nsILocalFile> libXULDir;
|
|
|
|
if (aLibXULDirectory) {
|
|
|
|
rv = File_to_nsILocalFile(env, aLibXULDirectory, getter_AddRefs(libXULDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILocalFile> appDir;
|
|
|
|
if (aAppDirectory) {
|
|
|
|
rv = File_to_nsILocalFile(env, aAppDirectory, getter_AddRefs(appDir));
|
2005-02-23 20:19:55 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create nsAppFileLocProviderProxy from given Java object
|
2005-07-13 22:15:09 +00:00
|
|
|
nsCOMPtr<nsIDirectoryServiceProvider> provider;
|
2005-10-28 01:59:05 +00:00
|
|
|
if (aAppDirProvider) {
|
|
|
|
rv = NS_NewAppFileLocProviderProxy(aAppDirProvider,
|
2005-07-13 22:15:09 +00:00
|
|
|
getter_AddRefs(provider));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-02-23 20:19:55 +00:00
|
|
|
}
|
|
|
|
|
2005-10-28 01:59:05 +00:00
|
|
|
// init libXUL
|
2005-11-23 14:16:33 +00:00
|
|
|
rv = XRE_InitEmbedding(libXULDir, appDir, provider, nsnull, 0);
|
2005-02-23 20:19:55 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT void
|
|
|
|
GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory,
|
|
|
|
jobject aAppDirectory, jobject aAppDirProvider)
|
2004-09-02 16:34:11 +00:00
|
|
|
{
|
2005-10-28 01:59:05 +00:00
|
|
|
nsresult rv = InitEmbedding_Impl(env, aLibXULDirectory, aAppDirectory,
|
|
|
|
aAppDirProvider);
|
2004-09-15 21:06:30 +00:00
|
|
|
|
2005-02-23 20:19:55 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ThrowException(env, rv, "Failure in initEmbedding");
|
|
|
|
FreeJavaGlobals(env);
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
2004-12-17 22:25:12 +00:00
|
|
|
}
|
2004-09-02 16:34:11 +00:00
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT void
|
2005-10-28 01:59:05 +00:00
|
|
|
GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject)
|
2004-12-17 22:25:12 +00:00
|
|
|
{
|
2005-10-28 01:59:05 +00:00
|
|
|
// Free globals before calling XRE_TermEmbedding(), since we need some
|
2005-02-24 23:17:36 +00:00
|
|
|
// XPCOM services.
|
|
|
|
FreeJavaGlobals(env);
|
|
|
|
|
2005-10-28 01:59:05 +00:00
|
|
|
XRE_TermEmbedding();
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-02-23 20:19:55 +00:00
|
|
|
nsresult
|
|
|
|
InitXPCOM_Impl(JNIEnv* env, jobject aMozBinDirectory,
|
|
|
|
jobject aAppFileLocProvider, jobject* aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!InitializeJavaGlobals(env))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// create an nsILocalFile from given java.io.File
|
|
|
|
nsCOMPtr<nsILocalFile> directory;
|
|
|
|
if (aMozBinDirectory) {
|
|
|
|
rv = File_to_nsILocalFile(env, aMozBinDirectory, getter_AddRefs(directory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create nsAppFileLocProviderProxy from given Java object
|
|
|
|
nsAppFileLocProviderProxy* provider = nsnull;
|
|
|
|
if (aAppFileLocProvider) {
|
2005-05-03 20:15:35 +00:00
|
|
|
provider = new nsAppFileLocProviderProxy(aAppFileLocProvider);
|
2005-02-23 20:19:55 +00:00
|
|
|
if (!provider)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init XPCOM
|
|
|
|
nsIServiceManager* servMan = nsnull;
|
|
|
|
rv = NS_InitXPCOM2(&servMan, directory, provider);
|
|
|
|
if (provider) {
|
|
|
|
delete provider;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// init Event Queue
|
|
|
|
nsCOMPtr<nsIEventQueueService>
|
|
|
|
eventQService(do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// create Java proxy for service manager returned by NS_InitXPCOM2
|
|
|
|
rv = CreateJavaProxy(env, servMan, NS_GET_IID(nsIServiceManager),
|
|
|
|
aResult);
|
|
|
|
NS_RELEASE(servMan); // Java proxy has owning ref
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT jobject
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory,
|
2004-12-17 22:25:12 +00:00
|
|
|
jobject aAppFileLocProvider)
|
2004-09-02 16:34:11 +00:00
|
|
|
{
|
2005-02-23 20:19:55 +00:00
|
|
|
jobject servMan;
|
|
|
|
nsresult rv = InitXPCOM_Impl(env, aMozBinDirectory, aAppFileLocProvider,
|
|
|
|
&servMan);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return servMan;
|
2004-12-17 22:25:12 +00:00
|
|
|
|
2005-01-12 17:59:01 +00:00
|
|
|
ThrowException(env, rv, "Failure in initXPCOM");
|
2004-12-21 23:35:02 +00:00
|
|
|
FreeJavaGlobals(env);
|
2004-12-17 22:25:12 +00:00
|
|
|
return nsnull;
|
|
|
|
}
|
2004-09-02 16:34:11 +00:00
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT void
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr)
|
2004-12-17 22:25:12 +00:00
|
|
|
{
|
2005-02-23 20:19:55 +00:00
|
|
|
nsresult rv;
|
2004-12-17 22:25:12 +00:00
|
|
|
nsCOMPtr<nsIServiceManager> servMgr;
|
|
|
|
if (aServMgr) {
|
2005-02-23 20:19:55 +00:00
|
|
|
// Get native XPCOM instance
|
|
|
|
void* xpcom_obj;
|
|
|
|
rv = GetXPCOMInstFromProxy(env, aServMgr, &xpcom_obj);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get XPCOM obj for ServiceMgr.");
|
2004-12-17 22:25:12 +00:00
|
|
|
|
2005-01-12 17:59:01 +00:00
|
|
|
// Even if we failed to get the matching xpcom object, we don't abort this
|
|
|
|
// function. Just call NS_ShutdownXPCOM with a null service manager.
|
|
|
|
|
2005-02-23 20:19:55 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj);
|
|
|
|
servMgr = do_QueryInterface(inst->GetInstance());
|
2005-01-12 17:59:01 +00:00
|
|
|
}
|
2004-12-17 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
2005-02-24 23:17:36 +00:00
|
|
|
// Free globals before calling NS_ShutdownXPCOM(), since we need some
|
|
|
|
// XPCOM services.
|
|
|
|
FreeJavaGlobals(env);
|
|
|
|
|
2005-02-23 20:19:55 +00:00
|
|
|
rv = NS_ShutdownXPCOM(servMgr);
|
2004-09-02 16:34:11 +00:00
|
|
|
if (NS_FAILED(rv))
|
2005-01-12 17:59:01 +00:00
|
|
|
ThrowException(env, rv, "NS_ShutdownXPCOM failed");
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT jobject
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath,
|
2004-10-15 21:50:15 +00:00
|
|
|
jboolean aFollowLinks)
|
2004-09-02 16:34:11 +00:00
|
|
|
{
|
|
|
|
// Create a Mozilla string from the jstring
|
|
|
|
const PRUnichar* buf = nsnull;
|
|
|
|
if (aPath) {
|
2005-01-20 23:59:10 +00:00
|
|
|
buf = env->GetStringChars(aPath, nsnull);
|
2005-01-12 17:59:01 +00:00
|
|
|
if (!buf)
|
|
|
|
return nsnull; // exception already thrown
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString path_str(buf);
|
2005-01-20 23:59:10 +00:00
|
|
|
env->ReleaseStringChars(aPath, buf);
|
2004-09-02 16:34:11 +00:00
|
|
|
|
|
|
|
// Make call to given function
|
2004-10-27 17:10:03 +00:00
|
|
|
nsILocalFile* file = nsnull;
|
|
|
|
nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, &file);
|
2004-09-02 16:34:11 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-01-20 22:07:26 +00:00
|
|
|
jobject javaProxy;
|
|
|
|
rv = CreateJavaProxy(env, file, NS_GET_IID(nsILocalFile), &javaProxy);
|
2004-10-27 17:10:03 +00:00
|
|
|
NS_RELEASE(file); // JavaXPCOMInstance has owning ref
|
2005-01-20 22:07:26 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return javaProxy;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 00:09:49 +00:00
|
|
|
ThrowException(env, rv, "Failure in newLocalFile");
|
|
|
|
return nsnull;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT jobject
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject)
|
2004-09-02 16:34:11 +00:00
|
|
|
{
|
|
|
|
// Call XPCOM method
|
2004-10-27 17:10:03 +00:00
|
|
|
nsIComponentManager* cm = nsnull;
|
|
|
|
nsresult rv = NS_GetComponentManager(&cm);
|
2004-09-02 16:34:11 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-01-20 22:07:26 +00:00
|
|
|
jobject javaProxy;
|
|
|
|
rv = CreateJavaProxy(env, cm, NS_GET_IID(nsIComponentManager), &javaProxy);
|
2004-10-27 17:10:03 +00:00
|
|
|
NS_RELEASE(cm); // JavaXPCOMInstance has owning ref
|
2005-01-20 22:07:26 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return javaProxy;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 00:09:49 +00:00
|
|
|
ThrowException(env, rv, "Failure in getComponentManager");
|
|
|
|
return nsnull;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT jobject
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject)
|
2004-12-17 22:25:12 +00:00
|
|
|
{
|
|
|
|
// Call XPCOM method
|
|
|
|
nsIComponentRegistrar* cr = nsnull;
|
|
|
|
nsresult rv = NS_GetComponentRegistrar(&cr);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-01-20 22:07:26 +00:00
|
|
|
jobject javaProxy;
|
|
|
|
rv = CreateJavaProxy(env, cr, NS_GET_IID(nsIComponentRegistrar), &javaProxy);
|
2004-12-17 22:25:12 +00:00
|
|
|
NS_RELEASE(cr); // JavaXPCOMInstance has owning ref
|
2005-01-20 22:07:26 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return javaProxy;
|
2004-12-17 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 00:09:49 +00:00
|
|
|
ThrowException(env, rv, "Failure in getComponentRegistrar");
|
|
|
|
return nsnull;
|
2004-12-17 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 14:17:20 +00:00
|
|
|
extern "C" NS_EXPORT jobject
|
2005-10-28 01:59:05 +00:00
|
|
|
XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject)
|
2004-09-02 16:34:11 +00:00
|
|
|
{
|
|
|
|
// Call XPCOM method
|
2004-10-27 17:10:03 +00:00
|
|
|
nsIServiceManager* sm = nsnull;
|
|
|
|
nsresult rv = NS_GetServiceManager(&sm);
|
2004-09-02 16:34:11 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-01-20 22:07:26 +00:00
|
|
|
jobject javaProxy;
|
|
|
|
rv = CreateJavaProxy(env, sm, NS_GET_IID(nsIServiceManager), &javaProxy);
|
2004-10-27 17:10:03 +00:00
|
|
|
NS_RELEASE(sm); // JavaXPCOMInstance has owning ref
|
2005-01-20 22:07:26 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return javaProxy;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 00:09:49 +00:00
|
|
|
ThrowException(env, rv, "Failure in getServiceManager");
|
|
|
|
return nsnull;
|
2004-09-02 16:34:11 +00:00
|
|
|
}
|
|
|
|
|