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 mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Sun Microsystems, Inc.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038        CSecureEnv.h
00039        
00040        Rewritten for use with MRJ plugin by Patrick C. Beard.
00041  */
00042 
00043 #ifndef CSecureJNI2_h___
00044 #define CSecureJNI_h___
00045 
00046 #include "nsISecureEnv.h"
00047 #include "nsIThreadManager.h"
00048 #include "SupportsMixin.h"
00049 #include "nsAgg.h"
00050 
00051 class MRJPlugin;
00052 class MRJSession;
00053 
00054 class Monitor;
00055 class nsIThreadManager;
00056 class nsIJVMManager;
00057 
00058 class JavaMessage;
00059 class JavaMessageQueue;
00060 
00061 class CSecureEnv : public nsISecureEnv, public nsIRunnable, private SupportsMixin {
00062 public:
00063 
00065     // from nsISupports and AggregatedQueryInterface:
00066 
00067        // NS_DECL_AGGREGATED
00068     DECL_SUPPORTS_MIXIN
00069 
00070        static NS_METHOD Create(MRJPlugin* plugin, JNIEnv* proxyEnv, const nsIID& aIID, void* *aInstancePtr);
00071 
00073     // from nsISecureJNI2:
00074 
00075 
00085     NS_IMETHOD NewObject(/*[in]*/  jclass clazz, 
00086                          /*[in]*/  jmethodID methodID,
00087                          /*[in]*/  jvalue *args, 
00088                          /*[out]*/ jobject* result,
00089                          /*[in]*/  nsISecurityContext* ctx = NULL);
00090    
00101     NS_IMETHOD CallMethod(/*[in]*/  jni_type type,
00102                           /*[in]*/  jobject obj, 
00103                           /*[in]*/  jmethodID methodID,
00104                           /*[in]*/  jvalue *args, 
00105                           /*[out]*/ jvalue* result,
00106                           /*[in]*/  nsISecurityContext* ctx = NULL);
00107 
00119     NS_IMETHOD CallNonvirtualMethod(/*[in]*/  jni_type type,
00120                                     /*[in]*/  jobject obj, 
00121                                     /*[in]*/  jclass clazz,
00122                                     /*[in]*/  jmethodID methodID,
00123                                     /*[in]*/  jvalue *args, 
00124                                     /*[out]*/ jvalue* result,
00125                                     /*[in]*/  nsISecurityContext* ctx = NULL);
00126 
00136     NS_IMETHOD GetField(/*[in]*/  jni_type type,
00137                         /*[in]*/  jobject obj, 
00138                         /*[in]*/  jfieldID fieldID,
00139                         /*[out]*/ jvalue* result,
00140                         /*[in]*/  nsISecurityContext* ctx = NULL);
00141 
00151     NS_IMETHOD SetField(/*[in]*/ jni_type type,
00152                         /*[in]*/ jobject obj, 
00153                         /*[in]*/ jfieldID fieldID,
00154                         /*[in]*/ jvalue val,
00155                         /*[in]*/ nsISecurityContext* ctx = NULL);
00156 
00167     NS_IMETHOD CallStaticMethod(/*[in]*/  jni_type type,
00168                                 /*[in]*/  jclass clazz,
00169                                 /*[in]*/  jmethodID methodID,
00170                                 /*[in]*/  jvalue *args, 
00171                                 /*[out]*/ jvalue* result,
00172                                 /*[in]*/  nsISecurityContext* ctx = NULL);
00173 
00183     NS_IMETHOD GetStaticField(/*[in]*/  jni_type type,
00184                               /*[in]*/  jclass clazz, 
00185                               /*[in]*/  jfieldID fieldID, 
00186                               /*[out]*/ jvalue* result,
00187                               /*[in]*/  nsISecurityContext* ctx = NULL);
00188 
00189 
00199     NS_IMETHOD SetStaticField(/*[in]*/ jni_type type,
00200                               /*[in]*/ jclass clazz, 
00201                               /*[in]*/ jfieldID fieldID,
00202                               /*[in]*/ jvalue val,
00203                               /*[in]*/ nsISecurityContext* ctx = NULL);
00204 
00205 
00206     NS_IMETHOD GetVersion(/*[out]*/ jint* version);
00207 
00208     NS_IMETHOD DefineClass(/*[in]*/  const char* name,
00209                            /*[in]*/  jobject loader,
00210                            /*[in]*/  const jbyte *buf,
00211                            /*[in]*/  jsize len,
00212                            /*[out]*/ jclass* clazz);
00213 
00214     NS_IMETHOD FindClass(/*[in]*/  const char* name, 
00215                          /*[out]*/ jclass* clazz);
00216 
00217     NS_IMETHOD GetSuperclass(/*[in]*/  jclass sub,
00218                              /*[out]*/ jclass* super);
00219 
00220     NS_IMETHOD IsAssignableFrom(/*[in]*/  jclass sub,
00221                                 /*[in]*/  jclass super,
00222                                 /*[out]*/ jboolean* result);
00223 
00224     NS_IMETHOD Throw(/*[in]*/  jthrowable obj,
00225                      /*[out]*/ jint* result);
00226 
00227     NS_IMETHOD ThrowNew(/*[in]*/  jclass clazz,
00228                         /*[in]*/  const char *msg,
00229                         /*[out]*/ jint* result);
00230 
00231     NS_IMETHOD ExceptionOccurred(/*[out]*/ jthrowable* result);
00232 
00233     NS_IMETHOD ExceptionDescribe(void);
00234 
00235     NS_IMETHOD ExceptionClear(void);
00236 
00237     NS_IMETHOD FatalError(/*[in]*/ const char* msg);
00238 
00239     NS_IMETHOD NewGlobalRef(/*[in]*/  jobject lobj, 
00240                             /*[out]*/ jobject* result);
00241 
00242     NS_IMETHOD DeleteGlobalRef(/*[in]*/ jobject gref);
00243 
00244     NS_IMETHOD DeleteLocalRef(/*[in]*/ jobject obj);
00245 
00246     NS_IMETHOD IsSameObject(/*[in]*/  jobject obj1,
00247                             /*[in]*/  jobject obj2,
00248                             /*[out]*/ jboolean* result);
00249 
00250     NS_IMETHOD AllocObject(/*[in]*/  jclass clazz,
00251                            /*[out]*/ jobject* result);
00252 
00253     NS_IMETHOD GetObjectClass(/*[in]*/  jobject obj,
00254                               /*[out]*/ jclass* result);
00255 
00256     NS_IMETHOD IsInstanceOf(/*[in]*/  jobject obj,
00257                             /*[in]*/  jclass clazz,
00258                             /*[out]*/ jboolean* result);
00259 
00260     NS_IMETHOD GetMethodID(/*[in]*/  jclass clazz, 
00261                            /*[in]*/  const char* name,
00262                            /*[in]*/  const char* sig,
00263                            /*[out]*/ jmethodID* id);
00264 
00265     NS_IMETHOD GetFieldID(/*[in]*/  jclass clazz, 
00266                           /*[in]*/  const char* name,
00267                           /*[in]*/  const char* sig,
00268                           /*[out]*/ jfieldID* id);
00269 
00270     NS_IMETHOD GetStaticMethodID(/*[in]*/  jclass clazz, 
00271                                  /*[in]*/  const char* name,
00272                                  /*[in]*/  const char* sig,
00273                                  /*[out]*/ jmethodID* id);
00274 
00275     NS_IMETHOD GetStaticFieldID(/*[in]*/  jclass clazz, 
00276                                 /*[in]*/  const char* name,
00277                                 /*[in]*/  const char* sig,
00278                                 /*[out]*/ jfieldID* id);
00279 
00280     NS_IMETHOD NewString(/*[in]*/  const jchar* unicode,
00281                          /*[in]*/  jsize len,
00282                          /*[out]*/ jstring* result);
00283 
00284     NS_IMETHOD GetStringLength(/*[in]*/  jstring str,
00285                                /*[out]*/ jsize* result);
00286     
00287     NS_IMETHOD GetStringChars(/*[in]*/  jstring str,
00288                               /*[in]*/  jboolean *isCopy,
00289                               /*[out]*/ const jchar** result);
00290 
00291     NS_IMETHOD ReleaseStringChars(/*[in]*/  jstring str,
00292                                   /*[in]*/  const jchar *chars);
00293 
00294     NS_IMETHOD NewStringUTF(/*[in]*/  const char *utf,
00295                             /*[out]*/ jstring* result);
00296 
00297     NS_IMETHOD GetStringUTFLength(/*[in]*/  jstring str,
00298                                   /*[out]*/ jsize* result);
00299     
00300     NS_IMETHOD GetStringUTFChars(/*[in]*/  jstring str,
00301                                  /*[in]*/  jboolean *isCopy,
00302                                  /*[out]*/ const char** result);
00303 
00304     NS_IMETHOD ReleaseStringUTFChars(/*[in]*/  jstring str,
00305                                      /*[in]*/  const char *chars);
00306 
00307     NS_IMETHOD GetArrayLength(/*[in]*/  jarray array,
00308                               /*[out]*/ jsize* result);
00309 
00310     NS_IMETHOD NewObjectArray(/*[in]*/  jsize len,
00311                                    /*[in]*/  jclass clazz,
00312                         /*[in]*/  jobject init,
00313                         /*[out]*/ jobjectArray* result);
00314 
00315     NS_IMETHOD GetObjectArrayElement(/*[in]*/  jobjectArray array,
00316                                      /*[in]*/  jsize index,
00317                                      /*[out]*/ jobject* result);
00318 
00319     NS_IMETHOD SetObjectArrayElement(/*[in]*/  jobjectArray array,
00320                                      /*[in]*/  jsize index,
00321                                      /*[in]*/  jobject val);
00322 
00323     NS_IMETHOD NewArray(/*[in]*/ jni_type element_type,
00324                         /*[in]*/  jsize len,
00325                         /*[out]*/ jarray* result);
00326 
00327     NS_IMETHOD GetArrayElements(/*[in]*/  jni_type type,
00328                                 /*[in]*/  jarray array,
00329                                 /*[in]*/  jboolean *isCopy,
00330                                 /*[out]*/ void* result);
00331 
00332     NS_IMETHOD ReleaseArrayElements(/*[in]*/ jni_type type,
00333                                     /*[in]*/ jarray array,
00334                                     /*[in]*/ void *elems,
00335                                     /*[in]*/ jint mode);
00336 
00337     NS_IMETHOD GetArrayRegion(/*[in]*/  jni_type type,
00338                               /*[in]*/  jarray array,
00339                               /*[in]*/  jsize start,
00340                               /*[in]*/  jsize len,
00341                               /*[out]*/ void* buf);
00342 
00343     NS_IMETHOD SetArrayRegion(/*[in]*/  jni_type type,
00344                               /*[in]*/  jarray array,
00345                               /*[in]*/  jsize start,
00346                               /*[in]*/  jsize len,
00347                               /*[in]*/  void* buf);
00348 
00349     NS_IMETHOD RegisterNatives(/*[in]*/  jclass clazz,
00350                                /*[in]*/  const JNINativeMethod *methods,
00351                                /*[in]*/  jint nMethods,
00352                                /*[out]*/ jint* result);
00353 
00354     NS_IMETHOD UnregisterNatives(/*[in]*/  jclass clazz,
00355                                  /*[out]*/ jint* result);
00356 
00357     NS_IMETHOD MonitorEnter(/*[in]*/  jobject obj,
00358                             /*[out]*/ jint* result);
00359 
00360     NS_IMETHOD MonitorExit(/*[in]*/  jobject obj,
00361                            /*[out]*/ jint* result);
00362 
00363     NS_IMETHOD GetJavaVM(/*[in]*/  JavaVM **vm,
00364                          /*[out]*/ jint* result);
00365                          
00367     // from nsIRunnable:
00368     
00369     NS_IMETHOD Run();
00370 
00371     CSecureEnv(MRJPlugin* plugin, JNIEnv* proxyEnv, JNIEnv* javaEnv = NULL);
00372     virtual ~CSecureEnv(void);
00373 
00378        void initialize(JNIEnv* javaEnv, jboolean* isRunning, JavaMessageQueue* javaQueue, JavaMessageQueue* nativeQueue);
00379        
00380        jboolean isInitialized() { return mJavaQueue != NULL; }
00381 
00382        void setProxyEnv(JNIEnv* proxyEnv) { mProxyEnv = proxyEnv; }
00383        JNIEnv* getProxyEnv() { return mProxyEnv; }
00384        
00385        void setJavaEnv(JNIEnv* javaEnv) { mJavaEnv = javaEnv; }
00386        JNIEnv* getJavaEnv() { return mJavaEnv; }
00387        
00388        MRJSession* getSession() { return mSession; }
00389        nsIThreadManager* getThreadManager() { return mThreadManager; }
00390        
00391        void getMessageQueues(JavaMessageQueue*& javaQueue, JavaMessageQueue*& nativeQueue)
00392        {
00393               javaQueue = mJavaQueue;
00394               nativeQueue = mNativeQueue;
00395        }
00396        
00397        void sendMessageToJava(JavaMessage* msg);
00398        void sendMessageFromJava(JNIEnv* javaEnv, JavaMessage* msg, Boolean busyWaiting = false);
00399        void messageLoop(JNIEnv* env, JavaMessage* msgToSend, JavaMessageQueue* sendQueue, JavaMessageQueue* receiveQueue, Boolean busyWaiting = false);
00400        
00401 protected:
00402 
00403        MRJPlugin*                         mPlugin;
00404     JNIEnv*                               mProxyEnv;
00405     MRJSession*                           mSession;
00406     nsIThreadManager*              mThreadManager;
00407     
00408     JNIEnv*                        mJavaEnv;
00409     jboolean*                      mIsRunning;
00410     JavaMessageQueue*              mJavaQueue;
00411     JavaMessageQueue*              mNativeQueue;
00412 
00413 private:
00414        // support for SupportsMixin.
00415        static const InterfaceInfo sInterfaces[];
00416        static const UInt32 kInterfaceCount;
00417 };
00418 
00422 CSecureEnv* GetSecureJNI(JNIEnv* env, jobject thread);
00423 
00424 #endif // CSecureJNI2_h___