Back to index

lightning-sunbird  0.9+nobinonly
CSecureEnv.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the MRJ Carbon OJI Plugin.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Patrick C. Beard <beard@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039     CSecureEnv.h
00040     
00041     Rewritten for use with MRJ plugin by Patrick C. Beard.
00042  */
00043 
00044 #ifndef CSecureJNI2_h___
00045 #define CSecureJNI_h___
00046 
00047 #include "nsISecureEnv.h"
00048 #include "nsIThreadManager.h"
00049 #include "SupportsMixin.h"
00050 
00051 class MRJPlugin;
00052 class MRJSession;
00053 
00054 class Monitor;
00055 class nsIThreadManager;
00056 class nsIJVMManager;
00057 class nsIScriptSecurityManager;
00058 class nsIPrincipal;
00059 
00060 class JavaMessage;
00061 class JavaMessageQueue;
00062 
00063 class CSecureEnv : public nsISecureEnv, public nsIRunnable, private SupportsMixin {
00064 public:
00065     DECL_SUPPORTS_MIXIN
00066 
00067     static NS_METHOD Create(MRJPlugin* plugin, JNIEnv* proxyEnv, const nsIID& aIID, void* *aInstancePtr);
00068 
00070     // from nsISecureJNI2:
00071 
00072 
00082     NS_IMETHOD NewObject(/*[in]*/  jclass clazz, 
00083                          /*[in]*/  jmethodID methodID,
00084                          /*[in]*/  jvalue *args, 
00085                          /*[out]*/ jobject* result,
00086                          /*[in]*/  nsISecurityContext* ctx = NULL);
00087    
00098     NS_IMETHOD CallMethod(/*[in]*/  jni_type type,
00099                           /*[in]*/  jobject obj, 
00100                           /*[in]*/  jmethodID methodID,
00101                           /*[in]*/  jvalue *args, 
00102                           /*[out]*/ jvalue* result,
00103                           /*[in]*/  nsISecurityContext* ctx = NULL);
00104 
00116     NS_IMETHOD CallNonvirtualMethod(/*[in]*/  jni_type type,
00117                                     /*[in]*/  jobject obj, 
00118                                     /*[in]*/  jclass clazz,
00119                                     /*[in]*/  jmethodID methodID,
00120                                     /*[in]*/  jvalue *args, 
00121                                     /*[out]*/ jvalue* result,
00122                                     /*[in]*/  nsISecurityContext* ctx = NULL);
00123 
00133     NS_IMETHOD GetField(/*[in]*/  jni_type type,
00134                         /*[in]*/  jobject obj, 
00135                         /*[in]*/  jfieldID fieldID,
00136                         /*[out]*/ jvalue* result,
00137                         /*[in]*/  nsISecurityContext* ctx = NULL);
00138 
00148     NS_IMETHOD SetField(/*[in]*/ jni_type type,
00149                         /*[in]*/ jobject obj, 
00150                         /*[in]*/ jfieldID fieldID,
00151                         /*[in]*/ jvalue val,
00152                         /*[in]*/ nsISecurityContext* ctx = NULL);
00153 
00164     NS_IMETHOD CallStaticMethod(/*[in]*/  jni_type type,
00165                                 /*[in]*/  jclass clazz,
00166                                 /*[in]*/  jmethodID methodID,
00167                                 /*[in]*/  jvalue *args, 
00168                                 /*[out]*/ jvalue* result,
00169                                 /*[in]*/  nsISecurityContext* ctx = NULL);
00170 
00180     NS_IMETHOD GetStaticField(/*[in]*/  jni_type type,
00181                               /*[in]*/  jclass clazz, 
00182                               /*[in]*/  jfieldID fieldID, 
00183                               /*[out]*/ jvalue* result,
00184                               /*[in]*/  nsISecurityContext* ctx = NULL);
00185 
00186 
00196     NS_IMETHOD SetStaticField(/*[in]*/ jni_type type,
00197                               /*[in]*/ jclass clazz, 
00198                               /*[in]*/ jfieldID fieldID,
00199                               /*[in]*/ jvalue val,
00200                               /*[in]*/ nsISecurityContext* ctx = NULL);
00201 
00202 
00203     NS_IMETHOD GetVersion(/*[out]*/ jint* version);
00204 
00205     NS_IMETHOD DefineClass(/*[in]*/  const char* name,
00206                            /*[in]*/  jobject loader,
00207                            /*[in]*/  const jbyte *buf,
00208                            /*[in]*/  jsize len,
00209                            /*[out]*/ jclass* clazz);
00210 
00211     NS_IMETHOD FindClass(/*[in]*/  const char* name, 
00212                          /*[out]*/ jclass* clazz);
00213 
00214     NS_IMETHOD GetSuperclass(/*[in]*/  jclass sub,
00215                              /*[out]*/ jclass* super);
00216 
00217     NS_IMETHOD IsAssignableFrom(/*[in]*/  jclass sub,
00218                                 /*[in]*/  jclass super,
00219                                 /*[out]*/ jboolean* result);
00220 
00221     NS_IMETHOD Throw(/*[in]*/  jthrowable obj,
00222                      /*[out]*/ jint* result);
00223 
00224     NS_IMETHOD ThrowNew(/*[in]*/  jclass clazz,
00225                         /*[in]*/  const char *msg,
00226                         /*[out]*/ jint* result);
00227 
00228     NS_IMETHOD ExceptionOccurred(/*[out]*/ jthrowable* result);
00229 
00230     NS_IMETHOD ExceptionDescribe(void);
00231 
00232     NS_IMETHOD ExceptionClear(void);
00233 
00234     NS_IMETHOD FatalError(/*[in]*/ const char* msg);
00235 
00236     NS_IMETHOD NewGlobalRef(/*[in]*/  jobject lobj, 
00237                             /*[out]*/ jobject* result);
00238 
00239     NS_IMETHOD DeleteGlobalRef(/*[in]*/ jobject gref);
00240 
00241     NS_IMETHOD DeleteLocalRef(/*[in]*/ jobject obj);
00242 
00243     NS_IMETHOD IsSameObject(/*[in]*/  jobject obj1,
00244                             /*[in]*/  jobject obj2,
00245                             /*[out]*/ jboolean* result);
00246 
00247     NS_IMETHOD AllocObject(/*[in]*/  jclass clazz,
00248                            /*[out]*/ jobject* result);
00249 
00250     NS_IMETHOD GetObjectClass(/*[in]*/  jobject obj,
00251                               /*[out]*/ jclass* result);
00252 
00253     NS_IMETHOD IsInstanceOf(/*[in]*/  jobject obj,
00254                             /*[in]*/  jclass clazz,
00255                             /*[out]*/ jboolean* result);
00256 
00257     NS_IMETHOD GetMethodID(/*[in]*/  jclass clazz, 
00258                            /*[in]*/  const char* name,
00259                            /*[in]*/  const char* sig,
00260                            /*[out]*/ jmethodID* id);
00261 
00262     NS_IMETHOD GetFieldID(/*[in]*/  jclass clazz, 
00263                           /*[in]*/  const char* name,
00264                           /*[in]*/  const char* sig,
00265                           /*[out]*/ jfieldID* id);
00266 
00267     NS_IMETHOD GetStaticMethodID(/*[in]*/  jclass clazz, 
00268                                  /*[in]*/  const char* name,
00269                                  /*[in]*/  const char* sig,
00270                                  /*[out]*/ jmethodID* id);
00271 
00272     NS_IMETHOD GetStaticFieldID(/*[in]*/  jclass clazz, 
00273                                 /*[in]*/  const char* name,
00274                                 /*[in]*/  const char* sig,
00275                                 /*[out]*/ jfieldID* id);
00276 
00277     NS_IMETHOD NewString(/*[in]*/  const jchar* unicode,
00278                          /*[in]*/  jsize len,
00279                          /*[out]*/ jstring* result);
00280 
00281     NS_IMETHOD GetStringLength(/*[in]*/  jstring str,
00282                                /*[out]*/ jsize* result);
00283     
00284     NS_IMETHOD GetStringChars(/*[in]*/  jstring str,
00285                               /*[in]*/  jboolean *isCopy,
00286                               /*[out]*/ const jchar** result);
00287 
00288     NS_IMETHOD ReleaseStringChars(/*[in]*/  jstring str,
00289                                   /*[in]*/  const jchar *chars);
00290 
00291     NS_IMETHOD NewStringUTF(/*[in]*/  const char *utf,
00292                             /*[out]*/ jstring* result);
00293 
00294     NS_IMETHOD GetStringUTFLength(/*[in]*/  jstring str,
00295                                   /*[out]*/ jsize* result);
00296     
00297     NS_IMETHOD GetStringUTFChars(/*[in]*/  jstring str,
00298                                  /*[in]*/  jboolean *isCopy,
00299                                  /*[out]*/ const char** result);
00300 
00301     NS_IMETHOD ReleaseStringUTFChars(/*[in]*/  jstring str,
00302                                      /*[in]*/  const char *chars);
00303 
00304     NS_IMETHOD GetArrayLength(/*[in]*/  jarray array,
00305                               /*[out]*/ jsize* result);
00306 
00307     NS_IMETHOD NewObjectArray(/*[in]*/  jsize len,
00308                         /*[in]*/  jclass clazz,
00309                         /*[in]*/  jobject init,
00310                         /*[out]*/ jobjectArray* result);
00311 
00312     NS_IMETHOD GetObjectArrayElement(/*[in]*/  jobjectArray array,
00313                                      /*[in]*/  jsize index,
00314                                      /*[out]*/ jobject* result);
00315 
00316     NS_IMETHOD SetObjectArrayElement(/*[in]*/  jobjectArray array,
00317                                      /*[in]*/  jsize index,
00318                                      /*[in]*/  jobject val);
00319 
00320     NS_IMETHOD NewArray(/*[in]*/ jni_type element_type,
00321                         /*[in]*/  jsize len,
00322                         /*[out]*/ jarray* result);
00323 
00324     NS_IMETHOD GetArrayElements(/*[in]*/  jni_type type,
00325                                 /*[in]*/  jarray array,
00326                                 /*[in]*/  jboolean *isCopy,
00327                                 /*[out]*/ void* result);
00328 
00329     NS_IMETHOD ReleaseArrayElements(/*[in]*/ jni_type type,
00330                                     /*[in]*/ jarray array,
00331                                     /*[in]*/ void *elems,
00332                                     /*[in]*/ jint mode);
00333 
00334     NS_IMETHOD GetArrayRegion(/*[in]*/  jni_type type,
00335                               /*[in]*/  jarray array,
00336                               /*[in]*/  jsize start,
00337                               /*[in]*/  jsize len,
00338                               /*[out]*/ void* buf);
00339 
00340     NS_IMETHOD SetArrayRegion(/*[in]*/  jni_type type,
00341                               /*[in]*/  jarray array,
00342                               /*[in]*/  jsize start,
00343                               /*[in]*/  jsize len,
00344                               /*[in]*/  void* buf);
00345 
00346     NS_IMETHOD RegisterNatives(/*[in]*/  jclass clazz,
00347                                /*[in]*/  const JNINativeMethod *methods,
00348                                /*[in]*/  jint nMethods,
00349                                /*[out]*/ jint* result);
00350 
00351     NS_IMETHOD UnregisterNatives(/*[in]*/  jclass clazz,
00352                                  /*[out]*/ jint* result);
00353 
00354     NS_IMETHOD MonitorEnter(/*[in]*/  jobject obj,
00355                             /*[out]*/ jint* result);
00356 
00357     NS_IMETHOD MonitorExit(/*[in]*/  jobject obj,
00358                            /*[out]*/ jint* result);
00359 
00360     NS_IMETHOD GetJavaVM(/*[in]*/  JavaVM **vm,
00361                          /*[out]*/ jint* result);
00362                          
00364     // from nsIRunnable:
00365     
00366     NS_IMETHOD Run();
00367 
00368     CSecureEnv(MRJPlugin* plugin, JNIEnv* proxyEnv, JNIEnv* javaEnv = NULL);
00369     virtual ~CSecureEnv(void);
00370 
00375     void initialize(JNIEnv* javaEnv, jboolean* isRunning, JavaMessageQueue* javaQueue, JavaMessageQueue* nativeQueue);
00376     
00377     jboolean isInitialized() { return mJavaQueue != NULL; }
00378 
00379     void setProxyEnv(JNIEnv* proxyEnv) { mProxyEnv = proxyEnv; }
00380     JNIEnv* getProxyEnv() { return mProxyEnv; }
00381     
00382     void setJavaEnv(JNIEnv* javaEnv) { mJavaEnv = javaEnv; }
00383     JNIEnv* getJavaEnv() { return mJavaEnv; }
00384     
00385     MRJSession* getSession() { return mSession; }
00386     nsIThreadManager* getThreadManager() { return mThreadManager; }
00387     
00388     void getMessageQueues(JavaMessageQueue*& javaQueue, JavaMessageQueue*& nativeQueue)
00389     {
00390         javaQueue = mJavaQueue;
00391         nativeQueue = mNativeQueue;
00392     }
00393     
00394     void sendMessageToJava(JavaMessage* msg);
00395     void sendMessageFromJava(JNIEnv* javaEnv, JavaMessage* msg, Boolean busyWaiting = false);
00396     void messageLoop(JNIEnv* env, JavaMessage* msgToSend, JavaMessageQueue* sendQueue, JavaMessageQueue* receiveQueue, Boolean busyWaiting = false);
00397 
00398     void savePendingException(JNIEnv* env);
00399     jthrowable getPendingException(JNIEnv* env);
00400     void clearPendingException(JNIEnv* env);
00401     
00402 protected:
00403     MRJPlugin*                  mPlugin;
00404     JNIEnv*                     mProxyEnv;
00405     JNIEnv*                     mJavaEnv;
00406     MRJSession*                 mSession;
00407     nsIThreadManager*           mThreadManager;
00408     
00409     jboolean*                   mIsRunning;
00410     JavaMessageQueue*           mJavaQueue;
00411     JavaMessageQueue*           mNativeQueue;
00412 
00413     jthrowable                  mPendingException;
00414     
00415     nsIScriptSecurityManager*   mScriptSecurityManager;
00416     nsIPrincipal*               mScriptPrincipal;
00417     jclass                      mLiveConnectProxy;
00418     
00419 private:
00420     // support for SupportsMixin.
00421     static const InterfaceInfo sInterfaces[];
00422     static const UInt32 kInterfaceCount;
00423 };
00424 
00428 CSecureEnv* GetSecureJNI(JNIEnv* env, jobject thread);
00429 
00430 #endif // CSecureJNI2_h___