Back to index

lightning-sunbird  0.9+nobinonly
LiveConnect.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Adam Lock <adamlock@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "stdafx.h"
00039 
00040 #include "jni.h"
00041 #include "npapi.h"
00042 
00043 #include "_java/java_lang_Throwable.h"
00044 #include "_java/java_lang_Error.h"
00045 #include "_java/java_lang_String.h"
00046 #include "_java/java_lang_Boolean.h"
00047 #include "_java/java_lang_Number.h"
00048 #include "_java/java_lang_Integer.h"
00049 #include "_java/java_lang_Long.h"
00050 // TODO:
00051 // These things require certain native statics to be implemented
00052 // so they're commented out for the time being.
00053 //#include "_java/java_lang_Double.h"
00054 //#include "_java/java_lang_Float.h"
00055 #include "_java/java_lang_Character.h"
00056 #include "_java/netscape_plugin_Plugin.h"
00057 #include "_java/MozAxPlugin.h"
00058 
00059 #include "LegacyPlugin.h"
00060 #include "LiveConnect.h"
00061 
00062 void liveconnect_Shutdown()
00063 {
00064     JRIEnv* env = NPN_GetJavaEnv();
00065     if (env) {
00066         unuse_MozAxPlugin(env);
00067         unuse_netscape_plugin_Plugin(env);
00068         unuse_java_lang_Error(env);
00069 //        unuse_java_lang_String(env);
00070         unuse_java_lang_Number(env);
00071         unuse_java_lang_Boolean(env);
00072         unuse_java_lang_Integer(env);
00073         unuse_java_lang_Long(env);
00074 //        unuse_java_lang_Float(env);
00075 //        unuse_java_lang_Double(env);
00076         unuse_java_lang_Character(env);
00077     }
00078 }
00079 
00080 jref liveconnect_GetJavaClass()
00081 {
00082     JRIEnv* env = NPN_GetJavaEnv();
00083     if (env) {
00084         // Note: The order of these is important (for some unknown reason)
00085         //       and was determined through trial and error. Do not rearrange
00086         //       without testing the new order!
00087         use_netscape_plugin_Plugin(env);
00088         jref myClass = (jref) use_MozAxPlugin(env);
00089         use_java_lang_Error(env);
00090 //        use_java_lang_String(env);
00091         use_java_lang_Number(env);
00092         use_java_lang_Boolean(env);
00093         use_java_lang_Integer(env);
00094         use_java_lang_Long(env);
00095 //        use_java_lang_Float(env);
00096 //        use_java_lang_Double(env);
00097         use_java_lang_Character(env);
00098         return myClass;
00099     }
00100     return NULL;
00101 }
00102 
00103 // The following will be callable from Javascript through LiveConnect
00104 HRESULT
00105 _GetIDispatchFromJRI(JRIEnv *env, struct MozAxPlugin* self, IDispatch **pdisp)
00106 {
00107     if (pdisp == NULL || env == NULL || self == NULL)
00108     {
00109         return E_INVALIDARG;
00110     }
00111     *pdisp = NULL;
00112 
00113     // Note: You get a nasty crash calling self->getPeer(env), this obscure cast fixes
00114     //       it. More details in the link:
00115     //
00116     // http://groups.google.com/groups?selm=385D9543.4087F1C6%40ermapper.com.au&output=gplain
00117 
00118     NPP npp = (NPP) netscape_plugin_Plugin_getPeer(env,
00119             reinterpret_cast<netscape_plugin_Plugin*> (self));
00120     PluginInstanceData *pData = (PluginInstanceData *) npp->pdata;
00121     if (pData == NULL)
00122     { 
00123         return E_FAIL;
00124     }
00125 
00126     IUnknownPtr unk;
00127     HRESULT hr = pData->pControlSite->GetControlUnknown(&unk);
00128     if (unk.GetInterfacePtr() == NULL)
00129     {
00130         return E_FAIL; 
00131     }
00132 
00133     IDispatchPtr disp = unk;
00134     if (disp.GetInterfacePtr() == NULL)
00135     { 
00136         return E_FAIL; 
00137     }
00138 
00139     *pdisp = disp.GetInterfacePtr();
00140     (*pdisp)->AddRef();
00141 
00142     return S_OK;
00143 }
00144 
00145 HRESULT
00146 _VariantToJRIObject(JRIEnv *env, VARIANT *v, java_lang_Object **o)
00147 {
00148     if (v == NULL || env == NULL || o == NULL)
00149     {
00150         return E_INVALIDARG;
00151     }
00152 
00153     *o = NULL;
00154 
00155     // TODO - VT_BYREF will cause problems
00156     if (v->vt == VT_EMPTY)
00157     {
00158         return S_OK;
00159     }
00160     else if (v->vt == VT_BOOL)
00161     {
00162         jbool value = (v->boolVal == VARIANT_TRUE) ? JRITrue : JRIFalse;
00163         java_lang_Boolean *j = java_lang_Boolean_new(env,
00164             class_java_lang_Boolean(env), value);
00165         *o = reinterpret_cast<java_lang_Object *>(j);
00166         return S_OK;
00167     }
00168     else if (v->vt == VT_I4)
00169     {
00170         jlong value = v->lVal;
00171         java_lang_Long *j = java_lang_Long_new(env,
00172             class_java_lang_Long(env), value);
00173         *o = reinterpret_cast<java_lang_Object *>(j);
00174         return S_OK;
00175     }
00176     else if (v->vt == VT_I2)
00177     {
00178         jlong value = v->iVal;
00179         java_lang_Long *j = java_lang_Long_new(env,
00180             class_java_lang_Long(env), value);
00181         *o = reinterpret_cast<java_lang_Object *>(j);
00182         return S_OK;
00183     }
00184 /*  else if (v->vt == VT_R4)
00185     {
00186         jfloat value = v->fltVal;
00187         java_lang_Float *j = java_lang_Float_new(env,
00188             class_java_lang_Float(env), value);
00189         *o = reinterpret_cast<java_lang_Object *>(j);
00190         return S_OK;
00191     }
00192     else if (v->vt == VT_R8)
00193     {
00194         jdouble value = v->dblVal;
00195         java_lang_Double *j = java_lang_Double_new(env,
00196             class_java_lang_Double(env), value);
00197         *o = reinterpret_cast<java_lang_Object *>(j);
00198         return S_OK;
00199     } */
00200     else if (v->vt == VT_BSTR)
00201     {
00202         USES_CONVERSION;
00203         char * value = OLE2A(v->bstrVal);
00204         java_lang_String *j = JRI_NewStringUTF(env, value, strlen(value));
00205         *o = reinterpret_cast<java_lang_Object *>(j);
00206         return S_OK;
00207     }
00208     /* TODO else if VT_UI1 etc. */
00209 
00210     return E_FAIL;
00211 }
00212 
00213 HRESULT
00214 _JRIObjectToVariant(JRIEnv *env, java_lang_Object *o, VARIANT *v)
00215 {
00216     VariantInit(v);
00217     if (JRI_IsInstanceOf(env, (jref) o, class_java_lang_String(env)))
00218     {
00219         USES_CONVERSION;
00220         const char *value = JRI_GetStringUTFChars(env, reinterpret_cast<java_lang_String *>(o));
00221         v->vt = VT_BSTR;
00222         v->bstrVal = SysAllocString(A2COLE(value));
00223     }
00224     else if (JRI_IsInstanceOf(env, (jref) o, class_java_lang_Boolean(env)))
00225     {
00226         jbool value = java_lang_Boolean_booleanValue(env, reinterpret_cast<java_lang_Boolean *>(o));
00227         v->vt = VT_BOOL;
00228         v->boolVal = value == JRITrue ? VARIANT_TRUE : VARIANT_FALSE;
00229     }
00230     else if (JRI_IsInstanceOf(env, o, class_java_lang_Integer(env)))
00231     {
00232         jint value = java_lang_Integer_intValue(env, reinterpret_cast<java_lang_Integer *>(o));
00233         v->vt = VT_I4;
00234         v->lVal = value;
00235     }
00236     else if (JRI_IsInstanceOf(env, o, class_java_lang_Long(env)))
00237     {
00238         jlong value = java_lang_Long_longValue(env, reinterpret_cast<java_lang_Long *>(o));
00239         v->vt = VT_I4;
00240         v->lVal = value;
00241     }
00242 /*  else if (JRI_IsInstanceOf(env, o, class_java_lang_Double(env)))
00243     {
00244         jdouble  value = java_lang_Double_doubleValue(env, reinterpret_cast<java_lang_Double *>(o));
00245         v->vt = VT_R8;
00246         v->dblVal = value;
00247     }
00248     else if (JRI_IsInstanceOf(env, o, class_java_lang_Float(env)))
00249     {
00250         jfloat value = java_lang_Float_floatValue(env, reinterpret_cast<java_lang_Float *>(o));
00251         v->vt = VT_R4;
00252         v->fltVal = value;
00253     } */
00254     else if (JRI_IsInstanceOf(env, o, class_java_lang_Character(env)))
00255     {
00256         jchar value = java_lang_Character_charValue(env, reinterpret_cast<java_lang_Character *>(o));
00257         v->vt = VT_UI1;
00258         v->bVal = value;
00259     }
00260     else if (JRI_IsInstanceOf(env, o, class_java_lang_Number(env)))
00261     {
00262         jlong value = java_lang_Number_longValue(env, reinterpret_cast<java_lang_Number *>(o));
00263         v->vt = VT_I4;
00264         v->lVal = value;
00265     }
00266     else
00267     {
00268         // TODO dump diagnostic error here
00269         return E_FAIL;
00270     }
00271     return S_OK;
00272 }
00273 
00274 struct java_lang_Object *
00275 _InvokeFromJRI(JRIEnv *env, struct MozAxPlugin* self, struct java_lang_String *func, int nargs, java_lang_Object *args[])
00276 {
00277     HRESULT hr;
00278     DISPID dispid = 0;
00279     
00280     // call the requested function
00281     const char* funcName = JRI_GetStringUTFChars(env, func);
00282 
00283     IDispatchPtr disp;
00284     if (FAILED(_GetIDispatchFromJRI(env, self, &disp)))
00285     {
00286         return NULL;
00287     }
00288 
00289     _variant_t *vargs = new _variant_t[nargs];
00290     for (int i = 0; i < nargs; i++)
00291     {
00292         if (FAILED(_JRIObjectToVariant(env, args[i], &vargs[i])))
00293         {
00294             delete []vargs;
00295             char error[64];
00296             sprintf(error, "Argument %d could not be converted into a variant", i);
00297             JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00298             return NULL;
00299         }
00300     }
00301 
00302     USES_CONVERSION;
00303     OLECHAR FAR* szMember = A2OLE(funcName);
00304     hr = disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid);
00305     if (FAILED(hr))
00306     { 
00307         char error[128];
00308         memset(error, 0, sizeof(error));
00309         _snprintf(error, sizeof(error) - 1, "invoke failed, member \"%s\" not found, hr=0x%08x", funcName, hr);
00310         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00311         return NULL; 
00312     }
00313 
00314     DISPPARAMS dispparams = {NULL, NULL, 0, 0};
00315     dispparams.rgvarg = vargs;
00316     dispparams.cArgs = nargs;
00317 
00318     _variant_t vResult;
00319     hr = disp->Invoke(
00320         dispid,
00321         IID_NULL,
00322         LOCALE_USER_DEFAULT,
00323         DISPATCH_METHOD,
00324         &dispparams, &vResult, NULL, NULL);
00325     
00326     if (FAILED(hr))
00327     { 
00328         char error[64];
00329         sprintf(error, "invoke failed, result from object = 0x%08x", hr);
00330         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00331         return NULL; 
00332     }
00333 
00334     java_lang_Object *oResult = NULL;
00335     _VariantToJRIObject(env, &vResult, &oResult);
00336 
00337     return reinterpret_cast<java_lang_Object *>(oResult);
00338 }
00339 
00340 /*******************************************************************************
00341  * Native Methods: 
00342  * These are the native methods which we are implementing.
00343  ******************************************************************************/
00344 
00345 /*** private native xinvoke (Ljava/lang/String;)Ljava/lang/Object; ***/
00346 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00347 native_MozAxPlugin_xinvoke(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a)
00348 {
00349     return _InvokeFromJRI(env, self, a, 0, NULL);
00350 }
00351 
00352 /*** private native xinvoke1 (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; ***/
00353 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00354 native_MozAxPlugin_xinvoke1(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a, struct java_lang_Object *b)
00355 {
00356     java_lang_Object *args[1];
00357     args[0] = b;
00358     return _InvokeFromJRI(env, self, a, sizeof(args) / sizeof(args[0]), args);
00359 }
00360 
00361 /*** private native xinvoke2 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
00362 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00363 native_MozAxPlugin_xinvoke2(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a, struct java_lang_Object *b, struct java_lang_Object *c)
00364 {
00365     java_lang_Object *args[2];
00366     args[0] = b;
00367     args[1] = c;
00368     return _InvokeFromJRI(env, self, a, sizeof(args) / sizeof(args[0]), args);
00369 }
00370 
00371 /*** private native xinvoke3 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
00372 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00373 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)
00374 {
00375     java_lang_Object *args[3];
00376     args[0] = b;
00377     args[1] = c;
00378     args[2] = d;
00379     return _InvokeFromJRI(env, self, a, sizeof(args) / sizeof(args[0]), args);
00380 }
00381 
00382 /*** private native xinvoke4 (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; ***/
00383 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00384 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)
00385 {
00386     java_lang_Object *args[4];
00387     args[0] = b;
00388     args[1] = c;
00389     args[2] = d;
00390     args[3] = e;
00391     return _InvokeFromJRI(env, self, a, sizeof(args) / sizeof(args[0]), args);
00392 }
00393 
00394 /*** private native xinvokeX (Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object; ***/
00395 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00396 native_MozAxPlugin_xinvokeX(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a, jobjectArray b)
00397 {
00398     // Turn the Java array of objects into a C++ array
00399     jsize length = JRI_GetObjectArrayLength(env, b);
00400     java_lang_Object **args = NULL;
00401     if (length)
00402     {
00403         args = (java_lang_Object **) malloc(length * sizeof(java_lang_Object *));
00404         for (long i = 0; i < length; i++)
00405         {
00406              args[i] = reinterpret_cast<java_lang_Object *>(JRI_GetObjectArrayElement(env, b, i));
00407         }
00408     }
00409     java_lang_Object *o = _InvokeFromJRI(env, self, a, length, args);
00410     free(args);
00411     return o;
00412 }
00413 
00414 /*** private native xgetProperty (Ljava/lang/String;)Ljava/lang/Object; ***/
00415 extern "C" JRI_PUBLIC_API(struct java_lang_Object *)
00416 native_MozAxPlugin_xgetProperty(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a)
00417 {
00418     HRESULT hr;
00419     DISPID dispid;
00420     _variant_t vResult;
00421 
00422     IDispatchPtr disp;
00423     if (FAILED(_GetIDispatchFromJRI(env, self, &disp)))
00424     {
00425         return NULL;
00426     }
00427 
00428     // return the requested property to the Java peer
00429     USES_CONVERSION;
00430     OLECHAR FAR* szMember = A2OLE(JRI_GetStringUTFChars(env, a));
00431     hr = disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid);
00432     if (FAILED(hr))
00433     { 
00434         char error[128];
00435         memset(error, 0, sizeof(error));
00436         _snprintf(error, sizeof(error) - 1, "getProperty failed, member \"%s\" not found, hr=0x%08x", szMember, hr);
00437         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00438         return NULL; 
00439     }
00440     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
00441     hr = disp->Invoke(
00442         dispid,
00443         IID_NULL,
00444         LOCALE_USER_DEFAULT,
00445         DISPATCH_PROPERTYGET,
00446         &dispparamsNoArgs, &vResult, NULL, NULL);
00447     if (FAILED(hr))
00448     { 
00449         char error[64];
00450         sprintf(error, "getProperty failed, result from object = 0x%08x", hr);
00451         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00452         return NULL; 
00453     }
00454 
00455     java_lang_Object *oResult = NULL;
00456     _VariantToJRIObject(env, &vResult, &oResult);
00457 
00458     return oResult;
00459 }
00460 
00461 /*** private native xsetProperty2 (Ljava/lang/String;Ljava/lang/Object;)V ***/
00462 extern "C" JRI_PUBLIC_API(void)
00463 native_MozAxPlugin_xsetProperty2(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a, struct java_lang_Object *b)
00464 
00465 {
00466     HRESULT hr;
00467     DISPID dispid;
00468     VARIANT VarResult;
00469 
00470     IDispatchPtr disp;
00471     if (FAILED(_GetIDispatchFromJRI(env, self, &disp)))
00472     {
00473         return;
00474     }
00475 
00476     USES_CONVERSION;
00477     OLECHAR FAR* szMember = A2OLE(JRI_GetStringUTFChars(env, a));
00478     hr = disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid);
00479     if (FAILED(hr))
00480     { 
00481         char error[128];
00482         memset(error, 0, sizeof(error));
00483         _snprintf(error, sizeof(error) - 1, "setProperty failed, member \"%s\" not found, hr=0x%08x", szMember, hr);
00484         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00485         return;
00486     }
00487 
00488     _variant_t *pvars = new _variant_t[1];
00489     if (FAILED(_JRIObjectToVariant(env, b, &pvars[0])))
00490     {
00491         delete []pvars;
00492         char error[64];
00493         sprintf(error, "Property value could not be converted into a variant");
00494         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00495         return;
00496     }
00497 
00498     DISPID dispIdPut = DISPID_PROPERTYPUT;
00499 
00500     DISPPARAMS functionArgs;
00501     functionArgs.rgdispidNamedArgs = &dispIdPut;
00502     functionArgs.rgvarg = pvars;
00503     functionArgs.cArgs = 1;
00504     functionArgs.cNamedArgs = 1;
00505 
00506     hr = disp->Invoke(
00507         dispid,
00508         IID_NULL,
00509         LOCALE_USER_DEFAULT,
00510         DISPATCH_PROPERTYPUT,
00511         &functionArgs, &VarResult, NULL, NULL);
00512 
00513     delete []pvars;
00514     
00515     if (FAILED(hr))
00516     {
00517         char error[64];
00518         sprintf(error, "setProperty failed, result from object = 0x%08x", hr);
00519         JRI_ThrowNew(env, class_java_lang_Error(env), error); 
00520         return;
00521     }
00522 }
00523 
00524 /*** private native xsetProperty1 (Ljava/lang/String;Ljava/lang/String;)V ***/
00525 extern "C" JRI_PUBLIC_API(void)
00526 native_MozAxPlugin_xsetProperty1(JRIEnv* env, struct MozAxPlugin* self, struct java_lang_String *a, struct java_lang_String *b)
00527 {
00528     native_MozAxPlugin_xsetProperty2(env, self, a, reinterpret_cast<java_lang_Object *>(b));
00529 }
00530 
00531 
00533 
00534 /*** private native printStackTrace0 (Ljava/io/PrintStream;)V ***/
00535 extern "C" JRI_PUBLIC_API(void)
00536 native_java_lang_Throwable_printStackTrace0(JRIEnv* env, struct java_lang_Throwable* self, struct java_io_PrintStream *a)
00537 {
00538 }
00539 
00540 /*** public native fillInStackTrace ()Ljava/lang/Throwable; ***/
00541 extern "C" JRI_PUBLIC_API(struct java_lang_Throwable *)
00542 native_java_lang_Throwable_fillInStackTrace(JRIEnv* env, struct java_lang_Throwable* self)
00543 {
00544     return self;
00545 }