Commit 90ff3d47 authored by Simon Morlat's avatar Simon Morlat
Browse files

better management of LinphoneAddress in java.

Indeed there was some risk of getting into a situation where a java LinphoneAddress would point to a destroyed C LinphoneAddress (typically with call-logs).
To prevent that, the java object can either refcount or clone the C object, depending on the constness of the C object.
parent bd95305d
......@@ -1566,10 +1566,23 @@ extern "C" jlong Java_org_linphone_core_LinphoneAddressImpl_newLinphoneAddressIm
return (jlong) address;
}
extern "C" void Java_org_linphone_core_LinphoneAddressImpl_delete(JNIEnv* env
extern "C" jlong Java_org_linphone_core_LinphoneAddressImpl_ref(JNIEnv* env
,jobject thiz
,jlong ptr) {
return (jlong)linphone_address_ref((LinphoneAddress*)ptr);
}
extern "C" jlong Java_org_linphone_core_LinphoneAddressImpl_clone(JNIEnv* env
,jobject thiz
,jlong ptr) {
return (jlong) (ptr ? linphone_address_clone((const LinphoneAddress*)ptr) : NULL);
}
extern "C" void Java_org_linphone_core_LinphoneAddressImpl_unref(JNIEnv* env
,jobject thiz
,jlong ptr) {
linphone_address_destroy((LinphoneAddress*)ptr);
linphone_address_unref((LinphoneAddress*)ptr);
}
extern "C" jstring Java_org_linphone_core_LinphoneAddressImpl_getDisplayName(JNIEnv* env
......
......@@ -21,10 +21,16 @@ package org.linphone.core;
public class LinphoneAddressImpl implements LinphoneAddress {
public enum WrapMode{
FromNew,
FromConst,
FromExisting
};
protected final long nativePtr;
boolean ownPtr = false;
private native long newLinphoneAddressImpl(String uri,String displayName);
private native void delete(long ptr);
private native long ref(long ptr);
private native void unref(long ptr);
private native long clone(long ptr);
private native String getDisplayName(long ptr);
private native String getUserName(long ptr);
private native String getDomain(long ptr);
......@@ -46,16 +52,28 @@ public class LinphoneAddressImpl implements LinphoneAddress {
this.setUserName(username);
this.setDomain(domain);
}
protected LinphoneAddressImpl(long aNativePtr,boolean javaOwnPtr) {
nativePtr = aNativePtr;
ownPtr=javaOwnPtr;
}
protected LinphoneAddressImpl(long aNativePtr) {
nativePtr = aNativePtr;
ownPtr=false;
//this method is there because JNI is calling it.
private LinphoneAddressImpl(long aNativeptr){
this(aNativeptr,WrapMode.FromConst);
}
protected LinphoneAddressImpl(long aNativePtr, WrapMode mode) {
switch(mode){
case FromNew:
nativePtr=aNativePtr;
break;
case FromConst:
nativePtr=clone(aNativePtr);
break;
case FromExisting:
nativePtr=ref(aNativePtr);
break;
default:
nativePtr=0;
}
}
protected void finalize() throws Throwable {
if (ownPtr) delete(nativePtr);
if (nativePtr!=0) unref(nativePtr);
}
public String getDisplayName() {
return getDisplayName(nativePtr);
......
......@@ -18,7 +18,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.linphone.core;
class LinphoneCallImpl implements LinphoneCall {
protected final long nativePtr;
......@@ -81,7 +80,7 @@ class LinphoneCallImpl implements LinphoneCall {
public LinphoneAddress getRemoteAddress() {
long lNativePtr = getRemoteAddress(nativePtr);
if (lNativePtr!=0) {
return new LinphoneAddressImpl(lNativePtr);
return new LinphoneAddressImpl(lNativePtr,LinphoneAddressImpl.WrapMode.FromConst);
} else {
return null;
}
......
......@@ -41,11 +41,11 @@ class LinphoneCallLogImpl implements LinphoneCallLog {
}
public LinphoneAddress getFrom() {
return new LinphoneAddressImpl(getFrom(nativePtr));
return new LinphoneAddressImpl(getFrom(nativePtr),LinphoneAddressImpl.WrapMode.FromExisting);
}
public LinphoneAddress getTo() {
return new LinphoneAddressImpl(getTo(nativePtr));
return new LinphoneAddressImpl(getTo(nativePtr),LinphoneAddressImpl.WrapMode.FromExisting);
}
public CallStatus getStatus() {
return LinphoneCallLog.CallStatus.fromInt(getStatus(nativePtr));
......
......@@ -42,7 +42,7 @@ public class LinphoneChatMessageImpl implements LinphoneChatMessage {
@Override
public LinphoneAddress getPeerAddress() {
return new LinphoneAddressImpl(getPeerAddress(nativePtr));
return new LinphoneAddressImpl(getPeerAddress(nativePtr),LinphoneAddressImpl.WrapMode.FromConst);
}
@Override
......@@ -57,7 +57,7 @@ public class LinphoneChatMessageImpl implements LinphoneChatMessage {
@Override
public LinphoneAddress getFrom() {
return new LinphoneAddressImpl(getFrom(nativePtr));
return new LinphoneAddressImpl(getFrom(nativePtr),LinphoneAddressImpl.WrapMode.FromConst);
}
private native void addCustomHeader(long nativePtr, String name, String value);
......
......@@ -43,7 +43,7 @@ class LinphoneChatRoomImpl implements LinphoneChatRoom {
}
public LinphoneAddress getPeerAddress() {
return new LinphoneAddressImpl(getPeerAddress(nativePtr));
return new LinphoneAddressImpl(getPeerAddress(nativePtr),LinphoneAddressImpl.WrapMode.FromConst);
}
public void sendMessage(String message) {
......
......@@ -216,7 +216,7 @@ class LinphoneCoreImpl implements LinphoneCore {
if (ptr==0) {
return null;
} else {
return new LinphoneAddressImpl(ptr);
return new LinphoneAddressImpl(ptr,LinphoneAddressImpl.WrapMode.FromConst);
}
}
public synchronized boolean isIncall() {
......@@ -267,7 +267,7 @@ class LinphoneCoreImpl implements LinphoneCore {
public synchronized LinphoneAddress interpretUrl(String destination) throws LinphoneCoreException {
long lAddress = interpretUrl(nativePtr,destination);
if (lAddress != 0) {
return new LinphoneAddressImpl(lAddress,true);
return new LinphoneAddressImpl(lAddress,LinphoneAddressImpl.WrapMode.FromNew);
} else {
throw new LinphoneCoreException("Cannot interpret ["+destination+"]");
}
......
......@@ -54,7 +54,7 @@ class LinphoneFriendImpl implements LinphoneFriend, Serializable {
this.setAddress(nativePtr, ((LinphoneAddressImpl)anAddress).nativePtr);
}
public LinphoneAddress getAddress() {
return new LinphoneAddressImpl(getAddress(nativePtr));
return new LinphoneAddressImpl(getAddress(nativePtr),LinphoneAddressImpl.WrapMode.FromConst);
}
public void setIncSubscribePolicy(SubscribePolicy policy) {
setIncSubscribePolicy(nativePtr,policy.mValue);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment