File: nsJavaXPCOMGlue.cpp

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (397 lines) | stat: -rw-r--r-- 15,564 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Java XPCOM Bindings.
 *
 * The Initial Developer of the Original Code is IBM Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * IBM Corporation. All Rights Reserved.
 *
 * Contributor(s):
 *   Javier Pedemonte (jhpedemonte@gmail.com)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "jni.h"
#include "nsXPCOMPrivate.h" // for XPCOM_DLL defines.
#include "nsXPCOMGlue.h"
#include <stdlib.h>

#if defined(XP_WIN) || defined(XP_OS2)
#define JX_EXPORT   JNIEXPORT
#else
#define JX_EXPORT   JNIEXPORT NS_EXPORT
#endif


/***********************
 *  JNI Load & Unload
 ***********************/

extern "C" JX_EXPORT jint JNICALL
JNI_OnLoad(JavaVM* vm, void* reserved)
{
  // Let the JVM know that we are using JDK 1.2 JNI features.
  return JNI_VERSION_1_2;
}

extern "C" JX_EXPORT void JNICALL
JNI_OnUnload(JavaVM* vm, void* reserved)
{
}

/********************************
 *  JavaXPCOM JNI interfaces
 ********************************/

#define JXM_NATIVE(func) Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_##func

enum {
  kFunc_Initialize,
  kFunc_InitEmbedding,
  kFunc_TermEmbedding,
  kFunc_LockProfileDirectory,
  kFunc_NotifyProfile,
  kFunc_InitXPCOM,
  kFunc_ShutdownXPCOM,
  kFunc_GetComponentManager,
  kFunc_GetComponentRegistrar,
  kFunc_GetServiceManager,
  kFunc_NewLocalFile,
  kFunc_CallXPCOMMethod,
  kFunc_FinalizeProxy,
  kFunc_IsSameXPCOMObject,
  kFunc_ReleaseProfileLock,
  kFunc_GetNativeHandleFromAWT,
  kFunc_WrapJavaObject,
  kFunc_WrapXPCOMObject
};

#define JX_NUM_FUNCS 18


// Get path string from java.io.File object.
jstring
GetJavaFilePath(JNIEnv* env, jobject aFile)
{
  jclass clazz = env->FindClass("java/io/File");
  if (clazz) {
    jmethodID pathMID = env->GetMethodID(clazz, "getCanonicalPath",
                                         "()Ljava/lang/String;");
    if (pathMID) {
      return (jstring) env->CallObjectMethod(aFile, pathMID);
    }
  }

  return nsnull;
}

