From ed7f7aa45de6faa68607578010f6e256b1e888a2 Mon Sep 17 00:00:00 2001 From: "pete.zha%sun.com" Date: Thu, 19 Jun 2003 08:00:35 +0000 Subject: [PATCH] bug=185000 r=timeless@bemail.org sr=alecf@flett.org Make Integration of mozilla and Java Plugin be better --- modules/libpref/src/unix/unix.js | 5 + modules/oji/public/Makefile.in | 1 + modules/oji/public/nsIJVMConfigManager.idl | 80 +++ modules/oji/src/Makefile.in | 7 + modules/oji/src/nsCJVMManagerFactory.cpp | 15 + modules/oji/src/nsJVMConfigManager.cpp | 119 ++++ modules/oji/src/nsJVMConfigManager.h | 68 ++ modules/oji/src/nsJVMConfigManagerUnix.cpp | 663 ++++++++++++++++++ modules/oji/src/nsJVMConfigManagerUnix.h | 148 ++++ .../resources/content/pref-advanced.xul | 185 ++++- 10 files changed, 1290 insertions(+), 1 deletion(-) create mode 100644 modules/oji/public/nsIJVMConfigManager.idl create mode 100644 modules/oji/src/nsJVMConfigManager.cpp create mode 100644 modules/oji/src/nsJVMConfigManager.h create mode 100644 modules/oji/src/nsJVMConfigManagerUnix.cpp create mode 100644 modules/oji/src/nsJVMConfigManagerUnix.h diff --git a/modules/libpref/src/unix/unix.js b/modules/libpref/src/unix/unix.js index 2bfa78dcf47..2b6f23e87e4 100644 --- a/modules/libpref/src/unix/unix.js +++ b/modules/libpref/src/unix/unix.js @@ -98,6 +98,11 @@ pref("helpers.global_mime_types_file", "/etc/mime.types"); pref("helpers.global_mailcap_file", "/etc/mailcap"); pref("helpers.private_mime_types_file", "~/.mime.types"); pref("helpers.private_mailcap_file", "~/.mailcap"); +pref("java.global_java_version_file", "/etc/.java/versions"); +pref("java.private_java_version_file", "~/.java/versions"); +pref("java.default_java_location_solaris", "/usr/j2se"); +pref("java.default_java_location_others", "/usr/java"); +pref("java.java_plugin_library_name", "javaplugin_oji"); pref("applications.telnet", "xterm -e telnet %h %p"); pref("applications.tn3270", "xterm -e tn3270 %h"); pref("applications.rlogin", "xterm -e rlogin %h"); diff --git a/modules/oji/public/Makefile.in b/modules/oji/public/Makefile.in index 516b133aab6..24789ce60cf 100644 --- a/modules/oji/public/Makefile.in +++ b/modules/oji/public/Makefile.in @@ -33,6 +33,7 @@ XPIDLSRCS = \ nsIJVMManager.idl \ nsIJVMPluginInstance.idl \ nsIJVMAuthTools.idl \ + nsIJVMConfigManager.idl \ $(NULL) EXPORTS = \ diff --git a/modules/oji/public/nsIJVMConfigManager.idl b/modules/oji/public/nsIJVMConfigManager.idl new file mode 100644 index 00000000000..4a3955862aa --- /dev/null +++ b/modules/oji/public/nsIJVMConfigManager.idl @@ -0,0 +1,80 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Sun Microsystems. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pete Zha (original author) + * + * 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 "nsIArray.idl" +#include "nsIFile.idl" + +%{C++ +#define NS_JVMCONFIGMANAGER_CID \ +{ /* 4a68cee9-6f07-4950-b441-a1ce6a082e2f */ \ + 0x4a68cee9, \ + 0x6f07, \ + 0x4950, \ + {0xb4, 0x41, 0xa1, 0xce, 0x6a, 0x08, 0x2e, 0x2f} \ +} +%} +/** + * This interface contains information for the Java installation. + */ +[scriptable, uuid(3e333e20-b190-42d8-b993-d5fa435e46c4)] +interface nsIJVMConfig : nsISupports { + readonly attribute AString version; + readonly attribute AString type; + readonly attribute AString OS; + readonly attribute AString arch; + readonly attribute nsIFile path; + readonly attribute nsIFile mozillaPluginPath; + readonly attribute AString description; +}; + +/** + * This interface is a manager that can get information about Java + * installations. + */ +[scriptable, uuid(ca29fff1-a677-493c-9d80-3dc60432212b)] +interface nsIJVMConfigManager : nsISupports { + /** + * This function returns a list of existing Java installations. + */ + nsIArray getJVMConfigList(); + + /** + * This function tells the browser to use a specific Java installation. + */ + void setCurrentJVMConfig(in nsIJVMConfig jvmConfig); +}; + diff --git a/modules/oji/src/Makefile.in b/modules/oji/src/Makefile.in index 286d0ab2e0f..b9d852bf81c 100644 --- a/modules/oji/src/Makefile.in +++ b/modules/oji/src/Makefile.in @@ -70,10 +70,17 @@ CPPSRCS = \ ProxyClassLoader.cpp \ nsCSecurityContext.cpp \ nsCJVMManagerFactory.cpp \ + nsJVMConfigManager.cpp \ lcglue.cpp \ nsJVMAuthTools.cpp \ $(NULL) +ifeq ($(TARGET_MD_ARCH), unix) +CPPSRCS += \ + nsJVMConfigManagerUnix.cpp \ + $(NULL) +endif + EXPORTS = \ jvmmgr.h \ nsJVMManager.h \ diff --git a/modules/oji/src/nsCJVMManagerFactory.cpp b/modules/oji/src/nsCJVMManagerFactory.cpp index 7b01204734d..f6293ba6c3f 100644 --- a/modules/oji/src/nsCJVMManagerFactory.cpp +++ b/modules/oji/src/nsCJVMManagerFactory.cpp @@ -40,12 +40,20 @@ #include "nsIGenericFactory.h" #include "nsJVMAuthTools.h" #include "nsJVMManager.h" +#include "nsJVMConfigManager.h" + +#ifdef XP_UNIX +#include "nsJVMConfigManagerUnix.h" +#endif /* * Note: In revision 1.17 of this file (and earlier) there was a * commented out implementation of nsCJVMManagerFactory, a hand-crafted * implementation of nsIFactory. */ +#ifdef XP_UNIX +NS_GENERIC_FACTORY_CONSTRUCTOR(nsJVMConfigManagerUnix) +#endif // The list of components we register static const nsModuleComponentInfo components[] = @@ -61,6 +69,13 @@ static const nsModuleComponentInfo components[] = "@mozilla.org/oji/jvm-auth-tools;1", nsJVMAuthTools::Create }, +#ifdef XP_UNIX + { "JVM Config Manager", + NS_JVMCONFIGMANAGER_CID, + "@mozilla.org/oji/jvm-config-mgr;1", + nsJVMConfigManagerUnixConstructor + }, +#endif }; NS_IMPL_NSGETMODULE(nsCJVMManagerModule, components); diff --git a/modules/oji/src/nsJVMConfigManager.cpp b/modules/oji/src/nsJVMConfigManager.cpp new file mode 100644 index 00000000000..0e8fc4c8bbf --- /dev/null +++ b/modules/oji/src/nsJVMConfigManager.cpp @@ -0,0 +1,119 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Sun Microsystems. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pete Zha (original author) + * + * 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 "nsJVMConfigManager.h" + +NS_IMPL_ISUPPORTS1(nsJVMConfig, nsIJVMConfig) + +nsJVMConfig::nsJVMConfig(const nsAString& aVersion, const nsAString& aType, + const nsAString& aOS, const nsAString& aArch, + nsIFile* aPath, nsIFile* aMozillaPluginPath, + const nsAString& aDescription) : +mVersion(aVersion), +mType(aType), +mOS(aOS), +mArch(aArch), +mPath(aPath), +mMozillaPluginPath(aMozillaPluginPath), +mDescription(aDescription) +{ +} + +nsJVMConfig::~nsJVMConfig() +{ +} + +/* readonly attribute AString version; */ +NS_IMETHODIMP +nsJVMConfig::GetVersion(nsAString & aVersion) +{ + aVersion = mVersion; + return NS_OK; +} + +/* readonly attribute AString type; */ +NS_IMETHODIMP +nsJVMConfig::GetType(nsAString & aType) +{ + aType = mType; + return NS_OK; +} + +/* readonly attribute AString os; */ +NS_IMETHODIMP +nsJVMConfig::GetOS(nsAString & aOS) +{ + aOS = mOS; + return NS_OK; +} + +/* readonly attribute AString arch; */ +NS_IMETHODIMP +nsJVMConfig::GetArch(nsAString & aArch) +{ + aArch = mArch; + return NS_OK; +} + +NS_IMETHODIMP +nsJVMConfig::GetPath(nsIFile** aPath) +{ + NS_ENSURE_ARG_POINTER(aPath); + + *aPath = mPath; + NS_IF_ADDREF(*aPath); + return NS_OK; +} + +NS_IMETHODIMP +nsJVMConfig::GetMozillaPluginPath(nsIFile** aMozillaPluginPath) +{ + NS_ENSURE_ARG_POINTER(aMozillaPluginPath); + + *aMozillaPluginPath = mMozillaPluginPath; + NS_IF_ADDREF(*aMozillaPluginPath); + return NS_OK; +} + +/* readonly attribute AString description; */ +NS_IMETHODIMP +nsJVMConfig::GetDescription(nsAString & aDescription) +{ + aDescription = mDescription; + return NS_OK; +} + diff --git a/modules/oji/src/nsJVMConfigManager.h b/modules/oji/src/nsJVMConfigManager.h new file mode 100644 index 00000000000..ecdac3f9927 --- /dev/null +++ b/modules/oji/src/nsJVMConfigManager.h @@ -0,0 +1,68 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Sun Microsystems. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pete Zha (original author) + * + * 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 ***** */ + +#ifndef nsJVMConfigManager_h___ +#define nsJVMConfigManager_h___ + +#include "nsIJVMConfigManager.h" +#include "nsString.h" +#include "nsIFile.h" + +class nsJVMConfig : public nsIJVMConfig +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIJVMCONFIG + + nsJVMConfig(const nsAString& aVersion, const nsAString& aType, + const nsAString& aOS, const nsAString& aArch, + nsIFile* aPath, nsIFile* aMozillaPluginPath, + const nsAString& aDescription); + + virtual ~nsJVMConfig(); + /* additional members */ +protected: + nsString mVersion; + nsString mType; + nsString mOS; + nsString mArch; + nsCOMPtr mPath; + nsCOMPtr mMozillaPluginPath; + nsString mDescription; +}; + +#endif // nsJVMConfigManager_h___ diff --git a/modules/oji/src/nsJVMConfigManagerUnix.cpp b/modules/oji/src/nsJVMConfigManagerUnix.cpp new file mode 100644 index 00000000000..5ed988924da --- /dev/null +++ b/modules/oji/src/nsJVMConfigManagerUnix.cpp @@ -0,0 +1,663 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Sun Microsystems. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pete Zha (original author) + * + * 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 +#include "nsJVMConfigManagerUnix.h" +#include "nsAppDirectoryServiceDefs.h" +#include "nsDirectoryServiceDefs.h" +#include "nsIPrefService.h" +#include "nsIPrefBranch.h" +#include "nsReadableUtils.h" +#include "prprf.h" +#include "nsNetCID.h" +#include "nsIHttpProtocolHandler.h" +#include "nsIVariant.h" +#include "nsISimpleEnumerator.h" +#include /* OSF/1 requires this before grp.h, so put it first */ +#include + +#define NS_COMPILER_GNUC3 defined(__GXX_ABI_VERSION) && \ + (__GXX_ABI_VERSION >= 102) /* G++ V3 ABI */ + +static NS_DEFINE_CID(kHttpHandlerCID, NS_HTTPPROTOCOLHANDLER_CID); + +// Implementation of nsJVMConfigManagerUnix +NS_IMPL_ISUPPORTS1(nsJVMConfigManagerUnix, nsIJVMConfigManager) + +nsJVMConfigManagerUnix::nsJVMConfigManagerUnix() +{ + InitJVMConfigList(); +} + +PR_STATIC_CALLBACK(PRBool) +FreeJVMConfig(nsHashKey *aKey, void *aData, void* aClosure) +{ + nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, aData); + + NS_IF_RELEASE(config); + + return PR_TRUE; +} + +PR_STATIC_CALLBACK(PRBool) +AppendJVMConfig(nsHashKey *aKey, void *aData, void* aClosure) +{ + nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, aData); + nsIMutableArray *array = NS_STATIC_CAST(nsIMutableArray *, aClosure); + NS_ENSURE_TRUE(config && array, PR_FALSE); + + array->AppendElement(config, PR_FALSE); + return PR_TRUE; +} + +nsJVMConfigManagerUnix::~nsJVMConfigManagerUnix() +{ + ClearJVMConfigList(); +} + +void +nsJVMConfigManagerUnix::ClearJVMConfigList() +{ + if (mJVMConfigList.Count() > 0) { + mJVMConfigList.Reset(FreeJVMConfig); + } +} + +NS_IMETHODIMP +nsJVMConfigManagerUnix::GetJVMConfigList(nsIArray **_retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + + ClearJVMConfigList(); + InitJVMConfigList(); + + nsCOMPtr array; + nsresult rv = NS_NewArray(getter_AddRefs(array)); + NS_ENSURE_SUCCESS(rv, rv); + + if (mJVMConfigList.Count() > 0) { + mJVMConfigList.Enumerate(AppendJVMConfig, + NS_STATIC_CAST(void *, array)); + *_retval = NS_STATIC_CAST(nsIArray *, array); + NS_IF_ADDREF(*_retval); + } else + *_retval = nsnull; + + return NS_OK; +} + +NS_IMETHODIMP +nsJVMConfigManagerUnix::InitJVMConfigList() +{ + nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); + NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE); + + nsCOMPtr globalFile; + prefs->GetComplexValue("java.global_java_version_file", + NS_GET_IID(nsILocalFile), + getter_AddRefs(globalFile)); + + nsCOMPtr privateFile; + prefs->GetComplexValue("java.private_java_version_file", + NS_GET_IID(nsILocalFile), + getter_AddRefs(privateFile)); + + nsCOMPtr globalStream; + nsresult rv = GetLineInputStream(globalFile, getter_AddRefs(globalStream)); + NS_ENSURE_TRUE(NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_NOT_FOUND, rv); + + nsCOMPtr privateStream; + rv = GetLineInputStream(privateFile, getter_AddRefs(privateStream)); + NS_ENSURE_TRUE(NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_NOT_FOUND, rv); + + rv = InitJVMConfigList(globalStream, privateStream); + NS_ENSURE_SUCCESS(rv, rv); + + // Search for a Java installation in the default install location. + return SearchDefault(); +} + +NS_IMETHODIMP +nsJVMConfigManagerUnix::SetCurrentJVMConfig(nsIJVMConfig* aJVMConfig) +{ + NS_ENSURE_ARG_POINTER(aJVMConfig); + + nsCOMPtr srcFile; + nsresult rv = aJVMConfig->GetMozillaPluginPath(getter_AddRefs(srcFile)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr pluginDir; + rv = NS_GetSpecialDirectory(NS_APP_PLUGINS_DIR, getter_AddRefs(pluginDir)); + NS_ENSURE_SUCCESS(rv, rv); + + PRBool hasPermission = PR_FALSE; + pluginDir->IsWritable(&hasPermission); + if (!hasPermission) { + return NS_ERROR_FAILURE; + } + + nsAutoString fileName; + srcFile->GetLeafName(fileName); + nsCOMPtr destFile(do_QueryInterface(pluginDir)); + if (TestExists(destFile, fileName)) + destFile->Remove(PR_FALSE); + + nsCAutoString srcFileName; + rv = srcFile->GetNativePath(srcFileName); + NS_ENSURE_SUCCESS(rv, rv); + + nsCAutoString destFileName; + destFile->GetNativePath(destFileName); + NS_ENSURE_SUCCESS(rv, rv); + + PRInt16 result = 0; + result = symlink(srcFileName.get(), destFileName.get()); + + return result >= 0 ? NS_OK : NS_ERROR_FAILURE; +} + +nsresult +nsJVMConfigManagerUnix::InitJVMConfigList(nsILineInputStream* aGlobal, + nsILineInputStream* aPrivate) +{ + nsresult rv = NS_OK; + + if (aGlobal) { + rv = ParseStream(aGlobal); + NS_ENSURE_SUCCESS(rv, rv); + } + + if (aPrivate) { + rv = ParseStream(aPrivate); + NS_ENSURE_SUCCESS(rv, rv); + } + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::GetLineInputStream(nsIFile* aFile, + nsILineInputStream** _retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + + nsresult rv = NS_OK; + + nsCOMPtr file(do_QueryInterface(aFile, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr + fileStream(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = fileStream->Init(file, -1, -1, PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr lineStream(do_QueryInterface(fileStream, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + *_retval = lineStream; + NS_IF_ADDREF(*_retval); + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::ParseStream(nsILineInputStream* aStream) +{ + NS_ENSURE_ARG_POINTER(aStream); + + PRBool notEOF = PR_TRUE; + + nsAutoString lineBuffer; + do { + nsAutoString line; + nsresult rv = aStream->ReadLine(line, ¬EOF); + NS_ENSURE_SUCCESS(rv, rv); + + PRInt32 slashOffset, equalsOffset; + slashOffset = line.FindChar('\\'); + equalsOffset = line.FindChar('='); + + // Since one java installation contains several key/value pair + // and they are separeted by '\'. We need to join them together + // to a single line and then parse it. + if (slashOffset != kNotFound && equalsOffset != kNotFound) { + // This means the line not finished, we need to append it to buffer + lineBuffer.Append(Substring(line, 0, slashOffset)); + } else if (slashOffset == kNotFound && equalsOffset != kNotFound) { + // This should the last one of a line. Append it to line + // and then we can Parse it and get necessary information + lineBuffer.Append(line); + ParseLine(lineBuffer); + } else { + // Start of a new line + lineBuffer.Truncate(); + } + } while (notEOF); + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::ParseLine(nsAString& aLine) +{ +#if (NS_COMPILER_GNUC3) + nsAutoString compiler; + GetValueFromLine(aLine, "compiler", compiler); + + NS_ENSURE_TRUE(compiler.Find("gcc32") != kNotFound, NS_OK); +#endif + + nsAutoString version; + GetValueFromLine(aLine, "version", version); + + nsAutoString type; + GetValueFromLine(aLine, "type", type); + + nsAutoString os; + GetValueFromLine(aLine, "os", os); + + nsAutoString arch; + GetValueFromLine(aLine, "arch", arch); + + nsAutoString pathStr; + GetValueFromLine(aLine, "path", pathStr); + + nsAutoString mozillaPluginPath; + GetMozillaPluginPath(aLine, mozillaPluginPath); + + NS_ENSURE_TRUE(!mozillaPluginPath.IsEmpty(), NS_OK); + + nsAutoString description; + GetValueFromLine(aLine, "description", description); + description.Trim("\""); + + // Test whether the plugin file is existing. + nsresult rv = NS_OK; + nsCOMPtr + testPath(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsAutoString testPathStr(pathStr); + if (type.Equals(NS_LITERAL_STRING("jdk"))) + testPathStr.Append(NS_LITERAL_STRING("/jre")); + + testPathStr.Append(mozillaPluginPath); + testPath->InitWithPath(testPathStr); + + // If the file doesn't exists, we just return NS_OK + PRBool exists; + testPath->Exists(&exists); + NS_ENSURE_TRUE(exists, NS_OK); + + nsCOMPtr mozPluginPath(do_QueryInterface(testPath, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr + path(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + path->InitWithPath(pathStr); + + // We use java home as the key because one home + // will contain only one java installation. + // This could make sure we don't duplicate the config info in the list + nsStringKey key(pathStr); + nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, + mJVMConfigList.Get(&key)); + + // Only create it and add the config to list if it doesn't exist. + if (!config) { + config = new nsJVMConfig(version, type, os, arch, path, + mozPluginPath, description); + NS_ENSURE_TRUE(config, NS_ERROR_OUT_OF_MEMORY); + mJVMConfigList.Put(&key, NS_STATIC_CAST(void *, config)); + NS_ADDREF(config); + } + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::GetMozillaPluginPath(nsAString& aLine, + nsAString& _retval) +{ + nsCAutoString agentVersion; + nsresult rv = GetAgentVersion(agentVersion); + NS_ENSURE_SUCCESS(rv, rv); + + // Get mozilla plugin path from key + // mozilla.plugin.path + // should like this: "1.2", "1.3" + nsCAutoString key("mozilla"); + key.Append(agentVersion); + key.Append(".plugin.path"); + + GetValueFromLine(aLine, key.get(), _retval); + + if (!_retval.IsEmpty()) return NS_OK; + + nsAutoString versionStr; + rv = GetNSVersion(versionStr); + NS_ENSURE_SUCCESS(rv, rv); + + key.AssignWithConversion(versionStr); + key.Append(".plugin.path"); + + GetValueFromLine(aLine, key.get(), _retval); + + // Fall back to use ns610.plugin.path if _retval is still empty. + if (_retval.IsEmpty()) + GetValueFromLine(aLine, "ns610.plugin.path", _retval); + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::GetAgentVersion(nsCAutoString& _retval) +{ + nsresult rv = NS_OK; + + nsCOMPtr http = do_GetService(kHttpHandlerCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCAutoString userAgent; + rv = http->GetUserAgent(userAgent); + NS_ENSURE_SUCCESS(rv, rv); + PRInt32 rvOffset = userAgent.Find("rv:"); + + if (rvOffset != kNotFound) + _retval.Assign(Substring(userAgent, rvOffset + 3, 3)); + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::GetAgentVersion(float* _retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + nsresult rv = NS_OK; + + nsCAutoString agentVersion; + GetAgentVersion(agentVersion); + nsCOMPtr p = + do_CreateInstance(NS_VARIANT_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + rv = p->SetAsACString(agentVersion); + NS_ENSURE_SUCCESS(rv, rv); + + return p->GetAsFloat(_retval); +} + +PRBool +nsJVMConfigManagerUnix::GetValueFromLine(nsAString& aLine, const char* aKey, + nsAString& _retval) +{ + _retval.Truncate(); + + nsAutoString line(aLine); + // Find the offset of the given key in the line + PRInt32 keyOffset = line.Find(aKey); + + // make sure the key exists in the line. + NS_ENSURE_TRUE(keyOffset != kNotFound, PR_FALSE); + + // Find '=' right after the key + PRInt32 equalsOffset = aLine.FindChar('=', keyOffset); + NS_ENSURE_TRUE(equalsOffset != kNotFound, PR_FALSE); + + // Find '|' which is the terminal of a pair of key/value + PRInt32 lineOffset = aLine.FindChar('|', equalsOffset); + lineOffset = lineOffset != kNotFound ? lineOffset : aLine.Length(); + + // OK, we separate the value from the line between '=' and '|' + nsAutoString value(Substring(aLine, + equalsOffset + 1, + lineOffset - equalsOffset -1)); + + // Removing the leading/trailing spaces + value.Trim(" "); + _retval = value; + return PR_TRUE; +} + +nsresult +nsJVMConfigManagerUnix::GetNSVersion(nsAString& _retval) +{ + float version; + nsresult rv = GetAgentVersion(&version); + NS_ENSURE_SUCCESS(rv, rv); + + // Check mozilla's version + // ns7 is for mozilla1.3 or later + // ns610 is for earlier version of mozilla. + if (version >= 1.3) { + _retval.Assign(NS_LITERAL_STRING("ns7")); + } else { + _retval.Assign(NS_LITERAL_STRING("ns610")); + } + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::SearchDefault() +{ +#ifdef SPARC + const char* defaultLocationName = "java.default_java_location_solaris"; +#else + const char* defaultLocationName = "java.default_java_location_others"; +#endif + + nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); + NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE); + + nsXPIDLCString defaultLocationXPIDLValue; + prefs->GetCharPref(defaultLocationName, + getter_Copies(defaultLocationXPIDLValue)); + + NS_ConvertUTF8toUCS2 defaultLocation(defaultLocationXPIDLValue); + +#ifdef SPARC + // On Solaris, the default location is the java home + return AddDirectory(defaultLocation); +#else + // On Linux and other platform, + // the default location can contain multiple java installations + return SearchDirectory(defaultLocation); +#endif +} + +nsresult +nsJVMConfigManagerUnix::SearchDirectory(nsAString& aDirName) +{ + nsresult rv = NS_OK; + + nsCOMPtr + localDir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = localDir->InitWithPath(aDirName); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr dir(do_QueryInterface(localDir, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr entries; + rv = dir->GetDirectoryEntries(getter_AddRefs(entries)); + NS_ENSURE_SUCCESS(rv, rv); + + PRBool hasMoreElements; + entries->HasMoreElements(&hasMoreElements); + while (hasMoreElements) { + nsCOMPtr next; + rv = entries->GetNext(getter_AddRefs(next)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr entry(do_QueryInterface(next, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + AddDirectory(entry); + entries->HasMoreElements(&hasMoreElements); + } + + return NS_OK; +} + +nsresult +nsJVMConfigManagerUnix::AddDirectory(nsIFile* aHomeDir) +{ + NS_ENSURE_ARG_POINTER(aHomeDir); + nsAutoString homeDirName; + aHomeDir->GetPath(homeDirName); + return AddDirectory(homeDirName); +} + +nsresult +nsJVMConfigManagerUnix::AddDirectory(nsAString& aHomeDirName) +{ + nsresult rv = NS_OK; + + nsAutoString type; + nsAutoString mozillaPluginPath; + + nsCOMPtr + testPath(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + testPath->InitWithPath(aHomeDirName); + testPath->Append(NS_LITERAL_STRING("jre")); + + PRBool exists; + testPath->Exists(&exists); + if (exists) { + type.Assign(NS_LITERAL_STRING("jdk")); + } else { + type.Assign(NS_LITERAL_STRING("jre")); + testPath->InitWithPath(aHomeDirName); + } + + testPath->Append(NS_LITERAL_STRING("plugin")); + + // Get Arch. "sparc" or "i386" + nsAutoString arch; + NS_ENSURE_TRUE(TestArch(testPath, arch), NS_OK); + + // Get NS Version. "ns610" or "ns7" + nsAutoString nsVersion; + NS_ENSURE_TRUE(TestNSVersion(testPath, nsVersion), NS_OK); + + nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); + NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE); + + nsCAutoString javaLibName("java.java_plugin_library_name"); + nsXPIDLCString javaLibNameXPIDLValue; + prefs->GetCharPref(javaLibName.get(), + getter_Copies(javaLibNameXPIDLValue)); + + char* temp = PR_GetLibraryName(nsnull, javaLibNameXPIDLValue.get()); + nsCAutoString pluginFileName(temp); + testPath->AppendNative(pluginFileName); + PR_FreeLibraryName(temp); + + // If the plugin file doesn't exist, we just return NS_OK + testPath->Exists(&exists); + NS_ENSURE_TRUE(exists, NS_OK); + + nsCOMPtr mozPluginPath(do_QueryInterface(testPath, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr + path(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + path->InitWithPath(aHomeDirName); + + nsAutoString version; + path->GetLeafName(version); + + nsStringKey key(aHomeDirName); + nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, + mJVMConfigList.Get(&key)); + if (!config) { + config = new nsJVMConfig(version, type, nsString(), arch, path, + mozPluginPath, nsString()); + NS_ENSURE_TRUE(config, NS_ERROR_OUT_OF_MEMORY); + mJVMConfigList.Put(&key, NS_STATIC_CAST(void *, config)); + NS_ADDREF(config); + } + + return NS_OK; +} + +PRBool +nsJVMConfigManagerUnix::TestArch(nsILocalFile* aPluginPath, nsAString& aArch) +{ +#ifdef SPARC + aArch.Assign(NS_LITERAL_STRING("sparc")); +#else + aArch.Assign(NS_LITERAL_STRING("i386")); +#endif + return TestExists(aPluginPath, aArch); +} + +PRBool +nsJVMConfigManagerUnix::TestNSVersion(nsILocalFile* aArchPath, + nsAString& aNSVersion) +{ + nsAutoString versionStr; + nsresult rv = GetNSVersion(versionStr); + NS_ENSURE_SUCCESS(rv, PR_FALSE); + + aNSVersion.Assign(versionStr); +#if (NS_COMPILER_GNUC3) + aNSVersion.Append(NS_LITERAL_STRING("-gcc32")); +#endif + return TestExists(aArchPath, aNSVersion); +} + +PRBool +nsJVMConfigManagerUnix::TestExists(nsILocalFile* aBaseDir, nsAString& aSubName) +{ + NS_ENSURE_ARG_POINTER(aBaseDir); + + aBaseDir->Append(aSubName); + PRBool exists; + aBaseDir->Exists(&exists); + + return exists; +} + diff --git a/modules/oji/src/nsJVMConfigManagerUnix.h b/modules/oji/src/nsJVMConfigManagerUnix.h new file mode 100644 index 00000000000..95567e6f1c8 --- /dev/null +++ b/modules/oji/src/nsJVMConfigManagerUnix.h @@ -0,0 +1,148 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is Sun Microsystems. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pete Zha (original author) + * + * 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 ***** */ + +#ifndef nsJVMConfigManagerUnix_h___ +#define nsJVMConfigManagerUnix_h___ + +#include "nsJVMConfigManager.h" +#include "nsString.h" +#include "nsILineInputStream.h" +#include "nsHashtable.h" +#include "nsIFile.h" +#include "nsILocalFile.h" +#include "nsIFileStreams.h" +#include "nsArray.h" + +class nsJVMConfigManagerUnix : public nsIJVMConfigManager +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIJVMCONFIGMANAGER + + nsJVMConfigManagerUnix(); + virtual ~nsJVMConfigManagerUnix(); + +protected: + nsresult InitJVMConfigList(); + + void ClearJVMConfigList(); + + nsresult InitJVMConfigList(nsILineInputStream* aGlobal, + nsILineInputStream* aPrivate); + + /** + * Parse a stream for information about Java installation(s). + */ + nsresult ParseStream(nsILineInputStream* aStream); + + /** + * Parse a line for information about a Java installation, and ensure its + * JVMConfig is in our JVMConfigList." + * A line looks like this: + * "version=1.4.2 | type=jre | os=linux | arch=i386 | ......" + */ + + nsresult ParseLine(nsAString& aLine); + + /** + * Search for Java installations in the default location. + */ + nsresult SearchDefault(); + + /** + * Search a specific directory to see if there are Java installations. + */ + nsresult SearchDirectory(nsAString& aDirName); + + /** + * Add a directory to the Java configuration list if + * it contains a java installation. + */ + nsresult AddDirectory(nsIFile* aHomeDir); + + nsresult AddDirectory(nsAString& aHomeDirName); + + /** + * Get a value by specific key from the line. A line should look like this: + * key=value | key2=value2 | key3=value3 + * Please see the proposal in bug 185000 for more detail about the format. + */ + static PRBool GetValueFromLine(nsAString& aLine, const char* aKey, + nsAString& _retval); + + static nsresult GetLineInputStream(nsIFile* aFile, + nsILineInputStream** _retval); + + /** + * Get value of mozilla.plugin.path from a line + */ + static nsresult GetMozillaPluginPath(nsAString& aLine, nsAString& _retval); + + /** + * Get agent version by string + */ + static nsresult GetAgentVersion(nsCAutoString& _retval); + + /** + * Get agent version by float + */ + static nsresult GetAgentVersion(float* _retval); + + static nsresult GetNSVersion(nsAString& _retval); + + /** + * Check for existing arch directory. + */ + static PRBool TestArch(nsILocalFile* aPluginPath, nsAString& aArch); + + /** + * Check for existing NS version directory. + */ + static PRBool TestNSVersion(nsILocalFile* aArchPath, nsAString& aNSVersion); + + /** + * Test if a specific node in the base directory exists. + */ + static PRBool TestExists(nsILocalFile* aBaseDir, nsAString& aSubName); + + /** + * The table to store the config list. + */ + nsHashtable mJVMConfigList; +}; + +#endif // nsJVMConfigManagerUnix_h___ diff --git a/xpfe/components/prefwindow/resources/content/pref-advanced.xul b/xpfe/components/prefwindow/resources/content/pref-advanced.xul index 9a01bcd6381..82c1f53db30 100644 --- a/xpfe/components/prefwindow/resources/content/pref-advanced.xul +++ b/xpfe/components/prefwindow/resources/content/pref-advanced.xul @@ -41,6 +41,7 @@ ftpCheck(false); turboCheck(); sysPrefCheck(); + initJVMConfigList(); } function ftpCheck(setFocus) { @@ -161,7 +162,8 @@ + prefstring="security.enable_java" + oncommand="javaCheck(true)"/> @@ -177,6 +179,187 @@ + + + + + + + + + + + + + + + +