/soc/2012/michael/android: 02e3280b04b8: Mark DnsQueryData objec...

Michael Zangl michael at soc.pidgin.im
Sun Jul 29 08:20:09 EDT 2012


Changeset: 02e3280b04b8951321e79fc502a146c9edce4e69
Author:	 Michael Zangl <michael at soc.pidgin.im>
Date:	 2012-07-29 14:19 +0200
Branch:	 soc.2012.android
URL: http://hg.pidgin.im/soc/2012/michael/android/rev/02e3280b04b8

Description:

Mark DnsQueryData objects as destroyed and some improvements to CALLBACK_START makros.

diffstat:

 android/workspace/im.pidgin.libpurple/native/DnsQueryData.c                              |  18 +++
 android/workspace/im.pidgin.libpurple/native/DnsQueryUiOps.c                             |   8 +
 android/workspace/im.pidgin.libpurple/native/helpers.c                                   |  20 +++-
 android/workspace/im.pidgin.libpurple/native/helpers.h                                   |  47 +++++++--
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java |   4 +
 5 files changed, 82 insertions(+), 15 deletions(-)

diffs (177 lines):

diff --git a/android/workspace/im.pidgin.libpurple/native/DnsQueryData.c b/android/workspace/im.pidgin.libpurple/native/DnsQueryData.c
--- a/android/workspace/im.pidgin.libpurple/native/DnsQueryData.c
+++ b/android/workspace/im.pidgin.libpurple/native/DnsQueryData.c
@@ -12,6 +12,24 @@ getNativeDnsData(JNIEnv *env, jobject ob
  * Class:     im_pidgin_libpurple_core_dns_DnsQueryData
  * Method:    getHost_native
  * Signature: ()Ljava/lang/String;
+ */JNIEXPORT void JNICALL
+Java_im_pidgin_libpurple_core_dns_DnsQueryData_register_1native(JNIEnv *env,
+		jobject object)
+{
+	PurpleDnsQueryData* object_native;
+	JavaObjectReference *ref;
+
+	object_native = getNativeDnsData(env, object);
+	ref = g_new0(JavaObjectReference, 1);
+	setJavaObject(ref, env, object);
+
+	purple_dnsquery_set_ui_data(object_native, ref);
+}
+
+/*
+ * Class:     im_pidgin_libpurple_core_dns_DnsQueryData
+ * Method:    getHost_native
+ * Signature: ()Ljava/lang/String;
  */JNIEXPORT jstring JNICALL
 Java_im_pidgin_libpurple_core_dns_DnsQueryData_getHost_1native(JNIEnv *env,
 		jobject object)
diff --git a/android/workspace/im.pidgin.libpurple/native/DnsQueryUiOps.c b/android/workspace/im.pidgin.libpurple/native/DnsQueryUiOps.c
--- a/android/workspace/im.pidgin.libpurple/native/DnsQueryUiOps.c
+++ b/android/workspace/im.pidgin.libpurple/native/DnsQueryUiOps.c
@@ -1,6 +1,7 @@
 #include "DnsQueryUiOps.h"
 #include "helpers.h"
 #include <libpurple/dnsquery.h>
+#include "DnsQueryData.h"
 
 static JavaObjectReference dns = JAVA_NULL_OBJECT_REF;
 
@@ -32,7 +33,14 @@ resolve_host(PurpleDnsQueryData* query_d
 }
 
 static void destroy(PurpleDnsQueryData *query_data) {
+	JavaObjectReference *ref;
 
+	CALLBACK_START_VOID(&dns);
+
+	ref = purple_dnsquery_get_ui_data(query_data);
+
+	notifyPeerDestroyed(env, ref->handlerObject);
+	unsetJavaObject(ref, env);
 }
 
 static PurpleDnsQueryUiOps uiOps = {
diff --git a/android/workspace/im.pidgin.libpurple/native/helpers.c b/android/workspace/im.pidgin.libpurple/native/helpers.c
--- a/android/workspace/im.pidgin.libpurple/native/helpers.c
+++ b/android/workspace/im.pidgin.libpurple/native/helpers.c
@@ -50,9 +50,6 @@ unsetJavaObject(JavaObjectReference *ref
 	ref->jvm = NULL;
 }
 
-/**
- * Gets the peer field of an object of the peer class.
- */
 void *
 getNativePointer(JNIEnv *env, jobject peered)
 {
@@ -75,9 +72,22 @@ getNativePointer(JNIEnv *env, jobject pe
 	return longToP((*env)->GetLongField(env, peered, peerField));
 }
 
+void
+notifyPeerDestroyed(JNIEnv *env, jobject peered)
+{
+	static JavaMethodIDCache methodCache =
+			METHOD_CACHE("nativeObjectFreed", "()V");
+	jclass class;
+	jmethodID mid;
+
+	class = (*env)->FindClass(env, "im/pidgin/libpurple/peering/Peered");
+	mid = getMethodIDCachedReferenced(env, class, &methodCache);
+
+	(*env)->CallVoidMethod(env, peered, mid);
+}
+
 jmethodID
-getMethodIDCachedReferenced(JNIEnv *env, jclass class,
-		JavaMethodIDCache *cache)
+getMethodIDCachedReferenced(JNIEnv *env, jclass class, JavaMethodIDCache *cache)
 {
 	g_return_val_if_fail(env != NULL, NULL);
 	g_return_val_if_fail(class != NULL, NULL);
diff --git a/android/workspace/im.pidgin.libpurple/native/helpers.h b/android/workspace/im.pidgin.libpurple/native/helpers.h
--- a/android/workspace/im.pidgin.libpurple/native/helpers.h
+++ b/android/workspace/im.pidgin.libpurple/native/helpers.h
@@ -65,27 +65,54 @@ typedef struct {
  * Checks the jvm context, adds the *env-Variable that points to the java enviroment.
  * If it fails, just logs the error and calls return.
  */
-#define CALLBACK_START_VOID(context)  CALLBACK_START(context, )
+#define CALLBACK_START_VOID(context) \
+		JNIEnv *env; \
+		g_return_if_fail((context)->jvm != NULL); \
+		g_return_if_fail((context)->handlerObject != NULL); \
+		g_return_if_fail((*(context)->jvm)->GetEnv((context)->jvm, (void**)&env, JNI_VERSION_1_2) != JNI_EDETACHED);
 
 /**
  * Same as CALLBACK_START_VOID, but returns a value.
  */
 #define CALLBACK_START(context, failval) \
 		JNIEnv *env; \
-		if ((context)->jvm == NULL || (context)->handlerObject == NULL || (*(context)->jvm)->GetEnv((context)->jvm, (void**)&env, JNI_VERSION_1_2) == JNI_EDETACHED) {\
-			__android_log_print(ANDROID_LOG_DEBUG, "jni",\
-					"Error: No callback object registered or in wrong thread");\
-			return failval;\
-		}
+		g_return_val_if_fail((context)->jvm != NULL, failval); \
+		g_return_val_if_fail((context)->handlerObject != NULL, failval);  \
+		g_return_val_if_fail((*(context)->jvm)->GetEnv((context)->jvm, (void**)&env, JNI_VERSION_1_2) != JNI_EDETACHED, failval);
 
 
-gboolean setJavaObject(JavaObjectReference *ref, JNIEnv *env, jobject object);
+/**
+ * Stores a reference to the given java object in the reference
+ * location.
+ */
+gboolean
+setJavaObject(JavaObjectReference *ref, JNIEnv *env, jobject object);
 
-void unsetJavaObject(JavaObjectReference *ref, JNIEnv *env);
+/**
+ * Deletes a reference to the java object that was stored.
+ */
+void
+unsetJavaObject(JavaObjectReference *ref, JNIEnv *env);
 
-void *getNativePointer(JNIEnv *env, jobject peered);
+/**
+ * Gets the native pointer field from a Peered object.
+ */
+void *
+getNativePointer(JNIEnv *env, jobject peered);
 
-jmethodID getMethodIDCachedReferenced(JNIEnv *env, jclass class, JavaMethodIDCache *cache);
+/**
+ * Invalidates a peered object by calling
+ * im.pidgin.libpurple.peering.Peered.nativeObjectFreed()
+ */
+void
+notifyPeerDestroyed(JNIEnv *env, jobject peered);
+
+/**
+ * Gets a method id of the metod that is described in the given method cache.
+ * The method id is cached there.
+ */
+jmethodID
+getMethodIDCachedReferenced(JNIEnv *env, jclass class, JavaMethodIDCache *cache);
 
 //jmethodID getMethodIDCached(JNIEnv env, jclass class, const char *name, const char *signature, JavaMethodIDCache cache);
 
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
@@ -11,9 +11,13 @@ public class DnsQueryData extends Peered
 
 	public DnsQueryData(long nativePointer) {
 		super(nativePointer);
+		register_native();
 	}
 	
+	private native void register_native();
+
 	public String getHost() {
+		ensureNotDestroyed();
 		return getHost_native();
 	}
 



More information about the Commits mailing list