void* threadId = PR_GetCurrentThread(); if (threadId == (void *) gEmbeddedThread) {  processEventLoop(initContext); }
processEventLoop(initContext);
int processEventLoop(WebShellInitContext * initContext);/** * Called from Java nativeInitialize to create the webshell  * and other mozilla things, then start the event loop. */nsresult InitMozillaStuff (WebShellInitContext * arg);//// Local data//nsIComponentManager *gComponentManager = nsnull;static PRBool	gFirstTime = PR_TRUE;PLEventQueue	*	gActionQueue = nsnull;PRThread		*	gEmbeddedThread = nsnull;nsISHistory *gHistory = nsnull;WindowCreator   *   gCreatorCallback = nsnull;char * errorMessages[] = {	"No Error",	"Could not obtain the event queue service.",	"Unable to create the WebShell instance.",	"Unable to initialize the WebShell instance.",	"Unable to show the WebShell."};//// JNI methods//JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NativeEventThread_nativeInitialize(JNIEnv *env, jobject obj, jint webShellPtr){    nsresult rv = NS_ERROR_FAILURE;    WebShellInitContext * initContext = (WebShellInitContext *) webShellPtr;    if (nsnull == initContext) {	    ::util_ThrowExceptionToJava(env,                                     "NULL webShellPtr passed to nativeInitialize.");        return;    }    if (nsnull == gVm) { // declared in ../src_share/jni_util.h        ::util_GetJavaVM(env, &gVm);  // save this vm reference    }    rv = InitMozillaStuff(initContext);    if (NS_FAILED(rv)) {	    ::util_ThrowExceptionToJava(env,                                     errorMessages[initContext->initFailCode]);        return;    }    while (initContext->initComplete == FALSE) {                ::PR_Sleep(PR_INTERVAL_NO_WAIT);                if (initContext->initFailCode != 0) {            ::util_ThrowExceptionToJava(env, errorMessages[initContext->initFailCode]);            return;        }    }}JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NativeEventThread_nativeProcessEvents(JNIEnv *env, jobject obj, jint webShellPtr){    WebShellInitContext * initContext = (WebShellInitContext *) webShellPtr;        if (nsnull == initContext) {	    ::util_ThrowExceptionToJava(env,                                     "NULL webShellPtr passed to nativeProcessEvents.");        return;    }    void* threadId = PR_GetCurrentThread();    if (threadId == (void *) gEmbeddedThread) {        //        printf("--------- Thread ID ---- %p\n",gEmbeddedThread);        processEventLoop(initContext);    }}/** * <P> This method takes the typedListener, which is a * WebclientEventListener java subclass, figures out what type of * subclass it is, using the gSupportedListenerInterfaces array, and * calls the appropriate add*Listener local function.  </P> *<P> PENDING(): we could do away with the switch statement using * function pointers, or some other mechanism.  </P> * <P>the NewGlobalRef call is very important, since the argument * typedListener is used to call back into java, at another time, as a * result of the a mozilla event being fired.</P> * PENDING(): implement nativeRemoveListener, which must call * RemoveGlobalRef. */JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NativeEventThread_nativeAddListener(JNIEnv *env, jobject obj, jint webShellPtr, jobject typedListener,  jstring listenerString){    WebShellInitContext *initContext = (WebShellInitContext *)webShellPtr;            if (initContext == nsnull) {        ::util_ThrowExceptionToJava(env, "Exception: null initContext passed tonativeAddListener");        return;    }    if (nsnull == initContext->nativeEventThread) {        // store the java EventRegistrationImpl class in the initContext        initContext->nativeEventThread =             ::util_NewGlobalRef(env, obj); // VERY IMPORTANT!!                // This enables the listener to call back into java    }        jclass clazz = nsnull;    int listenerType = 0;    const char *listenerStringChars = ::util_GetStringUTFChars(env,                                                                listenerString);    if (listenerStringChars == nsnull) {        ::util_ThrowExceptionToJava(env, "Exception: nativeAddListener: Can't get className for listener.");        return;    }            while (nsnull != gSupportedListenerInterfaces[listenerType]) {        if (0 == nsCRT::strcmp(gSupportedListenerInterfaces[listenerType],                                listenerStringChars)) {            // We've got a winner!            break;        }        listenerType++;    }    ::util_ReleaseStringUTFChars(env, listenerString, listenerStringChars);    listenerStringChars = nsnull;        if (LISTENER_NOT_FOUND == (LISTENER_CLASSES) listenerType) {        ::util_ThrowExceptionToJava(env, "Exception: NativeEventThread.nativeAddListener(): can't find listener \n\tclass for argument");        return;    }    jobject globalRef = nsnull;    // PENDING(edburns): make sure do DeleteGlobalRef on the removeListener    if (nsnull == (globalRef = ::util_NewGlobalRef(env, typedListener))) {        ::util_ThrowExceptionToJava(env, "Exception: NativeEventThread.nativeAddListener(): can't create NewGlobalRef\n\tfor argument");        return;    }    PR_ASSERT(initContext->browserContainer);    switch(listenerType) {    case DOCUMENT_LOAD_LISTENER:        initContext->browserContainer->AddDocumentLoadListener(globalRef);         break;    case MOUSE_LISTENER:        initContext->browserContainer->AddMouseListener(globalRef);         break;    case NEW_WINDOW_LISTENER:        if (gCreatorCallback)            gCreatorCallback->AddNewWindowListener(globalRef);         break;    }    return;}JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NativeEventThread_nativeRemoveListener(JNIEnv *env, jobject obj, jint webShellPtr, jobject typedListener, jstring listenerString){    WebShellInitContext *initContext = (WebShellInitContext *)webShellPtr;    if (initContext == nsnull) {        ::util_ThrowExceptionToJava(env, "Exception: null initContext passed to nativeRemoveListener");        return;    }        jclass clazz = nsnull;    int listenerType = 0;    const char *listenerStringChars = ::util_GetStringUTFChars(env,                                                                listenerString);    if (listenerStringChars == nsnull) {        ::util_ThrowExceptionToJava(env, "Exception: nativeRemoveListener: Can't get className for listener.");        return;    }        while (nsnull != gSupportedListenerInterfaces[listenerType]) {        if (0 == nsCRT::strcmp(gSupportedListenerInterfaces[listenerType],                                listenerStringChars)) {            // We've got a winner!            break;        }        listenerType++;    }    ::util_ReleaseStringUTFChars(env, listenerString, listenerStringChars);    listenerStringChars = nsnull;        if (LISTENER_NOT_FOUND == (LISTENER_CLASSES) listenerType) {        ::util_ThrowExceptionToJava(env, "Exception: NativeEventThread.nativeRemoveListener(): can't find listener \n\tclass for argument");        return;    }    PR_ASSERT(initContext->browserContainer);        switch(listenerType) {    case DOCUMENT_LOAD_LISTENER:        initContext->browserContainer->RemoveDocumentLoadListener();         break;    case MOUSE_LISTENER:        initContext->browserContainer->RemoveMouseListener();         break;    }}JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NativeEventThread_nativeRemoveAllListeners(JNIEnv *env, jobject obj, jint webShellPtr){    WebShellInitContext *initContext = (WebShellInitContext *)webShellPtr;    if (initContext == nsnull) {        ::util_ThrowExceptionToJava(env, "Exception: null initContext passed to nativeRemoveAllListeners");        return;    }    initContext->browserContainer->RemoveAllListeners();}/** * This function processes methods inserted into WebShellInitContext's * actionQueue.  It is called once during the initialization of the * NativeEventThread java thread, and infinitely in * NativeEventThread.run()'s event loop.  The call to PL_HandleEvent * below, ends up calling the nsActionEvent subclass's handleEvent() * method.  After which it calls nsActionEvent::destroyEvent(). */int processEventLoop(WebShellInitContext * initContext) {#ifdef XP_UNIX    while(gtk_events_pending()) {        gtk_main_iteration();    }#else    // PENDING(mark): Does this work on the Mac?    MSG msg;    PRBool wasHandled;        if (::PeekMessage(&msg, nsnull, 0, 0, PM_NOREMOVE)) {        if (::GetMessage(&msg, nsnull, 0, 0)) {            wasHandled = PR_FALSE;            ::NS_HandleEmbeddingEvent(msg, wasHandled);            if (!wasHandled) {                ::TranslateMessage(&msg);                ::DispatchMessage(&msg);            }        }    }#endif    ::PR_Sleep(PR_INTERVAL_NO_WAIT);        if ((initContext->initComplete) && (gActionQueue)) {        PLEvent * event = nsnull;                PL_ENTER_EVENT_QUEUE_MONITOR(gActionQueue);        if (::PL_EventAvailable(gActionQueue)) {            event = ::PL_GetEvent(gActionQueue);        }        PL_EXIT_EVENT_QUEUE_MONITOR(gActionQueue);        if (event != nsnull) {            ::PL_HandleEvent(event);        }    }    if (initContext->stopThread) {        initContext->stopThread++;                return 0;      }    // PENDING(edburns): revisit this.  Not sure why this is necessary, but    // this fixes bug 44327    //    printf("%c", 8); // 8 is ASCII for backspace    return 1;}