Back to index

lightning-sunbird  0.9+nobinonly
nsJVMConfigManagerUnix.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 Sun Microsystems.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Pete Zha <pete.zha@sun.com> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdlib.h>
00039 #include "nsJVMConfigManagerUnix.h"
00040 #include "nsAppDirectoryServiceDefs.h"
00041 #include "nsDirectoryServiceDefs.h"
00042 #include "nsIPrefService.h"
00043 #include "nsIPrefBranch.h"
00044 #include "nsReadableUtils.h"
00045 #include "prprf.h"
00046 #include "nsNetCID.h"
00047 #include "nsIHttpProtocolHandler.h"
00048 #include "nsIVariant.h"
00049 #include "nsISimpleEnumerator.h"
00050 #include <stdio.h>  /* OSF/1 requires this before grp.h, so put it first */
00051 #include <unistd.h>
00052 
00053 #define NS_COMPILER_GNUC3 defined(__GXX_ABI_VERSION) && \
00054                           (__GXX_ABI_VERSION >= 102) /* G++ V3 ABI */
00055 
00056 static NS_DEFINE_CID(kHttpHandlerCID, NS_HTTPPROTOCOLHANDLER_CID);
00057 
00058 // Implementation of nsJVMConfigManagerUnix
00059 NS_IMPL_ISUPPORTS1(nsJVMConfigManagerUnix, nsIJVMConfigManager)
00060 
00061 nsJVMConfigManagerUnix::nsJVMConfigManagerUnix()
00062 {
00063     InitJVMConfigList();
00064 }
00065 
00066 PR_STATIC_CALLBACK(PRBool)
00067 FreeJVMConfig(nsHashKey *aKey, void *aData, void* aClosure)
00068 {
00069     nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, aData);
00070 
00071     NS_IF_RELEASE(config);
00072 
00073     return PR_TRUE;
00074 }
00075 
00076 PR_STATIC_CALLBACK(PRBool)
00077 AppendJVMConfig(nsHashKey *aKey, void *aData, void* aClosure)
00078 {
00079     nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *, aData);
00080     nsIMutableArray *array = NS_STATIC_CAST(nsIMutableArray *, aClosure);
00081     NS_ENSURE_TRUE(config && array, PR_FALSE);
00082 
00083     array->AppendElement(config, PR_FALSE);
00084     return PR_TRUE;
00085 }
00086 
00087 nsJVMConfigManagerUnix::~nsJVMConfigManagerUnix()
00088 {
00089     ClearJVMConfigList();
00090 }
00091 
00092 void
00093 nsJVMConfigManagerUnix::ClearJVMConfigList()
00094 {
00095     if (mJVMConfigList.Count() > 0) {
00096         mJVMConfigList.Reset(FreeJVMConfig);
00097     }
00098 }
00099 
00100 NS_IMETHODIMP
00101 nsJVMConfigManagerUnix::GetJVMConfigList(nsIArray **_retval)
00102 {
00103     NS_ENSURE_ARG_POINTER(_retval);
00104 
00105     ClearJVMConfigList();
00106     InitJVMConfigList();
00107 
00108     nsCOMPtr<nsIMutableArray> array;
00109     nsresult rv = NS_NewArray(getter_AddRefs(array));
00110     NS_ENSURE_SUCCESS(rv, rv);
00111 
00112     if (mJVMConfigList.Count() > 0) {
00113         mJVMConfigList.Enumerate(AppendJVMConfig,
00114                                  NS_STATIC_CAST(void *, array));
00115         *_retval = NS_STATIC_CAST(nsIArray *, array);
00116         NS_IF_ADDREF(*_retval);
00117     } else
00118         *_retval = nsnull;
00119 
00120     return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP
00124 nsJVMConfigManagerUnix::InitJVMConfigList()
00125 {
00126     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
00127     NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE);
00128 
00129     nsCOMPtr<nsILocalFile> globalFile;
00130     prefs->GetComplexValue("java.global_java_version_file",
00131                            NS_GET_IID(nsILocalFile),
00132                            getter_AddRefs(globalFile));
00133 
00134     nsCOMPtr<nsILocalFile> privateFile;
00135     prefs->GetComplexValue("java.private_java_version_file",
00136                            NS_GET_IID(nsILocalFile),
00137                            getter_AddRefs(privateFile));
00138 
00139     nsCOMPtr<nsILineInputStream> globalStream;
00140     nsresult rv = GetLineInputStream(globalFile, getter_AddRefs(globalStream));
00141     NS_ENSURE_TRUE(NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_NOT_FOUND, rv);
00142 
00143     nsCOMPtr<nsILineInputStream> privateStream;
00144     rv = GetLineInputStream(privateFile, getter_AddRefs(privateStream));
00145     NS_ENSURE_TRUE(NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_NOT_FOUND, rv);
00146 
00147     rv = InitJVMConfigList(globalStream, privateStream);
00148     NS_ENSURE_SUCCESS(rv, rv);
00149 
00150     // Search for a Java installation in the default install location.
00151     return SearchDefault();
00152 }
00153 
00154 NS_IMETHODIMP
00155 nsJVMConfigManagerUnix::SetCurrentJVMConfig(nsIJVMConfig* aJVMConfig)
00156 {
00157     NS_ENSURE_ARG_POINTER(aJVMConfig);
00158 
00159     nsCOMPtr<nsIFile> srcFile;
00160     nsresult rv = aJVMConfig->GetMozillaPluginPath(getter_AddRefs(srcFile));
00161     NS_ENSURE_SUCCESS(rv, rv);
00162 
00163     nsCOMPtr<nsIFile> pluginDir;
00164     rv = NS_GetSpecialDirectory(NS_APP_PLUGINS_DIR, getter_AddRefs(pluginDir));
00165     NS_ENSURE_SUCCESS(rv, rv);
00166 
00167     PRBool hasPermission = PR_FALSE;
00168     pluginDir->IsWritable(&hasPermission);
00169     if (!hasPermission) {
00170         return NS_ERROR_FAILURE;
00171     }
00172 
00173     nsAutoString fileName;
00174     srcFile->GetLeafName(fileName);
00175     nsCOMPtr<nsILocalFile> destFile(do_QueryInterface(pluginDir));
00176     if (TestExists(destFile, fileName))
00177         destFile->Remove(PR_FALSE);
00178 
00179     nsCAutoString srcFileName;
00180     rv = srcFile->GetNativePath(srcFileName);
00181     NS_ENSURE_SUCCESS(rv, rv);
00182 
00183     nsCAutoString destFileName;
00184     destFile->GetNativePath(destFileName);
00185     NS_ENSURE_SUCCESS(rv, rv);
00186 
00187     PRInt16 result = 0;
00188     result = symlink(srcFileName.get(), destFileName.get());
00189 
00190     return result >= 0 ? NS_OK : NS_ERROR_FAILURE;
00191 }
00192 
00193 nsresult
00194 nsJVMConfigManagerUnix::InitJVMConfigList(nsILineInputStream* aGlobal,
00195                                           nsILineInputStream* aPrivate)
00196 {
00197     nsresult rv = NS_OK;
00198 
00199     if (aGlobal) {
00200         rv = ParseStream(aGlobal);
00201         NS_ENSURE_SUCCESS(rv, rv);
00202     }
00203 
00204     if (aPrivate) {
00205         rv = ParseStream(aPrivate);
00206         NS_ENSURE_SUCCESS(rv, rv);
00207     }
00208 
00209     return NS_OK;
00210 }
00211 
00212 nsresult
00213 nsJVMConfigManagerUnix::GetLineInputStream(nsIFile* aFile,
00214                                            nsILineInputStream** _retval)
00215 {
00216     NS_ENSURE_ARG_POINTER(_retval);
00217     
00218     nsresult rv = NS_OK;
00219     
00220     nsCOMPtr<nsILocalFile> file(do_QueryInterface(aFile, &rv));
00221     NS_ENSURE_SUCCESS(rv, rv);
00222 
00223     nsCOMPtr<nsIFileInputStream>
00224         fileStream(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv));
00225     NS_ENSURE_SUCCESS(rv, rv);
00226 
00227     rv = fileStream->Init(file, -1, -1, PR_FALSE);
00228     NS_ENSURE_SUCCESS(rv, rv);
00229 
00230     nsCOMPtr<nsILineInputStream> lineStream(do_QueryInterface(fileStream, &rv));
00231     NS_ENSURE_SUCCESS(rv, rv);
00232 
00233     *_retval = lineStream;
00234     NS_IF_ADDREF(*_retval);
00235 
00236     return NS_OK;
00237 }
00238 
00239 nsresult
00240 nsJVMConfigManagerUnix::ParseStream(nsILineInputStream* aStream)
00241 {
00242     NS_ENSURE_ARG_POINTER(aStream);
00243 
00244     PRBool notEOF = PR_TRUE;
00245 
00246     nsAutoString lineBuffer;
00247     do {
00248         nsAutoString line;
00249         nsCAutoString cLine;
00250         nsresult rv = aStream->ReadLine(cLine, &notEOF);
00251         NS_ENSURE_SUCCESS(rv, rv);
00252         CopyASCIItoUTF16(cLine, line);
00253 
00254         PRInt32 slashOffset, equalsOffset;
00255         slashOffset = line.FindChar('\\');
00256         equalsOffset = line.FindChar('=');
00257 
00258         // Since one java installation contains several key/value pair
00259         // and they are separeted by '\'. We need to join them together
00260         // to a single line and then parse it.
00261         if (slashOffset != kNotFound && equalsOffset != kNotFound) {
00262             // This means the line not finished, we need to append it to buffer
00263             lineBuffer.Append(Substring(line, 0, slashOffset));
00264         } else if (slashOffset == kNotFound && equalsOffset != kNotFound) {
00265             // This should the last one of a line. Append it to line
00266             // and then we can Parse it and get necessary information
00267             lineBuffer.Append(line);
00268             ParseLine(lineBuffer);
00269         } else {
00270             // Start of a new line
00271             lineBuffer.Truncate();
00272         }
00273     } while (notEOF);
00274 
00275     return NS_OK;
00276 }
00277 
00278 nsresult
00279 nsJVMConfigManagerUnix::ParseLine(nsAString& aLine)
00280 {
00281 #if (NS_COMPILER_GNUC3)
00282     nsAutoString compiler;
00283     GetValueFromLine(aLine, "compiler", compiler);
00284 
00285     NS_ENSURE_TRUE(compiler.Find("gcc32") != kNotFound, NS_OK);
00286 #endif
00287 
00288     nsAutoString version;
00289     GetValueFromLine(aLine, "version", version);
00290     
00291     nsAutoString type;
00292     GetValueFromLine(aLine, "type", type);
00293     
00294     nsAutoString os;
00295     GetValueFromLine(aLine, "os", os);
00296     
00297     nsAutoString arch;
00298     GetValueFromLine(aLine, "arch", arch);
00299     
00300     nsAutoString pathStr;
00301     GetValueFromLine(aLine, "path", pathStr);
00302 
00303     nsAutoString mozillaPluginPath;
00304     GetMozillaPluginPath(aLine, mozillaPluginPath);
00305 
00306     NS_ENSURE_TRUE(!mozillaPluginPath.IsEmpty(), NS_OK);
00307 
00308     nsAutoString description;
00309     GetValueFromLine(aLine, "description", description);
00310     description.Trim("\"");
00311 
00312     // Test whether the plugin file is existing.
00313     nsresult rv = NS_OK;
00314     nsCOMPtr<nsILocalFile>
00315         testPath(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00316     NS_ENSURE_SUCCESS(rv, rv);
00317 
00318     nsAutoString testPathStr(pathStr);
00319     if (type.EqualsLiteral("jdk"))
00320         testPathStr.AppendLiteral("/jre");
00321 
00322     testPathStr.Append(mozillaPluginPath);
00323     testPath->InitWithPath(testPathStr);
00324 
00325     // If the file doesn't exists, we just return NS_OK
00326     PRBool exists;
00327     testPath->Exists(&exists);
00328     NS_ENSURE_TRUE(exists, NS_OK);
00329 
00330     nsCOMPtr<nsIFile> mozPluginPath(do_QueryInterface(testPath, &rv));
00331     NS_ENSURE_SUCCESS(rv, rv);
00332 
00333     nsCOMPtr<nsILocalFile>
00334         path(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00335     path->InitWithPath(pathStr);
00336 
00337     // We use java home as the key because one home
00338     // will contain only one java installation.
00339     // This could make sure we don't duplicate the config info in the list
00340     nsStringKey key(pathStr);
00341     nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *,
00342                                          mJVMConfigList.Get(&key));
00343 
00344     // Only create it and add the config to list if it doesn't exist.
00345     if (!config) {
00346         config = new nsJVMConfig(version, type, os, arch, path,
00347                                  mozPluginPath, description);
00348         NS_ENSURE_TRUE(config, NS_ERROR_OUT_OF_MEMORY);
00349         mJVMConfigList.Put(&key, NS_STATIC_CAST(void *, config));
00350         NS_ADDREF(config);
00351     }
00352     
00353     return NS_OK;
00354 }
00355 
00356 nsresult
00357 nsJVMConfigManagerUnix::GetMozillaPluginPath(nsAString& aLine,
00358                                              nsAString& _retval)
00359 {
00360     nsCAutoString agentVersion;
00361     nsresult rv = GetAgentVersion(agentVersion);
00362     NS_ENSURE_SUCCESS(rv, rv);
00363 
00364     // Get mozilla plugin path from key
00365     // mozilla<version>.plugin.path
00366     // <version> should like this: "1.2", "1.3"
00367     nsCAutoString key("mozilla");
00368     key.Append(agentVersion);
00369     key.Append(".plugin.path");
00370 
00371     GetValueFromLine(aLine, key.get(), _retval);
00372 
00373     if (!_retval.IsEmpty()) return NS_OK;
00374 
00375     nsAutoString versionStr;
00376     rv = GetNSVersion(versionStr);
00377     NS_ENSURE_SUCCESS(rv, rv);
00378 
00379     key.AssignWithConversion(versionStr);
00380     key.Append(".plugin.path");
00381 
00382     GetValueFromLine(aLine, key.get(), _retval);
00383 
00384     // Fall back to use ns610.plugin.path if _retval is still empty.
00385     if (_retval.IsEmpty())
00386         GetValueFromLine(aLine, "ns610.plugin.path", _retval);
00387 
00388     return NS_OK;
00389 }
00390 
00391 nsresult
00392 nsJVMConfigManagerUnix::GetAgentVersion(nsCAutoString& _retval)
00393 {
00394     nsresult rv = NS_OK;
00395 
00396     nsCOMPtr<nsIHttpProtocolHandler> http = do_GetService(kHttpHandlerCID, &rv);
00397     NS_ENSURE_SUCCESS(rv, rv);
00398 
00399     nsCAutoString userAgent;
00400     rv = http->GetUserAgent(userAgent);
00401     NS_ENSURE_SUCCESS(rv, rv);
00402     PRInt32 rvOffset = userAgent.Find("rv:");
00403 
00404     if (rvOffset != kNotFound)
00405         _retval.Assign(Substring(userAgent, rvOffset + 3, 3));
00406 
00407     return NS_OK;
00408 }
00409 
00410 nsresult
00411 nsJVMConfigManagerUnix::GetAgentVersion(float* _retval)
00412 {
00413     NS_ENSURE_ARG_POINTER(_retval);
00414     nsresult rv = NS_OK;
00415 
00416     nsCAutoString agentVersion;
00417     GetAgentVersion(agentVersion);
00418     nsCOMPtr <nsIWritableVariant> p =
00419         do_CreateInstance(NS_VARIANT_CONTRACTID, &rv);
00420     NS_ENSURE_SUCCESS(rv, rv);
00421     
00422     rv = p->SetAsACString(agentVersion);
00423     NS_ENSURE_SUCCESS(rv, rv);
00424 
00425     return p->GetAsFloat(_retval);
00426 }
00427 
00428 PRBool
00429 nsJVMConfigManagerUnix::GetValueFromLine(nsAString& aLine, const char* aKey,
00430                                          nsAString& _retval)
00431 {
00432     _retval.Truncate();
00433 
00434     nsAutoString line(aLine);
00435     // Find the offset of the given key in the line
00436     PRInt32 keyOffset = line.Find(aKey);
00437 
00438     // make sure the key exists in the line.
00439     NS_ENSURE_TRUE(keyOffset != kNotFound, PR_FALSE);
00440 
00441     // Find '=' right after the key
00442     PRInt32 equalsOffset = aLine.FindChar('=', keyOffset);
00443     NS_ENSURE_TRUE(equalsOffset != kNotFound, PR_FALSE);
00444 
00445     // Find '|' which is the terminal of a pair of key/value
00446     PRInt32 lineOffset = aLine.FindChar('|', equalsOffset);
00447     lineOffset = lineOffset != kNotFound ? lineOffset : aLine.Length();
00448 
00449     // OK, we separate the value from the line between '=' and '|'
00450     nsAutoString value(Substring(aLine,
00451                                  equalsOffset + 1,
00452                                  lineOffset - equalsOffset -1));
00453 
00454     // Removing the leading/trailing spaces
00455     value.Trim(" ");
00456     _retval = value;
00457     return PR_TRUE;
00458 }
00459 
00460 nsresult
00461 nsJVMConfigManagerUnix::GetNSVersion(nsAString& _retval)
00462 {
00463     float version;
00464     nsresult rv = GetAgentVersion(&version);
00465     NS_ENSURE_SUCCESS(rv, rv);
00466 
00467     // Check mozilla's version
00468     // ns7 is for mozilla1.3 or later
00469     // ns610 is for earlier version of mozilla.
00470     if (version >= 1.3) {
00471         _retval.AssignLiteral("ns7");
00472     } else {
00473         _retval.AssignLiteral("ns610");
00474     }
00475 
00476     return NS_OK;
00477 }
00478 
00479 nsresult
00480 nsJVMConfigManagerUnix::SearchDefault()
00481 {
00482 #ifdef SPARC
00483     const char* defaultLocationName = "java.default_java_location_solaris";
00484 #else
00485     const char* defaultLocationName = "java.default_java_location_others";
00486 #endif
00487 
00488     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
00489     NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE);
00490 
00491     nsXPIDLCString defaultLocationXPIDLValue;
00492     prefs->GetCharPref(defaultLocationName,
00493                        getter_Copies(defaultLocationXPIDLValue));
00494 
00495     NS_ConvertUTF8toUCS2 defaultLocation(defaultLocationXPIDLValue);
00496 
00497 #ifdef SPARC
00498     // On Solaris, the default location is the java home
00499     return AddDirectory(defaultLocation);
00500 #else
00501     // On Linux and other platform,
00502     // the default location can contain multiple java installations
00503     return SearchDirectory(defaultLocation);
00504 #endif
00505 }
00506 
00507 nsresult
00508 nsJVMConfigManagerUnix::SearchDirectory(nsAString& aDirName)
00509 {
00510     nsresult rv = NS_OK;
00511 
00512     nsCOMPtr<nsILocalFile>
00513         localDir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00514     NS_ENSURE_SUCCESS(rv, rv);
00515 
00516     rv = localDir->InitWithPath(aDirName);
00517     NS_ENSURE_SUCCESS(rv, rv);
00518 
00519     nsCOMPtr<nsIFile> dir(do_QueryInterface(localDir, &rv));
00520     NS_ENSURE_SUCCESS(rv, rv);
00521 
00522     nsCOMPtr<nsISimpleEnumerator> entries;
00523     rv = dir->GetDirectoryEntries(getter_AddRefs(entries));
00524     NS_ENSURE_SUCCESS(rv, rv);
00525 
00526     PRBool hasMoreElements;
00527     entries->HasMoreElements(&hasMoreElements);
00528     while (hasMoreElements) {
00529         nsCOMPtr<nsISupports> next;
00530         rv = entries->GetNext(getter_AddRefs(next));
00531         NS_ENSURE_SUCCESS(rv, rv);
00532 
00533         nsCOMPtr<nsIFile> entry(do_QueryInterface(next, &rv));
00534         NS_ENSURE_SUCCESS(rv, rv);
00535 
00536         AddDirectory(entry);
00537         entries->HasMoreElements(&hasMoreElements);
00538     }
00539     
00540     return NS_OK;
00541 }
00542 
00543 nsresult
00544 nsJVMConfigManagerUnix::AddDirectory(nsIFile* aHomeDir)
00545 {
00546     NS_ENSURE_ARG_POINTER(aHomeDir);
00547     nsAutoString homeDirName;
00548     aHomeDir->GetPath(homeDirName);
00549     return AddDirectory(homeDirName);
00550 }
00551 
00552 nsresult
00553 nsJVMConfigManagerUnix::AddDirectory(nsAString& aHomeDirName)
00554 {
00555     nsresult rv = NS_OK;
00556 
00557     nsAutoString type;
00558     nsAutoString mozillaPluginPath;
00559 
00560     nsCOMPtr<nsILocalFile>
00561         testPath(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00562     NS_ENSURE_SUCCESS(rv, rv);
00563 
00564     testPath->InitWithPath(aHomeDirName);
00565     testPath->Append(NS_LITERAL_STRING("jre"));
00566 
00567     PRBool exists;
00568     testPath->Exists(&exists);
00569     if (exists) {
00570         type.AssignLiteral("jdk");
00571     } else {
00572         type.AssignLiteral("jre");
00573         testPath->InitWithPath(aHomeDirName);
00574     }
00575 
00576     testPath->Append(NS_LITERAL_STRING("plugin"));
00577 
00578     // Get Arch. "sparc" or "i386"
00579     nsAutoString arch;
00580     NS_ENSURE_TRUE(TestArch(testPath, arch), NS_OK);
00581 
00582     // Get NS Version. "ns610" or "ns7"
00583     nsAutoString nsVersion;
00584     NS_ENSURE_TRUE(TestNSVersion(testPath, nsVersion), NS_OK);
00585 
00586     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
00587     NS_ENSURE_TRUE(prefs, NS_ERROR_FAILURE);
00588 
00589     nsCAutoString javaLibName("java.java_plugin_library_name");
00590     nsXPIDLCString javaLibNameXPIDLValue;
00591     prefs->GetCharPref(javaLibName.get(),
00592                         getter_Copies(javaLibNameXPIDLValue));
00593 
00594     char* temp = PR_GetLibraryName(nsnull, javaLibNameXPIDLValue.get());
00595     nsCAutoString pluginFileName(temp);
00596     testPath->AppendNative(pluginFileName);
00597     PR_FreeLibraryName(temp);
00598 
00599     // If the plugin file doesn't exist, we just return NS_OK
00600     testPath->Exists(&exists);
00601     NS_ENSURE_TRUE(exists, NS_OK);
00602 
00603     nsCOMPtr<nsIFile> mozPluginPath(do_QueryInterface(testPath, &rv));
00604     NS_ENSURE_SUCCESS(rv, rv);
00605 
00606     nsCOMPtr<nsILocalFile>
00607         path(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00608     NS_ENSURE_SUCCESS(rv, rv);
00609     path->InitWithPath(aHomeDirName);
00610 
00611     nsAutoString version;
00612     path->GetLeafName(version);
00613 
00614     nsStringKey key(aHomeDirName);
00615     nsJVMConfig* config = NS_STATIC_CAST(nsJVMConfig *,
00616                                          mJVMConfigList.Get(&key));
00617     if (!config) {
00618         config = new nsJVMConfig(version, type, EmptyString(), arch, path,
00619                                  mozPluginPath, EmptyString());
00620         NS_ENSURE_TRUE(config, NS_ERROR_OUT_OF_MEMORY);
00621         mJVMConfigList.Put(&key, NS_STATIC_CAST(void *, config));
00622         NS_ADDREF(config);
00623     }
00624 
00625     return NS_OK;
00626 }
00627 
00628 PRBool
00629 nsJVMConfigManagerUnix::TestArch(nsILocalFile* aPluginPath, nsAString& aArch)
00630 {
00631 #ifdef SPARC
00632     aArch.AssignLiteral("sparc");
00633 #else
00634     aArch.AssignLiteral("i386");
00635 #endif
00636     return TestExists(aPluginPath, aArch);
00637 }
00638 
00639 PRBool
00640 nsJVMConfigManagerUnix::TestNSVersion(nsILocalFile* aArchPath,
00641                                       nsAString& aNSVersion)
00642 {
00643     nsAutoString versionStr;
00644     nsresult rv = GetNSVersion(versionStr);
00645     NS_ENSURE_SUCCESS(rv, PR_FALSE);
00646 
00647     aNSVersion.Assign(versionStr);
00648 #if (NS_COMPILER_GNUC3)
00649     aNSVersion.AppendLiteral("-gcc32");
00650 #endif
00651     return TestExists(aArchPath, aNSVersion);
00652 }
00653 
00654 PRBool
00655 nsJVMConfigManagerUnix::TestExists(nsILocalFile* aBaseDir, nsAString& aSubName)
00656 {
00657     NS_ENSURE_ARG_POINTER(aBaseDir);
00658 
00659     aBaseDir->Append(aSubName);
00660     PRBool exists;
00661     aBaseDir->Exists(&exists);
00662 
00663     return exists;
00664 }
00665