home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / plugin / nsjvm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.9 KB  |  352 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. ////////////////////////////////////////////////////////////////////////////////
  20. // Plugin Manager Methods to support the JVM Plugin API
  21. ////////////////////////////////////////////////////////////////////////////////
  22.  
  23. #include "npglue.h"
  24. #include "prefapi.h"
  25. #include "xp_str.h"
  26. #include "libmocha.h"
  27.  
  28. #include "nsjvm.h"
  29.  
  30. #if 0   // XXX for now
  31.  
  32. static int PR_CALLBACK
  33. JavaPrefChanged(const char *prefStr, void* data)
  34. {
  35.     nsPluginManager* mgr = (nsPluginManager*)data;
  36.     PRBool prefBool;
  37.     PREF_GetBoolPref(prefStr, &prefBool);
  38.     mgr->SetJVMEnabled(prefBool);
  39.     return 0;
  40.  
  41. void
  42. nsPluginManager::EnsurePrefCallbackRegistered(void)
  43. {
  44.     if (fRegisteredJavaPrefChanged != PR_TRUE) {
  45.         fRegisteredJavaPrefChanged = PR_TRUE;
  46.         PREF_RegisterCallback("security.enable_java", JavaPrefChanged, this);
  47.         JavaPrefChanged("security.enable_java", this);
  48.     }
  49. }
  50.  
  51. PRBool
  52. nsPluginManager::GetJVMEnabled(void)
  53. {
  54.     EnsurePrefCallbackRegistered();
  55.     return fJVM->GetJVMEnabled();
  56. }
  57.  
  58. void
  59. nsPluginManager::SetJVMEnabled(PRBool enable)
  60. {
  61.     return fJVM->SetJVMEnabled(enable);
  62. }
  63.  
  64. JVMStatus
  65. nsPluginManager::GetJVMStatus(void)
  66. {
  67.     EnsurePrefCallbackRegistered();
  68.     return fJVM->GetJVMStatus();
  69. }
  70.  
  71. ////////////////////////////////////////////////////////////////////////////////
  72.  
  73. // Should be in a header; must solve build-order problem first
  74. extern void VR_Initialize(JRIEnv* env);
  75. extern void SU_Initialize(JRIEnv * env);
  76.  
  77. JVMStatus
  78. nsPluginManager::StartupJVM()
  79. {
  80.     // Be sure to check the prefs first before asking java to startup.
  81.     if (GetJVMStatus() == JVMStatus_Disabled) {
  82.         return JVMStatus_Disabled;
  83.     }
  84.     JVMStatus status = fJVM->StartupJVM();
  85.     if (status == JVMStatus_Ok) {
  86.         JRIEnv* env = fJVM->EnsureExecEnv();
  87.  
  88.         /* initialize VersionRegistry native routines */
  89.         /* it is not an error that prevents java from starting if this stuff throws exceptions */
  90.         VR_Initialize(env);
  91.         if (JRI_ExceptionOccurred(env)) {
  92. #if DEBUG
  93.             fJVM->PrintToConsole("LJ:  VR_Initialize failed.  Bugs to dveditz.\n");
  94. #endif    
  95.             goto error;
  96.         }
  97.     
  98.         SU_Initialize(env);
  99.         if (JRI_ExceptionOccurred(env)) {
  100. #if DEBUG
  101.             fJVM->PrintToConsole("LJ:  SU_Initialize failed.  Bugs to atotic.\n");
  102. #endif    
  103.             goto error;
  104.         }
  105.     }
  106.     return status;
  107.  
  108.   error:
  109. #if DEBUG
  110.     JRI_ExceptionDescribe(env);
  111. #endif    
  112.     JRI_ExceptionClear(env);
  113.     ShutdownJVM();
  114.     return JVMStatus_JavaError;
  115. }
  116.  
  117. JVMStatus
  118. nsPluginManager::ShutdownJVM(PRBool fullShutdown = PR_TRUE)
  119. {
  120.     return fJVM->ShutdownJVM();
  121. }
  122.  
  123. #endif // 0
  124.  
  125. ////////////////////////////////////////////////////////////////////////////////
  126.  
  127. void NP_LOADDS
  128. nsPluginManager::BeginWaitCursor(void)
  129. {
  130.     if (fWaiting == 0) {
  131. #ifdef XP_PC
  132.         fOldCursor = (void*)GetCursor();
  133.     HCURSOR newCursor = LoadCursor(NULL, IDC_WAIT);
  134.     if (newCursor)
  135.         SetCursor(newCursor);
  136. #endif
  137. #ifdef XP_MAC
  138.     startAsyncCursors();
  139. #endif
  140.     }
  141.     fWaiting++;
  142. }
  143.  
  144. void NP_LOADDS
  145. nsPluginManager::EndWaitCursor(void)
  146. {
  147.     fWaiting--;
  148.     if (fWaiting == 0) {
  149. #ifdef XP_PC
  150.     if (fOldCursor)
  151.         SetCursor((HCURSOR)fOldCursor);
  152. #endif
  153. #ifdef XP_MAC
  154.     stopAsyncCursors();
  155. #endif
  156.         fOldCursor = NULL;
  157.     }
  158. }
  159.  
  160. ////////////////////////////////////////////////////////////////////////////////
  161.  
  162. const char* NP_LOADDS
  163. nsPluginManager::GetProgramPath(void)
  164. {
  165.     return fProgramPath;
  166. }
  167.  
  168. const char* NP_LOADDS
  169. nsPluginManager::GetTempDirPath(void)
  170. {
  171.     // XXX I don't need a static really, the browser holds the tempDir name
  172.     // as a static string -- it's just the XP_TempDirName that strdups it.
  173.     static const char* tempDirName = NULL;
  174.     if (tempDirName == NULL)
  175.         tempDirName = XP_TempDirName();
  176.     return tempDirName;
  177. }
  178.  
  179. nsresult NP_LOADDS
  180. nsPluginManager::GetFileName(const char* fn, FileNameType type,
  181.                              char* resultBuf, PRUint32 bufLen)
  182. {
  183.     // XXX This should be rewritten so that we don't have to malloc the name.
  184.     XP_FileType filetype;
  185.  
  186.     if (type == SIGNED_APPLET_DBNAME)
  187.         filetype = xpSignedAppletDB;
  188.     else if (type == TEMP_FILENAME)
  189.         filetype = xpTemporary;
  190.     else 
  191.         return NS_ERROR_ILLEGAL_VALUE;
  192.  
  193.     char* tempName = WH_FileName(fn, filetype);
  194.     if (tempName != NULL) return NS_ERROR_OUT_OF_MEMORY;
  195.     XP_STRNCPY_SAFE(resultBuf, tempName, bufLen);
  196.     XP_FREE(tempName);
  197.     return NS_OK;
  198. }
  199.  
  200. nsresult NP_LOADDS
  201. nsPluginManager::NewTempFileName(const char* prefix, char* resultBuf, PRUint32 bufLen)
  202. {
  203.     // XXX This should be rewritten so that we don't have to malloc the name.
  204.     char* tempName = WH_TempName(xpTemporary, prefix);
  205.     if (tempName != NULL) return NS_ERROR_OUT_OF_MEMORY;
  206.     XP_STRNCPY_SAFE(resultBuf, tempName, bufLen);
  207.     XP_FREE(tempName);
  208.     return NS_OK;
  209. }
  210.  
  211. PRBool NP_LOADDS
  212. nsPluginManager::HandOffJSLock(PRThread* oldOwner, PRThread* newOwner)
  213. {
  214.     return LM_HandOffJSLock(oldOwner, newOwner);
  215. }
  216.  
  217. ////////////////////////////////////////////////////////////////////////////
  218. // Debugger Stuff
  219.  
  220. extern "C" HWND FindNavigatorHiddenWindow(void);
  221.  
  222. PRBool NP_LOADDS
  223. nsPluginManager::SetDebugAgentPassword(PRInt32 pwd)
  224. {
  225. #if defined(XP_PC) && defined(_WIN32)
  226.     HWND win = FindNavigatorHiddenWindow();
  227.     HANDLE sem;
  228.     long err;
  229.  
  230.     /* set up by aHiddenFrameClass in CNetscapeApp::InitInstance */
  231.     err = SetWindowLong(win, 0, pwd);    
  232.     if (err == 0) {
  233. //        PR_LOG(NSJAVA, PR_LOG_ALWAYS,
  234. //               ("SetWindowLong returned %ld (err=%d)\n", err, GetLastError()));
  235.         /* continue so that we try to wake up the debugger */
  236.     }
  237.     sem = OpenSemaphore(SEMAPHORE_MODIFY_STATE, FALSE, "Netscape-Symantec Debugger");
  238.     if (sem) {
  239.         ReleaseSemaphore(sem, 1, NULL);
  240.         CloseHandle(sem);
  241.     }
  242.     return PR_TRUE;
  243. #else
  244.     return PR_FALSE;
  245. #endif
  246. }
  247.  
  248. ////////////////////////////////////////////////////////////////////////////////
  249.  
  250. #if 0 // XXX for now
  251.  
  252. PRBool
  253. nsPluginManager::StartDebugger(JVMDebugPort port)
  254. {
  255.     JVMError err = fJVM->StartDebugger(port);
  256.     if (err != JVMError_NoError) {
  257.         JRIEnv* env = fJVM->EnsureJNIEnv();
  258.         ReportJVMError(env, err);
  259.         return PR_FALSE;
  260.     }
  261.     return PR_TRUE;
  262. }
  263.  
  264. void
  265. nsPluginManager::ReportJVMError(JRIEnv* env, JVMError err)
  266. {
  267.     MWContext* cx = XP_FindSomeContext();
  268.     char *s;
  269.     switch (err) {
  270.       case LJStartupError_NoClasses: {
  271.           const char* cp = CLASSPATH();
  272.           cp = lj_ConvertToPlatformPathList(cp);
  273.           s = PR_smprintf(XP_GetString(XP_JAVA_NO_CLASSES),
  274.                           lj_jar_name, lj_jar_name,
  275.                           (cp ? cp : "<none>"));
  276.           free((void*)cp);
  277.           break;
  278.       }
  279.  
  280.       case LJStartupError_JavaError: {
  281.           const char* msg = GetJavaErrorString(env);
  282. #ifdef DEBUG
  283.           JRI_ExceptionDescribe(env);
  284. #endif
  285.           s = PR_smprintf(XP_GetString(XP_JAVA_STARTUP_FAILED), 
  286.                           (msg ? msg : ""));
  287.           if (msg) free((void*)msg);
  288.           break;
  289.       }
  290.       case LJStartupError_NoDebugger: {
  291.           s = PR_smprintf(XP_GetString(XP_JAVA_DEBUGGER_FAILED));
  292.           break;
  293.       }
  294.       default:
  295.         return;    /* don't report anything */
  296.     }
  297.     if (s) {
  298.         FE_Alert(cx, s);
  299.         free(s);
  300.     }
  301. }
  302.  
  303. /* stolen from java_lang_Object.h (jri version) */
  304. #define classname_java_lang_Object    "java/lang/Object"
  305. #define name_java_lang_Object_toString    "toString"
  306. #define sig_java_lang_Object_toString     "()Ljava/lang/String;"
  307.  
  308. const char*
  309. nsPluginManager::GetJavaErrorString(JRIEnv* env)
  310. {
  311.     /* XXX javah is a pain wrt mixing JRI and JDK native methods. 
  312.        Since we need to call a method on Object, we'll do it the hard way 
  313.        to avoid using javah for this.
  314.        Maybe we need a new JRI entry point for toString. Yikes! */
  315.  
  316.     const char* msg;
  317.     struct java_lang_Class* classObject;
  318.     JRIMethodID toString;
  319.     struct java_lang_String* excString;
  320.     struct java_lang_Throwable* exc = JRI_ExceptionOccurred(env);
  321.  
  322.     if (exc == NULL) {
  323.         return strdup("");    /* XXX better "no error" message? */
  324.     }
  325.  
  326.     /* Got to do this before trying to find a class (like Object). 
  327.        This is because the runtime refuses to do this with a pending 
  328.        exception! I think it's broken. */
  329.  
  330.     JRI_ExceptionClear(env);
  331.  
  332.     classObject = (struct java_lang_Class*)
  333.         JRI_FindClass(env, classname_java_lang_Object);
  334.     toString = JRI_GetMethodID(env, classObject,
  335.                                name_java_lang_Object_toString,
  336.                                sig_java_lang_Object_toString);
  337.     excString = (struct java_lang_String *)
  338.         JRI_CallMethod(env)(env, JRI_CallMethod_op,
  339.                             (struct java_lang_Object*)exc, toString);
  340.  
  341.     /* XXX change to JRI_GetStringPlatformChars */
  342.     msg = JRI_GetStringPlatformChars(env, excString, NULL, 0);
  343.     if (msg == NULL) return NULL;
  344.     return strdup(msg);
  345. }
  346.  
  347. #endif // 0
  348.  
  349. ////////////////////////////////////////////////////////////////////////////////
  350.  
  351.