Bug 469739 - Add support for displaying Vista UAC shield icon; r=joe sr=vladimir
[wine-gecko.git] / js / src / liveconnect / jsj_simpleapi.c
blob15f5853356045e9dc54a8d9eccbeb7b435391b90
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* The convenience functions below present a complete, but simplified
41 LiveConnect API which is designed to handle the special case of a single
42 Java-VM, single-threaded operation, and use of only one JSContext. */
44 #include "jsjava.h"
45 #include "jsprf.h"
46 #include "jsutil.h"
48 #include <string.h>
50 /* We can get away with global variables in our single-threaded,
51 single-JSContext case. */
52 static JSJavaVM * the_jsj_vm = NULL;
53 static JSContext * the_cx = NULL;
54 static JSJavaThreadState * the_jsj_thread = NULL;
55 static JSObject * the_global_js_obj = NULL;
57 /* Trivial implementation of callback function */
58 static JSJavaThreadState *
59 default_map_js_context_to_jsj_thread(JSContext *cx, char **errp)
61 return the_jsj_thread;
64 /* Trivial implementation of callback function */
65 static JSContext *
66 default_map_jsj_thread_to_js_context(JSJavaThreadState *jsj_env,
67 #ifdef OJI
68 void *java_applet_obj,
69 #endif
70 JNIEnv *jEnv,
71 char **errp)
73 return the_cx;
76 /* Trivial implementation of callback function */
77 static JSObject *
78 default_map_java_object_to_js_object(JNIEnv *jEnv, void *hint, char **errp)
80 return the_global_js_obj;
83 static JSBool
84 default_create_java_vm(SystemJavaVM* *jvm, JNIEnv* *initialEnv, void* initargs)
86 jint err;
87 const char* user_classpath = (const char*)initargs;
88 char* full_classpath = NULL;
90 /* No Java VM supplied, so create our own */
91 JDK1_1InitArgs vm_args;
92 memset(&vm_args, 0, sizeof(vm_args));
94 /* Magic constant indicates JRE version 1.1 */
95 vm_args.version = 0x00010001;
96 JNI_GetDefaultJavaVMInitArgs(&vm_args);
98 /* Prepend the classpath argument to the default JVM classpath */
99 if (user_classpath) {
100 #if defined(XP_UNIX) || defined(XP_BEOS)
101 full_classpath = JS_smprintf("%s:%s", user_classpath, vm_args.classpath);
102 #else
103 full_classpath = JS_smprintf("%s;%s", user_classpath, vm_args.classpath);
104 #endif
105 if (!full_classpath) {
106 return JS_FALSE;
108 vm_args.classpath = (char*)full_classpath;
111 err = JNI_CreateJavaVM((JavaVM**)jvm, initialEnv, &vm_args);
113 if (full_classpath)
114 JS_smprintf_free(full_classpath);
116 return err == 0;
119 static JSBool
120 default_destroy_java_vm(SystemJavaVM* jvm, JNIEnv* initialEnv)
122 JavaVM* java_vm = (JavaVM*)jvm;
123 jint err = (*java_vm)->DestroyJavaVM(java_vm);
124 return err == 0;
127 static JNIEnv*
128 default_attach_current_thread(SystemJavaVM* jvm)
130 JavaVM* java_vm = (JavaVM*)jvm;
131 JNIEnv* env = NULL;
132 (*java_vm)->AttachCurrentThread(java_vm, &env, NULL);
133 return env;
136 static JSBool
137 default_detach_current_thread(SystemJavaVM* jvm, JNIEnv* env)
139 JavaVM* java_vm = (JavaVM*)jvm;
140 /* assert that env is the JNIEnv of the current thread */
141 jint err = (*java_vm)->DetachCurrentThread(java_vm);
142 return err == 0;
145 static SystemJavaVM*
146 default_get_java_vm(JNIEnv* env)
148 JavaVM* java_vm = NULL;
149 (*env)->GetJavaVM(env, &java_vm);
150 return (SystemJavaVM*)java_vm;
153 /* Trivial implementations of callback functions */
154 JSJCallbacks jsj_default_callbacks = {
155 default_map_jsj_thread_to_js_context,
156 default_map_js_context_to_jsj_thread,
157 default_map_java_object_to_js_object,
158 NULL,
159 NULL,
160 NULL,
161 NULL,
162 NULL,
163 NULL,
164 default_create_java_vm,
165 default_destroy_java_vm,
166 default_attach_current_thread,
167 default_detach_current_thread,
168 default_get_java_vm
172 * Initialize the provided JSContext by setting up the JS classes necessary for
173 * reflection and by defining JavaPackage objects for the default Java packages
174 * as properties of global_obj. If java_vm is NULL, a new Java VM is
175 * created, using the provided classpath in addition to any default classpath.
176 * The classpath argument is ignored, however, if java_vm is non-NULL.
178 JSBool
179 JSJ_SimpleInit(JSContext *cx, JSObject *global_obj, SystemJavaVM *java_vm, const char *classpath)
181 JNIEnv *jEnv;
183 JSJ_Init(&jsj_default_callbacks);
185 JS_ASSERT(!the_jsj_vm);
186 the_jsj_vm = JSJ_ConnectToJavaVM(java_vm, (void*)classpath);
187 if (!the_jsj_vm)
188 return JS_FALSE;
190 if (!JSJ_InitJSContext(cx, global_obj, NULL))
191 goto error;
192 the_cx = cx;
193 the_global_js_obj = global_obj;
195 the_jsj_thread = JSJ_AttachCurrentThreadToJava(the_jsj_vm, "main thread", &jEnv);
196 if (!the_jsj_thread)
197 goto error;
198 JSJ_SetDefaultJSContextForJavaThread(cx, the_jsj_thread);
199 return JS_TRUE;
201 error:
202 JSJ_SimpleShutdown();
203 return JS_FALSE;
207 * Free up all LiveConnect resources. Destroy the Java VM if it was
208 * created by LiveConnect.
210 JS_EXPORT_API(void)
211 JSJ_SimpleShutdown()
213 JS_ASSERT(the_jsj_vm);
214 JSJ_DisconnectFromJavaVM(the_jsj_vm);
215 the_jsj_vm = NULL;
216 the_cx = NULL;
217 the_global_js_obj = NULL;
218 the_jsj_thread = NULL;