1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include "nsJVMManager.h"
40 #include "nsIServiceManager.h"
41 #include "nsIJVMPrefsWindow.h"
44 #include "nsCSecurityContext.h"
45 #include "nsIJSContextStack.h"
47 #define NS_JVMMANAGER_CONTRACTID "@mozilla.org/oji/jvm-mgr;1"
48 static NS_DEFINE_IID(kIJVMConsoleIID
, NS_IJVMCONSOLE_IID
);
49 static NS_DEFINE_IID(kIJVMPrefsWindowIID
, NS_IJVMPREFSWINDOW_IID
);
50 static NS_DEFINE_IID(kISymantecDebuggerIID
, NS_ISYMANTECDEBUGGER_IID
);
54 #ifdef PRE_SERVICE_MANAGER
55 extern nsPluginManager
* thePluginManager
;
59 JVM_ReleaseJVMMgr(nsJVMManager
* mgr
)
67 nsIJVMPlugin
* jvm
= NULL
;
69 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
70 if (NS_FAILED(rv
)) return jvm
;
71 nsJVMManager
* jvmMgr
= (nsJVMManager
*)managerService
.get();
73 nsJVMStatus status
= jvmMgr
->GetJVMStatus();
74 if (status
== nsJVMStatus_Enabled
)
75 status
= jvmMgr
->StartupJVM();
76 if (status
== nsJVMStatus_Running
) {
77 jvm
= jvmMgr
->GetJVMPlugin();
83 PR_IMPLEMENT(nsJVMStatus
)
87 return JVM_GetJVMStatus();
90 PR_IMPLEMENT(nsJVMStatus
)
93 nsJVMStatus status
= nsJVMStatus_Failed
;
95 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
96 if (NS_FAILED(rv
)) return status
;
97 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
99 status
= mgr
->ShutdownJVM();
105 PR_IMPLEMENT(nsJVMStatus
)
106 JVM_GetJVMStatus(void)
109 nsJVMStatus status
= nsJVMStatus_Disabled
;
110 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
111 if (NS_FAILED(rv
)) return status
;
112 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
114 status
= mgr
->GetJVMStatus();
120 JVM_AddToClassPath(const char* dirPath
)
122 nsresult err
= NS_ERROR_FAILURE
;
123 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &err
);
124 if (NS_FAILED(err
)) return PR_FALSE
;
125 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
127 err
= mgr
->AddToClassPath(dirPath
);
132 ////////////////////////////////////////////////////////////////////////////////
134 // This will get the JVMConsole if one is available. You have to Release it
135 // when you're done with it.
136 static nsIJVMConsole
*
139 // PENDING(edburns): workaround for bug 76677, make sure the JVM is
141 JNIEnv
* env
= JVM_GetJNIEnv();
145 nsIJVMConsole
* console
= nsnull
;
146 nsIJVMPlugin
* jvm
= GetRunningJVM();
148 jvm
->QueryInterface(kIJVMConsoleIID
, (void**)&console
);
153 JVM_ShowConsole(void)
155 nsIJVMConsole
* console
= GetConsole();
163 JVM_HideConsole(void)
165 nsJVMStatus status
= JVM_GetJVMStatus();
166 if (status
== nsJVMStatus_Running
) {
167 nsIJVMConsole
* console
= GetConsole();
176 JVM_IsConsoleVisible(void)
178 PRBool result
= PR_FALSE
;
179 nsJVMStatus status
= JVM_GetJVMStatus();
180 if (status
== nsJVMStatus_Running
) {
181 nsIJVMConsole
* console
= GetConsole();
183 nsresult err
= console
->IsVisible(&result
);
184 PR_ASSERT(err
!= NS_OK
? result
== PR_FALSE
: PR_TRUE
);
192 JVM_PrintToConsole(const char* msg
)
194 nsJVMStatus status
= JVM_GetJVMStatus();
195 if (status
!= nsJVMStatus_Running
)
197 nsIJVMConsole
* console
= GetConsole();
204 ////////////////////////////////////////////////////////////////////////////////
206 // This will get the JVMPrefsWindow if one is available. You have to Release it
207 // when you're done with it.
208 static nsIJVMPrefsWindow
*
211 nsIJVMPrefsWindow
* prefsWin
= NULL
;
212 nsIJVMPlugin
* jvm
= GetRunningJVM();
214 jvm
->QueryInterface(kIJVMPrefsWindowIID
, (void**)&prefsWin
);
215 // jvm->Release(); // GetRunningJVM no longer calls AddRef
221 JVM_ShowPrefsWindow(void)
223 nsIJVMPrefsWindow
* prefsWin
= GetPrefsWindow();
231 JVM_HidePrefsWindow(void)
233 nsJVMStatus status
= JVM_GetJVMStatus();
234 if (status
== nsJVMStatus_Running
) {
235 nsIJVMPrefsWindow
* prefsWin
= GetPrefsWindow();
244 JVM_IsPrefsWindowVisible(void)
246 PRBool result
= PR_FALSE
;
247 nsJVMStatus status
= JVM_GetJVMStatus();
248 if (status
== nsJVMStatus_Running
) {
249 nsIJVMPrefsWindow
* prefsWin
= GetPrefsWindow();
251 nsresult err
= prefsWin
->IsVisible(&result
);
252 PR_ASSERT(err
!= NS_OK
? result
== PR_FALSE
: PR_TRUE
);
259 ////////////////////////////////////////////////////////////////////////////////
262 JVM_StartDebugger(void)
264 nsIJVMPlugin
* jvm
= GetRunningJVM();
266 nsISymantecDebugger
* debugger
;
267 if (jvm
->QueryInterface(kISymantecDebuggerIID
, (void**)&debugger
) == NS_OK
) {
268 // XXX should we make sure the vm is started first?
269 debugger
->StartDebugger(nsSymantecDebugPort_SharedMemory
);
272 // jvm->Release(); // GetRunningJVM no longer calls AddRef
277 PR_IMPLEMENT(JNIEnv
*)
280 /* get proxy env for current thread. */
281 JVMContext
* context
= GetJVMContext();
282 JNIEnv
* env
= context
->proxyEnv
;
286 // Create a Proxy JNI to associate with this NSPR thread.
287 nsIJVMPlugin
* jvmPlugin
= GetRunningJVM();
288 if (jvmPlugin
!= NULL
)
289 env
= CreateProxyJNI(jvmPlugin
);
291 /* Associate the JNIEnv with the current thread. */
292 context
->proxyEnv
= env
;
298 JVM_ReleaseJNIEnv(JNIEnv
* env
)
301 * this is now done when the NSPR thread is shutdown. JNIEnvs are always tied to the
302 * lifetime of threads.
306 PR_IMPLEMENT(nsresult
)
307 JVM_SpendTime(PRUint32 timeMillis
)
310 nsresult result
= NS_ERROR_NOT_INITIALIZED
;
311 nsIJVMPlugin
* jvm
= GetRunningJVM();
313 result
= jvm
->SpendTime(timeMillis
);
316 return NS_ERROR_NOT_IMPLEMENTED
;
321 JVM_MaybeStartupLiveConnect()
323 PRBool result
= PR_FALSE
;
325 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
326 if (NS_FAILED(rv
)) return result
;
327 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
329 result
= mgr
->MaybeStartupLiveConnect();
336 JVM_MaybeShutdownLiveConnect(void)
338 PRBool result
= PR_FALSE
;
340 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
341 if (NS_FAILED(rv
)) return result
;
342 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
344 result
= mgr
->MaybeShutdownLiveConnect();
350 JVM_IsLiveConnectEnabled(void)
352 PRBool result
= PR_FALSE
;
354 nsCOMPtr
<nsIJVMManager
> managerService
= do_GetService(NS_JVMMANAGER_CONTRACTID
, &rv
);
355 if (NS_FAILED(rv
)) return result
;
356 nsJVMManager
* mgr
= (nsJVMManager
*)managerService
.get();
358 result
= mgr
->IsLiveConnectEnabled();
364 PR_IMPLEMENT(nsISecurityContext
*)
365 JVM_GetJSSecurityContext()
367 JSContext
*cx
= nsnull
;
368 nsCOMPtr
<nsIJSContextStack
> stack
= do_GetService("@mozilla.org/js/xpc/ContextStack;1");
369 if (stack
) stack
->Peek(&cx
);
371 nsCSecurityContext
*securityContext
= new nsCSecurityContext(cx
);
372 if (securityContext
== nsnull
) {
376 NS_ADDREF(securityContext
);
377 return securityContext
;
382 ////////////////////////////////////////////////////////////////////////////////