// Calls XPCOMGlueStartup using the given java.io.File object, and loads
// the JavaXPCOM methods from the XUL shared library.
nsresult
LoadXULMethods(JNIEnv* env, jobject aXPCOMPath, void** aFunctions)
{
  jstring pathString = GetJavaFilePath(env, aXPCOMPath);
  if (!pathString)
    return NS_ERROR_FAILURE;
  const char* path = env->GetStringUTFChars(pathString, nsnull);
  if (!path)
    return NS_ERROR_OUT_OF_MEMORY;

  int len = strlen(path);
  char* xpcomPath = (char*) malloc(len + sizeof(XPCOM_DLL) +
                                   sizeof(XPCOM_FILE_PATH_SEPARATOR) + 1);
  if (!xpcomPath)
    return NS_ERROR_OUT_OF_MEMORY;
  sprintf(xpcomPath, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DLL, path);

  nsresult rv = XPCOMGlueStartup(xpcomPath);
  free(xpcomPath);
  if (NS_FAILED(rv))
    return rv;

#ifdef XP_WIN32
  // The JNICALL calling convention defines to "__stdcall" on Win32, which
  // mangles the name.
  nsDynamicFunctionLoad funcs[] = {
    { "_Java_org_mozilla_xpcom_internal_MozillaImpl_initialize@8",
            (NSFuncPtr*) &aFunctions[kFunc_Initialize] },
    { "_Java_org_mozilla_xpcom_internal_GREImpl_initEmbedding@20",
            (NSFuncPtr*) &aFunctions[kFunc_InitEmbedding] },
    { "_Java_org_mozilla_xpcom_internal_GREImpl_termEmbedding@8",
            (NSFuncPtr*) &aFunctions[kFunc_TermEmbedding] },
    { "_Java_org_mozilla_xpcom_internal_GREImpl_lockProfileDirectory@12",
            (NSFuncPtr*) &aFunctions[kFunc_LockProfileDirectory] },
    { "_Java_org_mozilla_xpcom_internal_GREImpl_notifyProfile@8",
            (NSFuncPtr*) &aFunctions[kFunc_NotifyProfile] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_initXPCOM@16",
            (NSFuncPtr*) &aFunctions[kFunc_InitXPCOM] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_shutdownXPCOM@12",
            (NSFuncPtr*) &aFunctions[kFunc_ShutdownXPCOM] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentManager@8",
            (NSFuncPtr*) &aFunctions[kFunc_GetComponentManager] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentRegistrar@8",
            (NSFuncPtr*) &aFunctions[kFunc_GetComponentRegistrar] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_getServiceManager@8",
            (NSFuncPtr*) &aFunctions[kFunc_GetServiceManager] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMImpl_newLocalFile@16",
            (NSFuncPtr*) &aFunctions[kFunc_NewLocalFile] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_callXPCOMMethod@20",
            (NSFuncPtr*) &aFunctions[kFunc_CallXPCOMMethod] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_finalizeProxy@12",
            (NSFuncPtr*) &aFunctions[kFunc_FinalizeProxy] },
    { "_Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject@16",
            (NSFuncPtr*) &aFunctions[kFunc_IsSameXPCOMObject] },
    { "_Java_org_mozilla_xpcom_ProfileLock_release@16",
            (NSFuncPtr*) &aFunctions[kFunc_ReleaseProfileLock] },
    { "_Java_org_mozilla_xpcom_internal_MozillaImpl_getNativeHandleFromAWT@12",
            (NSFuncPtr*) &aFunctions[kFunc_GetNativeHandleFromAWT] },
    { "_Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapJavaObject@16",
            (NSFuncPtr*) &aFunctions[kFunc_WrapJavaObject] },
    { "_Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapXPCOMObject@20",
            (NSFuncPtr*) &aFunctions[kFunc_WrapXPCOMObject] },
    { nsnull, nsnull }
  };
#else
  nsDynamicFunctionLoad funcs[] = {
    { "Java_org_mozilla_xpcom_internal_MozillaImpl_initialize",
            (NSFuncPtr*) &aFunctions[kFunc_Initialize] },
    { "Java_org_mozilla_xpcom_internal_GREImpl_initEmbedding",
            (NSFuncPtr*) &aFunctions[kFunc_InitEmbedding] },
    { "Java_org_mozilla_xpcom_internal_GREImpl_termEmbedding",
            (NSFuncPtr*) &aFunctions[kFunc_TermEmbedding] },
    { "Java_org_mozilla_xpcom_internal_GREImpl_lockProfileDirectory",
            (NSFuncPtr*) &aFunctions[kFunc_LockProfileDirectory] },
    { "Java_org_mozilla_xpcom_internal_GREImpl_notifyProfile",
            (NSFuncPtr*) &aFunctions[kFunc_NotifyProfile] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_initXPCOM",
            (NSFuncPtr*) &aFunctions[kFunc_InitXPCOM] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_shutdownXPCOM",
            (NSFuncPtr*) &aFunctions[kFunc_ShutdownXPCOM] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentManager",
            (NSFuncPtr*) &aFunctions[kFunc_GetComponentManager] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentRegistrar",
            (NSFuncPtr*) &aFunctions[kFunc_GetComponentRegistrar] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getServiceManager",
            (NSFuncPtr*) &aFunctions[kFunc_GetServiceManager] },
    { "Java_org_mozilla_xpcom_internal_XPCOMImpl_newLocalFile",
            (NSFuncPtr*) &aFunctions[kFunc_NewLocalFile] },
    { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_callXPCOMMethod",
            (NSFuncPtr*) &aFunctions[kFunc_CallXPCOMMethod] },
    { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_finalizeProxy",
            (NSFuncPtr*) &aFunctions[kFunc_FinalizeProxy] },
    { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject",
            (NSFuncPtr*) &aFunctions[kFunc_IsSameXPCOMObject] },
    { "Java_org_mozilla_xpcom_ProfileLock_release",
            (NSFuncPtr*) &aFunctions[kFunc_ReleaseProfileLock] },
    { "Java_org_mozilla_xpcom_internal_MozillaImpl_getNativeHandleFromAWT",
            (NSFuncPtr*) &aFunctions[kFunc_GetNativeHandleFromAWT] },
    { "Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapJavaObject",
            (NSFuncPtr*) &aFunctions[kFunc_WrapJavaObject] },
    { "Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapXPCOMObject",
            (NSFuncPtr*) &aFunctions[kFunc_WrapXPCOMObject] },
    { nsnull, nsnull }
  };
#endif

  rv = XPCOMGlueLoadXULFunctions(funcs);
  if (NS_FAILED(rv))
    return rv;

  return NS_OK;
}

