/* gcjwebplugin - Webbrowser plugin to execute Java (tm) applets. Copyright (C) 2003 Michael Koch This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include "GCJPluginFactory.h" #include "GCJPluginInstance.h" #include "GCJSecureEnv.h" #define DEBUG(output) printf (output); #define DEBUG2(output, arg) printf (output, arg); #define PLUGIN_NAME "GCJ Java(tm) OJI plugin" #define PLUGIN_DESC "Java(tm) plugin for Mozilla and compatible browsers" #define PLUGIN_MIME_TYPE "application/x-java-vm" #define PLUGIN_MIME_DESC "application/x-java-vm::GCJ Java OJI Plugin" //#define PLUGIN_MIME_TYPE "application/x-java-applet" //#define PLUGIN_MIME_DESC "application/x-java-applet::GCJ Java OJI Plugin" #define PLUGIN_FILE_EXTS "class, jar" #define PLUGIN_MIME_COUNT 1 #define PLUGIN_URL NS_INLINE_PLUGIN_CONTRACTID_PREFIX NS_JVM_MIME_TYPE static const char plugin_name[] = PLUGIN_NAME; static const char plugin_desc[] = PLUGIN_DESC; static const char* plugin_mime_type[] = { PLUGIN_MIME_TYPE }; static const char* plugin_mime_desc[] = { PLUGIN_MIME_DESC }; static const char* plugin_file_exts[] = { PLUGIN_FILE_EXTS }; static const PRInt32 plugin_mime_count = PLUGIN_MIME_COUNT; static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID); static NS_DEFINE_IID(kIPluginIID, NS_IPLUGIN_IID); static NS_DEFINE_IID(kIJVMPluginIID, NS_IJVMPLUGIN_IID); static NS_DEFINE_IID(kIJVMConsoleIID, NS_IJVMCONSOLE_IID); static NS_DEFINE_CID (kPluginManagerCID, NS_PLUGINMANAGER_CID); static NS_DEFINE_CID (kGCJPluginFactoryCID, NS_OJIPLUGIN_CID); static nsModuleComponentInfo pluginData[] = { { PLUGIN_NAME, NS_OJIPLUGIN_CID, PLUGIN_URL, GCJPluginFactory::CreateOJIPlugin, GCJPluginFactory::RegisterSelf, GCJPluginFactory::UnregisterSelf } }; NS_IMPL_NSGETMODULE("GCJPluginFactory", pluginData) JavaVM* GCJPluginFactory::s_jvm; JNIEnv* GCJPluginFactory::s_jniEnv; int GCJPluginFactory::s_started = 0; std::string GCJPluginFactory::s_classpath; GCJPluginFactory::GCJPluginFactory () { DEBUG ("GCJPluginFactory::GCJPluginFactory\n"); /* classpath = ":.:"; classpath += getenv ("CLASSPATH"); classpath += ":"; */ } GCJPluginFactory::~GCJPluginFactory () { DEBUG ("GCJPluginFactory::~FactoryGCJPluginFactory\n"); } NS_IMPL_ISUPPORTS1 (GCJPluginFactory, nsIPlugin) NS_IMETHODIMP GCJPluginFactory::RegisterSelf (nsIComponentManager* aCompMgr, nsIFile* aPath, const char* aRegistryLocation, const char* aComponentType, const nsModuleComponentInfo *info) { DEBUG ("GCJPluginFactory::RegisterSelf\n"); nsIPluginManager* pm; nsresult rv = nsServiceManager::GetService (kPluginManagerCID, NS_GET_IID (nsIPluginManager), NS_REINTERPRET_CAST (nsISupports**, &pm)); if (NS_SUCCEEDED (rv)) { rv = pm->RegisterPlugin (kGCJPluginFactoryCID, plugin_name, plugin_desc, plugin_mime_type, plugin_mime_desc, plugin_file_exts, plugin_mime_count); NS_RELEASE (pm); } return rv; } NS_IMETHODIMP GCJPluginFactory::UnregisterSelf (nsIComponentManager* aCompMgr, nsIFile* aPath, const char* aRegistryLocation, const nsModuleComponentInfo *info) { DEBUG ("GCJPluginFactory::UnregisterSelf\n"); nsIPluginManager* pm; nsresult rv = nsServiceManager::GetService (kPluginManagerCID, NS_GET_IID (nsIPluginManager), NS_REINTERPRET_CAST (nsISupports**, &pm)); if (NS_SUCCEEDED (rv)) { rv = pm->UnregisterPlugin (kGCJPluginFactoryCID); NS_RELEASE (pm); } return rv; } NS_IMETHODIMP GCJPluginFactory::CreateOJIPlugin (nsISupports* aOuter, REFNSIID aIID, void** aResult) { DEBUG ("GCJPluginFactory::CreateOJIPlugin\n"); GCJPluginFactory* plugin = new GCJPluginFactory (); return plugin->CreatePluginInstance (aOuter, aIID, NULL, aResult); } NS_IMETHODIMP GCJPluginFactory::CreatePluginInstance (nsISupports* aOuter, REFNSIID aIID, const char* /*pluginMIMEType*/, void** result) { DEBUG ("GCJPluginFactory::CreatePluginInstance\n"); StartVM (); return CreateInstance (aOuter, aIID, result); } NS_IMETHODIMP GCJPluginFactory::CreateInstance (nsISupports* aOuter, REFNSIID aIID, void** result) { DEBUG ("GCJPluginFactory::CreateInstance\n"); GCJPluginInstance* pluginInstance = new GCJPluginInstance (this, s_jniEnv); if (pluginInstance == NULL) return NS_ERROR_OUT_OF_MEMORY; (*result) = pluginInstance; return NS_OK; } NS_IMETHODIMP GCJPluginFactory::LockFactory (PRBool aLock) { DEBUG ("GCJPluginFactory::LockFactory\n"); //return NS_ERROR_NOT_IMPLEMENTED; return NS_OK; } NS_IMETHODIMP GCJPluginFactory::Initialize () { DEBUG ("GCJPluginFactory::Inititalize\n"); StartVM (); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::Shutdown () { DEBUG ("GCJPluginFactory::Shutdown\n"); StopVM (); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::GetMIMEDescription (const char** result) { //DEBUG ("GCJPluginFactory::GetMIMEDescription\n"); (*result) = PLUGIN_MIME_DESC; return NS_OK; } NS_IMETHODIMP GCJPluginFactory::GetValue (nsPluginVariable variable, void* value) { //DEBUG ("GCJPluginFactory::GetValue\n"); if (variable == nsPluginVariable_NameString) (*((char**) value)) = PLUGIN_NAME; else if (variable == nsPluginVariable_DescriptionString) (*((char**) value)) = PLUGIN_MIME_DESC; else { DEBUG ("GCJPluginFactory::GetValue: unkown value requested\n"); return NS_ERROR_FAILURE; } return NS_OK; } /* NS_IMETHODIMP GCJPluginFactory::Show () { DEBUG ("GCJPluginFactory::Show\n"); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::Hide () { DEBUG ("GCJPluginFactory::Hide\n"); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::IsVisible (PRBool *result) { DEBUG ("GCJPluginFactory::IsVisible\n"); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::Print (const char* msg, const char* encodingName) { DEBUG ("GCJPluginFactory::Print\n"); return NS_OK; } */ NS_IMETHODIMP GCJPluginFactory::AddToClassPath (const char* dirPath) { DEBUG ("GCJPluginFactory::AddToClassPath\n"); s_classpath += std::string (dirPath) + ":"; return NS_OK; } NS_IMETHODIMP GCJPluginFactory::RemoveFromClassPath (const char* dirPath) { DEBUG ("GCJPluginFactory::RemoveFromClasspath\n"); //s_classpath.replace (":" + std::string (dirPath)+ ":", ":"); //return NS_OK; return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP GCJPluginFactory::GetClassPath (const char** result) { DEBUG ("GCJPluginFactory::GetClasspath\n"); (*result) = strdup (s_classpath.c_str ()); return NS_OK; } NS_IMETHODIMP GCJPluginFactory::GetJavaWrapper (JNIEnv* env, jint obj, jobject* jobj) { DEBUG ("GCJPluginFactory::GetJavaWrapper\n"); return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP GCJPluginFactory::CreateSecureEnv (JNIEnv* proxyEnv, nsISecureEnv** result) { DEBUG ("GCJPluginFactory::CreateSecureEnv\n"); if (!s_started) return NS_ERROR_FAILURE; //(*result) = new GCJSecureEnv (jvm, proxyEnv, jniEnv); //return NS_OK; return NS_ERROR_FAILURE; } NS_IMETHODIMP GCJPluginFactory::SpendTime (PRUint32 timeMillis) { DEBUG ("GCJPluginFactory::SpendTime"); // FIXME //return NS_ERROR_NOT_IMPLEMENTED; return NS_OK; } NS_IMETHODIMP GCJPluginFactory::UnwrapJavaWrapper (JNIEnv* jenv, jobject jobj, jint* obj) { DEBUG ("GCJPluginFactory::UnwrapJavaWrapper\n"); return NS_ERROR_NOT_IMPLEMENTED; } void GCJPluginFactory::StartVM () { DEBUG ("GCJPluginFactory::StartVM\n"); if (JNI_CreateJavaVM (&s_jvm, (void**) &s_jniEnv, NULL) != 0) { s_started = 0; DEBUG ("JVM not started\n"); } else { s_started = 1; DEBUG ("JVM successfully started\n"); } } void GCJPluginFactory::StopVM () { DEBUG ("GCJPluginFactory::StopVM\n"); s_jvm->DestroyJavaVM (); s_started = 0; DEBUG ("JVM successfully stopped\n"); }