Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions
CSecureEnv.cpp File Reference
#include "CSecureEnv.h"
#include "nsISecurityContext.h"
#include "MRJPlugin.h"
#include "MRJSession.h"
#include "nsIThreadManager.h"
#include "nsIJVMManager.h"
#include "MRJMonitor.h"
#include "NativeMonitor.h"
#include "JavaMessageQueue.h"

Go to the source code of this file.

Classes

class  CreateNativeThreadMessage
 Creates a new native thread in MRJ's main thread, to avoid deadlock problems. More...
class  NewObjectMessage
class  CallMethodMessage
class  CallNonvirtualMethodMessage
class  GetFieldMessage
class  CallStaticMethodMessage
class  GetStaticFieldMessage
class  DefineClassMessage
 To give proper "local" refs, need to run this in the Java thread. More...
class  FindClassMessage
 To give proper "local" refs, need to run this in the Java thread. More...
class  GetSuperclassMessage
 To give proper "local" refs, need to run this in the Java thread. More...
class  DeleteLocalRefMessage
 To give proper "local" refs, need to run this in the true thread. More...

Functions

static void netscape_oji_JNIThread_run (JNIEnv *env, jobject self)
 Native run method that communicates with LiveConnect from a Java thread.
static void CreateJNIThread (CSecureEnv *secureEnv)
 Called from browser side, starts the Java thread that calls from LiveConnect to Java are processed in.
static void CreateNativeThread (CSecureEnv *secureEnv)
 Called from a Java thread that wants to communicate with the browser.
CSecureEnvGetSecureJNI (JNIEnv *env, jobject thread)
 Returns the secure JNI associated with the current thread (if any).

Function Documentation

static void CreateJNIThread ( CSecureEnv secureEnv) [static]

Called from browser side, starts the Java thread that calls from LiveConnect to Java are processed in.

Definition at line 173 of file CSecureEnv.cpp.

{
       nsIThreadManager* manager = secureEnv->getThreadManager();
       MRJSession* session = secureEnv->getSession();
       JNIEnv* env = session->getCurrentEnv();
       jclass JNIThreadClass = env->FindClass("netscape/oji/JNIThread");
       if (JNIThreadClass != NULL) {
              JNINativeMethod method = { "run", "()V", &netscape_oji_JNIThread_run };
              env->RegisterNatives(JNIThreadClass, &method, 1);
              jmethodID constructorID = env->GetMethodID(JNIThreadClass, "<init>", "(I)V");
              if (constructorID != NULL) {
                     jobject javaThread = env->NewObject(JNIThreadClass, constructorID, secureEnv);
                     for (;;) {
                            // give time to Java, to allow the thread to come up.
                            session->idle(kDefaultJMTime);
                            // has the thread made contact?
                            if (secureEnv->isInitialized())
                                   break;
                            // give time to NSPR, to avoid hanging too long.
                            manager->Sleep();
                     }
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void CreateNativeThread ( CSecureEnv secureEnv) [static]

Called from a Java thread that wants to communicate with the browser.

Definition at line 221 of file CSecureEnv.cpp.

{
       nsresult result;
       PRUint32 threadID;
       MRJSession* session = secureEnv->getSession();
       
       // cause a native thread to be created on our behalf. Perhaps this should be a message we send to the
       // session itself. otherwise we could have reentrancy problems.
       CreateNativeThreadMessage message(&result, &threadID, secureEnv);
       session->sendMessage(&message);

       if (session->onMainThread()) {
              // give time to other native threads, so the new thread can come up.
              nsIThreadManager* manager = secureEnv->getThreadManager();
              while (!secureEnv->isInitialized()) {
                     manager->Sleep();
              }
       } else {
              // sleep the current Java thread until we rendezvous with the Native thread.
              JNIEnv* env = session->getCurrentEnv();
              jclass threadClass = env->FindClass("java/lang/Thread");
              if (threadClass != NULL) {
                     jmethodID sleepMethod = env->GetStaticMethodID(threadClass, "sleep", "(J)V");
                     if (sleepMethod != NULL) {
                            while (!secureEnv->isInitialized())
                                   env->CallStaticVoidMethod(threadClass, sleepMethod, jlong(kDefaultJMTime));
                     }
                     env->DeleteLocalRef(threadClass);
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

CSecureEnv* GetSecureJNI ( JNIEnv env,
jobject  thread 
)

Returns the secure JNI associated with the current thread (if any).

Definition at line 1975 of file CSecureEnv.cpp.

{
       CSecureEnv* secureJNI = NULL;
       
       jclass threadClass = env->GetObjectClass(thread);
       if (threadClass != NULL) {
              jfieldID fSecureEnvField = env->GetFieldID(threadClass, "fSecureEnv", "I");
              if (fSecureEnvField != NULL) {
                     secureJNI = (CSecureEnv*) env->GetIntField(thread, fSecureEnvField);
              } else {
                     env->ExceptionClear();
              }
              env->DeleteLocalRef(threadClass);
       }
       
       return secureJNI;
}

Here is the caller graph for this function:

static void netscape_oji_JNIThread_run ( JNIEnv env,
jobject  self 
) [static]

Native run method that communicates with LiveConnect from a Java thread.

Definition at line 117 of file CSecureEnv.cpp.

{
       CSecureEnv* secureEnv = NULL;
       jmethodID yieldMethod = NULL;
       jmethodID sleepMethod = NULL;
       
       jclass clazz = env->GetObjectClass(self);
       if (clazz != NULL) {
              // the field fSecureEnv contains a pointer to a CSecureEnv.
              jfieldID fSecureEnvField = env->GetFieldID(clazz, "fSecureEnv", "I");
              if (fSecureEnvField != NULL) {
                     secureEnv = (CSecureEnv*) env->GetIntField(self, fSecureEnvField);
              }
              yieldMethod = env->GetStaticMethodID(clazz, "yield", "()V");
              sleepMethod = env->GetStaticMethodID(clazz, "sleep", "(J)V");
       }
       
       // notify the secure JNI that we are here, and wait for messages to arrive.
       if (secureEnv != NULL) {
              jboolean isRunning = true;
              MRJSession* session = secureEnv->getSession();
              MRJMonitor requestMonitor(session, self);
              MRJMonitor replyMonitor(session);
              // NativeMonitor replyMonitor(secureEnv->getThreadManager());
              JavaMessageQueue requests(&requestMonitor), replies(&replyMonitor);
              secureEnv->initialize(env, &isRunning, &requests, &replies);
              
              // when this thread is running, no other thread can enter the request queue monitor.
              requests.enter();
              
              while (isRunning) {
                     // the protocol for now is dead simple:  get a message from the
                     // requests message queue, process it, and then put it back in
                     // the replies queue. This will get more elaborate to handle
                     // upcall requests. 
                     JavaMessage* msg = requests.getMessage();
                     if (msg != NULL) {
                            msg->execute(env);
                            replies.putMessage(msg);
                            replies.notify();
                     } else {
                            // should we do sleep, timed wait, or what?
                            // env->CallStaticVoidMethod(clazz, yieldMethod);
                            // env->CallStaticVoidMethod(clazz, sleepMethod, jlong(kDefaultJMTime));
                            requests.wait();
                     }
              }
              
              requests.exit();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function: