A weak reference implementation. Not part of default build.

git-svn-id: svn://10.0.0.236/trunk@162417 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
pedemont%us.ibm.com 2004-09-15 21:50:41 +00:00
parent 0410a075b8
commit 6573858b6d
6 changed files with 227 additions and 72 deletions

View File

@ -58,6 +58,7 @@ CPPSRCS = \
nsJavaInterfaces.cpp \
nsJavaWrapper.cpp \
nsJavaXPTCStub.cpp \
nsJavaXPTCStubWeakRef.cpp \
nsJavaXPCOMBindingUtils.cpp \
$(NULL)

View File

@ -320,19 +320,22 @@ SetupParams(JNIEnv *env, const jobject aParam,
// Check if we already have a corresponding XPCOM object
void* inst = GetMatchingXPCOMObject(env, data);
if (inst == nsnull) {
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(aIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams, PR_TRUE,
iid);
if (NS_FAILED(rv))
return rv;
PRBool isWeakRef = iid.Equals(NS_GET_IID(nsIWeakReference));
if (inst == nsnull && !isWeakRef) {
// If there is not corresponding XPCOM object, then that means that the
// parameter is non-generated class (that is, it is not one of our
// Java stubs that represent an exising XPCOM object). So we need to
// create an XPCOM stub, that can route any method calls to the class.
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(aIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams, PR_TRUE, iid);
if (NS_FAILED(rv))
return rv;
// Get interface info for class
nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
nsCOMPtr<nsIInterfaceInfo> iinfo;
@ -343,34 +346,14 @@ SetupParams(JNIEnv *env, const jobject aParam,
inst = SetAsXPTCStub(xpcomStub);
AddJavaXPCOMBinding(env, data, inst);
}
#if 1
// XXX This code is here to make sure that the nsJavaXPTCStub that is
// used has the correct (asked for) interface info. For example, in
// the Java code, the nsIWeakReference functions return the current
// Java object ('this'); so when we call GetMatchingXPCOMObject on
// that Java object, we get back a nsJavaXPTCStub that has the interface
// info for something other than nsIWeakReference. This will cause
// problems later when we try to call one of the nsIWeakReference
// functions on that Java object, but the nsJavaXPTCStub object doesn't
// know anything about nsIWeakReference.
else {
if (IsXPTCStub(inst)) {
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(aIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams, PR_TRUE, iid);
if (NS_FAILED(rv))
return rv;
void* temp;
rv = GetXPTCStubAddr(inst)->QueryInterface(iid, &temp);
if (NS_SUCCEEDED(rv) && temp != GetXPTCStubAddr(inst))
inst = SetAsXPTCStub((nsJavaXPTCStub*) temp);
}
}
#endif
if (IsXPTCStub(inst)) {
if (isWeakRef) {
// If the function expects an weak reference, then we need to
// create it here.
nsJavaXPTCStubWeakRef* weakref = new nsJavaXPTCStubWeakRef(env, data);
NS_ADDREF(weakref);
aVariant.val.p = aVariant.ptr = (void*) weakref;
} else if (IsXPTCStub(inst)) {
nsJavaXPTCStub* xpcomStub = GetXPTCStubAddr(inst);
NS_ADDREF(xpcomStub);
aVariant.val.p = aVariant.ptr = (void*) xpcomStub;

View File

@ -127,6 +127,14 @@ nsJavaXPTCStub::QueryInterface(const nsID &aIID, void **aInstancePtr)
return NS_OK;
}
// All Java objects support weak references
if (aIID.Equals(NS_GET_IID(nsISupportsWeakReference)))
{
*aInstancePtr = (void*) NS_STATIC_CAST(nsISupportsWeakReference*, master);
NS_ADDREF(master);
return NS_OK;
}
// does any existing stub support the requested IID?
nsJavaXPTCStub *stub = master->FindStubSupportingIID(aIID);
if (stub)
@ -1036,16 +1044,20 @@ nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
}
if (java_obj) {
// Check if we already have a corresponding XPCOM object
void* inst = GetMatchingXPCOMObject(mJavaEnv, java_obj);
if (inst == nsnull) {
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(mIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams,
PR_FALSE, iid);
if (NS_FAILED(rv))
return rv;
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(mIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams,
PR_FALSE, iid);
if (NS_FAILED(rv))
return rv;
PRBool isWeakRef = iid.Equals(NS_GET_IID(nsIWeakReference));
if (inst == nsnull && !isWeakRef) {
// Get interface info for class
nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
nsCOMPtr<nsIInterfaceInfo> iinfo;
@ -1057,35 +1069,15 @@ nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
inst = SetAsXPTCStub(xpcomStub);
AddJavaXPCOMBinding(mJavaEnv, java_obj, inst);
}
#if 1
// XXX This code is here to make sure that the nsJavaXPTCStub that is
// used has the correct (asked for) interface info. For example, in
// the Java code, the nsIWeakReference functions return the current
// Java object ('this'); so when we call GetMatchingXPCOMObject on
// that Java object, we get back a nsJavaXPTCStub that has the interface
// info for something other than nsIWeakReference. This will cause
// problems later when we try to call one of the nsIWeakReference
// functions on that Java object, but the nsJavaXPTCStub object doesn't
// know anything about nsIWeakReference.
else {
if (IsXPTCStub(inst)) {
// Get IID for this param
nsID iid;
rv = GetIIDForMethodParam(mIInfo, aMethodInfo, aParamInfo,
aMethodIndex, aDispatchParams,
PR_FALSE, iid);
if (NS_FAILED(rv))
return rv;
void* temp;
rv = GetXPTCStubAddr(inst)->QueryInterface(iid, &temp);
if (NS_SUCCEEDED(rv) && temp != GetXPTCStubAddr(inst))
inst = SetAsXPTCStub((nsJavaXPTCStub*) temp);
}
}
#endif
if (IsXPTCStub(inst)) {
if (isWeakRef) {
// If the function expects an weak reference, then we need to
// create it here.
nsJavaXPTCStubWeakRef* weakref =
new nsJavaXPTCStubWeakRef(mJavaEnv, java_obj);
NS_ADDREF(weakref);
*((void **) aVariant.val.p) = (void*) weakref;
} else if (IsXPTCStub(inst)) {
nsJavaXPTCStub* xpcomStub = GetXPTCStubAddr(inst);
NS_ADDREF(xpcomStub);
*((void **) aVariant.val.p) = (void*) xpcomStub;
@ -1173,3 +1165,22 @@ nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
return NS_OK;
}
NS_IMETHODIMP
nsJavaXPTCStub::GetWeakReference(nsIWeakReference** aInstancePtr)
{
if (mMaster)
return mMaster->GetWeakReference(aInstancePtr);
LOG("==> nsJavaXPTCStub::GetWeakReference()\n");
if (!aInstancePtr)
return NS_ERROR_NULL_POINTER;
nsJavaXPTCStubWeakRef* weakref =
new nsJavaXPTCStubWeakRef(mJavaEnv, mJavaObject);
*aInstancePtr = weakref;
NS_ADDREF(*aInstancePtr);
return NS_OK;
}

View File

@ -43,12 +43,16 @@
#include "nsVoidArray.h"
#include "nsIInterfaceInfo.h"
#include "nsCOMPtr.h"
#include "nsWeakReference.h"
#include "nsJavaXPTCStubWeakRef.h"
class nsJavaXPTCStub : public nsXPTCStubBase
class nsJavaXPTCStub : public nsXPTCStubBase,
public nsSupportsWeakReference
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISUPPORTSWEAKREFERENCE
nsJavaXPTCStub(JNIEnv* aJavaEnv, jobject aJavaObject, nsIInterfaceInfo *aIInfo);

View File

@ -0,0 +1,98 @@
/* ***** 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.
* Portions created by the Initial Developer are Copyright (C) 2004
* 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 ***** */
#include "jni.h"
#include "nsJavaXPTCStubWeakRef.h"
#include "nsJavaXPTCStub.h"
#include "nsJavaXPCOMBindingUtils.h"
#include "nsIInterfaceInfoManager.h"
nsJavaXPTCStubWeakRef::nsJavaXPTCStubWeakRef(JNIEnv* env, jobject aJavaObject)
{
mJavaEnv = env;
mWeakRef = mJavaEnv->NewWeakGlobalRef(aJavaObject);
}
nsJavaXPTCStubWeakRef::~nsJavaXPTCStubWeakRef()
{
mJavaEnv->DeleteWeakGlobalRef(mWeakRef);
}
NS_IMPL_ADDREF(nsJavaXPTCStubWeakRef)
NS_IMPL_RELEASE(nsJavaXPTCStubWeakRef)
NS_IMPL_THREADSAFE_QUERY_INTERFACE1(nsJavaXPTCStubWeakRef, nsIWeakReference)
NS_IMETHODIMP
nsJavaXPTCStubWeakRef::QueryReferent(const nsIID& aIID, void** aInstancePtr)
{
// Is weak ref still valid?
// We create a strong local ref to make sure Java object isn't garbage
// collected during this call.
jobject javaObject = mJavaEnv->NewLocalRef(mWeakRef);
if (!javaObject)
return NS_ERROR_NULL_POINTER;
// Java object has not been garbage collected. Do we have an
// associated nsJavaXPTCStub?
void* inst = GetMatchingXPCOMObject(mJavaEnv, javaObject);
NS_ASSERTION(IsXPTCStub(inst), "Found xpcom object was not an XPTCStub");
if (!inst) {
// No XPTCStub exists, so create one
// Get interface info for class
nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
nsCOMPtr<nsIInterfaceInfo> iinfo;
iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
// Create XPCOM stub
nsJavaXPTCStub* xpcomStub = new nsJavaXPTCStub(mJavaEnv, javaObject, iinfo);
NS_ADDREF(xpcomStub);
AddJavaXPCOMBinding(mJavaEnv, javaObject, SetAsXPTCStub(xpcomStub));
// return created stub
*aInstancePtr = (void*) xpcomStub;
return NS_OK;
}
// We have an exising XPTCStub, so return QI result
nsJavaXPTCStub* xpcomStub = GetXPTCStubAddr(inst);
return xpcomStub->QueryInterface(aIID, aInstancePtr);
}

View File

@ -0,0 +1,58 @@
/* ***** 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.
* Portions created by the Initial Developer are Copyright (C) 2004
* 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 ***** */
#ifndef _nsJavaXPTCStubWeakRef_h_
#define _nsJavaXPTCStubWeakRef_h_
#include "jni.h"
#include "nsIWeakReference.h"
class nsJavaXPTCStubWeakRef : public nsIWeakReference
{
public:
nsJavaXPTCStubWeakRef(JNIEnv* env, jobject aJavaObject);
virtual ~nsJavaXPTCStubWeakRef();
NS_DECL_ISUPPORTS
NS_DECL_NSIWEAKREFERENCE;
protected:
JNIEnv* mJavaEnv;
jweak mWeakRef;
};
#endif // _nsJavaXPTCStubWeakRef_h_