Back to index

lightning-sunbird  0.9+nobinonly
jvmmgr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 #include "prlog.h"
00039 #include "nsJVMManager.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsIJVMPrefsWindow.h"
00042 #include "ProxyJNI.h"
00043 #include "lcglue.h"
00044 #include "nsCSecurityContext.h"
00045 #include "nsIJSContextStack.h"
00046 
00047 static NS_DEFINE_CID(kJVMManagerCID, NS_JVMMANAGER_CID);
00048 static NS_DEFINE_IID(kIJVMConsoleIID, NS_IJVMCONSOLE_IID);
00049 static NS_DEFINE_IID(kIJVMPrefsWindowIID, NS_IJVMPREFSWINDOW_IID);
00050 static NS_DEFINE_IID(kISymantecDebuggerIID, NS_ISYMANTECDEBUGGER_IID);
00051 
00052 PR_BEGIN_EXTERN_C
00053 
00054 #ifdef PRE_SERVICE_MANAGER
00055 extern nsPluginManager* thePluginManager;
00056 #endif
00057 
00058 PR_IMPLEMENT(void)
00059 JVM_ReleaseJVMMgr(nsJVMManager* mgr)
00060 {
00061     mgr->Release();
00062 }
00063 
00064 static nsIJVMPlugin*
00065 GetRunningJVM(void)
00066 {
00067     nsIJVMPlugin* jvm = NULL;
00068     nsresult rv;
00069     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00070     if (NS_FAILED(rv)) return jvm;
00071     nsJVMManager* jvmMgr = (nsJVMManager *)managerService.get();  
00072     if (jvmMgr) {
00073         nsJVMStatus status = jvmMgr->GetJVMStatus();
00074         if (status == nsJVMStatus_Enabled)
00075             status = jvmMgr->StartupJVM();
00076         if (status == nsJVMStatus_Running) {
00077             jvm = jvmMgr->GetJVMPlugin();
00078         }
00079     }
00080     return jvm;
00081 }
00082 
00083 PR_IMPLEMENT(nsJVMStatus)
00084 JVM_StartupJVM(void)
00085 {
00086     GetRunningJVM();
00087     return JVM_GetJVMStatus();
00088 }
00089 
00090 PR_IMPLEMENT(nsJVMStatus)
00091 JVM_ShutdownJVM(void)
00092 {
00093     nsJVMStatus status = nsJVMStatus_Failed;
00094     nsresult rv;
00095     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00096     if (NS_FAILED(rv)) return status;
00097     nsJVMManager* mgr = (nsJVMManager *)managerService.get();  
00098     if (mgr) {
00099         status = mgr->ShutdownJVM();
00100     }
00101     return status;
00102 }
00103 
00104 
00105 PR_IMPLEMENT(nsJVMStatus)
00106 JVM_GetJVMStatus(void)
00107 {
00108     nsresult rv;
00109     nsJVMStatus status = nsJVMStatus_Disabled;
00110     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00111     if (NS_FAILED(rv)) return status;
00112     nsJVMManager* mgr = (nsJVMManager *)managerService.get();  
00113     if (mgr) {
00114         status = mgr->GetJVMStatus();
00115     }
00116     return status;
00117 }
00118 
00119 PR_IMPLEMENT(PRBool)
00120 JVM_AddToClassPath(const char* dirPath)
00121 {
00122     nsresult err = NS_ERROR_FAILURE;
00123     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &err);
00124     if (NS_FAILED(err)) return PR_FALSE;
00125     nsJVMManager* mgr = (nsJVMManager *)managerService.get();
00126     if (mgr) {
00127         err = mgr->AddToClassPath(dirPath);
00128     }
00129     return err == NS_OK;
00130 }
00131 
00133 
00134 // This will get the JVMConsole if one is available. You have to Release it 
00135 // when you're done with it.
00136 static nsIJVMConsole*
00137 GetConsole(void)
00138 {
00139     // PENDING(edburns): workaround for bug 76677, make sure the JVM is
00140     // started.
00141     JNIEnv* env = JVM_GetJNIEnv();
00142     if (!env)
00143         return nsnull;
00144     
00145     nsIJVMConsole* console = nsnull;
00146     nsIJVMPlugin* jvm = GetRunningJVM();
00147     if (jvm)
00148         jvm->QueryInterface(kIJVMConsoleIID, (void**)&console);
00149     return console;
00150 }
00151 
00152 PR_IMPLEMENT(void)
00153 JVM_ShowConsole(void)
00154 {
00155     nsIJVMConsole* console = GetConsole();
00156     if (console) {
00157         console->Show();
00158         console->Release();
00159     }
00160 }
00161 
00162 PR_IMPLEMENT(void)
00163 JVM_HideConsole(void)
00164 {
00165     nsJVMStatus status = JVM_GetJVMStatus();
00166     if (status == nsJVMStatus_Running) {
00167         nsIJVMConsole* console = GetConsole();
00168         if (console) {
00169             console->Hide();
00170             console->Release();
00171         }
00172     }
00173 }
00174 
00175 PR_IMPLEMENT(PRBool)
00176 JVM_IsConsoleVisible(void)
00177 {
00178     PRBool result = PR_FALSE;
00179     nsJVMStatus status = JVM_GetJVMStatus();
00180     if (status == nsJVMStatus_Running) {
00181         nsIJVMConsole* console = GetConsole();
00182         if (console) {
00183             nsresult err = console->IsVisible(&result);
00184             PR_ASSERT(err != NS_OK ? result == PR_FALSE : PR_TRUE);
00185             console->Release();
00186         }
00187     }
00188     return result;
00189 }
00190 
00191 PR_IMPLEMENT(void)
00192 JVM_PrintToConsole(const char* msg)
00193 {
00194     nsJVMStatus status = JVM_GetJVMStatus();
00195     if (status != nsJVMStatus_Running)
00196         return;
00197     nsIJVMConsole* console = GetConsole();
00198     if (console) {
00199         console->Print(msg);
00200         console->Release();
00201     }
00202 }
00203 
00205 
00206 // This will get the JVMPrefsWindow if one is available. You have to Release it 
00207 // when you're done with it.
00208 static nsIJVMPrefsWindow*
00209 GetPrefsWindow(void)
00210 {
00211     nsIJVMPrefsWindow* prefsWin = NULL;
00212     nsIJVMPlugin* jvm = GetRunningJVM();
00213     if (jvm) {
00214         jvm->QueryInterface(kIJVMPrefsWindowIID, (void**)&prefsWin);
00215         // jvm->Release(); // GetRunningJVM no longer calls AddRef
00216     }
00217     return prefsWin;
00218 }
00219 
00220 PR_IMPLEMENT(void)
00221 JVM_ShowPrefsWindow(void)
00222 {
00223     nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
00224     if (prefsWin) {
00225         prefsWin->Show();
00226         prefsWin->Release();
00227     }
00228 }
00229 
00230 PR_IMPLEMENT(void)
00231 JVM_HidePrefsWindow(void)
00232 {
00233     nsJVMStatus status = JVM_GetJVMStatus();
00234     if (status == nsJVMStatus_Running) {
00235         nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
00236         if (prefsWin) {
00237             prefsWin->Hide();
00238             prefsWin->Release();
00239         }
00240     }
00241 }
00242 
00243 PR_IMPLEMENT(PRBool)
00244 JVM_IsPrefsWindowVisible(void)
00245 {
00246     PRBool result = PR_FALSE;
00247     nsJVMStatus status = JVM_GetJVMStatus();
00248     if (status == nsJVMStatus_Running) {
00249         nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
00250         if (prefsWin) {
00251             nsresult err = prefsWin->IsVisible(&result);
00252             PR_ASSERT(err != NS_OK ? result == PR_FALSE : PR_TRUE);
00253             prefsWin->Release();
00254         }
00255     }
00256     return result;
00257 }
00258 
00260 
00261 PR_IMPLEMENT(void)
00262 JVM_StartDebugger(void)
00263 {
00264     nsIJVMPlugin* jvm = GetRunningJVM();
00265     if (jvm) {
00266         nsISymantecDebugger* debugger;
00267         if (jvm->QueryInterface(kISymantecDebuggerIID, (void**)&debugger) == NS_OK) {
00268             // XXX should we make sure the vm is started first?
00269             debugger->StartDebugger(nsSymantecDebugPort_SharedMemory);
00270             debugger->Release();
00271         }
00272         // jvm->Release(); // GetRunningJVM no longer calls AddRef
00273     }
00274 }
00275 
00276 
00277 PR_IMPLEMENT(JNIEnv*)
00278 JVM_GetJNIEnv(void)
00279 {
00280        /* get proxy env for current thread. */
00281        JVMContext* context = GetJVMContext();
00282     JNIEnv* env = context->proxyEnv;
00283        if (env != NULL)
00284               return env;
00285 
00286        // Create a Proxy JNI to associate with this NSPR thread.
00287     nsIJVMPlugin* jvmPlugin = GetRunningJVM();
00288        if (jvmPlugin != NULL)
00289               env = CreateProxyJNI(jvmPlugin);
00290 
00291        /* Associate the JNIEnv with the current thread. */
00292        context->proxyEnv = env;
00293 
00294     return env;
00295 }
00296 
00297 PR_IMPLEMENT(void)
00298 JVM_ReleaseJNIEnv(JNIEnv* env)
00299 {
00304 }
00305 
00306 PR_IMPLEMENT(nsresult)
00307 JVM_SpendTime(PRUint32 timeMillis)
00308 {
00309 #ifdef XP_MAC
00310        nsresult result = NS_ERROR_NOT_INITIALIZED;
00311     nsIJVMPlugin* jvm = GetRunningJVM();
00312     if (jvm != NULL)
00313               result = jvm->SpendTime(timeMillis);
00314        return result;
00315 #else
00316        return NS_ERROR_NOT_IMPLEMENTED;
00317 #endif
00318 }
00319 
00320 PR_IMPLEMENT(PRBool)
00321 JVM_MaybeStartupLiveConnect()
00322 {
00323     PRBool result = PR_FALSE;
00324     nsresult rv;
00325     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00326     if (NS_FAILED(rv)) return result;
00327     nsJVMManager* mgr = (nsJVMManager *)managerService.get();  
00328     if (mgr) {
00329         result = mgr->MaybeStartupLiveConnect();
00330     }
00331     return result;
00332 }
00333 
00334 
00335 PR_IMPLEMENT(PRBool)
00336 JVM_MaybeShutdownLiveConnect(void)
00337 {
00338     PRBool result = PR_FALSE;
00339     nsresult rv;
00340     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00341     if (NS_FAILED(rv)) return result;
00342     nsJVMManager* mgr = (nsJVMManager *)managerService.get(); 
00343     if (mgr) {
00344         result = mgr->MaybeShutdownLiveConnect();
00345     }
00346     return result;
00347 }
00348 
00349 PR_IMPLEMENT(PRBool)
00350 JVM_IsLiveConnectEnabled(void)
00351 {
00352     PRBool result = PR_FALSE;
00353     nsresult rv;
00354     nsCOMPtr<nsIJVMManager> managerService = do_GetService(kJVMManagerCID, &rv);
00355     if (NS_FAILED(rv)) return result;
00356     nsJVMManager* mgr = (nsJVMManager *)managerService.get();
00357     if (mgr) {
00358         result = mgr->IsLiveConnectEnabled();
00359     }
00360     return result;
00361 }
00362 
00363 
00364 PR_IMPLEMENT(nsISecurityContext*) 
00365 JVM_GetJSSecurityContext()
00366 {
00367     JSContext *cx = nsnull;
00368     nsCOMPtr<nsIJSContextStack> stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
00369     if (stack) stack->Peek(&cx);
00370 
00371     nsCSecurityContext *securityContext = new nsCSecurityContext(cx);
00372     if (securityContext == nsnull) {
00373         return nsnull;
00374     }
00375 
00376     NS_ADDREF(securityContext);
00377     return securityContext;
00378 }
00379 
00380 PR_END_EXTERN_C
00381