r=ashuk
bug=47357
M classes_spec/org/mozilla/webclient/wrapper_native/NativeEventThread.java
M src_moz/CBrowserContainer.cpp
M src_moz/NativeEventThread.cpp
M src_moz/ns_util.cpp
M src_moz/ns_util.h
M src_share/jni_util.cpp
M src_share/jni_util.h
tar -cvf 47357.2.tar classes_spec/org/mozilla/webclient/wrapper_native/NativeEventThread.java src_moz/CBrowserContainer.cpp src_moz/NativeEventThread.cpp src_moz/ns_util.cpp src_moz/ns_util.h src_share/jni_util.cpp src_share/jni_util.h
cvs diff -u classes_spec/org/mozilla/webclient/wrapper_native/NativeEventThread.java src_moz/CBrowserContainer.cpp src_moz/NativeEventThread.cpp src_moz/ns_util.cpp src_moz/ns_util.h src_share/jni_util.cpp src_share/jni_util.h
This change modifes what one has to do on the native side to add a
listener.
* How to create a new listener type on the native side: <P>
* 1. add an entry in the gSupportedListenerInterfaces array defined in
* ns_util.cpp <P>
* 2. add a corresponding entry in the LISTENER_CLASSES enum in
* ns_util.h <P>
* 3. add a jstring to the string constant list in
* CBrowserContainer.cpp, below.
* 4. Initialize this jstring constant in CBrowserContainer.cpp
* initStringConstants() <P>
* 5. add an entry to the switch statement in NativeEventThread.cpp
* native{add,remove}Listener <P>
You have to clobber_all in webclient after this change.
git-svn-id: svn://10.0.0.236/trunk@76019 18797224-902f-48f8-a5cc-f745e15eee43
517 lines
14 KiB
C++
517 lines
14 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
*
|
|
* 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 RaptorCanvas.
|
|
*
|
|
* The Initial Developer of the Original Code is Kirk Baker and
|
|
* Ian Wilkinson. Portions created by Kirk Baker and Ian Wilkinson are
|
|
* Copyright (C) 1999 Kirk Baker and Ian Wilkinson. All
|
|
* Rights Reserved.
|
|
*
|
|
* Contributor(s): Kirk Baker <kbaker@eb.com>
|
|
* Ian Wilkinson <iw@ennoble.com>
|
|
* Mark Lin <mark.lin@eng.sun.com>
|
|
* Mark Goddard
|
|
* Ed Burns <edburns@acm.org>
|
|
* Ashutosh Kulkarni <ashuk@eng.sun.com>
|
|
* Ann Sunhachawee
|
|
*/
|
|
|
|
#include "jni_util.h"
|
|
|
|
#include <string.h>
|
|
|
|
JavaVM *gVm = nsnull; // declared in ns_globals.h, which is included in
|
|
// jni_util.h
|
|
|
|
//
|
|
// Local cache variables of JNI data items
|
|
//
|
|
|
|
static jmethodID gPropertiesInitMethodID = nsnull;
|
|
static jmethodID gPropertiesSetPropertyMethodID = nsnull;
|
|
static jmethodID gPropertiesClearMethodID = nsnull;
|
|
|
|
void util_InitializeShareInitContext(void *yourInitContext)
|
|
{
|
|
ShareInitContext *initContext = (ShareInitContext *) yourInitContext;
|
|
initContext->propertiesClass = nsnull;
|
|
}
|
|
|
|
void util_DeallocateShareInitContext(void *yourInitContext)
|
|
{
|
|
// right now there is nothing to deallocate
|
|
}
|
|
|
|
void util_ThrowExceptionToJava (JNIEnv * env, const char * message)
|
|
{
|
|
if (env->ExceptionOccurred()) {
|
|
env->ExceptionClear();
|
|
}
|
|
jclass excCls = env->FindClass("java/lang/Exception");
|
|
|
|
if (excCls == 0) { // Unable to find the exception class, give up.
|
|
if (env->ExceptionOccurred())
|
|
env->ExceptionClear();
|
|
return;
|
|
}
|
|
|
|
// Throw the exception with the error code and description
|
|
jmethodID jID = env->GetMethodID(excCls, "<init>", "(Ljava/lang/String;)V"); // void Exception(String)
|
|
|
|
if (jID != nsnull) {
|
|
jstring exceptionString = env->NewStringUTF(message);
|
|
jthrowable newException = (jthrowable) env->NewObject(excCls, jID, exceptionString);
|
|
|
|
if (newException != nsnull) {
|
|
env->Throw(newException);
|
|
}
|
|
else {
|
|
if (env->ExceptionOccurred())
|
|
env->ExceptionClear();
|
|
}
|
|
} else {
|
|
if (env->ExceptionOccurred())
|
|
env->ExceptionClear();
|
|
}
|
|
} // ThrowExceptionToJava()
|
|
|
|
void util_SendEventToJava(JNIEnv *yourEnv, jobject nativeEventThread,
|
|
jobject webclientEventListener,
|
|
jstring eventListenerClassName,
|
|
jlong eventType, jobject eventData)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalEventOccurred) {
|
|
externalEventOccurred(yourEnv, nativeEventThread,
|
|
webclientEventListener, eventType, eventData);
|
|
}
|
|
#else
|
|
if (nsnull == gVm) {
|
|
return;
|
|
}
|
|
|
|
JNIEnv *env = (JNIEnv *) JNU_GetEnv(gVm, JNI_VERSION_1_2);
|
|
|
|
if (nsnull == env) {
|
|
return;
|
|
}
|
|
|
|
jthrowable exception;
|
|
|
|
if (nsnull != (exception = env->ExceptionOccurred())) {
|
|
env->ExceptionDescribe();
|
|
}
|
|
|
|
jclass clazz = env->GetObjectClass(nativeEventThread);
|
|
jmethodID mid = env->GetMethodID(clazz, "nativeEventOccurred",
|
|
"(Lorg/mozilla/webclient/WebclientEventListener;Ljava/lang/String;JLjava/lang/Object;)V");
|
|
if ( mid != nsnull) {
|
|
env->CallVoidMethod(nativeEventThread, mid, webclientEventListener,
|
|
eventListenerClassName,
|
|
eventType, eventData);
|
|
} else {
|
|
util_LogMessage(3, "cannot call the Java Method!\n");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
|
|
* @return: the string name of the current java thread. Must be freed!
|
|
|
|
*/
|
|
|
|
char *util_GetCurrentThreadName(JNIEnv *env)
|
|
{
|
|
jclass threadClass = env->FindClass("java/lang/Thread");
|
|
jobject currentThread;
|
|
jstring name;
|
|
char *result = nsnull;
|
|
const char *cstr;
|
|
jboolean isCopy;
|
|
|
|
if (nsnull != threadClass) {
|
|
jmethodID currentThreadID =
|
|
env->GetStaticMethodID(threadClass,"currentThread",
|
|
"()Ljava/lang/Thread;");
|
|
currentThread = env->CallStaticObjectMethod(threadClass,
|
|
currentThreadID);
|
|
if (nsnull != currentThread) {
|
|
jmethodID getNameID = env->GetMethodID(threadClass,
|
|
"getName",
|
|
"()Ljava/lang/String;");
|
|
name = (jstring) env->CallObjectMethod(currentThread, getNameID,
|
|
nsnull);
|
|
result = strdup(cstr = env->GetStringUTFChars(name, &isCopy));
|
|
if (JNI_TRUE == isCopy) {
|
|
env->ReleaseStringUTFChars(name, cstr);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// static
|
|
void util_DumpJavaStack(JNIEnv *env)
|
|
{
|
|
jclass threadClass = env->FindClass("java/lang/Thread");
|
|
if (nsnull != threadClass) {
|
|
jmethodID dumpStackID = env->GetStaticMethodID(threadClass,
|
|
"dumpStack",
|
|
"()V");
|
|
env->CallStaticVoidMethod(threadClass, dumpStackID);
|
|
}
|
|
}
|
|
|
|
jobject util_NewGlobalRef(JNIEnv *env, jobject obj)
|
|
{
|
|
jobject result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
// PENDING(edburns): do we need to do anything here?
|
|
result = obj;
|
|
#else
|
|
result = env->NewGlobalRef(obj);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
void util_DeleteGlobalRef(JNIEnv *env, jobject obj)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
env->DeleteGlobalRef(obj);
|
|
#endif
|
|
}
|
|
|
|
jthrowable util_ExceptionOccurred(JNIEnv *env)
|
|
{
|
|
jthrowable result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
result = env->ExceptionOccurred();
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jint util_GetJavaVM(JNIEnv *env, JavaVM **vm)
|
|
{
|
|
int result = -1;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
result = env->GetJavaVM(vm);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jclass util_FindClass(JNIEnv *env, const char *fullyQualifiedClassName)
|
|
{
|
|
jclass result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
result = util_GetClassMapping(fullyQualifiedClassName);
|
|
#else
|
|
result = env->FindClass(fullyQualifiedClassName);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jfieldID util_GetStaticFieldID(JNIEnv *env, jclass clazz,
|
|
const char *fieldName,
|
|
const char *signature)
|
|
{
|
|
jfieldID result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
result = env->GetStaticFieldID(clazz, fieldName, signature);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jlong util_GetStaticLongField(JNIEnv *env, jclass clazz, jfieldID id)
|
|
{
|
|
jlong result = -1;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
result = env->GetStaticLongField(clazz, id);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jboolean util_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)
|
|
{
|
|
jboolean result = JNI_FALSE;
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalInstanceOf) {
|
|
result = externalInstanceOf(env, obj, clazz);
|
|
}
|
|
#else
|
|
result = env->IsInstanceOf(obj, clazz);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
jint util_GetIntValueFromInstance(JNIEnv *env, jobject obj,
|
|
const char *fieldName)
|
|
{
|
|
int result = -1;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
jclass objClass = env->GetObjectClass(obj);
|
|
if (nsnull == objClass) {
|
|
util_LogMessage(3,
|
|
"util_GetIntValueFromInstance: Can't get object class from instance.\n");
|
|
return result;
|
|
}
|
|
|
|
jfieldID theFieldID = env->GetFieldID(objClass, fieldName, "I");
|
|
if (nsnull == theFieldID) {
|
|
util_LogMessage(3,
|
|
"util_GetIntValueFromInstance: Can't get fieldID for fieldName.\n");
|
|
return result;
|
|
}
|
|
|
|
result = env->GetIntField(obj, theFieldID);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
void util_SetIntValueForInstance(JNIEnv *env, jobject obj,
|
|
const char *fieldName, jint newValue)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
jclass objClass = env->GetObjectClass(obj);
|
|
if (nsnull == objClass) {
|
|
util_LogMessage(3,
|
|
"util_SetIntValueForInstance: Can't get object class from instance.\n");
|
|
return;
|
|
}
|
|
|
|
jfieldID fieldID = env->GetFieldID(objClass, fieldName, "I");
|
|
if (nsnull == fieldID) {
|
|
util_LogMessage(3,
|
|
"util_SetIntValueForInstance: Can't get fieldID for fieldName.\n");
|
|
return;
|
|
}
|
|
|
|
env->SetIntField(obj, fieldID, newValue);
|
|
#endif;
|
|
}
|
|
|
|
jobject util_CreatePropertiesObject(JNIEnv *env, jobject initContextObj)
|
|
{
|
|
jobject result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalCreatePropertiesObject) {
|
|
result = externalCreatePropertiesObject(env, initContextObj);
|
|
}
|
|
#else
|
|
util_Assert(initContextObj);
|
|
ShareInitContext *initContext = (ShareInitContext *) initContextObj;
|
|
|
|
if (nsnull == initContext->propertiesClass) {
|
|
if (nsnull == (initContext->propertiesClass =
|
|
::util_FindClass(env, "java/util/Properties"))) {
|
|
return result;
|
|
}
|
|
}
|
|
|
|
if (nsnull == gPropertiesInitMethodID) {
|
|
util_Assert(initContext->propertiesClass);
|
|
if (nsnull == (gPropertiesInitMethodID =
|
|
env->GetMethodID(initContext->propertiesClass,
|
|
"<init>", "()V"))) {
|
|
return result;
|
|
}
|
|
}
|
|
util_Assert(gPropertiesInitMethodID);
|
|
|
|
result = ::util_NewGlobalRef(env,
|
|
env->NewObject(initContext->propertiesClass,
|
|
gPropertiesInitMethodID));
|
|
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
void util_DestroyPropertiesObject(JNIEnv *env, jobject propertiesObject,
|
|
jobject reserved_NotUsed)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalDestroyPropertiesObject) {
|
|
externalDestroyPropertiesObject(env, propertiesObject,
|
|
reserved_NotUsed);
|
|
}
|
|
#else
|
|
::util_DeleteGlobalRef(env, propertiesObject);
|
|
#endif
|
|
}
|
|
|
|
void util_ClearPropertiesObject(JNIEnv *env, jobject propertiesObject,
|
|
jobject initContextObj)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalClearPropertiesObject) {
|
|
externalClearPropertiesObject(env, propertiesObject, initContextObj);
|
|
}
|
|
#else
|
|
util_Assert(initContextObj);
|
|
ShareInitContext *initContext = (ShareInitContext *) initContextObj;
|
|
|
|
if (nsnull == gPropertiesClearMethodID) {
|
|
util_Assert(initContext->propertiesClass);
|
|
if (nsnull == (gPropertiesClearMethodID =
|
|
env->GetMethodID(initContext->propertiesClass, "clear", "()V"))) {
|
|
return;
|
|
}
|
|
}
|
|
util_Assert(gPropertiesClearMethodID);
|
|
env->CallVoidMethod(propertiesObject, gPropertiesClearMethodID);
|
|
|
|
return;
|
|
#endif
|
|
}
|
|
|
|
void util_StoreIntoPropertiesObject(JNIEnv *env, jobject propertiesObject,
|
|
jobject name, jobject value,
|
|
jobject initContextObj)
|
|
{
|
|
#ifdef BAL_INTERFACE
|
|
if (nsnull != externalStoreIntoPropertiesObject) {
|
|
externalStoreIntoPropertiesObject(env, propertiesObject, name, value,
|
|
initContextObj);
|
|
}
|
|
#else
|
|
util_Assert(initContextObj);
|
|
ShareInitContext *initContext = (ShareInitContext *) initContextObj;
|
|
|
|
if (nsnull == gPropertiesSetPropertyMethodID) {
|
|
util_Assert(initContext->propertiesClass);
|
|
if (nsnull == (gPropertiesSetPropertyMethodID =
|
|
env->GetMethodID(initContext->propertiesClass,
|
|
"setProperty",
|
|
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;"))) {
|
|
return;
|
|
}
|
|
}
|
|
util_Assert(gPropertiesSetPropertyMethodID);
|
|
|
|
env->CallObjectMethod(propertiesObject, gPropertiesSetPropertyMethodID,
|
|
name, value);
|
|
|
|
|
|
|
|
#endif
|
|
}
|
|
|
|
JNIEXPORT jvalue JNICALL
|
|
JNU_CallMethodByName(JNIEnv *env,
|
|
jboolean *hasException,
|
|
jobject obj,
|
|
const char *name,
|
|
const char *signature,
|
|
...)
|
|
{
|
|
jvalue result;
|
|
va_list args;
|
|
|
|
va_start(args, signature);
|
|
result = JNU_CallMethodByNameV(env, hasException, obj, name, signature,
|
|
args);
|
|
va_end(args);
|
|
|
|
return result;
|
|
}
|
|
|
|
JNIEXPORT jvalue JNICALL
|
|
JNU_CallMethodByNameV(JNIEnv *env,
|
|
jboolean *hasException,
|
|
jobject obj,
|
|
const char *name,
|
|
const char *signature,
|
|
va_list args)
|
|
{
|
|
jclass clazz;
|
|
jmethodID mid;
|
|
jvalue result;
|
|
const char *p = signature;
|
|
/* find out the return type */
|
|
while (*p && *p != ')')
|
|
p++;
|
|
p++;
|
|
|
|
result.i = 0;
|
|
|
|
if (env->EnsureLocalCapacity(3) < 0)
|
|
goto done2;
|
|
clazz = env->GetObjectClass(obj);
|
|
mid = env->GetMethodID(clazz, name, signature);
|
|
if (mid == 0)
|
|
goto done1;
|
|
switch (*p) {
|
|
case 'V':
|
|
env->CallVoidMethodV(obj, mid, args);
|
|
break;
|
|
case '[':
|
|
case 'L':
|
|
result.l = env->CallObjectMethodV(obj, mid, args);
|
|
break;
|
|
case 'Z':
|
|
result.z = env->CallBooleanMethodV(obj, mid, args);
|
|
break;
|
|
case 'B':
|
|
result.b = env->CallByteMethodV(obj, mid, args);
|
|
break;
|
|
case 'C':
|
|
result.c = env->CallCharMethodV(obj, mid, args);
|
|
break;
|
|
case 'S':
|
|
result.s = env->CallShortMethodV(obj, mid, args);
|
|
break;
|
|
case 'I':
|
|
result.i = env->CallIntMethodV(obj, mid, args);
|
|
break;
|
|
case 'J':
|
|
result.j = env->CallLongMethodV(obj, mid, args);
|
|
break;
|
|
case 'F':
|
|
result.f = env->CallFloatMethodV(obj, mid, args);
|
|
break;
|
|
case 'D':
|
|
result.d = env->CallDoubleMethodV(obj, mid, args);
|
|
break;
|
|
default:
|
|
env->FatalError("JNU_CallMethodByNameV: illegal signature");
|
|
}
|
|
done1:
|
|
env->DeleteLocalRef(clazz);
|
|
done2:
|
|
if (hasException) {
|
|
*hasException = env->ExceptionCheck();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
JNIEXPORT void * JNICALL
|
|
JNU_GetEnv(JavaVM *vm, jint version)
|
|
{
|
|
// void *result;
|
|
//vm->GetEnv(&result, version);
|
|
JNIEnv *result = nsnull;
|
|
#ifdef BAL_INTERFACE
|
|
#else
|
|
vm->AttachCurrentThread((void **)&result, (void *) version);
|
|
#endif
|
|
return result;
|
|
}
|
|
|