1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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.
23 * Adam Lock <adamlock@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
43 #include "_java/java_lang_Throwable.h"
44 #include "_java/java_lang_Error.h"
45 #include "_java/java_lang_String.h"
46 #include "_java/java_lang_Boolean.h"
47 #include "_java/java_lang_Number.h"
48 #include "_java/java_lang_Integer.h"
49 #include "_java/java_lang_Long.h"
51 // These things require certain native statics to be implemented
52 // so they're commented out for the time being.
53 //#include "_java/java_lang_Double.h"
54 //#include "_java/java_lang_Float.h"
55 #include "_java/java_lang_Character.h"
56 #include "_java/netscape_plugin_Plugin.h"
57 #include "_java/MozAxPlugin.h"
59 #include "LegacyPlugin.h"
60 #include "LiveConnect.h"
62 void liveconnect_Shutdown()
64 JRIEnv
* env
= NPN_GetJavaEnv();
66 unuse_MozAxPlugin(env
);
67 unuse_netscape_plugin_Plugin(env
);
68 unuse_java_lang_Error(env
);
69 // unuse_java_lang_String(env);
70 unuse_java_lang_Number(env
);
71 unuse_java_lang_Boolean(env
);
72 unuse_java_lang_Integer(env
);
73 unuse_java_lang_Long(env
);
74 // unuse_java_lang_Float(env);
75 // unuse_java_lang_Double(env);
76 unuse_java_lang_Character(env
);
80 jref
liveconnect_GetJavaClass()
82 JRIEnv
* env
= NPN_GetJavaEnv();
84 // Note: The order of these is important (for some unknown reason)
85 // and was determined through trial and error. Do not rearrange
86 // without testing the new order!
87 use_netscape_plugin_Plugin(env
);
88 jref myClass
= (jref
) use_MozAxPlugin(env
);
89 use_java_lang_Error(env
);
90 // use_java_lang_String(env);
91 use_java_lang_Number(env
);
92 use_java_lang_Boolean(env
);
93 use_java_lang_Integer(env
);
94 use_java_lang_Long(env
);
95 // use_java_lang_Float(env);
96 // use_java_lang_Double(env);
97 use_java_lang_Character(env
);
103 // The following will be callable from Javascript through LiveConnect
105 _GetIDispatchFromJRI(JRIEnv
*env
, struct MozAxPlugin
* self
, IDispatch
**pdisp
)
107 if (pdisp
== NULL
|| env
== NULL
|| self
== NULL
)
113 // Note: You get a nasty crash calling self->getPeer(env), this obscure cast fixes
114 // it. More details in the link:
116 // http://groups.google.com/groups?selm=385D9543.4087F1C6%40ermapper.com.au&output=gplain
118 NPP npp
= (NPP
) netscape_plugin_Plugin_getPeer(env
,
119 reinterpret_cast<netscape_plugin_Plugin
*> (self
));
120 PluginInstanceData
*pData
= (PluginInstanceData
*) npp
->pdata
;
127 HRESULT hr
= pData
->pControlSite
->GetControlUnknown(&unk
);
128 if (unk
.GetInterfacePtr() == NULL
)
133 IDispatchPtr disp
= unk
;
134 if (disp
.GetInterfacePtr() == NULL
)
139 *pdisp
= disp
.GetInterfacePtr();
146 _VariantToJRIObject(JRIEnv
*env
, VARIANT
*v
, java_lang_Object
**o
)
148 if (v
== NULL
|| env
== NULL
|| o
== NULL
)
155 // TODO - VT_BYREF will cause problems
156 if (v
->vt
== VT_EMPTY
)
160 else if (v
->vt
== VT_BOOL
)
162 jbool value
= (v
->boolVal
== VARIANT_TRUE
) ? JRITrue
: JRIFalse
;
163 java_lang_Boolean
*j
= java_lang_Boolean_new(env
,
164 class_java_lang_Boolean(env
), value
);
165 *o
= reinterpret_cast<java_lang_Object
*>(j
);
168 else if (v
->vt
== VT_I4
)
170 jlong value
= v
->lVal
;
171 java_lang_Long
*j
= java_lang_Long_new(env
,
172 class_java_lang_Long(env
), value
);
173 *o
= reinterpret_cast<java_lang_Object
*>(j
);
176 else if (v
->vt
== VT_I2
)
178 jlong value
= v
->iVal
;
179 java_lang_Long
*j
= java_lang_Long_new(env
,
180 class_java_lang_Long(env
), value
);
181 *o
= reinterpret_cast<java_lang_Object
*>(j
);
184 /* else if (v->vt == VT_R4)
186 jfloat value = v->fltVal;
187 java_lang_Float *j = java_lang_Float_new(env,
188 class_java_lang_Float(env), value);
189 *o = reinterpret_cast<java_lang_Object *>(j);
192 else if (v->vt == VT_R8)
194 jdouble value = v->dblVal;
195 java_lang_Double *j = java_lang_Double_new(env,
196 class_java_lang_Double(env), value);
197 *o = reinterpret_cast<java_lang_Object *>(j);
200 else if (v
->vt
== VT_BSTR
)
203 char * value
= OLE2A(v
->bstrVal
);
204 java_lang_String
*j
= JRI_NewStringUTF(env
, value
, strlen(value
));
205 *o
= reinterpret_cast<java_lang_Object
*>(j
);
208 /* TODO else if VT_UI1 etc. */
214 _JRIObjectToVariant(JRIEnv
*env
, java_lang_Object
*o
, VARIANT
*v
)
217 if (JRI_IsInstanceOf(env
, (jref
) o
, class_java_lang_String(env
)))
220 const char *value
= JRI_GetStringUTFChars(env
, reinterpret_cast<java_lang_String
*>(o
));
222 v
->bstrVal
= SysAllocString(A2COLE(value
));
224 else if (JRI_IsInstanceOf(env
, (jref
) o
, class_java_lang_Boolean(env
)))
226 jbool value
= java_lang_Boolean_booleanValue(env
, reinterpret_cast<java_lang_Boolean
*>(o
));
228 v
->boolVal
= value
== JRITrue
? VARIANT_TRUE
: VARIANT_FALSE
;
230 else if (JRI_IsInstanceOf(env
, o
, class_java_lang_Integer(env
)))
232 jint value
= java_lang_Integer_intValue(env
, reinterpret_cast<java_lang_Integer
*>(o
));
236 else if (JRI_IsInstanceOf(env
, o
, class_java_lang_Long(env
)))
238 jlong value
= java_lang_Long_longValue(env
, reinterpret_cast<java_lang_Long
*>(o
));
242 /* else if (JRI_IsInstanceOf(env, o, class_java_lang_Double(env)))
244 jdouble value = java_lang_Double_doubleValue(env, reinterpret_cast<java_lang_Double *>(o));
248 else if (JRI_IsInstanceOf(env, o, class_java_lang_Float(env)))
250 jfloat value = java_lang_Float_floatValue(env, reinterpret_cast<java_lang_Float *>(o));
254 else if (JRI_IsInstanceOf(env
, o
, class_java_lang_Character(env
)))
256 jchar value
= java_lang_Character_charValue(env
, reinterpret_cast<java_lang_Character
*>(o
));
260 else if (JRI_IsInstanceOf(env
, o
, class_java_lang_Number(env
)))
262 jlong value
= java_lang_Number_longValue(env
, reinterpret_cast<java_lang_Number
*>(o
));
268 // TODO dump diagnostic error here
274 struct java_lang_Object
*
275 _InvokeFromJRI(JRIEnv
*env
, struct MozAxPlugin
* self
, struct java_lang_String
*func
, int nargs
, java_lang_Object
*args
[])
280 // call the requested function
281 const char* funcName
= JRI_GetStringUTFChars(env
, func
);
284 if (FAILED(_GetIDispatchFromJRI(env
, self
, &disp
)))
289 _variant_t
*vargs
= new _variant_t
[nargs
];
290 for (int i
= 0; i
< nargs
; i
++)
292 if (FAILED(_JRIObjectToVariant(env
, args
[i
], &vargs
[i
])))
296 sprintf(error
, "Argument %d could not be converted into a variant", i
);
297 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
303 OLECHAR FAR
* szMember
= A2OLE(funcName
);
304 hr
= disp
->GetIDsOfNames(IID_NULL
, &szMember
, 1, LOCALE_USER_DEFAULT
, &dispid
);
308 memset(error
, 0, sizeof(error
));
309 _snprintf(error
, sizeof(error
) - 1, "invoke failed, member \"%s\" not found, hr=0x%08x", funcName
, hr
);
310 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
314 DISPPARAMS dispparams
= {NULL
, NULL
, 0, 0};
315 dispparams
.rgvarg
= vargs
;
316 dispparams
.cArgs
= nargs
;
324 &dispparams
, &vResult
, NULL
, NULL
);
329 sprintf(error
, "invoke failed, result from object = 0x%08x", hr
);
330 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
334 java_lang_Object
*oResult
= NULL
;
335 _VariantToJRIObject(env
, &vResult
, &oResult
);
337 return reinterpret_cast<java_lang_Object
*>(oResult
);
340 /*******************************************************************************
342 * These are the native methods which we are implementing.
343 ******************************************************************************/
345 /*** private native xinvoke (Ljava/lang/String;)Ljava/lang/Object; ***/
346 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
347 native_MozAxPlugin_xinvoke(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
)
349 return _InvokeFromJRI(env
, self
, a
, 0, NULL
);
352 /*** private native xinvoke1 (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; ***/
353 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
354 native_MozAxPlugin_xinvoke1(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_Object
*b
)
356 java_lang_Object
*args
[1];
358 return _InvokeFromJRI(env
, self
, a
, sizeof(args
) / sizeof(args
[0]), args
);
361 /*** private native xinvoke2 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
362 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
363 native_MozAxPlugin_xinvoke2(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_Object
*b
, struct java_lang_Object
*c
)
365 java_lang_Object
*args
[2];
368 return _InvokeFromJRI(env
, self
, a
, sizeof(args
) / sizeof(args
[0]), args
);
371 /*** private native xinvoke3 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
372 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
373 native_MozAxPlugin_xinvoke3(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_Object
*b
, struct java_lang_Object
*c
, struct java_lang_Object
*d
)
375 java_lang_Object
*args
[3];
379 return _InvokeFromJRI(env
, self
, a
, sizeof(args
) / sizeof(args
[0]), args
);
382 /*** private native xinvoke4 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
383 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
384 native_MozAxPlugin_xinvoke4(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_Object
*b
, struct java_lang_Object
*c
, struct java_lang_Object
*d
, struct java_lang_Object
*e
)
386 java_lang_Object
*args
[4];
391 return _InvokeFromJRI(env
, self
, a
, sizeof(args
) / sizeof(args
[0]), args
);
394 /*** private native xinvokeX (Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object; ***/
395 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
396 native_MozAxPlugin_xinvokeX(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, jobjectArray b
)
398 // Turn the Java array of objects into a C++ array
399 jsize length
= JRI_GetObjectArrayLength(env
, b
);
400 java_lang_Object
**args
= NULL
;
403 args
= (java_lang_Object
**) malloc(length
* sizeof(java_lang_Object
*));
404 for (long i
= 0; i
< length
; i
++)
406 args
[i
] = reinterpret_cast<java_lang_Object
*>(JRI_GetObjectArrayElement(env
, b
, i
));
409 java_lang_Object
*o
= _InvokeFromJRI(env
, self
, a
, length
, args
);
414 /*** private native xgetProperty (Ljava/lang/String;)Ljava/lang/Object; ***/
415 extern "C" JRI_PUBLIC_API(struct java_lang_Object
*)
416 native_MozAxPlugin_xgetProperty(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
)
423 if (FAILED(_GetIDispatchFromJRI(env
, self
, &disp
)))
428 // return the requested property to the Java peer
430 OLECHAR FAR
* szMember
= A2OLE(JRI_GetStringUTFChars(env
, a
));
431 hr
= disp
->GetIDsOfNames(IID_NULL
, &szMember
, 1, LOCALE_USER_DEFAULT
, &dispid
);
435 memset(error
, 0, sizeof(error
));
436 _snprintf(error
, sizeof(error
) - 1, "getProperty failed, member \"%s\" not found, hr=0x%08x", szMember
, hr
);
437 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
440 DISPPARAMS dispparamsNoArgs
= {NULL
, NULL
, 0, 0};
445 DISPATCH_PROPERTYGET
,
446 &dispparamsNoArgs
, &vResult
, NULL
, NULL
);
450 sprintf(error
, "getProperty failed, result from object = 0x%08x", hr
);
451 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
455 java_lang_Object
*oResult
= NULL
;
456 _VariantToJRIObject(env
, &vResult
, &oResult
);
461 /*** private native xsetProperty2 (Ljava/lang/String;Ljava/lang/Object;)V ***/
462 extern "C" JRI_PUBLIC_API(void)
463 native_MozAxPlugin_xsetProperty2(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_Object
*b
)
471 if (FAILED(_GetIDispatchFromJRI(env
, self
, &disp
)))
477 OLECHAR FAR
* szMember
= A2OLE(JRI_GetStringUTFChars(env
, a
));
478 hr
= disp
->GetIDsOfNames(IID_NULL
, &szMember
, 1, LOCALE_USER_DEFAULT
, &dispid
);
482 memset(error
, 0, sizeof(error
));
483 _snprintf(error
, sizeof(error
) - 1, "setProperty failed, member \"%s\" not found, hr=0x%08x", szMember
, hr
);
484 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
488 _variant_t
*pvars
= new _variant_t
[1];
489 if (FAILED(_JRIObjectToVariant(env
, b
, &pvars
[0])))
493 sprintf(error
, "Property value could not be converted into a variant");
494 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
498 DISPID dispIdPut
= DISPID_PROPERTYPUT
;
500 DISPPARAMS functionArgs
;
501 functionArgs
.rgdispidNamedArgs
= &dispIdPut
;
502 functionArgs
.rgvarg
= pvars
;
503 functionArgs
.cArgs
= 1;
504 functionArgs
.cNamedArgs
= 1;
510 DISPATCH_PROPERTYPUT
,
511 &functionArgs
, &VarResult
, NULL
, NULL
);
518 sprintf(error
, "setProperty failed, result from object = 0x%08x", hr
);
519 JRI_ThrowNew(env
, class_java_lang_Error(env
), error
);
524 /*** private native xsetProperty1 (Ljava/lang/String;Ljava/lang/String;)V ***/
525 extern "C" JRI_PUBLIC_API(void)
526 native_MozAxPlugin_xsetProperty1(JRIEnv
* env
, struct MozAxPlugin
* self
, struct java_lang_String
*a
, struct java_lang_String
*b
)
528 native_MozAxPlugin_xsetProperty2(env
, self
, a
, reinterpret_cast<java_lang_Object
*>(b
));
532 ///////////////////////////////////////////////////////////////////////////////
534 /*** private native printStackTrace0 (Ljava/io/PrintStream;)V ***/
535 extern "C" JRI_PUBLIC_API(void)
536 native_java_lang_Throwable_printStackTrace0(JRIEnv
* env
, struct java_lang_Throwable
* self
, struct java_io_PrintStream
*a
)
540 /*** public native fillInStackTrace ()Ljava/lang/Throwable; ***/
541 extern "C" JRI_PUBLIC_API(struct java_lang_Throwable
*)
542 native_java_lang_Throwable_fillInStackTrace(JRIEnv
* env
, struct java_lang_Throwable
* self
)