void
ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage)
{
  // Only throw this exception if one hasn't already been thrown, so we don't
  // mask a previous exception/error.
  if (env->ExceptionCheck())
    return;

  // If the error code we get is for an Out Of Memory error, try to throw an
  // OutOfMemoryError.  The JVM may have enough memory to create this error.
  if (aErrorCode == NS_ERROR_OUT_OF_MEMORY) {
    jclass clazz = env->FindClass("java/lang/OutOfMemoryError");
    if (clazz) {
      env->ThrowNew(clazz, aMessage);
    }
    env->DeleteLocalRef(clazz);
    return;
  }

  // If the error was not handled above, then create an XPCOMException with the
  // given error code.
  jthrowable throwObj = nsnull;
  jclass exceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException");
  if (exceptionClass) {
    jmethodID mid = env->GetMethodID(exceptionClass, "<init>",
                                     "(JLjava/lang/String;)V");
    if (mid) {
      throwObj = (jthrowable) env->NewObject(exceptionClass, mid,
                                             (PRInt64) aErrorCode,
                                             env->NewStringUTF(aMessage));
    }
  }
  NS_ASSERTION(throwObj, "Failed to create XPCOMException object");

  // throw exception
  if (throwObj) {
    env->Throw(throwObj);
  }
}

// Register the JavaXPCOM native methods.  This associates a native Java
// method with its C implementation.
nsresult
RegisterNativeMethods(JNIEnv* env, void** aFunctions)
{
  JNINativeMethod mozilla_methods[] = {
    { "initializeNative", "()V",
      (void*) aFunctions[kFunc_Initialize] },
    { "getNativeHandleFromAWT", "(Ljava/lang/Object;)J",
      (void*) aFunctions[kFunc_GetNativeHandleFromAWT] }
  };

  JNINativeMethod gre_methods[] = {
    { "initEmbeddingNative",
      "(Ljava/io/File;Ljava/io/File;Lorg/mozilla/xpcom/IAppFileLocProvider;)V",
      (void*) aFunctions[kFunc_InitEmbedding] },
    { "termEmbedding", "()V",
      (void*) aFunctions[kFunc_TermEmbedding] },
    { "lockProfileDirectory", "(Ljava/io/File;)Lorg/mozilla/xpcom/ProfileLock;",
      (void*) aFunctions[kFunc_LockProfileDirectory] },
    { "notifyProfile", "()V",
      (void*) aFunctions[kFunc_NotifyProfile] },
  };

  JNINativeMethod xpcom_methods[] = {
    { "initXPCOMNative",
      "(Ljava/io/File;Lorg/mozilla/xpcom/IAppFileLocProvider;)Lorg/mozilla/interfaces/nsIServiceManager;",
      (void*) aFunctions[kFunc_InitXPCOM] },
    { "shutdownXPCOM", "(Lorg/mozilla/interfaces/nsIServiceManager;)V",
      (void*) aFunctions[kFunc_ShutdownXPCOM] },
    { "getComponentManager", "()Lorg/mozilla/interfaces/nsIComponentManager;",
      (void*) aFunctions[kFunc_GetComponentManager] },
    { "getComponentRegistrar", "()Lorg/mozilla/interfaces/nsIComponentRegistrar;",
      (void*) aFunctions[kFunc_GetComponentRegistrar] },
    { "getServiceManager", "()Lorg/mozilla/interfaces/nsIServiceManager;",
      (void*) aFunctions[kFunc_GetServiceManager] },
    { "newLocalFile", "(Ljava/lang/String;Z)Lorg/mozilla/interfaces/nsILocalFile;",
      (void*) aFunctions[kFunc_NewLocalFile] }
  };

  JNINativeMethod proxy_methods[] = {
    { "callXPCOMMethod",
      "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;",
      (void*) aFunctions[kFunc_CallXPCOMMethod] },
    { "finalizeProxyNative", "(Ljava/lang/Object;)V",
      (void*) aFunctions[kFunc_FinalizeProxy] },
    { "isSameXPCOMObject", "(Ljava/lang/Object;Ljava/lang/Object;)Z",
      (void*) aFunctions[kFunc_IsSameXPCOMObject] }
  };

  JNINativeMethod lockProxy_methods[] = {
    { "releaseNative", "(J)V",
      (void*) aFunctions[kFunc_ReleaseProfileLock] }
  };

  JNINativeMethod util_methods[] = {
    { "wrapJavaObject", "(Ljava/lang/Object;Ljava/lang/String;)J",
      (void*) aFunctions[kFunc_WrapJavaObject] },
    { "wrapXPCOMObject", "(JLjava/lang/String;)Ljava/lang/Object;",
      (void*) aFunctions[kFunc_WrapXPCOMObject] }
  };

  jint rc = -1;
  jclass clazz = env->FindClass("org/mozilla/xpcom/internal/MozillaImpl");
  if (clazz) {
    rc = env->RegisterNatives(clazz, mozilla_methods,
                          sizeof(mozilla_methods) / sizeof(mozilla_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  rc = -1;
  clazz = env->FindClass("org/mozilla/xpcom/internal/GREImpl");
  if (clazz) {
    rc = env->RegisterNatives(clazz, gre_methods,
                              sizeof(gre_methods) / sizeof(gre_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  rc = -1;
  clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMImpl");
  if (clazz) {
    rc = env->RegisterNatives(clazz, xpcom_methods,
                              sizeof(xpcom_methods) / sizeof(xpcom_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  rc = -1;
  clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMJavaProxy");
  if (clazz) {
    rc = env->RegisterNatives(clazz, proxy_methods,
                              sizeof(proxy_methods) / sizeof(proxy_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  rc = -1;
  clazz = env->FindClass("org/mozilla/xpcom/ProfileLock");
  if (clazz) {
    rc = env->RegisterNatives(clazz, lockProxy_methods,
                      sizeof(lockProxy_methods) / sizeof(lockProxy_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  rc = -1;
  clazz = env->FindClass("org/mozilla/xpcom/internal/JavaXPCOMMethods");
  if (clazz) {
    rc = env->RegisterNatives(clazz, util_methods,
                              sizeof(util_methods) / sizeof(util_methods[0]));
  }
  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);

  return NS_OK;
}

// Load the JavaXPCOM methods from the XUL shared library, and registers them
// as Java native methods.
extern "C" JX_EXPORT void JNICALL
JXM_NATIVE(registerJavaXPCOMMethodsNative) (JNIEnv *env, jclass that,
                                            jobject aXPCOMPath)
{
  void* functions[JX_NUM_FUNCS];
  memset(functions, 0, JX_NUM_FUNCS * sizeof(void*));

  nsresult rv = LoadXULMethods(env, aXPCOMPath, functions);
  if (NS_SUCCEEDED(rv)) {
    rv = RegisterNativeMethods(env, functions);
  }

  if (NS_FAILED(rv)) {
    ThrowException(env, rv, "Failed to register JavaXPCOM methods");
  }
}