Backed out changeset fa432b23baa5. (Backing out bug 454781 to investigate mochitest...
[wine-gecko.git] / modules / oji / src / jvmmgr.cpp
blobf9934784ae9f3dcb5498a091ec0cc8abb48ef2be
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
38 #include "prlog.h"
39 #include "nsJVMManager.h"
40 #include "nsIServiceManager.h"
41 #include "nsIJVMPrefsWindow.h"
42 #include "ProxyJNI.h"
43 #include "lcglue.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);
52 PR_BEGIN_EXTERN_C
54 #ifdef PRE_SERVICE_MANAGER
55 extern nsPluginManager* thePluginManager;
56 #endif
58 PR_IMPLEMENT(void)
59 JVM_ReleaseJVMMgr(nsJVMManager* mgr)
61 mgr->Release();
64 static nsIJVMPlugin*
65 GetRunningJVM(void)
67 nsIJVMPlugin* jvm = NULL;
68 nsresult rv;
69 nsCOMPtr<nsIJVMManager> managerService = do_GetService(NS_JVMMANAGER_CONTRACTID, &rv);
70 if (NS_FAILED(rv)) return jvm;
71 nsJVMManager* jvmMgr = (nsJVMManager *)managerService.get();
72 if (jvmMgr) {
73 nsJVMStatus status = jvmMgr->GetJVMStatus();
74 if (status == nsJVMStatus_Enabled)
75 status = jvmMgr->StartupJVM();
76 if (status == nsJVMStatus_Running) {
77 jvm = jvmMgr->GetJVMPlugin();
80 return jvm;
83 PR_IMPLEMENT(nsJVMStatus)
84 JVM_StartupJVM(void)
86 GetRunningJVM();
87 return JVM_GetJVMStatus();
90 PR_IMPLEMENT(nsJVMStatus)
91 JVM_ShutdownJVM(void)
93 nsJVMStatus status = nsJVMStatus_Failed;
94 nsresult rv;
95 nsCOMPtr<nsIJVMManager> managerService = do_GetService(NS_JVMMANAGER_CONTRACTID, &rv);
96 if (NS_FAILED(rv)) return status;
97 nsJVMManager* mgr = (nsJVMManager *)managerService.get();
98 if (mgr) {
99 status = mgr->ShutdownJVM();
101 return status;
105 PR_IMPLEMENT(nsJVMStatus)
106 JVM_GetJVMStatus(void)
108 nsresult rv;
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();
113 if (mgr) {
114 status = mgr->GetJVMStatus();
116 return status;
119 PR_IMPLEMENT(PRBool)
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();
126 if (mgr) {
127 err = mgr->AddToClassPath(dirPath);
129 return err == NS_OK;
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*
137 GetConsole(void)
139 // PENDING(edburns): workaround for bug 76677, make sure the JVM is
140 // started.
141 JNIEnv* env = JVM_GetJNIEnv();
142 if (!env)
143 return nsnull;
145 nsIJVMConsole* console = nsnull;
146 nsIJVMPlugin* jvm = GetRunningJVM();
147 if (jvm)
148 jvm->QueryInterface(kIJVMConsoleIID, (void**)&console);
149 return console;
152 PR_IMPLEMENT(void)
153 JVM_ShowConsole(void)
155 nsIJVMConsole* console = GetConsole();
156 if (console) {
157 console->Show();
158 console->Release();
162 PR_IMPLEMENT(void)
163 JVM_HideConsole(void)
165 nsJVMStatus status = JVM_GetJVMStatus();
166 if (status == nsJVMStatus_Running) {
167 nsIJVMConsole* console = GetConsole();
168 if (console) {
169 console->Hide();
170 console->Release();
175 PR_IMPLEMENT(PRBool)
176 JVM_IsConsoleVisible(void)
178 PRBool result = PR_FALSE;
179 nsJVMStatus status = JVM_GetJVMStatus();
180 if (status == nsJVMStatus_Running) {
181 nsIJVMConsole* console = GetConsole();
182 if (console) {
183 nsresult err = console->IsVisible(&result);
184 PR_ASSERT(err != NS_OK ? result == PR_FALSE : PR_TRUE);
185 console->Release();
188 return result;
191 PR_IMPLEMENT(void)
192 JVM_PrintToConsole(const char* msg)
194 nsJVMStatus status = JVM_GetJVMStatus();
195 if (status != nsJVMStatus_Running)
196 return;
197 nsIJVMConsole* console = GetConsole();
198 if (console) {
199 console->Print(msg);
200 console->Release();
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*
209 GetPrefsWindow(void)
211 nsIJVMPrefsWindow* prefsWin = NULL;
212 nsIJVMPlugin* jvm = GetRunningJVM();
213 if (jvm) {
214 jvm->QueryInterface(kIJVMPrefsWindowIID, (void**)&prefsWin);
215 // jvm->Release(); // GetRunningJVM no longer calls AddRef
217 return prefsWin;
220 PR_IMPLEMENT(void)
221 JVM_ShowPrefsWindow(void)
223 nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
224 if (prefsWin) {
225 prefsWin->Show();
226 prefsWin->Release();
230 PR_IMPLEMENT(void)
231 JVM_HidePrefsWindow(void)
233 nsJVMStatus status = JVM_GetJVMStatus();
234 if (status == nsJVMStatus_Running) {
235 nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
236 if (prefsWin) {
237 prefsWin->Hide();
238 prefsWin->Release();
243 PR_IMPLEMENT(PRBool)
244 JVM_IsPrefsWindowVisible(void)
246 PRBool result = PR_FALSE;
247 nsJVMStatus status = JVM_GetJVMStatus();
248 if (status == nsJVMStatus_Running) {
249 nsIJVMPrefsWindow* prefsWin = GetPrefsWindow();
250 if (prefsWin) {
251 nsresult err = prefsWin->IsVisible(&result);
252 PR_ASSERT(err != NS_OK ? result == PR_FALSE : PR_TRUE);
253 prefsWin->Release();
256 return result;
259 ////////////////////////////////////////////////////////////////////////////////
261 PR_IMPLEMENT(void)
262 JVM_StartDebugger(void)
264 nsIJVMPlugin* jvm = GetRunningJVM();
265 if (jvm) {
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);
270 debugger->Release();
272 // jvm->Release(); // GetRunningJVM no longer calls AddRef
277 PR_IMPLEMENT(JNIEnv*)
278 JVM_GetJNIEnv(void)
280 /* get proxy env for current thread. */
281 JVMContext* context = GetJVMContext();
282 JNIEnv* env = context->proxyEnv;
283 if (env != NULL)
284 return env;
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;
294 return env;
297 PR_IMPLEMENT(void)
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)
309 #ifdef XP_MAC
310 nsresult result = NS_ERROR_NOT_INITIALIZED;
311 nsIJVMPlugin* jvm = GetRunningJVM();
312 if (jvm != NULL)
313 result = jvm->SpendTime(timeMillis);
314 return result;
315 #else
316 return NS_ERROR_NOT_IMPLEMENTED;
317 #endif
320 PR_IMPLEMENT(PRBool)
321 JVM_MaybeStartupLiveConnect()
323 PRBool result = PR_FALSE;
324 nsresult rv;
325 nsCOMPtr<nsIJVMManager> managerService = do_GetService(NS_JVMMANAGER_CONTRACTID, &rv);
326 if (NS_FAILED(rv)) return result;
327 nsJVMManager* mgr = (nsJVMManager *)managerService.get();
328 if (mgr) {
329 result = mgr->MaybeStartupLiveConnect();
331 return result;
335 PR_IMPLEMENT(PRBool)
336 JVM_MaybeShutdownLiveConnect(void)
338 PRBool result = PR_FALSE;
339 nsresult rv;
340 nsCOMPtr<nsIJVMManager> managerService = do_GetService(NS_JVMMANAGER_CONTRACTID, &rv);
341 if (NS_FAILED(rv)) return result;
342 nsJVMManager* mgr = (nsJVMManager *)managerService.get();
343 if (mgr) {
344 result = mgr->MaybeShutdownLiveConnect();
346 return result;
349 PR_IMPLEMENT(PRBool)
350 JVM_IsLiveConnectEnabled(void)
352 PRBool result = PR_FALSE;
353 nsresult rv;
354 nsCOMPtr<nsIJVMManager> managerService = do_GetService(NS_JVMMANAGER_CONTRACTID, &rv);
355 if (NS_FAILED(rv)) return result;
356 nsJVMManager* mgr = (nsJVMManager *)managerService.get();
357 if (mgr) {
358 result = mgr->IsLiveConnectEnabled();
360 return result;
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) {
373 return nsnull;
376 NS_ADDREF(securityContext);
377 return securityContext;
380 PR_END_EXTERN_C
382 ////////////////////////////////////////////////////////////////////////////////